From 03f14ab416769382866335be313846e5fa9b2784 Mon Sep 17 00:00:00 2001 From: Tomas Petricek Date: Sun, 23 Sep 2012 17:43:53 +0100 Subject: [PATCH] Actually adding GetSlice method implementation. --- src/FSharp/Extensions.fs | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 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..0be99cae --- /dev/null +++ b/src/FSharp/Extensions.fs @@ -0,0 +1,66 @@ +// +// 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