gpt4 book ai didi

Java 矩阵和数值

转载 作者:行者123 更新时间:2023-11-29 05:51:19 25 4
gpt4 key购买 nike

所以我刚开始用 Java 做数字方法;

// I have a matrix class that has get, set methods for creating matrices; have used those in the method below.
// The method below computes a Matrix and prints as well returns it.

public Matrix computeA(){

int nrows = A.getNumRows();
int ncols = A.getNumCols();

for(int i = 0; i < nrows; i++){
double sum = 0.0;
for (int j = 0; i< ncols; j++){
if (i!=j){
A.setElement(i,j, aijCalc(i,j) );} // aijCalc is numeric method; setElement has parameters (i, j, value)
}
A.print();
return A;
}

现在,这是我不明白该怎么做的事情:i = j 的对角线,矩阵条目为 1 -(该行中所有 aij 条目(对于 i!=j)的总和)。我如何在上面的方法中对此进行编码?

我当前的输出(没有任何四舍五入看起来像这样):

0.0 0.026055555555555554 0.0248125 0.05069444444444445 0.05872222222222222 0.030208333333333334

-0.0053750000000000004 0.0 0.00792361111111111 0.01813194444444444 0.02361111111111111 0.009874999999999998

-0.0013854166666666667 -0.005291666666666666 0.0 0.008680555555555556 0.009041666666666667 0.0

0.0 0.009041666666666667 0.008680555555555556 0.0 -0.005291666666666666 -0.0013854166666666667

0.030208333333333334 0.05872222222222222 0.050694444444444445 0.0248125 0.026055555555555554 0.0

(它有 0,其中 i = j)

它应该是这样的:

0.8100.0260.0250.0510.0590.030

-‐0.0050.9460.0080.0180.0240.010

-‐0.001-‐0.0050.9890.0090.0090.000

0.0000.0090.0090.989-‐0.005-‐0.001

0.0100.0240.0180.0080.946-‐0.005

0.0300.0590.0510.0250.0260.810

谢谢!

最佳答案

那些额外的零实际上是“谎言”。

建议:只需将数字格式化为实际精度(例如小数点后四位或五位)。

示例:

System.out.println (String.format ("%11.2f", x);

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

附言:

完全几乎所有其他语言的浮点“printf”也是如此:包括 C# 和 C++。

关于Java 矩阵和数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13757136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com