Curve difference by sum-of-squares

An operator to compare two curves by the sum-of-squares-of-differences method.

Copy Source
Copy Link
Tools:
 1: let (-~) a1 a2 = 
 2:     let sq x = x * x
 3:     Array.fold2 (fun acc n1 n2 -> acc + sq (n1 - n2) ) 0. a1 a2
 4: 
 5: // Examples:
 6: 
 7: let ones = [|1.; 1.; 1.|]
 8: let twos = [|2.; 2.; 2.|]
 9: let oneTwoThree = [|1.; 2.; 3.|]
10: let oneishTwoishThreeish =  [|1.1; 1.9; 2.99999|]
11: let big = [|100.; 101.; 100.|]
12: 
13: // Simple comparisons:
14: let onesVersusOnes = ones -~ ones // 0.0
15: let onesVersusTwos = ones -~ twos // 1.0
16: let twosVersusOnes = twos -~ ones // 0.0
17: let sameish = oneTwoThree -~ oneishTwoishThreeish // 1e-10
18: let veryDifferent = ones -~ big // 9801.0
19: 
20: // Order a list of arrays based on their similarity to a 'seed' array:
21: let ordered =
22:     let seed = twos
23:     let list = [oneishTwoishThreeish; big; ones; oneTwoThree]
24:     list
25:     |> List.sortBy (fun arr -> arr -~ seed)
26: 
27: // [[|1.1; 1.9; 2.99999|]; [|1.0; 2.0; 3.0|]; [|1.0; 1.0; 1.0|]; [|100.0; 101.0; 100.0|]]
28: 
29: 
val a1 : float []

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val a2 : float []

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val sq : (float -> float)
val x : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
module Array

from Microsoft.FSharp.Collections
val fold2 : ('State -> 'T1 -> 'T2 -> 'State) -> 'State -> 'T1 [] -> 'T2 [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold2
val acc : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val n1 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val n2 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val ones : float []

Full name: Snippet.ones

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val twos : float []

Full name: Snippet.twos

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val oneTwoThree : float []

Full name: Snippet.oneTwoThree

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val oneishTwoishThreeish : float []

Full name: Snippet.oneishTwoishThreeish

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val big : float []

Full name: Snippet.big

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
val onesVersusOnes : float

Full name: Snippet.onesVersusOnes

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val onesVersusTwos : float

Full name: Snippet.onesVersusTwos

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val twosVersusOnes : float

Full name: Snippet.twosVersusOnes

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val sameish : float

Full name: Snippet.sameish

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val veryDifferent : float

Full name: Snippet.veryDifferent

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val ordered : float [] list

Full name: Snippet.ordered

  type: float [] list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<float []>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<float []>
  implements: System.Collections.IEnumerable
val seed : float []

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array
Multiple items
val list : float [] list

  type: float [] list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<float []>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<float []>
  implements: System.Collections.IEnumerable


--------------------

type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>

  type: 'T list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------

type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface System.Collections.IEnumerable
    interface System.Collections.Generic.IEnumerable<'T>
    member Head : 'T
    member IsEmpty : bool
    member Item : index:int -> 'T with get
    member Length : int
    member Tail : 'T list
    static member Cons : head:'T * tail:'T list -> 'T list
    static member Empty : 'T list
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val sortBy : ('T -> 'Key) -> 'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sortBy
val arr : float []

  type: float []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<float>
  implements: System.Collections.Generic.ICollection<float>
  implements: seq<float>
  implements: System.Collections.IEnumerable
  inherits: System.Array

More information

Link: http://fssnip.net/al
Posted: 3 months ago
Author: Kit Eason (website)
Tags: curve operator fold fold2