Home > other >  No instance for (RealFloat (Ratio Integer))
No instance for (RealFloat (Ratio Integer))

Time:07-23

I'm using the stuff provided here by @DavidYoung. I have a function:

hypergeom ::
     forall a. (Eq a, Fractional a, BaseFrac a)
  => Int            -- truncation weight
  -> BaseFracType a -- alpha parameter (usually 2)
  -> [a]            -- "upper" parameters
  -> [a]            -- "lower" parameters
  -> [a]            -- variables (the eigenvalues)
  -> IO a

But when I run it with a = Complex Rational I get this error:

No instance for (RealFloat (Ratio Integer))
        arising from a use of `hypergeom'

What does that mean?

More precisely, here is the command I run:

ghci> import Data.Ratio
ghci> import Data.Complex
ghci> alpha = 2 % 1 :: Rational
ghci> a = 2 % 10 :  1 % 1 :: Complex Rational
ghci> b = 1 % 2 :  0 % 1 :: Complex Rational
ghci> c = 2 % 1 :  3 % 1 :: Complex Rational
ghci> x1 = 1 % 3 :  1 % 4 :: Complex Rational
ghci> x2 = 1 % 5 :  1 % 6 :: Complex Rational
ghci> hypergeom 10 alpha [a, b] [c] [x1, x2]

<interactive>:28:1: error:
    * No instance for (RealFloat (Ratio Integer))
        arising from a use of `hypergeom'
    * In the expression: hypergeom 10 alpha [a, b] [c] [x1, x2]
      In an equation for `it':
          it = hypergeom 10 alpha [a, b] [c] [x1, x2]

I'm pretty sure this worked in the past but I used Data.Complex.Generic (I don't use it anymore because it's not possible with a recent resolver).

CodePudding user response:

Haven't run the code, but pretty sure the issue is this:

Use of hypergeom requires a Fractional a instance. The relevant instance for Complex is:

instance RealFloat a => Fractional (Complex a)

So, we need a RealFloat Rational instance. But Rational = Ratio Integer. So we need a RealFloat (Ratio Integer) instance. There is no such instance in scope. Hence, error.

CodePudding user response:

I found a trick. One can use the cyclotomic numbers. They contain the complex numbers with rational real and imaginary parts.

instance BaseFrac Cyclotomic where
  type BaseFracType Cyclotomic = Rational
  inject x = gaussianRat x 0

example:

ghci> import Data.Complex.Cyclotomic
ghci> import Data.Ratio
ghci> alpha = 2%1
ghci> a = gaussianRat (2%7) (1%2)
ghci> b = gaussianRat (1%2) (0%1)
ghci> c = gaussianRat (2%1) (3%1)
ghci> x1 = gaussianRat (1%3) (1%4)
ghci> x2 = gaussianRat (1%5) (1%6)
ghci> hypergeom 10 alpha [a, b] [c] [x1, x2]
2636302089639370293242525873640165272590821169/2525202250482591646557047218608537600000000000   107068626184021125113299563281620084111568417/2525202250482591646557047218608537600000000000*e(4)
  • Related