Пример #1
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);
        }