2009-09-03 2 views

답변

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

이제'RealMatrix identity = MatrixUtils.createRealIdentityMatrix (dimension);'가됩니다. –

+0

@BobCross는 404 오류로 인해 링크를 수정하십시오. –

+0

@p_d done. 감사! –

5

그냥 매트릭스없이 제 3 자 라이브러리를 표현하기 위해 2 차원 배열을 사용하려면 :

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; 
    } 
} 
+0

'new double'은 이미 제로로 채워진 배열을 만듭니다 ... 대각선 만 반복 할 것입니다. –

+0

@CarlosHeuberger .. 좋은 생각. 5 년 후, 내 대답을 업데이 트했습니다 : – James

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; 
    } 
} 
+0

당신은 정말 생성자 및 개인 변수가 필요하지 않습니다 .. 그리고 당신 getValue 정적 수 있습니다. – Theodor

관련 문제