javavectorapache-commons-math

Apache Commons Math3: Multiply row with column vector


I want to multiply two vectors a^T = (1,2,3) and b = (4,5,6). With pen and pencil, I got

c = 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32

With apache commons math3 I do

ArrayRealVector a = new ArrayRealVector(new double []{1, 2, 3});
ArrayRealVector b = new ArrayRealVector(new double []{4, 5, 6});

to get a representation of the vectors. And to get the result I want to do something like

double c = a.transpose().multiply(b);

but I can't find the right method for it (Wether transpose nor multiply).


Solution

  • This is the dot product, which you can do with double c = a.dotProduct(b);