/** * @param data dense matrix represented in row-major form * @return solver for the system Ax = b */ static Solver getSolver(double[][] data) { if (data == null) { return null; } RealMatrix M = new Array2DRowRealMatrix(data, false); double infNorm = M.getNorm(); double singularityThreshold = infNorm * SINGULARITY_THRESHOLD_RATIO; RRQRDecomposition decomposition = new RRQRDecomposition(M, singularityThreshold); DecompositionSolver solver = decomposition.getSolver(); if (solver.isNonSingular()) { return new Solver(solver); } // Otherwise try to report apparent rank int apparentRank = decomposition.getRank(0.01); // Better value? log.warn("{} x {} matrix is near-singular (threshold {}). Add more data or decrease the " + "number of features, to <= about {}", M.getRowDimension(), M.getColumnDimension(), singularityThreshold, apparentRank); throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank); }
public double norm1() { return apacheData.getNorm(); }
private static Solver recomputeSolver(FastByIDMap<float[]> M, Lock readLock) { readLock.lock(); try { if (M == null || M.isEmpty()) { return null; } RealMatrix MTM = MatrixUtils.transposeTimesSelf(M); double infNorm = MTM.getNorm(); if (infNorm < 1.0) { log.warn("X'*X or Y'*Y has small inf norm ({}); try decreasing model.als.lambda", infNorm); throw new IllConditionedSolverException("infNorm: " + infNorm); } return MatrixUtils.getSolver(MTM); } finally { readLock.unlock(); } }
private static Solver recomputeSolver(LongObjectMap<float[]> M, Lock readLock) { readLock.lock(); try { if (M == null || M.isEmpty()) { return null; } RealMatrix MTM = MatrixUtils.transposeTimesSelf(M); double infNorm = MTM.getNorm(); if (infNorm < 1.0) { log.warn("X'*X or Y'*Y has small inf norm ({}); try decreasing model.lambda", infNorm); throw new IllConditionedSolverException("infNorm: " + infNorm); } return MatrixUtils.getSolver(MTM); } finally { readLock.unlock(); } }
double normAInf = A.getNorm(); for(int j=0; j<A.getColumnDimension(); j++){ columnNormsList.add(A.getColumnVector(j).getLInfNorm());
/** numIterations >= 0 means use Newton's method with the given numIterations. * numIterations < 0 means use an exact LU decomposition to solve for the inverse. */ public static RealMatrix doInverse(RealMatrix matrix, int numIterations) { // long t = System.currentTimeMillis(); RealMatrix X; if (numIterations >= 0) { int numRows = matrix.getRowDimension(); RealMatrix transpose = matrix.transpose(); X = transpose.scalarMultiply(1.0 / (matrix.getNorm() * transpose.getNorm())); for (int i = 0; i < numIterations; i++) { // max iterations arbitrary X = X.multiply(MatrixUtils.createRealIdentityMatrix(numRows).scalarMultiply(2) .subtract(matrix.multiply(X))); } } else { X = new LUDecomposition(matrix).getSolver().getInverse(); } // log.debug("Inverse time for "+numIterations+" iterations: "+(System.currentTimeMillis()-t)); return X; }
/** * @param data dense matrix represented in row-major form * @return solver for the system Ax = b */ static Solver getSolver(double[][] data) { if (data == null) { return null; } RealMatrix M = new Array2DRowRealMatrix(data, false); double infNorm = M.getNorm(); double singularityThreshold = infNorm * SINGULARITY_THRESHOLD_RATIO; RRQRDecomposition decomposition = new RRQRDecomposition(M, singularityThreshold); DecompositionSolver solver = decomposition.getSolver(); if (solver.isNonSingular()) { return new Solver(solver); } // Otherwise try to report apparent rank int apparentRank = decomposition.getRank(0.01); // Better value? log.warn("{} x {} matrix is near-singular (threshold {}). Add more data or decrease the " + "number of features, to <= about {}", M.getRowDimension(), M.getColumnDimension(), singularityThreshold, apparentRank); throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank); }