I am trying to use ILNumerics from F#. It seems quite difficult.
I would like to replicate this example
What I have so far is the following code:
open ILNumerics
open type ILMath
let inline (!) (x :RetArray<'T>) = Array<'T>.op_Implicit(x)
let inline (!!) (x:Array<'T> ) = InArray<'T>.op_Implicit(x)
let inline (!!!) (x:float[]) :InArray<float> = Array.map float32 x
|> InArray.op_Implicit
let inline (!~) (x:float[]) : Array<float> = Array.map float32 x
|> Array.op_Implicit
let vec1 = (!~) [|1.0 .. 0.1 .. 2.0 |]
let X1 = (linspace<float>((!!!)[|-3.0|],(!!!) [|3.0|],(!!!)[|20.0|]))
|> Seq.toArray
|> (!~)
|> (!!)
let X2 = X1
let X3 :OutArray<float>= null
let T = meshgrid(X1,X2, X3)
Do I really need all these conversions to make it work? Also, how do I use an interpolant? I have scattered data and have to map from R3 -> R2 and from R4 -> R3.
I was considering Kriging interpolation, but it seems quite tough to use the library from F#. Any suggestion? Otherwise will be better off to solve the problem in C# and call it from F#, when needed?
Related
I need to estimate a multivariate function, known from discrete scattered data.
I am using ILNumerics interpolation toolbox for that.
I have the following code to test the library from F#:
let inline (!) (x :RetArray<'T>) = Array<'T>.op_Implicit(x)
let inline (!!) (x:Array<'T> ) = InArray<'T>.op_Implicit(x)
let inline (!!!) (x:float[]) :InArray<float> = x
|> InArray.op_Implicit
let inline (!~) (x:float[]) : Array<float> = x
|> Array.op_Implicit
let X3 :OutArray<float>= null
let V (x:'T[]) :Array<'T> = (ILMath.vector<'T> x) |> Array.op_Implicit
let X1':Array<float> = (linspace<float>((!!!)[|-3.0|],(!!!) [|3.0|],(!!!)[|20.0|]))
|> (Seq.toArray >> V )
let Y:Array<float> = sin(X1')|> (Seq.toArray >> V)
let result = kriging( (!!)Y, (!!) X1', (!!) X1', null, X3) |> (Seq.toArray >> V)
I get the following error:
System.ArgumentException: V must be a non-empty matrix of size [k x n], where n = X.S[1]
I suspect that the internal code tries to evaluate X.S[1] and it fails, in F#, since it would need X.S.[1]; I may be completely wrong, but I would like to know whether the library may also be used from F# or it is pointless even to try.
I have also tried using the KrigingInterpolator class and I get a similar error.
On a side note: do you know any reliable library which performs multivariate interpolation for scattered data, with F#?
I implemented newton's method to find roots of a function. I'm wondering if i can optimize the code as make it more time and space efficient and visually enlightening. Here I used a mutable variable, but I'm wondering if we can do it without. Here is the question and the code that I wrote:
open System
let newton (f:(float->float),x0: float,tol:float, dx:float)=
let mutable x=x0
while Math.Abs (f x) >= tol do
//compute derivative
let fderivative = (f x+dx-f x)/dx
x<-x-(f x)/fderivative
x
You can use recursion to avoid mutation:
let newton (f: float -> float,x0,tol,dx)=
let rec loop x =
if abs (f x) < tol then x
else
let f' = (f x+dx-f x)/dx
x - f x / f' |> loop
loop x0
Usually, when I need to build a list from 0 to 10, I simply do this: [0..10]. This gives me a list of integers from 0 to 10. But this time I would need a list of float from 0 to 10. Is there a way to do that?
let testFunc (x: float<metre>) =
x
let otherTestFunc =
[0.0 .. 10.0] // How do I make this return float<metre>
|> List.map (fun x -> testFunc x)
I reported this to the F# team a while ago, but you need to specify the step manually when using Measures.
let testFunc (x: float<metre>) =
x
let otherTestFunc =
[0.0 <metre> .. 1.0<metre> .. 10.0 <metre>] // How do I make this return float<metre>
|> List.map (fun x -> testFunc x)
Floating-point loops may be dangerous as they hide an accumulating round-off error. See F# Floating point ranges are experimental and may be deprecated for more details.
I believe, the easiest way is to keep your loop in a plain, non-measured int, and convert the value within the loop.
let otherTestFunc =
[0 .. 10]
|> List.map (float >> (*) 1.0<metre>)
|> List.map testFunc
Suppose I need to construct a tuple of length three:
(x , y, z)
And I have a function which returns a tuple of length two - exampleFunction and the last two elements of the tuple to be constructed are from this tuple.
How can I do this without having to call the exampleFunction two times:
(x, fst exampleFunction , snd exampleFunction)
I just want to do / achieve something like
(x, exampleFunction)
but it complains that the tuples have unmatched length ( of course )
Not looking at doing let y,z = exampleFunction()
There may be a built in function, but a custom one would work just as well.
let repack (a,(b,c)) = (a,b,c)
repack (x,exampleFunction)
I'm not sure it if worth a separate answer, but both answers provided above are not optimal since both construct redundant Tuple<'a, Tuple<'b, 'c>> upon invocation of the helper function. I would say a custom operator would be better for both readability and performance:
let inline ( +# ) a (b,c) = a, b, c
let result = x +# yz // result is ('x, 'y, 'z)
The problem you have is that the function return a*b so the return type becomes 'a*('b*'c) which is different to 'a*'b*'c the best solution is a small helper function like
let inline flatten (a,(b,c)) = a,b,c
then you can do
(x,examplefunction) |> flatten
I have the following function in my common extension file.
You may find this useful.
let inline squash12 ((a,(b,c) ):('a*('b*'c) )):('a*'b*'c ) = (a,b,c )
let inline squash21 (((a,b),c ):(('a*'b)*'c )):('a*'b*'c ) = (a,b,c )
let inline squash13 ((a,(b,c,d)):('a*('b*'c*'d))):('a*'b*'c*'d) = (a,b,c,d)
let seqsquash12 (sa:seq<'a*('b*'c) >) = sa |> Seq.map squash12
let seqsquash21 (sa:seq<('a*'b)*'c >) = sa |> Seq.map squash21
let seqsquash13 (sa:seq<'a*('b*'c*'d)>) = sa |> Seq.map squash13
let arrsquash12 (sa:('a*('b*'c) ) array) = sa |> Array.map squash12
let arrsquash21 (sa:(('a*'b)*'c ) array) = sa |> Array.map squash21
let arrsquash13 (sa:('a*('b*'c*'d)) array) = sa |> Array.map squash13
I've been reading Chris Okasaki's Purely Functional Data Structures, and am wondering if there is a nice way to build lazy algorithms with F# inside of a monad that enables lazy computation (a Lazy monad). Chris used a custom extension for suspension / force syntax in SML, but I'd like to think that we could instead just use a simple monad in F#. Manual use of lazy and force in F# seems pretty cluttery.
I found this implementation in Scheme, but I don't know how applicable it would be.
From my cursory knowledge and research, it seems both feasible and desirable within reasonable limitations.
Please let me know :)
To port Okasaki code, why not just go with F# lazy keyword and some helper syntax to express forcing, for example:
let (!) (x: Lazy<'T>) : 'T = x.Value
Since F# type system cannot properly express monads, I assume you suggest defining a computation expression for lazy computations. I guess one can do that, but how would that help exactly?
type LazyBuilder =
| Lazy
member this.Return(x: 'T) : Lazy<'T> =
Lazy.CreateFromValue(x)
member this.Bind(x: Lazy<'T1>, f: 'T1 -> Lazy<'T2>) : Lazy<'T2> =
lazy (f x.Value).Value
let test () =
let v =
Lazy {
let! x = lazy 1
let! y = lazy 2
return x + y
}
v.Value
let (!) (x: Lazy<'T>) : 'T = x.Value
let test2 () =
let v =
lazy
let x = lazy 1
let y = lazy 2
!x + !y
!v
I'm not sure this helps, but you can avoid using the lazy keyword altogether if you particularly want to for some reason:
type ('a, 'b) lazyT = Lz of 'a * ('a -> 'b)
let force (Lz (a, e)) = e a
let pack x = Lz(x, (fun i -> i))
type MyLazyBuilder =
| Mylazy
member this.Bind(x, f) =
match x with
| Lz(xa, xe) ->
Lz(xa, fun x -> force (f (xe x)))
member this.Return(x) = pack x
let sth =
Mylazy {
let! x = pack 12
let! y = pack (x + 1)
return y * x
}
let res = force sth
(absent the part where force only evaluates it once).
Late, but thought it was worth suggesting.