summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL/Polynomials.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/GSL/Polynomials.hs')
-rw-r--r--lib/Numeric/GSL/Polynomials.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/Numeric/GSL/Polynomials.hs b/lib/Numeric/GSL/Polynomials.hs
new file mode 100644
index 0000000..42694f0
--- /dev/null
+++ b/lib/Numeric/GSL/Polynomials.hs
@@ -0,0 +1,54 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
2-----------------------------------------------------------------------------
3{- |
4Module : Numeric.GSL.Polynomials
5Copyright : (c) Alberto Ruiz 2006
6License : GPL-style
7
8Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional
10Portability : uses ffi
11
12Polynomials.
13
14<http://www.gnu.org/software/gsl/manual/html_node/General-Polynomial-Equations.html#General-Polynomial-Equations>
15
16-}
17-----------------------------------------------------------------------------
18module Numeric.GSL.Polynomials (
19 polySolve
20) where
21
22import Data.Packed.Internal
23import Complex
24import Foreign
25
26{- | Solution of general polynomial equations, using /gsl_poly_complex_solve/. For example,
27 the three solutions of x^3 + 8 = 0
28
29@\> polySolve [8,0,0,1]
30[(-1.9999999999999998) :+ 0.0,
31 1.0 :+ 1.732050807568877,
32 1.0 :+ (-1.732050807568877)]@
33
34The example in the GSL manual: To find the roots of x^5 -1 = 0:
35
36@\> polySolve [-1, 0, 0, 0, 0, 1]
37[(-0.8090169943749475) :+ 0.5877852522924731,
38(-0.8090169943749475) :+ (-0.5877852522924731),
390.30901699437494734 :+ 0.9510565162951536,
400.30901699437494734 :+ (-0.9510565162951536),
411.0 :+ 0.0]@
42
43-}
44polySolve :: [Double] -> [Complex Double]
45polySolve = toList . polySolve' . fromList
46
47polySolve' :: Vector Double -> Vector (Complex Double)
48polySolve' v | dim v > 1 = unsafePerformIO $ do
49 r <- createVector (dim v-1)
50 c_polySolve // vec v // vec r // check "polySolve" [v]
51 return r
52 | otherwise = error "polySolve on a polynomial of degree zero"
53
54foreign import ccall "gsl-aux.h polySolve" c_polySolve:: TVCV