java 如何使用 JAMA 将矩阵乘以向量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6684047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I multiply a matrix by a vector using JAMA?
提问by The Crazy Chimp
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
...
回答by Grzegorz Szpetkowski
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:
换句话说就是:
回答by Ziggy
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 的 arrayTimes 方法?向量只是一个 1 xn 矩阵(我认为),所以您不能仅使用 1 维初始化第二个矩阵并使用 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.
这就是我认为通过阅读doc会起作用的。
回答by Ishtar
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
来自http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html