Zulip Chat Archive

Stream: general

Topic: fast fractional number computation


Kendall Frey (Jul 25 2020 at 02:09):

What's the fastest way to perform calculations involving fractional numbers? Specifically, I defined a complex number as a pair of rational numbers, and performed a Mandelbrot set test for 100000 iterations, which took several seconds. This is pretty slow, so what would be a better solution?

Scott Morrison (Jul 25 2020 at 02:10):

:four_leaf_clover:? (i.e. wait until Lean4 which will be much faster?)

Scott Morrison (Jul 25 2020 at 02:10):

How are you doing the calculation?

Kendall Frey (Jul 25 2020 at 02:13):

It's pretty simple, no thought to optimization.

def comp_rat :=  × 

notation `` := comp_rat

def cadd :     
| (r₁, i₁) (r₂, i₂) := (r₁ + r₂, i₁ + i₂)

def cmul :     
| (r₁, i₁) (r₂, i₂) := (r₁ * r₂ - i₁ * i₂, r₁ * i₂ + r₂ * i₁)

Kendall Frey (Jul 25 2020 at 02:14):

I see mathlib has a floating point implementation, so I may try that one as well for comparison

Reid Barton (Jul 25 2020 at 02:15):

I assume you're using #eval (or equivalent lean --run)

Kendall Frey (Jul 25 2020 at 02:15):

lean --run, yeah

Kendall Frey (Jul 25 2020 at 02:53):

I tried native.float, but it generates errors like:

kernel failed to type check declaration 'x' this is usually due to a buggy tactic or a bug in the builtin elaborator
elaborated type:
  native.float
elaborated value:
  3927 / 1250
nested exception message:
invalid definition, it uses untrusted declaration 'native.float'

Can this error be suppressed or ignored in some way?

Bryan Gin-ge Chen (Jul 25 2020 at 02:54):

You might have to put meta in front of your definitions?

Mario Carneiro (Jul 25 2020 at 06:29):

The problem with rational number arithmetic is that the denominators (and usually numerators as well) grow exponentially in the number of operations

Mario Carneiro (Jul 25 2020 at 06:29):

You probably want to do some heuristic rounding and interval arithmetic once the denominator gets large enough

Mario Carneiro (Jul 25 2020 at 06:32):

What is your actual mandelbrot set test? I only see the core add/mul operations in your snippet. In order to know what kind of rounding is appropriate I would like to know how exactly you are doing the mandelbrot set test


Last updated: Dec 20 2023 at 11:08 UTC