-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.go
60 lines (51 loc) · 1.65 KB
/
bigint.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
package genmatrix
import (
"math/big"
"fmt"
"reflect"
)
type Bigint struct {}
func assertBigint(a, b interface{}) error {
if reflect.TypeOf(a) != reflect.TypeOf(new(big.Int)) {
return fmt.Errorf("first operator is not *big.Int, but %T", a)
}
if reflect.TypeOf(b) != reflect.TypeOf(new(big.Int)) {
return fmt.Errorf("second operator is not *big.Int, but %T", b)
}
return nil
}
func (p Bigint) Add(a, b interface{}) (interface{}, error) {
err := assertBigint(a, b)
if err != nil {return nil, err}
return new(big.Int).Add(a.(*big.Int), b.(*big.Int)), nil
}
func (p Bigint) Subtract(a, b interface{}) (interface{}, error) {
err := assertBigint(a, b)
if err != nil {return nil, err}
return new(big.Int).Sub(a.(*big.Int), b.(*big.Int)), nil
}
func (p Bigint) Multiply(a, b interface{}) (interface{}, error) {
err := assertBigint(a, b)
if err != nil {return nil, err}
return new(big.Int).Mul(a.(*big.Int), b.(*big.Int)), nil
}
func (p Bigint) Scale(a interface{}, b interface{}) (interface{}, error) {
err := assertBigint(a, b)
if err != nil {return nil, err}
return p.Multiply(a.(*big.Int), b.(*big.Int))
}
func (p Bigint) Scalarspace() bool {
return true
}
// create a new Matrix from int values
func NewMatrixFromInt(rows, cols int, data []int) (Matrix, error) {
if data == nil {
return NewMatrix(rows, cols, nil, Bigint{})
}
l := len(data)
s := make([]interface{}, l)
for i := 0; i < l; i += 1 {
s[i] = big.NewInt(int64(data[i]))
}
return NewMatrix(rows, cols, s, Bigint{})
}