开发者

How to multiply two 1D matrices using JAMA?

开发者 https://www.devze.com 2023-03-31 20:51 出处:网络
This may be a bit of a silly question and I might also have misunderstood the best way to approach this problem but what I essentially want to do is the following:

This may be a bit of a silly question and I might also have misunderstood the best way to approach this problem but what I essentially want to do is the following:

I want to multiply the following matrices together to get the result -0.8. However I would ideally like to do this using a JAMA function. So far I have the following and I think I'm almost there, it's just the last step I'm stuck on..

// Create the two arrays (in reality I won't be creating these, the two 1D matrices
// will be the result of othe开发者_如何学JAVAr calculations - I have just created them for this example)
double[] aArray = [0.2, -0.2];
double[] bArray = [0, 4];

// Create matrices out of the arrays
Matrix a = new Matrix( aArray, 1 );
Matrix b = new Matrix( bArray, 1 );

// Multiply matrix a by matrix b to get matrix c
Matrix c = a.times(b);

// Turn matrix c into a double
double x = // ... this is where I'm stuck

Any help on this would be really appreciated. Thanks in advance!


Do you mean using get?

double x = c.get(0, 0);

http://math.nist.gov/javanumerics/jama/doc/


It sounds like you're looking for

double x = c.get(0, 0);

Also, your matrices have incompatible dimensions for multiplication. It would appear that the second matrix ought be constructed like so:

Matrix b = new Matrix( bArray, bArray.length );


You can simply use the get() method:

double x = c.get(0,0);

Note that you will get an IllegalArgumentException since you're trying to multiply two row vectors though. From the times() documentation:

java.lang.IllegalArgumentException - Matrix inner dimensions must agree.

You probably want to make the second array into a column vector.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号