From b71cacdb893aa4497e2a7788fcb9f4b3f552caa5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 3 Mar 2013 13:23:27 +0100 Subject: [PATCH] LA: Matrix arithmetics design pattern: review arg checks and forwards --- .../Generic/Matrix.Arithmetic.cs | 85 ++++++++++--------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 93888595..0bfaa5ef 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -384,8 +384,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// If this.ColumnCount != rightSide.Count. public Vector Multiply(Vector rightSide) { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + if (ColumnCount != rightSide.Count) + { + throw DimensionsDontMatch(this, rightSide, "rightSide"); + } + var ret = CreateVector(RowCount); - Multiply(rightSide, ret); + DoMultiply(rightSide, ret); return ret; } @@ -423,7 +433,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (ReferenceEquals(rightSide, result)) { var tmp = result.CreateVector(result.Count); - Multiply(rightSide, tmp); + DoMultiply(rightSide, tmp); tmp.CopyTo(result); } else @@ -441,8 +451,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// If this.RowCount != .Count. public Vector LeftMultiply(Vector leftSide) { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + if (RowCount != leftSide.Count) + { + throw DimensionsDontMatch(this, leftSide, "leftSide"); + } + var ret = CreateVector(ColumnCount); - LeftMultiply(leftSide, ret); + DoLeftMultiply(leftSide, ret); return ret; } @@ -480,7 +500,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (ReferenceEquals(leftSide, result)) { var tmp = result.CreateVector(result.Count); - LeftMultiply(leftSide, tmp); + DoLeftMultiply(leftSide, tmp); tmp.CopyTo(result); } else @@ -528,7 +548,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - Multiply(other, tmp); + DoMultiply(other, tmp); tmp.CopyTo(result); } else @@ -550,13 +570,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { throw new ArgumentNullException("other"); } + if (ColumnCount != other.RowCount) { throw DimensionsDontMatch(this, other); } var result = CreateMatrix(RowCount, other.ColumnCount); - Multiply(other, result); + DoMultiply(other, result); return result; } @@ -589,7 +610,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - TransposeAndMultiply(other, tmp); + DoTransposeAndMultiply(other, tmp); tmp.CopyTo(result); } else @@ -618,7 +639,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } var result = CreateMatrix(RowCount, other.RowCount); - TransposeAndMultiply(other, result); + DoTransposeAndMultiply(other, result); return result; } @@ -631,6 +652,16 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// If this.RowCount != rightSide.Count. public Vector TransposeThisAndMultiply(Vector rightSide) { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + if (RowCount != rightSide.Count) + { + throw DimensionsDontMatch(this, rightSide, "rightSide"); + } + var ret = CreateVector(ColumnCount); DoTransposeThisAndMultiply(rightSide, ret); return ret; @@ -652,14 +683,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentNullException("rightSide"); } - if (RowCount != rightSide.Count) + if (result == null) { - throw DimensionsDontMatch(this, rightSide, "rightSide"); + throw new ArgumentNullException("result"); } - if (result == null) + if (RowCount != rightSide.Count) { - throw new ArgumentNullException("result"); + throw DimensionsDontMatch(this, rightSide, "rightSide"); } if (ColumnCount != result.Count) @@ -786,21 +817,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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 DimensionsDontMatch(leftSide, rightSide); - } - return leftSide.Add(rightSide); } @@ -833,21 +854,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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 DimensionsDontMatch(leftSide, rightSide); - } - return leftSide.Subtract(rightSide); } @@ -919,16 +930,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentNullException("leftSide"); } - if (rightSide == null) - { - throw new ArgumentNullException("rightSide"); - } - - if (leftSide.ColumnCount != rightSide.RowCount) - { - throw DimensionsDontMatch(leftSide, rightSide); - } - return leftSide.Multiply(rightSide); }