📄 matrixops.java
字号:
package javax.math.factorization;/** * <p>Title: Factorization Library</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author Vladimir Silva * @version 1.0 *//** * I am truly sorry about all this awful spagethi code * I got it from: Dario Alejandro Alpern (Buenos Aires - Argentina) * Last updated October 16th, 2003. See http://www.alpertron.com.ar/ECM.HTM * I am not to blame for its crappy design and implementation, but.... * what the hell. it works! * I modified it though, to make it less horrible (extracted the pieces i needed) * If you are a brave soul, try to port this stuff to an object oriented, well-designed * structure. I tried, but just gave up.... */public class MatrixOps { /* Multiply binary matrices of length m x 32 by 32 x 32 */ /* The product matrix has size m x 32. Then add it to a m x 32 matrix. */ static void MatrixMultAdd(int [] LeftMatr, int [] RightMatr, int [] ProdMatr) { int leftMatr; int matrLength = LeftMatr.length; int prodMatr; int row, col; for (row = 0; row < matrLength; row++) { prodMatr = ProdMatr[row]; leftMatr = LeftMatr[row]; col = 0; while (leftMatr != 0) { if (leftMatr < 0) { prodMatr ^= RightMatr[col]; } leftMatr *= 2; col++; } ProdMatr[row] = prodMatr; } } /* Multiply binary matrices of length m x 32 by 32 x 32 */ /* The product matrix has size m x 32 */ static void MatrixMultiplication(int [] LeftMatr, int [] RightMatr, int [] ProdMatr) { int leftMatr; int matrLength = LeftMatr.length; int prodMatr; int row, col; for (row = 0; row < matrLength; row++) { prodMatr = 0; leftMatr = LeftMatr[row]; col = 0; while (leftMatr != 0) { if (leftMatr < 0) { prodMatr ^= RightMatr[col]; } leftMatr *= 2; col++; } ProdMatr[row] = prodMatr; } } /* Multiply the transpose of a binary matrix of length n x 32 by */ /* another binary matrix of length n x 32 */ /* The product matrix has size 32 x 32 */ static void MatrTranspMult(int [] LeftMatr, int [] RightMatr, int [] ProdMatr) { int prodMatr; int matrLength = LeftMatr.length; int row, col; int iMask = 1; for (col = 31; col >= 0; col--) { prodMatr = 0; for (row = 0; row < matrLength; row++) { if ((LeftMatr[row] & iMask) != 0) { prodMatr ^= RightMatr[row]; } } ProdMatr[col] = prodMatr; iMask *= 2; } } static void MatrixAddition(int [] leftMatr, int [] rightMatr, int [] sumMatr) { for (int row = leftMatr.length-1; row >= 0 ; row--) { sumMatr[row] = leftMatr[row] ^ rightMatr[row]; } } static void MatrMultBySSt(int [] Matr, int diagS, int [] Prod) { for (int row = Matr.length-1; row >= 0 ; row--) { Prod[row] = diagS & Matr[row]; } } /* Compute Bt * B * input matrix where B is the matrix that holds the */ /* factorization relations */ static void MultiplyAByMatrix(int [][] matrixB, int [] Matr, int [] TempMatr, int [] ProdMatr) { int index; int prodMatr; int row, col; int [] rowMatrixB; /* Compute TempMatr = B * Matr */ for (row = matrixB.length-1; row >= 0; row--) { TempMatr[row] = 0; } for (row = matrixB.length-1; row >= 0; row--) { rowMatrixB = matrixB[row]; for (index = rowMatrixB.length-1; index>=0; index--) { TempMatr[rowMatrixB[index]] ^= Matr[row]; } } /* Compute ProdMatr = Bt * TempMatr */ for (row = matrixB.length-1; row >= 0; row--) { prodMatr = 0; rowMatrixB = matrixB[row]; for (index = rowMatrixB.length-1; index>=0; index--) { prodMatr ^= TempMatr[rowMatrixB[index]]; } ProdMatr[row] = prodMatr; } } static void colexchange(int [] XmY, int [] V, int [] V1, int [] V2, int col1, int col2) { int i; int mask1, mask2; int [] matr1, matr2; if (col1==col2) { return; } mask1 = 1 << (31-(col1 & 31)); mask2 = 1 << (31-(col2 & 31)); matr1 = (col1 >= 32? V1: V2); matr2 = (col2 >= 32? V1: V2); for (i=V.length-1; i>=0; i--) { if (((matr1[i] & mask1) == 0) != ((matr2[i] & mask2) == 0)) { matr1[i] ^= mask1; matr2[i] ^= mask2; } } matr1 = (col1 >= 32? XmY: V); matr2 = (col2 >= 32? XmY: V); for (i=V.length-1; i>=0; i--) { if (((matr1[i] & mask1) == 0) != ((matr2[i] & mask2) == 0)) { matr1[i] ^= mask1; matr2[i] ^= mask2; } } } static void coladd(int [] XmY, int [] V, int [] V1, int [] V2, int col1, int col2) { int i; int mask1, mask2; int [] matr1, matr2; if (col1==col2) { return; } mask1 = 1 << (31-(col1 & 31)); mask2 = 1 << (31-(col2 & 31)); matr1 = (col1 >= 32? V1: V2); matr2 = (col2 >= 32? V1: V2); for (i=V.length-1; i>=0; i--) { if ((matr1[i] & mask1) != 0) { matr2[i] ^= mask2; } } matr1 = (col1 >= 32? XmY: V); matr2 = (col2 >= 32? XmY: V); for (i=V.length-1; i>=0; i--) { if ((matr1[i] & mask1) != 0) { matr2[i] ^= mask2; } } } public MatrixOps() { } public static void main(String[] args) { MatrixOps matrixOps1 = new MatrixOps(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -