/// <summary> /// Adds two matrices. This operation can only be performed if the matrices are of the same type and dimensions. </summary> /// <param name="m1"> The first matrix, not null </param> /// <param name="m2"> The second matrix, not null </param> /// <returns> The sum of the two matrices </returns> /// <exception cref="IllegalArgumentException"> If the matrices are not of the same type, if the matrices are not the same shape. </exception> public virtual Matrix add(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); if (m1 is DoubleArray) { if (m2 is DoubleArray) { DoubleArray array1 = (DoubleArray)m1; DoubleArray array2 = (DoubleArray)m2; return(array1.plus(array2)); } throw new System.ArgumentException("Tried to add a " + m1.GetType() + " and " + m2.GetType()); } else if (m1 is DoubleMatrix) { if (m2 is DoubleMatrix) { DoubleMatrix matrix1 = (DoubleMatrix)m1; DoubleMatrix matrix2 = (DoubleMatrix)m2; return(matrix1.plus(matrix2)); } throw new System.ArgumentException("Tried to add a " + m1.GetType() + " and " + m2.GetType()); } throw new System.NotSupportedException(); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testKroneckerProduct() public virtual void testKroneckerProduct() { Matrix m = ALGEBRA.kroneckerProduct(M3, M4); assertTrue(m is DoubleMatrix); assertMatrixEquals(m, DoubleMatrix.of(4, 4, 5, 6, 10, 12, 7, 8, 14, 16, 15, 18, 20, 24, 21, 24, 28, 32)); }
/// <summary> /// Returns the quotient of two matrices $C = \frac{A}{B} = AB^{-1}$, where /// $B^{-1}$ is the pseudo-inverse of $B$ i.e. $BB^{-1} = \mathbb{1}$. </summary> /// <param name="m1"> The numerator matrix, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param> /// <param name="m2"> The denominator, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param> /// <returns> The result </returns> public virtual Matrix divide(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); ArgChecker.isTrue(m1 is DoubleMatrix, "Can only divide a 2D matrix"); ArgChecker.isTrue(m2 is DoubleMatrix, "Can only perform division with a 2D matrix"); return(multiply(m1, getInverse(m2))); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testAdd() public virtual void testAdd() { Matrix m = ALGEBRA.add(M1, M2); assertTrue(m is DoubleArray); assertMatrixEquals(m, DoubleArray.of(4, 6)); m = ALGEBRA.add(M3, M4); assertTrue(m is DoubleMatrix); assertMatrixEquals(m, DoubleMatrix.of(2, 2, 6d, 8d, 10d, 12d)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testSubtract() public virtual void testSubtract() { Matrix m = ALGEBRA.subtract(M1, M2); assertTrue(m is DoubleArray); assertMatrixEquals(m, DoubleArray.of(-2, -2)); m = ALGEBRA.subtract(M3, M4); assertTrue(m is DoubleMatrix); assertMatrixEquals(m, DoubleMatrix.of(2, 2, -4d, -4d, -4d, -4d)); }
/// <summary> /// Scale a vector or matrix by a given amount, i.e. each element is multiplied by the scale. </summary> /// <param name="m"> A vector or matrix, not null </param> /// <param name="scale"> The scale </param> /// <returns> the scaled vector or matrix </returns> public virtual Matrix scale(Matrix m, double scale) { ArgChecker.notNull(m, "m"); if (m is DoubleArray) { return(((DoubleArray)m).multipliedBy(scale)); } else if (m is DoubleMatrix) { return(((DoubleMatrix)m).multipliedBy(scale)); } throw new System.NotSupportedException(); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testScale() public virtual void testScale() { Matrix m = ALGEBRA.scale(M1, 10); assertTrue(m is DoubleArray); assertMatrixEquals(m, DoubleArray.of(10, 20)); m = ALGEBRA.scale(m, 0.1); assertMatrixEquals(m, M1); m = ALGEBRA.scale(M3, 10); assertTrue(m is DoubleMatrix); assertMatrixEquals(m, DoubleMatrix.of(2, 2, 10d, 20d, 30d, 40d)); m = ALGEBRA.scale(m, 0.1); assertMatrixEquals(m, M3); }
public virtual DoubleMatrix getUpdatedMatrix(System.Func <DoubleArray, DoubleMatrix> j, DoubleArray x, DoubleArray deltaX, DoubleArray deltaY, DoubleMatrix matrix) { ArgChecker.notNull(deltaX, "deltaX"); ArgChecker.notNull(deltaY, "deltaY"); ArgChecker.notNull(matrix, "matrix"); double length2 = OG_ALGEBRA.getInnerProduct(deltaX, deltaX); if (length2 == 0.0) { return(matrix); } Matrix temp = OG_ALGEBRA.subtract(deltaY, OG_ALGEBRA.multiply(matrix, deltaX)); temp = OG_ALGEBRA.scale(temp, 1.0 / length2); return((DoubleMatrix)OG_ALGEBRA.add(matrix, OG_ALGEBRA.getOuterProduct(temp, deltaX))); }
//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."); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getNorm2(final com.opengamma.strata.collect.array.Matrix m) public override double getNorm2(Matrix m) { return(0); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public com.opengamma.strata.collect.array.DoubleMatrix getInverse(final com.opengamma.strata.collect.array.Matrix m) public override DoubleMatrix getInverse(Matrix m) { return(null); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getInnerProduct(final com.opengamma.strata.collect.array.Matrix m1, final com.opengamma.strata.collect.array.Matrix m2) public override double getInnerProduct(Matrix m1, Matrix m2) { return(0); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getDeterminant(final com.opengamma.strata.collect.array.Matrix m) public override double getDeterminant(Matrix m) { return(0); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getCondition(final com.opengamma.strata.collect.array.Matrix m) public override double getCondition(Matrix m) { return(0); }
/// <summary> /// Returns a matrix raised to an integer power, e.g. $\mathbf{A}^3 = \mathbf{A}\mathbf{A}\mathbf{A}$. </summary> /// <param name="m"> A square matrix, not null </param> /// <param name="p"> An integer power </param> /// <returns> The result </returns> public abstract DoubleMatrix getPower(Matrix m, int p);
/// <summary> /// Returns the transpose of a matrix. </summary> /// <param name="m"> A matrix, not null </param> /// <returns> The transpose matrix </returns> public abstract DoubleMatrix getTranspose(Matrix m);
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getTrace(final com.opengamma.strata.collect.array.Matrix m) public override double getTrace(Matrix m) { return(0); }
/// <summary> /// Returns the inverse (or pseudo-inverse) of the matrix. </summary> /// <param name="m"> A matrix, not null </param> /// <returns> The inverse matrix </returns> public abstract DoubleMatrix getInverse(Matrix m);
/// <summary> /// Returns the outer product. </summary> /// <param name="m1"> A vector, not null </param> /// <param name="m2"> A vector, not null </param> /// <returns> The outer product </returns> /// <exception cref="IllegalArgumentException"> If the vectors are not the same size </exception> public abstract DoubleMatrix getOuterProduct(Matrix m1, Matrix m2);
/// <summary> /// Returns a matrix raised to a power, $\mathbf{A}^3 = \mathbf{A}\mathbf{A}\mathbf{A}$. </summary> /// <param name="m"> A square matrix, not null </param> /// <param name="p"> The power </param> /// <returns> The result </returns> public abstract DoubleMatrix getPower(Matrix m, double p);
/// <summary> /// For a vector, returns <a href="http://mathworld.wolfram.com/L2-Norm.html">$L_2$ norm</a> (also known as the /// Euclidean norm). /// <para> /// For a matrix, returns the <a href="http://mathworld.wolfram.com/SpectralNorm.html">spectral norm</a> /// </para> /// </summary> /// <param name="m"> A vector or matrix, not null </param> /// <returns> the norm </returns> public abstract double getNorm2(Matrix m);
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double getNormInfinity(final com.opengamma.strata.collect.array.Matrix m) public override double getNormInfinity(Matrix m) { return(0); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public com.opengamma.strata.collect.array.Matrix multiply(final com.opengamma.strata.collect.array.Matrix m1, final com.opengamma.strata.collect.array.Matrix m2) public override Matrix multiply(Matrix m1, Matrix m2) { return(null); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public com.opengamma.strata.collect.array.DoubleMatrix getOuterProduct(final com.opengamma.strata.collect.array.Matrix m1, final com.opengamma.strata.collect.array.Matrix m2) public override DoubleMatrix getOuterProduct(Matrix m1, Matrix m2) { return(null); }
/// <summary> /// Returns the trace (i.e. sum of diagonal elements) of a matrix. </summary> /// <param name="m"> A matrix, not null. The matrix must be square. </param> /// <returns> The trace </returns> public abstract double getTrace(Matrix m);
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public com.opengamma.strata.collect.array.DoubleMatrix getTranspose(final com.opengamma.strata.collect.array.Matrix m) public override DoubleMatrix getTranspose(Matrix m) { return(null); }
/// <summary> /// Returns the inner (or dot) product. </summary> /// <param name="m1"> A vector, not null </param> /// <param name="m2"> A vector, not null </param> /// <returns> The scalar dot product </returns> /// <exception cref="IllegalArgumentException"> If the vectors are not the same size </exception> public abstract double getInnerProduct(Matrix m1, Matrix m2);
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public com.opengamma.strata.collect.array.DoubleMatrix getPower(final com.opengamma.strata.collect.array.Matrix m, final double p) public override DoubleMatrix getPower(Matrix m, double p) { return(null); }
/// <summary> /// For a vector, returns the <a href="http://mathworld.wolfram.com/L-Infinity-Norm.html">$L_\infty$ norm</a>. /// $L_\infty$ norm is the maximum of the absolute values of the elements. /// <para> /// For a matrix, returns the <a href="http://mathworld.wolfram.com/MaximumAbsoluteRowSumNorm.html">maximum absolute row sum norm</a> /// </para> /// </summary> /// <param name="m"> a vector or a matrix, not null </param> /// <returns> the norm </returns> public abstract double getNormInfinity(Matrix m);