From cc802b5ed1a37db2a3a8f4f38d51f739e44269cb Mon Sep 17 00:00:00 2001 From: Tomas Petricek Date: Sun, 23 Sep 2012 16:25:38 +0100 Subject: [PATCH] Adding GetSlice extensions to matrix and vector types (cherry picked from commit 555ba59714bcba86087f909092956a3e40b83e2d) (cherry picked from commit 03f14ab416769382866335be313846e5fa9b2784) Signed-off-by: Christoph Ruegg --- src/FSharp/Extensions.fs | 68 ++++++++++++++++++++++++++++++++++++++++ src/FSharp/FSharp.fsproj | 1 + src/FSharp/Main.fs | 1 + 3 files changed, 70 insertions(+) create mode 100755 src/FSharp/Extensions.fs diff --git a/src/FSharp/Extensions.fs b/src/FSharp/Extensions.fs new file mode 100755 index 00000000..7ca5b7dd --- /dev/null +++ b/src/FSharp/Extensions.fs @@ -0,0 +1,68 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.LinearAlgebra.Generic + +// Module that contains implementation of useful F#-specific +// extension members for generic Matrix and Vector types +[] +module FSharpExtensions = + + // A type extension for the generic vector type that + // adds the 'GetSlice' method to allow vec.[a .. b] syntax + type MathNet.Numerics.LinearAlgebra.Generic. + Vector<'T when 'T : struct and 'T : (new : unit -> 'T) + and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable + and 'T :> System.ValueType> with + + /// Gets a slice of a vector starting at a specified index + /// and ending at a specified index (both indices are optional) + /// This method can be used via the x.[start .. finish] syntax + member x.GetSlice(start, finish) = + let start = defaultArg start 0 + let finish = defaultArg finish (x.Count - 1) + x.SubVector(start, finish - start + 1) + + // A type extension for the generic matrix type that + // adds the 'GetSlice' method to allow m.[r1 .. r2, c1 .. c2] syntax + type MathNet.Numerics.LinearAlgebra.Generic. + Matrix<'T when 'T : struct and 'T : (new : unit -> 'T) + and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable + and 'T :> System.ValueType> with + + /// Gets a submatrix using a specified column range and + /// row range (all indices are optional) + /// This method can be used via the x.[r1 .. r2, c1 .. c2 ] syntax + member x.GetSlice(rstart, rfinish, cstart, cfinish) = + let cstart = defaultArg cstart 0 + let rstart = defaultArg rstart 0 + let cfinish = defaultArg cfinish (x.ColumnCount - 1) + let rfinish = defaultArg rfinish (x.RowCount - 1) + x.SubMatrix(rstart, rfinish - rstart + 1, cstart, cfinish - cstart + 1) \ No newline at end of file diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index bf850432..53e98c55 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -46,6 +46,7 @@ + diff --git a/src/FSharp/Main.fs b/src/FSharp/Main.fs index 47e3f14a..9bb5c897 100644 --- a/src/FSharp/Main.fs +++ b/src/FSharp/Main.fs @@ -32,6 +32,7 @@ namespace MathNet.Numerics open MathNet.Numerics.LinearAlgebra.Double open MathNet.Numerics.LinearAlgebra.Generic + /// A module which implements some F# utility functions. module FSharp =