This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathm.go
125 lines (116 loc) · 2.28 KB
/
mathm.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
// Code generated by scripts/math.goal. DO NOT EDIT.
package goal
import "math"
func mathm(x V, f func(float64) float64) V {
if x.IsI() {
return NewF(f(float64(x.I())))
}
if x.IsF() {
return NewF(f(x.F()))
}
switch xv := x.bv.(type) {
case *AB:
r := make([]float64, xv.Len())
for i, xi := range xv.elts {
r[i] = f(float64(xi))
}
return NewAF(r)
case *AI:
r := make([]float64, xv.Len())
for i, xi := range xv.elts {
r[i] = f(float64(xi))
}
return NewAF(r)
case *AF:
r := xv.reuse()
for i, xi := range xv.elts {
r.elts[i] = f(xi)
}
return NewV(r)
case *AV:
return mapAV(xv, func(xi V) V { return mathm(xi, f) })
case *D:
r := mathm(NewV(xv.values), f)
return NewV(&D{keys: xv.keys, values: r.bv.(Array)})
default:
return Panicf("bad type \"%s\" in x", x.Type())
}
}
// vfCos implements the cos variadic.
func vfCos(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.Cos)
if r.IsPanic() {
return ppanic("cos x : ", r)
}
return r
default:
return panicRank("cos")
}
}
// vfExp implements the exp variadic.
func vfExp(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.Exp)
if r.IsPanic() {
return ppanic("exp x : ", r)
}
return r
default:
return panicRank("exp")
}
}
// vfLog implements the log variadic.
func vfLog(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.Log)
if r.IsPanic() {
return ppanic("log x : ", r)
}
return r
default:
return panicRank("log")
}
}
// vfRoundToEven implements the round variadic.
func vfRoundToEven(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.RoundToEven)
if r.IsPanic() {
return ppanic("round x : ", r)
}
return r
default:
return panicRank("round")
}
}
// vfSin implements the sin variadic.
func vfSin(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.Sin)
if r.IsPanic() {
return ppanic("sin x : ", r)
}
return r
default:
return panicRank("sin")
}
}
// vfSqrt implements the sqrt variadic.
func vfSqrt(ctx *Context, args []V) V {
switch len(args) {
case 1:
r := mathm(args[0], math.Sqrt)
if r.IsPanic() {
return ppanic("sqrt x : ", r)
}
return r
default:
return panicRank("sqrt")
}
}