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.

Copy Source
Copy Link
Tools:

Setup

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

Infinite sums

 1: /// Generates partial sums of the reciprocal series
 2: /// First generate series, then use scan for partial sums
 3: let eulerSums =
 4:   Seq.initInfinite (fun i -> 1.0 / float (i + 1) ** 2.0) 
 5:   |> Seq.scan (+) 0.0
 6: 
 7: /// Generate partial sums of grandi series (even easier!)
 8: let grandiSums = 
 9:   Seq.initInfinite (fun i -> -1.0 ** float i)
10:   |> Seq.scan (+) 0.0
11: 
12: /// Generate partial sums of the grandiSums series
13: /// using the cesaro summation (simply take the sums
14: /// and divide by the element index in mapi)
15: /// (We skip the initial zero elements produced by scan) 
16: let grandiCesaro = 
17:   grandiSums
18:   |> Seq.skip 1
19:   |> Seq.scan (+) 0.0
20:   |> Seq.mapi (fun i partial -> partial / float i )
21:   |> Seq.skip 1

Visualization

1: // Take 100 (or whatever number of elements)
2: // and pass the series to Chart.Line to see a chart  
3: eulerSums
4: |> Seq.take 100
5: |> Chart.Line
6: 
7: // Same for the other visualizations!
8: grandiSums   |> Seq.take 100 |> Chart.Line
9: grandiCesaro |> Seq.take 100 |> Chart.Line
namespace FSharp
namespace FSharp.Charting
val eulerSums : seq<float>

Full name: Snippet.eulerSums

  type: seq<float>
  inherits: System.Collections.IEnumerable



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

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

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val i : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
Multiple items
val float : 'T -> float (requires member op_Explicit)

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

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

Full name: Microsoft.FSharp.Core.float<_>

  type: float<'Measure>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<'Measure>>
  implements: System.IEquatable<float<'Measure>>
  inherits: System.ValueType


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

Full name: Microsoft.FSharp.Core.float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val scan : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> seq<'State>

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

Full name: Snippet.grandiSums

  type: seq<float>
  inherits: System.Collections.IEnumerable



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

Full name: Snippet.grandiCesaro

  type: seq<float>
  inherits: System.Collections.IEnumerable



 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 : int -> seq<'T> -> seq<'T>

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

Full name: Microsoft.FSharp.Collections.Seq.mapi
val partial : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val take : int -> seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
type Chart =
  class
    static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member BoxPlotFromData : data:seq<#key * #seq<'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> ChartTypes.GenericChart (requires 'a2 :> value)
    static member BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> ChartTypes.GenericChart
    static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> ChartTypes.GenericChart
    static member Bubble : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> ChartTypes.GenericChart
    static member Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.CandlestickChart
    static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.CandlestickChart
    static member Column : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Column : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Columns : charts:seq<ChartTypes.GenericChart> -> ChartTypes.GenericChart
    static member Combine : charts:seq<ChartTypes.GenericChart> -> ChartTypes.GenericChart
    static member private ConfigureBubble : c:ChartTypes.GenericChart * vBubbleMaxSize:int option * vBubbleMinSize:int option * vBubbleScaleMax:float option * vBubbleScaleMin:float option * vBubbleUseSizeForLabel:bool option -> unit
    static member Doughnut : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.DoughnutChart
    static member Doughnut : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.DoughnutChart
    static member ErrorBar : data:seq<#key * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.ErrorBarChart
    static member FastLine : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member FastLine : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member FastPoint : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member FastPoint : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Funnel : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.FunnelChart
    static member Funnel : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.FunnelChart
    static member Kagi : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.KagiChart
    static member Kagi : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.KagiChart
    static member Line : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Line : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Pie : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PieChart
    static member Pie : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PieChart
    static member Point : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?MarkerColor:System.Drawing.Color * ?MarkerSize:int -> ChartTypes.GenericChart
    static member Point : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?MarkerColor:System.Drawing.Color * ?MarkerSize:int -> ChartTypes.GenericChart
    static member PointAndFigure : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PointAndFigureChart
    static member PointAndFigure : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PointAndFigureChart
    static member Polar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PolarChart
    static member Polar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PolarChart
    static member Pyramid : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PyramidChart
    static member Pyramid : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.PyramidChart
    static member Radar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.RadarChart
    static member Radar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.RadarChart
    static member Range : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Range : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member RangeBar : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member RangeBar : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member RangeColumn : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member RangeColumn : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member RegisterKeyConvertor : conversion:(#key -> key) -> unit
    static member Renko : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.RenkoChart
    static member Renko : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.RenkoChart
    static member Rows : charts:seq<ChartTypes.GenericChart> -> ChartTypes.GenericChart
    static member Spline : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Spline : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member SplineArea : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member SplineArea : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member SplineRange : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member SplineRange : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member StackedArea : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StackedArea100 : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StackedBar : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StackedBar100 : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StackedColumn : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StackedColumn100 : data:seq<#seq<'a1 * 'a2>> * ?Name:string * ?Title:string * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string * ?StackedGroupName:string -> ChartTypes.GenericChart (requires 'a1 :> key and 'a2 :> value)
    static member StepLine : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member StepLine : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
    static member Stock : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.StockChart
    static member Stock : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.StockChart
    static member ThreeLineBreak : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.ThreeLineBreakChart
    static member ThreeLineBreak : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.ThreeLineBreakChart
  end

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

More information

Link: http://fssnip.net/ls
Posted: 2 months ago
Author: Tomas Petricek (website)
Tags: series, charting, sums