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

Empty matrices do not support all operations #159

Closed
JozsefKutas opened this issue Nov 11, 2022 · 10 comments
Closed

Empty matrices do not support all operations #159

JozsefKutas opened this issue Nov 11, 2022 · 10 comments

Comments

@JozsefKutas
Copy link

Empty matrices (with no rows or columns, e.g. new SimpleMatrix(0, 0)) don't currently support calculating the inverse, pseudoinverse or determinant. Decompositions (EVD, SVD) are also not supported. I ran into this problem when using SimpleMatrix, but the issue extends to the common operations and solver classes which assume that the matrix is non-empty.

Support for empty matrices is convenient when matrix sizes are determined dynamically - for example, according to the results of statistical significance tests.

I'm not sure whether empty matrices are supposed to be supported at all - EJML doesn't do any shape checks, and actually allows matrices with negative dimensions (maybe this should be fixed?). A quick comparison to other libraries: Apache Math checks all dimensions are strictly positive in matrix/vector constructors, while numpy and Octave allow empty matrices and support the above operations for empty matrices.

If you let me know what you think, I'd be happy to submit a PR.

@lessthanoptimal
Copy link
Owner

@JozsefKutas Sorry for taking a bit. Yeah there should be a shape check when constructing matrices. Allowing a dimension with zero has come up in the past. Only hesitation is making sure every operation can gracefully support it. What would the SVD of a 0x0 matrix be? Seems to be undefined.

@JozsefKutas
Copy link
Author

The matrices in the SVD would all be 0x0 as well, and the singular values array would be empty. This is how numpy handles it, and IIRC it is the same for MATLAB:

>>> import numpy as np
>>> arr = np.array([]).reshape((0, 0))
>>> np.linalg.svd(arr)

(array([], shape=(0, 0), dtype=float64),
 array([], dtype=float64),
 array([], shape=(0, 0), dtype=float64))

@lessthanoptimal
Copy link
Owner

Got the first part of this work in a new branch https://github.com/lessthanoptimal/ejml/tree/feature/zero_dimen

Adds checks to make sure negative rows or columns throw an exception for all matrix types.

@lessthanoptimal
Copy link
Owner

@JozsefKutas for other decompositions does it just return 0x0 matrices for everything, like with LU? L and U will be 0x0?

@JozsefKutas
Copy link
Author

numpy returns 0x0 matrices/arrays for all decompositions, though it doesn't do LU. scipy does LU, but it doesn't handle 0x0 matrices (scipy wraps LAPACK and its preprocessing of inputs doesn't seem 100% consistent). I checked Octave and Eigen and they seem to return 0x0 matrices for all decompositions, including LU.

@dariuszzbyrad
Copy link
Contributor

@lessthanoptimal @JozsefKutas If you don't mind, I can prepare PR

@lessthanoptimal
Copy link
Owner

@dariuszzbyrad Sure! I've merged in the branch above into SNAPSHOT, so you can just fork that.

You will need to modify StandardSvdChecks_DDRM to ensure it handles SVD on 0x0, Nx0, and 0xM matrices correctly. I would do that first then see which implementations have issues.

@lessthanoptimal
Copy link
Owner

@dariuszzbyrad know when you might be able to start work on this PR?

@lessthanoptimal
Copy link
Owner

lessthanoptimal commented Jan 13, 2023

Handling of zeros check list (DDRM). I'll check these off as a branch is created to tackle them.

  • Matrix multiplication
  • LU Decomposition
  • Cholesky
  • QR
  • SVD
  • Eigenvalue
  • Linear Solvers

@lessthanoptimal
Copy link
Owner

PR with empty matrix support #172 just going to merge it once it passes CI since so many files have been touched. The PR also includes code clean up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants