{-# OPTIONS_GHC -fglasgow-exts #-}------------------------------------------------------------------------------- |-- Module : Numeric.GSL.Histogram2D-- Copyright : (c) A. V. H. McPhail 2010-- License : GPL-style---- Maintainer : haskell.vivian.mcphail <at> gmail <dot> com-- Stability : provisional-- Portability : uses ffi---- GSL 2D histogram functions---- <http://www.gnu.org/software/gsl/manual/>-------------------------------------------------------------------------------moduleNumeric.GSL.Histogram2D(-- * CreationHistogram2D,emptyRanges,emptyLimits,fromRanges,fromLimits-- * Loading,addList,addVector,addListWeighted,addVectorWeighted-- * Marshalling,toMatrix,fromMatrix-- * Information,getBin,getXRange,getYRange,getXMax,getYMax,getXMin,getYMin,getXBins,getYBins,reset-- * Querying,find,count,prob,probPaired,countPaired,countInstance,probability,maxVal,maxBin,minVal,minBin-- * Statistics,xmean,ymean,xstddev,ystddev,covariance,sum,equalBins-- * Mathematics,add,subtract,multiply,divide,shift,scale-- * Files ,fwriteHistogram2D,freadHistogram2D,fprintfHistogram2D,fscanfHistogram2D-- * PDF,Histogram2DPDF,fromHistogram2D,sample)where-----------------------------------------------------------------------------importData.Packed.VectorimportData.Packed.MatriximportData.Packed.DevelopmentimportData.Packed()--import Numeric.LinearAlgebra.Algorithms hiding (multiply)--import Numeric.LinearAlgebra.Linear hiding (multiply,add,divide,scale)importNumeric.LinearAlgebrahiding(multiply,add,divide,scale)--import Numeric.Container --import Control.Monad--import Control.Monad(when)importData.BinaryimportForeignhiding(shift)--import Foreign.Storable--import Foreign.Ptr--import Foreign.ForeignPtr--import Foreign.Marshal.Alloc(alloca)importForeign.C.Types(CInt,CChar)--import Foreign.C.String(newCString,peekCString)importForeign.C.String(newCString)--import GHC.ForeignPtr (mallocPlainForeignPtrBytes)--import GHC.Base--import GHC.IOBaseimportPreludehiding(subtract,sum)-----------------------------------------------------------------------------instance(Storablea)=>Storable(a,a)wheresizeOfz=2*sizeOf(fstz)alignmentz=alignment(fstz)peekp=doletq=castPtrpa<-peekqb<-peekElemOffq1return(a,b)pokep(a,b)=doletq=(castPtrp)pokeqapokeElemOffq1b-----------------------------------------------------------------------------dataHist2DtypeHist2DHandle=PtrHist2D-- | A histogram structuredataHistogram2D=H{hxdim::{-# UNPACK #-}!Int-- ^ number of bins,hydim::{-# UNPACK #-}!Int-- ^ number of bins,hist::{-# UNPACK #-}!(ForeignPtrHist2D)}dataPDFtypePDFHandle=PtrPDF-- | A histogram-derived cumulative distribution function (CDF)dataHistogram2DPDF=P{pdf::{-# UNPACK #-}!(ForeignPtrPDF)}-----------------------------------------------------------------------------instanceEqHistogram2Dwhere(==)=equalBins{-
instance Num Histogram2D where
(+) = add
(-) = subtract
negate = flip scale (-1.0)
(*) = multiply
signum = error "can't signumm Histogram2D"
abs = error "can't abs Histogram2D"
fromInteger x = fromLimits (fromInteger x) (0,1)
instance Fractional Histogram2D where
fromRational x = fromLimits (round x) (0,fromRational x)
(/) = divide
-}-----------------------------------------------------------------------------instanceBinaryHistogram2Dwhereputh=dolet(rx,ry,w)=toMatrixhputrxputryputwget=dorx<-getry<-getw<-getreturn$!fromMatrixrxryw-----------------------------------------------------------------------------foreignimportccall"gsl-histogram2d.h gsl_histogram2d_alloc"histogram2d_new::CInt->CInt->IOHist2DHandleforeignimportccall"gsl-histogram2d.h &gsl_histogram2d_free"histogram2d_free::FunPtr(Hist2DHandle->IO())------------------------------------------------------------------------------- | create a histogram with n bins from ranges (x0->x1),(x1->x2),..,(xn->xn+1) and y0,..,yn+1fromRangesIO::VectorDouble->VectorDouble->IOHistogram2DfromRangesIOvw=doletsx=fromIntegral$dimv-1letsy=fromIntegral$dimw-1h<-histogram2d_newsxsyh'<-newForeignPtrhistogram2d_freehapp2(\xsxpysyp->withForeignPtrh'(\f->histogram2d_set_rangesfxpxsypys))vecvvecw"fromRanges"return$H(fromIntegralsx)(fromIntegralsy)h'-- | create a histogram with n bins and lower and upper limitsfromLimitsIO::Int->Int-- ^ number of bins->(Double,Double)-- ^ xmin, xmax->(Double,Double)-- ^ ymin, ymax->IOHistogram2DfromLimitsIOnxny(lx,ux)(ly,uy)=doh<-histogram2d_new(fromIntegralnx)(fromIntegralny)h'<-newForeignPtrhistogram2d_freehcheck"set_ranges_uniform"$withForeignPtrh'(\f->histogram2d_set_ranges_uniformflxuxlyuy)return$Hnxnyh'foreignimportccall"gsl-histogram2d.h gsl_histogram2d_set_ranges"histogram2d_set_ranges::Hist2DHandle->PtrDouble->CInt->PtrDouble->CInt->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_set_ranges_uniform"histogram2d_set_ranges_uniform::Hist2DHandle->Double->Double->Double->Double->IOCInt------------------------------------------------------------------------------- | create a histogram with n bins from ranges (x0->x1),(x1->x2)..(xn->xn+1) emptyRanges::VectorDouble-- ^ the x ranges->VectorDouble-- ^ the y ranges->Histogram2D-- ^ resultemptyRangesxy=unsafePerformIO$fromRangesIOxy-- | create a histogram with n bins and lower and upper limitsemptyLimits::Int->Int-- ^ bins->(Double,Double)-- ^ lower and upper limits x->(Double,Double)-- ^ lower and upper limits y->Histogram2D-- ^ resultemptyLimitsnxnyxy=unsafePerformIO$fromLimitsIOnxnyxy-- | create a histogram with n bins from ranges (x0->x1),(x1->x2)..(xn->xn+1) and increment from a vectorfromRanges::VectorDouble-- ^ the x ranges->VectorDouble-- ^ the y ranges->[(Double,Double)]-- ^ the data->Histogram2D-- ^ resultfromRangesrxryd=unsafePerformIO$doh<-fromRangesIOrxryincrementListIOhdreturnh-- | create a histogram with n bins and lower and upper limits and increment from a vectorfromLimits::Int->Int-- ^ bins->(Double,Double)-- ^ x lower and upper limits->(Double,Double)-- ^ y lower and upper limits->[(Double,Double)]-- ^ the data->Histogram2D-- ^ resultfromLimitsnxnyrxryd=unsafePerformIO$doh<-fromLimitsIOnxnyrxryincrementListIOhdreturnh------------------------------------------------------------------------------- | extract the ranges and binstoMatrix::Histogram2D->(VectorDouble,VectorDouble,MatrixDouble)-- ^ (ranges,bins)toMatrix(Hbxbyh)=unsafePerformIO$dorx<-createVector(bx+1)ry<-createVector(by+1)bs<-createMatrixRowMajorbxbyapp3(\s1p1s2p2sxsyp->withForeignPtrh$\f->histogram_to_matrixfs1p1s2p2sxsyp)vecrxvecrymatbs"toMatrix"return(rx,ry,bs)foreignimportccall"histogram-aux.h to_matrix"histogram_to_matrix::Hist2DHandle->CInt->PtrDouble->CInt->PtrDouble->CInt->CInt->PtrDouble->IOCInt-----------------------------------------------------------------------------{-
vectorToTuples = toTuples . toList
where toTuples [] = error "need a minimum of two elements"
toTuples [_] = error "need a minimum of two elements"
toTuples [x1,x2] = [(x1,x2)]
toTuples (x1:x2:xs) = (x1,x2) : (toTuples (x2:xs))
-}------------------------------------------------------------------------------- | create from ranges and binsfromMatrix::VectorDouble-- ^ x ranges->VectorDouble-- ^ y ranges->MatrixDouble-- ^ bins->Histogram2D-- ^resultfromMatrixxyw=unsafePerformIO$doh@(H__h')<-fromRangesIOxyapp3(\xsx'ysy'rscsb->withForeignPtrh'$\h''->histogram_from_matrixh''xsx'ysy'rscsb)vecxvecymatw"fromMatrix"returnhforeignimportccall"histogram-aux.h from_matrix"histogram_from_matrix::Hist2DHandle->CInt->PtrDouble->CInt->PtrDouble->CInt->CInt->PtrDouble->IOCInt------------------------------------------------------------------------------- | create a copy of a histogramcloneHistogram2D::Histogram2D->IOHistogram2DcloneHistogram2D(Hnxnyh)=doh'<-withForeignPtrhhistogram2d_cloneh''<-newForeignPtrhistogram2d_freeh'return$Hnxnyh''foreignimportccall"gsl-histogram2d.h gsl_histogram2d_clone"histogram2d_clone::Hist2DHandle->IOHist2DHandle------------------------------------------------------------------------------- | add 1.0 to the correct bin for each element of the list, fails silently if the value is outside the rangeaddList::Histogram2D->[(Double,Double)]->Histogram2DaddListhxs=unsafePerformIO$doh'<-cloneHistogram2DhincrementListIOh'xsreturnh'-- | add 1.0 to the correct bin for each element of the vector pair, fails silently if the value is outside the rangeaddVector::Histogram2D->VectorDouble->VectorDouble->Histogram2DaddVectorhxy=unsafePerformIO$doh'<-cloneHistogram2DhincrementVectorIOh'xyreturnh'-- add the appropriate weight for each element of the list, fails silently if the value is outside the rangeaddListWeighted::Histogram2D->[(Double,Double,Double)]->Histogram2DaddListWeightedhxs=unsafePerformIO$doh'<-cloneHistogram2DhaccumulateListIOh'xsreturnh'-- add the appropriate weight for each element of the vector pair, fails silently if the value is outside the rangeaddVectorWeighted::Histogram2D->VectorDouble->VectorDouble->VectorDouble->Histogram2DaddVectorWeightedhxyw=unsafePerformIO$doh'<-cloneHistogram2DhaccumulateVectorIOh'xywreturnh'------------------------------------------------------------------------------- | add 1.0 to the correct bin, fails silently if the value is outside the rangeincrementIO::Histogram2D->Double->Double->IO()incrementIO(H__h)xy=docheck"increment"$withForeignPtrh(\f->histogram2d_incrementfxy)return()-- | add 1.0 to the correct bin for each element of the vector pair, fails silently if the value is outside the rangeincrementVectorIO::Histogram2D->VectorDouble->VectorDouble->IO()incrementVectorIO(H__h)xy=doapp2(\xsxpysyp->withForeignPtrh(\f->histogram2d_increment_matrixfxsxpysyp))vecxvecy"incrementVector"return()-- | add 1.0 to the correct bin for each element of the list, fails silently if the value is outside the rangeincrementListIO::Histogram2D->[(Double,Double)]->IO()incrementListIO(H__h)zs=withForeignPtrh(\f->mapM_(\(x,y)->histogram2d_incrementfxy)zs)-- | Adds the weight (third Double) to the bin appropriate for the value (first two Doubles)accumulateIO::Histogram2D->Double->Double->Double->IO()accumulateIO(H__h)xyw=docheck"accumulate"$withForeignPtrh(\f->histogram2d_accumulatefxyw)return()-- | add the weight (third) to the correct bin for each vector pair element, fails silently if the value is outside the rangeaccumulateVectorIO::Histogram2D->VectorDouble->VectorDouble->VectorDouble->IO()accumulateVectorIO(H__h)xyw=doapp3(\xsxpysypwswp->withForeignPtrh(\f->histogram2d_accumulate_matrixfxsxpysypwswp))vecxvecyvecw"accumulateVector"return()-- | add the weight (snd) to the correct bin for each (fst) element of the list, fails silently if the value is outside the rangeaccumulateListIO::Histogram2D->[(Double,Double,Double)]->IO()accumulateListIO(H__h)zs=withForeignPtrh(\f->mapM_(\(x,y,w)->histogram2d_accumulatefxyw)zs)-- | returns the contents of the i-th bingetBin::Histogram2D->(Int,Int)->DoublegetBin(H__h)(bx,by)=unsafePerformIO$dowithForeignPtrh(\f->histogram2d_getf(fromIntegralbx)(fromIntegralby))-- | returns the upper and lower limits in the first dimension of the i-th bingetXRange::Histogram2D->Int->(Double,Double)getXRange(H__h)b=unsafePerformIO$doalloca$\l->alloca$\u->docheck"get_xrange"$withForeignPtrh(\f->histogram2d_get_xrangef(fromIntegralb)lu)l'<-peeklu'<-peekureturn(l',u')-- | returns the upper and lower limits in the second dimension of the i-th bingetYRange::Histogram2D->Int->(Double,Double)getYRange(H__h)b=unsafePerformIO$doalloca$\l->alloca$\u->docheck"get_yrange"$withForeignPtrh(\f->histogram2d_get_yrangef(fromIntegralb)lu)l'<-peeklu'<-peekureturn(l',u')-- | the maximum upper range limit in the first dimensiongetXMax::Histogram2D->DoublegetXMax(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_xmax-- | the minimum lower range limit in the first dimensiongetXMin::Histogram2D->DoublegetXMin(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_xmin-- | the number of binsin the first dimensiongetXBins::Histogram2D->IntgetXBins(Hbx__)=bx-- | the maximum upper range limit in the first dimensiongetYMax::Histogram2D->DoublegetYMax(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_ymax-- | the minimum lower range limit in the first dimensiongetYMin::Histogram2D->DoublegetYMin(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_ymin-- | the number of binsin the first dimensiongetYBins::Histogram2D->IntgetYBins(H_by_)=by-- | reset all the bins to zeroreset::Histogram2D->IO()reset(H__h)=withForeignPtrhhistogram2d_resetforeignimportccall"gsl-histogram2d.h gsl_histogram2d_increment"histogram2d_increment::Hist2DHandle->Double->Double->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_accumulate"histogram2d_accumulate::Hist2DHandle->Double->Double->Double->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_get"histogram2d_get::Hist2DHandle->CInt->CInt->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_get_xrange"histogram2d_get_xrange::Hist2DHandle->CInt->PtrDouble->PtrDouble->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_get_yrange"histogram2d_get_yrange::Hist2DHandle->CInt->PtrDouble->PtrDouble->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_xmax"histogram2d_xmax::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_xmin"histogram2d_xmin::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_nx"histogram2d_xn::Hist2DHandle->IOIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_ymax"histogram2d_ymax::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_ymin"histogram2d_ymin::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_ny"histogram2d_yn::Hist2DHandle->IOIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_reset"histogram2d_reset::Hist2DHandle->IO()foreignimportccall"histogram-aux.h increment_matrix"histogram2d_increment_matrix::Hist2DHandle->CInt->PtrDouble->CInt->PtrDouble->IOCIntforeignimportccall"histogram-aux.h accumulate_matrix"histogram2d_accumulate_matrix::Hist2DHandle->CInt->PtrDouble->CInt->PtrDouble->CInt->PtrDouble->IOCInt------------------------------------------------------------------------------- | find the bin corresponding to the valuefind::Histogram2D->(Double,Double)->Maybe(Int,Int)find(H__h)(x,y)=unsafePerformIO$doalloca$\bx->alloca$\by->doerr<-withForeignPtrh(\f->histogram2d_findfxybxby)iferr==0thendobx'<-peekbxby'<-peekbyreturn$Just$(fromIntegralbx',fromIntegralby')elsereturnNothingforeignimportccall"gsl-histogram2d.h gsl_histogram2d_find"histogram2d_find::Hist2DHandle->Double->Double->PtrCInt->PtrCInt->IOCInt-- | find the number of occurences for the inputcountInstance::Histogram2D->(Double,Double)->DoublecountInstancehx=letJustx'=findhxingetBinhx'-- | find the probability of the inputprobability::Histogram2D->(Double,Double)->Doubleprobabilityhx=letJustx'=findhxin(getBinhx')/(sumh)-- | find the number of occurences for each element of the input vectorcount::Histogram2D->(VectorDouble,VectorDouble)->VectorDoublecount(H__h)(x,y)=unsafePerformIO$dor<-createVector$dimxapp3(\xs'x'ys'y'rs'r'->withForeignPtrh$\h'->histogram2d_counth'xs'x'ys'y'rs'r')vecxvecyvecr"histogram2d_count"returnrforeignimportccall"histogram-aux.h hist2d_count"histogram2d_count::Hist2DHandle->CInt->PtrDouble->CInt->PtrDouble->CInt->PtrDouble->IOCInt-- | find the joint probability of occuring for each element of the input vector pairprob::Histogram2D->(VectorDouble,VectorDouble)->VectorDoubleprobhz=(counthz)/(scalar$sumh)-- | find the number of occurences for each element of the input vectorcountPaired::Histogram2D->Vector(Double,Double)->VectorDoublecountPaired(H__h)x=unsafePerformIO$dor<-createVector$dimxapp2(\xs'x'rs'r'->withForeignPtrh$\h'->histogram2d_count_pairh'xs'x'rs'r')vecxvecr"histogram2d_count_pair"returnrforeignimportccall"histogram-aux.h hist2d_count_pair"histogram2d_count_pair::Hist2DHandle->CInt->Ptr(Double,Double)->CInt->PtrDouble->IOCInt-- | find the joint probability of occuring for each element of the input vector pairprobPaired::Histogram2D->(Vector(Double,Double))->VectorDoubleprobPairedhz=(countPairedhz)/(scalar$sumh)------------------------------------------------------------------------------- | the maximum value contained in the binsmaxVal::Histogram2D->DoublemaxVal(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_max_val-- | the index of the bin containing the maximum valuemaxBin::Histogram2D->(Int,Int)maxBin(H__h)=unsafePerformIO$doalloca$\bx->alloca$\by->dowithForeignPtrh(\f->histogram2d_max_binfbxby)bx'<-peekbxby'<-peekbyreturn$(fromIntegralbx',fromIntegralby')-- | the minimum value contained in the binsminVal::Histogram2D->DoubleminVal(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_min_val-- | the index of the bin containing the minimum valueminBin::Histogram2D->(Int,Int)minBin(H__h)=unsafePerformIO$doalloca$\bx->alloca$\by->dowithForeignPtrh(\f->histogram2d_min_binfbxby)bx'<-peekbxby'<-peekbyreturn$(fromIntegralbx',fromIntegralby')-- | the mean of the values in the first dimension, accuracy limited by bin widthxmean::Histogram2D->Doublexmean(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_xmean-- | the mean of the values in the second dimension, accuracy limited by bin widthymean::Histogram2D->Doubleymean(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_ymean-- | the standard deviation of the values in thee first dimension, accuracy limited by bin widthxstddev::Histogram2D->Doublexstddev(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_xsigma-- | the standard deviation of the values in thee first dimension, accuracy limited by bin widthystddev::Histogram2D->Doubleystddev(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_ysigma-- | the covariance of the first and second dimensionscovariance::Histogram2D->Doublecovariance(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_cov-- | the sum of the values, accuracy limited by bin widthsum::Histogram2D->Doublesum(H__h)=unsafePerformIO$withForeignPtrhhistogram2d_sumforeignimportccall"gsl-histogram2d.h gsl_histogram2d_max_val"histogram2d_max_val::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_max_bin"histogram2d_max_bin::Hist2DHandle->PtrCInt->PtrCInt->IO()foreignimportccall"gsl-histogram2d.h gsl_histogram2d_min_val"histogram2d_min_val::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_min_bin"histogram2d_min_bin::Hist2DHandle->PtrCInt->PtrCInt->IO()foreignimportccall"gsl-histogram2d.h gsl_histogram2d_xmean"histogram2d_xmean::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_ymean"histogram2d_ymean::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_xsigma"histogram2d_xsigma::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_ysigma"histogram2d_ysigma::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_cov"histogram2d_cov::Hist2DHandle->IODoubleforeignimportccall"gsl-histogram2d.h gsl_histogram2d_sum"histogram2d_sum::Hist2DHandle->IODouble------------------------------------------------------------------------------- | returns True of all the individual bin ranges of the two histograms are identicalequalBins::Histogram2D->Histogram2D->BoolequalBins(H__h1)(H__h2)=unsafePerformIO$doj<-withForeignPtrh1$\p1->dowithForeignPtrh2$\p2->histogram2d_equal_binsp1p2if(fromIntegralj)==(1::Int)thenreturnTrueelsereturnFalse-- | adds the contents of the bins of the second histogram to the firstadd::Histogram2D->Histogram2D->Histogram2Daddd(H__s)=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"add"$withForeignPtrd'$\dp->withForeignPtrs$\sp->histogram2d_adddpspreturnh-- | subtracts the contents of the bins of the second histogram from the firstsubtract::Histogram2D->Histogram2D->Histogram2Dsubtractd(H__s)=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"subtract"$withForeignPtrd'$\dp->withForeignPtrs$\sp->histogram2d_subdpspreturnh-- | multiplies the contents of the bins of the second histogram by the firstmultiply::Histogram2D->Histogram2D->Histogram2Dmultiplyd(H__s)=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"multiply"$withForeignPtrd'$\dp->withForeignPtrs$\sp->histogram2d_muldpspreturnh-- | divides the contents of the bins of the first histogram by the seconddivide::Histogram2D->Histogram2D->Histogram2Ddivided(H__s)=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"divide"$withForeignPtrd'$\dp->withForeignPtrs$\sp->histogram2d_divdpspreturnh-- | multiplies the contents of the bins by a constantscale::Histogram2D->Double->Histogram2Dscaleds=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"scale"$withForeignPtrd'$(\f->histogram2d_scalefs)returnh-- | adds a constant to the contents of the binsshift::Histogram2D->Double->Histogram2Dshiftds=unsafePerformIO$doh@(H__d')<-cloneHistogram2Ddcheck"shift"$withForeignPtrd'$(\f->histogram2d_shiftfs)returnhforeignimportccall"gsl-histogram2d.h gsl_histogram2d_equal_bins_p"histogram2d_equal_bins::Hist2DHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_add"histogram2d_add::Hist2DHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_sub"histogram2d_sub::Hist2DHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_mul"histogram2d_mul::Hist2DHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_div"histogram2d_div::Hist2DHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_scale"histogram2d_scale::Hist2DHandle->Double->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_shift"histogram2d_shift::Hist2DHandle->Double->IOCInt------------------------------------------------------------------------------- | write a histogram in the native binary format (may not be portable)fwriteHistogram2D::FilePath->Histogram2D->IO()fwriteHistogram2Dfn(H__h)=docn<-newCStringfncheck"fwriteHistogram2d2D"$withForeignPtrh$histogram2d_fwritecnfreecn-- | read a histogram in the native binary format, number of bins must be knownfreadHistogram2D::FilePath->Int->Int->IOHistogram2DfreadHistogram2Dfnbxby=doh<-histogram2d_new(fromIntegralbx)(fromIntegralby)h'<-newForeignPtrhistogram2d_freehcn<-newCStringfncheck"freadHistogram2d2D"$withForeignPtrh'$histogram2d_freadcnreturn$Hbxbyh'-- | saves the histogram with the given formats (%f,%e,%g) for ranges and bins-- each line comprises: xrange[i] xrange[i+1] xrange[j] xrange[j+1] bin(i,j)fprintfHistogram2D::FilePath->String->String->Histogram2D->IO()fprintfHistogram2Dfnfrfb(H__h)=docn<-newCStringfncr<-newCStringfrcb<-newCStringfbcheck"fprintfHistogram2d2D"$withForeignPtrh$histogram2d_fprintfcncrcbfreecnfreecrfreecbreturn()-- | reads formatted data as written by fprintf, the number of bins must be known in advancefscanfHistogram2D::FilePath->Int->Int->IOHistogram2DfscanfHistogram2Dfnbxby=doh<-histogram2d_new(fromIntegralbx)(fromIntegralby)h'<-newForeignPtrhistogram2d_freehcn<-newCStringfncheck"fscanfHistogram2d2D"$withForeignPtrh'$histogram2d_fscanfcnreturn$Hbxbyh'foreignimportccall"histogram-aux.h hist2d_fwrite"histogram2d_fwrite::PtrCChar->Hist2DHandle->IOCIntforeignimportccall"histogram-aux.h hist2d_fread"histogram2d_fread::PtrCChar->Hist2DHandle->IOCIntforeignimportccall"histogram-aux.h hist2d_fprintf"histogram2d_fprintf::PtrCChar->PtrCChar->PtrCChar->Hist2DHandle->IOCIntforeignimportccall"histogram-aux.h hist2d_fscanf"histogram2d_fscanf::PtrCChar->Hist2DHandle->IOCInt---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------foreignimportccall"gsl-histogram2d.h gsl_histogram2d_pdf_alloc"histogram2d_pdf_new::CInt->CInt->IOPDFHandleforeignimportccall"gsl-histogram2d.h &gsl_histogram2d_pdf_free"histogram2d_pdf_free::FunPtr(PDFHandle->IO())------------------------------------------------------------------------------- | create a histogram PDF from a histogramfromHistogram2D::Histogram2D->Histogram2DPDFfromHistogram2D(Hbxbyh)=unsafePerformIO$dop<-histogram2d_pdf_new(fromIntegralbx)(fromIntegralby)p'<-newForeignPtrhistogram2d_pdf_freepwithForeignPtrp'$\p''->withForeignPtrh$\h'->docheck"pdf_init"$histogram2d_pdf_initp''h'return$Pp'-- | given a randomm from the uniform distribution [0,1], draw a random sample from the PDFsample::Histogram2DPDF->Double->Doublesample(Pp)r=unsafePerformIO$withForeignPtrp$\p'->histogram2d_pdf_samplep'rforeignimportccall"gsl-histogram2d.h gsl_histogram2d_pdf_init"histogram2d_pdf_init::PDFHandle->Hist2DHandle->IOCIntforeignimportccall"gsl-histogram2d.h gsl_histogram2d_pdf_sample"histogram2d_pdf_sample::PDFHandle->Double->IODouble---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------{-
unzipPair :: Vector (Double,Double) -> (Vector Double,Vector Double)
unzipPair v = unsafePerformIO $ do
a <- createVector $ dim v
b <- createVector $ dim v
app3 histogram2d_unzip_double_pair vec v vec a vec b "unzipPair"
return (a,b)
foreign import ccall "histogram-aux.h unzip_double_pair" histogram2d_unzip_double_pair :: CInt -> Ptr (Double,Double) -> CInt -> Ptr Double -> CInt -> Ptr Double -> IO CInt
-}-----------------------------------------------------------------------------