[R] Odp: ^ operator

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Nov 16 13:55:30 CET 2009


On 16-Nov-09 11:40:29, Petr PIKAL wrote:
> Hi
> AFAIK, this is issue of the preference of operators. 
> 
> r-help-bounces at r-project.org napsal dne 16.11.2009 11:24:59:
> 

Not in this case (see below), though of course in general "-" takes
precedence over "^", so, for example, in the expression

  -2^(1/3)

the "-" is applied first, giving (-2); and then "^" is applied
next, giving (-2)^(1/3). There is a work-round (see below).

>> Hi,
>> I want to apply ^ operator to a vector but it is applied to
>> some of the elements correctly and to some others, it generates
>> NaN. Why is it not able to calculate -6.108576e-05^(1/3) even
>> though it exists?

It only exists (in the real domain) if "^" takes precedence over "-"
which (in R) it does not!

>>  tmp
>> [1] -6.108576e-05  4.208762e-05  3.547092e-05  7.171101e-04 
> -1.600269e-03
>> > tmp^(1/3)
>> [1]        NaN 0.03478442 0.03285672 0.08950802        NaN
> 
> This computes (-a)^(1/3) which is not possible in real numbers.

In this example, that is not accurate. "tmp" has already been
defined, and contains numbers which are already stored as negative
numbers, so "-" is no longer on the scene as an operator, before
"^" is applied; the issue of precedence of "-" over "^" is no
longer present. The NaN arises from x^(1/3) where x is negative.

> You have to use as.complex(tmp)^(1/3) to get a result.
> 
>> > -6.108576e-05^(1/3)
>> [1] -0.03938341

This is not the result I get:

  as.complex(tmp)^(1/3)
# [1] 0.01969171+0.03410703i 0.03478442+0.00000000i
# [3] 0.03285672+0.00000000i 0.08950802+0.00000000i
# [5] 0.05848363+0.10129662i

> this is actually
> -(6.108576e-05^(1/3))
> 
> Regards
> Petr

It is possible to work round the problem without using as.complex
which can introduce complications -- see above, and also:

  x <- (-1)
  x^(1/3)
  # [1] NaN
  as.complex(x)^(1/3)
  # [1] 0.5+0.8660254i

  as.complex(-1)^(1/2)
  # [1] 0+1i

which you would not want if you are working throughout in real
numbers (you would want the result "-1" instead). Although, in the
mathematics of complex numbers, (-1)^(1/3) has three values, one
of which is -1, R only returns a single value.

However, you would have to define a new operator, called say "%^%":

  "%^%"<-function(X,x){sign(X)*(abs(X)^x)}

  tmp <- c(-6.108576e-05, 4.208762e-05, 3.547092e-05,
            7.171101e-04, -1.600269e-03)
  tmp%^%(1/3)
  # [1] -0.03938341  0.03478442  0.03285672  0.08950802 -0.11696726

The definition of "%^%" forces "^" to take precedence over "-",
by in effect removing "-" from the scene until "^" has done its
work. But, if you hope to rely on this, note that if you apply
to 'tmp' any function in which the ordinary "^" will be used on
a negative number, you will still have the same problem.

Note: Trying to redefine "^" will not work, since invoking the
result initiates an infinite recursion:

  "^"<- function(X,x){sign(X)*(abs(X)^x)}
  ## (This definition will be accepted by R)
  tmp%^%(1/3)
  # Error: evaluation nested too deeply: infinite recursion /
  #   options(expressions=)?

It's not a clean situatio, but I hope the above helps!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 16-Nov-09                                       Time: 12:55:25
------------------------------ XFMail ------------------------------




More information about the R-help mailing list