Code covered by the BSD License  

Highlights from
FACTORIALRATIO

5.0

5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 2.75 KB File ID: #23018

FACTORIALRATIO

by Jos (10584)

 

18 Feb 2009 (Updated 18 Feb 2009)

Ratio of factorials, as in (a!b!c!...) / (d!e!f!g!...)

| Watch this File

File Information
Description

FACTORIALRATIO - ratio of factorial numbers
 
    FR = FACTORIALRATIO (A,B) calculates the ratio between products of
    factorials specified by A and B. A and B are lists of positive
    integers, with N and M elements, respectively. The number of values in
    each list may differ. The factorial ratio is based on the following formula:
 
               (A1! * A2! * ... * AN!)
       FR = -------------------------
               (B1! * B2! * ... * BM!)
 
     However, an approach is taken that increases the accuracy when A or B
     contain larger values, as mentioned in the help of FACTORIAL.
 
     Examples:
 
       f1 = factorialratio([10 8 6],[9 6 5]) ;
       f2 = 10 * 8 * 7 * 6 ; % by hand
       f3 = (factorial(10) * factorial(8) * factorial(6)) ./ ...
             (factorial(9) * factorial(6) * factorial(5)) ;
       disp('f1, f2, f3 = ') ; disp([f1 f2 f3])
 
 
       % Also for larger values FACTORIALRATIO works correctly
       f1 = factorialratio(150,143) ;
       f2 = prod(144:150) ; % by hand
       % where FACTORIAL lacks precision
       f3 = factorial(150) ./ factorial(143) ;
       disp('f1, f2, f3 = ') ; disp([f1 f2 f3])
       disp(' Differences with f2 = ') ; disp([f1-f2 f2-f2 f3-f2])
 
       % and even for extreme numbers things turn out well
       f1 = factorialratio(30000,29998) ;
       f2 = (30000 * 29999) ; % by hand
       % whereas calls to FACTORIAL fail miserably ...
       f3 = factorial(30000) ./ factorial(29998) ;
       disp('f1, f2, f3 = ') ; disp([f1 f2 f3])
 
     Note that, for instance, factorialratio(200,1) also fails, of course.
     For such occasions other engines have to be used.
      
     See also prod, factorial, gammaln

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
18 Feb 2009 John D'Errico

Good help as you would expect from Jos. Well written. An interesting solution for the computation itself.

18 Feb 2009 John D'Errico

Sorry, I forgot to point out what I feel to be one of the best features of this code - the copious internal comments that explain how it works.

I've often argued that the best MATLAB code should aim for a 1-1 ratio of comments to lines of code. That is, each line of code should have an associated comment line, at least in those places where the code is doing something tricky. This code goes beyond my aim. People interested in how to write good comments should look at what the author did here and learn from it.

Please login to add a comment or rating.
Updates
18 Feb 2009

fixed small bug in error checks

Tag Activity for this File
Tag Applied By Date/Time
factorial Jos (10584) 18 Feb 2009 12:54:34
product Jos (10584) 18 Feb 2009 12:54:34
large numbers Jos (10584) 18 Feb 2009 12:54:34
ratio Jos (10584) 18 Feb 2009 12:54:34
division Jos (10584) 18 Feb 2009 12:54:34
infinite Jos (10584) 18 Feb 2009 12:54:34
products Jos (10584) 18 Feb 2009 12:54:34
factor Jos (10584) 18 Feb 2009 12:54:34
factorials Jos (10584) 18 Feb 2009 12:54:34

Contact us at files@mathworks.com