diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs index fa98bba6..3cd7d430 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs @@ -149,6 +149,142 @@ namespace MathNet.Numerics.LinearAlgebra.Double 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. /// @@ -363,5 +499,39 @@ namespace MathNet.Numerics.LinearAlgebra.Double 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); + } } } \ No newline at end of file