Skip to content

Conversation

jverzani
Copy link
Contributor

@jverzani jverzani commented Mar 2, 2025

This implements suggestion in #225 to have a step for early termination in diff if possible.
This passes on #225; the additional allocations to check for early termination would be paid in every instance; whereas the suggestion of #225 only pays off in certain situations. (The allocation could be avoided by some call to basic_has_symbol, but even that had a performance cost)

  • Avoids use of BasicType to check that x is a symbol; as this is more performant
  • adds a diff(ex) method for case where ex has just 0 or 1 symbol
  • extends diff interface closer to SymPy by allowing expressions like diff(ex, x, 2, y).
  • does not cover diff(ex, (x,2)) interface of SymPy, as that method is used otherwise. (would fix were it not breaking)
  • adds tests
  • clean up one verbose test by using repr

@jverzani jverzani mentioned this pull request Mar 2, 2025
@jverzani jverzani requested a review from isuruf March 3, 2025 20:55
@jverzani
Copy link
Contributor Author

jverzani commented Mar 3, 2025

There are two things here: one extends the interface, the other closes #225. The latter adds a check for whether the expression is zero. As such, it may be a performance concern. I didn't benchmark, But if there is a concern, I can modify this PR.

@jverzani
Copy link
Contributor Author

This last commit to this PR has a noticeable speed up for basic differentation:

Before

julia> @btime diff(sin(x),x)
  1.243 μs (5 allocations: 88 bytes)

After

julia> @btime diff(sin(x), x)
  833.444 ns (3 allocations: 48 bytes)

This is gained by avoiding the use of BasicType for dispatch, which is slower than desired, as it needs to allocate a string to check if x is a symbol.

@jverzani jverzani changed the title early termination in diff Speed up diff; extend interface Mar 17, 2025
@jverzani
Copy link
Contributor Author

Hi @isuruf any objections to my merging this?

@jverzani jverzani mentioned this pull request Mar 19, 2025
@jverzani
Copy link
Contributor Author

Hi @isuruf, can I merge this?

Copy link
Member

@isuruf isuruf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One question. Wouldn't it be better to have an inplace diff and call it multiple times to reduce the number of allocations?

@jverzani
Copy link
Contributor Author

Great idea! (I always forget Basic is mutable). I'll try and investigate soon

end

function diff(b1::SymbolicType, b2::SymbolicType, n::Integer, xs...)
diff(diff(b1,b2,n), xs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use a mutating diff as well, but not necessary for this PR

Copy link
Member

@isuruf isuruf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment

@jverzani jverzani merged commit 267124c into symengine:master Apr 2, 2025
10 checks passed
@jverzani jverzani deleted the issue_225 branch April 2, 2025 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants