5.0

5.0 | 5 ratings Rate this file 42 downloads (last 30 days) File Size: 53.33 KB File ID: #24878

Fractions Toolbox

by Ben Petschel

 

30 Jul 2009 (Updated 14 Dec 2009)

Code covered by the BSD License  

create and manipulate fractions (K+N/D) using exact arithmetic

Download Now | Watch this File

File Information
Description

The fractions toolbox allows users to create and manipulate fractions and fraction arrays of the form K+N/D, e.g.

  fr(1,3) % returns 1 / 3
  fr(pi) % returns 3 + 4703 / 33215

All the standard arithmetic and comparison operations are valid:
   fr(1,3)+fr(1,2) % returns 5 / 6
   fr(1,3)>0.3 % returns 1

Linear equations:
   A = fr(ones(2),[2,3;5,7]);
   B = fr(ones(2,1),[11;13]);
   A\B % returns [-3+49/143; 4+37/143]
   lsq(fr([1;1]),[0;1]) % returns 1/2

The treatment of singular and non-square systems is different from that of the built-in "\" so please read the documentation, e.g. for reasons of personal preference "\" does not do least-squares by default - use lsq instead.

Partial fractions and arbitrary-base digits can be computed:
  [d,r]=digits(fr(1,7),4,3) % 4 digits of base-3 expansion of 1/7 plus remainder
  % returns d=[0,1,0,2] and r= 4/567

Continued fractions expansions of fractions and square roots:
  [cf,rep] = cfracsqrt(fr(13,5)) % continued fraction of sqrt(13/5)
  [r1,r2] = bestrat(cf,rep,1000) % best rational approximations with denominator limit 1000

A powerful feature of the toolbox is that the numerator and denominator can theoretically be any data types that accept the standard arithmetic and comparison operations as well as gcd and mod. For example, if you have John D'Errico's Variable Precision Integer Toolbox (20 July 2009 release or later; see link below):

   prod(fr(1,vpi(2:7)).^10)

ans =
      1 / 10575608481180064985917685760000000000

If there exists a suitably defined polynomial object, this toolbox could be used to perform partial fraction and series expansions of rational functions.

See the demo and help files for a full list of features.

The functions have been tested with doubles and vpi integers, but message me if you encounter any problems, and let me know how it goes with other data types.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Variable Precision Integer Arithmetic

MATLAB release MATLAB 7.9 (2009b)
Zip File Content  
Published M Files Fractions Toolbox
Other Files
@fr/abs.m,
@fr/bestrat.m,
@fr/ceil.m,
@fr/cfrac.m,
@fr/cfracsqrt.m,
@fr/conv.m,
@fr/cumprod.m,
@fr/cumsum.m,
@fr/digits.m,
@fr/disp.m,
@fr/display.m,
@fr/double.m,
@fr/eq.m,
@fr/factor.m,
@fr/find.m,
@fr/fix.m,
@fr/floor.m,
@fr/fr.m,
@fr/freduce.m,
@fr/frinv.m,
@fr/full.m,
@fr/ge.m,
@fr/gt.m,
@fr/isequal.m,
@fr/isfinite.m,
@fr/isinf.m,
@fr/isnan.m,
@fr/isunit.m,
@fr/iszero.m,
@fr/le.m,
@fr/lsq.m,
@fr/lt.m,
@fr/minus.m,
@fr/mldivide.m,
@fr/mpower.m,
@fr/mrdivide.m,
@fr/mtimes.m,
@fr/ne.m,
@fr/partial.m,
@fr/plus.m,
@fr/power.m,
@fr/prod.m,
@fr/rank.m,
@fr/rat.m,
@fr/rdivide.m,
@fr/round.m,
@fr/rref.m,
@fr/sign.m,
@fr/single.m,
@fr/sort.m,
@fr/sortrows.m,
@fr/sum.m,
@fr/times.m,
@fr/uminus.m,
@fr/unique.m,
@fr/uplus.m,
demo_fr.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
30 Jul 2009 John D'Errico

Nifty

31 Jul 2009 Khaled Hamed

It seems to require vpi to be in the Matlab path, or a previous instance of a vpi.

>> fr(1,7)
??? Error using ==> superiorto
Unknown class 'vpi' listed in 'SUPERIORTO'.

Error in ==> fr.fr at 201
superiorto('vpi','double','single','int8','uint8','int16', ...

31 Jul 2009 Ben Petschel

Hi Khaled, thanks for pointing that out (I forgot to run "rehash path" when testing without the vpi toolbox). I've submitted an update which should become available soon. Let me know if you have any further problems.

31 Jul 2009 Khaled Hamed

Very useful toolbox, especially with the added precision of the vpi toolbox.

I think the demo_fr.m file needs an "echo on" at the beginning to see the header of each of the demo problems, rather than answers only (followed by echo off at the end).

01 Aug 2009 Derek O'Connor

This is a very useful toolbox, especially when used with
John D'Errico's Variable Precision Integer Toolbox.

Here are two tests I ran :

   function z = RumpFrac(x,y)

   % Testing John D'Errico's Variable Precision Integer Toolbox
   % and Ben Petschel's Fractions Toolbox using
   % Rump's polynomial. Derek O'Connor Aug 01 2009

     x = fr(vpi(x));
     y = fr(vpi(y));

    R1 = (33375/100)*y^6+ x^2*(11*x^2*y^2-y^6- 121*y^4- 2)
    R2 = (55/10)*y^8
    R3 = x/(2*y)
    z1 = R1+R2
    z = z1 + R3;

%
% R1 =
% -7917111340668961361101134701524942850
% R2 =
% 7917111340668961361101134701524942848
% R3 =
% 1 + 11425 / 66192
% z1 =
% -2
% z =
% -1 + 11425 / 66192 = -54767/66192 --- Correct.

% z = -1.180591620717411e+021 without first two statements

and

function z = JuddFrac(x,y)
   % Testing John D'Errico's Variable Precision Integer Toolbox
   % and Ben Petschel's Fractions Toolbox using
   % Judd's polynomial. Derek O'Connor Aug 01 2009

     x = fr(vpi(x));
     y = fr(vpi(y));

 J1 = 1682*x*y^4
 J2 = 3*x^3
 J3 = 29*x*y^2
 J4 = - 2*x^5
 z1 = J1+J2
 z2 = z1+J3
 z3 = z2+J4
 z4 = z3+832
 z = z4/107751

% z = JuddFrac(192119201,35675640);
% J1 =
% 523460426438903533308340192814390277120000
% J2 =
% 21273236588999014470832803
% J3 =
% 7091078862999671298158400
% J4 =
% -523460426438903561672655644813075853992002
% z1 =
% 523460426438903554581576781813404747952803
% z2 =
% 523460426438903561672655644813076046111203
% z3 =
% 192119201
% z4 =
% 192120033
% z =
% 1783 --- Correct.

% z = 7.721506064908910e-003 without first two statements

01 Aug 2009 Matt Fig

Great work!

18 Aug 2009 Erdal Bizkevelci  
Please login to add a comment or rating.
Updates
31 Jul 2009

fixed bug that can occur when users do not have the VPI toolbox.

17 Aug 2009

added functions mldivide (\), rref, lsq

25 Aug 2009

Improved performance of times and other operations; added functions for dealing with continued fractions

14 Dec 2009

added rank function; bugfix for frinv handling non-double arrays

Tag Activity for this File
Tag Applied By Date/Time
fraction Ben Petschel 30 Jul 2009 10:52:35
rational Ben Petschel 30 Jul 2009 10:52:35
arithmetic Ben Petschel 30 Jul 2009 10:52:35
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com