-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig.go
90 lines (75 loc) · 1.72 KB
/
big.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
package LatticeReduction
import (
"fmt"
"math/big"
)
// Lattice basis for big.Int sized values
type BigBasis [][]*big.Int
var (
bigOne = big.NewInt(1)
)
func bigIntToFloat(in *big.Int) float64 {
rat := new(big.Rat)
rat.SetFrac(in, bigOne)
f, _ := rat.Float64()
return f
}
func (b BigBasis) Copy() Basis {
o := make(BigBasis, len(b))
for i, x := range b {
o[i] = make([]*big.Int, len(x))
for j, y := range x {
o[i][j] = new(big.Int)
o[i][j].Set(y)
}
}
return o
}
func (b BigBasis) ColumnSwap(column1, column2 int) {
b[column1], b[column2] = b[column2], b[column1]
}
func (b BigBasis) Rank() int {
return len(b)
}
func (b BigBasis) Dimension() int {
return len(b[0])
}
func (b BigBasis) FDot(column1, column2 int) (r float64) {
var intermediate = new(big.Int)
for i, x := range b[column1] {
intermediate.Mul(x, b[column2][i])
r += bigIntToFloat(intermediate)
}
return
}
func (b BigBasis) FPairSize(column1, column2 int) (sub,add float64) {
var intermediate = new(big.Int)
for i, x := range b[column1] {
intermediate.Sub(x, b[column2][i])
intermediate.Mul(intermediate,intermediate)
sub += bigIntToFloat(intermediate)
intermediate.Add(x, b[column2][i])
intermediate.Mul(intermediate,intermediate)
add += bigIntToFloat(intermediate)
}
return
}
func (b BigBasis) ColumnReduceInt64(column1, column2 int, mu int64) {
var (
intermediate = new(big.Int)
_mu = big.NewInt(mu)
)
for i := range b[column1] {
intermediate.Mul(_mu, b[column2][i])
b[column1][i].Sub(b[column1][i], intermediate)
}
}
func (b BigBasis) FGet(column, row int) float64 {
return bigIntToFloat(b[column][row])
}
func (b BigBasis) String() (s string) {
for _, r := range b {
s += fmt.Sprintln(r)
}
return s
}