/// <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)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testGetters() public virtual void testGetters() { assertTrue(Arrays.Equals(A, M.DiagonalData)); assertTrue(Arrays.Equals(B, M.UpperSubDiagonalData)); assertTrue(Arrays.Equals(C, M.LowerSubDiagonalData)); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int n = A.length; int n = A.Length; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix matrix = M.toDoubleMatrix(); DoubleMatrix matrix = M.toDoubleMatrix(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { assertEquals(matrix.get(i, j), A[i], 0); } else if (j == i + 1) { assertEquals(matrix.get(i, j), B[j - 1], 0); } else if (j == i - 1) { assertEquals(matrix.get(i, j), C[j], 0); } else { assertEquals(matrix.get(i, j), 0, 0); } } } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testRecall() public virtual void testRecall() { const double chiSq = 12.46; LeastSquareResults res = new LeastSquareResults(chiSq, PARAMS, COVAR); assertEquals(chiSq, res.ChiSq, 0.0); for (int i = 0; i < 2; i++) { assertEquals(PARAMS.get(i), res.FitParameters.get(i), 0); for (int j = 0; j < 2; j++) { assertEquals(COVAR.get(i, j), res.Covariance.get(i, j), 0); } } res = new LeastSquareResults(chiSq, PARAMS, COVAR, INV_JAC); assertEquals(chiSq, res.ChiSq, 0.0); for (int i = 0; i < 2; i++) { assertEquals(PARAMS.get(i), res.FitParameters.get(i), 0); for (int j = 0; j < 2; j++) { assertEquals(COVAR.get(i, j), res.Covariance.get(i, j), 0); assertEquals(INV_JAC.get(i, j), res.FittingParameterSensitivityToData.get(i, j), 0); } } }
/// <summary> /// Gamma is in the form gamma^i_{j,k} =\partial^2y_j/\partial x_i \partial x_k, where i is the /// index of the matrix in the stack (3rd index of the tensor), and j,k are the individual /// matrix indices. We would like it in the form H^i_{j,k} =\partial^2y_i/\partial x_j \partial x_k, /// so that each matrix is a Hessian (for the dependent variable y_i), hence the reshaping below. /// </summary> /// <param name="gamma"> the rank 3 tensor </param> /// <returns> the reshaped rank 3 tensor </returns> private DoubleMatrix[] reshapeTensor(DoubleMatrix[] gamma) { int m = gamma.Length; int n = gamma[0].rowCount(); ArgChecker.isTrue(gamma[0].columnCount() == m, "tenor wrong size. Seond index is {}, should be {}", gamma[0].columnCount(), m); DoubleMatrix[] res = new DoubleMatrix[n]; for (int i = 0; i < n; i++) { //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[][] temp = new double[m][m]; double[][] temp = RectangularArrays.ReturnRectangularDoubleArray(m, m); for (int j = 0; j < m; j++) { DoubleMatrix gammaJ = gamma[j]; for (int k = j; k < m; k++) { temp[j][k] = gammaJ.get(i, k); } } for (int j = 0; j < m; j++) { for (int k = 0; k < j; k++) { temp[j][k] = temp[k][j]; } } res[i] = DoubleMatrix.copyOf(temp); } return(res); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testInvertIdentity() public virtual void testInvertIdentity() { const int n = 11; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double[] a = new double[n]; double[] a = new double[n]; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double[] b = new double[n - 1]; double[] b = new double[n - 1]; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double[] c = new double[n - 1]; double[] c = new double[n - 1]; int i, j; for (i = 0; i < n; i++) { a[i] = 1.0; } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix res = CALCULATOR.apply(new TridiagonalMatrix(a, b, c)); DoubleMatrix res = CALCULATOR.apply(new TridiagonalMatrix(a, b, c)); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { assertEquals((i == j ? 1.0 : 0.0), res.get(i, j), EPS); } } }
//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); } } } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void test() public virtual void test() { DoubleMatrix m1 = ESTIMATE.getInitializedMatrix(J, X); DoubleMatrix m2 = J.apply(X); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals(m1.get(i, j), m2.get(i, j), 1e-9); } } }
public virtual void test() { DoubleMatrix m1 = ESTIMATE.getInitializedMatrix(J, X); DoubleMatrix m2 = J.apply(X); DoubleMatrix m3 = (DoubleMatrix)(ALGEBRA.multiply(m1, m2)); DoubleMatrix identity = DoubleMatrix.identity(2); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals(m3.get(i, j), identity.get(i, j), 1e-6); } } }
/// <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); } } }
//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"); } } } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testInvert() public virtual void testInvert() { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix res = CALCULATOR.apply(MATRIX); DoubleMatrix res = CALCULATOR.apply(MATRIX); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix idet = (com.opengamma.strata.collect.array.DoubleMatrix) OG_ALGEBRA.multiply(TRI, res); DoubleMatrix idet = (DoubleMatrix)OG_ALGEBRA.multiply(TRI, res); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int n = idet.rowCount(); int n = idet.rowCount(); int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { assertEquals((i == j ? 1.0 : 0.0), idet.get(i, j), EPS); } } }
//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); } } } }
/// <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."); }