コード例 #1
0
        /// <summary>
        /// Multiplies a tri-diagonal matrix by a real constant.
        /// </summary>
        /// <param name="f">The constant.</param>
        /// <param name="T">The matrix.</param>
        /// <returns>The product matrix.</returns>
        public static TridiagonalMatrix operator *(double f, TridiagonalMatrix T)
        {
            if (T == null)
            {
                throw new ArgumentNullException(nameof(T));
            }

            int n = T.Dimension;
            TridiagonalMatrix fT = new TridiagonalMatrix(n);

            fT.SetDiagonalElement(0, f * T.GetDiagonalElement(0));

            int i0 = 0;
            int i1 = 1;

            while (i1 < n)
            {
                fT.SetDiagonalElement(i1, f * T.GetDiagonalElement(i1));
                fT.SetSubdiagonalElement(i0, f * T.GetSubdiagonalElement(i0));
                fT.SetSuperdiagonalElement(i0, f * T.GetSuperdiagonalElement(i0));
                i0 = i1;
                i1++;
            }

            return(fT);
        }
コード例 #2
0
 public TridiagonalMatrix CreateRandomTridiagonalMatrix(int n, Random rng)
 {
     TridiagonalMatrix T = new TridiagonalMatrix(n);
     for (int i = 0; i < (n-1); i++) {
         T[i, i] = 2.0 * rng.NextDouble() - 1.0;
         T[i + 1, i] = 2.0 * rng.NextDouble() - 1.0;
         T[i, i + 1] = 2.0 * rng.NextDouble() - 1.0;
     }
     T[n - 1, n - 1] = 2.0 * rng.NextDouble() - 1.0;
     return (T);
 }
コード例 #3
0
        /// <summary>
        /// Creates a transpose of the matrix.
        /// </summary>
        /// <returns>The matrix transpose M<sup>T</sup>.</returns>
        public TridiagonalMatrix Transpose()
        {
            TridiagonalMatrix T = new TridiagonalMatrix(dimension);

            for (int i = 0; i < (dimension - 1); i++)
            {
                T.SetDiagonalElement(i, GetDiagonalElement(i));
                T.SetSubdiagonalElement(i, GetSuperdiagonalElement(i));
                T.SetSuperdiagonalElement(i, GetSubdiagonalElement(i));
            }
            T.SetDiagonalElement(dimension - 1, GetDiagonalElement(dimension - 1));
            return(T);
        }
コード例 #4
0
        /// <summary>
        /// Copies the matrix.
        /// </summary>
        /// <returns>An independent copy of the matrix.</returns>
        public TridiagonalMatrix Copy()
        {
            TridiagonalMatrix C = new TridiagonalMatrix(dimension);

            for (int i = 0; i < (dimension - 1); i++)
            {
                C.SetDiagonalElement(i, GetDiagonalElement(i));
                C.SetSubdiagonalElement(i, GetSubdiagonalElement(i));
                C.SetSuperdiagonalElement(i, GetSuperdiagonalElement(i));
            }
            C.SetDiagonalElement(dimension - 1, GetDiagonalElement(dimension - 1));
            return(C);
        }
コード例 #5
0
        /// <summary>
        /// Subtracts two tri-diagonal matrices.
        /// </summary>
        /// <param name="A">The first matrix.</param>
        /// <param name="B">The second matrix.</param>
        /// <returns>The difference matrix A - B.</returns>
        public static TridiagonalMatrix operator -(TridiagonalMatrix A, TridiagonalMatrix B)
        {
            if (A == null)
            {
                throw new ArgumentNullException(nameof(A));
            }
            if (B == null)
            {
                throw new ArgumentNullException(nameof(B));
            }

            int n = A.Dimension;

            if (B.Dimension != n)
            {
                throw new DimensionMismatchException();
            }

            TridiagonalMatrix T = new TridiagonalMatrix(n);

            // add the elements
            // this is a glorified for-next loop structured so as to minimize the
            // integer arithmetic required to keep track of two numbers one unit apart
            // and avoid if-tests inside the loop
            T.SetDiagonalElement(0, A.GetDiagonalElement(0) - B.GetDiagonalElement(0));
            int i0 = 0;
            int i1 = 1;

            while (i1 < n)
            {
                T.SetDiagonalElement(i1, A.GetDiagonalElement(i1) - B.GetDiagonalElement(i1));
                T.SetSubdiagonalElement(i0, A.GetSubdiagonalElement(i0) - B.GetSubdiagonalElement(i0));
                T.SetSuperdiagonalElement(i0, A.GetSuperdiagonalElement(i0) - B.GetSuperdiagonalElement(i0));
                i0 = i1;
                i1++;
            }

            return(T);
        }
コード例 #6
0
 public void TridiagonalMatrixInvalidSetTest()
 {
     TridiagonalMatrix T = new TridiagonalMatrix(4);
     T[0, 3] = 1.0;
 }
コード例 #7
0
 /// <summary>
 /// Creates a transpose of the matrix.
 /// </summary>
 /// <returns>The matrix transpose M<sup>T</sup>.</returns>
 public TridiagonalMatrix Transpose()
 {
     TridiagonalMatrix T = new TridiagonalMatrix(dimension);
     for (int i = 0; i < (dimension - 1); i++) {
         T.SetDiagonalElement(i, GetDiagonalElement(i));
         T.SetSubdiagonalElement(i, GetSuperdiagonalElement(i));
         T.SetSuperdiagonalElement(i, GetSubdiagonalElement(i));
     }
     T.SetDiagonalElement(dimension - 1, GetDiagonalElement(dimension - 1));
     return (T);
 }
コード例 #8
0
 /// <summary>
 /// Copies the matrix.
 /// </summary>
 /// <returns>An independent copy of the matrix.</returns>
 public TridiagonalMatrix Copy()
 {
     TridiagonalMatrix C = new TridiagonalMatrix(dimension);
     for (int i = 0; i < (dimension-1); i++) {
         C.SetDiagonalElement(i, GetDiagonalElement(i));
         C.SetSubdiagonalElement(i, GetSubdiagonalElement(i));
         C.SetSuperdiagonalElement(i, GetSuperdiagonalElement(i));
     }
     C.SetDiagonalElement(dimension - 1, GetDiagonalElement(dimension - 1));
     return (C);
 }
コード例 #9
0
        /// <summary>
        /// Subtracts two tridiagonal matrices.
        /// </summary>
        /// <param name="T1">The first matrix M<sub>1</sub>.</param>
        /// <param name="T2">The first matrix M<sub>2</sub>.</param>
        /// <returns>The difference M<sub>1</sub> - M<sub>2</sub>.</returns>
        public static TridiagonalMatrix operator -(TridiagonalMatrix T1, TridiagonalMatrix T2)
        {
            if (T1 == null) throw new ArgumentNullException("T1");
            if (T2 == null) throw new ArgumentNullException("T1");

            int n = T1.Dimension;
            if (T2.Dimension != n) throw new DimensionMismatchException();

            TridiagonalMatrix T = new TridiagonalMatrix(n);

            // add the elements
            // this is a glorified for-next loop structured so as to minimize the
            // integer arithmetic required to keep track of two numbers one unit apart
            // and avoid if-tests inside the loop
            T.SetDiagonalElement(0, T1.GetDiagonalElement(0) - T2.GetDiagonalElement(0));
            int i0 = 0;
            int i1 = 1;
            while (i1 < n) {
                T.SetDiagonalElement(i1, T1.GetDiagonalElement(i1) - T2.GetDiagonalElement(i1));
                T.SetSubdiagonalElement(i0, T1.GetSubdiagonalElement(i0) - T2.GetSubdiagonalElement(i0));
                T.SetSuperdiagonalElement(i0, T1.GetSuperdiagonalElement(i0) - T2.GetSuperdiagonalElement(i0));
                i0 = i1;
                i1++;
            }

            return (T);
        }
コード例 #10
0
        /// <summary>
        /// Multiplies a tridiagonal matrix by a real constant.
        /// </summary>
        /// <param name="f">The constant.</param>
        /// <param name="T">The matrix.</param>
        /// <returns>The product matrix.</returns>
        public static TridiagonalMatrix operator *(double f, TridiagonalMatrix T)
        {
            if (T == null) throw new ArgumentNullException("T");

            int n = T.Dimension;
            TridiagonalMatrix fT = new TridiagonalMatrix(n);

            fT.SetDiagonalElement(0, f * T.GetDiagonalElement(0));

            int i0 = 0;
            int i1 = 1;
            while (i1 < n) {
                fT.SetDiagonalElement(i1, f * T.GetDiagonalElement(i1));
                fT.SetSubdiagonalElement(i0, f * T.GetSubdiagonalElement(i0));
                fT.SetSuperdiagonalElement(i0, f * T.GetSuperdiagonalElement(i0));
                i0 = i1;
                i1++;
            }

            return (fT);
        }