Ejemplo n.º 1
0
        /**
         * scalar = A<sup>T</sup>*B*C
         *
         * @param a (Input) vector
         * @param offsetA Input) first index in vector a
         * @param B (Input) Matrix
         * @param c (Output) vector
         * @param offsetC (Output) first index in vector c
         */
        public static double innerProduct(double[] a, int offsetA,
                                          DMatrix1Row B,
                                          double[] c, int offsetC)
        {
            if (a.Count() - offsetA < B.numRows)
            {
                throw new ArgumentException("Length of 'a' isn't long enough");
            }
            if (c.Count() - offsetC < B.numCols)
            {
                throw new ArgumentException("Length of 'c' isn't long enough");
            }

            int    cols   = B.numCols;
            double output = 0;

            for (int k = 0; k < B.numCols; k++)
            {
                double sum = 0;
                for (int i = 0; i < B.numRows; i++)
                {
                    sum += a[offsetA + i] * B.data[k + i * cols];
                }
                output += sum * c[offsetC + k];
            }

            return(output);
        }
        /**
         * Computes the inner product of A times A and stores the results in B. The inner product is symmetric and this
         * function will only store the lower triangle. The value of the upper triangular matrix is undefined.
         *
         * <p>B = A<sup>T</sup>*A</sup>
         *
         * @param A (Input) Matrix
         * @param B (Output) Storage for output.
         */
        public static void inner_reorder_lower(DMatrix1Row A, DMatrix1Row B)
        {
            int cols = A.numCols;

            B.reshape(cols, cols);

            Arrays.Fill(B.data, 0);
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    B.data[i * cols + j] += A.data[i] * A.data[j];
                }

                for (int k = 1; k < A.numRows; k++)
                {
                    int    indexRow = k * cols;
                    double valI     = A.data[i + indexRow];
                    int    indexB   = i * cols;
                    for (int j = 0; j <= i; j++)
                    {
                        B.data[indexB++] += valI * A.data[indexRow++];
                    }
                }
            }
        }
        /**
         * <p>
         * Adds to A &isin; &real; <sup>m &times; 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 + &gamma; x * y<sup>T</sup>
         * where x &isin; &real; <sup>m</sup> and y &isin; &real; <sup>n</sup> are vectors.
         * </p>
         * <p>
         * Which is equivalent to: A<sub>ij</sub> = A<sub>ij</sub> + &gamma; 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(double gamma, DMatrixD1 x, DMatrixD1 y, DMatrix1Row A)
        {
            int m = A.numRows;
            int n = A.numCols;

            int index = 0;

            if (gamma == 1.0)
            {
                for (int i = 0; i < m; i++)
                {
                    double 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++)
                {
                    double xdat = x.get(i);
                    for (int j = 0; j < n; j++)
                    {
                        A.plus(index++, gamma * xdat * y.get(j));
                    }
                }
            }
        }
        /**
         * @see CommonOps_DDRM#multAdd(double, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row)
         */
        public static void multAdd_small(double alpha, DMatrix1Row A, DMatrix1Row B, DMatrix1Row C)
        {
            UtilEjml.assertTrue(A != C && B != C, "Neither 'A' or 'B' can be the same matrix as 'C'");
            UtilEjml.assertShape(A.numCols, B.numRows, "The 'A' and 'B' matrices do not have compatible dimensions");
            UtilEjml.assertShape(A.numRows == C.numRows && B.numCols == C.numCols, "C is not compatible with A and B");

            //CONCURRENT_BELOW EjmlConcurrency.loopFor(0, A.numRows, i -> {
            for (int i = 0; i < A.numRows; i++)
            {
                int cIndex      = i * B.numCols;
                int aIndexStart = i * A.numCols;
                for (int j = 0; j < B.numCols; j++)
                {
                    double total = 0;

                    int indexA = aIndexStart;
                    int indexB = j;
                    int end    = indexA + B.numRows;
                    while (indexA < end)
                    {
                        total  += A.data[indexA++] * B.data[indexB];
                        indexB += B.numCols;
                    }

                    C.plus(cIndex++, alpha * total);
                }
            }
            //CONCURRENT_ABOVE });
        }
        //CONCURRENT_OMIT_END

        /**
         * @see CommonOps_DDRM#multTransB(double, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row)
         */
        public static void multTransB(double alpha, DMatrix1Row A, DMatrix1Row B, DMatrix1Row C)
        {
            UtilEjml.assertTrue(A != C && B != C, "Neither 'A' or 'B' can be the same matrix as 'C'");
            UtilEjml.assertShape(A.numCols, B.numCols, "The 'A' and 'B' matrices do not have compatible dimensions");
            C.reshape(A.numRows, B.numRows);

            //CONCURRENT_BELOW EjmlConcurrency.loopFor(0, A.numRows, xA -> {
            for (int xA = 0; xA < A.numRows; xA++)
            {
                int cIndex      = xA * B.numRows;
                int aIndexStart = xA * B.numCols;
                int end         = aIndexStart + B.numCols;
                int indexB      = 0;
                for (int xB = 0; xB < B.numRows; xB++)
                {
                    int indexA = aIndexStart;

                    double total = 0;
                    while (indexA < end)
                    {
                        total += A.data[indexA++] * B.data[indexB++];
                    }

                    C.set(cIndex++, alpha * total);
                }
            }
            //CONCURRENT_ABOVE });
        }
Ejemplo n.º 6
0
        public static bool lower4(DMatrix1Row A, DMatrix1Row L)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a21 = data[4];
            double a22 = data[5];
            double a31 = data[8];
            double a32 = data[9];
            double a33 = data[10];
            double a41 = data[12];
            double a42 = data[13];
            double a43 = data[14];
            double a44 = data[15];

            L.data[0]  = a11 = Math.Sqrt(a11);
            L.data[1]  = 0;
            L.data[2]  = 0;
            L.data[3]  = 0;
            L.data[4]  = a21 = (a21) / a11;
            L.data[5]  = a22 = Math.Sqrt(a22 - a21 * a21);
            L.data[6]  = 0;
            L.data[7]  = 0;
            L.data[8]  = a31 = (a31) / a11;
            L.data[9]  = a32 = (a32 - a31 * a21) / a22;
            L.data[10] = a33 = Math.Sqrt(a33 - a31 * a31 - a32 * a32);
            L.data[11] = 0;
            L.data[12] = a41 = (a41) / a11;
            L.data[13] = a42 = (a42 - a41 * a21) / a22;
            L.data[14] = a43 = (a43 - a41 * a31 - a42 * a32) / a33;
            L.data[15] = Math.Sqrt(a44 - a41 * a41 - a42 * a42 - a43 * a43);
            return(!UtilEjml.isUncountable(L.data[15]));
        }
        /**
         * @see CommonOps_DDRM#multTransA(double, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row)
         */
        public static void multTransA_small(double alpha, DMatrix1Row A, DMatrix1Row B, DMatrix1Row C)
        {
            UtilEjml.assertTrue(A != C && B != C, "Neither 'A' or 'B' can be the same matrix as 'C'");
            UtilEjml.assertShape(A.numRows, B.numRows, "The 'A' and 'B' matrices do not have compatible dimensions");
            C.reshape(A.numCols, B.numCols);

            //CONCURRENT_BELOW EjmlConcurrency.loopFor(0, A.numCols, i -> {
            for (int i = 0; i < A.numCols; i++)
            {
                int cIndex = i * B.numCols;
                for (int j = 0; j < B.numCols; j++)
                {
                    int indexA = i;
                    int indexB = j;
                    int end    = indexB + B.numRows * B.numCols;

                    double total = 0;
                    // loop for k
                    for (; indexB < end; indexB += B.numCols)
                    {
                        total  += A.data[indexA] * B.data[indexB];
                        indexA += A.numCols;
                    }

                    C.set(cIndex++, alpha * total);
                }
            }
            //CONCURRENT_ABOVE });
        }
 public static void outer(DMatrix1Row a, DMatrix1Row c)
 {
     for (int i = 0; i < a.numRows; i++)
     {
         int indexC1 = i * c.numCols + i;
         int indexC2 = indexC1;
         for (int j = i; j < a.numRows; j++, indexC2 += c.numCols)
         {
             int    indexA = i * a.numCols;
             int    indexB = j * a.numCols;
             double sum    = 0;
             int    end    = indexA + a.numCols;
             for (; indexA < end; indexA++, indexB++)
             {
                 sum += a.data[indexA] * a.data[indexB];
             }
             c.data[indexC2] = c.data[indexC1++] = sum;
         }
     }
     //        for( int i = 0; i < a.numRows; i++ ) {
     //            for( int j = 0; j < a.numRows; j++ ) {
     //                double sum = 0;
     //                for( int k = 0; k < a.numCols; k++ ) {
     //                    sum += a.get(i,k)*a.get(j,k);
     //                }
     //                c.set(i,j,sum);
     //            }
     //        }
 }
Ejemplo n.º 9
0
        /**
         * Performs a transpose across block sub-matrices.  Reduces
         * the number of cache misses on larger matrices.
         *
         * *NOTE* If this is beneficial is highly dependent on the computer it is run on. e.g:
         * - Q6600 Almost twice as fast as standard.
         * - Pentium-M Same speed and some times a bit slower than standard.
         *
         * @param A Original matrix.  Not modified.
         * @param A_tran Transposed matrix.  Modified.
         * @param blockLength Length of a block.
         */
        public static void block(DMatrix1Row A, DMatrix1Row A_tran,
                                 int blockLength)
        {
            for (int i = 0; i < A.numRows; i += blockLength)
            {
                int blockHeight = Math.Min(blockLength, A.numRows - i);

                int indexSrc = i * A.numCols;
                int indexDst = i;

                for (int j = 0; j < A.numCols; j += blockLength)
                {
                    int blockWidth = Math.Min(blockLength, A.numCols - j);

//                int indexSrc = i*A.numCols + j;
//                int indexDst = j*A_tran.numCols + i;

                    int indexSrcEnd = indexSrc + blockWidth;
//                for( int l = 0; l < blockWidth; l++ , indexSrc++ ) {
                    for (; indexSrc < indexSrcEnd; indexSrc++)
                    {
                        int rowSrc = indexSrc;
                        int rowDst = indexDst;
                        int end    = rowDst + blockHeight;
//                    for( int k = 0; k < blockHeight; k++ , rowSrc += A.numCols ) {
                        for (; rowDst < end; rowSrc += A.numCols)
                        {
                            // faster to write in sequence than to read in sequence
                            A_tran.data[rowDst++] = A.data[rowSrc];
                        }
                        indexDst += A_tran.numCols;
                    }
                }
            }
        }
Ejemplo n.º 10
0
        /**
         * Takes a matrix and splits it into a set of row or column vectors.
         *
         * @param A original matrix.
         * @param column If true then column vectors will be created.
         * @return Set of vectors.
         */
        public static DMatrixRMaj[] splitIntoVectors(DMatrix1Row A, bool column)
        {
            int w = column ? A.numCols : A.numRows;

            int M = column ? A.numRows : 1;
            int N = column ? 1 : A.numCols;

            int o = Math.Max(M, N);

            DMatrixRMaj[] ret = new DMatrixRMaj[w];

            for (int i = 0; i < w; i++)
            {
                DMatrixRMaj a = new DMatrixRMaj(M, N);

                if (column)
                {
                    subvector(A, 0, i, o, false, 0, a);
                }
                else
                {
                    subvector(A, i, 0, o, true, 0, a);
                }

                ret[i] = a;
            }

            return(ret);
        }
Ejemplo n.º 11
0
        /**
         * <p>
         * Performs the following operation:<br>
         * <br>
         * B = A + &alpha;I
         * <p>
         *
         * @param A A square matrix.  Not modified.
         * @param B A square matrix that the results are saved to.  Modified.
         * @param alpha Scaling factor for the identity matrix.
         */
        public static void addIdentity(DMatrix1Row A, DMatrix1Row B, double alpha)
        {
            if (A.numCols != A.numRows)
            {
                throw new ArgumentException("A must be square");
            }
            if (B.numCols != A.numCols || B.numRows != A.numRows)
            {
                throw new ArgumentException("B must be the same shape as A");
            }

            int n = A.numCols;

            int index = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++, index++)
                {
                    if (i == j)
                    {
                        B.set(index, A.get(index) + alpha);
                    }
                    else
                    {
                        B.set(index, A.get(index));
                    }
                }
            }
        }
        //CONCURRENT_OMIT_BEGIN

        /**
         * @see CommonOps_DDRM#mult(double, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row)
         */
        public static void mult_aux(double alpha, DMatrix1Row A, DMatrix1Row B, DMatrix1Row C, double[] aux)
        {
            UtilEjml.assertTrue(A != C && B != C, "Neither 'A' or 'B' can be the same matrix as 'C'");
            UtilEjml.assertShape(A.numCols, B.numRows, "The 'A' and 'B' matrices do not have compatible dimensions");
            C.reshape(A.numRows, B.numCols);

            if (aux == null)
            {
                aux = new double[B.numRows];
            }

            for (int j = 0; j < B.numCols; j++)
            {
                // create a copy of the column in B to avoid cache issues
                for (int k = 0; k < B.numRows; k++)
                {
                    aux[k] = B.unsafe_get(k, j);
                }

                int indexA = 0;
                for (int i = 0; i < A.numRows; i++)
                {
                    double total = 0;
                    for (int k = 0; k < B.numRows;)
                    {
                        total += A.data[indexA++] * aux[k++];
                    }
                    C.set(i * C.numCols + j, alpha * total);
                }
            }
        }
 public static void inner_small(DMatrix1Row a, DMatrix1Row c)
 {
     for (int i = 0; i < a.numCols; i++)
     {
         for (int j = i; j < a.numCols; j++)
         {
             int    indexC1 = i * c.numCols + j;
             int    indexC2 = j * c.numCols + i;
             int    indexA  = i;
             int    indexB  = j;
             double sum     = 0;
             int    end     = indexA + a.numRows * a.numCols;
             for (; indexA < end; indexA += a.numCols, indexB += a.numCols)
             {
                 sum += a.data[indexA] * a.data[indexB];
             }
             c.data[indexC1] = c.data[indexC2] = sum;
         }
     }
     //        for( int i = 0; i < a.numCols; i++ ) {
     //            for( int j = i; j < a.numCols; j++ ) {
     //                double sum = 0;
     //                for( int k = 0; k < a.numRows; k++ ) {
     //                    sum += a.get(k,i)*a.get(k,j);
     //                }
     //                c.set(i,j,sum);
     //                c.set(j,i,sum);
     //            }
     //        }
 }
Ejemplo n.º 14
0
        /**
         * <p>
         * Element wise p-norm:<br>
         * <br>
         * norm = {&sum;<sub>i=1:m</sub> &sum;<sub>j=1:n</sub> { |a<sub>ij</sub>|<sup>p</sup>}}<sup>1/p</sup>
         * </p>
         *
         * <p>
         * This is not the same as the induced p-norm used on matrices, but is the same as the vector p-norm.
         * </p>
         *
         * @param A Matrix. Not modified.
         * @param p p value.
         * @return The norm's value.
         */
        public static double elementP(DMatrix1Row A, double p)
        {
            if (p == 1)
            {
                return(CommonOps_DDRM.elementSumAbs(A));
            }
            if (p == 2)
            {
                return(normF(A));
            }
            else
            {
                double max = CommonOps_DDRM.elementMaxAbs(A);

                if (max == 0.0)
                {
                    return(0.0);
                }

                double total = 0;

                int size = A.getNumElements();

                for (int i = 0; i < size; i++)
                {
                    double a = A.get(i) / max;

                    total += Math.Pow(Math.Abs(a), p);
                }

                return(max * Math.Pow(total, 1.0 / p));
            }
        }
Ejemplo n.º 15
0
        public static bool upper4(DMatrix1Row A, DMatrix1Row R)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a12 = data[1];
            double a22 = data[5];
            double a13 = data[2];
            double a23 = data[6];
            double a33 = data[10];
            double a14 = data[3];
            double a24 = data[7];
            double a34 = data[11];
            double a44 = data[15];

            R.data[0]  = a11 = Math.Sqrt(a11);
            R.data[4]  = 0;
            R.data[8]  = 0;
            R.data[12] = 0;
            R.data[1]  = a12 = (a12) / a11;
            R.data[5]  = a22 = Math.Sqrt(a22 - a12 * a12);
            R.data[9]  = 0;
            R.data[13] = 0;
            R.data[2]  = a13 = (a13) / a11;
            R.data[6]  = a23 = (a23 - a12 * a13) / a22;
            R.data[10] = a33 = Math.Sqrt(a33 - a13 * a13 - a23 * a23);
            R.data[14] = 0;
            R.data[3]  = a14 = (a14) / a11;
            R.data[7]  = a24 = (a24 - a12 * a14) / a22;
            R.data[11] = a34 = (a34 - a13 * a14 - a23 * a24) / a33;
            R.data[15] = Math.Sqrt(a44 - a14 * a14 - a24 * a24 - a34 * a34);
            return(!UtilEjml.isUncountable(R.data[15]));
        }
        public static double det4(DMatrix1Row mat)
        {
            double[] data = mat.data;

            double a11 = data[5]; double a12 = data[6]; double a13 = data[7];
            double a21 = data[9]; double a22 = data[10]; double a23 = data[11];
            double a31 = data[13]; double a32 = data[14]; double a33 = data[15];

            double ret = 0;

            ret += data[0] * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
            a11  = data[4];
            a21  = data[8];
            a31  = data[12];
            ret -= data[1] * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
            a12  = data[5];
            a22  = data[9];
            a32  = data[13];
            ret += data[2] * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
            a13  = data[6];
            a23  = data[10];
            a33  = data[14];
            ret -= data[3] * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
            return(ret);
        }
Ejemplo n.º 17
0
        public static bool lower1(DMatrix1Row A, DMatrix1Row L)
        {
            double[] data = A.data;

            double a11 = data[0];

            L.data[0] = Math.Sqrt(a11);
            return(!UtilEjml.isUncountable(L.data[0]));
        }
Ejemplo n.º 18
0
        public static void invert(LinearSolverDense <DMatrixRMaj> solver, DMatrix1Row A, DMatrixRMaj A_inv)
        {
            if (A.numRows != A_inv.numRows || A.numCols != A_inv.numCols)
            {
                throw new ArgumentException("A and A_inv must have the same dimensions");
            }

            CommonOps_DDRM.setIdentity(A_inv);

            solver.solve(A_inv, A_inv);
        }
Ejemplo n.º 19
0
        /**
         * Computes the product of the diagonal elements.  For a diagonal or triangular
         * matrix this is the determinant.
         *
         * @param T A matrix.
         * @return product of the diagonal elements.
         */
        public static double diagProd(DMatrix1Row T)
        {
            double prod = 1.0;
            int    N    = Math.Min(T.numRows, T.numCols);

            for (int i = 0; i < N; i++)
            {
                prod *= T.unsafe_get(i, i);
            }

            return(prod);
        }
Ejemplo n.º 20
0
        /**
         * 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(DMatrix1Row A, DMatrixD1 B, DMatrixD1 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_DDRM.fill(C, 0);
                return;
            }

            double 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);
                }
            }
        }
Ejemplo n.º 21
0
        /**
         * <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(DMatrix1Row A, DMatrixD1 B, DMatrixD1 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_DDRM.fill(C, 0);
                return;
            }

            int    indexA = 0;
            int    cIndex = 0;
            double b0     = B.get(0);

            for (int i = 0; i < A.numRows; i++)
            {
                double total = A.get(indexA++) * b0;

                for (int j = 1; j < A.numCols; j++)
                {
                    total += A.get(indexA++) * B.get(j);
                }

                C.set(cIndex++, total);
            }
        }
Ejemplo n.º 22
0
 public static void setSubMatrix(DMatrix1Row src, DMatrix1Row dst,
                                 int srcRow, int srcCol, int dstRow, int dstCol,
                                 int numSubRows, int numSubCols)
 {
     for (int i = 0; i < numSubRows; i++)
     {
         for (int j = 0; j < numSubCols; j++)
         {
             double val = src.get(i + srcRow, j + srcCol);
             dst.set(i + dstRow, j + dstCol, val);
         }
     }
 }
Ejemplo n.º 23
0
        public static bool lower2(DMatrix1Row A, DMatrix1Row L)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a21 = data[2];
            double a22 = data[3];

            L.data[0] = a11 = Math.Sqrt(a11);
            L.data[1] = 0;
            L.data[2] = a21 = (a21) / a11;
            L.data[3] = Math.Sqrt(a22 - a21 * a21);
            return(!UtilEjml.isUncountable(L.data[3]));
        }
Ejemplo n.º 24
0
        public static bool upper2(DMatrix1Row A, DMatrix1Row R)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a12 = data[1];
            double a22 = data[3];

            R.data[0] = a11 = Math.Sqrt(a11);
            R.data[2] = 0;
            R.data[1] = a12 = (a12) / a11;
            R.data[3] = Math.Sqrt(a22 - a12 * a12);
            return(!UtilEjml.isUncountable(R.data[3]));
        }
Ejemplo n.º 25
0
        /**
         * A straight forward transpose.  Good for small non-square matrices.
         *
         * @param A Original matrix.  Not modified.
         * @param A_tran Transposed matrix.  Modified.
         */
        public static void standard(DMatrix1Row A, DMatrix1Row A_tran)
        {
            int index = 0;

            for (int i = 0; i < A_tran.numRows; i++)
            {
                int index2 = i;

                int end = index + A_tran.numCols;
                while (index < end)
                {
                    A_tran.data[index++] = A.data[index2];
                    index2 += A.numCols;
                }
            }
        }
        /**
         * <p>
         * Sets A &isin; &real; <sup>m &times; 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 &isin; &real; <sup>m</sup> and y &isin; &real; <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(DMatrixD1 x, DMatrixD1 y, DMatrix1Row A)
        {
            int m = A.numRows;
            int n = A.numCols;

            int index = 0;

            for (int i = 0; i < m; i++)
            {
                double xdat = x.get(i);
                for (int j = 0; j < n; j++)
                {
                    A.set(index++, xdat * y.get(j));
                }
            }
        }
Ejemplo n.º 27
0
        /**
         * <p>
         * Creates a reflector from the provided vector.<br>
         * <br>
         * Q = I - &gamma; u u<sup>T</sup><br>
         * &gamma; = 2/||u||<sup>2</sup>
         * </p>
         *
         * <p>
         * In practice {@link VectorVectorMult_DDRM#householder(double, DMatrixD1, DMatrixD1, DMatrixD1)}  multHouseholder}
         * should be used for performance reasons since there is no need to calculate Q explicitly.
         * </p>
         *
         * @param u A vector. Not modified.
         * @return An orthogonal reflector.
         */
        public static DMatrixRMaj createReflector(DMatrix1Row u)
        {
            if (!MatrixFeatures_DDRM.isVector(u))
            {
                throw new ArgumentException("u must be a vector");
            }

            double norm  = NormOps_DDRM.fastNormF(u);
            double gamma = -2.0 / (norm * norm);

            DMatrixRMaj Q = CommonOps_DDRM.identity(u.getNumElements());

            CommonOps_DDRM.multAddTransB(gamma, u, u, Q);

            return(Q);
        }
Ejemplo n.º 28
0
        public static bool upper5(DMatrix1Row A, DMatrix1Row R)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a12 = data[1];
            double a22 = data[6];
            double a13 = data[2];
            double a23 = data[7];
            double a33 = data[12];
            double a14 = data[3];
            double a24 = data[8];
            double a34 = data[13];
            double a44 = data[18];
            double a15 = data[4];
            double a25 = data[9];
            double a35 = data[14];
            double a45 = data[19];
            double a55 = data[24];

            R.data[0]  = a11 = Math.Sqrt(a11);
            R.data[5]  = 0;
            R.data[10] = 0;
            R.data[15] = 0;
            R.data[20] = 0;
            R.data[1]  = a12 = (a12) / a11;
            R.data[6]  = a22 = Math.Sqrt(a22 - a12 * a12);
            R.data[11] = 0;
            R.data[16] = 0;
            R.data[21] = 0;
            R.data[2]  = a13 = (a13) / a11;
            R.data[7]  = a23 = (a23 - a12 * a13) / a22;
            R.data[12] = a33 = Math.Sqrt(a33 - a13 * a13 - a23 * a23);
            R.data[17] = 0;
            R.data[22] = 0;
            R.data[3]  = a14 = (a14) / a11;
            R.data[8]  = a24 = (a24 - a12 * a14) / a22;
            R.data[13] = a34 = (a34 - a13 * a14 - a23 * a24) / a33;
            R.data[18] = a44 = Math.Sqrt(a44 - a14 * a14 - a24 * a24 - a34 * a34);
            R.data[23] = 0;
            R.data[4]  = a15 = (a15) / a11;
            R.data[9]  = a25 = (a25 - a12 * a15) / a22;
            R.data[14] = a35 = (a35 - a13 * a15 - a23 * a25) / a33;
            R.data[19] = a45 = (a45 - a14 * a15 - a24 * a25 - a34 * a35) / a44;
            R.data[24] = Math.Sqrt(a55 - a15 * a15 - a25 * a25 - a35 * a35 - a45 * a45);
            return(!UtilEjml.isUncountable(R.data[24]));
        }
        /**
         * @see CommonOps_DDRM#mult(double, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row, org.ejml.data.DMatrix1Row)
         */
        public static void mult_reorder(double alpha, DMatrix1Row A, DMatrix1Row B, DMatrix1Row C)
        {
            UtilEjml.assertTrue(A != C && B != C, "Neither 'A' or 'B' can be the same matrix as 'C'");
            UtilEjml.assertShape(A.numCols, B.numRows, "The 'A' and 'B' matrices do not have compatible dimensions");
            C.reshape(A.numRows, B.numCols);

            if (A.numCols == 0 || A.numRows == 0)
            {
                CommonOps_DDRM.fill(C, 0);
                return;
            }
            int endOfKLoop = B.numRows * B.numCols;

            //CONCURRENT_BELOW EjmlConcurrency.loopFor(0, A.numRows, i -> {
            for (int i = 0; i < A.numRows; i++)
            {
                int indexCbase = i * C.numCols;
                int indexA     = i * A.numCols;

                // need to assign C.data to a value initially
                int indexB = 0;
                int indexC = indexCbase;
                int end    = indexB + B.numCols;

                double valA = alpha * A.data[indexA++];

                while (indexB < end)
                {
                    C.set(indexC++, valA * B.data[indexB++]);
                }

                // now add to it
                while (indexB != endOfKLoop)
                { // k loop
                    indexC = indexCbase;
                    end    = indexB + B.numCols;

                    valA = alpha * A.data[indexA++];

                    while (indexB < end)
                    { // j loop
                        C.data[indexC++] += valA * B.data[indexB++];
                    }
                }
            }
            //CONCURRENT_ABOVE });
        }
Ejemplo n.º 30
0
        public static bool lower5(DMatrix1Row A, DMatrix1Row L)
        {
            double[] data = A.data;

            double a11 = data[0];
            double a21 = data[5];
            double a22 = data[6];
            double a31 = data[10];
            double a32 = data[11];
            double a33 = data[12];
            double a41 = data[15];
            double a42 = data[16];
            double a43 = data[17];
            double a44 = data[18];
            double a51 = data[20];
            double a52 = data[21];
            double a53 = data[22];
            double a54 = data[23];
            double a55 = data[24];

            L.data[0]  = a11 = Math.Sqrt(a11);
            L.data[1]  = 0;
            L.data[2]  = 0;
            L.data[3]  = 0;
            L.data[4]  = 0;
            L.data[5]  = a21 = (a21) / a11;
            L.data[6]  = a22 = Math.Sqrt(a22 - a21 * a21);
            L.data[7]  = 0;
            L.data[8]  = 0;
            L.data[9]  = 0;
            L.data[10] = a31 = (a31) / a11;
            L.data[11] = a32 = (a32 - a31 * a21) / a22;
            L.data[12] = a33 = Math.Sqrt(a33 - a31 * a31 - a32 * a32);
            L.data[13] = 0;
            L.data[14] = 0;
            L.data[15] = a41 = (a41) / a11;
            L.data[16] = a42 = (a42 - a41 * a21) / a22;
            L.data[17] = a43 = (a43 - a41 * a31 - a42 * a32) / a33;
            L.data[18] = a44 = Math.Sqrt(a44 - a41 * a41 - a42 * a42 - a43 * a43);
            L.data[19] = 0;
            L.data[20] = a51 = (a51) / a11;
            L.data[21] = a52 = (a52 - a51 * a21) / a22;
            L.data[22] = a53 = (a53 - a51 * a31 - a52 * a32) / a33;
            L.data[23] = a54 = (a54 - a51 * a41 - a52 * a42 - a53 * a43) / a44;
            L.data[24] = Math.Sqrt(a55 - a51 * a51 - a52 * a52 - a53 * a53 - a54 * a54);
            return(!UtilEjml.isUncountable(L.data[24]));
        }