summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/Polynomials.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Polynomials.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Polynomials.hs58
1 files changed, 58 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/Polynomials.hs b/packages/hmatrix/src/Numeric/GSL/Polynomials.hs
new file mode 100644
index 0000000..290c615
--- /dev/null
+++ b/packages/hmatrix/src/Numeric/GSL/Polynomials.hs
@@ -0,0 +1,58 @@
1{-# LANGUAGE CPP, ForeignFunctionInterface #-}
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 Data.Complex
24import System.IO.Unsafe (unsafePerformIO)
25
26#if __GLASGOW_HASKELL__ >= 704
27import Foreign.C.Types (CInt(..))
28#endif
29
30{- | Solution of general polynomial equations, using /gsl_poly_complex_solve/.
31
32For example, the three solutions of x^3 + 8 = 0
33
34>>> polySolve [8,0,0,1]
35[(-2.0) :+ 0.0,1.0 :+ 1.7320508075688776,1.0 :+ (-1.7320508075688776)]
36
37
38The example in the GSL manual: To find the roots of x^5 -1 = 0:
39
40>>> polySolve [-1, 0, 0, 0, 0, 1]
41[(-0.8090169943749472) :+ 0.5877852522924731,
42(-0.8090169943749472) :+ (-0.5877852522924731),
430.30901699437494756 :+ 0.9510565162951535,
440.30901699437494756 :+ (-0.9510565162951535),
451.0000000000000002 :+ 0.0]
46
47-}
48polySolve :: [Double] -> [Complex Double]
49polySolve = toList . polySolve' . fromList
50
51polySolve' :: Vector Double -> Vector (Complex Double)
52polySolve' v | dim v > 1 = unsafePerformIO $ do
53 r <- createVector (dim v-1)
54 app2 c_polySolve vec v vec r "polySolve"
55 return r
56 | otherwise = error "polySolve on a polynomial of degree zero"
57
58foreign import ccall unsafe "gsl-aux.h polySolve" c_polySolve:: TVCV