-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction.go
85 lines (71 loc) · 1.68 KB
/
function.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
package formulae
import (
"math"
"fmt"
)
type Vars map[string]complex128
func (v Vars) Set(name string, value complex128) {
v[name] = value
}
func (v Vars) Duplicate() Vars {
v2 := make(map[string]complex128, len(v))
for key, val := range v {
v2[key] = val
}
return v2
}
var DefaultVars = Vars{
"e": complex(math.E, 0),
"pi": complex(math.Pi, 0),
"phi": complex(math.Phi, 0),
}
////////////////
type Function struct {
root Node
Vars
nthDerivative int
}
func (f *Function) String() string {
return f.root.String()
}
func (f *Function) LaTeX() string {
d := ""
if f.nthDerivative == 1 {
d = fmt.Sprintf("\\frac{\\partial}{\\partial x} ")
} else if f.nthDerivative > 1 {
d = fmt.Sprintf("\\frac{\\partial^{%v}}{\\partial x^{%v}} ", f.nthDerivative, f.nthDerivative)
}
return fmt.Sprintf("%sf(x) = %s", d, f.root.LaTeX())
}
func (f *Function) Optimize() {
f.root = Optimize(f.root)
}
func (f *Function) Derivative() *Function {
root := f.root.Derivative()
root = Optimize(root)
return &Function{
root: root,
Vars: f.Vars,
nthDerivative: f.nthDerivative + 1,
}
}
func (f *Function) Calc(x complex128) (complex128, error) {
return f.root.Calc(x, f.Vars)
}
func (f *Function) Interval(xMin, xStep, xMax float64) ([]float64, []complex128, []error) {
n := int((xMax-xMin)/xStep) + 1
xs := make([]float64, n)
ys := make([]complex128, n)
x := xMin
var err error
var errs []error
for i := 0; i < n; i++ {
xs[i] = x
ys[i], err = f.Calc(complex(x, 0))
if err != nil {
errs = append(errs, err)
}
x += xStep
}
return xs, ys, errs
}