-
Notifications
You must be signed in to change notification settings - Fork 25
/
ex_rational.go
139 lines (120 loc) · 3 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
package expreduce
import "fmt"
import "math/big"
import "hash"
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 &Symbol{"Indeterminate"}
}
if this.Den.Cmp(big.NewInt(0)) == 0 {
return &Symbol{"ComplexInfinity"}
}
if this.Num.Cmp(big.NewInt(0)) == 0 {
return &Integer{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 &Integer{absNum}
} else {
return &Integer{absNum.Neg(absNum)}
}
}
if !negateRes {
this.Num.Set(absNum)
this.Den.Set(absDen)
this.needsEval = false
return this
} else {
this.Num.Set(absNum.Neg(absNum))
this.Den.Set(absDen)
this.needsEval = false
return this
}
this.needsEval = false
return this
}
func (this *Rational) StringForm(form string) string {
return fmt.Sprintf("%d/%d", this.Num, this.Den)
}
func (this *Rational) String() string {
return this.StringForm("InputForm")
}
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) AsBigRat() *big.Rat {
return big.NewRat(this.Num.Int64(), this.Den.Int64())
}
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(h *hash.Hash64) {
(*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)
}
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) MulR(r *Rational) {
this.Num.Mul(this.Num, r.Num)
this.Den.Mul(this.Den, r.Den)
}