Example #1
0
        /// <summary>
        /// Compute $A^T A$, where A is a matrix. </summary>
        /// <param name="a"> The matrix </param>
        /// <returns> The result of $A^T A$ </returns>
        public virtual DoubleMatrix matrixTransposeMultiplyMatrix(DoubleMatrix a)
        {
            ArgChecker.notNull(a, "a");
            int n = a.rowCount();
            int m = a.columnCount();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] data = new double[m][m];
            double[][] data = RectangularArrays.ReturnRectangularDoubleArray(m, m);
            for (int i = 0; i < m; i++)
            {
                double sum = 0d;
                for (int k = 0; k < n; k++)
                {
                    sum += a.get(k, i) * a.get(k, i);
                }
                data[i][i] = sum;

                for (int j = i + 1; j < m; j++)
                {
                    sum = 0d;
                    for (int k = 0; k < n; k++)
                    {
                        sum += a.get(k, i) * a.get(k, j);
                    }
                    data[i][j] = sum;
                    data[j][i] = sum;
                }
            }
            return(DoubleMatrix.ofUnsafe(data));
        }
Example #2
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: private void assertMatrixEquals(final com.opengamma.strata.collect.array.Matrix m1, final com.opengamma.strata.collect.array.Matrix m2)
        private void assertMatrixEquals(Matrix m1, Matrix m2)
        {
            if (m1 is DoubleArray)
            {
                assertTrue(m2 is DoubleArray);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray m3 = (com.opengamma.strata.collect.array.DoubleArray) m1;
                DoubleArray m3 = (DoubleArray)m1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray m4 = (com.opengamma.strata.collect.array.DoubleArray) m2;
                DoubleArray m4 = (DoubleArray)m2;
                assertEquals(m3.size(), m4.size());
                for (int i = 0; i < m3.size(); i++)
                {
                    assertEquals(m3.get(i), m4.get(i), EPS);
                }
                return;
            }
            if (m2 is DoubleMatrix)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix m3 = (com.opengamma.strata.collect.array.DoubleMatrix) m1;
                DoubleMatrix m3 = (DoubleMatrix)m1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix m4 = (com.opengamma.strata.collect.array.DoubleMatrix) m2;
                DoubleMatrix m4 = (DoubleMatrix)m2;
                assertEquals(m3.size(), m4.size());
                assertEquals(m3.rowCount(), m4.rowCount());
                assertEquals(m3.columnCount(), m4.columnCount());
                for (int i = 0; i < m3.rowCount(); i++)
                {
                    for (int j = 0; j < m3.columnCount(); j++)
                    {
                        assertEquals(m3.get(i, j), m4.get(i, j), EPS);
                    }
                }
            }
        }
Example #3
0
        public LeastSquareResults(double chiSq, DoubleArray parameters, DoubleMatrix covariance, DoubleMatrix inverseJacobian)
        {
            ArgChecker.isTrue(chiSq >= 0, "chi square < 0");
            ArgChecker.notNull(parameters, "parameters");
            ArgChecker.notNull(covariance, "covariance");
            int n = parameters.size();

            ArgChecker.isTrue(covariance.columnCount() == n, "covariance matrix not square");
            ArgChecker.isTrue(covariance.rowCount() == n, "covariance matrix wrong size");
            //TODO test size of inverse Jacobian
            _chiSq           = chiSq;
            _parameters      = parameters;
            _covariance      = covariance;
            _inverseJacobian = inverseJacobian;
        }
        /// <summary>
        /// Assert that two matrices (as <seealso cref="DoubleMatrix"/>) equal concerning a delta. To be equal the matrices
        /// must be the same size, and each element must match to within delta.
        /// If they are not equal an AssertionFailedError is thrown. </summary>
        /// <param name="m1"> expected matrix </param>
        /// <param name="m2"> actual matrix </param>
        /// <param name="delta"> the allowed difference between the elements  </param>
        public static void assertEqualsMatrix(DoubleMatrix m1, DoubleMatrix m2, double delta)
        {
            ArgChecker.notNull(m1, "m1");
            ArgChecker.notNull(m2, "m2");
            int rows = m1.rowCount();
            int cols = m1.columnCount();

            assertEquals("Number of rows:", rows, m2.rowCount());
            assertEquals("Number of columns:", cols, m2.columnCount());
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    assertEquals("", m1.get(i, j), m2.get(i, j), delta);
                }
            }
        }
Example #5
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: private void checkEquals(final com.opengamma.strata.collect.array.DoubleMatrix x, final com.opengamma.strata.collect.array.DoubleMatrix y)
        private void checkEquals(DoubleMatrix x, DoubleMatrix y)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n = x.rowCount();
            int n = x.rowCount();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int m = x.columnCount();
            int m = x.columnCount();

            assertEquals(n, y.rowCount());
            assertEquals(m, y.columnCount());
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    assertEquals(x.get(i, j), y.get(i, j), EPS);
                }
            }
        }
        public static void notNaNOrInfinite(DoubleMatrix x)
        {
            int rows = x.rowCount();
            int cols = x.columnCount();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    double temp = x.get(i, j);
                    if (double.IsNaN(temp))
                    {
                        throw new MathException("Matrix contains a NaN");
                    }
                    if (double.IsInfinity(temp))
                    {
                        throw new MathException("Matrix contains an infinite");
                    }
                }
            }
        }
Example #7
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: private void checkIdentity(final com.opengamma.strata.collect.array.DoubleMatrix x)
        private void checkIdentity(DoubleMatrix x)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n = x.rowCount();
            int n = x.rowCount();

            assertEquals(x.columnCount(), n);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        assertEquals(1.0, x.get(i, i), EPS);
                    }
                    else
                    {
                        assertEquals(0.0, x.get(i, j), EPS);
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Returns the Kronecker product of two matrices. If $\mathbf{A}$ is an $m
        /// \times n$ matrix and $\mathbf{B}$ is a $p \times q$ matrix, then the
        /// Kronecker product $A \otimes B$ is an $mp \times nq$ matrix with elements
        /// $$
        /// \begin{align*}
        /// A \otimes B &=
        /// \begin{pmatrix}
        /// a_{11}\mathbf{B} & \cdots & a_{1n}\mathbf{B} \\
        /// \vdots & \ddots & \vdots \\
        /// a_{m1}\mathbf{B} & \cdots & a_{mn}\mathbf{B}
        /// \end{pmatrix}\\
        /// &=
        /// \begin{pmatrix}
        /// a_{11}b_{11} & a_{11}b_{12} & \cdots & a_{11}b_{1q} & \cdots & a_{1n}b_{11} & a_{1n}b_{12} & \cdots & a_{1n}b_{1q}\\
        /// a_{11}b_{21} & a_{11}b_{22} & \cdots & a_{11}b_{2q} & \cdots & a_{1n}b_{21} & a_{1n}b_{22} & \cdots & a_{1n}b_{2q} \\
        /// \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \ddots & \cdots \\
        /// a_{11}b_{p1} & a_{11}b_{p2} & \cdots & a_{11}b_{pq} & \cdots & a_{1n}b_{p1} & a_{1n}b_{p2} & \cdots & a_{1n}b_{pq} \\
        /// \vdots & \vdots & & \vdots & \ddots & \vdots & \vdots & & \cdots \\
        /// a_{m1}b_{11} & a_{m1}b_{12} & \cdots & a_{m1}b_{1q} & \cdots & a_{mn}b_{11} & a_{mn}b_{12} & \cdots & a_{mn}b_{1q} \\
        /// a_{m1}b_{21} & a_{m1}b_{22} & \cdots & a_{m1}b_{2q} & \cdots & a_{mn}b_{21} & a_{mn}b_{22} & \cdots & a_{mn}b_{2q} \\
        /// \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \ddots & \cdots \\
        /// a_{m1}b_{p1} & a_{m1}b_{p2} & \cdots & a_{m1}b_{pq} & \cdots & a_{mn}b_{p1} & a_{mn}b_{p2} & \cdots & a_{mn}b_{pq}
        /// \end{pmatrix}
        /// \end{align*}
        /// $$ </summary>
        /// <param name="m1"> The first matrix, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param>
        /// <param name="m2"> The second matrix, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param>
        /// <returns> The Kronecker product </returns>
        public virtual Matrix kroneckerProduct(Matrix m1, Matrix m2)
        {
            ArgChecker.notNull(m1, "m1");
            ArgChecker.notNull(m2, "m2");
            if (m1 is DoubleMatrix && m2 is DoubleMatrix)
            {
                DoubleMatrix matrix1 = (DoubleMatrix)m1;
                DoubleMatrix matrix2 = (DoubleMatrix)m2;
                int          aRows   = matrix1.rowCount();
                int          aCols   = matrix1.columnCount();
                int          bRows   = matrix2.rowCount();
                int          bCols   = matrix2.columnCount();
                int          rRows   = aRows * bRows;
                int          rCols   = aCols * bCols;
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] res = new double[rRows][rCols];
                double[][] res = RectangularArrays.ReturnRectangularDoubleArray(rRows, rCols);
                for (int i = 0; i < aRows; i++)
                {
                    for (int j = 0; j < aCols; j++)
                    {
                        double t = matrix1.get(i, j);
                        if (t != 0.0)
                        {
                            for (int k = 0; k < bRows; k++)
                            {
                                for (int l = 0; l < bCols; l++)
                                {
                                    res[i * bRows + k][j * bCols + l] = t * matrix2.get(k, l);
                                }
                            }
                        }
                    }
                }
                return(DoubleMatrix.ofUnsafe(res));
            }
            throw new System.ArgumentException("Can only calculate the Kronecker product of two DoubleMatrix.");
        }