From: Ken Knoblauch <knoblauch_at_lyon.inserm.fr>

Date: Wed 29 Jun 2005 - 22:16:49 EST

Hi

Date: Wed 29 Jun 2005 - 22:16:49 EST

Something like this is exploited very nicely in the mtx.exp
for matrix powers in the Malmig package, actually.

Hi

I have been wondering if there one can speed up calculating small powers of numbers such as x^8 using multiplication.

In addition, one can be a bit clever and calculate x^8 using only 3 multiplies.

look at this:

> f1 <- function(x){x*x*x*x*x*x*x*x}

* > f2 <- function(x){x^8}
** > f3 <- function(x){x2 <- x*x;x4 <- x2*x2;return(x4*x4)}
*

[so f1() and f2() and f3() are algebraically identical]

> a <- rnorm(1000000)

* > system.time(ignore <- f1(a))
*

[1] 0.50 0.17 2.88 0.00 0.00

> system.time(ignore <- f2(a))

[1] 0.31 0.03 1.40 0.00 0.00

> system.time(ignore <- f3(a))

[1] 0.10 0.07 0.18 0.00 0.00

[these figures show little variance from trial to trial]

I was expecting f2() and f3() to be about the same. I was not expecting a factor of 3 there!

anyone got any comments?

-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 ____________________ Ken Knoblauch Inserm U371, Cerveau et Vision Department of Cognitive Neurosciences 18 avenue du Doyen Lepine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: 06 84 10 64 10 http://www.lyon.inserm.fr/371/ ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlReceived on Wed Jun 29 22:19:43 2005

*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:33:06 EST
*