Jump to content

Cumulative standard deviation: Difference between revisions

Add Axiom task
m (→‎{{header|Perl 6}}: cosmetic change)
(Add Axiom task)
Line 234:
sum += x, sqr += x*x, n++ ; update state
Return sqrt((sqr-sum*sum/n)/n)
}</lang>
=={{header|Axiom}}==
<lang Axiom>)abbrev package TESTD TestDomain
TestDomain(T : Join(Field,RadicalCategory)): Exports == Implementation where
R ==> Record(n : Integer, sum : T, ssq : T)
Exports == AbelianMonoid with
_+ : (%,T) -> %
_+ : (T,%) -> %
sd : % -> T
Implementation ==> R add
Rep := R -- similar representation and implementation
obj : %
0 == [0,0,0]
obj + (obj2:%) == [obj.n + obj2.n, obj.sum + obj2.sum, obj.ssq + obj2.ssq]
obj + (x:T) == obj + ([1, x, x*x] @ %)
(x:T) + obj == obj + x
sd obj ==
mean : T := obj.sum / (obj.n::T)
sqrt(obj.ssq / (obj.n::T) - mean*mean)</lang>This can be called using:<lang Axiom>T ==> Expression Integer
D ==> TestDomain(T)
items := [2,4,4,4,5,5,7,9+x] :: List T;
map(sd, scan(+, items, 0$D))
+---------------+
+-+ +-+ +-+ +-+ | 2
2\|2 \|3 2\|6 4\|6 \|7x + 64x + 256
(1) [0,1,-----,----,-----,1,-----,------------------]
3 2 5 7 8
Type: List(Expression(Integer))
eval subst(%.8,x=0)
 
(2) 2
Type: Expression(Integer)</lang>
 
=={{header|C}}==
136

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.