📄 matrices.java
字号:
} @Override public void set(int index, double value) { x.set(this.index[index], value); } } /** * Ensures correctness in the vector assembly. Since it extends the * AbstractVector class, algebraic operations will be slow. It is not * possible to implement Vector and delegate calls to the imbedded vector, * since casting to the imbedded vector is not possible */ private static class SynchronizedVector extends AbstractVector { private Vector x; public SynchronizedVector(Vector x) { super(x); this.x = x; } @Override public synchronized void add(int index, double value) { x.add(index, value); } @Override public synchronized void set(int index, double value) { x.set(index, value); } @Override public synchronized double get(int index) { return x.get(index); } @Override public Vector copy() { return Matrices.synchronizedVector(x.copy()); } } /** * Ensures correctness in the matrix assembly. Since it extends the * AbstractMatrix class, algebraic operations will be slow. It is not * possible to implement Matrix and delegate calls to the imbedded matrix, * since casting to the imbedded matrix is not possible */ private static class SynchronizedMatrix extends AbstractMatrix { private Matrix A; public SynchronizedMatrix(Matrix A) { super(A); this.A = A; } @Override public synchronized void add(int row, int column, double value) { A.add(row, column, value); } @Override public synchronized void set(int row, int column, double value) { A.set(row, column, value); } @Override public synchronized double get(int row, int column) { return A.get(row, column); } @Override public Matrix copy() { return Matrices.synchronizedMatrix(A.copy()); } } /** * Ensures correctness in the matrix assembly. Since it extends the * AbstractMatrix class, algebraic operations will be slow. It is not * possible to implement Matrix and delegate calls to the imbedded matrix, * since casting to the imbedded matrix is not possible * <p> * Locks individual rows instead of the whole matrix */ private static class SynchronizedRowMatrix extends AbstractMatrix { private Matrix A; private Object[] lock; public SynchronizedRowMatrix(Matrix A) { super(A); this.A = A; lock = new Object[A.numRows()]; for (int i = 0; i < lock.length; ++i) lock[i] = new Object(); } @Override public void add(int row, int column, double value) { synchronized (lock[row]) { A.add(row, column, value); } } @Override public void set(int row, int column, double value) { synchronized (lock[row]) { A.set(row, column, value); } } @Override public double get(int row, int column) { return A.get(row, column); } @Override public Matrix copy() { return Matrices.synchronizedMatrixByRows(A.copy()); } } /** * Ensures correctness in the matrix assembly. Implements matrix instead of * subclassing the abstract matrix in order to correctly delegate every * method to possbly overridden method in the encapsulated matrix. * <p> * Locks individual columns instead of the whole matrix */ private static class SynchronizedColumnMatrix extends AbstractMatrix { private Matrix A; private Object[] lock; public SynchronizedColumnMatrix(Matrix A) { super(A); this.A = A; lock = new Object[A.numColumns()]; for (int i = 0; i < lock.length; ++i) lock[i] = new Object(); } @Override public void add(int row, int column, double value) { synchronized (lock[column]) { A.add(row, column, value); } } @Override public void set(int row, int column, double value) { synchronized (lock[column]) { A.set(row, column, value); } } @Override public double get(int row, int column) { return A.get(row, column); } @Override public Matrix copy() { return Matrices.synchronizedMatrixByColumns(A.copy()); } } /** * Creates a continuous linear index. * * @param from * Start, inclusive * @param to * Stop, exclusive */ public static int[] index(int from, int to) { int length = to - from; if (length < 0) length = 0; int[] index = new int[length]; for (int i = from, j = 0; j < length; ++i, ++j) index[j] = i; return index; } /** * Creates a strided linear index. * * @param from * Start, inclusive * @param stride * <code>stride=1</code> for continuous. Negative strides are * allowed * @param to * Stop, exclusive */ public static int[] index(int from, int stride, int to) { if (stride == 1) return index(from, to); else if (stride == 0) return new int[0]; if (to <= from && stride > 0) return new int[0]; if (from <= to && stride < 0) return new int[0]; int length = Math.abs((to - from) / stride); if (Math.abs((to - from) % stride) > 0) length++; if (length < 0) length = 0; int[] index = new int[length]; for (int i = from, j = 0; j < length; i += stride, ++j) index[j] = i; return index; } /** * Finds the number of non-zero entries on each row */ public static int[] rowBandwidth(Matrix A) { int[] nz = new int[A.numRows()]; for (MatrixEntry e : A) nz[e.row()]++; return nz; } /** * Finds the number of non-zero entries on each column */ public static int[] columnBandwidth(Matrix A) { int[] nz = new int[A.numColumns()]; for (MatrixEntry e : A) nz[e.column()]++; return nz; } /** * Finds the number of diagonals below the main diagonal. Useful for * converting a general matrix into a banded matrix */ public static int getNumSubDiagonals(Matrix A) { int kl = 0; for (MatrixEntry e : A) kl = Math.max(kl, e.row() - e.column()); return kl; } /** * Finds the number of diagonals above the main diagonal. Useful for * converting a general matrix into a banded matrix */ public static int getNumSuperDiagonals(Matrix A) { int ku = 0; for (MatrixEntry e : A) ku = Math.max(ku, e.column() - e.row()); return ku; } /** * Sets the selected rows of <code>A</code> equal zero, and puts * <code>diagonal</code> on the diagonal of those rows. Useful for * enforcing boundary conditions */ public static void zeroRows(Matrix A, double diagonal, int... row) { // Sort the rows int[] rowS = row.clone(); Arrays.sort(rowS); for (MatrixEntry e : A) { int j = java.util.Arrays.binarySearch(rowS, e.row()); if (j >= 0) { // Found if (e.row() == e.column()) // Diagonal e.set(diagonal); else // Off diagonal e.set(0); } } // Ensure the diagonal is set. This is necessary in case of missing // rows if (diagonal != 0) for (int rowI : row) A.set(rowI, rowI, diagonal); } /** * Sets the selected columns of <code>A</code> equal zero, and puts * <code>diagonal</code> on the diagonal of those columns. Useful for * enforcing boundary conditions */ public static void zeroColumns(Matrix A, double diagonal, int... column) { // Sort the columns int[] columnS = column.clone(); Arrays.sort(columnS); for (MatrixEntry e : A) { int j = java.util.Arrays.binarySearch(columnS, e.column()); if (j >= 0) { // Found if (e.row() == e.column()) // Diagonal e.set(diagonal); else // Off diagonal e.set(0); } } // Ensure the diagonal is set. This is necessary in case of missing // columns if (diagonal != 0) for (int columnI : column) A.set(columnI, columnI, diagonal); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -