/** * <p> * x<sup>T</sup>A<sup>T</sup>y * </p> * * @param x A vector with n elements. Not modified. * @param A A matrix with n by n elements. Not modified. * @param y A vector with n elements. Not modified. * @return The results. */ // TODO better name for this public static double innerProdTranA(DMatrixD1 x, DMatrixD1 A, DMatrixD1 y) { int n = A.numRows; if (n != A.numCols) { throw new ArgumentException("A must be square"); } if (x.getNumElements() != n) { throw new ArgumentException("Unexpected number of elements in x"); } if (y.getNumElements() != n) { throw new ArgumentException("Unexpected number of elements in y"); } double result = 0; for (int i = 0; i < n; i++) { double total = 0; for (int j = 0; j < n; j++) { total += x.get(j) * A.unsafe_get(i, j); } result += total * y.get(i); } return(result); }
/** * <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 fillGaussian(DMatrixD1 mat, double mean, double stdev, IMersenneTwister rand) { double[] d = mat.getData(); int size = mat.getNumElements(); for (int i = 0; i < size; i++) { d[i] = mean + stdev * (double)rand.NextGaussian(); } }
/** * <p> * Sets each element in the matrix to a value drawn from an uniform distribution from 'min' to 'max' inclusive. * </p> * * @param min The minimum value each element can be. * @param max The maximum value each element can be. * @param mat The matrix who is to be randomized. Modified. * @param rand Random number generator used to fill the matrix. */ public static void fillUniform(DMatrixD1 mat, double min, double max, IMersenneTwister rand) { double[] d = mat.getData(); int size = mat.getNumElements(); double r = max - min; for (int i = 0; i < size; i++) { d[i] = r * rand.NextDouble() + min; } }
/** * Checks to see all the elements in the matrix are zeros * * @param m A matrix. Not modified. * @return True if all elements are zeros or false if not */ public static bool isZeros(DMatrixD1 m, double tol) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { if (Math.Abs(m.get(i)) > tol) { return(false); } } return(true); }
/** * <p> * Computes the inner product of the two vectors. In geometry this is known as the dot product.<br> * <br> * ∑<sub>k=1:n</sub> x<sub>k</sub> * y<sub>k</sub><br> * where x and y are vectors with n elements. * </p> * * <p> * These functions are often used inside of highly optimized code and therefor sanity checks are * kept to a minimum. It is not recommended that any of these functions be used directly. * </p> * * @param x A vector with n elements. Not modified. * @param y A vector with n elements. Not modified. * @return The inner product of the two vectors. */ public static double innerProd(DMatrixD1 x, DMatrixD1 y) { int m = x.getNumElements(); double total = 0; for (int i = 0; i < m; i++) { total += x.get(i) * y.get(i); } return(total); }
/** * Checks to see if any element in the matrix is NaN. * * @param m A matrix. Not modified. * @return True if any element in the matrix is NaN. */ public static bool hasNaN(DMatrixD1 m) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { if (double.IsNaN(m.get(i))) { return(true); } } return(false); }
/** * Sums up the square of each element in the matrix. This is equivalent to the * Frobenius norm squared. * * @param m Matrix. * @return Sum of elements squared. */ public static double elementSumSq(DMatrixD1 m) { double total = 0; int N = m.getNumElements(); for (int i = 0; i < N; i++) { double d = m.data[i]; total += d * d; } return(total); }
/** * Checks to see if any element in the matrix is NaN of Infinite. * * @param m A matrix. Not modified. * @return True if any element in the matrix is NaN of Infinite. */ public static bool hasUncountable(DMatrixD1 m) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { double a = m.get(i); if (double.IsNaN(a) || double.IsInfinity(a)) { return(true); } } return(false); }
/** * <p> * This implementation of the Frobenius norm is a straight forward implementation and can * be susceptible for overflow/underflow issues. A more resilient implementation is * {@link #normF}. * </p> * * @param a The matrix whose norm is computed. Not modified. */ public static double fastNormF(DMatrixD1 a) { double total = 0; int size = a.getNumElements(); for (int i = 0; i < size; i++) { double val = a.get(i); total += val * val; } return(Math.Sqrt(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, DMatrixD1 u, DMatrixD1 x, DMatrixD1 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 p=1 p-norm of the difference between the two Matrices:<br> * <br> * ∑<sub>i=1:m</sub> ∑<sub>j=1:n</sub> | a<sub>ij</sub> - b<sub>ij</sub>| <br> * <br> * where |x| is the absolute value of x. * </p> * <p> * This is often used as a cost function. * </p> * * @param a m by n matrix. Not modified. * @param b m by n matrix. Not modified. * * @return The p=1 p-norm of the difference matrix. */ public static double diffNormP1(DMatrixD1 a, DMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Both matrices must have the same shape."); } int size = a.getNumElements(); double total = 0; for (int i = 0; i < size; i++) { total += Math.Abs(b.get(i) - a.get(i)); } return(total); }
/** * <p> * Computes the F norm of the difference between the two Matrices:<br> * <br> * Sqrt{∑<sub>i=1:m</sub> ∑<sub>j=1:n</sub> ( a<sub>ij</sub> - b<sub>ij</sub>)<sup>2</sup>} * </p> * <p> * This is often used as a cost function. * </p> * * @see NormOps_DDRM#fastNormF * * @param a m by n matrix. Not modified. * @param b m by n matrix. Not modified. * * @return The F normal of the difference matrix. */ public static double diffNormF(DMatrixD1 a, DMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Both matrices must have the same shape."); } int size = a.getNumElements(); DMatrixRMaj diff = new DMatrixRMaj(size, 1); for (int i = 0; i < size; i++) { diff.set(i, b.get(i) - a.get(i)); } return(NormOps_DDRM.normF(diff)); }
/** * <p> * Checks to see if each element in the two matrices are equal: * a<sub>ij</sub> == b<sub>ij</sub> * <p> * * <p> * NOTE: If any of the elements are NaN then false is returned. If two corresponding * elements are both positive or negative infinity then they are equal. * </p> * * @param a A matrix. Not modified. * @param b A matrix. Not modified. * @return true if identical and false otherwise. */ public static bool isEquals(DMatrixD1 a, DMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { return(false); } int length = a.getNumElements(); for (int i = 0; i < length; i++) { if (!(a.get(i) == b.get(i))) { return(false); } } return(true); }
/** * <p> * Checks to see if the two matrices are the negative of each other:<br> * <br> * a<sub>ij</sub> = -b<sub>ij</sub> * </p> * * @param a First matrix. Not modified. * @param b Second matrix. Not modified. * @param tol Numerical tolerance. * @return True if they are the negative of each other within tolerance. */ public static bool isNegative(DMatrixD1 a, DMatrixD1 b, double tol) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Matrix dimensions must match"); } int length = a.getNumElements(); for (int i = 0; i < length; i++) { if (!(Math.Abs(a.get(i) + b.get(i)) <= tol)) { return(false); } } return(true); }
/** * <p> * Computes the Frobenius matrix norm:<br> * <br> * normF = Sqrt{ ∑<sub>i=1:m</sub> ∑<sub>j=1:n</sub> { a<sub>ij</sub><sup>2</sup>} } * </p> * <p> * This is equivalent to the element wise p=2 norm. See {@link #fastNormF} for another implementation * that is faster, but more prone to underflow/overflow errors. * </p> * * @param a The matrix whose norm is computed. Not modified. * @return The norm's value. */ public static double normF(DMatrixD1 a) { double total = 0; double scale = CommonOps_DDRM.elementMaxAbs(a); if (scale == 0.0) { return(0.0); } int size = a.getNumElements(); for (int i = 0; i < size; i++) { double val = a.get(i) / scale; total += val * val; } return(scale * Math.Sqrt(total)); }
/** * Same as {@link #elementP} but runs faster by not mitigating overflow/underflow related problems. * * @param A Matrix. Not modified. * @param p p value. * @return The norm's value. */ public static double fastElementP(DMatrixD1 A, double p) { if (p == 2) { return(fastNormF(A)); } else { double total = 0; int size = A.getNumElements(); for (int i = 0; i < size; i++) { double a = A.get(i); total += Math.Pow(Math.Abs(a), p); } return(Math.Pow(total, 1.0 / p)); } }
/** * <p> * Checks to see if each corresponding element in the two matrices are * within tolerance of each other or have the some symbolic meaning. This * can handle NaN and Infinite numbers. * <p> * * <p> * If both elements are countable then the following equality test is used:<br> * |a<sub>ij</sub> - b<sub>ij</sub>| ≤ tol.<br> * Otherwise both numbers must both be Double.NaN, Double.POSITIVE_INFINITY, or * Double.NEGATIVE_INFINITY to be identical. * </p> * * @param a A matrix. Not modified. * @param b A matrix. Not modified. * @param tol Tolerance for equality. * @return true if identical and false otherwise. */ public static bool isIdentical(DMatrixD1 a, DMatrixD1 b, double tol) { if (a.numRows != b.numRows || a.numCols != b.numCols) { return(false); } if (tol < 0) { throw new ArgumentException("Tolerance must be greater than or equal to zero."); } int length = a.getNumElements(); for (int i = 0; i < length; i++) { if (!isIdentical(a.get(i), b.get(i), tol)) { return(false); } } return(true); }
/** * <p> * Checks to see if each element in the two matrices are within tolerance of * each other: tol ≥ |a<sub>ij</sub> - b<sub>ij</sub>|. * <p> * * <p> * NOTE: If any of the elements are not countable then false is returned.<br> * NOTE: If a tolerance of zero is passed in this is equivalent to calling * {@link #isEquals(DMatrixD1, DMatrixD1)} * </p> * * @param a A matrix. Not modified. * @param b A matrix. Not modified. * @param tol How close to being identical each element needs to be. * @return true if equals and false otherwise. */ public static bool isEquals(DMatrixD1 a, DMatrixD1 b, double tol) { if (a.numRows != b.numRows || a.numCols != b.numCols) { return(false); } if (tol == 0.0) { return(isEquals(a, b)); } int length = a.getNumElements(); for (int i = 0; i < length; i++) { if (!(tol >= Math.Abs(a.get(i) - b.get(i)))) { return(false); } } return(true); }