-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
125 lines (108 loc) · 2.02 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
package gorf
import (
"strconv"
"github.com/whipstein/golinalg/golapack"
"github.com/whipstein/golinalg/mat"
)
const (
epsf64 = 2.2204460492503131e-016
tiny = 2.2250738585072014e-308
huge = 1.7976931348623157e+308
radix = 2
digits = 53
minexp = -1021
maxexp = 1024
)
func absint(a int) int {
if a < 0 {
return -a
}
return a
}
func maxf64(a ...float64) float64 {
maxval := a[0]
for _, val := range a {
if val > maxval {
maxval = val
}
}
return maxval
}
func maxint(a ...int) int {
maxval := a[0]
for _, val := range a {
if val > maxval {
maxval = val
}
}
return maxval
}
func minf64(a ...float64) float64 {
minval := a[0]
for _, val := range a {
if val < minval {
minval = val
}
}
return minval
}
func minint(a ...int) int {
minval := a[0]
for _, val := range a {
if val < minval {
minval = val
}
}
return minval
}
func powint(a, b int) int {
if b == 0 {
return 1
}
result := a
for i := 1; i < b; i++ {
result *= a
}
return result
}
func matInv(m *mat.Matrix) {
var colMajor int
r, c := m.Rows, m.Cols
work := mf(r, c)
ipiv := make([]int, r)
if m.Opts.Major == mat.Row {
m.ToColMajor()
colMajor = 1
}
if info, err := golapack.Dgetrf(r, c, m, &ipiv); err != nil || info != 0 {
panic("golapack.Zgetrf error: " + strconv.Itoa(info))
}
if info, err := golapack.Dgetri(r, m, ipiv, work); err != nil || info != 0 {
panic("golapack.Zgetri error: " + strconv.Itoa(info))
}
if colMajor == 1 {
m.ToRowMajor()
colMajor = 0
}
}
func cmatInv(m *mat.CMatrix) *mat.CMatrix {
var colMajor int
r, c := m.Rows, m.Cols
work := cvf(r * c)
lwork := r * c
ipiv := make([]int, r)
if m.Opts.Major == mat.Row {
m.ToColMajor()
colMajor = 1
}
if info, err := golapack.Zgetrf(r, c, m, &ipiv); err != nil || info != 0 {
panic("golapack.Zgetrf error: " + strconv.Itoa(info))
}
if info, err := golapack.Zgetri(r, m, &ipiv, work, lwork); err != nil || info != 0 {
panic("golapack.Zgetri error: " + strconv.Itoa(info))
}
if colMajor == 1 {
m.ToRowMajor()
}
return m
}