mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: added statistics library
This commit is contained in:
parent
06daa30432
commit
3239c77aeb
@ -125,6 +125,9 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
|
||||
, templateDeleteArray
|
||||
, templateCopyArray
|
||||
, templateStrArray
|
||||
, templateSort
|
||||
, templateIndexOf
|
||||
, templateElemCount
|
||||
]
|
||||
|
||||
startingGlobalEnv :: Env
|
||||
@ -160,6 +163,7 @@ preludeModules carpDir = map (\s -> carpDir ++ "/core/" ++ s ++ ".carp") [ "Macr
|
||||
, "Vector"
|
||||
, "Geometry"
|
||||
, "Test"
|
||||
, "Statistics"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
|
@ -6,8 +6,8 @@
|
||||
(for [i 0 (count xs)]
|
||||
(set! &total (f &total (nth xs i))))
|
||||
total)))
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
;; BUGS! These function definitions will not create the required typedef for Array, for instance:
|
||||
;; typedef Array Array__int;
|
||||
|
119
core/Statistics.carp
Normal file
119
core/Statistics.carp
Normal file
@ -0,0 +1,119 @@
|
||||
(use Int)
|
||||
(use Double)
|
||||
|
||||
(defmodule Statistics
|
||||
(defn sorter [a b]
|
||||
(to-int (- @a @b)))
|
||||
|
||||
(defn sum [data]
|
||||
(if (= 0 (Array.count data))
|
||||
0.0
|
||||
(let [total @(Array.nth data 0)]
|
||||
(do
|
||||
(for [i 1 (Array.count data)]
|
||||
(set! &total (+ total @(Array.nth data i))))
|
||||
total))))
|
||||
|
||||
(defn mean [data]
|
||||
(/ (sum data) (from-int (Array.count data))))
|
||||
|
||||
(defn min [a]
|
||||
(let [m (Double.copy (Array.nth a 0))]
|
||||
(do
|
||||
(for [i 1 (Array.count a)]
|
||||
(let [el (Double.copy (Array.nth a i))]
|
||||
(if (Double.< el m)
|
||||
(set! &m el)
|
||||
())))
|
||||
m)))
|
||||
|
||||
(defn max [a]
|
||||
(let [m (Double.copy (Array.nth a 0))]
|
||||
(do
|
||||
(for [i 1 (Array.count a)]
|
||||
(let [el (Double.copy (Array.nth a i))]
|
||||
(if (Double.> el m)
|
||||
(set! &m el)
|
||||
())))
|
||||
m)))
|
||||
|
||||
(defn _pp [a mean]
|
||||
(let [sum 0.0]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(let [tmp (Double.- (Double.copy (Array.nth a i)) mean)]
|
||||
(set! &sum (Double.* tmp tmp))))
|
||||
sum)))
|
||||
|
||||
(defn _xx [a mean]
|
||||
(let [sum 0.0]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(set! &sum (Double.- (Double.copy (Array.nth a i)) mean)))
|
||||
sum)))
|
||||
|
||||
(defn _ss [data]
|
||||
(let [m (mean data)
|
||||
tmp (_xx data m)]
|
||||
(Double.- (_pp data m)
|
||||
(Double./ (Double.* tmp tmp)
|
||||
(Double.from-int (Array.count data))))))
|
||||
|
||||
(defn median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
(if (= (mod n 2) 1)
|
||||
@(Array.nth data (/ n 2))
|
||||
(let [mid (/ n 2)]
|
||||
(/ (+ (the Double @(Array.nth data (dec mid)))
|
||||
@(Array.nth data mid))
|
||||
2.0))))))
|
||||
|
||||
(defn low-median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
(if (= (mod n 2) 1)
|
||||
@(Array.nth data (/ n 2))
|
||||
@(Array.nth data (dec (/ n 2)))))))
|
||||
|
||||
(defn high-median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
@(Array.nth data (/ n 2)))))
|
||||
|
||||
(defn grouped-median [data interval]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
(if (= n 1)
|
||||
@(Array.nth data 0)
|
||||
(let [x @(Array.nth data (/ n 2))
|
||||
l (- x (/ (from-int interval) 2.0))
|
||||
cf (Array.index-of data x)
|
||||
f (Array.element-count data x)]
|
||||
(+ l (/ (* (from-int interval) (- (/ (from-int n) 2.0) (from-int cf)))
|
||||
(from-int f))))))))
|
||||
|
||||
(defn variance [data]
|
||||
(let [n (Array.count data)
|
||||
ss (_ss data)]
|
||||
(/ ss (from-int (dec n)))))
|
||||
|
||||
(defn pvariance [data]
|
||||
(let [n (Array.count data)
|
||||
ss (_ss data)]
|
||||
(/ ss (from-int n))))
|
||||
|
||||
(defn stdev [data]
|
||||
(Double.sqrt (variance data)))
|
||||
|
||||
(defn pstdev [data]
|
||||
(Double.sqrt (pvariance data)))
|
||||
)
|
@ -186,6 +186,53 @@ templateNth =
|
||||
,"}"])
|
||||
(\(FuncTy [arrayType, _] _) -> [defineArrayTypeAlias arrayType])
|
||||
|
||||
templateSort :: (String, Binder)
|
||||
templateSort =
|
||||
let t = VarTy "t"
|
||||
in defineTemplate
|
||||
(SymPath ["Array"] "sort")
|
||||
(FuncTy [RefTy (StructTy "Array" [t]), FuncTy [RefTy t, RefTy t] IntTy] (RefTy (StructTy "Array" [t])))
|
||||
(toTemplate "Array* $NAME (Array *a, $(Fn [(Ref t), (Ref t)] Int) f)")
|
||||
(toTemplate $ unlines ["$DECL {"
|
||||
," qsort(a->data, a->len, sizeof($t), (int(*)(const void*, const void*))f);"
|
||||
," return a;"
|
||||
,"}"])
|
||||
(\(FuncTy [arrayType, sortType] _) -> [defineFunctionTypeAlias sortType,
|
||||
defineArrayTypeAlias arrayType])
|
||||
|
||||
templateIndexOf :: (String, Binder)
|
||||
templateIndexOf =
|
||||
let t = VarTy "t"
|
||||
in defineTemplate
|
||||
(SymPath ["Array"] "index-of")
|
||||
(FuncTy [RefTy (StructTy "Array" [t]), t] IntTy)
|
||||
(toTemplate "int $NAME (Array *a, $t elem)")
|
||||
(toTemplate $ unlines ["$DECL {"
|
||||
," int i;"
|
||||
," for (i = 0; i < a->len; i++) {"
|
||||
," if ((($t*)a->data)[i] == elem) return i;"
|
||||
," }"
|
||||
," return -1;"
|
||||
,"}"])
|
||||
(\(FuncTy [arrayType, _] _) -> [defineArrayTypeAlias arrayType])
|
||||
|
||||
templateElemCount :: (String, Binder)
|
||||
templateElemCount =
|
||||
let t = VarTy "t"
|
||||
in defineTemplate
|
||||
(SymPath ["Array"] "element-count")
|
||||
(FuncTy [RefTy (StructTy "Array" [t]), t] IntTy)
|
||||
(toTemplate "int $NAME (Array *a, $t elem)")
|
||||
(toTemplate $ unlines ["$DECL {"
|
||||
," int i;"
|
||||
," int count = 0;"
|
||||
," for (i = 0; i < a->len; i++) {"
|
||||
," if ((($t*)a->data)[i] == elem) count++;"
|
||||
," }"
|
||||
," return count;"
|
||||
,"}"])
|
||||
(\(FuncTy [arrayType, _] _) -> [defineArrayTypeAlias arrayType])
|
||||
|
||||
templateReplicate :: (String, Binder)
|
||||
templateReplicate = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = SymPath ["Array"] "replicate"
|
||||
|
Loading…
Reference in New Issue
Block a user