Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

early termination in diff #279

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Base: diff
## what is the rest of the interface. This does:
## diff(ex, x, n) f^(n)
## diff(ex, x, y, ...) f_{xy...} # also diff(ex, (x,y))
## no support for diff(ex, x,n1, y,n2, ...), but can do diff(ex, (x,y), (n1, n2))
## Support for diff(ex, x,n1, y,n2, ...),
## but can also do diff(ex, (x,y), (n1, n2))

function diff(b1::SymbolicType, b2::BasicType{Val{:Symbol}})
a = Basic()
Expand All @@ -23,12 +24,21 @@ function diff(b1::SymbolicType, b2::SymbolicType, n::Integer=1)
n > 1 && return diff(diff(b1, BasicType(b2)), BasicType(b2), n-1)
end

function diff(b1::SymbolicType, b2::SymbolicType, n::Integer, xs...)
diff(diff(b1,b2,n), xs...)
end

function diff(b1::SymbolicType, b2::SymbolicType, b3::SymbolicType)
isa(BasicType(b3), BasicType{Val{:Integer}}) ? diff(b1, b2, N(b3)) : diff(b1, (b2, b3))
if isa(BasicType(b3), BasicType{Val{:Integer}})
diff(b1, b2, N(b3))
else
diff(b1, (b2, b3))
end
end

diff(b1::SymbolicType, b2::SymbolicType, b3::SymbolicType, b4::SymbolicType, b5...) =
diff(b1, (b2,b3,b4,b5...))
function diff(b1::SymbolicType, b2::SymbolicType, b3::SymbolicType, bs...)
diff(diff(b1,b2,b3), bs...)
end

## mixed partials
diff(ex::SymbolicType, bs::Tuple) = reduce((ex, x) -> diff(ex, x), bs, init=ex)
Expand Down
14 changes: 9 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ c = Basic(-5)
@test abs(c) == 5
@test abs(c) != 4

show(a)
println()
show(b)
println()
# test show
a = x^2 + x/2 - x*y*5
b = diff(a, x)
repr("text/plain", a) == (1/2)*x - 5*x*y + x^2
repr("text/plain", b) == 1/2 + 2*x - 5*y

@test 1 // x == 1 / x
@test x // 2 == (1//2) * x
Expand All @@ -89,7 +90,10 @@ ex = sin(x*y)
@test diff(ex, x, 2) == diff(diff(ex,x), x)
@test diff(ex, x, n) == diff(diff(ex,x), x)
@test diff(ex, x, y) == diff(diff(ex,x), y)
@test diff(ex, x, y,x) == diff(diff(diff(ex,x), y), x)
@test diff(ex, x, y, x) == diff(diff(diff(ex,x), y), x)
@test diff(ex, x, 2, y, 3) == diff(ex, x,x,y,y,y)
@test diff(ex, x, n, y, 3) == diff(ex, x,x,y,y,y)
@test diff(ex, x, 2, y, x) == diff(ex, x,x,x,y)
@test series(sin(x), x, 0, 2) == x
@test series(sin(x), x, 0, 3) == x - x^3/6

Expand Down
Loading