From 0c41bdbf9d4918502087b8f8205c9c2ce0a67e7e Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 2 Aug 2013 21:18:04 +0200 Subject: [PATCH] LA: Support for pointwise .*, ./ and .% operators where supported (F# for now) --- src/FSharpUnitTests/MatrixTests.fs | 14 +++++++++++-- src/FSharpUnitTests/VectorTests.fs | 15 ++++++++++++++ .../Generic/Matrix.Arithmetic.cs | 14 +++++++++++++ src/Numerics/LinearAlgebra/Generic/Vector.cs | 20 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/FSharpUnitTests/MatrixTests.fs b/src/FSharpUnitTests/MatrixTests.fs index 26e95558..2017ec03 100644 --- a/src/FSharpUnitTests/MatrixTests.fs +++ b/src/FSharpUnitTests/MatrixTests.fs @@ -10,14 +10,14 @@ module MatrixTests = let approximately_equal tolerance = equalWithin (10.0 ** (float -tolerance)) - /// A small uniform vector. + /// A small uniform matrix. let smallM = DenseMatrix.OfArray( Array2D.create 2 2 0.3 ) let failingFoldBackM = DenseMatrix.init 2 3 (fun i j -> 1.0) /// A small sparse matrix. let sparseM = SparseMatrix.ofListi 2 3 [(1,0,0.3)] - /// A large vector with increasingly large entries + /// A large matrix with increasingly large entries let largeM = DenseMatrix.OfArray( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) [] @@ -179,3 +179,13 @@ module MatrixTests = [] let ``Matrix.foldByRow`` () = Matrix.foldByRow (+) 0.0 smallM |> should equal (DenseVector.ofList [0.6;0.6] :> Vector) + + [] + let ``Pointwise Multiplication using .* Operator`` () = + let z = largeM .* largeM + z |> should equal (DenseMatrix.init 100 100 (fun i j -> (float i * 100.0 + float j) ** 2.0)) + + [] + let ``Pointwise Division using ./ Operator`` () = + let z = largeM ./ DenseMatrix.create 100 100 2.0 + z |> should equal (largeM * 0.5) diff --git a/src/FSharpUnitTests/VectorTests.fs b/src/FSharpUnitTests/VectorTests.fs index d5f9187e..a8430d2e 100644 --- a/src/FSharpUnitTests/VectorTests.fs +++ b/src/FSharpUnitTests/VectorTests.fs @@ -164,3 +164,18 @@ module VectorTests = [] let ``Vector.insert`` () = Vector.insert 2 0.5 smallv |> should (approximately_equal 14) (new DenseVector ( [|0.3;0.3;0.5;0.3;0.3;0.3|] ) :> Vector) + + [] + let ``Pointwise Multiplication using .* Operator`` () = + let z = largev .* largev + z |> should equal (DenseVector.init 100 (fun i -> (float i / 100.0) ** 2.0)) + + [] + let ``Pointwise Division using ./ Operator`` () = + let z = largev ./ DenseVector.create 100 2.0 + z |> should equal (largev * 0.5) + + [] + let ``Pointwise Modulus using .% Operator`` () = + let z = largev .% DenseVector.create 100 2.0 + z |> should equal (largev % 2.0) diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 0a9754bc..b0826bac 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -29,6 +29,8 @@ // +using System.Runtime.CompilerServices; + namespace MathNet.Numerics.LinearAlgebra.Generic { using System; @@ -1160,6 +1162,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.Modulus(rightSide); } + [SpecialName] + public static Matrix op_DotMultiply(Matrix x, Matrix y) + { + return x.PointwiseMultiply(y); + } + + [SpecialName] + public static Matrix op_DotDivide(Matrix x, Matrix y) + { + return x.PointwiseDivide(y); + } + /// /// Computes the trace of this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index d896c2e7..c3a1915c 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -28,6 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System.Runtime.CompilerServices; + namespace MathNet.Numerics.LinearAlgebra.Generic { using System; @@ -1297,6 +1299,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.PointwiseModulus(rightSide); } + [SpecialName] + public static Vector op_DotMultiply(Vector x, Vector y) + { + return x.PointwiseMultiply(y); + } + + [SpecialName] + public static Vector op_DotDivide(Vector x, Vector y) + { + return x.PointwiseDivide(y); + } + + [SpecialName] + public static Vector op_DotPercent(Vector x, Vector y) + { + return x.PointwiseModulus(y); + } + /// /// Computes the p-Norm. ///