-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIntegrate.hs
More file actions
41 lines (31 loc) · 973 Bytes
/
Integrate.hs
File metadata and controls
41 lines (31 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
module Main where
import Language.Rust.Inline
import Data.Int (Int32, Int64)
extendContext basic
extendContext functions
setCrateRoot [("rayon", "0.9")]
[rust|
extern crate rayon;
use rayon::prelude::*;
|]
main = do
print (integrate (0, pi) (\x -> sin x) 1000)
print (integrate (0, 5) (\x -> x^2 + 5 * x - 3) 1000)
-- | Take a double integral
integrate :: (Double, Double) -> (Double -> Double) -> Int32 -> Double
integrate (lo, hi) func n = unsafeLocalState $
$(withFunPtr [t| Double -> Double |]) func $ \func1 ->
[rustIO|
f64 {
let f = $( func1: extern "C" fn(f64) -> f64 );
let delta = ( $(hi: f64) - $(lo: f64) ) / $(n: i32) as f64;
(0..n)
.into_par_iter()
.map(|i| {
let a = i as f64 * delta;
delta * (f(a) + f(a + delta)) / 2f64
})
.sum()
}
|]