Plus method like Python broadcasting?

24 views
Skip to first unread message

Stephen Whiddett

unread,
Dec 20, 2017, 9:24:21 AM12/20/17
to efficient-java-matrix-library-discuss
Hi. I've just discovered EJML and am using SimpleMatrix. I'm writing a program where I would like to add a 1x1 matrix to say a 1x2 matrix or a 2x3 matrix. In python this is done via broadcasting where it knows to add the 1x1 value to each cell. Is there something similar in EJML? Alternatively, is there a way to duplicate the rows and/or columns of a matrix until it matches the size of another matrix so that it is ready for adding?

Thanks
Steve

Peter A

unread,
Dec 20, 2017, 11:38:24 AM12/20/17
to efficient-java-mat...@googlegroups.com
In Equations I believe there is something similar.


That API tends to be much more concise if you're building matrices from other matrices. The element-wise multiplication using SimpleMatrix or CommonOps will complain that matrix sizes don't agree with each other. What you can do is if you know it's a 1x1 matrix extract the value from it and then call scale()

--
You received this message because you are subscribed to the Google Groups "efficient-java-matrix-library-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to efficient-java-matrix-library-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
"Now, now my good man, this is no time for making enemies."    — Voltaire (1694-1778), on his deathbed in response to a priest asking that he renounce Satan.

Stephen Whiddett

unread,
Dec 21, 2017, 8:23:43 AM12/21/17
to efficient-java-matrix-library-discuss
Hi Peter. Thanks for the reply. I took your advice and tried using the Equation class. But I still have the same issue. Perhaps I misunderstood? Here is my code:

double[][] a = {{ 1.62434536, -0.61175641}, 
    {-0.52817175, -1.07296862}, 
    { 0.86540763, -2.3015387}};
SimpleMatrix A = new SimpleMatrix(a);
double[][] w = {{1.74481176, -0.7612069, 0.3190391}};
SimpleMatrix W = new SimpleMatrix(w);
double[][] b = {{-0.24937038}};
SimpleMatrix B = new SimpleMatrix(b);

SimpleMatrix Z = new SimpleMatrix(0,0);
Equation eq = new Equation();
eq.alias(A,"A",W,"W",B,"B",Z,"Z");
      eq.process("Z = W*A");
eq.process("Z = Z+B");

Everything works fine until the final line where I get the error 'The matrices are not all the same dimension'. I can't assume that B is 1x1 as this may vary. However, if its not 1x1 I would expect the dims of B to be (Zrows,1) or (1, Zcols). Are you able to assist further?
Thanks

Peter A

unread,
Jan 4, 2018, 1:49:16 PM1/4/18
to efficient-java-mat...@googlegroups.com
sorry for the slow response.

Thought I had added support for that but I guess I was thinking of some other element-wise operations. I just added support for that inside of CommonOps_DDRM. Adding support inside of Equations will take more than a couple of minutes. Could you create a feature requests on github for it?

Thanks,
- Peter

--
You received this message because you are subscribed to the Google Groups "efficient-java-matrix-library-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to efficient-java-matrix-library-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages