-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
84 lines (76 loc) · 1.79 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
package zeno
import (
"fmt"
"math"
)
var (
functionArgs = map[string]int{
"neg": 1, "sin": 1, "cos": 1, "tan": 1,
"max": 2, "min": 2, "log": 2,
}
mappedFunctions = map[string]func(x, y float64) (float64, error){
"neg": func(x, y float64) (float64, error) {
return -y, nil
},
"sin": func(x, y float64) (float64, error) {
return math.Sin(y), nil
},
"cos": func(x, y float64) (float64, error) {
return math.Cos(y), nil
},
"tan": func(x, y float64) (float64, error) {
return math.Tan(y), nil
},
"max": func(x, y float64) (float64, error) {
return math.Max(x, y), nil
},
"min": func(x, y float64) (float64, error) {
return math.Min(x, y), nil
},
"log": func(x, y float64) (float64, error) {
// log<b>(x) = log(x)/log(b)
if x == 1 {
return 0, ErrorZeroDivision
}
return math.Log(y) / math.Log(x), nil
},
}
// custom latex for some functions
latexFunctions = map[string]func(x, y *Operation) string{
"log": func(x, y *Operation) string {
return fmt.Sprintf("\\log_{%s}%s", x.LaTeX(), y.LaTeX())
},
}
)
// Function represents a mathematical function
type Function struct {
Name string
}
func (f *Function) Operate(x, y *Operation) (float64, error) {
args := functionArgs[f.Name]
right, err := y.Operate()
if err != nil {
return 0, err
}
left := float64(0)
if args == 2 {
left, err = x.Operate()
if err != nil {
return 0, err
}
}
return mappedFunctions[f.Name](left, right)
}
func (f *Function) LaTeX(x, y *Operation) string {
if specialLatex, isSpecial := latexFunctions[f.Name]; isSpecial {
return specialLatex(x, y)
}
args := functionArgs[f.Name]
if args == 1 {
return fmt.Sprintf("%s(%s)", f.Name, y.LaTeX())
}
if args == 2 {
return fmt.Sprintf("%s(%s,%s)", f.Name, x.LaTeX(), y.LaTeX())
}
return "" // haks
}