diff options
Diffstat (limited to 'lib/Numeric/GSL/Polynomials.hs')
-rw-r--r-- | lib/Numeric/GSL/Polynomials.hs | 54 |
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 | {- | | ||
4 | Module : Numeric.GSL.Polynomials | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : uses ffi | ||
11 | |||
12 | Polynomials. | ||
13 | |||
14 | <http://www.gnu.org/software/gsl/manual/html_node/General-Polynomial-Equations.html#General-Polynomial-Equations> | ||
15 | |||
16 | -} | ||
17 | ----------------------------------------------------------------------------- | ||
18 | module Numeric.GSL.Polynomials ( | ||
19 | polySolve | ||
20 | ) where | ||
21 | |||
22 | import Data.Packed.Internal | ||
23 | import Complex | ||
24 | import 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 | |||
34 | The 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), | ||
39 | 0.30901699437494734 :+ 0.9510565162951536, | ||
40 | 0.30901699437494734 :+ (-0.9510565162951536), | ||
41 | 1.0 :+ 0.0]@ | ||
42 | |||
43 | -} | ||
44 | polySolve :: [Double] -> [Complex Double] | ||
45 | polySolve = toList . polySolve' . fromList | ||
46 | |||
47 | polySolve' :: Vector Double -> Vector (Complex Double) | ||
48 | polySolve' 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 | |||
54 | foreign import ccall "gsl-aux.h polySolve" c_polySolve:: TVCV | ||