Exemplo n.º 1
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];
            }
        }
Exemplo n.º 2
0
        /**
         * <p>
         * Sets every element in the matrix to the specified value.<br>
         * <br>
         * a<sub>ij</sub> = value
         * <p>
         *
         * @param a A matrix whose elements are about to be set. Modified.
         * @param real The real component
         * @param imaginary The imaginary component
         */
        public static void fill(ZMatrixD1 a, double real, double imaginary)
        {
            int N = a.getDataLength();

            for (int i = 0; i < N; i += 2)
            {
                a.data[i]     = real;
                a.data[i + 1] = imaginary;
            }
        }
Exemplo n.º 3
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(ZMatrixD1 mat, double min, double max, IMersenneTwister rand)
        {
            double[] d    = mat.getData();
            int      size = mat.getDataLength();

            double r = max - min;

            for (int i = 0; i < size; i++)
            {
                d[i] = r * rand.NextDouble() + min;
            }
        }
Exemplo n.º 4
0
        /**
         * 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(ZMatrixD1 m)
        {
            int length = m.getDataLength();

            for (int i = 0; i < length; i++)
            {
                if (double.IsNaN(m.data[i]))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 5
0
        /**
         * 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(ZMatrixD1 m)
        {
            int length = m.getDataLength();

            for (int i = 0; i < length; i++)
            {
                double a = m.data[i];
                if (double.IsNaN(a) || double.IsInfinity(a))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        /**
         * <p>Performs the following operation:<br>
         * <br>
         * c = a - b <br>
         * c<sub>ij</sub> = a<sub>ij</sub> - b<sub>ij</sub> <br>
         * </p>
         *
         * <p>
         * Matrix C can be the same instance as Matrix A and/or B.
         * </p>
         *
         * @param a A Matrix. Not modified.
         * @param b A Matrix. Not modified.
         * @param c A Matrix where the results are stored. Modified.
         */
        public static void subtract(ZMatrixD1 a, ZMatrixD1 b, ZMatrixD1 c)
        {
            if (a.numCols != b.numCols || a.numRows != b.numRows ||
                a.numCols != c.numCols || a.numRows != c.numRows)
            {
                throw new ArgumentException("The matrices are not all the same dimension.");
            }

            int length = a.getDataLength();

            for (int i = 0; i < length; i++)
            {
                c.data[i] = a.data[i] - b.data[i];
            }
        }
Exemplo n.º 7
0
        /**
         * <p>
         * Computes the complex conjugate of the input matrix.<br>
         * <br>
         * real<sub>i,j</sub> = real<sub>i,j</sub><br>
         * imaginary<sub>i,j</sub> = -1*imaginary<sub>i,j</sub><br>
         * </p>
         *
         * @param input Input matrix.  Not modified.
         * @param output The complex conjugate of the input matrix.  Modified.
         */
        public static void conjugate(ZMatrixD1 input, ZMatrixD1 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)
            {
                output.data[i]     = input.data[i];
                output.data[i + 1] = -input.data[i + 1];
            }
        }
Exemplo n.º 8
0
        /**
         * <p>
         * Returns the value of the imaginary element in the matrix that has the minimum value.<br>
         * <br>
         * Min{ a<sub>ij</sub> } for all i and j<br>
         * </p>
         *
         * @param a A matrix. Not modified.
         * @return The the minimum value out of all the real values.
         */
        public static double elementMinImaginary(ZMatrixD1 a)
        {
            int size = a.getDataLength();

            double min = a.data[1];

            for (int i = 3; i < size; i += 2)
            {
                double val = a.data[i];
                if (val < min)
                {
                    min = val;
                }
            }

            return(min);
        }
Exemplo n.º 9
0
        /**
         * <p>
         * Returns the value of the imaginary element in the matrix that has the minimum value.<br>
         * <br>
         * Min{ a<sub>ij</sub> } for all i and j<br>
         * </p>
         *
         * @param a A matrix. Not modified.
         * @return The the minimum value out of all the real values.
         */
        public static double elementMaxImaginary(ZMatrixD1 a)
        {
            int size = a.getDataLength();

            double max = a.data[1];

            for (int i = 3; i < size; i += 2)
            {
                double val = a.data[i];
                if (val > max)
                {
                    max = val;
                }
            }

            return(max);
        }
Exemplo n.º 10
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);
            }
        }
Exemplo n.º 11
0
        /**
         * <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>| &le; tol.<br>
         * Otherwise both numbers must both be Double.NaN, Double.POSITIVE_INFINITY, or
         * Double.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(ZMatrixD1 a, ZMatrixD1 b, double 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.getDataLength();

            for (int i = 0; i < length; i++)
            {
                double valA = a.data[i];
                double valB = b.data[i];

                // if either is negative or positive infinity the result will be positive infinity
                // if either is NaN the result will be NaN
                double diff = Math.Abs(valA - valB);

                // diff = NaN == false
                // diff = infinity == false
                if (tol >= diff)
                {
                    continue;
                }

                if (double.IsNaN(valA))
                {
                    return(double.IsNaN(valB));
                }
                else if (double.IsInfinity(valA))
                {
                    return(valA == valB);
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 12
0
        /**
         * <p>Performs  element by element multiplication operation with a complex numbert<br>
         * <br>
         * output<sub>ij</sub> = input<sub>ij</sub> * (real + imaginary*i) <br>
         * </p>
         * @param input The left matrix in the multiplication operation. Not modified.
         * @param real Real component of the number it is multiplied by
         * @param imaginary Imaginary component of the number it is multiplied by
         * @param output Where the results of the operation are stored. Modified.
         */
        public static void elementMultiply(ZMatrixD1 input, double real, double imaginary, ZMatrixD1 output)
        {
            if (input.numCols != output.numCols || input.numRows != output.numRows)
            {
                throw new ArgumentException("The 'input' and 'output' matrices do not have compatible dimensions");
            }

            int N = input.getDataLength();

            for (int i = 0; i < N; i += 2)
            {
                double inReal  = input.data[i];
                double intImag = input.data[i + 1];

                output.data[i]     = inReal * real - intImag * imaginary;
                output.data[i + 1] = inReal * imaginary + intImag * real;
            }
        }
Exemplo n.º 13
0
        /**
         * <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(ZMatrixD1 a, ZMatrixD1 b)
        {
            if (a.numRows != b.numRows || a.numCols != b.numCols)
            {
                return(false);
            }

            int length = a.getDataLength();

            for (int i = 0; i < length; i++)
            {
                if (!(a.data[i] == b.data[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 14
0
        /**
         * Places the imaginary component of the input matrix into the output matrix.
         *
         * @param input Complex matrix. Not modified.
         * @param output real matrix. Modified.
         */
        public static DMatrixRMaj stripImaginary(ZMatrixD1 input, DMatrixRMaj output)
        {
            if (output == null)
            {
                output = new DMatrixRMaj(input.numRows, input.numCols);
            }
            else 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 = 1; i < length; i += 2)
            {
                output.data[i / 2] = input.data[i];
            }
            return(output);
        }
Exemplo n.º 15
0
        /**
         * <p>Performs  element by element division operation with a complex number on the right<br>
         * <br>
         * output<sub>ij</sub> = (real + imaginary*i) / input<sub>ij</sub> <br>
         * </p>
         * @param real Real component of the number it is multiplied by
         * @param imaginary Imaginary component of the number it is multiplied by
         * @param input The right matrix in the multiplication operation. Not modified.
         * @param output Where the results of the operation are stored. Modified.
         */
        public static void elementDivide(double real, double imaginary, ZMatrixD1 input, ZMatrixD1 output)
        {
            if (input.numCols != output.numCols || input.numRows != output.numRows)
            {
                throw new ArgumentException("The 'input' and 'output' matrices do not have compatible dimensions");
            }

            int N = input.getDataLength();

            for (int i = 0; i < N; i += 2)
            {
                double inReal = input.data[i];
                double inImag = input.data[i + 1];

                double norm = inReal * inReal + inImag * inImag;

                output.data[i]     = (real * inReal + imaginary * inImag) / norm;
                output.data[i + 1] = (imaginary * inReal - real * inImag) / norm;
            }
        }
Exemplo n.º 16
0
        /**
         * <p>
         * Returns the magnitude squared of the complex element with the largest magnitude<br>
         * <br>
         * Max{ |a<sub>ij</sub>|^2 } for all i and j<br>
         * </p>
         *
         * @param a A matrix. Not modified.
         * @return The max magnitude squared
         */
        public static double elementMaxMagnitude2(ZMatrixD1 a)
        {
            int size = a.getDataLength();

            double max = 0;

            for (int i = 0; i < size;)
            {
                double real      = a.data[i++];
                double imaginary = a.data[i++];

                double m = real * real + imaginary * imaginary;

                if (m > max)
                {
                    max = m;
                }
            }

            return(max);
        }
Exemplo n.º 17
0
        /**
         * <p>
         * Checks to see if each element in the two matrices are within tolerance of
         * each other: tol &ge; |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(ZMatrixD1, ZMatrixD1)}
         * </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(ZMatrixD1 a, ZMatrixD1 b, double tol)
        {
            if (a.numRows != b.numRows || a.numCols != b.numCols)
            {
                return(false);
            }

            if (tol == 0.0)
            {
                return(isEquals(a, b));
            }

            int length = a.getDataLength();

            for (int i = 0; i < length; i++)
            {
                if (!(tol >= Math.Abs(a.data[i] - b.data[i])))
                {
                    return(false);
                }
            }
            return(true);
        }