// // Math.NET Numerics, part of the Math.NET Project // http://mathnet.opensourcedotnet.info // // Copyright (c) 2009 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.Double { using System; using System.Text; using Properties; using Threading; /// /// Defines the base class for Matrix classes. /// public abstract partial class Matrix { /// /// Adds another matrix to this matrix. The result will be written into this matrix. /// /// The matrix to add to this matrix. /// If the other matrix is . /// If the two matrices don't have the same dimensions. public virtual void Add(Matrix other) { if (other == null) { throw new ArgumentNullException("other"); } if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } Parallel.For( 0, RowCount, i => { for (int j = 0; j < ColumnCount; j++) { At(i, j, At(i, j) + other.At(i, j)); } }); } /// /// Subtracts another matrix from this matrix. The result will be written into this matrix. /// /// The matrix to subtract. /// If the other matrix is . /// If the two matrices don't have the same dimensions. public virtual void Subtract(Matrix other) { if (other == null) { throw new ArgumentNullException("other"); } if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } Parallel.For( 0, RowCount, i => { for (int j = 0; j < ColumnCount; j++) { At(i, j, At(i, j) - other.At(i, j)); } }); } /// /// Multiplies each element of this matrix with a scalar. /// /// The scalar to multiply with. public virtual void Multiply(double scalar) { if (Precision.AlmostEqualInDecimalPlaces(1.0, scalar, 15)) { return; } Parallel.For( 0, RowCount, i => { for (int j = 0; j < ColumnCount; j++) { At(i, j, At(i, j) * scalar); } }); } /// /// 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 multiply. /// If the result matrix is . /// If the result matrix's dimensions are not the same as this matrix. public virtual void Multiply(double scalar, Matrix result) { if (result == null) { throw new ArgumentNullException("result"); } if (result.RowCount != RowCount) { throw new ArgumentException("result", Resources.ArgumentMatrixSameRowDimension); } if (result.ColumnCount != ColumnCount) { throw new ArgumentException("result", Resources.ArgumentMatrixSameColumnDimension); } CopyTo(result); result.Multiply(scalar); } /// /// Multiplies this matrix by a vector and returns the result. /// /// The vector to multiply with. /// The result of the multiplication. /// If rightSide is . /// If this.ColumnCount != rightSide.Count. public virtual Vector Multiply(Vector rightSide) { Vector ret = CreateVector(RowCount); Multiply(rightSide, ret); return ret; } /// /// Multiplies this matrix with a vector and places the results into the result matrix. /// /// The vector to multiply with. /// The result of the multiplication. /// If is . /// If is . /// If result.Count != this.RowCount. /// If this.ColumnCount != rightSide.Count. public virtual void Multiply(Vector rightSide, Vector result) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (ColumnCount != rightSide.Count) { throw new ArgumentException("rightSide", Resources.ArgumentMatrixDimensions); } if (result == null) { throw new ArgumentNullException("result"); } if (RowCount != result.Count) { throw new ArgumentException("result", Resources.ArgumentMatrixDimensions); } if (ReferenceEquals(rightSide, result)) { Vector tmp = result.CreateVector(result.Count); Multiply(rightSide, tmp); tmp.CopyTo(result); } else { Parallel.For( 0, RowCount, i => { double s = 0; for (int j = 0; j != ColumnCount; j++) { s += At(i, j) * rightSide[j]; } result[i] = s; }); } } /// /// Left multiply a matrix with a vector ( = vector * matrix ). /// /// The vector to multiply with. /// The result of the multiplication. /// If is . /// If this.RowCount != leftSide.Count. public virtual Vector LeftMultiply(Vector leftSide) { Vector ret = CreateVector(ColumnCount); LeftMultiply(leftSide, ret); return ret; } /// /// 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. /// If leftSide is . /// If the result matrix is . /// If result.Count != this.ColumnCount. /// If this.RowCount != leftSide.Count. public virtual void LeftMultiply(Vector leftSide, Vector result) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (RowCount != leftSide.Count) { throw new ArgumentException("leftSide", Resources.ArgumentMatrixDimensions); } if (result == null) { throw new ArgumentNullException("result"); } if (ColumnCount != result.Count) { throw new ArgumentException("result", Resources.ArgumentMatrixDimensions); } if (ReferenceEquals(leftSide, result)) { Vector tmp = result.CreateVector(result.Count); LeftMultiply(leftSide, tmp); tmp.CopyTo(result); } else { Parallel.For( 0, ColumnCount, j => { double s = 0; for (int i = 0; i != leftSide.Count; i++) { s += leftSide[i] * At(i, j); } result[j] = s; }); } } /// /// Multiplies this matrix with another matrix and places the results into the result matrix. /// /// The matrix to multiply with. /// The result of the multiplication. /// If the other matrix is . /// If the result matrix is . /// If this.Columns != other.Rows. /// If the result matrix's dimensions are not the this.Rows x other.Columns. public virtual void Multiply(Matrix other, Matrix result) { if (other == null) { throw new ArgumentNullException("other"); } if (result == null) { throw new ArgumentNullException("result"); } if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } if (result.RowCount != RowCount || result.ColumnCount != other.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { Matrix tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); Multiply(other, tmp); tmp.CopyTo(result); } else { Parallel.For( 0, this.RowCount, j => { for (int i = 0; i != other.ColumnCount; i++) { double s = 0; for (int l = 0; l < this.ColumnCount; l++) { s += this.At(j, l) * other.At(l, i); } result.At(j, i, s); } }); } } /// /// Multiplies this matrix with another matrix and returns the result. /// /// The matrix to multiply with. /// If this.Columns != other.Rows. /// If the other matrix is . /// The result of the multiplication. public virtual Matrix Multiply(Matrix other) { if (other == null) { throw new ArgumentNullException("other"); } if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } Matrix result = CreateMatrix(RowCount, other.ColumnCount); Multiply(other, result); return result; } /// /// Negates each element of this matrix. /// public virtual void Negate() { Multiply(-1); } /// /// Negate each element of this matrix and place the results into the result matrix. /// /// The result of the negation. /// If the result matrix is . /// if the result matrix's dimensions are not the same as this matrix. public virtual void Negate(Matrix result) { if (result == null) { throw new ArgumentNullException("result"); } if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } CopyTo(result); result.Negate(); } /// /// Adds two matrices together and returns the results. /// /// This operator will allocate new memory for the result. It will /// choose the representation of either or depending on which /// is denser. /// The left matrix to add. /// The right matrix to add. /// The result of the addition. /// If and don't have the same dimensions. /// If or is . public static Matrix operator +(Matrix leftSide, Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (leftSide.RowCount != rightSide.RowCount || leftSide.ColumnCount != rightSide.ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } Matrix ret = leftSide.Clone(); ret.Add(rightSide); return ret; } /// /// Returns a Matrix containing the same values of rightSide. /// /// The matrix to get the values from. /// A matrix containing a the same values as . /// If is . public static Matrix operator +(Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return rightSide.Clone(); } /// /// Subtracts two matrices together and returns the results. /// /// This operator will allocate new memory for the result. It will /// choose the representation of either or depending on which /// is denser. /// The left matrix to subtract. /// The right matrix to subtract. /// The result of the addition. /// If and don't have the same dimensions. /// If or is . public static Matrix operator -(Matrix leftSide, Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (leftSide.RowCount != rightSide.RowCount || leftSide.ColumnCount != rightSide.ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } Matrix ret = leftSide.Clone(); ret.Subtract(rightSide); return ret; } /// /// Negates each element of the matrix. /// /// The matrix to negate. /// A matrix containing the negated values. /// If is . public static Matrix operator -(Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } Matrix ret = rightSide.Clone(); ret.Negate(); return ret; } /// /// Multiplies a Matrix by a constant and returns the result. /// /// The matrix to multiply. /// The constant to multiply the matrix by. /// The result of the multiplication. /// If is . public static Matrix operator *(Matrix leftSide, double rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } Matrix ret = leftSide.Clone(); ret.Multiply(rightSide); return ret; } /// /// Multiplies a Matrix by a constant and returns the result. /// /// The matrix to multiply. /// The constant to multiply the matrix by. /// The result of the multiplication. /// If is . public static Matrix operator *(double leftSide, Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } Matrix ret = rightSide.Clone(); ret.Multiply(leftSide); return ret; } /// /// Multiplies two matrices. /// /// This operator will allocate new memory for the result. It will /// choose the representation of either or depending on which /// is denser. /// The left matrix to multiply. /// The right matrix to multiply. /// The result of multiplication. /// If or is . /// If the dimensions of or don't conform. public static Matrix operator *(Matrix leftSide, Matrix rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide.ColumnCount != rightSide.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } return leftSide.Multiply(rightSide); } /// /// Multiplies a Matrix and a . /// /// The matrix to multiply. /// The vector to multiply. /// The result of multiplication. /// If or is . public static Vector operator *(Matrix leftSide, Vector rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return leftSide.Multiply(rightSide); } /// /// Multiplies a and a Matrix. /// /// The vector to multiply. /// The matrix to multiply. /// The result of multiplication. /// If or is . public static Vector operator *(Vector leftSide, Matrix rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return rightSide.LeftMultiply(leftSide); } } }