From 197e88c3b56d28840217010a2871c6ea3a4dd1a4 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 21 May 2014 10:30:55 +0200 Subject: update dependencies, move examples etc --- packages/gsl/src/Numeric/GSL/Polynomials.hs | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 packages/gsl/src/Numeric/GSL/Polynomials.hs (limited to 'packages/gsl/src/Numeric/GSL/Polynomials.hs') diff --git a/packages/gsl/src/Numeric/GSL/Polynomials.hs b/packages/gsl/src/Numeric/GSL/Polynomials.hs new file mode 100644 index 0000000..b1be85d --- /dev/null +++ b/packages/gsl/src/Numeric/GSL/Polynomials.hs @@ -0,0 +1,57 @@ +{- | +Module : Numeric.GSL.Polynomials +Copyright : (c) Alberto Ruiz 2006 +License : GPL +Maintainer : Alberto Ruiz +Stability : provisional + +Polynomials. + + + +-} + + +module Numeric.GSL.Polynomials ( + polySolve +) where + +import Data.Packed +import Numeric.GSL.Internal +import Data.Complex +import System.IO.Unsafe (unsafePerformIO) + +#if __GLASGOW_HASKELL__ >= 704 +import Foreign.C.Types (CInt(..)) +#endif + +{- | Solution of general polynomial equations, using /gsl_poly_complex_solve/. + +For example, the three solutions of x^3 + 8 = 0 + +>>> polySolve [8,0,0,1] +[(-2.0) :+ 0.0,1.0 :+ 1.7320508075688776,1.0 :+ (-1.7320508075688776)] + + +The example in the GSL manual: To find the roots of x^5 -1 = 0: + +>>> polySolve [-1, 0, 0, 0, 0, 1] +[(-0.8090169943749472) :+ 0.5877852522924731, +(-0.8090169943749472) :+ (-0.5877852522924731), +0.30901699437494756 :+ 0.9510565162951535, +0.30901699437494756 :+ (-0.9510565162951535), +1.0000000000000002 :+ 0.0] + +-} +polySolve :: [Double] -> [Complex Double] +polySolve = toList . polySolve' . fromList + +polySolve' :: Vector Double -> Vector (Complex Double) +polySolve' v | dim v > 1 = unsafePerformIO $ do + r <- createVector (dim v-1) + app2 c_polySolve vec v vec r "polySolve" + return r + | otherwise = error "polySolve on a polynomial of degree zero" + +foreign import ccall unsafe "gsl-aux.h polySolve" c_polySolve:: TV (TCV Res) + -- cgit v1.2.3