0 people like it.

Earth Similarity Index

Calculating the Earth Similarity Index of a planet. http://phl.upr.edu/projects/earth-similarity-index-esi

 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: 
module EarthSimilarityIndex

// A planet:
type Planet = {
                mass : float; 
                radius : float; 
                density : float; 
                g : float; 
                ve : float; 
                a : float; 
                Tsurf : float; 
                Teq : float
              }

// Some basic planetary numbers:
let earth = {
                mass = 1.0; 
                radius = 1.0; 
                density = 1.0; 
                g = 1.0; 
                ve = 1.0; 
                a = 1.0; 
                Tsurf = 288.; 
                Teq = 254.
             }

let mars = {
                mass = 0.107; 
                radius = 0.53; 
                density = 0.71; 
                g = 0.38; 
                ve = 0.45; 
                a = 1.52; 
                Tsurf = 227.; 
                Teq = 210.
            }

let mercury = {
                mass = 0.0553; 
                radius = 0.38; 
                density = 0.98; 
                g = 0.38; 
                ve = 0.38; 
                a = 0.39; 
                Tsurf = 440.; 
                Teq = 434.
              }

// Same numbers but as arrays:
let earthStats = [|earth.mass; earth.radius; earth.density; earth.g; earth.ve; earth.a; earth.Tsurf; earth.Teq|]
let marsStats = [|mars.mass; mars.radius; mars.density; mars.g; mars.ve; mars.a; mars.Tsurf; mars.Teq|]
let mercuryStats = [|mercury.mass; mercury.radius; mercury.density; mercury.g; mercury.ve; mercury.a; mercury.Tsurf; mercury.Teq|]

// Weight all numbers equally:
let weights = [|1.; 1.; 1.; 1.; 1.; 1.; 1.; 1.|]

// As Array.fold but applying the given function to each element, and starting with the initial value x:
let foldBy f x a =
    a |> Array.fold (fun acc elem -> f acc elem) x

// Multiply up the values in an array:
let pi a = a |> foldBy (*) 1.

// Calculate the similarity index of two planets:
let similarityIndex p1 p2 w = 
    let n = p1 |> Array.length |> float
    Array.zip3 p1 p2 w
    |> Array.map (fun (p1Val, p2Val, wVal) -> 
                    (1. - abs((p1Val - p2Val) / (p1Val + p2Val)) ) ** (wVal / n))
    |> pi 

// Calculate the Earth Similarity Index of any planet:
let ESI = similarityIndex earthStats

// Some similarity indices within the Solar System:
let earthEarth = ESI earthStats weights // 1.0
let earthMars = ESI marsStats weights // 0.6276230757
let earthMercury = ESI mercuryStats weights // 0.5239652331
module EarthSimilarityIndex
type Planet =
  {mass: float;
   radius: float;
   density: float;
   g: float;
   ve: float;
   a: float;
   Tsurf: float;
   Teq: float;}

Full name: EarthSimilarityIndex.Planet
Planet.mass: float
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<_>
Planet.radius: float
Planet.density: float
Planet.g: float
Planet.ve: float
Planet.a: float
Planet.Tsurf: float
Planet.Teq: float
val earth : Planet

Full name: EarthSimilarityIndex.earth
val mars : Planet

Full name: EarthSimilarityIndex.mars
val mercury : Planet

Full name: EarthSimilarityIndex.mercury
val earthStats : float []

Full name: EarthSimilarityIndex.earthStats
val marsStats : float []

Full name: EarthSimilarityIndex.marsStats
val mercuryStats : float []

Full name: EarthSimilarityIndex.mercuryStats
val weights : float []

Full name: EarthSimilarityIndex.weights
val foldBy : f:('a -> 'b -> 'a) -> x:'a -> a:'b [] -> 'a

Full name: EarthSimilarityIndex.foldBy
val f : ('a -> 'b -> 'a)
val x : 'a
val a : 'b []
module Array

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold
val acc : 'a
val elem : 'b
val pi : a:float [] -> float

Full name: EarthSimilarityIndex.pi
val a : float []
val similarityIndex : p1:float [] -> p2:float [] -> w:float [] -> float

Full name: EarthSimilarityIndex.similarityIndex
val p1 : float []
val p2 : float []
val w : float []
val n : float
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
val zip3 : array1:'T1 [] -> array2:'T2 [] -> array3:'T3 [] -> ('T1 * 'T2 * 'T3) []

Full name: Microsoft.FSharp.Collections.Array.zip3
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val p1Val : float
val p2Val : float
val wVal : float
val abs : value:'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
val ESI : (float [] -> float [] -> float)

Full name: EarthSimilarityIndex.ESI
val earthEarth : float

Full name: EarthSimilarityIndex.earthEarth
val earthMars : float

Full name: EarthSimilarityIndex.earthMars
val earthMercury : float

Full name: EarthSimilarityIndex.earthMercury
Raw view Test code New version

More information

Link:http://fssnip.net/dc
Posted:12 years ago
Author:Kit Eason
Tags: astronomy , partial application , fold