Stream: helpdesk (published)

Topic: Symbolics.jl derivative cot(x)


view this post on Zulip Dale Black (Mar 18 2021 at 00:33):

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?

view this post on Zulip Mosè Giordano (Mar 18 2021 at 01:11):

(1+cot2(x))=(1+1tan2(x))=(1+cos2(x)sin2(x))=sin2(x)+cos2(x)sin2(x)=1sin2(x)=csc2(x)-(1 + \cot^2(x)) = - (1 + \frac{1}{\tan^2(x)}) = -(1 + \frac{\cos^2(x)}{\sin^2(x)}) = - \frac{\sin^2(x) + \cos^2(x)}{\sin^2(x)} = -\frac{1}{\sin^2(x)} = -\csc^2(x)

view this post on Zulip Mason Protter (Mar 18 2021 at 01:15):

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))

view this post on Zulip Expanding Man (Mar 18 2021 at 01:17):

it would be pretty simple to add... pretty sure you can even do it at runtime if you really want

view this post on Zulip Dale Black (Mar 18 2021 at 01:26):

Whoops, thank you

view this post on Zulip Mason Protter (Mar 18 2021 at 01:58):

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

view this post on Zulip Expanding Man (Mar 18 2021 at 02:00):

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

view this post on Zulip Expanding Man (Mar 18 2021 at 02:00):

but I did have a number of similar frustrations to the above when I was trying to figure out what I did wrong)

view this post on Zulip Mason Protter (Mar 18 2021 at 02:01):

Yes. That's part of the problem. It's currently very hard to tell the difference between bugs and user error.

view this post on Zulip Expanding Man (Mar 18 2021 at 02:01):

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

view this post on Zulip Mason Protter (Mar 18 2021 at 02:04):

I think the matching algorithm just has a bug related to subtraction though

view this post on Zulip Expanding Man (Mar 18 2021 at 02:09):

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!"

view this post on Zulip Expanding Man (Mar 18 2021 at 02:09):

thinks it's fair to call that a bug

view this post on Zulip Mason Protter (Mar 18 2021 at 02:49):

Huh. For some reason, I could have sworn I tried that

view this post on Zulip Shashi Gowda (Mar 18 2021 at 08:55):

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.

view this post on Zulip Shashi Gowda (Mar 18 2021 at 08:55):

also note that derivative will not automatically call simplify

view this post on Zulip Shashi Gowda (Mar 18 2021 at 08:56):

This is just what DiffRules seems to do.

view this post on Zulip Alessandro Cheli (Mar 19 2021 at 15:36):

@Shashi Gowda what Term data structure are you using now?

view this post on Zulip Shashi Gowda (Mar 20 2021 at 00:32):

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 06 2024 at 04:40 UTC