{"id":2405,"date":"2009-03-13T13:18:23","date_gmt":"2009-03-13T13:18:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/03\/13\/variable-precision-arithmetic-without-the-symbolic-toolbox\/"},"modified":"2009-03-13T13:19:57","modified_gmt":"2009-03-13T13:19:57","slug":"variable-precision-arithmetic-without-the-symbolic-toolbox","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/03\/13\/variable-precision-arithmetic-without-the-symbolic-toolbox\/","title":{"rendered":"Variable Precision Arithmetic without the Symbolic Math Toolbox?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week provides a subset of the functionality of our <a href=\"https:\/\/www.mathworks.com\/products\/symbolic\/\">Symbolic Math Toolbox<\/a>, but doesn't require any tools besides core MATLAB!\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>In his introduction to his <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22725-variable-precision-integer-arithmetic\">submission on variable precision integer arithmetic<\/a>, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679\">John D'Errico<\/a> wrote:\r\n   <\/p>\r\n   <p>\"Every once in a while, I've wanted to do arithmetic with large integers with magnitude exceeding that which can fit into\r\n      MATLAB's standard data types. Since I don't have the symbolic toolbox, the simple solution was to write it in MATLAB.\" I don't\r\n      know how \"simple\" his solution was to implement, but John created a new variable class (vpi) that is quite easy to use.\r\n   <\/p>\r\n   <p>Actually, John's code is pretty impressive. Using his object class, one can easily manipulate very large integers--often larger\r\n      than MATLAB is comfortable with. Consider:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = 17^17<\/pre><pre style=\"font-style:oblique\">a =\r\n  8.2724e+020\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">class(a)<\/pre><pre style=\"font-style:oblique\">ans =\r\ndouble\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    factor(a)\r\n<span style=\"color: #0000FF\">catch<\/span> ME\r\n    disp(ME.message);\r\n    disp(<span style=\"color: #A020F0\">'Not gonna do it...wouldn''t be prudent!'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">The maximum value of n allowed is 2^32.\r\nNot gonna do it...wouldn't be prudent!\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b = vpi(17)<\/pre><pre style=\"font-style:oblique\">b =\r\n17\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">class(b)<\/pre><pre style=\"font-style:oblique\">ans =\r\nvpi\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b^17<\/pre><pre style=\"font-style:oblique\">ans =\r\n827240261886336764177\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">factor(b^17)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17\r\n<\/pre><p><b>The Symbolic Math Toolbox<\/b><\/p>\r\n   <p>If you need more, or faster, there's always the Symbolic Math Toolbox. John's VPI class deals only with scalar integers, whereas\r\n      VPA (from our Toolbox) also supports floating point numbers; it can work with Pi, for instance. Also, it supports matrices\r\n      and n-dim arrays of numbers, and allows you to combine symbolic and numeric variables in expressions like \"1.2*x+3.4*y\". Nonetheless,\r\n      John has given us a nice tool for manipulating integers. I'm interested in hearing your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2405#respond\"><b>comments<\/b><\/a> on this. Let us know how this might play a role in your workflows.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_30e3866d8c46429ead33f57ce9b8f661() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='30e3866d8c46429ead33f57ce9b8f661 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 30e3866d8c46429ead33f57ce9b8f661';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Brett Shoelson';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_30e3866d8c46429ead33f57ce9b8f661()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.7<br><\/p>\r\n<\/div>\r\n<!--\r\n30e3866d8c46429ead33f57ce9b8f661 ##### SOURCE BEGIN #####\r\n%% Variable Precision Arithmetic without the Symbolic Toolbox?\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week provides a subset of the functionality\r\n% of our <https:\/\/www.mathworks.com\/products\/symbolic\/ Symbolic Math Toolbox>, but doesn't require any tools besides\r\n% core MATLAB!\r\n\r\n%%\r\n% In his introduction to his <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22725-variable-precision-integer-arithmetic submission on variable precision\r\n% integer arithmetic>, <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679 John D'Errico> wrote:\r\n\r\n%%\r\n% \"Every once in a while, I've wanted to do arithmetic with large\r\n% integers with magnitude exceeding that which can fit into\r\n% MATLAB's standard data types. Since I don't have the symbolic\r\n% toolbox, the simple solution was to write it in MATLAB.\" I\r\n% don't know how \"simple\" his solution was to implement, but John\r\n% created a new variable class (vpi) that is quite easy to use.\r\n\r\n%%\r\n% Actually, John's code is pretty impressive. Using his object\r\n% class, one can easily manipulate very large integersREPLACE_WITH_DASH_DASHoften larger\r\n% than MATLAB is comfortable with. Consider:\r\n\r\na = 17^17\r\n%%\r\nclass(a)\r\n%%\r\ntry\r\n    factor(a)\r\ncatch ME\r\n    disp(ME.message);\r\n    disp('Not gonna do it...wouldn''t be prudent!')\r\nend\r\n%%\r\nb = vpi(17)\r\n%%\r\nclass(b)\r\n%%\r\nb^17\r\n%%\r\nfactor(b^17)\r\n\r\n%%\r\n% *The Symbolic Math Toolbox*\r\n%\r\n% If you need more, or faster, there's always the Symbolic\r\n% Math Toolbox. John's VPI class deals only with scalar integers,\r\n% whereas VPA (from our Toolbox) also supports floating\r\n% point numbers; it can work with Pi, for instance. Also, it supports matrices and n-dim arrays of\r\n% numbers, and allows you to combine symbolic and numeric\r\n% variables in expressions like \"1.2*x+3.4*y\". Nonetheless, John\r\n% has given us a nice tool for manipulating integers. I'm\r\n% interested in hearing your <comments> on this. Let us know how\r\n% this might play a role in your workflows.\r\n##### SOURCE END ##### 30e3866d8c46429ead33f57ce9b8f661\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Brett's Pick this week provides a subset of the functionality of our Symbolic Math Toolbox, but doesn't require any tools besides core MATLAB!\r\n      \r\n   \r\n   In his introduction to his... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/03\/13\/variable-precision-arithmetic-without-the-symbolic-toolbox\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2405"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2405"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2405\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}