2
votes

I was wondering if there is a feature in the Apache Math Commons library for element-wise multiplication similar to the one used in MATLAB being that the resulting matrix C is equal to each value in matrix A multiplied by the corresponding value in matrix B. I want to avoid writing my own version because I know linear algebra packages are highly optimized for these kinds of operations and I don't want to lose performance on my own implementation if one already exists (that's optimized).

In MATLAB: C = A.*B and A's dimensions must equal B's.

1
I'm missing the part of your question where you say you went over the RealMatrix interface and didn't find anything. After you did that take a look here.Dev-iL
Sorry for the late reply, and I did look I can't believe I didn't find it! Thanks for clearing it up :)Mark Shark
note that the last link I posted is to a 3rd-party library that happens to implement what you need. No reason to feel bad :)Dev-iL

1 Answers

0
votes

I've used for RealVectors from Apache math the ebeMultiplication. I don't know if there is an equivalent function for matrices, I checked and I did not find. As for the vectors:

    RealVector output = new ArrayRealVector(o); // o and y are double[]
    RealVector expected = new ArrayRealVector(y);
    RealVector errors = expected.subtract(output);    
    RealVector delta = errors.ebeMultiply(output);