diff --git a/src/MathNet.Numerics.5.1.ReSharper b/src/MathNet.Numerics.5.1.ReSharper index eacc79ba..529d0949 100644 --- a/src/MathNet.Numerics.5.1.ReSharper +++ b/src/MathNet.Numerics.5.1.ReSharper @@ -78,7 +78,8 @@ Ulps Matlab &lt &gt -Wikipedia +Wikipedia +kronecker diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index f240e1d5..68d8ab49 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -541,5 +541,215 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return sum; } + + /// + /// 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 DenseMatrix operator +(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static DenseMatrix operator +(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)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 DenseMatrix operator -(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static DenseMatrix operator -(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Negate(); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, Complex rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 DenseMatrix operator *(Complex leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, DenseMatrix 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 (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseMatrix leftSide, DenseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseVector leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 DenseMatrix operator %(DenseMatrix leftSide, Complex rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 34467dc8..825b2877 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -888,7 +888,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public override Complex FrobeniusNorm() { var transpose = (SparseMatrix)Transpose(); - var aat = (SparseMatrix)(this * transpose); + var aat = this * transpose; var norm = 0.0; @@ -1568,6 +1568,216 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return true; } + + /// + /// 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 SparseMatrix operator +(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static SparseMatrix operator +(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)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 SparseMatrix operator -(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static SparseMatrix operator -(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Negate(); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, Complex rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 SparseMatrix operator *(Complex leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, SparseMatrix 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 (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseMatrix leftSide, SparseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseVector leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 SparseMatrix operator %(SparseMatrix leftSide, Complex rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Modulus(rightSide); + } } -} + } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 380c8fb5..1b15088f 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -421,14 +421,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(SparseVector rightSide) + public static SparseVector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (SparseVector)rightSide.Plus(); } /// @@ -439,7 +439,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -456,7 +456,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (SparseVector)leftSide.Add(rightSide); } /// @@ -502,14 +502,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(SparseVector rightSide) + public static SparseVector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (SparseVector)rightSide.Negate(); } /// @@ -520,7 +520,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -537,7 +537,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (SparseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index bd948a02..f2d28138 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -520,7 +520,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return ret; } - + /// /// Computes the trace of this matrix. /// @@ -541,5 +541,215 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return sum; } + + /// + /// 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 DenseMatrix operator +(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static DenseMatrix operator +(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)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 DenseMatrix operator -(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static DenseMatrix operator -(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Negate(); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, Complex32 rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 DenseMatrix operator *(Complex32 leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, DenseMatrix 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 (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseMatrix leftSide, DenseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseVector leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 DenseMatrix operator %(DenseMatrix leftSide, Complex32 rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 7e506585..f80ea30c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -291,14 +291,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(DenseVector rightSide) + public static DenseVector operator +(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (DenseVector)rightSide.Plus(); } /// @@ -309,7 +309,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator +(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -326,7 +326,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (DenseVector)leftSide.Add(rightSide); } /// @@ -375,14 +375,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(DenseVector rightSide) + public static DenseVector operator -(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (DenseVector)rightSide.Negate(); } /// @@ -393,7 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator -(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -410,7 +410,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (DenseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index d776b26b..1e6d03c6 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -888,7 +888,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public override Complex32 FrobeniusNorm() { var transpose = (SparseMatrix)Transpose(); - var aat = (SparseMatrix)(this * transpose); + var aat = this * transpose; var norm = 0.0f; @@ -1569,5 +1569,215 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return true; } + + /// + /// 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 SparseMatrix operator +(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static SparseMatrix operator +(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)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 SparseMatrix operator -(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static SparseMatrix operator -(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Negate(); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, Complex32 rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 SparseMatrix operator *(Complex32 leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, SparseMatrix 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 (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseMatrix leftSide, SparseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseVector leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 SparseMatrix operator %(SparseMatrix leftSide, Complex32 rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index a5ede8b9..c0881658 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -451,14 +451,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(SparseVector rightSide) + public static SparseVector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (SparseVector)rightSide.Plus(); } /// @@ -469,7 +469,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -486,7 +486,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (SparseVector)leftSide.Add(rightSide); } /// @@ -532,14 +532,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(SparseVector rightSide) + public static SparseVector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (SparseVector)rightSide.Negate(); } /// @@ -550,7 +550,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -567,7 +567,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (SparseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 800cec08..6073a8d5 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -302,7 +302,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// /// Multiplies each element of the matrix by a scalar and places results into the result matrix. /// @@ -559,5 +558,215 @@ namespace MathNet.Numerics.LinearAlgebra.Double return sum; } + + /// + /// 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 DenseMatrix operator +(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static DenseMatrix operator +(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)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 DenseMatrix operator -(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static DenseMatrix operator -(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Negate(); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, double rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 DenseMatrix operator *(double leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, DenseMatrix 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 (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseMatrix leftSide, DenseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseVector leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 DenseMatrix operator %(DenseMatrix leftSide, double rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 5550b5aa..8ebb3c0a 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -342,14 +342,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(DenseVector rightSide) + public static DenseVector operator +(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (DenseVector)rightSide.Plus(); } /// @@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator +(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -377,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (DenseVector)leftSide.Add(rightSide); } /// @@ -426,14 +426,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(DenseVector rightSide) + public static DenseVector operator -(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (DenseVector)rightSide.Negate(); } /// @@ -444,7 +444,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator -(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -461,7 +461,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (DenseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 8d197d7c..43a10b57 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -899,7 +899,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double FrobeniusNorm() { var transpose = (SparseMatrix)Transpose(); - var aat = (SparseMatrix)(this * transpose); + var aat = this * transpose; var norm = 0.0; @@ -1607,5 +1607,215 @@ namespace MathNet.Numerics.LinearAlgebra.Double return true; } + + /// + /// 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 SparseMatrix operator +(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static SparseMatrix operator +(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)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 SparseMatrix operator -(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static SparseMatrix operator -(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Negate(); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, double rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 SparseMatrix operator *(double leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, SparseMatrix 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 (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseMatrix leftSide, SparseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseVector leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 SparseMatrix operator %(SparseMatrix leftSide, double rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index a279ee23..d0b4770a 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -369,14 +369,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(SparseVector rightSide) + public static SparseVector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (SparseVector)rightSide.Plus(); } /// @@ -387,7 +387,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -404,7 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (SparseVector)leftSide.Add(rightSide); } /// @@ -450,14 +450,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(SparseVector rightSide) + public static SparseVector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (SparseVector)rightSide.Negate(); } /// @@ -468,7 +468,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -485,7 +485,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (SparseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 3a6ca20b..b833b8ad 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -558,5 +558,215 @@ namespace MathNet.Numerics.LinearAlgebra.Single return sum; } + + /// + /// 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 DenseMatrix operator +(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static DenseMatrix operator +(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)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 DenseMatrix operator -(DenseMatrix leftSide, DenseMatrix 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); + } + + return (DenseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static DenseMatrix operator -(DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Negate(); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, float rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 DenseMatrix operator *(float leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 DenseMatrix operator *(DenseMatrix leftSide, DenseMatrix 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 (DenseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseMatrix leftSide, DenseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static DenseVector operator *(DenseVector leftSide, DenseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (DenseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 DenseMatrix operator %(DenseMatrix leftSide, float rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (DenseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 3dc950c6..73a78965 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -342,14 +342,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(DenseVector rightSide) + public static DenseVector operator +(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (DenseVector)rightSide.Plus(); } /// @@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator +(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -377,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (DenseVector)leftSide.Add(rightSide); } /// @@ -426,14 +426,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(DenseVector rightSide) + public static DenseVector operator -(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (DenseVector)rightSide.Negate(); } /// @@ -444,7 +444,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(DenseVector leftSide, DenseVector rightSide) + public static DenseVector operator -(DenseVector leftSide, DenseVector rightSide) { if (rightSide == null) { @@ -461,7 +461,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (DenseVector)leftSide.Subtract(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 9fa5c8d0..f2efa00c 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -882,7 +882,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public override float FrobeniusNorm() { var transpose = (SparseMatrix)Transpose(); - var aat = (SparseMatrix)(this * transpose); + var aat = this * transpose; var norm = 0.0f; @@ -1590,5 +1590,215 @@ namespace MathNet.Numerics.LinearAlgebra.Single return true; } + + /// + /// 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 SparseMatrix operator +(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Add(rightSide); + } + + /// + /// Returns a Matrix containing the same values of . + /// + /// The matrix to get the values from. + /// A matrix containing a the same values as . + /// If is . + public static SparseMatrix operator +(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)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 SparseMatrix operator -(SparseMatrix leftSide, SparseMatrix 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); + } + + return (SparseMatrix)leftSide.Subtract(rightSide); + } + + /// + /// Negates each element of the matrix. + /// + /// The matrix to negate. + /// A matrix containing the negated values. + /// If is . + public static SparseMatrix operator -(SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Negate(); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, float rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// 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 SparseMatrix operator *(float leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseMatrix)rightSide.Multiply(leftSide); + } + + /// + /// 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 SparseMatrix operator *(SparseMatrix leftSide, SparseMatrix 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 (SparseMatrix)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Matrix and a Vector. + /// + /// The matrix to multiply. + /// The vector to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseMatrix leftSide, SparseVector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseVector)leftSide.Multiply(rightSide); + } + + /// + /// Multiplies a Vector and a Matrix. + /// + /// The vector to multiply. + /// The matrix to multiply. + /// The result of multiplication. + /// If or is . + public static SparseVector operator *(SparseVector leftSide, SparseMatrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return (SparseVector)rightSide.LeftMultiply(leftSide); + } + + /// + /// 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 SparseMatrix operator %(SparseMatrix leftSide, float rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return (SparseMatrix)leftSide.Modulus(rightSide); + } } } diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index f41380a6..d3a85f2c 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -399,14 +399,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The vector to get the values from. /// A vector containing a the same values as . /// If is . - public static Vector operator +(SparseVector rightSide) + public static SparseVector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Plus(); + return (SparseVector)rightSide.Plus(); } /// @@ -417,7 +417,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the addition. /// If and are not the same size. /// If or is . - public static Vector operator +(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -434,7 +434,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Add(rightSide); + return (SparseVector)leftSide.Add(rightSide); } /// @@ -480,14 +480,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The vector to get the values from. /// A vector containing the negated values as . /// If is . - public static Vector operator -(SparseVector rightSide) + public static SparseVector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } - return rightSide.Negate(); + return (SparseVector)rightSide.Negate(); } /// @@ -498,7 +498,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the subtraction. /// If and are not the same size. /// If or is . - public static Vector operator -(SparseVector leftSide, SparseVector rightSide) + public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { @@ -515,7 +515,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } - return leftSide.Subtract(rightSide); + return (SparseVector)leftSide.Subtract(rightSide); } ///