简体   繁体   English

如何使用JAMA将两个一维矩阵相乘?

[英]How to multiply two 1D matrices using JAMA?

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. 我想将以下矩阵相乘以获得结果-0.8。 However I would ideally like to do this using a JAMA function. 但是,理想情况下,我想使用JAMA函数执行此操作。 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! 提前致谢!

Do you mean using get? 您是说使用get吗?

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

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

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 );

You can simply use the get() method: 您可以简单地使用get()方法:

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

Note that you will get an IllegalArgumentException since you're trying to multiply two row vectors though. 请注意,由于尝试将两个行向量相乘,因此将获得IllegalArgumentException。 From the times() documentation: times()文档中:

java.lang.IllegalArgumentException - Matrix inner dimensions must agree.

You probably want to make the second array into a column vector. 您可能要使第二个数组成为列向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM