Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handling of size 0 inputs #1517

Merged
merged 7 commits into from
Dec 16, 2019
Merged

Conversation

mcol
Copy link
Contributor

@mcol mcol commented Dec 14, 2019

Summary

The following is done:

  • matrix_exp_multiply, scale_matrix_exp_multiply and matrix_exp_pade: move check_square at the beginning, so that it throws if A is not square (currently it returns an empty matrix if any dimension is 0)
  • scale_matrix_exp_multiply: it returns a rectangular matrix with 0 rows if A is empty and B has zero rows (currently it returns a square empty matrix)
  • mdivide_left_ldlt: it returns a rectangular matrix with 0 rows if A is empty and b has zero rows (currently it returns a square empty matrix)
  • mdivide_right_ldlt: it returns a rectangular matrix with 0 columns if A is empty and b has zero columns (currently it returns a square empty matrix)
  • trace_inv_quad_form_ldlt: use B.rows() instead of B.size(), so we ensure that matrices are multipliable before returning 0

This also:

  • avoids extra work in check_symmetric by checking if the size of the square matrix is 0
  • same for check_corr_matrix, so that for size 0 matrix it returns instead of throwing

Fixes #1515.

Tests

I've added a bunch of tests that check for all of the above. It's still worth checking for more cases that I might have missed. I'm also not entirely convinced that my mental mapping of prim/rev/mix is complete or correct, so it's possible I've missed something.

Side Effects

None.

Checklist

  • Math issue Improve handling of size 0 inputs #1515

  • Copyright holder: Marco Colombo

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

The following don't fail, but provide more coverage for corner cases:

  test/unit/math/mix/mat/fun/matrix_exp_multiply_test.cpp
  test/unit/math/mix/mat/fun/matrix_exp_pade_test.cpp
  test/unit/math/mix/mat/fun/trace_inv_quad_form_ldlt_test.cpp
@stan-buildbot
Copy link
Contributor

(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 0.99)
(stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan, 0.96)
(stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan, 1.01)
(stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan, 1.01)
(stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan, 1.05)
(stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan, 1.0)
(stat_comp_benchmarks/benchmarks/arK/arK.stan, 1.01)
(performance.compilation, 1.01)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan, 0.98)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan, 0.99)
(stat_comp_benchmarks/benchmarks/sir/sir.stan, 1.0)
(stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan, 1.0)
(stat_comp_benchmarks/benchmarks/garch/garch.stan, 1.01)
(stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan, 0.99)
(stat_comp_benchmarks/benchmarks/arma/arma.stan, 1.0)
Result: 1.00046914435
Commit hash: 117e21e

Copy link
Member

@rok-cesnovar rok-cesnovar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning these up! Two minor return cleanups then its good.

stan/math/prim/mat/fun/mdivide_left_ldlt.hpp Outdated Show resolved Hide resolved
stan/math/rev/mat/fun/matrix_exp_multiply.hpp Outdated Show resolved Hide resolved
rok-cesnovar
rok-cesnovar previously approved these changes Dec 15, 2019
Copy link
Member

@rok-cesnovar rok-cesnovar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks for the quick fixes.

@stan-buildbot
Copy link
Contributor

(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 0.99)
(stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan, 0.96)
(stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan, 1.0)
(stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan, 1.01)
(stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan, 0.99)
(stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan, 1.0)
(stat_comp_benchmarks/benchmarks/arK/arK.stan, 1.0)
(performance.compilation, 1.01)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan, 1.0)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan, 0.99)
(stat_comp_benchmarks/benchmarks/sir/sir.stan, 1.01)
(stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan, 0.97)
(stat_comp_benchmarks/benchmarks/garch/garch.stan, 1.0)
(stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan, 1.02)
(stat_comp_benchmarks/benchmarks/arma/arma.stan, 1.03)
Result: 0.99904817297
Commit hash: 45afd6e

@rok-cesnovar rok-cesnovar merged commit ad4a0ec into stan-dev:develop Dec 16, 2019
@mcol mcol deleted the issue_1515 branch December 16, 2019 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve handling of size 0 inputs
3 participants