Browse Source

LA: Support for pointwise .*, ./ and .% operators where supported (F# for now)

optimization-1
Christoph Ruegg 13 years ago
parent
commit
50a334bf2c
  1. 14
      src/FSharpUnitTests/MatrixTests.fs
  2. 15
      src/FSharpUnitTests/VectorTests.fs
  3. 14
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  4. 20
      src/Numerics/LinearAlgebra/Generic/Vector.cs

14
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) )
[<Test>]
@ -179,3 +179,13 @@ module MatrixTests =
[<Test>]
let ``Matrix.foldByRow`` () =
Matrix.foldByRow (+) 0.0 smallM |> should equal (DenseVector.ofList [0.6;0.6] :> Vector<float>)
[<Test>]
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))
[<Test>]
let ``Pointwise Division using ./ Operator`` () =
let z = largeM ./ DenseMatrix.create 100 100 2.0
z |> should equal (largeM * 0.5)

15
src/FSharpUnitTests/VectorTests.fs

@ -164,3 +164,18 @@ module VectorTests =
[<Test>]
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<float>)
[<Test>]
let ``Pointwise Multiplication using .* Operator`` () =
let z = largev .* largev
z |> should equal (DenseVector.init 100 (fun i -> (float i / 100.0) ** 2.0))
[<Test>]
let ``Pointwise Division using ./ Operator`` () =
let z = largev ./ DenseVector.create 100 2.0
z |> should equal (largev * 0.5)
[<Test>]
let ``Pointwise Modulus using .% Operator`` () =
let z = largev .% DenseVector.create 100 2.0
z |> should equal (largev % 2.0)

14
src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs

@ -29,6 +29,8 @@
// </copyright>
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<T> op_DotMultiply(Matrix<T> x, Matrix<T> y)
{
return x.PointwiseMultiply(y);
}
[SpecialName]
public static Matrix<T> op_DotDivide(Matrix<T> x, Matrix<T> y)
{
return x.PointwiseDivide(y);
}
/// <summary>
/// Computes the trace of this matrix.
/// </summary>

20
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -28,6 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System.Runtime.CompilerServices;
namespace MathNet.Numerics.LinearAlgebra.Generic
{
using System;
@ -1284,6 +1286,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.PointwiseModulus(rightSide);
}
[SpecialName]
public static Vector<T> op_DotMultiply(Vector<T> x, Vector<T> y)
{
return x.PointwiseMultiply(y);
}
[SpecialName]
public static Vector<T> op_DotDivide(Vector<T> x, Vector<T> y)
{
return x.PointwiseDivide(y);
}
[SpecialName]
public static Vector<T> op_DotPercent(Vector<T> x, Vector<T> y)
{
return x.PointwiseModulus(y);
}
/// <summary>
/// Computes the p-Norm.
/// </summary>

Loading…
Cancel
Save