예제 #1
0
        /// <summary>
        /// Scalar-Matrix multiplication.
        /// </summary>
        /// <param name="s"> The left side scalar of the multiplication operator.</param>
        /// <param name="A">The right side matrix of the multiplication operator.</param>
        /// <returns>A matrix that represents the result of the multiplication.</returns>
        public static TridiagonalMatrix operator *(double s, TridiagonalMatrix A)
        {
            TridiagonalMatrix C = new TridiagonalMatrix(A.RowCount);

            double[] AData = A.Data;
            double[] CData = C.Data;


            Matrix.MultiplicationSM(s, AData, CData);

            return(C);
        }
예제 #2
0
        /// <summary>Generate a TridiagonalMatrix with random elements</summary>
        /// <param name="size">Size</param>
        public static TridiagonalMatrix Random(int size)
        {
            System.Random random = new System.Random();

            TridiagonalMatrix X = new TridiagonalMatrix(size);

            double[] XData = X.Data;

            for (int j = 0; j < X.ColumnCount; j++)
            {
                for (int i = 0; i < X.RowCount; i++)
                {
                    X[i, j] = random.NextDouble();
                }
            }
            return(X);
        }
예제 #3
0
        /// <summary>
        /// Computes the solution to a real system of linear equations
        /// A * X = B, where A is a tridiagonal matrix.
        /// </summary>
        /// <param name="A">The tridiagonal matrix.</param>
        /// <param name="B">The matrix containing the right-hand side of the linear system.</param>
        /// <returns>A matrix containing the solution to the linear system of equations.</returns>
        public Matrix Solve(TridiagonalMatrix A, Matrix B)
        {
            if (this._dgtsv == null)
            {
                this._dgtsv = new DGTSV();
            }

            this.CheckDimensions(A, B);

            Matrix Solution = B.Clone();

            double[] SolutionData = Solution.Data;
            double[] Diagonal;
            double[] SubDiagonal;
            double[] SuperDiagonal;
            A.GetPackedMatrix(out SubDiagonal, out SuperDiagonal, out Diagonal);

            int[] IPIV = new int[A.RowCount];
            int   Info = 0;

            this._dgtsv.Run(A.RowCount, B.ColumnCount, ref SubDiagonal, 0, ref Diagonal, 0, ref SuperDiagonal, 0, ref SolutionData, 0, Solution.RowCount, ref Info);


            #region Error
            /// = 0: successful exit
            /// .LT. 0: if INFO = -i, the i-th argument had an illegal value
            /// .GT. 0: if INFO = i, U(i,i) is exactly zero, and the solution
            /// has not been computed.  The factorization has not been
            /// completed unless i = N.
            if (Info < 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
            }
            else if (Info > 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new Exception("U(" + infoSTg + "," + infoSTg + ") is exactly zero.  and the solution has not been computed.  The factorization has not been completed unless i = N.");
            }

            #endregion

            return(Solution);
        }
예제 #4
0
        /// <summary>
        /// Matrix subtraction.
        /// </summary>
        /// <param name="A"> The left side matrix of the subtraction operator.</param>
        /// <param name="B">The right side matrix of the subtraction operator.</param>
        /// <returns>A matrix that represents the result of the matrix subtraction.</returns>
        public static TridiagonalMatrix operator -(TridiagonalMatrix A, TridiagonalMatrix B)
        {
            if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            TridiagonalMatrix C = new TridiagonalMatrix(A.RowCount);

            double[] AData = A.Data;
            double[] BData = B.Data;
            double[] CData = C.Data;

            for (int i = 0; i < AData.Length; i++)
            {
                CData[i] = AData[i] - BData[i];
            }

            return(C);
        }
예제 #5
0
        /// <summary>
        /// Scalar-Matrix multiplication.
        /// </summary>
        /// <param name="s"> The left side scalar of the multiplication operator.</param>
        /// <param name="A">The right side matrix of the multiplication operator.</param>
        /// <returns>A matrix that represents the result of the multiplication.</returns>
        public static TridiagonalMatrix operator *(double s, TridiagonalMatrix A)
        {
            TridiagonalMatrix C = new TridiagonalMatrix(A.RowCount);

            double[] AData = A.Data;
            double[] CData = C.Data;

            Matrix.MultiplicationSM(s, AData, CData);

            return C;
        }
예제 #6
0
 public TridiagonalMatrix Clone()
 {
     TridiagonalMatrix NewMatrix = new TridiagonalMatrix(this._RowCount, this._Data);
     return NewMatrix;
 }
예제 #7
0
        /// <summary>Generate a TridiagonalMatrix with random elements</summary>
        /// <param name="size">Size</param>
        public static TridiagonalMatrix Random(int size)
        {
            System.Random random = new System.Random();

            TridiagonalMatrix X = new TridiagonalMatrix(size);

            double[] XData = X.Data;

            for (int j = 0; j < X.ColumnCount; j++)
            {
                for (int i = 0; i < X.RowCount; i++)
                {
                    X[i, j] = random.NextDouble();
                }
            }
            return X;
        }
예제 #8
0
        /// <summary>
        /// Matrix subtraction.
        /// </summary>
        /// <param name="A"> The left side matrix of the subtraction operator.</param>
        /// <param name="B">The right side matrix of the subtraction operator.</param>
        /// <returns>A matrix that represents the result of the matrix subtraction.</returns>
        public static TridiagonalMatrix operator -(TridiagonalMatrix A, TridiagonalMatrix B)
        {
            if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            TridiagonalMatrix C = new TridiagonalMatrix(A.RowCount);

            double[] AData = A.Data;
            double[] BData = B.Data;
            double[] CData = C.Data;

            for (int i = 0; i < AData.Length; i++)
            {
                CData[i] = AData[i] - BData[i];
            }

            return C;
        }
예제 #9
0
        /// <summary>
        /// Computes the solution to a real system of linear equations
        /// A * X = B, where A is a tridiagonal matrix.
        /// </summary>
        /// <param name="A">The tridiagonal matrix.</param>
        /// <param name="B">The matrix containing the right-hand side of the linear system.</param>
        /// <returns>A matrix containing the solution to the linear system of equations.</returns>
        public Matrix Solve(TridiagonalMatrix A, Matrix B)
        {
            if (this._dgtsv == null) this._dgtsv = new DGTSV();

            this.CheckDimensions(A, B);

            Matrix Solution = B.Clone();
            double[] SolutionData = Solution.Data;
            double[] Diagonal;
            double[] SubDiagonal;
            double[] SuperDiagonal;
            A.GetPackedMatrix(out SubDiagonal, out SuperDiagonal, out Diagonal);

            int[] IPIV = new int[A.RowCount];
            int Info = 0;

            this._dgtsv.Run(A.RowCount, B.ColumnCount, ref SubDiagonal, 0, ref Diagonal, 0, ref SuperDiagonal, 0, ref SolutionData, 0, Solution.RowCount, ref Info);

            #region Error
            /// = 0: successful exit
            /// .LT. 0: if INFO = -i, the i-th argument had an illegal value
            /// .GT. 0: if INFO = i, U(i,i) is exactly zero, and the solution
            /// has not been computed.  The factorization has not been
            /// completed unless i = N.
            if (Info < 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
            }
            else if (Info > 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new Exception("U(" + infoSTg + "," + infoSTg + ") is exactly zero.  and the solution has not been computed.  The factorization has not been completed unless i = N.");
            }

            #endregion

            return Solution;
        }
예제 #10
0
        public TridiagonalMatrix Clone()
        {
            TridiagonalMatrix NewMatrix = new TridiagonalMatrix(this._RowCount, this._Data);

            return(NewMatrix);
        }