From 88a39a5d1bdf7799a2e1f38d420f4392bb9c35bc Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 24 May 2015 19:32:33 +0200 Subject: fromInt --- packages/base/src/Data/Packed/Internal/Numeric.hs | 12 +++++++- packages/base/src/Data/Packed/Internal/Vector.hs | 35 ++++++++++++++++------- packages/base/src/Numeric/LinearAlgebra/Data.hs | 3 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs index 3e6c90c..fec8c05 100644 --- a/packages/base/src/Data/Packed/Internal/Numeric.hs +++ b/packages/base/src/Data/Packed/Internal/Numeric.hs @@ -36,7 +36,7 @@ module Data.Packed.Internal.Numeric ( Convert(..), Complexable(), RealElement(), - roundVector, + roundVector, fromInt, RealOf, ComplexOf, SingleOf, DoubleOf, IndexOf, CInt, Extractor(..), (??), @@ -179,6 +179,7 @@ class Element e => Container c e -- element by element inverse tangent arctan2' :: Fractional e => c e -> c e -> c e cmod' :: Integral e => e -> c e -> c e + fromInt' :: c CInt -> c e -------------------------------------------------------------------------- @@ -215,6 +216,7 @@ instance Container Vector CInt cmod' m x | m /= 0 = vectorMapValI ModVS m x | otherwise = error $ "cmod 0 on vector of size "++(show $ dim x) + fromInt' = id instance Container Vector Float where @@ -246,6 +248,7 @@ instance Container Vector Float divide = vectorZipF Div arctan2' = vectorZipF ATan2 cmod' = undefined + fromInt' = int2floatV @@ -279,6 +282,7 @@ instance Container Vector Double divide = vectorZipR Div arctan2' = vectorZipR ATan2 cmod' = undefined + fromInt' = int2DoubleV instance Container Vector (Complex Double) @@ -311,6 +315,7 @@ instance Container Vector (Complex Double) divide = vectorZipC Div arctan2' = vectorZipC ATan2 cmod' = undefined + fromInt' = complex . int2DoubleV instance Container Vector (Complex Float) where @@ -342,6 +347,7 @@ instance Container Vector (Complex Float) divide = vectorZipQ Div arctan2' = vectorZipQ ATan2 cmod' = undefined + fromInt' = complex . int2floatV --------------------------------------------------------------- @@ -379,6 +385,7 @@ instance (Num a, Element a, Container Vector a) => Container Matrix a cmod' m x | m /= 0 = liftMatrix (cmod' m) x | otherwise = error $ "cmod 0 on matrix "++shSize x + fromInt' = liftMatrix fromInt' emptyErrorV msg f v = @@ -416,6 +423,9 @@ arctan2 = arctan2' cmod :: (Integral e, Container c e) => e -> c e -> c e cmod = cmod' +fromInt :: (Container c e) => c CInt -> c e +fromInt = fromInt' + -- | like 'fmap' (cannot implement instance Functor because of Element class constraint) cmap :: (Element b, Container c e) => (e -> b) -> c e -> c b diff --git a/packages/base/src/Data/Packed/Internal/Vector.hs b/packages/base/src/Data/Packed/Internal/Vector.hs index 7b0df64..ac35596 100644 --- a/packages/base/src/Data/Packed/Internal/Vector.hs +++ b/packages/base/src/Data/Packed/Internal/Vector.hs @@ -18,7 +18,7 @@ module Data.Packed.Internal.Vector ( mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_, foldVector, foldVectorG, foldLoop, foldVectorWithIndex, createVector, vec, - asComplex, asReal, float2DoubleV, double2FloatV, + asComplex, asReal, float2DoubleV, double2FloatV, double2IntV, int2DoubleV, float2IntV, int2floatV, stepF, stepD, stepI, condF, condD, condI, conjugateQ, conjugateC, cloneVector, @@ -231,20 +231,35 @@ asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2) --------------------------------------------------------------- float2DoubleV :: Vector Float -> Vector Double -float2DoubleV v = unsafePerformIO $ do - r <- createVector (dim v) - app2 c_float2double vec v vec r "float2double" - return r +float2DoubleV = tog c_float2double double2FloatV :: Vector Double -> Vector Float -double2FloatV v = unsafePerformIO $ do +double2FloatV = tog c_double2float + +double2IntV :: Vector Double -> Vector CInt +double2IntV = tog c_double2int + +int2DoubleV :: Vector CInt -> Vector Double +int2DoubleV = tog c_int2double + +float2IntV :: Vector Float -> Vector CInt +float2IntV = tog c_float2int + +int2floatV :: Vector CInt -> Vector Float +int2floatV = tog c_int2float + + +tog f v = unsafePerformIO $ do r <- createVector (dim v) - app2 c_double2float vec v vec r "double2float2" + app2 f vec v vec r "tog" return r - -foreign import ccall unsafe "float2double" c_float2double:: TFV -foreign import ccall unsafe "double2float" c_double2float:: TVF +foreign import ccall unsafe "float2double" c_float2double :: TFV +foreign import ccall unsafe "double2float" c_double2float :: TVF +foreign import ccall unsafe "int2double" c_int2double :: CV CInt (CV Double (IO CInt)) +foreign import ccall unsafe "double2int" c_double2int :: CV Double (CV CInt (IO CInt)) +foreign import ccall unsafe "int2float" c_int2float :: CV CInt (CV Float (IO CInt)) +foreign import ccall unsafe "float2int" c_float2int :: CV Float (CV CInt (IO CInt)) --------------------------------------------------------------- diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs index 80877cb..7bc0497 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Data.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs @@ -63,6 +63,7 @@ module Numeric.LinearAlgebra.Data( -- * Conversion Convert(..), roundVector, + fromInt, -- * Misc arctan2, rows, cols, @@ -81,6 +82,6 @@ import Numeric.LinearAlgebra.Util hiding ((&),(#)) import Data.Complex import Numeric.Sparse import Data.Packed.Internal.Vector(Idxs) -import Data.Packed.Internal.Numeric(CInt,Extractor(..),(??)) +import Data.Packed.Internal.Numeric(CInt,Extractor(..),(??),fromInt) -- cgit v1.2.3