Refine search
public Matrix transAmultAdd(double alpha, Matrix B, Matrix C) { checkTransAmultAdd(B, C); if (alpha != 0) for (int i = 0; i < numColumns; ++i) for (int j = 0; j < C.numColumns(); ++j) { double dot = 0; for (int k = 0; k < numRows; ++k) dot += get(k, i) * B.get(k, j); C.add(i, j, alpha * dot); } return C; }
/** * Populates a matrix with random numbers drawn from a uniform distribution * between 0 and 1 * * @param A * Matrix to populate */ public static Matrix random(Matrix A) { for (int j = 0; j < A.numColumns(); ++j) for (int i = 0; i < A.numRows(); ++i) A.set(i, j, Math.random()); return A; }
/** * Constructor for AbstractMatrix, same size as A. The invoking constructor * should set this matrix equal the argument matrix */ protected AbstractMatrix(Matrix A) { this(A.numRows(), A.numColumns()); }
public Vector apply(Vector b, Vector x) { // R'y = b, y = R'\b Rt.transSolve(b, y); // Rx = R'\b = y return Rt.solve(y, x); }
/** * Creates an LU decomposition of the given matrix * * @param A * Matrix to decompose. Not modified * @return The current decomposition */ public static DenseLU factorize(Matrix A) { return new DenseLU(A.numRows(), A.numColumns()).factor(new DenseMatrix( A)); }
Vector diff = instanceToVector(inst); diff = diff.add(-1.0, perClassMeans[(int) inst.classValue()]); Cw = Cw.rank1(inst.weight(), diff); for (int i = 0; i < Cw.numColumns(); i++) { Cw.add(i, i, m_Ridge); Vector diff = perClassMeans[i].copy(); diff = diff.add(-1.0, globalMean); Cb = Cb.rank1(perClassWeights[i], diff); for (int i = 0; i < evs.length; i++) { if (evs[i] > 0) { D.set(i, i, Math.sqrt(1.0 / evs[i])); } else { throw new IllegalArgumentException("Found non-positive eigenvalue of within-class scatter matrix."); System.err.println("evCw : \n" + evCw); System.err.println("Sqrt of reciprocal of eigenvalues of Cw: \n" + D); System.err.println("evCw times evCwTransposed : \n" + evCw.mult(evCw.transpose(new DenseMatrix(m,m)), new DenseMatrix(m, m))); Matrix temp = evCw.mult(D, new DenseMatrix(m, m)); Matrix sqrtCwInverse = temp.mult(evCw.transpose(), new UpperSymmDenseMatrix(m)); for (int i = 0; i < sqrtCwInverse.numRows(); i++) { for (int j = 0; j < sqrtCwInverse.numColumns(); j++) { System.err.print(sqrtCwInverse.get(i, j) + "\t"); System.err.println("sqrtCwInverse times sqrtCwInverse : \n" + sqrtCwInverse.mult(sqrtCwInverse, new DenseMatrix(m, m)));
independentTransposed.set(index, i, value * sqrt_weight); } else { independent.set(i, index, value * sqrt_weight); double sqrtRidge = Math.sqrt(m_Ridge); for (int i = 0; i < numAttributes; i++) { independent.set(m_TransformedData.numInstances() + i, i, sqrtRidge); Vector aTy = independentTransposed.mult(dependent, new DenseVector(numAttributes)); Matrix aTa = new UpperSPDDenseMatrix(numAttributes).rank1(independentTransposed); independentTransposed = null; aTa.add(i, i, ridge); Vector coeffsWithoutIntercept = aTa.solve(aTy, new DenseVector(numAttributes)); System.arraycopy(((DenseVector) coeffsWithoutIntercept).getData(), 0, coefficients, 0, numAttributes); } else { // Use QR decomposition Matrix R = new UpperTriangDenseMatrix(qrp.getR(), false); Matrix P = qrp.getP(); DenseVector cPlusd = (DenseVector)Q.transMult(dependent, new DenseVector(dependent.size())); dependent = null; Vector c = new DenseVector(Arrays.copyOf(cPlusd.getData(), numAttributes)); Vector y = R.solve(c, new DenseVector(numAttributes)); Vector coeffsWithoutIntercept = P.mult(y, new DenseVector(numAttributes)); System.arraycopy(((DenseVector) coeffsWithoutIntercept).getData(), 0, coefficients, 0, numAttributes);
@Override public Matrix transAmult(double alpha, Matrix B, Matrix C) { if (!(C instanceof DenseMatrix)) return super.transAmult(alpha, B, C); checkTransAmultAdd(B, C); double[] Cd = ((DenseMatrix) C).getData(); C.set(B); // C = alpha*A'*C BLAS.getInstance().dtrmm(Side.Left.netlib(), uplo.netlib(), Transpose.Transpose.netlib(), diag.netlib(), C.numRows(), C.numColumns(), alpha, data, Math.max(1, ld), Cd, Math.max(1, C.numRows())); return C; }
// create matrix A double[][] matValues = new double[n][n]; ... // initialize values of the matrix Matrix A = new DenseMatrix(matValues); // create vector x double[] vecValues = new double[n]; ... // initialize values of the vector Vector x = new DenseVector(vecValues); // create vector y to store result of multiplication Vector y = new DenseVector(n); // perform multiplication A.mult(x, y);
/** * Returns the column pivoting matrix. This function allocates a new Matrix * to be returned, a more cheap option is tu use {@link #getPVector()}. */ public Matrix getP() { Matrix P = new DenseMatrix(jpvt.length, jpvt.length); for (int i = 0; i < jpvt.length; i++) { P.set(jpvt[i], i, 1); } return P; }
Matrix scatter = new UpperSymmDenseMatrix(data[0].numRows()).rank1(data[0]). add(new UpperSymmDenseMatrix(data[1].numRows()).rank1(data[1])); for (int i = 0; i < scatter.numColumns(); i++) { scatter.add(i, i, m_Ridge); m_Weights = scatter.solve(diff, new DenseVector(scatter.numColumns())); m_Weights.scale(1.0 / m_Weights.norm(Vector.Norm.Two));
public void setMatrix(Matrix A) { if (A.numRows() != invdiag.length) throw new IllegalArgumentException( "Matrix size differs from preconditioner size"); for (int i = 0; i < invdiag.length; ++i) { invdiag[i] = A.get(i, i); if (invdiag[i] == 0) // Avoid zero-division throw new RuntimeException("Zero diagonal on row " + (i + 1)); else invdiag[i] = 1 / invdiag[i]; } }
public void setElement( int rowIndex, int columnIndex, double value) { this.internalMatrix.set(rowIndex, columnIndex, value); }
private void av(double[] work, int input_offset, int output_offset) { DenseVector w = new DenseVector(work, false); Vector x = new DenseVectorSub(w, input_offset, matrix.numColumns()); Vector y = new DenseVectorSub(w, output_offset, matrix.numColumns()); matrix.mult(x, y); } }
public Vector solve(Matrix A, Vector b, Vector x) throws IterativeSolverNotConvergedException { checkSizes(A, b, x); double alpha = 0, beta = 0, c = 0, d = 0; A.multAdd(-1, x, r.set(b)); c = (eigmax - eigmin) / 2.0; d = (eigmax + eigmin) / 2.0; for (iter.setFirst(); !iter.converged(r, x); iter.next()) { M.apply(r, z); if (iter.isFirst()) { p.set(z); alpha = 2.0 / d; } else { beta = (alpha * c) / 2.0; beta *= beta; alpha = 1.0 / (d - beta); p.scale(beta).add(z); } A.mult(p, q); x.add(alpha, p); r.add(-alpha, q); } return x; }
/** * Convenience method for computing the complete eigenvalue decomposition of * the given matrix * * @param A * Matrix to factorize. Not modified * @return Newly allocated decomposition * @throws NotConvergedException */ public static EVD factorize(Matrix A) throws NotConvergedException { return new EVD(A.numRows()).factor(new DenseMatrix(A)); }
/** * Takes a batch of data and transforms it. * * @param instances the data to process * @return the processed instances * @throws Exception is thrown if a problem occurs */ @Override protected Instances process(Instances instances) throws Exception { Instances transformed = getOutputFormat(); for (Instance inst : instances) { Vector newInst = m_WeightingMatrix.mult(instanceToVector(inst), new DenseVector(m_WeightingMatrix.numRows())); double[] newVals = new double[m_WeightingMatrix.numRows() + 1]; for (int i = 0; i < m_WeightingMatrix.numRows(); i++) { newVals[i] = newInst.get(i); } newVals[transformed.classIndex()] = inst.classValue(); transformed.add(new DenseInstance(inst.weight(), newVals)); } return transformed; }
@Override public Matrix transRank2(double alpha, Matrix B, Matrix C) { if (!(B instanceof DenseMatrix) || !(C instanceof DenseMatrix)) return super.transRank2(alpha, B, C); checkTransRank2(B, C); double[] Bd = ((DenseMatrix) B).getData(), Cd = ((DenseMatrix) C) .getData(); BLAS.getInstance().dsyr2k(uplo.netlib(), Transpose.Transpose.netlib(), numRows, B.numRows(), alpha, Bd, Math.max(1, B.numRows()), Cd, Math.max(1, B.numRows()), 1, data, Math.max(1, numRows)); return this; }