0

是否有在 Java 中创建指定大小的单位矩阵的实用程序?

4

4 回答 4

6

为常用的线性代数尝试Apache Commons Math :

// Set dimension to the size of the square matrix that you would like
// Example, this will make a 3x3 matrix with ones on the diagonal and
// zeros elsewhere.
int dimension = 3;
RealMatrix identity = RealMatrix.createRealIdentityMatrix(dimension);
于 2009-09-03T17:26:00.897 回答
5

如果您只想使用二维数组来表示矩阵并且没有第三方库:

public class MatrixHelper {
  public static double[][] getIdentity(int size) {
    double[][] matrix = new double[size][size];
    for(int i = 0; i < size; i++) matrix[i][i] = 1;
    return matrix;
  }
}
于 2009-09-03T18:12:15.657 回答
4

我推荐Jama来满足您所有的矩阵需求。有一个调用来生成一个身份矩阵(请参阅身份方法)。

于 2009-09-03T16:57:14.620 回答
1

一个节省内存的解决方案是创建一个像这样的类:

public class IdentityMatrix{
     private int dimension;

     public IdentityMatrix(int dimension){
          this.dimension=dimension
     }

     public double getValue(int row,int column){
          return row == column ? 1 : 0;
     }
}
于 2009-11-25T14:40:52.817 回答