-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
135 lines (126 loc) · 2.82 KB
/
util.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
package matrix
import (
"errors"
"math"
)
var a []int
var n int
var count int
// PermEachResult have the answer of return value of each perm calc result
type PermResult struct {
value interface{}
err error
}
// Perm will have every infomation to do permutation
type Perm struct {
length int
f func([]int, *PermResult, interface{}) *PermResult
argument interface{}
array []int
count int
result *PermResult
}
func (pt *Perm) perm(k int) {
if k > pt.length {
pt.count++
pt.result = pt.f(pt.array[:len(pt.array)-1], pt.result, pt.argument)
return
}
for i := k - 1; i >= 0; i-- {
pt.array[i+1] = pt.array[i]
pt.array[i] = k
pt.perm(k + 1)
}
for i := 1; i < k; i++ {
pt.array[i-1] = pt.array[i]
}
}
// PermutationProcess will calc permutation, each permutation you can call function f
func PermutationProcess(length int, f func([]int, *PermResult, interface{}) *PermResult, argument interface{}) *Perm {
pt := new(Perm)
pt.argument = argument
pt.length = length
pt.array = make([]int, length+1)
pt.count = 0
pt.f = f
pt.result = new(PermResult)
pt.perm(1)
return pt
}
// Sgn will return 1 when even permutation
// Sgn will return -1 when odd permutation
func Sgn(num []int) int {
if len(num) <= 0 {
return 0
}
result := 1
length := len(num)
if length < 2 {
return 1
}
for i := 1; i < length; i++ {
for j := i - 1; j >= 0; j-- {
result *= (num[i] - num[j])
if result > 100000 {
result /= int(math.Abs(float64(result)))
}
}
}
if result < 0 {
return -1
}
return 1
}
func (m *Matrix) findNonZeroFromBelowRow(r, c int) (row int, err error) {
if r > m.row || c > m.column {
return -1, errors.New("The row or column are outside of this matrix")
}
for i := r + 1; i <= m.row; i++ {
if val, _ := m.At(i, c); val != 0 {
return i, nil
}
}
return -1, errors.New("There is no available row")
}
// Determinant will calculate determinant
func (m *Matrix) Determinant() (float64, error) {
if m.row != m.column {
// TODO REUTN ERROR
return 0, errors.New("This is not square error")
}
length := m.Row()
matrix := Copy(m)
for i := 1; i <= length; i++ {
for j := 1; j <= length; j++ {
if i < j {
aji, _ := matrix.At(j, i)
aii, _ := matrix.At(i, i)
if aii != 0 {
buf := aji / aii
for k := 1; k <= length; k++ {
ajk, _ := matrix.At(j, k)
aik, _ := matrix.At(i, k)
matrix.Set(j, k, ajk-aik*buf)
}
} else if aii == 0 {
r, err := matrix.findNonZeroFromBelowRow(i, i)
if err != nil {
return 0, errors.New("there is no Determinant")
}
for k := 1; k <= length; k++ {
aik, _ := matrix.At(i, k)
ark, _ := matrix.At(r, k)
matrix.Set(i, k, aik+ark)
j--
}
}
}
}
}
result := float64(1)
for i := 1; i <= length; i++ {
aii, _ := matrix.At(i, i)
result *= aii
}
return result, nil
}