From e0ff4e89d311e8286938b426e4728a268ed078a9 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Tue, 8 Mar 2011 17:28:21 +0200 Subject: [PATCH] Implemented virtual TransposeThisAndMultiply methods for matrices. --- .../Generic/Matrix.Arithmetic.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 9525ced1..d9c31895 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -570,6 +570,80 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The result of the multiplication. protected abstract void DoTransposeAndMultiply(Matrix other, Matrix result); + /// + /// Multiplies the transpose of 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.Rows != other.RowCount. + /// If the result matrix's dimensions are not the this.ColumnCount x other.ColumnCount. + public virtual void TransposeThisAndMultiply(Matrix other, Matrix result) + { + if (other == null) + { + throw new ArgumentNullException("other"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (RowCount != other.RowCount) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions); + } + + if ((result.RowCount != ColumnCount) || (result.ColumnCount != other.ColumnCount)) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions); + } + + if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + { + var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); + TransposeThisAndMultiply(other, tmp); + tmp.CopyTo(result); + } + else + { + DoTransposeThisAndMultiply(other, result); + } + } + + /// + /// Multiplies the transpose of this matrix with another matrix and returns the result. + /// + /// The matrix to multiply with. + /// If this.Rows != other.RowCount. + /// If the other matrix is . + /// The result of the multiplication. + public virtual Matrix TransposeThisAndMultiply(Matrix other) + { + if (other == null) + { + throw new ArgumentNullException("other"); + } + + if (RowCount != other.RowCount) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions); + } + + var result = CreateMatrix(ColumnCount, other.ColumnCount); + TransposeThisAndMultiply(other, result); + return result; + } + + /// + /// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix. + /// + /// The matrix to multiply with. + /// The result of the multiplication. + protected abstract void DoTransposeThisAndMultiply(Matrix other, Matrix result); + /// /// Negate each element of this matrix. ///