/**
         * <p>
         * return = x<sup>T</sup>*A*y
         * </p>
         *
         * @param x  A vector with n elements. Not modified.
         * @param A  A matrix with n by m elements.  Not modified.
         * @param y  A vector with m elements. Not modified.
         * @return  The results.
         */
        public static double innerProdA(DMatrixD1 x, DMatrixD1 A, DMatrixD1 y)
        {
            int n = A.numRows;
            int m = A.numCols;

            if (x.getNumElements() != n)
            {
                throw new ArgumentException("Unexpected number of elements in x");
            }
            if (y.getNumElements() != m)
            {
                throw new ArgumentException("Unexpected number of elements in y");
            }

            double result = 0;

            for (int i = 0; i < m; i++)
            {
                double total = 0;

                for (int j = 0; j < n; j++)
                {
                    total += x.get(j) * A.unsafe_get(j, i);
                }

                result += total * y.get(i);
            }

            return(result);
        }
        public static double elementMin(DMatrixD1 a, ElementLocation loc)
        {
            int size = a.NumElements;

            int    bestIndex = 0;
            double min       = a.get(0);

            for (int i = 1; i < size; i++)
            {
                double val = a.get(i);
                if (val < min)
                {
                    bestIndex = i;
                    min       = val;
                }
            }

            if (loc != null)
            {
                loc.row = bestIndex / a.numCols;
                loc.col = bestIndex % a.numCols;
            }

            return(min);
        }
        public static double elementMaxAbs(DMatrixD1 a, ElementLocation loc)
        {
            int size = a.NumElements;

            int    bestIndex = 0;
            double max       = 0;

            for (int i = 0; i < size; i++)
            {
                double val = Math.Abs(a.get(i));
                if (val > max)
                {
                    bestIndex = i;
                    max       = val;
                }
            }

            if (loc != null)
            {
                loc.row = bestIndex / a.numCols;
                loc.col = bestIndex % a.numCols;
            }

            return(max);
        }
示例#4
0
        /**
         * Creates a window visually showing the matrix's state.  Block means an element is zero.
         * Red positive and blue negative.  More intense the color larger the element's absolute value
         * is.
         *
         * @param A A matrix.
         * @param title Name of the window.
         */
        public static void show(DMatrixD1 A, string title)
        {
            // TODO : BRS: Implement for .NET
            throw new NotImplementedException();

            /*
             * JFrame frame = new JFrame(title);
             *
             * int width = 300;
             * int height = 300;
             *
             * if (A.numRows > A.numCols)
             * {
             *  width = width * A.numCols / A.numRows;
             * }
             * else
             * {
             *  height = height * A.numRows / A.numCols;
             * }
             *
             * DMatrixComponent panel = new DMatrixComponent(width, height);
             * panel.setMatrix(A);
             *
             * frame.add(panel, BorderLayout.CENTER);
             *
             * frame.pack();
             * frame.setVisible(true);
             */
        }
        /**
         * <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));
                    }
                }
            }
        }
        /**
         * <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.NumElements != n)
            {
                throw new ArgumentException("Unexpected number of elements in x");
            }
            if (y.NumElements != 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);
        }
示例#7
0
 public void set(DMatrixD1 original,
                 int row0, int row1, int col0, int col1)
 {
     this.original = original;
     this.row0     = row0;
     this.col0     = col0;
     this.row1     = row1;
     this.col1     = col1;
 }
示例#8
0
        /**
         * <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();
            }
        }
示例#9
0
        public /* synchronized */ void setMatrix(DMatrixD1 A)
        {
            // TODO : BRS: Convert to .NET
            throw new NotImplementedException();

            /*
             * double maxValue = CommonOps_DDRM.elementMaxAbs(A);
             * renderMatrix(A, image, maxValue);
             * repaint();
             */
        }
        public static void elementDiv(DMatrixD1 A, DMatrixD1 B)
        {
            UtilEjml.checkSameShape(A, B, true);

            int length = A.NumElements;

            for (int i = 0; i < length; i++)
            {
                A.div(i, B.get(i));
            }
        }
示例#11
0
        /**
         * <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 randJava.Util.Random number generator used to fill the matrix.
         */
        public static void fillGaussian(DMatrixD1 mat, double mean, double stdev, Java.Util.Random rand)
        {
            double[] d    = mat.getData();
            int      size = mat.NumElements;

            Java.Util.Random random2 = new Java.Util.Random();
            for (int i = 0; i < size; i++)
            {
                d[i] = mean + stdev * (double)random2.NextGaussian();
            }
        }
示例#12
0
        /**
         * <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;
            }
        }
示例#13
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);
                }
            }
        }
示例#14
0
        /**
         * <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 randJava.Util.Random number generator used to fill the matrix.
         */
        public static void fillUniform(DMatrixD1 mat, double min, double max, Java.Util.Random rand)
        {
            double[] d    = mat.getData();
            int      size = mat.NumElements;

            double r = max - min;

            for (int i = 0; i < size; i++)
            {
                d[i] = r * rand.NextDouble() + min;
            }
        }
示例#15
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);
            }
        }
        /**
         * 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.NumElements;

            for (int i = 0; i < length; i++)
            {
                if (Double.IsNaN(m.get(i)))
                {
                    return(true);
                }
            }
            return(false);
        }
        public static double elementSumAbs(DMatrixD1 mat)
        {
            double total = 0;

            int size = mat.NumElements;

            for (int i = 0; i < size; i++)
            {
                total += Math.Abs(mat.get(i));
            }

            return(total);
        }
        /**
         * <p>
         * Computes the inner product of the two vectors.  In geometry this is known as the dot product.<br>
         * <br>
         * &sum;<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.NumElements;

            double total = 0;

            for (int i = 0; i < m; i++)
            {
                total += x.get(i) * y.get(i);
            }

            return(total);
        }
        /**
         * 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.NumElements;

            for (int i = 0; i < length; i++)
            {
                if (Math.Abs(m.get(i)) > tol)
                {
                    return(false);
                }
            }
            return(true);
        }
示例#20
0
        /**
         * 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.NumElements;

            for (int i = 0; i < length; i++)
            {
                double a = m.get(i);
                if (Double.IsNaN(a) || Double.IsInfinity(a))
                {
                    return(true);
                }
            }
            return(false);
        }
        /**
         * <p>
         * Computes the magnitude of the complex number in the input matrix and stores the results in the output
         * matrix.
         * </p>
         *
         * magnitude = sqrt(real^2 + imaginary^2)
         *
         * @param input Complex matrix. Not modified.
         * @param output real matrix. Modified.
         */
        public static void magnitude(ZMatrixD1 input, DMatrixD1 output)
        {
            output.reshape(input.numRows, input.numCols);

            int length = input.DataLength;

            for (int i = 0; i < length; i += 2)
            {
                double real      = input.data[i];
                double imaginary = input.data[i + 1];

                output.data[i / 2] = Math.Sqrt(real * real + imaginary * imaginary);
            }
        }
示例#23
0
        /**
         * <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));
        }
示例#24
0
        /**
         * Converts the real matrix into a complex matrix.
         *
         * @param input Real matrix. Not modified.
         * @param output Complex matrix. Modified.
         */
        public static void convert(DMatrixD1 input, ZMatrixD1 output)
        {
            if (input.numCols != output.numCols || input.numRows != output.numRows)
            {
                throw new ArgumentException("The matrices are not all the same dimension.");
            }
            Array.Clear(output.data, 0, output.getDataLength());

            int length = output.getDataLength();

            for (int i = 0; i < length; i += 2)
            {
                output.data[i] = input.data[i / 2];
            }
        }
        /**
         * <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));
                }
            }
        }
示例#26
0
        /**
         * <p>
         * Computes the p=1 p-norm of the difference between the two Matrices:<br>
         * <br>
         * &sum;<sub>i=1:m</sub> &sum;<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>
         * Multiplies a householder reflection against a vector:<br>
         * <br>
         * y = (I + &gamma; 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.NumElements;

            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);
            }
        }
示例#28
0
        /**
         * <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 double elementDiagonalMaxAbs(DMatrixD1 a)
        {
            int size = Math.Min(a.numRows, a.numCols);

            double max = 0;

            for (int i = 0; i < size; i++)
            {
                double val = Math.Abs(a.get(i, i));
                if (val > max)
                {
                    max = val;
                }
            }

            return(max);
        }
示例#29
0
        /**
         * <p>
         * Computes the magnitude of the complex number in the input matrix and stores the results in the output
         * matrix.
         * </p>
         *
         * magnitude = sqrt(real^2 + imaginary^2)
         *
         * @param input Complex matrix. Not modified.
         * @param output real matrix. Modified.
         */
        public static void magnitude(ZMatrixD1 input, DMatrixD1 output)
        {
            if (input.numCols != output.numCols || input.numRows != output.numRows)
            {
                throw new ArgumentException("The matrices are not all the same dimension.");
            }

            int length = input.getDataLength();

            for (int i = 0; i < length; i += 2)
            {
                double real      = input.data[i];
                double imaginary = input.data[i + 1];

                output.data[i / 2] = Math.Sqrt(real * real + imaginary * imaginary);
            }
        }
示例#30
0
        /**
         * <p>
         * Computes the F norm of the difference between the two Matrices:<br>
         * <br>
         * Sqrt{&sum;<sub>i=1:m</sub> &sum;<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));
        }