Skip to content

Commit a48ef7d

Browse files
committed
add mutating diff!
1 parent 49fca4d commit a48ef7d

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/calculus.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@ import Base: diff
88
## Support for diff(ex, x,n1, y,n2, ...),
99
## but can also do diff(ex, (x,y), (n1, n2))
1010

11-
function diff(b1::SymbolicType, b2::Basic)
11+
12+
function diff!(a::Basic, b1::SymbolicType, b2::Basic)
1213
is_symbol(b2) || throw(ArgumentError("Must differentiate with respect to a symbol"))
13-
a = Basic()
1414
ret = ccall((:basic_diff, libsymengine), Int, (Ref{Basic}, Ref{Basic}, Ref{Basic}), a, b1, b2)
1515
return a
1616
end
1717

18+
function diff(b1::SymbolicType, b2::Basic)
19+
a = Basic()
20+
diff!(a, b1, b2)
21+
a
22+
end
23+
1824
function diff(b1::SymbolicType, b2::SymbolicType, n::Integer)
1925
n < 0 && throw(DomainError("n must be non-negative integer"))
20-
n==0 && return b1
26+
n == 0 && return b1
2127
x = Basic(b2)
22-
ex = diff(b1, x)
23-
n==1 && return ex
24-
return diff(ex, x, n-1)
28+
out = Basic()
29+
diff!(out, b1, x)
30+
for _ in (n-1):-1:1
31+
diff!(out, out, x)
32+
end
33+
out
2534
end
2635

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

0 commit comments

Comments
 (0)