是否有用于在Java中创建指定大小的单位矩阵的实用程序?
答案 0 :(得分:6)
尝试Apache Commons Math for commonly used linear algebra:
// 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);
答案 1 :(得分: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;
}
}
答案 2 :(得分:4)
我建议Jama满足您的所有矩阵需求。有人要求生成identity matrix(请参阅identity method)。
答案 3 :(得分: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;
}
}