0 people like it.
Like the snippet!
Fuzzy calculus
Simple implementation of interval and fuzzy calculus.
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:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
|
namespace FSharp.Fuzzy
open System
type Interval =
{
a:decimal
b:decimal
}
member this.Middle = (this.a+this.b)/2m
static member operation f (x : Interval, y : Interval) =
let list = [f x.a y.a; f x.a y.b; f x.b y.a; f x.b y.b]
let min (xs : decimal seq) =
Seq.fold (fun (acc : decimal) x -> Math.Min(acc,x)) Decimal.MaxValue xs
let max (xs : decimal seq) = Seq.fold (fun (acc : decimal) x -> Math.Max(acc,x)) Decimal.MinValue xs
{ a = min list; b = max list}
static member (*) (x : Interval, y : Interval) = Interval.operation (fun x y-> x*y) (x,y)
static member zeroLength (x : decimal) = {a=x; b=x}
static member Zero = Interval.zeroLength 0m
static member (/) (x : Interval, y : Interval) =
if y.a < 0m && y.b > 0m then failwith "Divider cannot contain zero."
Interval.operation (fun x y-> x/y) (x,y)
static member (+) (x : Interval, y : Interval) = { a = x.a + y.a; b = x.b + y.b}
static member (-) (x : Interval, y : Interval) = { a = x.a - y.b; b = x.b - y.a}
static member (*) (x : Interval, y : decimal) = x * Interval.zeroLength(y)
static member (/) (x : Interval, y : decimal) = x / Interval.zeroLength(y)
static member (+) (x : Interval, y : decimal) = x + Interval.zeroLength(y)
static member (-) (x : Interval, y : decimal) = x - Interval.zeroLength(y)
static member (*) (x : decimal, y : Interval) = Interval.zeroLength(x) * y
static member (/) (x : decimal, y : Interval) = Interval.zeroLength(x) / y
static member (+) (x : decimal, y : Interval) = Interval.zeroLength(x) + y
static member (-) (x : decimal, y : Interval) = Interval.zeroLength(x) - y
static member pow (x : Interval, p : double) = { a = double x.a ** p |> decimal; b = double x.b ** p |> decimal }
static member distance (x : Interval, y : Interval) = Math.Abs(y.Middle - x.Middle)
[<StructuredFormatDisplayAttribute("{alphaCuts}")>]
type Fuzzy(a : Interval seq) =
let alphas = a |> Array.ofSeq
member this.alphaCuts with get() = alphas |> Array.ofSeq
member this.Bottom with get() = alphas.[0]
member this.Top with get() = alphas.[alphas.Length - 1]
override this.ToString() = sprintf "%A" alphas
override x.Equals(yobj) =
match yobj with
| :? Fuzzy as y -> x.alphaCuts = y.alphaCuts
| _ -> false
override x.GetHashCode() = hash x.alphaCuts
interface System.IComparable with
member x.CompareTo yobj =
match yobj with
| :? Fuzzy as y -> compare x.alphaCuts y.alphaCuts
| _ -> invalidArg "yobj" "cannot compare values of different types"
static member Zero = Fuzzy(Array.create 11 Interval.Zero)
static member operation f (a:Fuzzy) (b:Fuzzy) = Fuzzy(Seq.map2 f a.alphaCuts b.alphaCuts )
static member map f (a:Fuzzy) = Fuzzy(Seq.map f a.alphaCuts )
static member (*) (x : Fuzzy, y : Fuzzy) = Fuzzy.operation (fun a b-> a*b) x y
static member (/) (x : Fuzzy, y : Fuzzy) = Fuzzy.operation (fun a b-> a/b) x y
static member (+) (x : Fuzzy, y : Fuzzy) = Fuzzy.operation (fun a b-> a+b) x y
static member (-) (x : Fuzzy, y : Fuzzy) = Fuzzy.operation (fun a b-> a-b) x y
static member (*) (x : Fuzzy, y : decimal) = Fuzzy.map (fun a-> a*y) x
static member (/) (x : Fuzzy, y : decimal) = Fuzzy.map (fun a-> a/y) x
static member (+) (x : Fuzzy, y : decimal) = Fuzzy.map (fun a-> a+y) x
static member (-) (x : Fuzzy, y : decimal) = Fuzzy.map (fun a-> a-y) x
static member (*) (x : decimal, y : Fuzzy) = y * x
static member (/) (x : decimal, y : Fuzzy) = Fuzzy.map (fun a-> x/a) y
static member (+) (x : decimal, y : Fuzzy) = y + x
static member (-) (x : decimal, y : Fuzzy) = Fuzzy.map (fun a-> x-a) y
static member pow (x : Fuzzy, p : double) = Fuzzy.map (fun a-> Interval.pow(a, p)) x
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<AutoOpen>]
module Fuzzy =
let alpha total level = if level = 0 then 0m else 1.m / decimal (total - 1) * decimal level
let trapezoid levels (a,b,c,d) =
if a>b || b>c || c>d then failwith "expected a>=b>=c>=d"
let maxIndex = levels - 1
let step = 1.m / (decimal maxIndex)
Fuzzy(seq { for i in 0..maxIndex -> { a = a+(b-a)*step*decimal i; b = c+(d-c)*step*decimal (maxIndex-i) } } )
let interval(a,b,c,d) = trapezoid 11 (a,b,c,d)
let number(a,b,c) = interval(a,b,b,c)
let point(a) = number(a,a,a)
let binary f (a: Fuzzy) (b: Fuzzy) =
assert (a.alphaCuts.Length = b.alphaCuts.Length)
let length = a.alphaCuts.Length
let result =
Seq.zip a.alphaCuts b.alphaCuts
|> Seq.mapi (fun i pair -> alpha a.alphaCuts.Length i * f pair )
|> Seq.sum
result / (decimal a.alphaCuts.Length / 2m)
let unary f (a: Fuzzy) =
let result =
a.alphaCuts
|> Seq.mapi (fun i b -> alpha a.alphaCuts.Length i * f b )
|> Seq.sum
result / (decimal a.alphaCuts.Length / 2m)
let distance a b = binary Interval.distance a b
let width a = unary (fun i->i.b - i.a) a
let risk a = unary (fun i->2m * (i.b - i.a)/(i.a+i.b)) a
let plot (a : Fuzzy) =
let length = a.alphaCuts.Length - 1
seq { for i in 0..length -> a.alphaCuts.[i].a, alpha a.alphaCuts.Length i
for i in length .. -1 .. 0 -> a.alphaCuts.[i].b, alpha a.alphaCuts.Length i } |> Array.ofSeq
|
namespace Microsoft.FSharp
Multiple items
module Fuzzy
from FSharp.Fuzzy
--------------------
type Fuzzy =
interface IComparable
new : a:seq<Interval> -> Fuzzy
override Equals : yobj:obj -> bool
override GetHashCode : unit -> int
override ToString : unit -> string
member Bottom : Interval
member Top : Interval
member alphaCuts : Interval []
static member Zero : Fuzzy
static member map : f:(Interval -> Interval) -> a:Fuzzy -> Fuzzy
...
Full name: FSharp.Fuzzy.Fuzzy
--------------------
new : a:seq<Interval> -> Fuzzy
namespace System
type Interval =
{a: decimal;
b: decimal;}
member Middle : decimal
static member distance : x:Interval * y:Interval -> decimal
static member Zero : Interval
static member ( + ) : x:Interval * y:Interval -> Interval
static member ( + ) : x:Interval * y:decimal -> Interval
static member ( + ) : x:decimal * y:Interval -> Interval
static member ( / ) : x:Interval * y:Interval -> Interval
static member ( / ) : x:Interval * y:decimal -> Interval
static member ( / ) : x:decimal * y:Interval -> Interval
static member ( * ) : x:Interval * y:Interval -> Interval
static member ( * ) : x:Interval * y:decimal -> Interval
static member ( * ) : x:decimal * y:Interval -> Interval
static member ( - ) : x:Interval * y:Interval -> Interval
static member ( - ) : x:Interval * y:decimal -> Interval
static member ( - ) : x:decimal * y:Interval -> Interval
static member operation : f:(decimal -> decimal -> decimal) -> x:Interval * y:Interval -> Interval
static member pow : x:Interval * p:double -> Interval
static member zeroLength : x:decimal -> Interval
Full name: FSharp.Fuzzy.Interval
Interval.a: decimal
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.decimal
--------------------
type decimal = Decimal
Full name: Microsoft.FSharp.Core.decimal
--------------------
type decimal<'Measure> = decimal
Full name: Microsoft.FSharp.Core.decimal<_>
Interval.b: decimal
val this : Interval
member Interval.Middle : decimal
Full name: FSharp.Fuzzy.Interval.Middle
static member Interval.operation : f:(decimal -> decimal -> decimal) -> x:Interval * y:Interval -> Interval
Full name: FSharp.Fuzzy.Interval.operation
val f : (decimal -> decimal -> decimal)
val x : Interval
val y : Interval
Multiple items
val list : decimal list
--------------------
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val min : (seq<decimal> -> decimal)
val xs : seq<decimal>
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<_>
module Seq
from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
Full name: Microsoft.FSharp.Collections.Seq.fold
val acc : decimal
val x : decimal
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.Min(val1: decimal, val2: decimal) : decimal
(+0 other overloads)
Math.Min(val1: float, val2: float) : float
(+0 other overloads)
Math.Min(val1: float32, val2: float32) : float32
(+0 other overloads)
Math.Min(val1: uint64, val2: uint64) : uint64
(+0 other overloads)
Math.Min(val1: int64, val2: int64) : int64
(+0 other overloads)
Math.Min(val1: uint32, val2: uint32) : uint32
(+0 other overloads)
Math.Min(val1: int, val2: int) : int
(+0 other overloads)
Math.Min(val1: uint16, val2: uint16) : uint16
(+0 other overloads)
Math.Min(val1: int16, val2: int16) : int16
(+0 other overloads)
Math.Min(val1: byte, val2: byte) : byte
(+0 other overloads)
Multiple items
type Decimal =
struct
new : value:int -> decimal + 7 overloads
member CompareTo : value:obj -> int + 1 overload
member Equals : value:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val Zero : decimal
static val One : decimal
static val MinusOne : decimal
static val MaxValue : decimal
...
end
Full name: System.Decimal
--------------------
Decimal()
Decimal(value: int) : unit
Decimal(value: uint32) : unit
Decimal(value: int64) : unit
Decimal(value: uint64) : unit
Decimal(value: float32) : unit
Decimal(value: float) : unit
Decimal(bits: int []) : unit
Decimal(lo: int, mid: int, hi: int, isNegative: bool, scale: byte) : unit
field decimal.MaxValue
val max : (seq<decimal> -> decimal)
Math.Max(val1: decimal, val2: decimal) : decimal
(+0 other overloads)
Math.Max(val1: float, val2: float) : float
(+0 other overloads)
Math.Max(val1: float32, val2: float32) : float32
(+0 other overloads)
Math.Max(val1: uint64, val2: uint64) : uint64
(+0 other overloads)
Math.Max(val1: int64, val2: int64) : int64
(+0 other overloads)
Math.Max(val1: uint32, val2: uint32) : uint32
(+0 other overloads)
Math.Max(val1: int, val2: int) : int
(+0 other overloads)
Math.Max(val1: uint16, val2: uint16) : uint16
(+0 other overloads)
Math.Max(val1: int16, val2: int16) : int16
(+0 other overloads)
Math.Max(val1: byte, val2: byte) : byte
(+0 other overloads)
field decimal.MinValue
static member Interval.operation : f:(decimal -> decimal -> decimal) -> x:Interval * y:Interval -> Interval
val y : decimal
static member Interval.zeroLength : x:decimal -> Interval
Full name: FSharp.Fuzzy.Interval.zeroLength
static member Interval.Zero : Interval
Full name: FSharp.Fuzzy.Interval.Zero
static member Interval.zeroLength : x:decimal -> Interval
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
static member Interval.pow : x:Interval * p:double -> Interval
Full name: FSharp.Fuzzy.Interval.pow
val p : double
Multiple items
val double : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double
--------------------
type double = Double
Full name: Microsoft.FSharp.Core.double
static member Interval.distance : x:Interval * y:Interval -> decimal
Full name: FSharp.Fuzzy.Interval.distance
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
property Interval.Middle: decimal
Multiple items
type StructuredFormatDisplayAttribute =
inherit Attribute
new : value:string -> StructuredFormatDisplayAttribute
member Value : string
Full name: Microsoft.FSharp.Core.StructuredFormatDisplayAttribute
--------------------
new : value:string -> StructuredFormatDisplayAttribute
Multiple items
type Fuzzy =
interface IComparable
new : a:seq<Interval> -> Fuzzy
override Equals : yobj:obj -> bool
override GetHashCode : unit -> int
override ToString : unit -> string
member Bottom : Interval
member Top : Interval
member alphaCuts : Interval []
static member Zero : Fuzzy
static member map : f:(Interval -> Interval) -> a:Fuzzy -> Fuzzy
...
Full name: FSharp.Fuzzy.Fuzzy
--------------------
new : a:seq<Interval> -> Fuzzy
val a : seq<Interval>
val alphas : Interval []
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
val ofSeq : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Array.ofSeq
val this : Fuzzy
member Fuzzy.alphaCuts : Interval []
Full name: FSharp.Fuzzy.Fuzzy.alphaCuts
member Fuzzy.Bottom : Interval
Full name: FSharp.Fuzzy.Fuzzy.Bottom
member Fuzzy.Top : Interval
Full name: FSharp.Fuzzy.Fuzzy.Top
property Array.Length: int
override Fuzzy.ToString : unit -> string
Full name: FSharp.Fuzzy.Fuzzy.ToString
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val x : Fuzzy
override Fuzzy.Equals : yobj:obj -> bool
Full name: FSharp.Fuzzy.Fuzzy.Equals
val yobj : obj
val y : Fuzzy
property Fuzzy.alphaCuts: Interval []
override Fuzzy.GetHashCode : unit -> int
Full name: FSharp.Fuzzy.Fuzzy.GetHashCode
val hash : obj:'T -> int (requires equality)
Full name: Microsoft.FSharp.Core.Operators.hash
Multiple items
type IComparable<'T> =
member CompareTo : other:'T -> int
Full name: System.IComparable<_>
--------------------
type IComparable =
member CompareTo : obj:obj -> int
Full name: System.IComparable
override Fuzzy.CompareTo : yobj:obj -> int
Full name: FSharp.Fuzzy.Fuzzy.CompareTo
val compare : e1:'T -> e2:'T -> int (requires comparison)
Full name: Microsoft.FSharp.Core.Operators.compare
val invalidArg : argumentName:string -> message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.invalidArg
static member Fuzzy.Zero : Fuzzy
Full name: FSharp.Fuzzy.Fuzzy.Zero
val create : count:int -> value:'T -> 'T []
Full name: Microsoft.FSharp.Collections.Array.create
property Interval.Zero: Interval
static member Fuzzy.operation : f:(Interval -> Interval -> Interval) -> a:Fuzzy -> b:Fuzzy -> Fuzzy
Full name: FSharp.Fuzzy.Fuzzy.operation
val f : (Interval -> Interval -> Interval)
val a : Fuzzy
val b : Fuzzy
val map2 : mapping:('T1 -> 'T2 -> 'U) -> source1:seq<'T1> -> source2:seq<'T2> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map2
static member Fuzzy.map : f:(Interval -> Interval) -> a:Fuzzy -> Fuzzy
Full name: FSharp.Fuzzy.Fuzzy.map
val f : (Interval -> Interval)
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
static member Fuzzy.operation : f:(Interval -> Interval -> Interval) -> a:Fuzzy -> b:Fuzzy -> Fuzzy
val a : Interval
val b : Interval
static member Fuzzy.map : f:(Interval -> Interval) -> a:Fuzzy -> Fuzzy
static member Fuzzy.pow : x:Fuzzy * p:double -> Fuzzy
Full name: FSharp.Fuzzy.Fuzzy.pow
static member Interval.pow : x:Interval * p:double -> Interval
Multiple items
type CompilationRepresentationAttribute =
inherit Attribute
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
member Flags : CompilationRepresentationFlags
Full name: Microsoft.FSharp.Core.CompilationRepresentationAttribute
--------------------
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
type CompilationRepresentationFlags =
| None = 0
| Static = 1
| Instance = 2
| ModuleSuffix = 4
| UseNullAsTrueValue = 8
| Event = 16
Full name: Microsoft.FSharp.Core.CompilationRepresentationFlags
CompilationRepresentationFlags.ModuleSuffix: CompilationRepresentationFlags = 4
Multiple items
type AutoOpenAttribute =
inherit Attribute
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
member Path : string
Full name: Microsoft.FSharp.Core.AutoOpenAttribute
--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
val alpha : total:int -> level:int -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.alpha
val total : int
val level : int
val trapezoid : levels:int -> a:decimal * b:decimal * c:decimal * d:decimal -> Fuzzy
Full name: FSharp.Fuzzy.FuzzyModule.trapezoid
val levels : int
val a : decimal
val b : decimal
val c : decimal
val d : decimal
val maxIndex : int
val step : decimal
val i : int
val interval : a:decimal * b:decimal * c:decimal * d:decimal -> Fuzzy
Full name: FSharp.Fuzzy.FuzzyModule.interval
val number : a:decimal * b:decimal * c:decimal -> Fuzzy
Full name: FSharp.Fuzzy.FuzzyModule.number
val point : a:decimal -> Fuzzy
Full name: FSharp.Fuzzy.FuzzyModule.point
val binary : f:(Interval * Interval -> decimal) -> a:Fuzzy -> b:Fuzzy -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.binary
val f : (Interval * Interval -> decimal)
val length : int
val result : decimal
val zip : source1:seq<'T1> -> source2:seq<'T2> -> seq<'T1 * 'T2>
Full name: Microsoft.FSharp.Collections.Seq.zip
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.mapi
val pair : Interval * Interval
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.Seq.sum
val unary : f:(Interval -> decimal) -> a:Fuzzy -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.unary
val f : (Interval -> decimal)
val distance : a:Fuzzy -> b:Fuzzy -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.distance
static member Interval.distance : x:Interval * y:Interval -> decimal
val width : a:Fuzzy -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.width
val i : Interval
val risk : a:Fuzzy -> decimal
Full name: FSharp.Fuzzy.FuzzyModule.risk
val plot : a:Fuzzy -> (decimal * decimal) []
Full name: FSharp.Fuzzy.FuzzyModule.plot
More information