/
ex_rational.go
155 lines (134 loc) · 3.54 KB
/
ex_rational.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package expreduce
import "fmt"
import "math/big"
import "hash/fnv"
type Rational struct {
Num *big.Int
Den *big.Int
needsEval bool
}
func (this *Rational) Eval(es *EvalState) Ex {
if this.Num.Cmp(big.NewInt(0)) == 0 && this.Den.Cmp(big.NewInt(0)) == 0 {
return NewSymbol("System`Indeterminate")
}
if this.Den.Cmp(big.NewInt(0)) == 0 {
return NewSymbol("System`ComplexInfinity")
}
if this.Num.Cmp(big.NewInt(0)) == 0 {
return NewInteger(big.NewInt(0))
}
negNum := this.Num.Cmp(big.NewInt(0)) == -1
negDen := this.Den.Cmp(big.NewInt(0)) == -1
negateRes := negNum != negDen
absNum := big.NewInt(0)
absNum.Abs(this.Num)
absDen := big.NewInt(0)
absDen.Abs(this.Den)
gcd := big.NewInt(0)
gcd.GCD(nil, nil, absNum, absDen)
absNum.Div(absNum, gcd)
absDen.Div(absDen, gcd)
if absDen.Cmp(big.NewInt(1)) == 0 {
if !negateRes {
return NewInteger(absNum)
} else {
return NewInteger(absNum.Neg(absNum))
}
}
if !negateRes {
this.Num.Set(absNum)
this.Den.Set(absDen)
this.needsEval = false
return this
}
this.Num.Set(absNum.Neg(absNum))
this.Den.Set(absDen)
this.needsEval = false
return this
}
func (this *Rational) StringForm(params ToStringParams) string {
if params.form == "FullForm" {
return fmt.Sprintf("Rational[%d, %d]", this.Num, this.Den)
}
//if params.previousHead == "System`Power" {
if needsParens("System`Times", params.previousHead) {
return fmt.Sprintf("(%d/%d)", this.Num, this.Den)
}
return fmt.Sprintf("%d/%d", this.Num, this.Den)
}
func (this *Rational) String(es *EvalState) string {
context, contextPath := DefaultStringFormArgs()
return this.StringForm(ToStringParams{form: "InputForm", context: context, contextPath: contextPath, es: es})
}
func (this *Rational) IsEqual(other Ex, cl *CASLogger) string {
otherConv, otherIsRational := other.(*Rational)
if !otherIsRational {
return "EQUAL_FALSE"
}
// Assume rational already simplified
if (this.Num.Cmp(otherConv.Num) != 0) || (this.Den.Cmp(otherConv.Den) != 0) {
return "EQUAL_FALSE"
}
return "EQUAL_TRUE"
}
func (this *Rational) DeepCopy() Ex {
tmpn := big.NewInt(0)
tmpn.Set(this.Num)
tmpd := big.NewInt(0)
tmpd.Set(this.Den)
return &Rational{tmpn, tmpd, this.needsEval}
}
func (this *Rational) Copy() Ex {
return this.DeepCopy()
}
func (this *Rational) AsBigRat() *big.Rat {
res := big.NewRat(1, 1)
return res.SetFrac(this.Num, this.Den)
}
func (this *Rational) NeedsEval() bool {
return this.needsEval
}
func NewRational(n *big.Int, d *big.Int) *Rational {
return &Rational{n, d, true}
}
func (this *Rational) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte{90, 82, 214, 51, 52, 7, 7, 33})
nBytes, _ := this.Num.MarshalText()
h.Write(nBytes)
dBytes, _ := this.Den.MarshalText()
h.Write(dBytes)
return h.Sum64()
}
func (this *Rational) AsBigFloat() *big.Float {
num := big.NewFloat(0)
den := big.NewFloat(0)
newquo := big.NewFloat(0)
num.SetInt(this.Num)
den.SetInt(this.Den)
newquo.Quo(num, den)
return newquo
}
func (this *Rational) AddI(i *Integer) {
tmp := big.NewInt(0)
tmp.Mul(i.Val, this.Den)
this.Num.Add(this.Num, tmp)
}
func (this *Rational) AddR(r *Rational) {
tmp := big.NewInt(0)
// lastrNum/lastrDen + theratNum/theratDen // Together
tmp.Mul(this.Den, r.Num)
this.Den.Mul(this.Den, r.Den)
this.Num.Mul(this.Num, r.Den)
this.Num.Add(this.Num, tmp)
}
func (this *Rational) MulI(i *Integer) {
this.Num.Mul(this.Num, i.Val)
}
func (this *Rational) MulBigI(i *big.Int) {
this.Num.Mul(this.Num, i)
}
func (this *Rational) MulR(r *Rational) {
this.Num.Mul(this.Num, r.Num)
this.Den.Mul(this.Den, r.Den)
}