Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My Haskell implementation represents numbers as the ratio of products of ordered integer streams. For example, I represent 3!/(4*5) as (R numerator=[1,2,3] denominator=[4,5]). In this representation, multiplication becomes merging the numerator and denominator streams and then canceling the first stream by the second. In this way I can remove all cancelable original terms in the Pcutoff formula before finally multiplying the terms that remain.
*FishersExactTest> fac 6 R {numer = [2,3,4,5,6], denom = []} *FishersExactTest> fac 3 R {numer = [2,3], denom = []} *FishersExactTest> fac 6 `rdivide` fac 3 R {numer = [4,5,6], denom = []}
Here's the example from the MathWorld page:
*FishersExactTest> rpCutoff [ [5,0], [1,4] ] R {numer = [2,3,4,5], denom = [7,8,9,10]} *FishersExactTest> fromRational . toRatio $ it 2.3809523809523808e-2
The code:
module FishersExactTest (pCutoff) where import Data.Ratio import Data.List (transpose) pCutoff = toRatio . rpCutoff rpCutoff rows = facproduct (rs ++ cs) `rdivide` facproduct (n:xs) where rs = map sum rows cs = map sum (transpose rows) n = sum rs xs = concat rows -- cells facproduct = rproduct . map fac fac n | n < 2 = runit | otherwise = R [2..n] [] -- I represent numbers as ratios of products of integer streams -- R [1,2,3] [4,5] === (1 * 2 * 3) / (4 * 5) data Rops = R { numer :: [Int], denom :: [Int] } deriving Show runit = R [] [] -- the number 1 toRatio (R ns ds) = bigProduct ns % bigProduct ds bigProduct = product . map toInteger -- multiplication is merging numerator and denominator streams -- and then canceling the first by the second rtimes (R xns xds) (R yns yds) = uncurry R $ (merge xns yns) `cancel` (merge xds yds) rproduct = foldr rtimes runit -- division is multiplication by the inverse rdivide x (R yns yds) = rtimes x (R yds yns) -- helpers merge (x:xs) (y:ys) | x < y = x : merge xs (y:ys) | otherwise = y : merge (x:xs) ys merge [] ys = ys merge xs [] = xs cancel (x:xs) (y:ys) | x == y = cancel xs ys | x < y = let (xs', ys') = cancel xs (y:ys) in (x:xs', ys') | otherwise = let (xs', ys') = cancel (x:xs) ys in (xs', y:ys') cancel xs ys = (xs, ys)

In reply to Re^7: Algorithm for cancelling common factors between two lists of multiplicands by tmoertel
in thread Algorithm for cancelling common factors between two lists of multiplicands by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-16 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found