Skip to content

Commit ebe9c63

Browse files
committed
Simplify return statements
1 parent 56a0964 commit ebe9c63

20 files changed

+52
-58
lines changed

stan/math/fwd/fun/mdivide_left.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left(
2020
check_square("mdivide_left", "A", A);
2121
check_multiplicable("mdivide_left", "A", A, "b", b);
2222
if (A.size() == 0) {
23-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
23+
return {0, b.cols()};
2424
}
2525

2626
Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
@@ -62,7 +62,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left(
6262
check_square("mdivide_left", "A", A);
6363
check_multiplicable("mdivide_left", "A", A, "b", b);
6464
if (A.size() == 0) {
65-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
65+
return {0, b.cols()};
6666
}
6767

6868
Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
@@ -85,7 +85,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left(
8585
check_square("mdivide_left", "A", A);
8686
check_multiplicable("mdivide_left", "A", A, "b", b);
8787
if (A.size() == 0) {
88-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
88+
return {0, b.cols()};
8989
}
9090

9191
Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());

stan/math/fwd/fun/mdivide_left_tri_low.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left_tri_low(
1919
check_square("mdivide_left_tri_low", "A", A);
2020
check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
2121
if (A.size() == 0) {
22-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
22+
return {0, b.cols()};
2323
}
2424

2525
Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
@@ -63,7 +63,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left_tri_low(
6363
check_square("mdivide_left_tri_low", "A", A);
6464
check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
6565
if (A.size() == 0) {
66-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
66+
return {0, b.cols()};
6767
}
6868

6969
Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
@@ -102,7 +102,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_left_tri_low(
102102
check_square("mdivide_left_tri_low", "A", A);
103103
check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
104104
if (A.size() == 0) {
105-
return Eigen::Matrix<fvar<T>, R1, C2>(0, b.cols());
105+
return {0, b.cols()};
106106
}
107107

108108
Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());

stan/math/fwd/fun/mdivide_right.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_right(
2020
check_square("mdivide_right", "b", b);
2121
check_multiplicable("mdivide_right", "A", A, "b", b);
2222
if (b.size() == 0) {
23-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
23+
return {A.rows(), 0};
2424
}
2525

2626
Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
@@ -62,7 +62,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_right(
6262
check_square("mdivide_right", "b", b);
6363
check_multiplicable("mdivide_right", "A", A, "b", b);
6464
if (b.size() == 0) {
65-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
65+
return {A.rows(), 0};
6666
}
6767

6868
Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
@@ -86,7 +86,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_right(
8686
check_square("mdivide_right", "b", b);
8787
check_multiplicable("mdivide_right", "A", A, "b", b);
8888
if (b.size() == 0) {
89-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
89+
return {A.rows(), 0};
9090
}
9191

9292
Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());

stan/math/fwd/fun/mdivide_right_tri_low.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inline Eigen::Matrix<fvar<T>, R1, C1> mdivide_right_tri_low(
1818
check_square("mdivide_right_tri_low", "b", b);
1919
check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
2020
if (b.size() == 0) {
21-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
21+
return {A.rows(), 0};
2222
}
2323

2424
Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
@@ -62,7 +62,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_right_tri_low(
6262
check_square("mdivide_right_tri_low", "b", b);
6363
check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
6464
if (b.size() == 0) {
65-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
65+
return {A.rows(), 0};
6666
}
6767

6868
Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
@@ -94,7 +94,7 @@ inline Eigen::Matrix<fvar<T>, R1, C2> mdivide_right_tri_low(
9494
check_square("mdivide_right_tri_low", "b", b);
9595
check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
9696
if (b.size() == 0) {
97-
return Eigen::Matrix<fvar<T>, R1, C2>(A.rows(), 0);
97+
return {A.rows(), 0};
9898
}
9999

100100
Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());

stan/math/prim/fun/matrix_exp_multiply.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ inline Eigen::Matrix<double, -1, Cb> matrix_exp_multiply(
2222
check_square("matrix_exp_multiply", "input matrix", A);
2323
check_multiplicable("matrix_exp_multiply", "A", A, "B", B);
2424
if (A.size() == 0) {
25-
return Eigen::Matrix<double, -1, Cb>(0, B.cols());
25+
return {0, B.cols()};
2626
}
2727

2828
return matrix_exp_action_handler().action(A, B);

stan/math/prim/fun/mdivide_left.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_left(
3232
check_square("mdivide_left", "A", A);
3333
check_multiplicable("mdivide_left", "A", A, "b", b);
3434
if (A.size() == 0) {
35-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(0, b.cols());
35+
return {0, b.cols()};
3636
}
3737

3838
return Eigen::Matrix<return_type_t<T1, T2>, R1, C1>(A).lu().solve(

stan/math/prim/fun/mdivide_left_ldlt.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_left_ldlt(
3232
const LDLT_factor<T1, R1, C1> &A, const Eigen::Matrix<T2, R2, C2> &b) {
3333
check_multiplicable("mdivide_left_ldlt", "A", A, "b", b);
3434
if (A.cols() == 0) {
35-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(0, b.cols());
35+
return {0, b.cols()};
3636
}
3737

3838
return A.solve(Eigen::Matrix<return_type_t<T1, T2>, R2, C2>(b));

stan/math/prim/fun/mdivide_left_spd.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_left_spd(
3535
check_symmetric(function, "A", A);
3636
check_not_nan(function, "A", A);
3737
if (A.size() == 0) {
38-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(0, b.cols());
38+
return {0, b.cols()};
3939
}
4040

4141
auto llt = Eigen::Matrix<return_type_t<T1, T2>, R1, C1>(A).llt();

stan/math/prim/fun/mdivide_left_tri.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_left_tri(
3737
check_square("mdivide_left_tri", "A", A);
3838
check_multiplicable("mdivide_left_tri", "A", A, "b", b);
3939
if (A.rows() == 0) {
40-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(0, b.cols());
40+
return {0, b.cols()};
4141
}
4242

4343
return Eigen::Matrix<return_type_t<T1, T2>, R1, C1>(A)
@@ -97,7 +97,7 @@ inline Eigen::Matrix<double, R1, C2> mdivide_left_tri(
9797
check_square("mdivide_left_tri", "A", A);
9898
check_multiplicable("mdivide_left_tri", "A", A, "b", b);
9999
if (A.rows() == 0) {
100-
return Eigen::MatrixXd(0, b.cols());
100+
return {0, b.cols()};
101101
}
102102

103103
#ifdef STAN_OPENCL

stan/math/prim/fun/mdivide_left_tri_low.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_left_tri_low(
3636
check_square("mdivide_left_tri_low", "A", A);
3737
check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
3838
if (A.rows() == 0) {
39-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(0, b.cols());
39+
return {0, b.cols()};
4040
}
4141

4242
return mdivide_left_tri<Eigen::Lower>(A, b);

stan/math/prim/fun/mdivide_right.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_right(
3232
check_square("mdivide_right", "A", A);
3333
check_multiplicable("mdivide_right", "b", b, "A", A);
3434
if (A.size() == 0) {
35-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(b.rows(), 0);
35+
return {b.rows(), 0};
3636
}
3737

3838
return Eigen::Matrix<return_type_t<T1, T2>, R2, C2>(A)

stan/math/prim/fun/mdivide_right_ldlt.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_right_ldlt(
3232
const Eigen::Matrix<T1, R1, C1> &b, const LDLT_factor<T2, R2, C2> &A) {
3333
check_multiplicable("mdivide_right_ldlt", "b", b, "A", A);
3434
if (A.rows() == 0) {
35-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(b.rows(), 0);
35+
return {b.rows(), 0};
3636
}
3737

3838
return transpose(mdivide_left_ldlt(A, transpose(b)));
@@ -44,7 +44,7 @@ inline Eigen::Matrix<double, R1, C2> mdivide_right_ldlt(
4444
const LDLT_factor<double, R2, C2> &A) {
4545
check_multiplicable("mdivide_right_ldlt", "b", b, "A", A);
4646
if (A.rows() == 0) {
47-
return Eigen::Matrix<double, R1, C2>(b.rows(), 0);
47+
return {b.rows(), 0};
4848
}
4949

5050
return A.solveRight(b);

stan/math/prim/fun/mdivide_right_spd.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_right_spd(
3636
check_symmetric(function, "A", A);
3737
check_not_nan(function, "A", A);
3838
if (A.size() == 0) {
39-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(b.rows(), 0);
39+
return {b.rows(), 0};
4040
}
4141

4242
// FIXME: After allowing for general MatrixBase in mdivide_left_spd,

stan/math/prim/fun/mdivide_right_tri.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ inline Eigen::Matrix<return_type_t<T1, T2>, R1, C2> mdivide_right_tri(
4444
"", "");
4545
}
4646
if (A.rows() == 0) {
47-
return Eigen::Matrix<return_type_t<T1, T2>, R1, C2>(b.rows(), 0);
47+
return {b.rows(), 0};
4848
}
4949

5050
return Eigen::Matrix<return_type_t<T1, T2>, R2, C2>(A)
@@ -81,7 +81,7 @@ inline Eigen::Matrix<double, R1, C2> mdivide_right_tri(
8181
check_square("mdivide_right_tri", "A", A);
8282
check_multiplicable("mdivide_right_tri", "b", b, "A", A);
8383
if (A.rows() == 0) {
84-
return Eigen::Matrix<double, R1, C2>(b.rows(), 0);
84+
return {b.rows(), 0};
8585
}
8686

8787
#ifdef STAN_OPENCL

stan/math/prim/fun/scale_matrix_exp_multiply.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ inline Eigen::Matrix<double, -1, Cb> scale_matrix_exp_multiply(
2929
check_square("scale_matrix_exp_multiply", "input matrix", A);
3030
check_multiplicable("scale_matrix_exp_multiply", "A", A, "B", B);
3131
if (A.size() == 0) {
32-
return Eigen::Matrix<double, -1, Cb>(0, B.cols());
32+
return {0, B.cols()};
3333
}
3434

3535
return matrix_exp_action_handler().action(A, B, t);
@@ -56,7 +56,7 @@ scale_matrix_exp_multiply(const Tt& t, const Eigen::Matrix<Ta, -1, -1>& A,
5656
check_square("scale_matrix_exp_multiply", "input matrix", A);
5757
check_multiplicable("scale_matrix_exp_multiply", "A", A, "B", B);
5858
if (A.size() == 0) {
59-
return Eigen::Matrix<return_type_t<Tt, Ta, Tb>, -1, Cb>(0, B.cols());
59+
return {0, B.cols()};
6060
}
6161

6262
return multiply(matrix_exp(multiply(A, t)), B);

stan/math/rev/fun/matrix_exp_multiply.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ inline Eigen::Matrix<return_type_t<Ta, Tb>, -1, Cb> matrix_exp_multiply(
2828
check_square("matrix_exp_multiply", "input matrix", A);
2929
check_multiplicable("matrix_exp_multiply", "A", A, "B", B);
3030
if (A.size() == 0) {
31-
return Eigen::Matrix<return_type_t<Ta, Tb>, -1, Cb>(0, B.cols());
31+
return {0, B.cols()};
3232
}
3333

3434
return multiply(matrix_exp(A), B);

stan/math/rev/fun/mdivide_left.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,10 @@ class mdivide_left_vd_vari : public vari {
175175
template <int R1, int C1, int R2, int C2>
176176
inline Eigen::Matrix<var, R1, C2> mdivide_left(
177177
const Eigen::Matrix<var, R1, C1> &A, const Eigen::Matrix<var, R2, C2> &b) {
178-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
179-
180178
check_square("mdivide_left", "A", A);
181179
check_multiplicable("mdivide_left", "A", A, "b", b);
182180
if (A.size() == 0) {
183-
return res;
181+
return {0, b.cols()};
184182
}
185183

186184
// NOTE: this is not a memory leak, this vari is used in the
@@ -190,6 +188,7 @@ inline Eigen::Matrix<var, R1, C2> mdivide_left(
190188
internal::mdivide_left_vv_vari<R1, C1, R2, C2> *baseVari
191189
= new internal::mdivide_left_vv_vari<R1, C1, R2, C2>(A, b);
192190

191+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
193192
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
194193

195194
return res;
@@ -199,12 +198,10 @@ template <int R1, int C1, int R2, int C2>
199198
inline Eigen::Matrix<var, R1, C2> mdivide_left(
200199
const Eigen::Matrix<var, R1, C1> &A,
201200
const Eigen::Matrix<double, R2, C2> &b) {
202-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
203-
204201
check_square("mdivide_left", "A", A);
205202
check_multiplicable("mdivide_left", "A", A, "b", b);
206203
if (A.size() == 0) {
207-
return res;
204+
return {0, b.cols()};
208205
}
209206

210207
// NOTE: this is not a memory leak, this vari is used in the
@@ -214,6 +211,7 @@ inline Eigen::Matrix<var, R1, C2> mdivide_left(
214211
internal::mdivide_left_vd_vari<R1, C1, R2, C2> *baseVari
215212
= new internal::mdivide_left_vd_vari<R1, C1, R2, C2>(A, b);
216213

214+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
217215
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
218216

219217
return res;
@@ -223,12 +221,10 @@ template <int R1, int C1, int R2, int C2>
223221
inline Eigen::Matrix<var, R1, C2> mdivide_left(
224222
const Eigen::Matrix<double, R1, C1> &A,
225223
const Eigen::Matrix<var, R2, C2> &b) {
226-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
227-
228224
check_square("mdivide_left", "A", A);
229225
check_multiplicable("mdivide_left", "A", A, "b", b);
230226
if (A.size() == 0) {
231-
return res;
227+
return {0, b.cols()};
232228
}
233229

234230
// NOTE: this is not a memory leak, this vari is used in the
@@ -238,6 +234,7 @@ inline Eigen::Matrix<var, R1, C2> mdivide_left(
238234
internal::mdivide_left_dv_vari<R1, C1, R2, C2> *baseVari
239235
= new internal::mdivide_left_dv_vari<R1, C1, R2, C2>(A, b);
240236

237+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
241238
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
242239

243240
return res;

stan/math/rev/fun/mdivide_left_ldlt.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ template <int R1, int C1, int R2, int C2>
208208
inline Eigen::Matrix<var, R1, C2> mdivide_left_ldlt(
209209
const LDLT_factor<var, R1, C1> &A, const Eigen::Matrix<var, R2, C2> &b) {
210210
check_multiplicable("mdivide_left_ldlt", "A", A, "b", b);
211-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
212211
if (A.cols() == 0) {
213-
return res;
212+
return {0, b.cols()};
214213
}
215214

216215
internal::mdivide_left_ldlt_vv_vari<R1, C1, R2, C2> *baseVari
217216
= new internal::mdivide_left_ldlt_vv_vari<R1, C1, R2, C2>(A, b);
218217

218+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
219219
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
220220

221221
return res;
@@ -240,14 +240,14 @@ template <int R1, int C1, int R2, int C2>
240240
inline Eigen::Matrix<var, R1, C2> mdivide_left_ldlt(
241241
const LDLT_factor<var, R1, C1> &A, const Eigen::Matrix<double, R2, C2> &b) {
242242
check_multiplicable("mdivide_left_ldlt", "A", A, "b", b);
243-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
244243
if (A.cols() == 0) {
245-
return res;
244+
return {0, b.cols()};
246245
}
247246

248247
internal::mdivide_left_ldlt_vd_vari<R1, C1, R2, C2> *baseVari
249248
= new internal::mdivide_left_ldlt_vd_vari<R1, C1, R2, C2>(A, b);
250249

250+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
251251
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
252252

253253
return res;
@@ -272,14 +272,14 @@ template <int R1, int C1, int R2, int C2>
272272
inline Eigen::Matrix<var, R1, C2> mdivide_left_ldlt(
273273
const LDLT_factor<double, R1, C1> &A, const Eigen::Matrix<var, R2, C2> &b) {
274274
check_multiplicable("mdivide_left_ldlt", "A", A, "b", b);
275-
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
276275
if (A.cols() == 0) {
277-
return res;
276+
return {0, b.cols()};
278277
}
279278

280279
internal::mdivide_left_ldlt_dv_vari<R1, C1, R2, C2> *baseVari
281280
= new internal::mdivide_left_ldlt_dv_vari<R1, C1, R2, C2>(A, b);
282281

282+
Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
283283
res.vi() = Eigen::Map<matrix_vi>(baseVari->variRefC_, res.rows(), res.cols());
284284

285285
return res;

0 commit comments

Comments
 (0)