blas.dscal(v.length, 1 / alfa, v, 1); anorm = Math.sqrt(Math.pow(anorm, 2) + Math.pow(alfa, 2) + Math.pow(beta, 2) + Math.pow(damp, 2)); blas.dscal(v.length, -beta, v, 1); blas.dscal(v.length, 1 / alfa, v, 1); blas.dscal(dk.length, 1 / rho, dk, 1); blas.dscal(w.length, t2, w, 1); blas.daxpy(w.length, 1, v, 1, w, 1);
/** * Performs in-place multiplication of vector x by a real scalar a. (x = a * x) * * @param a Scalar a. * @param x Vector x. **/ public static void scal(Double a, Vector x) { if (x.isArrayBased()) f2jBlas.dscal(x.size(), a, x.getStorage().data(), 1); else if (x instanceof SparseVector) { Set<Integer> indexes = ((SparseVector)x).indexes(); for (Integer i : indexes) x.compute(i, (ind, v) -> v * a); } else throw new IllegalArgumentException(); }
/** * Calculates standard deviation by all columns. * * @return Standard deviations. */ public double[] std() { double[] mean = mean(); ValueWithCount<double[]> res = delegate.compute(data -> { double[] features = data.getFeatures(); int rows = data.getRows(); int cols = features.length / rows; double[] y = new double[cols]; for (int col = 0; col < cols; col++) for (int j = col * rows; j < (col + 1) * rows; j++) y[col] += Math.pow(features[j] - mean[col], 2); return new ValueWithCount<>(y, rows); }, (a, b) -> a == null ? b : b == null ? a : new ValueWithCount<>(sum(a.val, b.val), a.cnt + b.cnt)); if (res != null) { blas.dscal(res.val.length, 1.0 / res.cnt, res.val, 1); for (int i = 0; i < res.val.length; i++) res.val[i] = Math.sqrt(res.val[i]); return res.val; } return null; }
/** * Calculates mean value by all columns. * * @return Mean values. */ public double[] mean() { ValueWithCount<double[]> res = delegate.compute((data, partIdx) -> { double[] features = data.getFeatures(); int rows = data.getRows(); int cols = features.length / rows; double[] y = new double[cols]; for (int col = 0; col < cols; col++) for (int j = col * rows; j < (col + 1) * rows; j++) y[col] += features[j]; return new ValueWithCount<>(y, rows); }, (a, b) -> a == null ? b : b == null ? a : new ValueWithCount<>(sum(a.val, b.val), a.cnt + b.cnt)); if (res != null) { blas.dscal(res.val.length, 1.0 / res.cnt, res.val, 1); return res.val; } return null; }
/** {@inheritDoc} */ @Override protected double[] iter(double bnorm, double[] target) { double[] res = dataset.computeWithCtx((ctx, data) -> { if (data.getFeatures() == null) return null; int cols = data.getFeatures().length / data.getRows(); BLAS.getInstance().dscal(ctx.getU().length, 1 / bnorm, ctx.getU(), 1); double[] v = new double[cols]; BLAS.getInstance().dgemv("T", data.getRows(), cols, 1.0, data.getFeatures(), Math.max(1, data.getRows()), ctx.getU(), 1, 0, v, 1); return v; }, (a, b) -> { if (a == null) return b; else if (b == null) return a; else { BLAS.getInstance().daxpy(a.length, 1.0, a, 1, b, 1); return b; } }); BLAS.getInstance().daxpy(res.length, 1.0, res, 1, target, 1); return target; }
@Override protected void dscal(int N, double alpha, INDArray X, int incX) { double[] data = getDoubleData(X); BLAS.getInstance().dscal(N, alpha, data, BlasBufferUtil.getBlasOffset(X), incX); setData(data,X); }
/** * Multiply the given ndarray * by alpha * * @param alpha * @param x * @return */ public static INDArray scal(double alpha, INDArray x) { DataTypeValidation.assertDouble(x); BLAS.getInstance().dscal( x.length(), alpha, x.data().asDouble(), x.offset(), x.majorStride()); return x; }
/** * Multiply the given ndarray * by alpha * * @param alpha * @param x * @return */ public static INDArray scal(double alpha, INDArray x) { if(x.data().dataType() == DataBuffer.Type.FLOAT) { return scal((float) alpha,x); } DataTypeValidation.assertDouble(x); BLAS.getInstance().dscal( x.length(), alpha, x.data().asDouble(), x.offset(), x.majorStride()); return x; }