// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // Copyright (c) 2009-2010 Math.NET // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics.LinearAlgebra.Complex { using System; using System.Numerics; using Algorithms.LinearAlgebra; using Generic; using Properties; using Threading; /// /// A Matrix class with dense storage. The underlying storage is a one dimensional array in column-major order. /// public class DenseMatrix : Matrix { /// /// Initializes a new instance of the class. This matrix is square with a given size. /// /// the size of the square matrix. /// /// If is less than one. /// public DenseMatrix(int order) : base(order) { Data = new Complex[order * order]; } /// /// Initializes a new instance of the class. /// /// /// The number of rows. /// /// /// The number of columns. /// public DenseMatrix(int rows, int columns) : base(rows, columns) { Data = new Complex[rows * columns]; } /// /// Initializes a new instance of the class with all entries set to a particular value. /// /// /// The number of rows. /// /// /// The number of columns. /// /// The value which we assign to each element of the matrix. public DenseMatrix(int rows, int columns, Complex value) : base(rows, columns) { Data = new Complex[rows * columns]; for (var i = 0; i < Data.Length; i++) { Data[i] = value; } } /// /// Initializes a new instance of the class from a one dimensional array. This constructor /// will reference the one dimensional array and not copy it. /// /// The number of rows. /// The number of columns. /// The one dimensional array to create this matrix from. This array should store the matrix in column-major order. see: http://en.wikipedia.org/wiki/Row-major_order public DenseMatrix(int rows, int columns, Complex[] array) : base(rows, columns) { Data = array; } /// /// Initializes a new instance of the class from a 2D array. This constructor /// will allocate a completely new memory block for storing the dense matrix. /// /// The 2D array to create this matrix from. public DenseMatrix(Complex[,] array) : base(array.GetLength(0), array.GetLength(1)) { var rows = array.GetLength(0); var columns = array.GetLength(1); Data = new Complex[rows * columns]; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { Data[(j * rows) + i] = array[i, j]; } } } /// /// Gets the matrix's data. /// /// The matrix's data. public Complex[] Data { get; private set; } /// /// Creates a DenseMatrix for the given number of rows and columns. /// /// /// The number of rows. /// /// /// The number of columns. /// /// /// A DenseMatrix with the given dimensions. /// public override Matrix CreateMatrix(int numberOfRows, int numberOfColumns) { return new DenseMatrix(numberOfRows, numberOfColumns); } /// /// Creates a with a the given dimension. /// /// The size of the vector. /// /// A with the given dimension. /// public override Vector CreateVector(int size) { return new DenseVector(size); } /// /// Retrieves the requested element without range checking. /// /// /// The row of the element. /// /// /// The column of the element. /// /// /// The requested element. /// public override Complex At(int row, int column) { return Data[(column * RowCount) + row]; } /// /// Sets the value of the given element. /// /// /// The row of the element. /// /// /// The column of the element. /// /// /// The value to set the element to. /// public override void At(int row, int column, Complex value) { Data[(column * RowCount) + row] = value; } /// /// Sets all values to zero. /// public override void Clear() { Array.Clear(Data, 0, Data.Length); } /// /// Returns the transpose of this matrix. /// /// The transpose of this matrix. public override Matrix Transpose() { var ret = new DenseMatrix(ColumnCount, RowCount); for (var j = 0; j < ColumnCount; j++) { var index = j * RowCount; for (var i = 0; i < RowCount; i++) { ret.Data[(i * ColumnCount) + j] = Data[index + i]; } } return ret; } /// Calculates the L1 norm. /// The L1 norm of the matrix. public override Complex L1Norm() { return Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, RowCount, ColumnCount, Data); } /// Calculates the Frobenius norm of this matrix. /// The Frobenius norm of this matrix. public override Complex FrobeniusNorm() { return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, RowCount, ColumnCount, Data); } /// Calculates the infinity norm of this matrix. /// The infinity norm of this matrix. public override Complex InfinityNorm() { return Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, RowCount, ColumnCount, Data); } #region Elementary operations /// /// Adds another matrix to this matrix. /// /// The matrix to add to this matrix. /// The matrix to store the result of add /// If the other matrix is . /// If the two matrices don't have the same dimensions. protected override void DoAdd(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoAdd(other, result); } else { Control.LinearAlgebraProvider.AddArrays(Data, denseOther.Data, denseResult.Data); } } /// /// Subtracts another matrix from this matrix. /// /// The matrix to subtract. /// The matrix to store the result of the subtraction. protected override void DoSubtract(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoSubtract(other, result); } else { Control.LinearAlgebraProvider.SubtractArrays(Data, denseOther.Data, denseResult.Data); } } #endregion #region Static constructors for special matrices. /// /// Initializes a square with all zero's except for ones on the diagonal. /// /// the size of the square matrix. /// A dense identity matrix. /// /// If is less than one. /// public static DenseMatrix Identity(int order) { var m = new DenseMatrix(order); for (var i = 0; i < order; i++) { m.Data[(i * order) + i] = 1.0; } return m; } #endregion /// /// Returns the conjugate transpose of this matrix. /// /// The conjugate transpose of this matrix. public override Matrix ConjugateTranspose() { var ret = new DenseMatrix(ColumnCount, RowCount); for (var j = 0; j < ColumnCount; j++) { var index = j * RowCount; for (var i = 0; i < RowCount; i++) { ret.Data[(i * ColumnCount) + j] = Data[index + i].Conjugate(); } } return ret; } /// /// Multiplies each element of the matrix by a scalar and places results into the result matrix. /// /// The scalar to multiply the matrix with. /// The matrix to store the result of the multiplication. protected override void DoMultiply(Complex scalar, Matrix result) { var denseResult = result as DenseMatrix; if (denseResult == null) { base.DoMultiply(scalar, result); } else { Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } } /// /// Multiplies this matrix with a vector and places the results into the result vector. /// /// The vector to multiply with. /// The result of the multiplication. protected override void DoMultiply(Vector rightSide, Vector result) { var denseRight = rightSide as DenseVector; var denseResult = result as DenseVector; if (denseRight == null || denseResult == null) { base.DoMultiply(rightSide, result); } else { Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate( Algorithms.LinearAlgebra.Transpose.DontTranspose, Algorithms.LinearAlgebra.Transpose.DontTranspose, 1.0, Data, RowCount, ColumnCount, denseRight.Data, denseRight.Count, 1, 0.0, denseResult.Data); } } /// /// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector. /// /// The vector to multiply with. /// The result of the multiplication. protected override void DoLeftMultiply(Vector leftSide, Vector result) { var denseLeft = leftSide as DenseVector; var denseResult = result as DenseVector; if (denseLeft == null || denseResult == null) { base.DoLeftMultiply(leftSide, result); } else { Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate( Algorithms.LinearAlgebra.Transpose.DontTranspose, Algorithms.LinearAlgebra.Transpose.DontTranspose, 1.0, denseLeft.Data, 1, denseLeft.Count, Data, RowCount, ColumnCount, 0.0, denseResult.Data); } } /// /// Multiplies this matrix with another matrix and places the results into the result matrix. /// /// The matrix to multiply with. /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoMultiply(other, result); } else { Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate( Algorithms.LinearAlgebra.Transpose.DontTranspose, Algorithms.LinearAlgebra.Transpose.DontTranspose, 1.0, Data, RowCount, ColumnCount, denseOther.Data, denseOther.RowCount, denseOther.ColumnCount, 0.0, denseResult.Data); } } /// /// Multiplies this matrix with transpose of another matrix and places the results into the result matrix. /// /// The matrix to multiply with. /// The result of the multiplication. protected override void DoTransposeAndMultiply(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoTransposeAndMultiply(other, result); } else { Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate( Algorithms.LinearAlgebra.Transpose.DontTranspose, Algorithms.LinearAlgebra.Transpose.Transpose, 1.0, Data, RowCount, ColumnCount, denseOther.Data, denseOther.RowCount, denseOther.ColumnCount, 0.0, denseResult.Data); } } /// /// Negate each element of this matrix and place the results into the result matrix. /// /// The result of the negation. protected override void DoNegate(Matrix result) { var denseResult = result as DenseMatrix; if (denseResult == null) { base.DoNegate(result); } else { Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); } } /// /// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix. /// /// The matrix to pointwise multiply with this one. /// The matrix to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoPointwiseMultiply(other, result); } else { Control.LinearAlgebraProvider.PointWiseMultiplyArrays(Data, denseOther.Data, denseResult.Data); } } /// /// Pointwise divide this matrix by another matrix and stores the result into the result matrix. /// /// The matrix to pointwise divide this one by. /// The matrix to store the result of the pointwise division. protected override void DoPointwiseDivide(Matrix other, Matrix result) { var denseOther = other as DenseMatrix; var denseResult = result as DenseMatrix; if (denseOther == null || denseResult == null) { base.DoPointwiseDivide(other, result); } else { Control.LinearAlgebraProvider.PointWiseDivideArrays(Data, denseOther.Data, denseResult.Data); } } /// /// Computes the trace of this matrix. /// /// The trace of this matrix /// If the matrix is not square public override Complex Trace() { if (RowCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } var sum = Complex.Zero; for (var i = 0; i < RowCount; i++) { sum += Data[(i * RowCount) + i]; } return sum; } } }