{"id":4462,"date":"2012-08-13T17:59:46","date_gmt":"2012-08-13T16:59:46","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=4462"},"modified":"2012-08-13T17:59:46","modified_gmt":"2012-08-13T16:59:46","slug":"variable-precision-qr-decomposition-in-matlab","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=4462","title":{"rendered":"Variable precision QR decomposition in MATLAB"},"content":{"rendered":"<p>Someone recently contacted me complaining that MATLAB could not do a <a href=\"http:\/\/en.wikipedia.org\/wiki\/QR_decomposition\">QR factorisation<\/a> of a <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/symbolic\/f1-5556.html\">variable precision arithmetic<\/a> matrix.\u00a0 Double precision matrices work fine of course:<\/p>\n<pre>&gt;&gt; A=[2 1 3;-1 0 7; 0 -1 -1];\r\n&gt;&gt; [Q R]=qr(A)\r\n\r\nQ =\r\n   -0.8944   -0.1826    0.4082\r\n    0.4472   -0.3651    0.8165\r\n         0    0.9129    0.4082\r\n\r\nR =\r\n   -2.2361   -0.8944    0.4472\r\n         0   -1.0954   -4.0166\r\n         0         0    6.5320<\/pre>\n<p>Variable precision matrices do not (I&#8217;m using MATLAB 2012a and the symbolic toolbox here).<\/p>\n<pre>&gt;&gt; a=vpa([2 1 3;-1 0 7; 0 -1 -1]);\r\n&gt;&gt; [Q R]=qr(a)\r\nUndefined function 'qr' for input arguments of type 'sym'.<\/pre>\n<p>It turns out that MATLAB and the symbolic toolbox CAN do variable precision QR factorisation&#8230;.it&#8217;s just hidden a bit.  The following very simple function, <a href=\"https:\/\/www.walkingrandomly.com\/images\/matlab\/vpa_qr.m\">vpa_qr.m<\/a>, shows how to get at it<\/p>\n<pre>function [ q,r ] = vpa_qr( x )\r\nresult = feval(symengine,'linalg::factorQR',x);\r\nq=result(1);\r\nr=result(2);\r\nend<\/pre>\n<p>Let&#8217;s see how that does<\/p>\n<pre>&gt;&gt; a=vpa([2 1 3;-1 0 7; 0 -1 -1]);\r\n&gt;&gt; [Q R]=vpa_qr(a);<\/pre>\n<p>I&#8217;ve suppressed the output because it&#8217;s so large but it has definitely worked.  Let&#8217;s take a look at the first element of Q for example<\/p>\n<pre>&gt;&gt; Q(1)\r\n\r\nans =\r\n0.89442719099991587856366946749251<\/pre>\n<p>Which is correct to the default number of variable precision digits, 32.\u00a0 Of course we could change this to anything we like using the <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/symbolic\/digits.html\">digits<\/a> function.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/images\/matlab\/vpa_qr.m\">Download vpa_qr.m<\/a> (needs symbolic toolbox)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Someone recently contacted me complaining that MATLAB could not do a QR factorisation of a variable precision arithmetic matrix.\u00a0 Double precision matrices work fine of course: &gt;&gt; A=[2 1 3;-1 0 7; 0 -1 -1]; &gt;&gt; [Q R]=qr(A) Q = -0.8944 -0.1826 0.4082 0.4472 -0.3651 0.8165 0 0.9129 0.4082 R = -2.2361 -0.8944 0.4472 0 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,11,7],"tags":[],"class_list":["post-4462","post","type-post","status-publish","format-standard","hentry","category-math-software","category-matlab","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-19Y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4462"}],"version-history":[{"count":3,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4462\/revisions"}],"predecessor-version":[{"id":4465,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4462\/revisions\/4465"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}