Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 81590101CB for ; Mon, 17 Feb 2014 03:49:19 +0000 (UTC) Received: (qmail 16814 invoked by uid 500); 17 Feb 2014 03:49:18 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 15635 invoked by uid 500); 17 Feb 2014 03:48:59 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 15608 invoked by uid 99); 17 Feb 2014 03:48:54 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Feb 2014 03:48:54 +0000 X-ASF-Spam-Status: No, hits=0.2 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT,LOTS_OF_MONEY,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of meyerjp3@gmail.com designates 209.85.192.47 as permitted sender) Received: from [209.85.192.47] (HELO mail-qg0-f47.google.com) (209.85.192.47) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Feb 2014 03:48:46 +0000 Received: by mail-qg0-f47.google.com with SMTP id 63so4270225qgz.6 for ; Sun, 16 Feb 2014 19:48:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-type:content-transfer-encoding:thread-index :content-language; bh=OK2QJBgZq/X5tCxpJ3MP9iqQJ43AXjgQP1jjiYKKqqk=; b=lqh47qKFR+HwMNojN3px/E5ezsQi0emy3X0pK4JeBMgcscHCmhSXwNAxyUTkZuljKI MX/EIGKdIu/M+5xbTzjNSI4wwV9uT/I10uZ8V38dI1nzL080IMy7mJTsf8rONusBTXiD NrRRB9wmLCulJb1HNUu7XF5GWoecZud4i5nF5zcBLubZzm3HiUbSLW/C9evzlxGXFKBO xjF5PDP5Q0Xra1w1rOHjQvyvNJtp5dtTnn/XoyT8C0H03NWZYjPZFI4aXtrRvackFAaP UaaTXXSyAfFZfXeLWMI153Z1hUL1KCq7WvC6rtfZT330sHdxjxywTgxvgJ5hI6BY1nfs zXow== X-Received: by 10.140.20.175 with SMTP id 44mr29829044qgj.46.1392608905787; Sun, 16 Feb 2014 19:48:25 -0800 (PST) Received: from R2D2 (c-98-249-58-20.hsd1.va.comcast.net. [98.249.58.20]) by mx.google.com with ESMTPSA id q10sm41356982qaj.13.2014.02.16.19.48.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 16 Feb 2014 19:48:25 -0800 (PST) From: "Patrick Meyer" To: "'Commons Developers List'" References: <000601cf29f2$b75aa830$260ff890$@gmail.com> <003301cf2a46$c42fd4a0$4c8f7de0$@gmail.com> In-Reply-To: Subject: RE: [math] trouble with SingularValueDecomposition Date: Sun, 16 Feb 2014 22:48:23 -0500 Message-ID: <005701cf2b93$1c269070$5473b150$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQMAP3IHSiZyyLPu6lPrF2bjZjNf6AGl2JrmAg36OSMB5ICTOpgp5BiA Content-Language: en-us X-Virus-Checked: Checked by ClamAV on apache.org Thanks again Ted.=20 For what it's worth I have been using the linear algebra features in CM = recently and they are really nice. Thanks to everyone who contributed to = it! -----Original Message----- From: Ted Dunning [mailto:ted.dunning@gmail.com]=20 Sent: Saturday, February 15, 2014 8:02 PM To: Commons Developers List Subject: Re: [math] trouble with SingularValueDecomposition Note that the only reason that the order is unconstrained is because the = two corresponding singular values are equal. Strictly speaking, for equal singular values, any unitary transformation = of the corresponding singular vectors are also valid singular vectors. On Sat, Feb 15, 2014 at 4:09 AM, Patrick Meyer = wrote: > Thanks Ted. As I mentioned my knowledge of SVD is limited, and I was=20 > not aware that it is OK to have a different order of the first two=20 > columns in the results (or the conditions under which the order=20 > doesn't matter). I am trying to track down a bug in some code and = that=E2=80=99s what led me to the SVD. > I guess I need to keep looking for the real bug. > > For completeness, my results R were the same as you reported. My=20 > results from CM are shown below and if you swap the first and second=20 > column, the results agree with R. > > U: > 0.9940594018965339 0.06774763124429131 -0.08518312016997649 > 0.10615872136916754 -0.7761401247896214 0.6215599991704858 > 0.02400481989869077 0.6269104921377042 0.778721390144956 > > V: > 0.9963653125425972 0.0 -0.08518312016997495 > 0.0531395658155507 -0.7815621241949481 0.6215599991704865 > 0.06657590034559915 0.6238274168581248 0.7787213901449556 > > > > -----Original Message----- > From: Ted Dunning [mailto:ted.dunning@gmail.com] > Sent: Saturday, February 15, 2014 2:17 AM > To: Commons Developers List > Subject: Re: [math] trouble with SingularValueDecomposition > > For what its worth, I tested the Mahout SVD which shares code lineage=20 > with the Commons Math implementation. > > The results I got were: > > > > > > > > > > > > > > > > > > > > > > > > > > *sum(abs(m - u * s * v')) =3D 4.31946146e-16S =3D 1.002319690998 > > 1.002319690998 1.000000000000 > > U =3D 0.994059401897 0.067747631244 > > -0.085183120170 0.106158721369 -0.776140124790 0.621559999170 > > 0.024004819899 0.626910492138 0.778721390145 V =3D = 0.996365312543 > > 0.000000000000 -0.085183120170 0.053139565816 -0.781562124195 > > 0.621559999170 0.066575900346 0.623827416858 0.778721390145* > > > Note that the residue of the reconstruction is excellently small. =20 > This indicates that the result is correct. > > > If you compare these to the R results, > > > > > > > > > > > > > > > > > > > > > > > > *[1] 1.0023196909980066 1.0023196909980066 1.0000000000000000$u > > [,1] [,2] [,3][1,] > > 0.067747631244291326 -0.994059401896534967 0.085183120169970525=20 > > [2,] > > -0.776140124789635122 -0.106158721369163295=20 > > -0.621559999170469113[3,] > > 0.626910492137687125 -0.024004819898688426 -0.778721390144969994$v > > [,1] [,2] [,3] [1,] > > 0.00000000000000000 -0.996365312542597747 0.085183120169970497[2,] > > -0.78156212419496163 -0.053139565815546450 -0.621559999170469668[3,] > > 0.62382741685810772 -0.066575900345596822 -0.778721390144969550* > > > These are identical to the previous results except that the first two=20 > singular values are equal which means that the order of the=20 > corresponding left and right singular vectors are different and there=20 > are sign changes in the singular vectors. > > My guess is that you will get the same results in Apache Commons Math. > > > > On Fri, Feb 14, 2014 at 6:07 PM, Patrick Meyer = wrote: > > > Hi, > > > > > > > > I am using the SingularValueDecomposition class with a matrix but it = > > gives me a different result than R. My knowledge of SVD is limited,=20 > > so any advice is welcomed. > > > > > > > > Here's the method in Java > > > > > > > > public void svdTest(){ > > > > > > > > double[][] x =3D { > > > > {1.0, -0.053071807862720116, 0.04236086650321309}, > > > > {0.05307180786272012, 1.0, 0.0058054424137053435}, > > > > {-0.04236086650321309, -0.005805442413705342, 1.0} > > > > }; > > > > > > > > RealMatrix X =3D new Array2DRowRealMatrix(x); > > > > > > > > SingularValueDecomposition svd =3D new=20 > > SingularValueDecomposition(X); > > > > > > > > RealMatrix U =3D svd.getU(); > > > > for(int i=3D0;i > > > for(int j=3D0;j > > > System.out.print(U.getEntry(i,j) + " "); > > > > } > > > > System.out.println(); > > > > } > > > > > > > > System.out.println(); > > > > System.out.println(); > > > > RealMatrix V =3D svd.getV(); > > > > for(int i=3D0;i > > > for(int j=3D0;j > > > System.out.print(V.getEntry(i,j) + " "); > > > > } > > > > System.out.println(); > > > > } > > > > > > > > > > > > } > > > > > > > > > > > > And here's the function in R. > > > > > > > > x<-matrix(c( > > > > 1.0, -0.053071807862720116, 0.04236086650321309, > > > > 0.05307180786272012, 1.0, 0.0058054424137053435, > > > > -0.04236086650321309, -0.005805442413705342, 1.0), > > > > nrow=3D3, byrow=3DTRUE) > > > > svd(x) > > > > > > > > Does anyone know why I am getting different results for U and V? I=20 > > am using commons math 3.1. > > > > > > > > Thanks, > > > > Patrick > > > > > > > > > > > > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org