Java에서 지정된 크기의 단위 행렬을 만드는 유틸리티가 있습니까?Java에서 임의의 크기의 항등 행렬을 만드는 방법은 무엇입니까?
답변
매트릭스 요구 사항에 따라 Jama을 권장합니다. identity matrix을 생성하라는 전화가 있습니다 (identity method 참조).
는 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);
이제'RealMatrix identity = MatrixUtils.createRealIdentityMatrix (dimension);'가됩니다. –
@BobCross는 404 오류로 인해 링크를 수정하십시오. –
@p_d done. 감사! –
그냥 매트릭스없이 제 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;
}
}
'new double'은 이미 제로로 채워진 배열을 만듭니다 ... 대각선 만 반복 할 것입니다. –
@CarlosHeuberger .. 좋은 생각. 5 년 후, 내 대답을 업데이 트했습니다 : – James
하는 메모리 효율적인 솔루션을 생성하는 것을 다음과 같은 수업 :
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;
}
}
당신은 정말 생성자 및 개인 변수가 필요하지 않습니다 .. 그리고 당신 getValue 정적 수 있습니다. – Theodor
대학 숙제를 요청하는 사람이 아닌가요? – Justin
심지어는 태그가 붙지 않았습니다 ... –
@Justin, 나는 질문을 좀 더 유용하고 덜 숙제로 바꾸려고했습니다. –