public static void inv(FMatrixRMaj mat, FMatrixRMaj inv)
        {
            float max = Math.Abs(mat.data[0]);
            int   N   = mat.getNumElements();

            for (int i = 1; i < N; i++)
            {
                float a = Math.Abs(mat.data[i]);
                if (a > max)
                {
                    max = a;
                }
            }

            switch (mat.numRows)
            {
            case 2:
                inv2(mat, inv, 1.0f / max);
                break;

            case 3:
                inv3(mat, inv, 1.0f / max);
                break;

            case 4:
                inv4(mat, inv, 1.0f / max);
                break;

            case 5:
                inv5(mat, inv, 1.0f / max);
                break;

            default: throw new ArgumentException("Not supported");
            }
        }
示例#2
0
        public static void convert(FMatrixRMaj src, DMatrixRMaj dst)
        {
            int N = src.getNumElements();

            for (int i = 0; i < N; i++)
            {
                dst.data[i] = src.data[i];
            }
        }
示例#3
0
        /**
         * <p>
         * Adds random values to each element in the matrix from an uniform distribution.<br>
         * <br>
         * a<sub>ij</sub> = a<sub>ij</sub> + U(min,max)<br>
         * </p>
         *
         * @param A The matrix who is to be randomized. Modified
         * @param min The minimum value each element can be.
         * @param max The maximum value each element can be..
         * @param rand Random number generator used to fill the matrix.
         */
        public static void addUniform(FMatrixRMaj A, float min, float max, IMersenneTwister rand)
        {
            float[] d    = A.getData();
            int     size = A.getNumElements();

            float r = max - min;

            for (int i = 0; i < size; i++)
            {
                d[i] += r * rand.NextFloat() + min;
            }
        }
示例#4
0
        /**
         * <p>
         * Creates a reflector from the provided vector and gamma.<br>
         * <br>
         * Q = I - &gamma; u u<sup>T</sup><br>
         * </p>
         *
         * <p>
         * In practice {@link VectorVectorMult_FDRM#householder(float, FMatrixD1, FMatrixD1, FMatrixD1)}  multHouseholder}
         * should be used for performance reasons since there is no need to calculate Q explicitly.
         * </p>
         *
         * @param u A vector.  Not modified.
         * @param gamma To produce a reflector gamma needs to be equal to 2/||u||.
         * @return An orthogonal reflector.
         */
        public static FMatrixRMaj createReflector(FMatrixRMaj u, float gamma)
        {
            if (!MatrixFeatures_FDRM.isVector(u))
            {
                throw new ArgumentException("u must be a vector");
            }

            FMatrixRMaj Q = CommonOps_FDRM.identity(u.getNumElements());

            CommonOps_FDRM.multAddTransB(-gamma, u, u, Q);

            return(Q);
        }
示例#5
0
        /**
         * Normalizes the matrix such that the Frobenius norm is equal to one.
         *
         * @param A The matrix that is to be normalized.
         */
        public static void normalizeF(FMatrixRMaj A)
        {
            float val = normF(A);

            if (val == 0)
            {
                return;
            }

            int size = A.getNumElements();

            for (int i = 0; i < size; i++)
            {
                A.div(i, val);
            }
        }
        /**
         * <p>
         * Performs a rank one update on matrix A using vectors u and w.  The results are stored in A.<br>
         * <br>
         * A = A + &gamma; u w<sup>T</sup><br>
         * </p>
         * <p>
         * This is called a rank1 update because the matrix u w<sup>T</sup> has a rank of 1.
         * </p>
         *
         * @param gamma A scalar.
         * @param A A m by m matrix. Modified.
         * @param u A vector with m elements.  Not modified.
         */
        public static void rank1Update(float gamma,
                                       FMatrixRMaj A,
                                       FMatrixRMaj u,
                                       FMatrixRMaj w)
        {
            int n = u.getNumElements();

            int matrixIndex = 0;

            for (int i = 0; i < n; i++)
            {
                float elementU = u.data[i];

                for (int j = 0; j < n; j++)
                {
                    A.data[matrixIndex++] += gamma * elementU * w.data[j];
                }
            }
        }