Refine search
/** * Calculates the principal component vectors and their eigenvalues (lambda) for the covariance matrix. * The result includes two things: the eigenvectors (modes) as result[0] and the eigenvalues (lambda) * as result[1]. * * @param cov The covariance matrix (calculated with the covarianceMatrix(in) method) * @return Array INDArray[2] "result". The principal component vectors in decreasing flexibility are * the columns of element 0 and the eigenvalues are element 1. */ public static INDArray[] principalComponents(INDArray cov) { assert cov.rows() == cov.columns(); INDArray[] result = new INDArray[2]; result[0] = Nd4j.eye(cov.rows()); result[1] = Eigen.symmetricGeneralizedEigenvalues(result[0], cov, true); return result; }
/** * Get the dimension associated with * the given ordering. * * When working with blas routines, they typically assume * c ordering, instead you can invert the rows/columns * which enable you to do no copy blas operations. * * * * @param arr * @param defaultRows * @return */ public static int getDimension(INDArray arr, boolean defaultRows) { // FIXME: int cast //ignore ordering for vectors if (arr.isVector()) { return defaultRows ? (int) arr.rows() : (int) arr.columns(); } if (arr.ordering() == NDArrayFactory.C) return defaultRows ? (int) arr.columns() : (int) arr.rows(); return defaultRows ? (int) arr.rows() : (int) arr.columns(); }
/** * * matrix multiply: implements op(a)*op(b) * * where op(x) means transpose x (or not) depending on * setting of arguments transposea and transposeb.<br> * so gemm(a,b,false,false) == a.mmul(b), gemm(a,b,true,false) == a.transpose().mmul(b) etc. * @param a first matrix * @param b second matrix * @param transposeA if true: transpose matrix a before mmul * @param transposeB if true: transpose matrix b before mmul * @return result */ public static INDArray gemm(INDArray a, INDArray b, boolean transposeA, boolean transposeB) { long cRows = (transposeA ? a.columns() : a.rows()); long cCols = (transposeB ? b.rows() : b.columns()); INDArray c = Nd4j.createUninitialized(new long[] {cRows, cCols}, 'f'); return gemm(a, b, c, transposeA, transposeB, 1.0, 0.0); }
/** * Read line via input streams * * @param filePath the input stream ndarray * @param split the split separator * @return the read txt method */ @Deprecated public static void writeNumpy(INDArray write, String filePath, String split) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); for (int i = 0; i < write.rows(); i++) { StringBuilder sb = new StringBuilder(); INDArray row = write.getRow(i); for (int j = 0; j < row.columns(); j++) { sb.append(row.getDouble(j)); if (j < row.columns() - 1) // not the last element sb.append(split); } sb.append("\n"); writer.write(sb.toString()); } writer.flush(); writer.close(); }
/** * Computes the eigenvalues of a general matrix. */ public static IComplexNDArray eigenvalues(INDArray A) { assert A.rows() == A.columns(); INDArray WR = Nd4j.create(A.rows(), A.rows()); INDArray WI = WR.dup(); Nd4j.getBlasWrapper().geev('N', 'N', A.dup(), WR, WI, dummy, dummy); return Nd4j.createComplex(WR, WI); }
/** * Compute generalized eigenvalues of the problem A x = L B x. * The data will be unchanged, no eigenvectors returned. * * @param A symmetric Matrix A. * @param B symmetric Matrix B. * @return a vector of eigenvalues L. */ public static INDArray symmetricGeneralizedEigenvalues(INDArray A, INDArray B) { assert A.rows() == A.columns(); assert B.rows() == B.columns(); INDArray W = Nd4j.create(A.rows()); A = InvertMatrix.invert(B, false).mmuli(A); Nd4j.getBlasWrapper().syev('V', 'L', A, W); return W; }
@Override public int syev(char jobz, char uplo, INDArray A, INDArray V) { if (A.rows() != A.columns()) { throw new Error("syev: A must be square."); } if (A.rows() != V.length()) { throw new Error("syev: V must be the length of the matrix dimension."); } // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int status = -1; if (A.data().dataType() == DataBuffer.Type.DOUBLE) { status = dsyev(jobz, uplo, (int) A.rows(), A, V); } else if (A.data().dataType() == DataBuffer.Type.FLOAT) { status = ssyev(jobz, uplo, (int) A.rows(), A, V); } else { throw new UnsupportedOperationException(); } return status; }
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) { if (!viewArray.isRowVector() && !(viewArray.rank() == 2 && viewArray.columns() == 1 && viewArray.rows() == 1)) throw new IllegalArgumentException("Invalid input: expect row vector input"); if (initialize) viewArray.assign(epsilon); this.historicalGradient = viewArray; //Reshape to match the expected shape of the input gradient arrays this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f'); if (historicalGradient == null) throw new IllegalStateException("Could not correctly reshape gradient view array"); this.gradientReshapeOrder = gradientOrder; }
@Override public INDArray getUFactor(INDArray A) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray U = Nd4j.create(n, n); for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { if (r <= c && r < m && c < n) { U.putScalar(r, c, A.getFloat(r, c)); } else { U.putScalar(r, c, 0.f); } } } return U; }
/** * Merge the vectors and append a bias. * Each vector must be either row or column vectors. * An exception is thrown for inconsistency (mixed row and column vectors) * * @param vectors the vectors to merge * @return the merged ndarray appended with the bias */ @Override public INDArray appendBias(INDArray... vectors) { int size = 0; for (INDArray vector : vectors) { size += vector.rows(); } INDArray result = Nd4j.create(size + 1, vectors[0].columns()); int index = 0; for (INDArray vector : vectors) { INDArray put = toFlattened(vector, Nd4j.ones(1)); result.put(new INDArrayIndex[] {NDArrayIndex.interval(index, index + vector.rows() + 1), NDArrayIndex.interval(0, vectors[0].columns())}, put); index += vector.rows(); } return result; }
/** * Compute generalized eigenvalues of the problem A x = L B x. * The data will be unchanged, no eigenvectors returned unless calculateVectors is true. * If calculateVectors == true, A will contain a matrix with the eigenvectors as columns. * * @param A symmetric Matrix A. * @param B symmetric Matrix B. * @return a vector of eigenvalues L. */ public static INDArray symmetricGeneralizedEigenvalues(INDArray A, INDArray B, boolean calculateVectors) { assert A.rows() == A.columns(); assert B.rows() == B.columns(); INDArray W = Nd4j.create(A.rows()); if (calculateVectors) A.assign(InvertMatrix.invert(B, false).mmuli(A)); else A = InvertMatrix.invert(B, false).mmuli(A); Nd4j.getBlasWrapper().syev('V', 'L', A, W); return W; }
if (in.rank() != 2) throw new IllegalArgumentException("Cannot sort columns on non-2d matrix"); if (rowIdx < 0 || rowIdx >= in.rows()) throw new IllegalArgumentException("Cannot sort on values in row " + rowIdx + ", nRows=" + in.rows()); if (in.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int nCols = (int) in.columns(); ArrayList<Integer> list = new ArrayList<>(nCols); for (int i = 0; i < nCols; i++)
@Override public INDArray getLFactor(INDArray A) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray L = Nd4j.create(m, n); for (int r = 0; r < m; r++) { for (int c = 0; c < n; c++) { if (r > c && r < m && c < n) { L.putScalar(r, c, A.getFloat(r, c)); } else if (r < c) { L.putScalar(r, c, 0.f); } else { L.putScalar(r, c, 1.f); } } } return L; }
if (in.rank() != 2) throw new IllegalArgumentException("Cannot sort rows on non-2d matrix"); if (colIdx < 0 || colIdx >= in.columns()) throw new IllegalArgumentException("Cannot sort on values in column " + colIdx + ", nCols=" + in.columns()); if (in.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int nRows = (int) in.rows(); ArrayList<Integer> list = new ArrayList<Integer>(nRows); for (int i = 0; i < nRows; i++)
this.y = y; if (a.columns() > Integer.MAX_VALUE || a.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); if (x.columns() > Integer.MAX_VALUE || x.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.rows(); } else if (a.ordering() == 'c' && a.isMatrix()) { this.m = (int) a.columns(); this.n = (int) a.rows(); this.lda = (int) a.columns(); aOrdering = 'T'; this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.size(0);
/** * Asserts matrix multiply rules (columns of left == rows of right or rows of left == columns of right) * * @param nd1 the left ndarray * @param nd2 the right ndarray */ public static void assertMultiplies(INDArray nd1, INDArray nd2) { if (nd1.rank() == 2 && nd2.rank() == 2 && nd1.columns() == nd2.rows()) { return; } // 1D edge case if (nd1.rank() == 2 && nd2.rank() == 1 && nd1.columns() == nd2.length()) return; throw new ND4JIllegalStateException("Cannot execute matrix multiplication: " + Arrays.toString(nd1.shape()) + "x" + Arrays.toString(nd2.shape()) + (nd1.rank() != 2 || nd2.rank() != 2 ? ": inputs are not matrices" : ": Column of left array " + nd1.columns() + " != rows of right " + nd2.rows())); }
/** * Create from a matrix. The rows are the indices * The columns are the individual element in each ndarrayindex * * @param index the matrix to getFloat indices from * @return the indices to getFloat */ public static INDArrayIndex[] create(INDArray index) { if (index.isMatrix()) { if (index.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); NDArrayIndex[] ret = new NDArrayIndex[(int) index.rows()]; for (int i = 0; i < index.rows(); i++) { INDArray row = index.getRow(i); val nums = new long[(int) index.getRow(i).columns()]; for (int j = 0; j < row.columns(); j++) { nums[j] = (int) row.getFloat(j); } NDArrayIndex idx = new NDArrayIndex(nums); ret[i] = idx; } return ret; } else if (index.isVector()) { long[] indices = NDArrayUtil.toLongs(index); return new NDArrayIndex[] {new NDArrayIndex(indices)}; } throw new IllegalArgumentException("Passed in ndarray must be a matrix or a vector"); }
@Override public void geqrf(INDArray A, INDArray R) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray INFO = Nd4j.createArrayFromShapeBuffer(Nd4j.getDataBufferFactory().createInt(1), Nd4j.getShapeInfoProvider().createShapeInformation(new int[] {1, 1}).getFirst()); if (R.rows() != A.columns() || R.columns() != A.columns()) { throw new Error("geqrf: R must be N x N (n = columns in A)"); } if (A.data().dataType() == DataBuffer.Type.DOUBLE) { dgeqrf(m, n, A, R, INFO); } else if (A.data().dataType() == DataBuffer.Type.FLOAT) { sgeqrf(m, n, A, R, INFO); } else { throw new UnsupportedOperationException(); } if (INFO.getInt(0) < 0) { throw new Error("Parameter #" + INFO.getInt(0) + " to getrf() was not valid"); } }
assert A.columns() == A.rows(); INDArray WR = Nd4j.create(A.rows()); INDArray WI = WR.dup(); INDArray VR = Nd4j.create(A.rows(), A.rows()); INDArray VL = Nd4j.create(A.rows(), A.rows()); IComplexNDArray V = Nd4j.createComplex((int) A.rows(), (int) A.rows()); for (int i = 0; i < A.rows(); i++) { if (E.getComplex(i).isReal()) { IComplexNDArray column = Nd4j.createComplex(VR.getColumn(i));
public static RealMatrix convertToApacheMatrix(INDArray matrix) { if (matrix.rank() != 2) throw new IllegalArgumentException("Input rank is not 2 (not matrix)"); long[] shape = matrix.shape(); if (matrix.columns() > Integer.MAX_VALUE || matrix.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); BlockRealMatrix out = new BlockRealMatrix((int) shape[0], (int) shape[1]); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { double value = matrix.getDouble(i, j); out.setEntry(i, j, value); } } return out; }