An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130703/3e0ac748/attachment.pl>
nth root of matrix
4 messages · Sachinthaka Abeywardana, arun, David Winsemius +1 more
?sign(a)*abs(a)^(1/3) #????????? [,1] #[1,] -1.000000 #[2,] -1.259921 #[3,] -1.442250 A.K. ----- Original Message ----- From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, July 2, 2013 11:11 PM Subject: [R] nth root of matrix Hi all, I want to do the following: a=matrix(c(-1,-2,-3)) a^(1/3) #get 3rd root of numbers[,1] [1,]? NaN [2,]? NaN [3,]? NaN All I get is NaNs, what is the proper way of doing this? Would like to retain the fact that it is a matrix if possible (not a requirement though). Thanks, Sachin ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Jul 2, 2013, at 8:11 PM, Sachinthaka Abeywardana wrote:
Hi all, I want to do the following: a=matrix(c(-1,-2,-3)) a^(1/3) #get 3rd root of numbers[,1] [1,] NaN [2,] NaN [3,] NaN All I get is NaNs, what is the proper way of doing this? Would like to retain the fact that it is a matrix if possible (not a requirement though).
?complex a=matrix(c(-1+0i,-2+0i,-3+0i)) David Winsemius Alameda, CA, USA
On 7/2/2013 9:24 PM, David Winsemius wrote:
On Jul 2, 2013, at 8:11 PM, Sachinthaka Abeywardana wrote:
Hi all, I want to do the following: a=matrix(c(-1,-2,-3)) a^(1/3) #get 3rd root of numbers[,1] [1,] NaN [2,] NaN [3,] NaN All I get is NaNs, what is the proper way of doing this? Would like to retain the fact that it is a matrix if possible (not a requirement though).
?complex a=matrix(c(-1+0i,-2+0i,-3+0i))
I tried that. The problem is that there are 3 different cube
roots in the complex plane, and a^(1/3) only gives one of them. See
Wikipedia, "roots of unity" or the examples in the help file for
"newton_raphson {elliptic}".
I assume that Sachinthaka wants the real roots. Try the following:
n <- 3 # n must be an odd integer for this to work
a=matrix(c(-1,-2,-3))
as <- sign(a)
ab <- abs(a)
cr <- as*(ab^(1/n))
> cr
[,1]
[1,] -1.000000
[2,] -1.259921
[3,] -1.442250
cr^n
Hope this helps. Spencer Graves
David Winsemius Alameda, CA, USA
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.