Cryptography Reference
In-Depth Information
int i=currentRow;
//Make sure diagonal element is nonzero, if possible, by swapping
while
(i<=mat.numRows&&mat.array[i][currentRow].equals(BigIntegerMath.ZERO))
i++;
if (i>mat.numRows) throw new SingularMatrixException
(“Linear dependence exists here!”);
//Swap with a row not having zero in diagonal position
if (currentRow!=i) swapRows(mat,b,currentRow,i);
//Now, you must produce all zeros below and above the diagonal element
i=1;
//Multiply each row by the proper scalar
while (i<=mat.numRows) {
if (i!=currentRow) {
BigInteger scalar=mat.array[i][currentRow];
if (!scalar.equals(BigIntegerMath.ZERO)) {
multiplyRow(mat,b,i,mat.array[currentRow][currentRow]);
multiplyRow(mat,b,currentRow,scalar);
//Replace row i with row i minus diagonal row
subtractRow(mat,b,i,currentRow);
}
}
i++;
}
currentRow++;
}
//Now, produce 1's along main diagonal by multiplying by an inverse
for (int index=1;index<=mat.numRows;index++) {
multiplyRow(mat,b,index,mat.array[index][index].modInverse(modulus));
}
//Remember, b may be a square matrix-polymorphism takes care of this here
return b;
}
//This method exists in case the answer is actually a square matrix
public ModSquareMatrix gaussianSolve(ModSquareMatrix constants)
throws MatricesNonConformableException,SingularMatrixException {
return (ModSquareMatrix) gaussianSolve((ModMatrix)constants);
}
//Used by gaussianSolve to multiply a row by some scalar
private void multiplyRow(ModSquareMatrix mat,ModMatrix b,int i,BigInteger
scalar) {
//Multiplies row i by scalar-answer replaces i-th row
for (int k=1;k<=mat.numCols;k++)
mat.array[i][k]=BigIntegerMath.lnr(mat.array[i][k].multiply(scalar),mat.modulus);
Search WWH ::




Custom Search