2 Module : Gargantext.Prelude.Utils
3 Description : Useful Tools near Prelude of the project
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Nice optimization of the Fibonacci function.
13 Gabriel Gonzales, Blazing fast Fibonacci numbers using Monoids, 2020-04,
14 http://www.haskellforall.com/2020/04/blazing-fast-fibonacci-numbers-using.html
15 (This post illustrates a nifty application of Haskell’s standard library to solve a numeric problem.)
23 module Gargantext.Prelude.Fibonacci where
27 import qualified Data.Monoid as Monoid
28 import qualified Data.Semigroup as Semigroup
30 -------------------------------------------------------------
31 fib' :: Integer -> Integer
34 fib' n = fib (n-1) + fib (n-2)
35 -------------------------------------------------------------
38 data Matrix2x2 = Matrix
39 { x00 :: Integer, x01 :: Integer
40 , x10 :: Integer, x11 :: Integer
43 instance Monoid.Monoid Matrix2x2 where
50 instance Semigroup.Semigroup Matrix2x2 where
51 Matrix l00 l01 l10 l11 <> Matrix r00 r01 r10 r11 =
53 { x00 = l00 * r00 + l01 * r10, x01 = l00 * r01 + l01 * r11
54 , x10 = l10 * r00 + l11 * r10, x11 = l10 * r01 + l11 * r11
57 fib :: Integer -> Integer
58 fib n = x01 (Semigroup.mtimesDefault n matrix)