/** * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new matrix must be of type <tt>SparseDoubleMatrix2D</tt>, etc. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public DoubleMatrix2D like2D(int rows, int columns) { return new DenseDoubleMatrix2D(rows,columns); } /**
/** * Constructor. * * @param uIndex fast user index * @param iIndex fast item index * @param K dimension of the latent feature space * @param initFunction function to initialize the cells of the matrices */ public Factorization(FastUserIndex<U> uIndex, FastItemIndex<I> iIndex, int K, DoubleFunction initFunction) { this.userMatrix = new DenseDoubleMatrix2D(uIndex.numUsers(), K); this.userMatrix.assign(initFunction); this.itemMatrix = new DenseDoubleMatrix2D(iIndex.numItems(), K); this.itemMatrix.assign(initFunction); this.K = K; this.uIndex = uIndex; this.iIndex = iIndex; }
private static DoubleMatrix2D getGt(final DenseDoubleMatrix2D p, final DenseDoubleMatrix2D q, double lambda) { final int K = p.columns(); DenseDoubleMatrix2D A1 = new DenseDoubleMatrix2D(K, K); q.zMult(q, A1, 1.0, 0.0, true, false); for (int k = 0; k < K; k++) { A1.setQuick(k, k, lambda + A1.getQuick(k, k)); } EigenvalueDecomposition eig = new EigenvalueDecomposition(A1); DoubleMatrix1D d = eig.getRealEigenvalues(); DoubleMatrix2D gt = eig.getV(); for (int k = 0; k < K; k++) { double a = sqrt(d.get(k)); gt.viewColumn(k).assign(x -> a * x); } return gt; }
public static DenseDoubleMatrix2D convertToColt( RowMajorMatrix orig ) { DenseDoubleMatrix2D mat = new DenseDoubleMatrix2D(orig.numRows,orig.numCols); for( int i = 0; i < orig.numRows; i++ ) { for( int j = 0; j < orig.numCols; j++ ) { mat.set(i,j,orig.get(i,j)); } } return mat; }
public double zSum() { return new DenseDoubleMatrix2D(apacheData.getData()).zSum(); }
private static <U, I, O> void set_min(final DenseDoubleMatrix2D p, final DenseDoubleMatrix2D q, DoubleUnaryOperator confidence, double lambda, FastPreferenceData<U, I> data) { final int K = p.columns(); DenseDoubleMatrix2D A1P = new DenseDoubleMatrix2D(K, K); q.zMult(q, A1P, 1.0, 0.0, true, false); for (int k = 0; k < K; k++) { A1P.setQuick(k, k, lambda + A1P.getQuick(k, k)); DenseDoubleMatrix2D[] A2P = new DenseDoubleMatrix2D[q.rows()]; data.getIidxWithPreferences().parallel().forEach(iidx -> { A2P[iidx] = new DenseDoubleMatrix2D(K, K); DoubleMatrix1D qi = q.viewRow(iidx); ALG.multOuter(qi, qi, A2P[iidx]); }); DoubleMatrix2D A = new DenseDoubleMatrix2D(K, K); DoubleMatrix1D b = new DenseDoubleMatrix1D(K); A.assign(A1P); double cui = confidence.applyAsDouble(rui); DoubleMatrix1D qi = q.viewRow(iidx); lu.decompose(A); lu.solve(b); p.viewRow(uidx).assign(b); });
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseDoubleMatrix2D matA = inputs[0].getOriginal(); DoubleMatrix2D result = new DenseDoubleMatrix2D(matA.rows(),matA.columns()); long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { // in-place operator result.assign(matA); result.assign(cern.jet.math.Functions.mult(BenchmarkConstants.SCALE)); } long elapsed = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new ColtBenchmarkMatrix(result); } return elapsed; } }
public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, boolean transposeA, boolean transposeB) { if (transposeA) return viewDice().zMult(B,C,alpha,beta,false,transposeB); if (B instanceof SparseDoubleMatrix2D || B instanceof RCDoubleMatrix2D) { if (transposeB) return this.zMult(B.viewDice(),C,alpha,beta,transposeA,false); if (C==null) C = new DenseDoubleMatrix2D(m,p); if (!(C instanceof DenseDoubleMatrix2D)) return super.zMult(B,C,alpha,beta,transposeA,transposeB); if (B.rows != n) throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+B.toStringShort()); if (C.rows != m || C.columns != p) throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+B.toStringShort()+", "+C.toStringShort()); if (this == C || B == C) throw new IllegalArgumentException("Matrices must not be identical"); if (m%m_optimal != 0) blocks++; for (; --blocks >= 0; ) { int jB = BB.index(0,0); int indexA = index(rr,0); int jC = CC.index(rr,0); rr += m_optimal; if (blocks==0) m_optimal += m - rr;
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseDoubleMatrix2D matA = inputs[0].getOriginal(); LUDecompositionQuick lu = new LUDecompositionQuick(); DenseDoubleMatrix2D tmp = new DenseDoubleMatrix2D(matA.rows(),matA.columns()); DoubleMatrix2D L = null; DoubleMatrix2D U = null; int pivot[] = null; long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { tmp.assign(matA); lu.decompose(tmp); L = lu.getL(); U = lu.getU(); pivot = lu.getPivot(); if( !lu.isNonsingular() ) throw new DetectedException("Singular matrix"); } long elapsed = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new ColtBenchmarkMatrix(L); outputs[1] = new ColtBenchmarkMatrix(U); outputs[2] = new RowMajorBenchmarkMatrix(RowMajorOps.pivotMatrix(null, pivot, pivot.length, false)); } return elapsed; } }
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseDoubleMatrix2D matA = inputs[0].getOriginal(); DenseDoubleMatrix2D matB = inputs[1].getOriginal(); DoubleMatrix2D result = new DenseDoubleMatrix2D(matA.columns(),matB.columns()); long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { matA.zMult(matB, result, 1, 0, false, true); } long elapsed = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new ColtBenchmarkMatrix(result); } return elapsed; } }
public Matrix mtimes(Matrix m) { if (m instanceof ColtDenseDoubleMatrix2D) { DenseDoubleMatrix2D ret = new DenseDoubleMatrix2D((int) getRowCount(), (int) m.getColumnCount()); matrix.zMult(((ColtDenseDoubleMatrix2D) m).matrix, ret); return new ColtDenseDoubleMatrix2D(ret); } else { return super.mtimes(m); } }
@Override public double error(Factorization<U, I> factorization, FastPreferenceData<U, I> data) { DenseDoubleMatrix2D pu_z = factorization.getUserMatrix(); DenseDoubleMatrix2D piz = factorization.getItemMatrix(); return data.getUidxWithPreferences().parallel().mapToDouble(uidx -> { DoubleMatrix1D pU_z = pu_z.viewRow(uidx); DoubleMatrix1D pUi = piz.zMult(pU_z, null); return data.getUidxPreferences(uidx) .mapToDouble(iv -> -iv.v2 * pUi.getQuick(iv.v1)) .sum(); }).sum(); }
final double[] elems = this.elements; if (elems==null) throw new InternalError(); int index = index(0,0); int cs = this.columnStride; int rs = this.rowStride; double multiplicator = ((cern.jet.math.Mult)function).multiplicator; if (multiplicator==1) return this; if (multiplicator==0) return assign(0); for (int row=rows; --row >= 0; ) { // the general case for (int i=index, column=columns; --column >= 0; ) {
checkShape(other); if (haveSharedCells(other)) { DoubleMatrix2D c = other.copy(); if (! (c instanceof DenseDoubleMatrix2D)) { // should not happen return super.assign(other); int ors = other.rowStride; int otherIndex = other.index(0,0); int index = index(0,0); for (int row=rows; --row >= 0; ) { for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseDoubleMatrix2D matA = inputs[0].getOriginal(); DoubleMatrix2D result = null; long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { CholeskyDecomposition chol = new CholeskyDecomposition(matA); result = chol.solve(DoubleFactory2D.dense.identity(matA.rows())); } long elapsed = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new ColtBenchmarkMatrix(result); } return elapsed; } }
public Matrix copy() { Matrix m = new ColtDenseDoubleMatrix2D((DenseDoubleMatrix2D) matrix.copy()); if (getMetaData() != null) { m.setMetaData(getMetaData().clone()); } return m; }
/** * Constructs a matrix with a copy of the given values. * <tt>values</tt> is required to have the form <tt>values[row][column]</tt> * and have exactly the same number of columns in every row. * <p> * The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa. * * @param values The values to be filled into the new matrix. * @throws IllegalArgumentException if <tt>for any 1 <= row < values.length: values[row].length != values[row-1].length</tt>. */ public DenseDoubleMatrix2D(double[][] values) { this(values.length, values.length==0 ? 0: values[0].length); assign(values); } /**
@Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { DoubleMatrix1D pu; pu = factorization.getUserVector(uidx2user(uidx)); if (pu == null) { return new FastRecommendation(uidx, new ArrayList<>()); } IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, factorization.numItems())); DoubleMatrix1D r = factorization.getItemMatrix().zMult(pu, null); for (int iidx = 0; iidx < r.size(); iidx++) { if (filter.test(iidx)) { topN.add(iidx, r.getQuick(iidx)); } } topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); }