1

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 other 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!

  • IIRC, the product of matrices needs that the number of columns of the first matrix is equal to the number of rows of the second one... I am no expert with JAMA but it loos like both of your matrices have the same dimensions (and are not square matrices) – SJuan76 Aug 31 '11 at 16:28

3 Answers3

2

Do you mean using get?

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

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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 );
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

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.

Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67