5

I'm trying to create a vector from an array of doubles. I then want to multiply this vector by a matrix. Does anyone know how I can achieve this? Below is a really simple example that I would like to get working.

// Create the matrix (using JAMA)
Matrix a = new Matrix( [[1,2,3],[1,2,3],[1,2,3]] );

// Create a vector out of an array
...

// Multiply the vector by the matrix
...
skaffman
  • 398,947
  • 96
  • 818
  • 769

3 Answers3

13

Here is simple example of wanted operation:

double[][] array = {{1.,2.,3},{1.,2.,3.},{1.,2.,3.}}; 
Matrix a = new Matrix(array);   
Matrix b = new Matrix(new double[]{1., 1., 1.}, 1);     
Matrix c = b.times(a);  
System.out.println(Arrays.deepToString(c.getArray()));

Result:

[[3.0, 6.0, 9.0]]

In other words that is:

enter image description here

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 1
    @Jon: Note that `Matrix` class API (http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html) is your friend to get more options :) – Grzegorz Szpetkowski Jul 13 '11 at 19:34
  • Hi! Sorry to be an idiot, but could you please give us an example of what happens when [1 1 1] is something like [2 5 3] instead? – Joehot200 Feb 12 '15 at 16:20
  • @Joehot200: Matrix multiplication is fairly easy to learn by yourself, [this](https://www.khanacademy.org/math/algebra2/alg2-matrices/matrix-multiplication-alg2/v/multiplying-a-matrix-by-a-matrix) should make your clear how to do it. Essentially you multiply "rows by columns", each product is then summed up and placed accordingly. – Grzegorz Szpetkowski Feb 12 '15 at 16:25
  • Right. Got it. Thanks. I can't believe that I had got all the way to learning sines, cosines, tangent, vector multiplication, etc, and yet never encountered matrix-vector multiplication. – Joehot200 Feb 12 '15 at 16:39
1

Why can't you use Matrix's arrayTimes method? A vector is just a 1 x n matrix (I think) so can't you initialize a second matrix with just 1 dimension and use arrayTimes?

Matrix a = new Matrix( [[1,2,3],[1,2,3],[1,2,3]] );
Matrix b = new Matrix( [[1,2,3]] ); // this is a vector
Matrix c = a.arrayTimes(b.transpose); // transpose so that the inner dimensions agree

This is what I think would work from reading the doc.

Ziggy
  • 21,845
  • 28
  • 75
  • 104
  • 1
    I just tried out your idea, however it seems to want the matrix dimensions to agree (ie. same number of rows and cols). However since the vector only has one row it throws an exception. –  Jul 13 '11 at 19:19
  • Ah my mistake! Multiplying matrices is a little tricky. It's not that both dimensions have to agree (that would be boring) it's just that the inner dimensions have to agree. You can still multiply a and b above, but you need to transpose b. I've edited the answer. – Ziggy Jul 13 '11 at 20:00
  • 1
    This doesn't work because the arguments of the constructor are not Java Syntax... – kiltek Oct 31 '14 at 16:00
0

How about this:

double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
Matrix A = new Matrix(vals);

From http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html

Ishtar
  • 11,542
  • 1
  • 25
  • 31