forked from soudy/mathcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
155 lines (143 loc) · 2.97 KB
/
functions.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
// Copyright 2016 Steven Oud. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package mathcat
import (
"fmt"
"math"
"math/rand"
)
type function struct {
arity int
fn func(args []float64) float64
}
type functions map[string]*function
// Functions holds all the function names that are available for use
var Functions []string
var funcs = make(functions)
func (f functions) register(name string, function *function) {
Functions = append(Functions, name)
f[name] = function
}
func init() {
funcs.register("abs", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Abs(args[0])
},
})
funcs.register("ceil", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Ceil(args[0])
},
})
funcs.register("floor", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Floor(args[0])
},
})
funcs.register("sin", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Sin(args[0])
},
})
funcs.register("cos", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Cos(args[0])
},
})
funcs.register("tan", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Tan(args[0])
},
})
funcs.register("asin", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Asin(args[0])
},
})
funcs.register("acos", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Acos(args[0])
},
})
funcs.register("atan", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Atan(args[0])
},
})
funcs.register("log", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Log(args[0])
},
})
funcs.register("max", &function{
arity: 2,
fn: func(args []float64) float64 {
return math.Max(args[0], args[1])
},
})
funcs.register("min", &function{
arity: 2,
fn: func(args []float64) float64 {
return math.Min(args[0], args[1])
},
})
funcs.register("sqrt", &function{
arity: 1,
fn: func(args []float64) float64 {
return math.Sqrt(args[0])
},
})
funcs.register("rand", &function{
arity: 0,
fn: func(_ []float64) float64 {
return rand.Float64()
},
})
funcs.register("fact", &function{
arity: 1,
fn: func(args []float64) float64 {
return float64(Factorial(int64(args[0])))
},
})
funcs.register("gcd", &function{
arity: 2,
fn: func(args []float64) float64 {
return Gcd(args[0], args[1])
},
})
funcs.register("list", &function{
arity: 0,
fn: func(_ []float64) float64 {
for _, name := range Functions {
fmt.Printf(name + " ")
}
fmt.Println()
return 0
},
})
}
// Factorial calculates the factorial of number n
func Factorial(n int64) int64 {
if n <= 1 {
return 1
}
return n * Factorial(n-1)
}
// Gcd calculates the greatest common divisor of the numbers x and y
func Gcd(x, y float64) float64 {
for y != 0 {
x, y = y, math.Mod(x, y)
}
return x
}