-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
261 lines (247 loc) · 7.15 KB
/
math.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package builtin_libs
import (
"math"
"github.com/samber/lo"
. "github.com/zgg-lang/zgg-go/runtime"
)
func libMath(*Context) ValueObject {
r := NewObject()
r.SetMember("abs", NewNativeFunction("abs", func(c *Context, _ Value, args []Value) Value {
var (
xi ValueInt
xf ValueFloat
xt int
)
EnsureFuncParams(c, "math.abs", args,
ArgRuleOneOf("x", []ValueType{TypeInt, TypeFloat}, []any{&xi, &xf}, &xt, nil, nil),
)
switch xt {
case 0: // int
x := xi.Value()
return NewInt(lo.Ternary(x >= 0, x, -x))
case 1: // float
return NewFloat(math.Abs(xf.Value()))
}
return nil
}), nil)
r.SetMember("gcd", NewNativeFunction("gcd", func(c *Context, _ Value, args []Value) Value {
return NewInt(mathGCD(c, "gcd", args))
}), nil)
r.SetMember("lcm", NewNativeFunction("lcm", func(c *Context, _ Value, args []Value) Value {
gcd := mathGCD(c, "lcm", args)
r := int64(1)
for i, a := range args {
r *= a.(ValueInt).Value()
if i == 0 {
r /= gcd
}
}
return NewInt(r)
}), nil)
r.SetMember("reduction", NewNativeFunction("reduction", func(c *Context, _ Value, args []Value) Value {
gcd := mathGCD(c, "reduction", args)
rv := NewArray(len(args))
for _, a := range args {
rv.PushBack(NewInt(a.(ValueInt).Value() / gcd))
}
return rv
}), nil)
r.SetMember("P", NewNativeFunction("P", func(c *Context, _ Value, args []Value) Value {
var an, ar ValueInt
EnsureFuncParams(c, "P", args, ArgRuleRequired("n", TypeInt, &an), ArgRuleRequired("r", TypeInt, &ar))
n, r := an.AsInt(), ar.AsInt()
if r <= 0 || n <= r {
c.RaiseRuntimeError("invalid arguments! n %d r %d", n, r)
}
rv := 1
for i := n - r + 1; i <= n; i++ {
rv *= i
}
return NewInt(int64(rv))
}, "n", "r"), nil)
r.SetMember("C", NewNativeFunction("C", func(c *Context, _ Value, args []Value) Value {
var an, ar ValueInt
EnsureFuncParams(c, "C", args, ArgRuleRequired("n", TypeInt, &an), ArgRuleRequired("r", TypeInt, &ar))
n, r := an.AsInt(), ar.AsInt()
if r <= 0 || n <= r {
c.RaiseRuntimeError("invalid arguments! n %d r %d", n, r)
}
if r+r < n {
r = n - r
}
rv := 1
for i := r + 1; i <= n; i++ {
rv *= i
}
for i := n - r; i > 1; i-- {
rv /= i
}
return NewInt(int64(rv))
}, "n", "r"), nil)
for name, fnv := range mathPortFuncs {
switch fn := fnv.(type) {
case func(float64) float64:
r.SetMember(name, mathPortFuncF_F(name, fn), nil)
case func(float64) (float64, float64):
r.SetMember(name, mathPortFuncF_FF(name, fn), nil)
case func(float64, float64) float64:
r.SetMember(name, mathPortFuncFF_F(name, fn), nil)
case func(float64, float64, float64) float64:
r.SetMember(name, mathPortFuncFFF_F(name, fn), nil)
}
}
for name, v := range mathPortConsts {
switch vv := v.(type) {
case float64:
r.SetMember(name, NewFloat(vv), nil)
}
}
return r
}
func mathPortFuncF_F(name string, fn func(float64) float64) ValueCallable {
return NewNativeFunction(name, func(ctx *Context, _ Value, args []Value) Value {
var a ValueFloat
EnsureFuncParams(ctx, "math."+name, args,
ArgRuleRequired("a", TypeFloat, &a),
)
return NewFloat(fn(a.Value()))
})
}
func mathPortFuncFF_F(name string, fn func(float64, float64) float64) ValueCallable {
return NewNativeFunction(name, func(ctx *Context, _ Value, args []Value) Value {
var a, b ValueFloat
EnsureFuncParams(ctx, "math."+name, args,
ArgRuleRequired("a", TypeFloat, &a),
ArgRuleRequired("b", TypeFloat, &b),
)
return NewFloat(fn(a.Value(), b.Value()))
})
}
func mathPortFuncFFF_F(name string, fn func(float64, float64, float64) float64) ValueCallable {
return NewNativeFunction(name, func(ctx *Context, _ Value, args []Value) Value {
var a, b, c ValueFloat
EnsureFuncParams(ctx, "math."+name, args,
ArgRuleRequired("a", TypeFloat, &a),
ArgRuleRequired("b", TypeFloat, &b),
ArgRuleRequired("c", TypeFloat, &c),
)
return NewFloat(fn(a.Value(), b.Value(), c.Value()))
})
}
func mathPortFuncF_FF(name string, fn func(float64) (float64, float64)) ValueCallable {
return NewNativeFunction(name, func(ctx *Context, _ Value, args []Value) Value {
var a ValueFloat
EnsureFuncParams(ctx, "math."+name, args,
ArgRuleRequired("a", TypeFloat, &a),
)
r1, r2 := fn(a.Value())
return NewArrayByValues(NewFloat(r1), NewFloat(r2))
})
}
var mathPortFuncs = map[string]any{
"acos": math.Acos,
"acosh": math.Acosh,
"asin": math.Asin,
"asinh": math.Asinh,
"atan": math.Atan,
"atan2": math.Atan2,
"atanh": math.Atanh,
"cbrt": math.Cbrt,
"ceil": math.Ceil,
"copysign": math.Copysign,
"cos": math.Cos,
"cosh": math.Cosh,
"dim": math.Dim,
"erf": math.Erf,
"erfc": math.Erfc,
"erfcinv": math.Erfcinv,
"erfinv": math.Erfinv,
"exp": math.Exp,
"exp2": math.Exp2,
"expm1": math.Expm1,
"fma": math.FMA,
"float32bits": math.Float32bits,
"float32frombits": math.Float32frombits,
"float64bits": math.Float64bits,
"float64frombits": math.Float64frombits,
"floor": math.Floor,
"frexp": math.Frexp,
"gamma": math.Gamma,
"hypot": math.Hypot,
"ilogb": math.Ilogb,
"inf": math.Inf,
"isInf": math.IsInf,
"isNaN": math.IsNaN,
"j0": math.J0,
"j1": math.J1,
"jn": math.Jn,
"ldexp": math.Ldexp,
"lgamma": math.Lgamma,
"log": math.Log,
"log10": math.Log10,
"log1p": math.Log1p,
"log2": math.Log2,
"logb": math.Logb,
"max": math.Max,
"min": math.Min,
"mod": math.Mod,
"modf": math.Modf,
"naN": math.NaN,
"nextafter": math.Nextafter,
"nextafter32": math.Nextafter32,
"pow": math.Pow,
"pow10": math.Pow10,
"remainder": math.Remainder,
"round": math.Round,
"roundToEven": math.RoundToEven,
"signbit": math.Signbit,
"sin": math.Sin,
"sincos": math.Sincos,
"sinh": math.Sinh,
"sqrt": math.Sqrt,
"tan": math.Tan,
"tanh": math.Tanh,
"trunc": math.Trunc,
"y0": math.Y0,
"y1": math.Y1,
"yn": math.Yn,
}
var mathPortConsts = map[string]any{
"PI": math.Pi,
"E": math.E,
}
func mathGCD2(a, b int64) int64 {
for b > 0 {
t := a % b
a = b
b = t
}
return a
}
func mathMustGetPositiveInt(c *Context, a Value) int64 {
if ai, is := a.(ValueInt); !is {
c.RaiseRuntimeError("argument must be an integer")
return -1
} else if vi := ai.Value(); vi <= 0 {
c.RaiseRuntimeError("argument must be larger than 0")
return -1
} else {
return vi
}
}
func mathGCD(c *Context, f string, args []Value) int64 {
n := len(args)
if n < 2 {
c.RaiseRuntimeError("math.%s: requires at lease 2 arguments", f)
return -1
}
a := mathMustGetPositiveInt(c, args[0])
b := mathMustGetPositiveInt(c, args[1])
r := mathGCD2(a, b)
for i := 2; i < n; i++ {
a = r
b = mathMustGetPositiveInt(c, args[i])
r = mathGCD2(a, b)
}
return r
}