I might just be making a silly mistake but this seems like an error
Symbolics.derivative(cot(x), x)
outputs -1 -(cot(x)^2)
but I expect it to output -(csc(x)^2)
Am I missing something?
The real problem is that simplify
doesn't know about this identity :sad:
julia> Symbolics.derivative(cot(x), x) == -(csc(x))^2 |> simplify
(-1 - ((cot(x))^2)) == (-((csc(x))^2))
it would be pretty simple to add... pretty sure you can even do it at runtime if you really want
Whoops, thank you
It actually appears ot be kinda tricky to solve because the matchers are so brittle. I don't actually know how to even match this expression now that they've changed the representation of terms so much.
julia> @rule(-1 - cot(~x)^2 => "match!")(-1 - cot(x)^2)
julia> @rule(-1 - cot(~x)^2 => "match!")(-(1 + cot(x)^2))
julia> @acrule(-1 - cot(~x)^2 => "match!")(-1 - cot(x)^2)
julia> @acrule(-1 - cot(~x)^2 => "match!")(-(1 + cot(x)^2))
cc @Shashi @Yingbo Ma
Yeah, I have found matching to be pretty rough, I really think we need some ways to "hook into" the matching function to see what goes wrong. I was thinking of opening an issue and exploring some options, but I hesitated because my biggest difficulty actually came from me idiotically not noticing something
but I did have a number of similar frustrations to the above when I was trying to figure out what I did wrong)
Yes. That's part of the problem. It's currently very hard to tell the difference between bugs and user error.
matching being finicky seems unavoidable because Julia's AST is so complicated, but currently it's just about impossible to figure out what goes wrong
I think the matching algorithm just has a bug related to subtraction though
I figured out what happens there, it reads some -
operators as +
and multiplication by -1
without recognizing the equivalence
julia> @rule(-1 + (-1)*cot(~x)^2 => "works!")(-1 - cot(x)^2)
"works!"
thinks it's fair to call that a bug
Huh. For some reason, I could have sworn I tried that
Yeah the matcher requires some love since we stopped using the straightforward Term datastructure. The performance benefits of the more complicated representation is immense, but shouldn’t have to come at the cost of expressability. That said only @Expanding Man ‘s rule would work with or simplify rules even without the new representation since the first thing we do is to canonicalize in this way.
also note that derivative will not automatically call simplify
This is just what DiffRules seems to do.
@Shashi Gowda what Term data structure are you using now?
It's a combination of 4 types: Term, Add, Mul and Pow -- they have different internal datastructures for performance. But they all look like istree
and support operation
, arguments
and similarterm
. :)
Last updated: Nov 22 2024 at 04:41 UTC