#region Copyright © 2009, De Santiago-Castillo JA. All rights reserved. //Copyright © 2009 Jose Antonio De Santiago-Castillo //E-mail:JAntonioDeSantiago@gmail.com //Web: www.DotNumerics.com // #endregion using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace DotNumerics.LinearAlgebra { /// /// Represents a general Matrix. /// public sealed class Matrix : BaseMatrix { #region Public Constructors /// /// Initializes a new instance of the Matrix class of the given size. /// /// Size public Matrix(int size) : base(size) { } /// /// Initializes a new instance of the Matrix class of the given size. /// /// Number of rows. /// Number of columns. public Matrix(int rows, int columns) : base(rows, columns) { } /// /// Initializes a new instance of the Matrix class using a array. /// /// The data of the matrix. public Matrix(double[,] data) : base(data.GetLength(0), data.GetLength(1)) { for (int column = 0; column < base._ColumnCount; column++) { for (int row = 0; row < base._RowCount; row++) { this._Data[row + column * this._RowCount] = data[row, column]; } } } /// /// Initializes a new instance of the Matrix class of the given size using a array. /// /// Number of rows. /// Number of columns. /// The data, the data is copied. internal Matrix(int rows, int columns, double[] Data) : base(rows, columns, Data) { } #endregion #region Public Methods /// /// In place addition A=A+B /// /// The Matrix public void AddInplace(BaseMatrix B) { base.CheckMatrixDimensions(B); double[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i] += BData[i]; } } /// /// In place matrix subtraction, A=A-B /// /// The Matrix public void SubtractInplace(BaseMatrix B) { CheckMatrixDimensions(B); double[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i] -= BData[i]; } } /// /// Unary minus. /// /// -this public Matrix UnaryMinus() { Matrix C= new Matrix(this._RowCount, this._ColumnCount); double[] dataC=C.Data; for (int i = 0; i < this._Data.Length; i++) { dataC[i] = -this._Data[i]; } return C; } #endregion #region Overloading Operators #region Matrix-Matrix Addition /// /// Matrix addition. /// /// The left side matrix of the addition operator. /// The right side matrix of the addition operator. /// A matrix that represents the result of the matrix addition. public static Matrix operator +(Matrix A, Matrix B) { return A.Add(B); } #endregion #region Matrix-Matrix Subtraction ///// Matrix Subtraction /// /// Matrix subtraction. /// /// The left side matrix of the subtraction operator. /// The right side matrix of the subtraction operator. /// A matrix that represents the result of the matrix subtraction. public static Matrix operator -(Matrix A, Matrix B) { return A.Subtract(B); } /// /// Unary minus. /// /// The Matric. /// Matrix r[i] = -this[i] public static Matrix operator -(Matrix A) { return A.UnaryMinus(); } #endregion #region Scalar-Matrix Multiplication /// /// Scalar-Matrix multiplication. /// /// The left side scalar of the multiplication operator. /// The right side matrix of the multiplication operator. /// A matrix that represents the result of the multiplication. public static Matrix operator *(double s, Matrix A) { return A.Multiply(s); } /// /// Scalar-Matrix multiplication. /// /// The right side matrix of the multiplication operator. /// The left side scalar of the multiplication operator. /// A matrix that represents the result of the multiplication. public static Matrix operator *(Matrix A, double s) { return A.Multiply(s); } internal static void MultiplicationSM(double s, double[] A, double[] C) { for (int i = 0; i < C.Length; i++) { C[i] = s * A[i]; } } #endregion #endregion #region Public Methods //public double this[int row, int column] //{ // get // { // return this.MeData[row - 1 + (column - 1) * this.MeRowCount]; // } // set // { // this.MeData[row - 1 + (column - 1) * this.MeRowCount] = value; // } //} /// /// Creates a copy of the matrix. /// /// The copy of the Matrix. public Matrix Clone() { Matrix NewMatrix = new Matrix(this._RowCount, this._ColumnCount, this._Data); return NewMatrix; } #region Static methods /// Generate a matrix with random elements /// Number of rows. /// Number of columns. /// An m-by-n matrix with uniformly distributed /// random elements in [0, 1) interval. public static Matrix Random(int rows, int columns) { System.Random random = new System.Random(); Matrix X = new Matrix(rows, columns); double[] XData = X.Data; for (int i = 0; i < XData.Length; i++) { XData[i] = random.NextDouble(); } return X; } /// Generate a matrix with random elements /// Number of rows. /// Number of columns. /// /// A number used to calculate a starting value for the pseudo-random number /// sequence. If a negative number is specified, the absolute value of the number /// is used. /// /// /// An m-by-n matrix with uniformly distributed /// random elements in [0, 1) interval. /// public static Matrix Random(int rows, int columns, int Seed) { System.Random random = new System.Random(Seed); Matrix X = new Matrix(rows, columns); double[] XData = X.Data; for (int i = 0; i < XData.Length; i++) { XData[i] = random.NextDouble(); } return X; } #endregion #endregion } }