Closed gnoatto closed 9 years ago
Hi Alessandro,
I suppose there's no reason why it couldn't.
Note that depending on the asymptotic complexity, and the difficulty of implementation, some operations might be better done in native Java; it depends.
For those operations that are best done by Eigen, here is an example of the C++ wrapper code in Jeigen, for a matrix multiply:
void dense_multiply( int rows, int middle, int cols, double *afirst, double
*asecond, double *aresult ) {
Map<MatrixXd>first(afirst,rows,middle);
Map<MatrixXd>second(asecond,middle,cols);
Map<MatrixXd>result(aresult,rows,cols);
result = first * second;
}
This is in 'jeigen.cpp'. It takes in two matrices as 1d arrays of floats,
one per matrix, then creates Eigen matrices from them, using Map
In the java code, we declare this in JeigenJna.java:
public static native void dense_multiply( int rows, int middle, int cols,
double []first, double []second, double []result );
... simply using java arrays, in place of the double* C++ arrays in the C++ code.
Finally, this is added to DenseMatrix.java:
public DenseMatrix mmul( DenseMatrix second ) {
DenseMatrix result = new DenseMatrix(this.rows, second.cols);
JeigenJna.Jeigen.dense_multiply(this.rows, this.cols, second.cols,
this.values, second.values, result.values );
return result;
}
The '.values' array maps directly onto the 1d arrays required, and we also send the number of rows and columns.
Finally, we add in some validation code, in the same function:
public DenseMatrix mmul( DenseMatrix second ) {
if( this.cols != second.rows ) {
throw new RuntimeException("matrix size mismatch " + shape() + " vs " +
second.shape());
}
DenseMatrix result = new DenseMatrix(this.rows, second.cols);
JeigenJna.Jeigen.dense_multiply(this.rows, this.cols, second.cols,
this.values, second.values, result.values );
return result;
}
Other operations follow the same pattern. You could look for example at DenseMatrix.svd(), which is similar to one of the Unsupported Modules I think?
Hi Hugh,
I'm looking for the same thing. JBLAS doesn't support Win64, and no pure Java libraries support expm. I'm new to this arena and have spent the past few days surveying the options. There just aren't any! I'm going to keep an eye on this, because as far as I can tell today, there is no way to compute a matrix exponential in Java on Win64 using the Pade + square-and-scale method.
Ok, I've added mexp and mlog, which are matrix exponential and matrix logarithm respectively. You can see that the changes in the jeigen code are:
As far as using the new functions, just call .mexp(), or .mlog() on any dense square matrix.
This is great, thank you very much.
Hello,
your wrapper is very interesting. I was wondering if it is possible to wrap also the matrix functions which are featured in the Unsupported Modules.
Thanks, Alessandro