//@Override public override void invert(ZMatrixRMaj A_inv) { double[] vv = decomp._getVV(); ZMatrixRMaj LU = decomp.getLU(); if (A_inv.numCols != LU.numCols || A_inv.numRows != LU.numRows) { throw new ArgumentException("Unexpected matrix dimension"); } int n = A.numCols; double[] dataInv = A_inv.data; int strideAinv = A_inv.getRowStride(); for (int j = 0; j < n; j++) { // don't need to change inv into an identity matrix before hand Array.Clear(vv, 0, n * 2); vv[j * 2] = 1; vv[j * 2 + 1] = 0; decomp._solveVectorInternal(vv); // for( int i = 0; i < n; i++ ) dataInv[i* n +j] = vv[i]; int index = j * 2; for (int i = 0; i < n; i++, index += strideAinv) { dataInv[index] = vv[i * 2]; dataInv[index + 1] = vv[i * 2 + 1]; } } }
/** * Computes the quality of a triangular matrix, where the quality of a matrix * is defined in {@link LinearSolverDense#quality()}. In * this situation the quality is the magnitude of the product of * each diagonal element divided by the magnitude of the largest diagonal element. * If all diagonal elements are zero then zero is returned. * * @return the quality of the system. */ public static double qualityTriangular(ZMatrixRMaj T) { int N = Math.Min(T.numRows, T.numCols); double max = elementDiagMaxMagnitude2(T); if (max == 0.0) { return(0.0); } max = Math.Sqrt(max); int rowStride = T.getRowStride(); double qualityR = 1.0; double qualityI = 0.0; for (int i = 0; i < N; i++) { int index = i * rowStride + i * 2; double real = T.data[index] / max; double imaginary = T.data[index] / max; double r = qualityR * real - qualityI * imaginary; double img = qualityR * imaginary + real * qualityI; qualityR = r; qualityI = img; } return(Math.Sqrt(qualityR * qualityR + qualityI * qualityI)); }
public static void squareConjugate(ZMatrixRMaj mat) { int index = 2; int rowStride = mat.getRowStride(); int indexEnd = rowStride; for (int i = 0; i < mat.numRows; i++, index += (i + 1) * 2, indexEnd += rowStride) { mat.data[index - 1] = -mat.data[index - 1]; int indexOther = (i + 1) * rowStride + i * 2; for (; index < indexEnd; index += 2, indexOther += rowStride) { double real = mat.data[index]; double img = mat.data[index + 1]; mat.data[index] = mat.data[indexOther]; mat.data[index + 1] = -mat.data[indexOther + 1]; mat.data[indexOther] = real; mat.data[indexOther + 1] = -img; } } }
/** * Sets all the diagonal elements equal to one and everything else equal to zero. * If this is a square matrix then it will be an identity matrix. * * @param mat A square matrix. */ public static void setIdentity(ZMatrixRMaj mat) { int width = mat.numRows < mat.numCols ? mat.numRows : mat.numCols; Array.Clear(mat.data, 0, mat.getDataLength()); int index = 0; int stride = mat.getRowStride(); for (int i = 0; i < width; i++, index += stride + 2) { mat.data[index] = 1; } }
/** * A straight forward transpose. Good for small non-square matrices. * * @param A Original matrix. Not modified. * @param A_tran Transposed matrix. Modified. */ public static void standard(ZMatrixRMaj A, ZMatrixRMaj A_tran) { int index = 0; int rowStrideTran = A_tran.getRowStride(); int rowStride = A.getRowStride(); for (int i = 0; i < A_tran.numRows; i++) { int index2 = i * 2; int end = index + rowStrideTran; while (index < end) { A_tran.data[index++] = A.data[index2]; A_tran.data[index++] = A.data[index2 + 1]; index2 += rowStride; } } }
//@Override public override void solve(ZMatrixRMaj b, ZMatrixRMaj x) { if (b.numCols != x.numCols || b.numRows != numRows || x.numRows != numCols) { throw new ArgumentException("Unexpected matrix size"); } int bnumCols = b.numCols; int bstride = b.getRowStride(); double[] dataB = b.data; double[] dataX = x.data; double[] vv = decomp._getVV(); // for( int j = 0; j < numCols; j++ ) { // for( int i = 0; i < this.numCols; i++ ) vv[i] = dataB[i*numCols+j]; // decomp._solveVectorInternal(vv); // for( int i = 0; i < this.numCols; i++ ) dataX[i*numCols+j] = vv[i]; // } for (int j = 0; j < bnumCols; j++) { int index = j * 2; for (int i = 0; i < numRows; i++, index += bstride) { vv[i * 2] = dataB[index]; vv[i * 2 + 1] = dataB[index + 1]; } decomp._solveVectorInternal(vv); index = j * 2; for (int i = 0; i < numRows; i++, index += bstride) { dataX[index] = vv[i * 2]; dataX[index + 1] = vv[i * 2 + 1]; } } }
/** * <p> * Returns the magnitude squared of the complex element along the diagonal with the largest magnitude<br> * <br> * Max{ |a<sub>ij</sub>|^2 } for all i and j<br> * </p> * * @param a A matrix. Not modified. * @return The max magnitude squared */ public static double elementDiagMaxMagnitude2(ZMatrixRMaj a) { int size = Math.Min(a.numRows, a.numCols); int rowStride = a.getRowStride(); double max = 0; for (int i = 0; i < size; i++) { int index = i * rowStride + i * 2; double real = a.data[index]; double imaginary = a.data[index + 1]; double m = real * real + imaginary * imaginary; if (m > max) { max = m; } } return(max); }