📄 pnmatrix.java
字号:
*/ public int getRowDimension () { return m; }//###################################################################################### /** * Get column dimension. * @return The number of columns. */ public int getColumnDimension () { return n; }//###################################################################################### /** * Find the first non-zero row of a matrix. * @return Row index (starting from 0 for 1st row) of the first row from top that is not only zeros, -1 of there is no such row. */ public int firstNonZeroRowIndex() { int m = getRowDimension(), n = getColumnDimension(); int h = -1; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (get(i, j) != 0) return i; } } return h; }//###################################################################################### /** * Form a matrix with columns the row indices of non-zero elements. * @return The matrix with columns the row indices of non-zero elements. First row has index 1. */ public PNMatrix nonZeroIndices() { PNMatrix X = new PNMatrix(m, n); for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ if ( get(i, j) == 0 ) X.set(i, j, 0); else X.set(i, j, i+1); } } return X; }//###################################################################################### /** * Find a column with non-minimal support. * @return The column index that has non-minimal support, -1 if there is none. */ public int findNonMinimal() { int k = -1; // the non-minimal support column index int m = getRowDimension(), n = getColumnDimension(); PNMatrix X = new PNMatrix(m, 1); // column one, represents first col of comparison PNMatrix Y = new PNMatrix(m, 1); // col two, represents rest columns of comparison PNMatrix Z = new PNMatrix(m, 1); // difference column 1 - column 2 for (int i = 0; i < n ; i++){ X = getMatrix(0, m-1, i, i); for (int j = 0; j < n; j++){ if (i != j){ Y = getMatrix(0, m-1, j, j); Z = X.minus(Y); // if there is at least one -ve element then break inner loop // and try another Y vector (because X is minimal with respect to Y) // if there is no -ve element then return from the function with index of X // to be eliminated, because X is not minimal with respect to Y if (!(Z.hasNegativeElements())) return i; } } } return k; // compare each columns' set with the other columns // if you find during comparison with another another column that it has // one more element, stop the comparison (the current col cannot be eliminated // based on this comparison) and go to the next comparison // if you find that the col in question has all the elements of another one // then eliminate the col in question }//###################################################################################### /** * Find if a column vector has negative elements. * @return True or false. */ public boolean hasNegativeElements() { boolean hasNegative = false; int m = getRowDimension(); for (int i = 0; i < m ; i++) if (get(i, 0) < 0) return true; return hasNegative; }//###################################################################################### /** * Find the column index of the first non zero element of row h. * @param h The row to look for the non-zero element in * @return Column index (starting from 0 for 1st column) of the first non-zero element of row h, -1 if there is no such column. */ public int firstNonZeroElementIndex(int h) { int n = getColumnDimension(); int k = -1; for (int j = 0; j < n; j++) { if (get(h, j) != 0) return j; } return k; }//###################################################################################### /** * Find the column indices of all but the first non zero elements of row h. * @param h The row to look for the non-zero element in * @return Array of ints of column indices (starting from 0 for 1st column) of all but the first non-zero elements of row h. */ public int[] findRemainingNZIndices(int h) { int n = getColumnDimension(); int[] k = new int[n]; int count = 0; // increases as we add new indices in the array of ints for (int j = 1; j < n; j++) { if (get(h, j) != 0) k[count++] = j; } return k; }//###################################################################################### /** * Find the coefficients corresponding to column indices of all but the first non zero elements of row h. * @param h The row to look for the non-zero coefficients in * @return Array of ints of coefficients of all but the first non-zero elements of row h. */ public int[] findRemainingNZCoef(int h) { int n = getColumnDimension(); int[] k = new int[n]; int count = 0; // increases as we add new indices in the array of ints int anElement; // an element of the matrix for (int j = 1; j < n; j++) { if ((anElement = get(h, j)) != 0) k[count++] = anElement; } return k; }//###################################################################################### /** * Get a single element. * @param i Row index. * @param j Column index. * @return A(i,j) * @exception ArrayIndexOutOfBoundsException */ public int get (int i, int j) { return A[i][j]; }//###################################################################################### /** Get a submatrix. * @param i0 Initial row index * @param i1 Final row index * @param j0 Initial column index * @param j1 Final column index * @return A(i0:i1,j0:j1) * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public PNMatrix getMatrix (int i0, int i1, int j0, int j1) { PNMatrix X = new PNMatrix(i1-i0+1,j1-j0+1); int[][] B = X.getArray(); try { for (int i = i0; i <= i1; i++) { for (int j = j0; j <= j1; j++) { B[i-i0][j-j0] = A[i][j]; } } } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } return X; }//###################################################################################### /** * Get a submatrix. * @param r Array of row indices. * @param c Array of column indices. * @return A(r(:),c(:)) * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public PNMatrix getMatrix (int[] r, int[] c) { PNMatrix X = new PNMatrix(r.length,c.length); int[][] B = X.getArray(); try { for (int i = 0; i < r.length; i++) { for (int j = 0; j < c.length; j++) { B[i][j] = A[r[i]][c[j]]; } } } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } return X; }//###################################################################################### /** * Get a submatrix. * @param i0 Initial row index * @param i1 Final row index * @param c Array of column indices. * @return A(i0:i1,c(:)) * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public PNMatrix getMatrix (int i0, int i1, int[] c) { PNMatrix X = new PNMatrix(i1-i0+1,c.length); int[][] B = X.getArray(); try { for (int i = i0; i <= i1; i++) { for (int j = 0; j < c.length; j++) { B[i-i0][j] = A[i][c[j]]; } } } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } return X; }//###################################################################################### /** * Get a submatrix. * @param r Array of row indices. * @param j0 Initial column index * @param j1 Final column index * @return A(r(:),j0:j1) * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public PNMatrix getMatrix (int[] r, int j0, int j1) { PNMatrix X = new PNMatrix(r.length,j1-j0+1); int[][] B = X.getArray(); try { for (int i = 0; i < r.length; i++) { for (int j = j0; j <= j1; j++) { B[i][j-j0] = A[r[i]][j]; } } } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } return X; }//###################################################################################### /** * For row rowNo of the matrix received return the column indices of all the negative elements * @param rowNo row iside the Matrix to check for -ve elements * @return Integer array of indices of negative elements. * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public int[] getNegativeIndices (int rowNo) { int n = getColumnDimension(); // find the number of columns // create the single row submatrix for the required row try { PNMatrix A = new PNMatrix(1, n); A = getMatrix(rowNo, rowNo, 0, n-1); int count = 0; // index of a negative element in the returned array int[] negativesArray = new int[n]; for (int i = 0; i < n; i++) // initialise to zero negativesArray[i] = 0; for (int i = 0; i < n; i++) { if (A.get(0, i) < 0) negativesArray[count++] = i + 1; } return negativesArray; } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } }//###################################################################################### /** * For row rowNo of the matrix received return the column indices of all the positive elements * @param rowNo row iside the Matrix to check for +ve elements * @return The integer array of indices of all positive elements. * @exception ArrayIndexOutOfBoundsException Submatrix indices */ public int[] getPositiveIndices (int rowNo) { int n = getColumnDimension(); // find the number of columns // create the single row submatrix for the required row try { PNMatrix A = new PNMatrix(1, n); A = getMatrix(rowNo, rowNo, 0, n-1); int count = 0; // index of a positive element in the returned array int[] positivesArray = new int[n]; for (int i = 0; i < n; i++) // initialise to zero positivesArray[i] = 0; for (int i = 0; i < n; i++) { if (A.get(0, i) > 0) positivesArray[count++] = i + 1; } return positivesArray; } catch(ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException("Submatrix indices"); } }//###################################################################################### /** * Check if a matrix is all zeros. * @return true if all zeros, false otherwise */ public boolean isZeroMatrix () { int m = getRowDimension(), n = getColumnDimension(); boolean isZero = true; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (get(i, j) != 0) return false; } } return isZero; }//###################################################################################### /** * isZeroRow returns true if the ith row is all zeros * @param r row to check for full zeros. * @return true if the row is full of zeros. */ public boolean isZeroRow (int r) { PNMatrix A = new PNMatrix(1, getColumnDimension()); A = getMatrix(r, r, 0, getColumnDimension()-1); return A.isZeroMatrix(); }//###################################################################################### /** * Find if a matrix of invariants is covered. * @return true if it is covered, false otherwise. */ public boolean isCovered(){ boolean isCovered = true; // if there is an all-zeros row then it is not covered for (int i = 0; i < m; i++){ if (isZeroRow(i) || this.transpose().hasNegativeElements()) return false; } return isCovered; }//###################################################################################### /** * Add a linear combination of column k to columns in array j[]. * @param k Column index to add. * @param chk Coefficient of col to add * @param j Array of column indices to add to. * @param jC Array of coefficients of column indices to add to. * @exception ArrayIndexOutOfBoundsException */ public void linearlyCombine(int k, int chk, int[] j, int[] jC){ // k is column index of coefficient of col to add // chj is coefficient of col to add int chj = 0; // coefficient of column to add to int m = getRowDimension(); for (int i = 0; i < j.length; i++){ if (j[i] != 0){ chj = jC[i]; // System.out.print("\nchk = " + chk + "\n"); for (int w = 0; w < m; w++) set(w, j[i]-1, chj*get(w, k)+chk*get(w, j[i]-1)); } } }//###################################################################################### /** * Add a linear combination of column k to columns in array j[]. * @param k Column index to add. * @param alpha Array of coefficients of col to add * @param j Array of column indices to add to. * @param beta Array of coefficients of column indices to add to. * @exception ArrayIndexOutOfBoundsException */ public void linearlyCombine(int k, int[] alpha, int[] j, int[] beta){ // k is column index of coefficient of col to add // a is array of coefficients of col to add //int chk = 0; // coefficient of column to add to int m = getRowDimension(), n = j.length; for (int i = 0; i < n; i++){ if (j[i] != 0){ //chk = jC[i]; // System.out.print("\nchk = " + chk + "\n"); for (int w = 0; w < m; w++) // for all the elements in a column set(w, j[i], alpha[i]*get(w, k)+beta[i]*get(w, j[i])); } } }//###################################################################################### /** * Find the first row with a negative element in a matrix. * @return Row index (starting from 0 for 1st row) of the first row from top that is has a negative element, -1 of there is no such row. */ public int rowWithNegativeElement() { int m = getRowDimension(), n = getColumnDimension(); int h = -1; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (get(i, j) < 0) return i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -