/
bigIntVector.go
90 lines (71 loc) · 1.78 KB
/
bigIntVector.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 projecteuler
import (
"fmt"
"math/big"
)
var errDim error
var errNegDim error
func init() {
errDim = fmt.Errorf("dimensions wrong")
errNegDim = fmt.Errorf("dimensions have to be positive")
}
// BigIntVector struct
type BigIntVector struct {
vec []*big.Int
}
// Dim returns length of the BigIntVector
func (a *BigIntVector) Dim() int {
return len(a.vec)
}
func bigIntVectorConstructionHelper(dim int) (result *BigIntVector, err error) {
if dim < 0 {
err = errNegDim
return
}
result = &BigIntVector{}
result.vec = make([]*big.Int, dim)
return
}
// NewBigIntVector constructs new BigIntVector from slice of int64
func NewBigIntVector(vec []int64) (result *BigIntVector, err error) {
dim := len(vec)
if result, err = bigIntVectorConstructionHelper(dim); err != nil {
return
}
for i := 0; i < dim; i++ {
result.vec[i] = big.NewInt(vec[i])
}
return
}
// NewVectorFromBigIntSlice constructs new BigIntVector from slice of *big.Int
func NewVectorFromBigIntSlice(vec []*big.Int) (result *BigIntVector, err error) {
dim := len(vec)
if result, err = bigIntVectorConstructionHelper(dim); err != nil {
return
}
for i := 0; i < dim; i++ {
result.vec[i] = &big.Int{}
result.vec[i].Set(vec[i])
}
return
}
// Clone copies receiver to new BigIntVector
func (a *BigIntVector) Clone() (result *BigIntVector) {
dim := a.Dim()
result, _ = bigIntVectorConstructionHelper(dim)
for i := 0; i < dim; i++ {
result.vec[i] = &big.Int{}
result.vec[i].Set(a.vec[i])
}
return
}
// MulBigIntVectors multiplies arguments and puts the result in the new big.Int
func MulBigIntVectors(a, b *BigIntVector) (result *big.Int) {
result = big.NewInt(0)
for i := 0; i < a.Dim(); i++ {
currInt := &big.Int{}
currInt.Mul(a.vec[i], b.vec[i])
result.Add(result, currInt)
}
return
}