Math.NET Numerics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
2.7 KiB

// (c) Microsoft Corporation 2005-2009.
#nowarn "9"
namespace Microsoft.FSharp.Compatibility
module Array =
open System.Runtime.InteropServices
let invalidArg arg msg = raise (new System.ArgumentException((msg:string),(arg:string)))
let nonempty (arr: _[]) = (arr.Length <> 0)
let inline pinObjUnscoped (obj: obj) = GCHandle.Alloc(obj,GCHandleType.Pinned)
let inline pinObj (obj: obj) f =
let gch = pinObjUnscoped obj
try f gch
finally
gch.Free()
[<NoDynamicInvocation>]
let inline pin (arr: 'T []) (f : nativeptr<'T> -> 'U) =
pinObj (box arr) (fun _ -> f (NativeInterop.NativePtr.of_array arr 0))
[<NoDynamicInvocation>]
let inline pinUnscoped (arr: 'T []) : nativeptr<'T> * _ =
let gch = pinObjUnscoped (box arr)
NativeInterop.NativePtr.of_array arr 0, gch
[<NoDynamicInvocation>]
let inline pin_unscoped arr= pinUnscoped arr
let inline contains x (arr: 'T []) =
let mutable found = false
let mutable i = 0
while not found && i < arr.Length do
if x = arr.[i] then
found <- true
else
i <- i + 1
found
let inline mem x arr = contains x arr
let scanSubRight f (arr : _[]) start fin initState =
let mutable state = initState
let res = Array.create (2+fin-start) initState
for i = fin downto start do
state <- f arr.[i] state;
res.[i - start] <- state
done;
res
let scanSubLeft f initState (arr : _[]) start fin =
let mutable state = initState
let res = Array.create (2+fin-start) initState
for i = start to fin do
state <- f state arr.[i];
res.[i - start+1] <- state
done;
res
let scanReduce f (arr : _[]) =
let arrn = arr.Length
if arrn = 0 then invalidArg "arr" "the input array is empty"
else scanSubLeft f arr.[0] arr 1 (arrn - 1)
let scan1_left f arr = scanReduce f arr
let scanReduceBack f (arr : _[]) =
let arrn = arr.Length
if arrn = 0 then invalidArg "arr" "the input array is empty"
else scanSubRight f arr 0 (arrn - 2) arr.[arrn - 1]
let scan1_right f arr = scanReduceBack f arr
let createJaggedMatrix (n:int) (m:int) (x:'T) =
let arr = (Array.zeroCreate n : 'T [][])
for i = 0 to n - 1 do
let inner = (Array.zeroCreate m : 'T [])
for j = 0 to m - 1 do
inner.[j] <- x
arr.[i] <- inner
arr
let make_matrix n m x = createJaggedMatrix n m x
let create_matrix n m x = make_matrix n m x