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

Matrix and vector operations (broadcasting) #32

Open
mroodschild opened this issue Mar 24, 2018 · 4 comments
Open

Matrix and vector operations (broadcasting) #32

mroodschild opened this issue Mar 24, 2018 · 4 comments

Comments

@mroodschild
Copy link

mroodschild commented Mar 24, 2018

Hi Peter,

I think it would be good, if ejml supports other matrix operations, such as adding or multiplying a vector to an entire matrix, for example:

[4 5 6]   [1]   [(4+1) (5+1) (6+1)]   [5 6 7]
[1 2 3] + [2] = [(1+2) (2+2) (3+2)] = [3 4 5]
[1 5 9]   [3]   [(1+3) (5+3) (9+3)]   [4 8 12]

or

[4 5 6]             [(4+1) (5+2) (6+3)]   [5 7 9]
[1 2 3] + [1 2 3] = [(1+1) (2+2) (3+3)] = [2 4 6]
[1 5 9]             [(1+1) (5+2) (9+3)]   [2 7 12]

https://jakevdp.github.io/PythonDataScienceHandbook/02.05-computation-on-arrays-broadcasting.html

Thanks Peter!

@lessthanoptimal
Copy link
Owner

If something like this was added how would you suggest it be incorporated?

CommonOps.bcAdd(A,B,C);
C = SimpleMatrix.bcAdd(A,B);

I don't like automatically broadcasting if the dimensions don't line up. That can easily lead to a difficult to detect bug. Which I have encountered in python/matlab before.

@mroodschild
Copy link
Author

mroodschild commented Mar 24, 2018

Actually for the broadcasting I have to do:

Z [n x m] = W [n x j] * A [j x m] + B [n x 1]

SimpleMatrix W = new SimpleMatrix(2, 3, true, 1.0, 5, 6, 3, 2, 1);
SimpleMatrix A = new SimpleMatrix(3, 1, true, 1.0, 2, 3);
SimpleMatrix B = new SimpleMatrix(2, 1, true, 0.5, 1);

SimpleMatrix Z = W.mult(A);
Z.print();
for (int i = 0; i < Z.numCols(); i++) {
        Z.setColumn(i, 0, Z.extractVector(false, i).plus(B).getDDRM().getData());
}
Z.print();

but it would be easier if:

Z = W.mult(A).bcAdd(B, false)

where:
   true - broadcasting in rows
   false - broadcasting in columns

@lessthanoptimal
Copy link
Owner

Did you end up implementing this on your own?

@VestOfHolding
Copy link

This seems close enough to my request that I'll bump this. I would also really like to be able to do arithmetic operations on two matrices, not just one scalar value.

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