/** * <p> * Checks to see if a matrix is orthogonal or isometric. * </p> * * @param Q The matrix being tested. Not modified. * @param tol Tolerance. * @return True if it passes the test. */ public static bool isOrthogonal(DMatrixRMaj Q, double tol) { if (Q.numRows < Q.numCols) { throw new ArgumentException("The number of rows must be more than or equal to the number of columns"); } DMatrixRMaj[] u = CommonOps_DDRM.columnsToVector(Q, null); for (int i = 0; i < u.Count(); i++) { DMatrixRMaj a = u[i]; for (int j = i + 1; j < u.Count(); j++) { double val = VectorVectorMult_DDRM.innerProd(a, u[j]); if (!(Math.Abs(val) <= tol)) { return(false); } } } return(true); }
/** * <p> * Creates a randomly generated set of orthonormal vectors. At most it can generate the same * number of vectors as the dimension of the vectors. * </p> * * <p> * This is done by creatingJava.Util.Random vectors then ensuring that they are orthogonal * to all the ones previously created with reflectors. * </p> * * <p> * NOTE: This employs a brute force O(N<sup>3</sup>) algorithm. * </p> * * @param dimen dimension of the space which the vectors will span. * @param numVectors How many vectors it should generate. * @param rand Used to createJava.Util.Random vectors. * @return Array of NJava.Util.Random orthogonal vectors of unit Count(). */ // is there a faster algorithm out there? This one is a bit sluggish public static DMatrixRMaj[] span(int dimen, int numVectors, Java.Util.Random rand) { if (dimen < numVectors) { throw new ArgumentException("The number of vectors must be less than or equal to the dimension"); } DMatrixRMaj[] u = new DMatrixRMaj[numVectors]; u[0] = RandomMatrices_DDRM.rectangle(dimen, 1, -1, 1, rand); NormOps_DDRM.normalizeF(u[0]); for (int i = 1; i < numVectors; i++) { // System.out.println(" i = "+i); DMatrixRMaj a = new DMatrixRMaj(dimen, 1); DMatrixRMaj r = RandomMatrices_DDRM.rectangle(dimen, 1, -1, 1, rand); for (int j = 0; j < i; j++) { // find a vector that is normal to vector j // u[i] = (1/2)*(r + Q[j]*r) a.setTo(r); VectorVectorMult_DDRM.householder(-2.0, u[j], r, a); CommonOps_DDRM.add(r, a, a); CommonOps_DDRM.scale(0.5, a); // UtilEjml.print(a); DMatrixRMaj t = a; a = r; r = t; // normalize it so it doesn't get too small double val = NormOps_DDRM.normF(r); if (val == 0 || Double.IsNaN(val) || Double.IsInfinity(val)) { throw new SystemException("Failed sanity check"); } CommonOps_DDRM.divide(r, val); } u[i] = r; } return(u); }
public double dot(Matrix A, Matrix v) { return(VectorVectorMult_DDRM.innerProd((DMatrixRMaj)A, (DMatrixRMaj)v)); }