/
builtin_power.go
281 lines (261 loc) · 8.51 KB
/
builtin_power.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package expreduce
import (
"github.com/corywalker/mathbigext"
"math/big"
)
func GetPowerDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Power",
Usage: "`base^exp` finds `base` raised to the power of `exp`.",
Attributes: []string{"Listable", "NumericFunction", "OneIdentity"},
Default: "1",
Rules: []Rule{
// Simplify nested exponents
{"Power[Power[a_,b_Integer],c_Integer]", "a^(b*c)"},
{"Power[Power[a_,b_Real],c_Integer]", "a^(b*c)"},
{"Power[Power[a_,b_Symbol],c_Integer]", "a^(b*c)"},
{"Power[Infinity, -1]", "0"},
// Power definitions
{"(Except[_Symbol, first_] * inner___)^Except[_Symbol, pow_]", "first^pow * Times[inner]^pow"},
{"(first_ * inner___)^Except[_Symbol, pow_]", "first^pow * Times[inner]^pow"},
// Rational simplifications
// These take up time. Possibly convert to Upvalues.
{"Power[Rational[a_,b_], -1]", "Rational[b,a]"},
{"Power[Rational[a_,b_], e_?Positive]", "Rational[a^e,b^e]"},
},
toString: func(this *Expression, form string) (bool, string) {
return ToStringInfixAdvanced(this.Parts[1:], "^", false, "", "", form)
},
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
// TODO: Handle cases like float raised to the float and things raised to
// zero and 1
baseInt, baseIsInt := this.Parts[1].(*Integer)
powerInt, powerIsInt := this.Parts[2].(*Integer)
baseFlt, baseIsFlt := this.Parts[1].(*Flt)
powerFlt, powerIsFlt := this.Parts[2].(*Flt)
// Anything raised to the 1st power is itself
if powerIsFlt {
if powerFlt.Val.Cmp(big.NewFloat(1)) == 0 {
return this.Parts[1]
}
} else if powerIsInt {
if powerInt.Val.Cmp(big.NewInt(1)) == 0 {
return this.Parts[1]
}
}
// Anything raised to the 0th power is 1, with a small exception
isZerothPower := false
if powerIsFlt {
if powerFlt.Val.Cmp(big.NewFloat(0)) == 0 {
isZerothPower = true
}
} else if powerIsInt {
if powerInt.Val.Cmp(big.NewInt(0)) == 0 {
isZerothPower = true
}
}
isZeroBase := false
if baseIsFlt {
if baseFlt.Val.Cmp(big.NewFloat(0)) == 0 {
isZeroBase = true
}
} else if baseIsInt {
if baseInt.Val.Cmp(big.NewInt(0)) == 0 {
isZeroBase = true
}
}
if isZerothPower {
if isZeroBase {
return &Symbol{"Indeterminate"}
}
return &Integer{big.NewInt(1)}
}
//es.Debugf("Power eval. baseIsInt=%v, powerIsInt=%v", baseIsInt, powerIsInt)
// Fully integer Power expression
if baseIsInt && powerIsInt {
cmpres := powerInt.Val.Cmp(big.NewInt(0))
//es.Debugf("Cmpres: %v", cmpres)
if cmpres == 1 {
res := big.NewInt(0)
res.Exp(baseInt.Val, powerInt.Val, nil)
return &Integer{res}
} else if cmpres == -1 {
newbase := big.NewInt(0)
absPower := big.NewInt(0)
absPower.Abs(powerInt.Val)
newbase.Exp(baseInt.Val, absPower, nil)
if newbase.Cmp(big.NewInt(1)) == 0 {
return &Integer{big.NewInt(1)}
}
if newbase.Cmp(big.NewInt(-1)) == 0 {
return &Integer{big.NewInt(-1)}
}
//return NewExpression([]Ex{&Symbol{"Power"}, &Integer{newbase}, &Integer{big.NewInt(-1)}})
return NewExpression([]Ex{&Symbol{"Rational"}, &Integer{big.NewInt(1)}, &Integer{newbase}})
} else {
return NewExpression([]Ex{&Symbol{"Error"}, &String{"Unexpected zero power in Power evaluation."}})
}
}
if baseIsFlt && powerIsInt {
return &Flt{mathbigext.Pow(baseFlt.Val, big.NewFloat(0).SetInt(powerInt.Val))}
}
if baseIsInt && powerIsFlt {
return &Flt{mathbigext.Pow(big.NewFloat(0).SetInt(baseInt.Val), powerFlt.Val)}
}
if baseIsFlt && powerIsFlt {
return &Flt{mathbigext.Pow(baseFlt.Val, powerFlt.Val)}
}
powerRat, powerIsRat := this.Parts[2].(*Rational)
if powerIsRat {
if powerRat.Num.Cmp(big.NewInt(1)) == 0 && powerRat.Den.Cmp(big.NewInt(2)) == 0 {
return NewExpression([]Ex{
&Symbol{"Sqrt"},
this.Parts[1],
})
}
}
return this
},
SimpleExamples: []TestInstruction{
&TestComment{"Exponents of integers are computed exactly:"},
&StringTest{"-1/125", "(-5)^-3"},
&TestComment{"Floating point exponents are handled with floating point precision:"},
&StringTest{"1.99506e+3010", ".5^-10000."},
&TestComment{"Automatically apply some basic simplification rules:"},
&SameTest{"m^4.", "(m^2.)^2"},
},
FurtherExamples: []TestInstruction{
&TestComment{"Expreduce handles problematic exponents accordingly:"},
&StringTest{"Indeterminate", "0^0"},
&SameTest{"ComplexInfinity", "0^(-1)"},
},
Tests: []TestInstruction{
// Test raising expressions to the first power
&StringTest{"(1 + x)", "(x+1)^1"},
&StringTest{"0", "0^1"},
&StringTest{"0.", "0.^1"},
&StringTest{"-5", "-5^1"},
&StringTest{"-5.5", "-5.5^1"},
&StringTest{"(1 + x)", "(x+1)^1."},
&StringTest{"0", "0^1."},
&StringTest{"0.", "0.^1."},
&StringTest{"-5", "(-5)^1."},
&StringTest{"-5.5", "-5.5^1."},
// Test raising expressions to the zero power
&StringTest{"1", "(x+1)^0"},
&StringTest{"Indeterminate", "0^0"},
&StringTest{"Indeterminate", "0.^0"},
&StringTest{"-1", "-5^0"},
&StringTest{"1", "(-5)^0"},
&StringTest{"1", "(-5.5)^0"},
&StringTest{"1", "(x+1)^0."},
&StringTest{"Indeterminate", "0^0."},
&StringTest{"Indeterminate", "0.^0."},
&StringTest{"-1", "-5^0."},
&StringTest{"1", "(-5.5)^0."},
&StringTest{"-1", "-5^0"},
&StringTest{"1", "99^0"},
&StringTest{"125", "5^3"},
&StringTest{"1/125", "5^-3"},
&StringTest{"-125", "(-5)^3"},
&StringTest{"-1/125", "(-5)^-3"},
&StringTest{"2.97538e+1589", "39^999."},
&StringTest{"3.36092e-1590", "39^-999."},
&StringTest{"1.99506e+3010", ".5^-10000."},
&StringTest{"1.99506e+3010", ".5^-10000"},
&StringTest{"1", "1^1"},
&StringTest{"1", "1^2"},
&StringTest{"1", "1^0"},
&StringTest{"1", "1^-1"},
&StringTest{"1", "1^-2"},
&StringTest{"1", "1^99999992"},
&StringTest{"1.", "1^2."},
&StringTest{"1.", "1^99999992."},
&StringTest{"1.", "1.^30"},
&StringTest{"4.", "(1.*2*1.)^2"},
&StringTest{"-1", "(-1)^1"},
&StringTest{"1", "(-1)^2"},
&StringTest{"1", "(-1)^0"},
&StringTest{"1", "(-1)^0"},
&StringTest{"-1", "(-1)^-1"},
&StringTest{"1", "(-1)^-2"},
&StringTest{"1", "(-1)^99999992"},
&StringTest{"1.", "(-1.)^30"},
&StringTest{"4.", "(1.*2*-1.)^2"},
&StringTest{"-0.5", "(1.*2*-1.)^(-1)"},
&SameTest{"Rational", "Power[2, -1] // Head"},
&SameTest{"Integer", "Power[1, -1] // Head"},
&SameTest{"Integer", "Power[2, 2] // Head"},
&SameTest{"Rational", "Power[-2, -1] // Head"},
&SameTest{"Rational", "Power[2, -2] // Head"},
// Exponent simplifications
&SameTest{"m^4", "m^2*m^2"},
&SameTest{"m^4", "(m^2)^2"},
&SameTest{"(m^2)^2.", "(m^2)^2."},
&SameTest{"(m^2.)^2.", "(m^2.)^2."},
&SameTest{"m^4.", "(m^2.)^2"},
&SameTest{"ComplexInfinity", "0^(-1)"},
&SameTest{"{1}", "ReplaceAll[a, a^p_. -> {p}]"},
},
KnownFailures: []TestInstruction{
// Fix these when I have Abs functionality
&StringTest{"2.975379863266329e+1589", "39^999."},
&StringTest{"3.360915398890324e-1590", "39^-999."},
&StringTest{"1.9950631168791027e+3010", ".5^-10000."},
&StringTest{"1.9950631168791027e+3010", ".5^-10000"},
},
})
defs = append(defs, Definition{
Name: "PowerExpand",
// This function is not implemented to a satisfiable extent. Do not
// document it yet.
OmitDocumentation: true,
SimpleExamples: []TestInstruction{
&TestComment{"`PowerExpand` can expand nested log expressions:"},
&SameTest{"Log[a] + e (Log[b] + d Log[c])", "PowerExpand[Log[a (b c^d)^e]]"},
},
Rules: []Rule{
{"PowerExpand[exp_]", "exp //. {Log[x_ y_]:>Log[x]+Log[y],Log[x_^k_]:>k Log[x]}"},
},
})
defs = append(defs, Definition{
Name: "Expand",
})
defs = append(defs, Definition{
Name: "PolynomialQ",
})
defs = append(defs, Definition{
Name: "Exponent",
})
defs = append(defs, Definition{
Name: "Coefficient",
})
defs = append(defs, Definition{
Name: "PolynomialQuotientRemainder",
})
defs = append(defs, Definition{
Name: "PolynomialQuotient",
})
defs = append(defs, Definition{
Name: "PolynomialRemainder",
})
defs = append(defs, Definition{
Name: "FactorTermsList",
})
defs = append(defs, Definition{
Name: "Variables",
})
defs = append(defs, Definition{
Name: "PolynomialGCD",
})
defs = append(defs, Definition{Name: "SquareFreeQ"})
defs = append(defs, Definition{
Name: "PSimplify",
OmitDocumentation: true,
ExpreduceSpecific: true,
})
return
}