rtoy / maxima

A Clone of Maxima's repo
Other
0 stars 0 forks source link

Trigreduce doesn't return principal value of atan(tan(...)) #1179

Open rtoy opened 4 months ago

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:48:52 Created by gustavklopp on 2022-05-28 06:20:05 Original: https://sourceforge.net/p/maxima/bugs/3981


Trying to get the argument of the product of 2 complex numbers produces the wrong result: image image link

So I multiply

3(\cos(\frac{5π}{13}) + i*\sin(\frac{5π}{13})

with

4(\cos(\frac{7π}{13}) + i*\sin(\frac{7π}{13})

The right answer for the argument should be \frac{12π}{13}, not \frac{25π}{13}.

You can try yourself with this Maxima file that I've joined: BUG.wxmx

Someone on Github of Wxmaxima suggested it's a problem with Maxima " switching to atan and atan getting the wrong result larger than pi/2." Github issue

My version of WxMaxima: 22.04.0 Maxima version: 5.46.0 (x86_64-unknown-linux-gnu) Maxima compiled using: ECL 21.2.1

EDIT (Robert Dodier): For reference, I'm appending here the content of the BUG.wxmx notebook.

Input:
             c:3*(cos(5*%pi/13)+%i*sin(5*%pi/13));

   Output:
               5 π        5 π
     3 (%i sin(---) + cos(---))
               13         13

Input:
             d:4*(cos(7*%pi/13)+%i*sin(7*%pi/13));

   Output:
               7 π        7 π
     4 (%i sin(---) + cos(---))
               13         13

Input:
             carg(c*d);

   Output:
                  7 π              5 π
              sin(---)         sin(---)
                  13               13
     π + atan(--------) + atan(--------)
                  7 π              5 π
              cos(---)         cos(---)
                  13               13

Input:
             trigreduce(%);

   Output:
     25 π
     ----
      13

Attachments:

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:48:53 Created by willisbl on 2022-05-28 10:53:24 Original: https://sourceforge.net/p/maxima/bugs/3981/#f58e


Thanks for the bug report.

The function trigreduce simplifies atan(tan(XXX)) to XXX. This simplification doesn’t respect the value of triginverses.

The user documentation for trigreduce doesn’t say anything about simplifying all atan(tan(XXX)) expressions to XXX, so I say this is a bug.

A possible fix to this bug is insert a check for the value of triginverses into sp1atrig (defined in trgred.lisp)

(defun sp1atrig (fn exp)
  (cond ((atom exp)
     (sp1atrig2 fn exp))
    ((and (eq fn (zl-get (caar exp) '$inverse)) (eq '$triginverses '$all))
     (sp1 (cadr exp)))
    (t (sp1atrig2 fn exp))))
rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:48:57 Created by willisbl on 2022-05-28 10:53:54 Original: https://sourceforge.net/p/maxima/bugs/3981/#ecc1


rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:00 Created by willisbl on 2022-05-28 11:16:19 Original: https://sourceforge.net/p/maxima/bugs/3981/#68a8


A quick test using the proposed change to sp1atrign gives a failure to rtest14, problem 376. The test istrigreduce(atan(tan(x+y+z))).

I think that's the only failure caused by this proposed change,

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:04 Created by richardgobeli on 2022-05-28 15:09:10 Original: https://sourceforge.net/p/maxima/bugs/3981/#6785


I don't think there is any problem with the conversion of atan(tan(x)) to x, but the check still needs to be done that the resulting x needs to be in range of atan and that is -pi/2 to pi/2. This is necessary because the atan2 function added pi knowing that atan needed to be negative angle.

It seems like the check of the resulting range of atan is not respected. The problem is that the atan(sin(7pi/13)/cos(7pi/13)) when reduced does not get atan(tan(x)). atan(tan(7pi/13)) does give -6pi/13.

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:07 Created by richardgobeli on 2022-09-13 00:24:54 Original: https://sourceforge.net/p/maxima/bugs/3981/#b40d


Here is example of using rules and set limits function. the limit to the atan2rule can also be-pi/2 to pi/2.

Attachments:

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:11 Created by macrakis on 2022-09-13 20:11:29 Original: https://sourceforge.net/p/maxima/bugs/3981/#647f


rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:15 Created by gustavklopp on 2022-09-30 04:04:07 Original: https://sourceforge.net/p/maxima/bugs/3981/#bf80


I'd like to know more about this bug. can someone explain me why "carg" produce this "atan (sin x/cos x) + pi"? The manual only explains how to use it but not HOW it is done: https://maxima.sourceforge.io/docs/manual/maxima_singlepage.html#index-carg

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-04 01:49:18 Created by rtoy on 2022-10-04 20:52:00 Original: https://sourceforge.net/p/maxima/bugs/3981/#a93e


I wonder where the extra %pi comes from. c*d is

12*(%i*sin((5*%pi)/13)+cos((5*%pi)/13))*(%i*sin((7*%pi)/13)+cos((7*%pi)/13))

If then take the carg of each component I would expect 0 + atan(tan(5*%pi/13)) + atan(tan(7*%pi/13)) which trigreduce would simply to 12*%pi/13. But carg(c*d) has an extra %pi.

Another weird result:

expand(exponentialize(c*d));
12*%e^((12*%i*%pi)/13)
carg(%);
atan(sin((12*%pi)/13)/cos((12*%pi)/13))+%pi;

Where does that %pi come from? The arg of the exponential is just 12*%pi/13, which is the expected answer.

Some more weird results:

carg(exp(7*%i*%pi/13));
atan(sin((7*%pi)/13)/cos((7*%pi)/13))+%pi

But

carg(exp(6*%i*%pi/13));
atan(sin((6*%pi)/13)/cos((6*%pi)/13))

It's almost as if the arg of the exponential is more than 6.5/13, then a %pi is added.