/** * Computes the "cost" for the parameters given. * * cost = (1/N) Sum (f(x;p) - y)^2 */ private double cost(DMatrixRMaj param, DMatrixRMaj X, DMatrixRMaj Y) { func.compute(param, X, temp0); double error = SpecializedOps_DDRM.diffNormF(temp0, Y); return(error * error / (double)X.numRows); }
public static double quality(DMatrixRMaj orig, DMatrixRMaj U, DMatrixRMaj W, DMatrixRMaj Vt) { // foundA = U*W*Vt DMatrixRMaj UW = new DMatrixRMaj(U.numRows, W.numCols); CommonOps_DDRM.mult(U, W, UW); DMatrixRMaj foundA = new DMatrixRMaj(UW.numRows, Vt.numCols); CommonOps_DDRM.mult(UW, Vt, foundA); double normA = NormOps_DDRM.normF(foundA); return(SpecializedOps_DDRM.diffNormF(orig, foundA) / normA); }
/** * An other implementation of solve() that processes the matrices in a different order. * It seems to have the same runtime performance as {@link #solve} and is more complicated. * It is being kept around to avoid future replication of work. * * @param b A matrix that is n by m. Not modified. * @param x An n by m matrix where the solution is writen to. Modified. */ //@Override public override void solve(DMatrixRMaj b, DMatrixRMaj x) { if (b.numCols != x.numCols || b.numRows != numRows || x.numRows != numCols) { throw new ArgumentException("Unexpected matrix size"); } if (b != x) { SpecializedOps_DDRM.copyChangeRow(pivot, b, x); } else { throw new ArgumentException("Current doesn't support using the same matrix instance"); } // Copy right hand side with pivoting int nx = b.numCols; double[] dataX = x.data; // Solve L*Y = B(piv,:) for (int k = 0; k < numCols; k++) { for (int i = k + 1; i < numCols; i++) { for (int j = 0; j < nx; j++) { dataX[i * nx + j] -= dataX[k * nx + j] * dataLU[i * numCols + k]; } } } // Solve U*X = Y; for (int k = numCols - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { dataX[k * nx + j] /= dataLU[k * numCols + k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { dataX[i * nx + j] -= dataX[k * nx + j] * dataLU[i * numCols + k]; } } } }
private void solveUsingTriangle(double real, int index, DMatrixRMaj r) { for (int i = 0; i < index; i++) { _implicit.A.add(i, i, -real); } SpecializedOps_DDRM.subvector(_implicit.A, 0, index, index, false, 0, r); CommonOps_DDRM.changeSign(r); TriangularSolver_DDRM.solveU(_implicit.A.data, r.data, _implicit.A.numRows, 0, index); for (int i = 0; i < index; i++) { _implicit.A.add(i, i, real); } }
/// <summary> /// Extracts a row or column from this matrix. /// The returned vector will either be a row /// or column vector depending on the input type. /// </summary> /// <param name="extractRow">If true a row will be extracted.</param> /// <param name="element">The row or column the vector is contained in.</param> /// <returns>Extracted vector.</returns> public override SimpleMatrixD extractVector(bool extractRow, int element) { int length = extractRow ? mat.getNumCols() : mat.getNumRows(); SimpleMatrixD ret = extractRow ? createMatrix(1, length) : createMatrix(length, 1); var rm = ret.getMatrix(); if (extractRow) { SpecializedOps_DDRM.subvector(mat, element, 0, length, true, 0, rm); } else { SpecializedOps_DDRM.subvector(mat, 0, element, length, false, 0, rm); } return(ret); }
/** * Computes the most dominant eigen vector of A using an inverted shifted matrix. * The inverted shifted matrix is defined as <b>B = (A - αI)<sup>-1</sup></b> and * can converge faster if α is chosen wisely. * * @param A An invertible square matrix matrix. * @param alpha Shifting factor. * @return If it converged or not. */ public bool computeShiftInvert(DMatrixRMaj A, double alpha) { initPower(A); LinearSolverDense <DMatrixRMaj> solver = LinearSolverFactory_DDRM.linear(A.numCols); SpecializedOps_DDRM.addIdentity(A, B, -alpha); solver.setA(B); bool converged = false; for (int i = 0; i < maxIterations && !converged; i++) { solver.solve(q0, q1); double s = NormOps_DDRM.normPInf(q1); CommonOps_DDRM.divide(q1, s, q2); converged = checkConverged(A); } return(converged); }
private void solveWithLU(double real, int index, DMatrixRMaj r) { DMatrixRMaj A = new DMatrixRMaj(index, index); CommonOps_DDRM.extract(_implicit.A, 0, index, 0, index, A, 0, 0); for (int i = 0; i < index; i++) { A.add(i, i, -real); } r.reshape(index, 1, false); SpecializedOps_DDRM.subvector(_implicit.A, 0, index, index, false, 0, r); CommonOps_DDRM.changeSign(r); // TODO this must be very inefficient if (!solver.setA(A)) { throw new InvalidOperationException("Solve failed"); } solver.solve(r, r); }
public virtual DMatrixRMaj getRowPivot(DMatrixRMaj pivot) { return(SpecializedOps_DDRM.pivotMatrix(pivot, this.pivot, LU.numRows, false)); }
/** * Computes the most dominant eigen vector of A using a shifted matrix. * The shifted matrix is defined as <b>B = A - αI</b> and can converge faster * if α is chosen wisely. In general it is easier to choose a value for α * that will converge faster with the shift-invert strategy than this one. * * @param A The matrix. * @param alpha Shifting factor. * @return If it converged or not. */ public bool computeShiftDirect(DMatrixRMaj A, double alpha) { SpecializedOps_DDRM.addIdentity(A, B, -alpha); return(computeDirect(B)); }