-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
210 lines (182 loc) · 3.62 KB
/
stat.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ops
import (
"math/big"
"github.com/blackchip-org/zc/v5/pkg/zc"
)
/*
oper average
func Average BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
func - Complex* -- Complex
alias avg
title Average
desc
The average of all items on the stack.
end
example
0 100 25 75 avg -- 50
end
*/
func Average(c zc.Calc) {
n := c.StackLen()
if n > 0 {
c.Eval("sum %v div", n)
}
}
/*
oper factorial
func Factorial n:BigInt -- BigInt
alias fact
title Factorial
desc
The product of all positive integers less than or equal to *n*. If *n* is
negative, an invalid argument error is displayed.
end
example
c 3 fact -- 6
c 10 fact -- 3628800
end
*/
func Factorial(c zc.Calc) {
zero := big.NewInt(0)
one := big.NewInt(1)
n := zc.PopBigInt(c)
switch n.Cmp(zero) {
case -1:
zc.ErrInvalidArgs(c, "cannot be negative")
case 0:
zc.PushBigInt(c, one)
case 1:
acc := big.NewInt(1)
i := big.NewInt(1)
for i.Cmp(n) <= 0 {
acc.Mul(acc, i)
i.Add(i, one)
}
zc.PushBigInt(c, acc)
}
}
/*
oper prod
func - BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
func - Complex* -- Complex
macro [mul] fold
title Product
desc
The product of all items on the stack.
end
example
1 2 3 4 5 -- 1 | 2 | 3 | 4 | 5
prod -- 120
end
*/
/*
oper standard-deviation-pop
func StandardDeviationPop BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
alias stdev-p
macro var-p sqrt
title Population standard deviation
desc
Standard deviation of the stack where it contains the entire population
end
example
2 4 4 4 5 5 7 9 -- 2 | 4 | 4 | 4 | 5 | 5 | 7 | 9
stdev-p -- 2
end
*/
/*
oper standard-deviation-samp
func StandardDeviationSamp BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
alias stdev-s
macro var-s sqrt
title Sample standard deviation
desc
Standard deviation of the stack where it contains a sample of the population
end
example
2 4 4 4 5 5 7 9 -- 2 | 4 | 4 | 4 | 5 | 5 | 7 | 9
stdev-s 2 round -- 2.14
end
*/
/*
oper sum
func - BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
func - Complex* -- Complex
macro [add] fold
title Summation
desc
The sum of all items on the stack.
end
example
1 2 3 4 5 -- 1 | 2 | 3 | 4 | 5
sum -- 15
end
*/
/*
oper variance-pop
func VariancePop BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
alias var-p
title Population variance
desc
Variance of the stack where it contains the entire population
end
example
2 4 4 4 5 5 7 9 -- 2 | 4 | 4 | 4 | 5 | 5 | 7 | 9
var-p -- 4
end
*/
func VariancePop(c zc.Calc) {
Variance(c, 0)
}
/*
oper variance-samp
func VarianceSamp BigInt* -- BigInt
func - Decimal* -- Decimal
func - Float* -- Float
func - Rational* -- Rational
alias var-s
title Sample variance
desc
Variance of the stack where it contains a sample of the population
end
example
2 4 4 4 5 5 7 9 -- 2 | 4 | 4 | 4 | 5 | 5 | 7 | 9
var-s 2 round -- 4.57
end
*/
func VarianceSamp(c zc.Calc) {
Variance(c, -1)
}
func Variance(c zc.Calc, nadj int) {
n := c.StackLen()
if n == 0 {
return
}
data := c.Stack()
if err := c.Eval("average"); err != nil {
return
}
mean := zc.PopInt(c)
c.SetStack(data)
zc.PushInt(c, mean)
c.MustEval("[sub square] [map] 2 apply sum")
zc.PushInt(c, n+nadj)
c.MustEval("div")
}