5 people like it.

Fun with infinite sums - Haacked!

F# version of the code samples from an article "Fun with infinite sums" by Phil Haack. Using infinite sequences to separate the concerns and F# charting for simpler visualization.

Setup

1: 
2: 
3: 
4: 
5: 
// Install FSharp.Charting package from NuGet
// and then reference the charting library for
// easy visualization from F# Interactive
#load @"packages\FSharp.Charting.0.90.5\FSharp.Charting.fsx"
open FSharp.Charting

Infinite sums

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
/// Generates partial sums of the reciprocal series
/// First generate series, then use scan for partial sums
let eulerSums =
  Seq.initInfinite (fun i -> 1.0 / float (i + 1) ** 2.0) 
  |> Seq.scan (+) 0.0

/// Generate partial sums of grandi series (even easier!)
let grandiSums = 
  Seq.initInfinite (fun i -> -1.0 ** float i)
  |> Seq.scan (+) 0.0

/// Generate partial sums of the grandiSums series
/// using the cesaro summation (simply take the sums
/// and divide by the element index in mapi)
/// (We skip the initial zero elements produced by scan) 
let grandiCesaro = 
  grandiSums
  |> Seq.skip 1
  |> Seq.scan (+) 0.0
  |> Seq.mapi (fun i partial -> partial / float i )
  |> Seq.skip 1

Visualization

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
// Take 100 (or whatever number of elements)
// and pass the series to Chart.Line to see a chart  
eulerSums
|> Seq.take 100
|> Chart.Line

// Same for the other visualizations!
grandiSums   |> Seq.take 100 |> Chart.Line
grandiCesaro |> Seq.take 100 |> Chart.Line
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Charting
val eulerSums : seq<float>

Full name: Script.eulerSums


 Generates partial sums of the reciprocal series
 First generate series, then use scan for partial sums
module Seq

from Microsoft.FSharp.Collections
val initInfinite : initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val i : int
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
val scan : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> seq<'State>

Full name: Microsoft.FSharp.Collections.Seq.scan
val grandiSums : seq<float>

Full name: Script.grandiSums


 Generate partial sums of grandi series (even easier!)
val grandiCesaro : seq<float>

Full name: Script.grandiCesaro


 Generate partial sums of the grandiSums series
 using the cesaro summation (simply take the sums
 and divide by the element index in mapi)
 (We skip the initial zero elements produced by scan)
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val partial : float
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
type Chart =
  static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member BoxPlotFromData : data:seq<#key * #seq<'a2>> * ?Name:string * ?Title:string * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart (requires 'a2 :> value)
  static member BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart
  static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Bubble : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  ...

Full name: FSharp.Charting.Chart
static member Chart.Line : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
static member Chart.Line : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
Raw view Test code New version

More information

Link:http://fssnip.net/ls
Posted:10 years ago
Author:Tomas Petricek
Tags: series , charting , sums