Beispiel #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testHashCodeAndEquals()
        public virtual void testHashCodeAndEquals()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] a = java.util.Arrays.copyOf(A, A.length);
            double[] a = Arrays.copyOf(A, A.Length);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] b = java.util.Arrays.copyOf(B, B.length);
            double[] b = Arrays.copyOf(B, B.Length);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] c = java.util.Arrays.copyOf(C, C.length);
            double[]          c     = Arrays.copyOf(C, C.Length);
            TridiagonalMatrix other = new TridiagonalMatrix(a, b, c);

            assertEquals(other, M);
            assertEquals(other.GetHashCode(), M.GetHashCode());
            a[1]  = 1000;
            other = new TridiagonalMatrix(a, B, C);
            assertFalse(other.Equals(M));
            b[1]  = 1000;
            other = new TridiagonalMatrix(A, b, C);
            assertFalse(other.Equals(M));
            c[1]  = 1000;
            other = new TridiagonalMatrix(A, B, c);
            assertFalse(other.Equals(M));
        }
Beispiel #2
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }
            TridiagonalMatrix other = (TridiagonalMatrix)obj;

            if (!Arrays.Equals(_a, other._a))
            {
                return(false);
            }
            if (!Arrays.Equals(_b, other._b))
            {
                return(false);
            }
            if (!Arrays.Equals(_c, other._c))
            {
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Solves the system Ax = y for the unknown vector x, where A is a tridiagonal matrix and y is a vector.
        /// This takes order n operations where n is the size of the system
        /// (number of linear equations), as opposed to order n^3 for the general problem. </summary>
        /// <param name="aM"> tridiagonal matrix </param>
        /// <param name="b"> known vector (must be same length as rows/columns of matrix) </param>
        /// <returns> vector (as an array of doubles) with same length as y </returns>
        public static double[] solvTriDag(TridiagonalMatrix aM, double[] b)
        {
            ArgChecker.notNull(aM, "null matrix");
            ArgChecker.notNull(b, "null vector");
            double[] d = aM.Diagonal;     //b is modified, so get copy of diagonal
            int      n = d.Length;

            ArgChecker.isTrue(n == b.Length, "vector y wrong length for matrix");
            double[] y = Arrays.copyOf(b, n);

            double[] l = aM.LowerSubDiagonalData;
            double[] u = aM.UpperSubDiagonalData;

            double[] x = new double[n];
            for (int i = 1; i < n; i++)
            {
                double m = l[i - 1] / d[i - 1];
                d[i] = d[i] - m * u[i - 1];
                y[i] = y[i] - m * y[i - 1];
            }

            x[n - 1] = y[n - 1] / d[n - 1];

            for (int i = n - 2; i >= 0; i--)
            {
                x[i] = (y[i] - u[i] * x[i + 1]) / d[i];
            }

            return(x);
        }
//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()
        {
            const int n = 97;

            double[] a = new double[n - 1];
            double[] b = new double[n];
            double[] c = new double[n - 1];
            double[] x = new double[n];

            for (int ii = 0; ii < n; ii++)
            {
                b[ii] = RANDOM.nextRandom();
                x[ii] = RANDOM.nextRandom();
                if (ii < n - 1)
                {
                    a[ii] = RANDOM.nextRandom();
                    c[ii] = RANDOM.nextRandom();
                }
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TridiagonalMatrix m = new TridiagonalMatrix(b, a, c);
            TridiagonalMatrix m = new TridiagonalMatrix(b, a, c);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray xVec = com.opengamma.strata.collect.array.DoubleArray.copyOf(x);
            DoubleArray xVec = DoubleArray.copyOf(x);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray yVec = (com.opengamma.strata.collect.array.DoubleArray) MA.multiply(m, xVec);
            DoubleArray yVec = (DoubleArray)MA.multiply(m, xVec);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] xSolv = solvTriDag(m, yVec).toArray();
            double[] xSolv = solvTriDag(m, yVec).toArray();

            for (int i = 0; i < n; i++)
            {
                assertEquals(x[i], xSolv[i], 1e-9);
            }

            DoubleArray resi = (DoubleArray)MA.subtract(MA.multiply(m, DoubleArray.copyOf(xSolv)), yVec);
            double      err  = MA.getNorm2(resi);

            assertEquals(0.0, err, 1e-14);
        }
 /// <summary>
 /// Solves the system Ax = y for the unknown vector x, where A is a tridiagonal matrix and y is a vector.
 /// This takes order n operations where n is the size of the system
 /// (number of linear equations), as opposed to order n^3 for the general problem. </summary>
 /// <param name="aM"> tridiagonal matrix </param>
 /// <param name="b"> known vector (must be same length as rows/columns of matrix) </param>
 /// <returns> vector with same length as y </returns>
 public static DoubleArray solvTriDag(TridiagonalMatrix aM, DoubleArray b)
 {
     return(DoubleArray.copyOf(solvTriDag(aM, b.toArray())));
 }