Skip to content

Communative Matrix Multiplcation

9 messages · Berend Hasselman, Nordlund, Dan (DSHS/RDA), David Reiner +2 more

#
On 14-08-2012, at 19:26, Doran, Harold wrote:

            
I ran this in a clean environment

<code>
library(Matrix)

A1 <- new("dsCMatrix"
   , i = c(0L, 1L, 2L, 3L, 0L, 1L, 4L, 2L, 3L, 5L)
   , p = c(0L, 1L, 2L, 3L, 4L, 7L, 10L)
   , Dim = c(6L, 6L)
   , Dimnames = list(NULL, NULL)
   , x = c(5, 5, 5, 5, 5, 5, 10, 5, 5, 10)
   , uplo = "U"
   , factors = list()
)

A2 <- new("dgeMatrix"
   , x = c(1.41313955042629, 1.23647532592053, 1.36413787785255, 0.986247245800629,
2.64961487634682, 2.35038512365318, 1.23647532592053, 1.55872699943971,
0.579347997632991, 1.62544967700677, 2.79520232536024, 2.20479767463976,
1.36413787785255, 0.579347997632991, 2.75106274522123, 0.305451379293231,
1.94348587548554, 3.05651412451446, 0.986247245800629, 1.62544967700677,
0.305451379293232, 2.08285169789937, 2.6116969228074, 2.3883030771926,
2.64961487634682, 2.79520232536024, 1.94348587548554, 2.6116969228074,
5.44481720170706, 4.55518279829294, 2.35038512365318, 2.20479767463976,
3.05651412451446, 2.3883030771926, 4.55518279829294, 5.44481720170706
)
   , Dim = c(6L, 6L)
   , Dimnames = list(NULL, NULL)
   , factors = list()
)

D <- new("ddiMatrix"
   , diag = "N"
   , Dim = c(6L, 6L)
   , Dimnames = list(NULL, NULL)
   , x = c(5, 5, 5, 5, 10, 10)
)

A1
A2
D

T1 <- (A1 - A2) %*% D
T2 <- (A1 %*% D) - (A2 %*% D)
identical(T1, T2)
</code>

and qot this result

[1] TRUE


so it's now up to you to find out what you did exactly.

Berend
#
Thanks, Berend. I updated my R and got same result. I reposted the old sessionInfo(), which was an older version. But, same code, same matrices, different result
#
Probably FAQ 7.31.  Try 

all.equal(T1,T2)


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
No, not at all actually. I gave small enough matrices so you can eyeball it, but they are way off. It seems to be a problem (ahem, maybe even a bug) with some matrix operations in older version of R.

Newer R yields true for the comparison.
#
As a mathematician, I have to correct the subject line to 'Distributive Matrix operations'

-- David


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Doran, Harold
Sent: Tuesday, August 14, 2012 12:55 PM
To: Berend Hasselman
Cc: 'r-help at r-project.org'
Subject: Re: [R] Communative Matrix Multiplcation

Thanks, Berend. I updated my R and got same result. I reposted the old sessionInfo(), which was an older version. But, same code, same matrices, different result
______________________________________________
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.


This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates.

THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
#
> As a mathematician, I have to correct the subject line
    > to 'Distributive Matrix operations' -- David

Yes, indeed; 
that was *really* disturbing to me as well, about this thread!

{in addition to the fact that he should have used all.equal()
 and not identical()  as someone had pointed out already ..}

And at last: Yes,  some of the methods for Diagonal() matrices in the
 'Matrix' package did have bugs, in earlier versions.

NOTE: You can (and often should) update packages also if for
     some reason you cannot update your version of R!

Martin Maechler,  ETH Zurich

    > -----Original Message----- From:
    > r-help-bounces at r-project.org
    > [mailto:r-help-bounces at r-project.org] On Behalf Of
    > Doran, Harold Sent: Tuesday, August 14, 2012 12:55 PM
    > To: Berend Hasselman Cc: 'r-help at r-project.org' Subject:
    > Re: [R] Communative Matrix Multiplcation

    > Thanks, Berend. I updated my R and got same result. I
    > reposted the old sessionInfo(), which was an older
    > version. But, same code, same matrices, different result

    >> -----Original Message----- From: Berend Hasselman
    >> [mailto:bhh at xs4all.nl] Sent: Tuesday, August 14, 2012
    >> 1:49 PM To: Doran, Harold Cc: 'r-help at r-project.org'
    >> Subject: Re: [R] Communative Matrix Multiplcation
    >> 
    >>
>> On 14-08-2012, at 19:26, Doran, Harold wrote:
>> 
    >> >
    >> > I'm not seeing why the following occurs:
    >> >
    >>  > T1 <- (A1 - A2) %*% D 
    >>  > T2 <- (A1 %*% D) - (A2 %*% D)
    >>  > identical(T1, T2) 
    >>  [1] FALSE 
    >>
#
Thanks to you both. I'm not sure all.equal() was right for this situation, is it? If the differences were to the right of the decimal and differed as a function of precision, then I see all.equal() being the right function. But, the differences in the reproducible code were to the left of the decimal, but a lot, so I thought identical was perhaps better.

Martin, there seem to be some issues with calculations in the updated Matrix package. I'll send an email to matrix authors. It seems some calculations that were almost instantaneous with older versions are now causing R to hang. I'll provide reproducible examples.
#
You are right that the difference was dramatic, and hence your
posting was well in place, well, would have, iff it had been
earlier, when that version of Matrix was current
 (... looking it
  up: the next version after the one you used, was
  0.999375-47, CRAN-released: 2011-02-23;
  and there were *many* releases since, notably 1.0-0 & 1.0-1
  the latter released 2011-10-18.)

However, using identical() for comparisons of the results of
double precision computations is almost always "wrong", in
principle:  identical() very often can give FALSE notably from
such simple changes as algebraical shuffling (commutative,
associative, distributive laws), and indeed these things are
covered by the BFMFAQ (By Far Most Frequently Asked Question) 7.31.
Thank you in advance!
Martin