Hello, I wonder by chance if there is a way to reduce computing time for matrix addition or subtraction. With a lot of iterations, it would be helpful to reduce a little amount time. Simple example is as below n <- 2000 P <- matrix(rnorm(n*n),n,n) PP <- P %*% P M <- diag(n) - P R <- M + t(M) - diag(n) + PP I would like to reduce time in calculating R. Thanks, Yongwan
Gabor Grothendieck
2006-Nov-13 04:47 UTC
[R] Computing time for matrix addition or subtraction
This is slightly faster but not by much:
ad <- function(m, a) { diag(m) <- diag(m) + a; m }
R <- ad(P %*% P - t(P) - P, 1)
On 11/12/06, YONGWAN CHUN <chun.49 at osu.edu>
wrote:> Hello,
>
>
> I wonder by chance if there is a way to reduce computing time for matrix
addition or subtraction. With a lot of iterations, it would be helpful to reduce
a little amount time.
>
> Simple example is as below
>
> n <- 2000
> P <- matrix(rnorm(n*n),n,n)
> PP <- P %*% P
> M <- diag(n) - P
> R <- M + t(M) - diag(n) + PP
>
> I would like to reduce time in calculating R.
>
> Thanks,
>
> Yongwan
>
> ______________________________________________
> R-help at 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.html
> and provide commented, minimal, self-contained, reproducible code.
>
Prof Brian Ripley
2006-Nov-13 08:28 UTC
[R] Computing time for matrix addition or subtraction
On Sun, 12 Nov 2006, YONGWAN CHUN wrote:> I wonder by chance if there is a way to reduce computing time for matrix > addition or subtraction. With a lot of iterations, it would be helpful > to reduce a little amount time.Yes, by making use of an optimized BLAS: see the R-admin manual. On my (dual CPU) system this reduced your example from 36 to 6 seconds. BTW, it is the calculation of PP that is taking the most of time, not as in your subject line.> Simple example is as below > > n <- 2000 > P <- matrix(rnorm(n*n),n,n) > PP <- P %*% P > M <- diag(n) - P > R <- M + t(M) - diag(n) + PP > > I would like to reduce time in calculating R.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
not concerning your subject line, but function "crossprod" may be useful, too Matthias ----- original message -------- Subject: Re: [R] Computing time for matrix addition or subtraction Sent: Mon, 13 Nov 2006 From: Prof Brian Ripley<ripley at stats.ox.ac.uk>> On Sun, 12 Nov 2006, YONGWAN CHUN wrote: > > > I wonder by chance if there is a way to reduce computing time for matrix > > addition or subtraction. With a lot of iterations, it would be helpful > > to reduce a little amount time. > > Yes, by making use of an optimized BLAS: see the R-admin manual. On my > (dual CPU) system this reduced your example from 36 to 6 seconds. > > BTW, it is the calculation of PP that is taking the most of time, not as > in your subject line. > > > Simple example is as below > > > > n <- 2000 > > P <- matrix(rnorm(n*n),n,n) > > PP <- P %*% P > > M <- diag(n) - P > > R <- M + t(M) - diag(n) + PP > > > > I would like to reduce time in calculating R. > > -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > > ______________________________________________ > R-help at 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.html > and provide commented, minimal, self-contained, reproducible code. >--- original message end ----