/// <summary> /// Computes the solution to a real system of linear equations /// A * X = B, where A is a band matrix. /// </summary> /// <param name="A">The band matrix.</param> /// <param name="B">The vector containing the right-hand side of the linear system.</param> /// <returns>A vector containing the solution to the linear system of equations.</returns> public Vector Solve(BandMatrix A, Vector B) { Matrix myB = B; Vector solution = this.Solve(A, myB).GetColumnVector(0); return(this.Solve(A, B)); }
/// <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 BandMatrix operator *(double s, BandMatrix A) { BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, A.UpperBandWidth); double[] AData = A.Data; double[] CData = C.Data; Matrix.MultiplicationSM(s, AData, CData); return C; }
/// <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 BandMatrix operator *(double s, BandMatrix A) { BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, A.UpperBandWidth); double[] AData = A.Data; double[] CData = C.Data; Matrix.MultiplicationSM(s, AData, CData); return(C); }
/// <summary> /// Computes the solution to a real system of linear equations /// A * X = B, where A is a band matrix. /// </summary> /// <param name="A">The band 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(BandMatrix A, Matrix B) { if (this._dgbsv == null) { this._dgbsv = new DGBSV(); } this.CheckDimensions(A, B); Matrix Solution = B.Clone(); Matrix ExtendedMatrix = A.GetBandPackedMatrix(); double[] BAData = ExtendedMatrix.Data; double[] SolutionData = Solution.Data; int[] IPIV = new int[A.RowCount]; int Info = 0; this._dgbsv.Run(A.RowCount, A.LowerBandWidth, A.UpperBandWidth, B.ColumnCount, ref BAData, 0, ExtendedMatrix.RowCount, ref IPIV, 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. The factorization /// has been completed, but the factor U is exactly /// singular, and the solution has not been computed. ///</param> 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. The factorization has been completed, but the factor U is exactly singular, so the solution could not be computed."); } #endregion return(Solution); }
/// <summary>Generate a BandMatrix with random elements</summary> /// <param name="rows">Number of rows.</param> /// <param name="columns">Number of columns.</param> /// <param name="lowerBandWidth">Number of bands below the main diagonal</param> /// <param name="upperBandWidth">Number of bands above the main diagonal</param> public static BandMatrix Random(int rows, int columns, int lowerBandWidth, int upperBandWidth) { System.Random random = new System.Random(); BandMatrix X = new BandMatrix(rows, columns, lowerBandWidth, upperBandWidth); 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); }
/// <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 BandMatrix operator -(BandMatrix A, BandMatrix B) { if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount || B.LowerBandWidth != A.LowerBandWidth || B.UpperBandWidth != A.UpperBandWidth) { throw new System.ArgumentException("Matrix dimensions are not valid."); } BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, B.UpperBandWidth); 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); }
/// <summary> /// Matrix addition. /// </summary> /// <param name="A">The left side matrix of the addition operator.</param> /// <param name="B">The right side matrix of the addition operator.</param> /// <returns>A matrix that represents the result of the matrix addition.</returns> public static BandMatrix operator +(BandMatrix A, BandMatrix B) { if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount || B.LowerBandWidth != A.LowerBandWidth || B.UpperBandWidth != A.UpperBandWidth) { throw new System.ArgumentException("Matrix dimensions are not valid."); } BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, B.UpperBandWidth); 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; }
/// <summary> /// Creates a copy of the matrix. /// </summary> /// <returns>The copy of the Matrix.</returns> public BandMatrix Clone() { BandMatrix NewBandMatix = new BandMatrix(this._RowCount, this._ColumnCount, this.MeLowerBandWidth, this.MeUpperBandWidth, this._Data); return NewBandMatix; }
/// <summary>Generate a BandMatrix with random elements</summary> /// <param name="rows">Number of rows.</param> /// <param name="columns">Number of columns.</param> /// <param name="lowerBandWidth">Number of bands below the main diagonal</param> /// <param name="upperBandWidth">Number of bands above the main diagonal</param> public static BandMatrix Random(int rows, int columns, int lowerBandWidth, int upperBandWidth) { System.Random random = new System.Random(); BandMatrix X = new BandMatrix(rows, columns, lowerBandWidth, upperBandWidth); 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; }
/// <summary> /// Computes the solution to a real system of linear equations /// A * X = B, where A is a band matrix. /// </summary> /// <param name="A">The band 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(BandMatrix A, Matrix B) { if (this._dgbsv == null) this._dgbsv = new DGBSV(); this.CheckDimensions(A, B); Matrix Solution = B.Clone(); Matrix ExtendedMatrix = A.GetBandPackedMatrix(); double[] BAData = ExtendedMatrix.Data; double[] SolutionData = Solution.Data; int[] IPIV = new int[A.RowCount]; int Info = 0; this._dgbsv.Run(A.RowCount, A.LowerBandWidth, A.UpperBandWidth, B.ColumnCount, ref BAData, 0, ExtendedMatrix.RowCount, ref IPIV, 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. The factorization /// has been completed, but the factor U is exactly /// singular, and the solution has not been computed. ///</param> 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. The factorization has been completed, but the factor U is exactly singular, so the solution could not be computed."); } #endregion return Solution; }
/// <summary> /// Computes the solution to a real system of linear equations /// A * X = B, where A is a band matrix. /// </summary> /// <param name="A">The band matrix.</param> /// <param name="B">The vector containing the right-hand side of the linear system.</param> /// <returns>A vector containing the solution to the linear system of equations.</returns> public Vector Solve(BandMatrix A, Vector B) { Matrix myB = B; Vector solution = this.Solve(A, myB).GetColumnVector(0); return this.Solve(A, B); }
/// <summary> /// Creates a copy of the matrix. /// </summary> /// <returns>The copy of the Matrix.</returns> public BandMatrix Clone() { BandMatrix NewBandMatix = new BandMatrix(this._RowCount, this._ColumnCount, this.MeLowerBandWidth, this.MeUpperBandWidth, this._Data); return(NewBandMatix); }