lessthanoptimal / ejml

A fast and easy to use linear algebra library written in Java for dense, sparse, real, and complex matrices.

Home Page:https://ejml.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty matrices do not support all operations

JozsefKutas opened this issue · comments

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.

@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.

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))

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.

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

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.

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

@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.

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

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

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.