开发者

How can I multiply a matrix by a vector using JAMA?

开发者 https://www.devze.com 2023-03-20 15:02 出处:网络
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

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
...


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:

How can I multiply a matrix by a vector using JAMA?


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.


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

0

精彩评论

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

关注公众号