/** * <p> * Adds to A ∈ ℜ <sup>m × n</sup> the results of an outer product multiplication * of the two vectors. This is also known as a rank 1 update.<br> * <br> * A = A + γ x * y<sup>T</sup> * where x ∈ ℜ <sup>m</sup> and y ∈ ℜ <sup>n</sup> are vectors. * </p> * <p> * Which is equivalent to: A<sub>ij</sub> = A<sub>ij</sub> + γ x<sub>i</sub>*y<sub>j</sub> * </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 gamma A multiplication factor for the outer product. * @param x A vector with m elements. Not modified. * @param y A vector with n elements. Not modified. * @param A A Matrix with m by n elements. Modified. */ public static void addOuterProd(float gamma, FMatrixD1 x, FMatrixD1 y, FMatrix1Row A) { int m = A.numRows; int n = A.numCols; int index = 0; if (gamma == 1.0f) { for (int i = 0; i < m; i++) { float xdat = x.get(i); for (int j = 0; j < n; j++) { A.plus(index++, xdat * y.get(j)); } } } else { for (int i = 0; i < m; i++) { float xdat = x.get(i); for (int j = 0; j < n; j++) { A.plus(index++, gamma * xdat * y.get(j)); } } } }
/** * An alternative implementation of {@link #multTransA_small} that performs well on large * matrices. There is a relative performance hit when used on small matrices. * * @param A A matrix that is m by n. Not modified. * @param B A Vector that has length m. Not modified. * @param C A column vector that has length n. Modified. */ public static void multTransA_reorder(FMatrix1Row A, FMatrixD1 B, FMatrixD1 C) { if (C.numCols != 1) { throw new MatrixDimensionException("C is not a column vector"); } else if (C.numRows != A.numCols) { throw new MatrixDimensionException("C is not the expected length"); } if (B.numRows == 1) { if (A.numRows != B.numCols) { throw new MatrixDimensionException("A and B are not compatible"); } } else if (B.numCols == 1) { if (A.numRows != B.numRows) { throw new MatrixDimensionException("A and B are not compatible"); } } else { throw new MatrixDimensionException("B is not a vector"); } if (A.numRows == 0) { CommonOps_FDRM.fill(C, 0); return; } float B_val = B.get(0); for (int i = 0; i < A.numCols; i++) { C.set(i, A.get(i) * B_val); } int indexA = A.numCols; for (int i = 1; i < A.numRows; i++) { B_val = B.get(i); for (int j = 0; j < A.numCols; j++) { C.plus(j, A.get(indexA++) * B_val); } } }
/** * <p> * Performs a matrix vector multiply.<br> * <br> * c = A * b <br> * and<br> * c = A * b<sup>T</sup> <br> * <br> * c<sub>i</sub> = Sum{ j=1:n, a<sub>ij</sub> * b<sub>j</sub>}<br> * <br> * where A is a matrix, b is a column or transposed row vector, and c is a column vector. * </p> * * @param A A matrix that is m by n. Not modified. * @param B A vector that has length n. Not modified. * @param C A column vector that has length m. Modified. */ public static void mult(FMatrix1Row A, FMatrixD1 B, FMatrixD1 C) { if (C.numCols != 1) { throw new MatrixDimensionException("C is not a column vector"); } else if (C.numRows != A.numRows) { throw new MatrixDimensionException("C is not the expected length"); } if (B.numRows == 1) { if (A.numCols != B.numCols) { throw new MatrixDimensionException("A and B are not compatible"); } } else if (B.numCols == 1) { if (A.numCols != B.numRows) { throw new MatrixDimensionException("A and B are not compatible"); } } else { throw new MatrixDimensionException("B is not a vector"); } if (A.numCols == 0) { CommonOps_FDRM.fill(C, 0); return; } int indexA = 0; int cIndex = 0; float b0 = B.get(0); for (int i = 0; i < A.numRows; i++) { float total = A.get(indexA++) * b0; for (int j = 1; j < A.numCols; j++) { total += A.get(indexA++) * B.get(j); } C.set(cIndex++, total); } }
/** * <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 float innerProdTranA(FMatrixD1 x, FMatrixD1 A, FMatrixD1 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"); } float result = 0; for (int i = 0; i < n; i++) { float 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> * 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(float gamma, FMatrixD1 u, FMatrixD1 x, FMatrixD1 y) { int n = u.getNumElements(); float 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); } }
/** * 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(FMatrixD1 m, float tol) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { if (Math.Abs(m.get(i)) > tol) { return(false); } } return(true); }
/** * 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(FMatrixD1 m) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { if (float.IsNaN(m.get(i))) { return(true); } } return(false); }
/** * <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 float innerProd(FMatrixD1 x, FMatrixD1 y) { int m = x.getNumElements(); float total = 0; for (int i = 0; i < m; i++) { total += x.get(i) * y.get(i); } return(total); }
/** * <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 float fastNormF(FMatrixD1 a) { float total = 0; int size = a.getNumElements(); for (int i = 0; i < size; i++) { float val = a.get(i); total += val * val; } return((float)Math.Sqrt(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(FMatrixD1 m) { int length = m.getNumElements(); for (int i = 0; i < length; i++) { float a = m.get(i); if (float.IsNaN(a) || float.IsInfinity(a)) { return(true); } } return(false); }
/** * <p> * Sets A ∈ ℜ <sup>m × n</sup> equal to an outer product multiplication of the two * vectors. This is also known as a rank-1 operation.<br> * <br> * A = x * y' * where x ∈ ℜ <sup>m</sup> and y ∈ ℜ <sup>n</sup> are vectors. * </p> * <p> * Which is equivalent to: A<sub>ij</sub> = x<sub>i</sub>*y<sub>j</sub> * </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 m elements. Not modified. * @param y A vector with n elements. Not modified. * @param A A Matrix with m by n elements. Modified. */ public static void outerProd(FMatrixD1 x, FMatrixD1 y, FMatrix1Row A) { int m = A.numRows; int n = A.numCols; int index = 0; for (int i = 0; i < m; i++) { float xdat = x.get(i); for (int j = 0; j < n; j++) { A.set(index++, xdat * y.get(j)); } } }
/** * <p> * Returns the absolute value of the digonal element in the matrix that has the largest absolute value.<br> * <br> * Max{ |a<sub>ij</sub>| } for all i and j<br> * </p> * * @param a A matrix. Not modified. * @return The max abs element value of the matrix. */ public static float elementDiagonalMaxAbs(FMatrixD1 a) { int size = Math.Min(a.numRows, a.numCols); float max = 0; for (int i = 0; i < size; i++) { float val = Math.Abs(a.get(i, i)); if (val > max) { max = val; } } return(max); }
/** * <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 float diffNormP1(FMatrixD1 a, FMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Both matrices must have the same shape."); } int size = a.getNumElements(); float 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_FDRM#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 float diffNormF(FMatrixD1 a, FMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Both matrices must have the same shape."); } int size = a.getNumElements(); FMatrixRMaj diff = new FMatrixRMaj(size, 1); for (int i = 0; i < size; i++) { diff.set(i, b.get(i) - a.get(i)); } return(NormOps_FDRM.normF(diff)); }
/** * <p> * Performs a matrix vector multiply.<br> * <br> * C = C + A<sup>T</sup> * B <br> * or<br> * C = C<sup>T</sup> + A<sup>T</sup> * B<sup>T</sup> <br> * <br> * c<sub>i</sub> = Sum{ j=1:n, c<sub>i</sub> + a<sub>ji</sub> * b<sub>j</sub>}<br> * <br> * where A is a matrix, B is a column or transposed row vector, and C is a column vector. * </p> * <p> * This implementation is optimal for small matrices. There is a huge performance hit when * used on large matrices due to CPU cache issues. * </p> * * @param A A matrix that is m by n. Not modified. * @param B A vector that has length m. Not modified. * @param C A column vector that has length n. Modified. */ public static void multAddTransA_small(FMatrix1Row A, FMatrixD1 B, FMatrixD1 C) { if (C.numCols != 1) { throw new MatrixDimensionException("C is not a column vector"); } else if (C.numRows != A.numCols) { throw new MatrixDimensionException("C is not the expected length"); } if (B.numRows == 1) { if (A.numRows != B.numCols) { throw new MatrixDimensionException("A and B are not compatible"); } } else if (B.numCols == 1) { if (A.numRows != B.numRows) { throw new MatrixDimensionException("A and B are not compatible"); } } else { throw new MatrixDimensionException("B is not a vector"); } int cIndex = 0; for (int i = 0; i < A.numCols; i++) { float total = 0.0f; int indexA = i; for (int j = 0; j < A.numRows; j++) { total += A.get(indexA) * B.get(j); indexA += A.numCols; } C.plus(cIndex++, total); } }
public static float diffNormF_fast(FMatrixD1 a, FMatrixD1 b) { if (a.numRows != b.numRows || a.numCols != b.numCols) { throw new ArgumentException("Both matrices must have the same shape."); } int size = a.getNumElements(); float total = 0; for (int i = 0; i < size; i++) { float diff = b.get(i) - a.get(i); total += diff * diff; } return((float)Math.Sqrt(total)); }
/** * <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(FMatrixD1 a, FMatrixD1 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(FMatrixD1 a, FMatrixD1 b, float 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 float normF(FMatrixD1 a) { float total = 0; float scale = CommonOps_FDRM.elementMaxAbs(a); if (scale == 0.0f) { return(0.0f); } int size = a.getNumElements(); for (int i = 0; i < size; i++) { float val = a.get(i) / scale; total += val * val; } return(scale * (float)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 float fastElementP(FMatrixD1 A, float p) { if (p == 2) { return(fastNormF(A)); } else { float total = 0; int size = A.getNumElements(); for (int i = 0; i < size; i++) { float a = A.get(i); total += (float)Math.Pow(Math.Abs(a), p); } return((float)Math.Pow(total, 1.0f / 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 Float.NaN, Float.POSITIVE_INFINITY, or * Float.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(FMatrixD1 a, FMatrixD1 b, float 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(FMatrixD1, FMatrixD1)} * </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(FMatrixD1 a, FMatrixD1 b, float tol) { if (a.numRows != b.numRows || a.numCols != b.numCols) { return(false); } if (tol == 0.0f) { 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); }