Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplication of polynomials is terrible #7

Closed
aljce opened this issue Mar 22, 2018 · 1 comment
Closed

Multiplication of polynomials is terrible #7

aljce opened this issue Mar 22, 2018 · 1 comment

Comments

@aljce
Copy link

aljce commented Mar 22, 2018

Specifically the instance for vectors is awful

polyTimes :: Semiring a => Vector a -> Vector a -> Vector a
polyTimes x y
  = if Vector.null x then x else if Vector.null y then y else
      Vector.cons (times a b) (Vector.map (a *) q + Vector.map (* b) p + (Vector.cons zero (polyTimes p q)))
  where
    a = Vector.unsafeHead x
    b = Vector.unsafeHead y
    p = (\t d -> if Vector.null t then d else t) (Vector.tail x) (Vector.empty)
    q = (\t d -> if Vector.null t then d else t) (Vector.tail y) (Vector.empty)

Technically this is the optimum in terms of asymptotic analysis.
Proof:

C(0) = 0
C(n) = n (Vector.cons) + n (Vector.map) + n (Vector.map) + n (Vector.cons) + C(n - 1) (recursive call)

That recurrence represent the worst case running time of polyTimes. Solving this with the master method C(n) = Θ(n^2). This being said the asymptotics hide some serious constants. You copy the vector into a new vector two times each recursive call because of Vector.cons. Never use Vector.cons it has to copy the entire vector into a new one that is just one element bigger. Instead rethink the algorithm to not use Vector.cons. You could completely redesign the alg or use ST and mutable vectors.

@chessai
Copy link
Owner

chessai commented Mar 26, 2018

fixed

@chessai chessai closed this as completed Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants