File Exchange Pick of the Week

Our best user submissions

Variable Precision Arithmetic without the Symbolic Math Toolbox?

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!

In his introduction to his submission on variable precision integer arithmetic, John D'Errico wrote:

"Every once in a while, I've wanted to do arithmetic with large integers with magnitude exceeding that which can fit into 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 know how "simple" his solution was to implement, but John created a new variable class (vpi) that is quite easy to use.

Actually, John's code is pretty impressive. Using his object class, one can easily manipulate very large integers--often larger than MATLAB is comfortable with. Consider:

a = 17^17
a =
  8.2724e+020
class(a)
ans =
double
try
    factor(a)
catch ME
    disp(ME.message);
    disp('Not gonna do it...wouldn''t be prudent!')
end
The maximum value of n allowed is 2^32.
Not gonna do it...wouldn't be prudent!
b = vpi(17)
b =
17
class(b)
ans =
vpi
b^17
ans =
827240261886336764177
factor(b^17)
ans =
     1    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17    17

The Symbolic Math Toolbox

If you need more, or faster, there's always the Symbolic Math Toolbox. John's VPI class deals only with scalar integers, whereas VPA (from our Toolbox) also supports floating point numbers; it can work with Pi, for instance. Also, it supports matrices 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, John has given us a nice tool for manipulating integers. I'm interested in hearing your comments on this. Let us know how this might play a role in your workflows.




Published with MATLAB® 7.7

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.