24 people like it.

Asserting Series Convergence: Ramanujan's 1/pi formula

Asserting a series convergence using high order functions taking for example the 1/pi formula by Ramanujan. Play with the parameters to see where the numeric data types limits makes the function to return false.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open System

//Assert the convergence of a series.
let assertSeriesConvergence(expected : float) (delta : float) low high series =
    if low > high then 
        failwith "Incorrect boundaries for the series"
    else
        seq {for n = low to high do yield n}
        |> Seq.map (fun n -> Math.Abs((series n) - expected) <= delta) //build the assertions
        |> Seq.forall ((=) true)

//Naive factorial with tail recursion.
let (!) n  = let rec _fact n result = if n = 1.0 || n = 0.0 then result else _fact (n - 1.0) (result * n)
             _fact n 1.0

//Ramanujan Formula.
let Ramanujan x = 
    let sum = seq {for i = 0.0 to x do yield i}
               |> Seq.map (fun n -> (!(4.0 * n) * (1103.0 + (26390.0 * n))) / (((!n) ** 4.0) * (3964.0 ** (4.0 * n))))
               |> Seq.sum
    ((2.0 * (sqrt 2.0)) / 9801.0) * sum //any ideas?

assertSeriesConvergence (1.0 / Math.PI) (10.0 ** (-8.0)) 1.0 40.0 Ramanujan
namespace System
val assertSeriesConvergence : expected:float -> delta:float -> low:float -> high:float -> series:(float -> float) -> bool

Full name: Script.assertSeriesConvergence
val expected : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val delta : float
val low : float
val high : float
val series : (float -> float)
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val n : float
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
Math.Abs(value: decimal) : decimal
Math.Abs(value: float) : float
Math.Abs(value: float32) : float32
Math.Abs(value: int64) : int64
Math.Abs(value: int) : int
Math.Abs(value: int16) : int16
Math.Abs(value: sbyte) : sbyte
val forall : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.forall
val _fact : (float -> float -> float)
val result : float
val Ramanujan : x:float -> float

Full name: Script.Ramanujan
val x : float
val sum : float
val i : float
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
field Math.PI = 3.14159265359
Raw view Test code New version

More information

Link:http://fssnip.net/3x
Posted:13 years ago
Author:Horacio Nuñez
Tags: ramanujan series