ratsimp bad, rectform good



On  19 May 2009, Richard Fateman  wrote:
---------------------------------------------------
May be related to your starting expression being ambiguous. There are 6
values for (-1)^(1/6).
There are as many as 36 values for the expression.  (Though probably
fewer distinct ones).
-------------------------------------------------------
Maxima's solve function can return a solution in terms of z = (-1)^(1/n), 
which
is any (in general complex) number z such that z^n = -1.

Since complex roots must occur in pairs, if n is even (such as 6) all the 
roots
are complex, and rectform, realpart, and imagpart all pick out what I will 
call
the " k = 0 root", in the sense that the n roots are given by
  exp( %i * %pi * ( 1 + 2*k ) / n ), for k = 0, 1, 2, ..., (n - 1), for 
example.

If solve finds a solution in terms of (-1)^(1/n) for n odd, such as 
(-1)^(1/3),
maxima always simplifies this to (-1), the one real root, which is never the
"k = 0 " choice.

In other words for n odd the user has no choice, but for n even, the user
has a choice.  One such choice is to use that taken by rectform, realpart,
and imagpart.

The danger here, in working with a solution returned by solve which
includes unsimplified n'th roots of (-1), is that if you simplify first
using ratsimp, say, and  then use rectform, your final result may be
different than if you use rectform first, to "lock in" a specific choice.

This is all elementary, but can bite, and cause wasted time if one
does not recognise the danger.

Here is a simple example of this possible source of confusion:

(%i1) display2d:false$

(%i2) z (n) := (-1)^(1/n)$

(%i3) e : z(6) + 1/z(6);

(%o3) (-1)^(1/6)+1/(-1)^(1/6)

(%i4) ratsimp (rectform (e));

(%o4) sqrt(3)

(%i5) rectform (ratsimp (e));

(%o5) 0

(%i6) ratsimp (e);

(%o6) 0
================

and here is simple code for explorations

(%i7) rr (x) := ratsimp (rectform (x))$

(%i8) v(n,k) :=
    block( [fac],
      fac: ratsimp ( (1 + 2*k)/n),
           rr (exp ( %i*%pi*fac )))$

(%i9) vals (n) := for i:0 thru (n-1) do print (i," ",v(n,i))$

(%i10) z(3);
(%o10) -1
(%i11) vals(3)$
0   (sqrt(3)*%i+1)/2
1   -1
2   -(sqrt(3)*%i-1)/2

(%i12) z(4);
(%o12) (-1)^(1/4)

(%i13) rr(%);
(%o13) (sqrt(2)*%i+sqrt(2))/2

(%i14) vals(4)$
0   (sqrt(2)*%i+sqrt(2))/2
1   (%i-1)/sqrt(2)
2   -(sqrt(2)*%i+sqrt(2))/2
3   -(%i-1)/sqrt(2)

(%i15) z(5);
(%o15) -1

(%i16) vals(5)$
0   %i*sin(%pi/5)+cos(%pi/5)
1   %i*sin(3*%pi/5)+cos(3*%pi/5)
2   -1
3   cos(3*%pi/5)-%i*sin(3*%pi/5)
4   cos(%pi/5)-%i*sin(%pi/5)

(%i17) z(6);
(%o17) (-1)^(1/6)

(%i18) rr(%);
(%o18) (%i+sqrt(3))/2

(%i19) vals(6)$
0   (%i+sqrt(3))/2
1   %i
2   (%i-sqrt(3))/2
3   -(%i+sqrt(3))/2
4   -%i
5   -(%i-sqrt(3))/2

==================
Ted Woollett
p.s.  ratsimp is not bad and rectform is not bad ;)