/** * <p> * Sets each element in the matrix to a value drawn from an Gaussian distribution with the specified mean and * standard deviation * </p> * * @param mat The matrix who is to be randomized. Modified. * @param mean Mean value in the distribution * @param stdev Standard deviation in the distribution * @param rand Random number generator used to fill the matrix. */ public static void setGaussian( D1Matrix64F mat , double mean , double stdev , Random rand ) { double d[] = mat.getData(); int size = mat.getNumElements(); for( int i = 0; i < size; i++ ) { d[i] = mean + stdev*rand.nextGaussian(); } }
/** * <p>Performs the an element by element division operation:<br> * <br> * a<sub>ij</sub> = a<sub>ij</sub> / b<sub>ij</sub> <br> * </p> * @param a The left matrix in the division operation. Modified. * @param b The right matrix in the division operation. Not modified. */ public static void elementDiv( D1Matrix64F a , D1Matrix64F b ) { if( a.numCols != b.numCols || a.numRows != b.numRows ) { throw new IllegalArgumentException("The 'a' and 'b' matrices do not have compatible dimensions"); } int length = a.getNumElements(); for( int i = 0; i < length; i++ ) { a.div(i , b.get(i)); } }
/** * <p>Performs the following operation:<br> * <br> * a = a + b <br> * a<sub>ij</sub> = a<sub>ij</sub> + b<sub>ij</sub> <br> * </p> * * @param a A Matrix. Modified. * @param b A Matrix. Not modified. */ public static void addEquals( D1Matrix64F a , D1Matrix64F b ) { if( a.numCols != b.numCols || a.numRows != b.numRows ) { throw new IllegalArgumentException("The 'a' and 'b' matrices do not have compatible dimensions"); } final int length = a.getNumElements(); for( int i = 0; i < length; i++ ) { a.plus(i, b.get(i)); } }
/** * <p>Performs an in-place scalar addition:<br> * <br> * a = a + val<br> * a<sub>ij</sub> = a<sub>ij</sub> + val<br> * </p> * * @param a A matrix. Modified. * @param val The value that's added to each element. */ public static void add( D1Matrix64F a , double val ) { final int length = a.getNumElements(); for( int i = 0; i < length; i++ ) { a.plus(i, val); } }
/** * <p> * Computes the sum of all the elements in the matrix:<br> * <br> * sum(i=1:m , j=1:n ; a<sub>ij</sub>) * <p> * * @param mat An m by n matrix. Not modified. * @return The sum of the elements. */ public static double elementSum( D1Matrix64F mat ) { double total = 0; int size = mat.getNumElements(); for( int i = 0; i < size; i++ ) { total += mat.get(i); } return total; }
/** * <p> * Multiplies a householder reflection against a vector:<br> * <br> * y = (I + γ u u<sup>T</sup>)x<br> * </p> * <p> * The Householder reflection is used in some implementations of QR decomposition. * </p> * @param u A vector. Not modified. * @param x a vector. Not modified. * @param y Vector where the result are written to. */ public static void householder( double gamma, D1Matrix64F u , D1Matrix64F x , D1Matrix64F y ) { int n = u.getNumElements(); double sum = 0; for( int i = 0; i < n; i++ ) { sum += u.get(i)*x.get(i); } for( int i = 0; i < n; i++ ) { y.set( i , x.get(i) + gamma*u.get(i)*sum); } }
/** * <p> * return = x<sup>T</sup>*A*y * </p> * * @param x A vector with n elements. Not modified. * @param A A matrix with n by m elements. Not modified. * @param y A vector with m elements. Not modified. * @return The results. */ public static double innerProdA( D1Matrix64F x, D1Matrix64F A , D1Matrix64F y ) { int n = A.numRows; int m = A.numCols; if( x.getNumElements() != n ) throw new IllegalArgumentException("Unexpected number of elements in x"); if( y.getNumElements() != m ) throw new IllegalArgumentException("Unexpected number of elements in y"); double result = 0; for( int i = 0; i < m; i++ ) { double total = 0; for( int j = 0; j < n; j++ ) { total += x.get(j)*A.unsafe_get(j,i); } result += total*y.get(i); } return result; }
/** * <p>Performs the an element by element multiplication operation:<br> * <br> * a<sub>ij</sub> = a<sub>ij</sub> * b<sub>ij</sub> <br> * </p> * @param a The left matrix in the multiplication operation. Modified. * @param b The right matrix in the multiplication operation. Not modified. */ public static void elementMult( D1Matrix64F a , D1Matrix64F b ) { if( a.numCols != b.numCols || a.numRows != b.numRows ) { throw new IllegalArgumentException("The 'a' and 'b' matrices do not have compatible dimensions"); } int length = a.getNumElements(); for( int i = 0; i < length; i++ ) { a.times(i , b.get(i)); } }
WTA.row1 = Y.col1-Y.col0; WTA.col1 = subB.col1-subB.col0; WTA.original.reshape(WTA.row1,WTA.col1,false);
/** * <p> * Changes the sign of every element in the matrix.<br> * <br> * a<sub>ij</sub> = -a<sub>ij</sub> * </p> * * @param a A matrix. Modified. */ public static void changeSign( D1Matrix64F a ) { final int size = a.getNumElements(); for( int i = 0; i < size; i++ ) { a.data[i] = -a.data[i]; } }
private double[] computeFittedValues() { D1Matrix64F fitted = new DenseMatrix64F(response.length, 1); MatrixVectorMult.mult(X, b, fitted); return fitted.getData(); }
/** * Computes the quality of a triangular matrix, where the quality of a matrix * is defined in {@link org.ejml.interfaces.linsol.LinearSolver#quality()}. In * this situation the quality os the absolute value of the product of * each diagonal element divided by the magnitude of the largest diagonal element. * If all diagonal elements are zero then zero is returned. * * @param T A matrix. @return product of the diagonal elements. * @return the quality of the system. */ public static double qualityTriangular(D1Matrix64F T) { int N = Math.min(T.numRows,T.numCols); // TODO make faster by just checking the upper triangular portion double max = elementDiagonalMaxAbs(T); if( max == 0.0d ) return 0.0d; double quality = 1.0; for( int i = 0; i < N; i++ ) { quality *= T.unsafe_get(i,i)/max; } return Math.abs(quality); }
public double get(int row, int col ) { return original.get(row+row0,col+col0); }
public void set(int row, int col, double value) { original.set(row+row0,col+col0,value); }
/** * <p> * Multiplies a householder reflection against a vector:<br> * <br> * y = (I + γ u u<sup>T</sup>)x<br> * </p> * <p> * The Householder reflection is used in some implementations of QR decomposition. * </p> * @param u A vector. Not modified. * @param x a vector. Not modified. * @param y Vector where the result are written to. */ public static void householder( double gamma, D1Matrix64F u , D1Matrix64F x , D1Matrix64F y ) { int n = u.getNumElements(); double sum = 0; for( int i = 0; i < n; i++ ) { sum += u.get(i)*x.get(i); } for( int i = 0; i < n; i++ ) { y.set( i , x.get(i) + gamma*u.get(i)*sum); } }
/** * <p> * Computes the sum of all the elements in the matrix:<br> * <br> * sum(i=1:m , j=1:n ; a<sub>ij</sub>) * <p> * * @param mat An m by n matrix. Not modified. * @return The sum of the elements. */ public static double elementSum( D1Matrix64F mat ) { double total = 0; int size = mat.getNumElements(); for( int i = 0; i < size; i++ ) { total += mat.get(i); } return total; }
/** * <p> * x<sup>T</sup>Ay * </p> * * @param x A vector with n elements. Not modified. * @param A A matrix with n by m elements. Not modified. * @param y A vector with m elements. Not modified. * @return The results. */ public static double innerProdA( D1Matrix64F x, D1Matrix64F A , D1Matrix64F y ) { int n = A.numRows; int m = A.numCols; if( x.getNumElements() != n ) throw new IllegalArgumentException("Unexpected number of elements in x"); if( y.getNumElements() != m ) throw new IllegalArgumentException("Unexpected number of elements in y"); double result = 0; for( int i = 0; i < m; i++ ) { double total = 0; for( int j = 0; j < n; j++ ) { total += x.get(j)*A.unsafe_get(j,i); } result += total*y.get(i); } return result; }