packages feed

numerical-integration 0.1.0.2 → 0.1.1.0

raw patch · 5 files changed

+39/−21 lines, 5 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

+ Numerical.Integration: IntegralResult :: CDouble -> CDouble -> Int -> IntegralResult
+ Numerical.Integration: [_code] :: IntegralResult -> Int
+ Numerical.Integration: [_error] :: IntegralResult -> CDouble
+ Numerical.Integration: [_value] :: IntegralResult -> CDouble
+ Numerical.Integration: data IntegralResult
+ Numerical.Integration: instance GHC.Show.Show Numerical.Integration.IntegralResult
- Numerical.Integration: integration :: (Double -> Double) -> Double -> Double -> Double -> Int -> IO (Double, Double, Int)
+ Numerical.Integration: integration :: (CDouble -> CDouble) -> CDouble -> CDouble -> CDouble -> CInt -> IO IntegralResult

Files

CHANGELOG.md view
@@ -3,3 +3,10 @@ ## 0.1.0.0 - 2023-08-30  First release.+++## 0.1.1.0 - 2023-09-18++- New data type `IntegralResult` to store the result (value, error, code), instead of in a triplet.++- Replaced `Double` with `CDouble` and `Int` with `CInt`.
README.md view
@@ -1,13 +1,17 @@ # numerical-integration -One-dimensional numerical integration using the 'NumericalIntegration' C++ library.+One-dimensional numerical integration using the +['NumericalIntegration'](https://github.com/tbs1980/NumericalIntegration) C++ library.  ___ -***Example.*** Integrate x² between 0 and 1 with desired relative error 1e-10 and -using 200 subdivisions.+***Example.*** Integrate x² between 0 and 1 with desired relative error 1e-5 and +using 200 subdivisions. Exact value: 1/3.  ```haskell-example :: IO (Double, Double, Int) -- value, error estimate, error code-example = integration (\x -> x*x) 0 1 1e-10 200+example :: IO IntegralResult -- value, error estimate, error code+example = integration (\x -> x*x) 0 1 1e-5 200+-- IntegralResult {_value = 0.3333333333333334, _error = 3.7007434154171895e-15, _code = 0} ```++The error code 0 indicates the success.
cpp/integration.cpp view
@@ -16,7 +16,7 @@ 
  public:
   Integrand(std::function<double(double)>& f_) : f(f_) {}
-  double operator()(const double x) const { return f(x); }
+  double operator()(const double& x) const { return f(x); }
 };
 
 double integration(double f(double),
numerical-integration.cabal view
@@ -1,6 +1,6 @@ cabal-version:       2.2 name:                numerical-integration-version:             0.1.0.2+version:             0.1.1.0 synopsis:            Numerical integration. description:         One-dimensional numerical integration using the 'NumericalIntegration' C++ library. homepage:            https://github.com/stla/numerical-integration#readme
src/Numerical/Integration.hs view
@@ -1,34 +1,41 @@ {-# LANGUAGE ForeignFunctionInterface #-} module Numerical.Integration-  (integration)+  (integration, IntegralResult(..))   where import           Foreign.Marshal.Alloc (free, mallocBytes) import           Foreign.Ptr           (FunPtr, Ptr, freeHaskellFunPtr) import           Foreign.Storable      (peek, sizeOf)+import           Foreign.C             (CDouble(..), CInt(..))                +data IntegralResult = IntegralResult {+  _value :: CDouble,+  _error :: CDouble,+  _code  :: Int+} deriving Show+ foreign import ccall safe "wrapper" funPtr-    :: (Double -> Double) -> IO(FunPtr (Double -> Double))+    :: (CDouble -> CDouble) -> IO (FunPtr (CDouble -> CDouble))  foreign import ccall safe "integration" c_integration-    :: FunPtr (Double -> Double) -> Double -> Double -> Double -> Int-    -> Ptr Double -> Ptr Int -> IO Double+    :: FunPtr (CDouble -> CDouble) -> CDouble -> CDouble -> CDouble -> CInt+    -> Ptr CDouble -> Ptr CInt -> IO CDouble  -- | Numerical integration.-integration :: (Double -> Double)       -- ^ integrand-            -> Double                   -- ^ lower bound-            -> Double                   -- ^ upper bound-            -> Double                   -- ^ desired relative error-            -> Int                      -- ^ number of subdivisions-            -> IO (Double, Double, Int) -- ^ value, error estimate, error code+integration :: (CDouble -> CDouble)       -- ^ integrand+            -> CDouble                   -- ^ lower bound+            -> CDouble                   -- ^ upper bound+            -> CDouble                   -- ^ desired relative error+            -> CInt                      -- ^ number of subdivisions+            -> IO IntegralResult        -- ^ value, error estimate, error code integration f lower upper relError subdiv = do-  errorEstimatePtr <- mallocBytes (sizeOf (0 :: Double))-  errorCodePtr <- mallocBytes (sizeOf (0 :: Int))+  errorEstimatePtr <- mallocBytes (sizeOf (0 :: CDouble))+  errorCodePtr <- mallocBytes (sizeOf (0 :: CInt))   fPtr <- funPtr f   result <-     c_integration fPtr lower upper relError subdiv errorEstimatePtr errorCodePtr   errorEstimate <- peek errorEstimatePtr-  errorCode <- peek errorCodePtr-  let out = (result, errorEstimate, errorCode)+  errorCode <- fromIntegral <$> peek errorCodePtr+  let out = IntegralResult {_value = result, _error = errorEstimate, _code = errorCode}   free errorEstimatePtr   free errorCodePtr   freeHaskellFunPtr fPtr