Skip to content

Commit 837fe9c

Browse files
committed
improved handling of exponents. (x^y)^2/x == x^(y*2 -1)
1 parent a341dff commit 837fe9c

File tree

3 files changed

+20
-77
lines changed

3 files changed

+20
-77
lines changed

src/Simplification.jl

+13-76
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Simplification.jl
2+
13
# [[file:~/Documents/Julia/scrap.org::*Simplification.jl][Simplification.jl:1]]
24
#----------------------------------------------------------------------------
35
# Some utility functions
@@ -92,35 +94,6 @@ end
9294
# The simplification functions
9395
#----------------------------------------------------------------------------
9496

95-
96-
# addition_rules(x) = x
97-
# function addition_rules(ex::T) where {T<:AbstractSymExpr}
98-
# if ex.op == :+
99-
# @> ex begin
100-
# remove_addative_identity
101-
# end
102-
# else
103-
# ex
104-
# end
105-
# end
106-
107-
# remove_addative_identity(x) = x
108-
# function remove_addative_identity(ex::T) where {T<:AbstractSymExpr}
109-
# if (0 in expr.args)
110-
# return +(expr.args[expr.args .!= 0]...)
111-
# end
112-
# end
113-
114-
# remove_addative_identity(x) = x
115-
# function remove_addative_identity(ex::T) where {T<:AbstractSymExpr}
116-
# if (0 in expr.args)
117-
# return +(expr.args[expr.args .!= 0]...)
118-
# end
119-
# end
120-
121-
# # collect_over_op(x) = x
122-
# # function collect_associative_terms(x, op) = x
123-
12497
#----------------------------------------------------------------------------
12598
# Rules for Addition
12699
#----------------------------------------------------------------------------
@@ -166,8 +139,6 @@ function factor_addition(ex::T) where {T<:AbstractSymExpr}
166139
end
167140

168141

169-
170-
171142
#----------------------------------------------------------------------------
172143
# Rules for Multiplication
173144
#----------------------------------------------------------------------------
@@ -223,6 +194,7 @@ function exp_rules(ex::T) where {T<:AbstractSymExpr}
223194
@> ex begin
224195
exp_one
225196
exp_zero
197+
exp_exp
226198
end
227199
end
228200

@@ -241,6 +213,16 @@ function exp_zero(ex::T) where {T<:AbstractSymExpr}
241213
end
242214
ex
243215
end
216+
217+
exp_exp(x) = x
218+
function exp_exp(ex::T) where {T<:AbstractSymExpr}
219+
@match ex begin
220+
T(:^, [T(:^, [a,b]), c]) => a^(b*c)
221+
_ => ex
222+
end
223+
end
224+
225+
244226

245227

246228
#----------------------------------------------------------------------------
@@ -313,49 +295,4 @@ function collect_identical(ex::T) where {T<:AbstractSymExpr}
313295
ex
314296
end
315297
end
316-
317-
# eval_numeric(x) = x
318-
# function eval_numeric(ex::T) where {T<:AbstractSymExpr}
319-
# if (ex.args isa Array{U,1} where {U<:Number}) && (try eval(Symbol(ex.op)) isa Function catch e; false end)
320-
# eval(Expr(ex))
321-
# else
322-
# ex
323-
# end
324-
# end
325-
326-
327-
# function mult_zero(expr::SymExpr)
328-
# if expr.op == *
329-
# if length(findall(0 .== expr.args)) > 0
330-
# return 0
331-
# end
332-
# end
333-
# expr
334-
# end
335-
# mult_zero(x::Union{Sym,Number}) = x
336-
337-
338-
# function remove_identity_operations(expr::SymExpr)
339-
# if (expr.op == (^)) && (expr.args[2] == 1)
340-
# return expr.args[1]
341-
342-
# elseif expr.op == +
343-
# lst = findall(0 .== expr.args)
344-
# if (length(expr.args) == 2) && (length(lst) > 0)
345-
# return expr.args[1:end .!= lst[1]]
346-
# elseif length(lst) > 0
347-
# return SymExpr(+, expr.args[1:end .!= lst[1]])
348-
# end
349-
350-
# elseif expr.op == *
351-
# lst = findall(1 .== expr.args)
352-
# if (length(expr.args) == 2) && (length(lst) > 0)
353-
# return expr.args[1:end .!= lst[1]]
354-
# elseif length(lst) > 0
355-
# return SymExpr(*, expr.args[1:end .!= lst[1]])
356-
# end
357-
# end
358-
# expr
359-
# end
360-
# remove_identity_operations(x::Union{Sym,Number}) = x
361298
# Simplification.jl:1 ends here

src/SymbolicAlgebra.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# SymbolicAlgebra.jl
2+
13
# [[file:~/Documents/Julia/scrap.org::*SymbolicAlgebra.jl][SymbolicAlgebra.jl:1]]
24
#_____________________________________________
35
# Promotion Rules
@@ -33,7 +35,7 @@ Base.zero(a::Symbolic) = 0
3335

3436
#_____________________________________________
3537
# Division
36-
Base.:(/)(x::T, y::T) where {T<:Symbolic} = (x == y) ? 1 : promote(T)(:*, stripiden.([x, y^-1])) |> simplify
38+
Base.:(/)(x::T, y::T) where {T<:Symbolic} = promote(T)(:*, stripiden.([x, y^-1])) |> simplify
3739

3840

3941
#_____________________________________________

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# tests
2+
13
# [[file:~/Documents/Julia/scrap.org::*tests][tests:1]]
24
using Symbolics, Test
35

@@ -14,6 +16,8 @@ end
1416
@test (2x + y) - 8x == -6x + y
1517
@test x^2 * y * x^-1 == x*y
1618
@test ((2x + 3)^2 + 4(2x + 3)^2) * (2x + 3)^3 == (2 * x + 3)^5 * 5
19+
@test x * x^-4 == x^-3
20+
@test (x^y)^2/x == x^(y*2 - 1)
1721
end
1822

1923
@testset "Function Algebra" begin

0 commit comments

Comments
 (0)