From 60c6d789e3c3b776ce913f50d5d57989b0de5e3c Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Mon, 12 Nov 2012 10:02:22 -0500 Subject: [PATCH] Added the copy and sqrt operator to SpectrumFunction --- src/fourier/base_types.hpp | 5 +++++ src/fourier/euclidian.hpp | 27 +++++++++++++++++++++++++++ src/fourier/healpix.hpp | 13 ++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/fourier/base_types.hpp b/src/fourier/base_types.hpp index 73d3698..b1ee549 100644 --- a/src/fourier/base_types.hpp +++ b/src/fourier/base_types.hpp @@ -25,10 +25,15 @@ namespace CosmoTool typedef Eigen::Map ConstMapType; typedef FourierMap > FourierMapType; typedef boost::shared_ptr FourierMapPtr; + typedef boost::shared_ptr > SpectrumFunctionPtr; virtual void newRandomFourier(gsl_rng *rng, FourierMapType& out_map) const = 0; + virtual SpectrumFunctionPtr copy() const = 0; + + virtual void sqrt() = 0; + virtual void mul(FourierMapType& m) const = 0; virtual void mul_sqrt(FourierMapType& m) const = 0; virtual void mul_inv(FourierMapType& m) const = 0; diff --git a/src/fourier/euclidian.hpp b/src/fourier/euclidian.hpp index fb92e40..1a8c53f 100644 --- a/src/fourier/euclidian.hpp +++ b/src/fourier/euclidian.hpp @@ -1,6 +1,7 @@ #ifndef __COSMOTOOL_FOURIER_EUCLIDIAN_HPP #define __COSMOTOOL_FOURIER_EUCLIDIAN_HPP +#include #include #include #include @@ -11,6 +12,18 @@ namespace CosmoTool { + template + class EuclidianOperator + { + public: + typedef boost::function1 Function; + + Function base, op; + T operator()(T k) { + return op(base(k)); + } + }; + template class EuclidianSpectrum_1D: public SpectrumFunction { @@ -18,8 +31,11 @@ namespace CosmoTool typedef boost::function1 Function; protected: Function f; + + static T msqrt(T a) { return std::sqrt(a); } public: typedef typename SpectrumFunction::FourierMapType FourierMapType; + typedef typename SpectrumFunction::SpectrumFunctionPtr SpectrumFunctionPtr; typedef boost::shared_ptr ptr_map; EuclidianSpectrum_1D(Function P) @@ -29,6 +45,17 @@ namespace CosmoTool void newRandomFourier(gsl_rng *rng, FourierMapType& out_map) const; + SpectrumFunctionPtr copy() const { + return SpectrumFunctionPtr(new EuclidianSpectrum_1D(f)); + } + + void sqrt() { + EuclidianOperator o; + o.base = f; + o.op = &EuclidianSpectrum_1D::msqrt; + f = (Function(o)); + } + void mul(FourierMapType& m) const; void mul_sqrt(FourierMapType& m) const; void mul_inv(FourierMapType& m) const; diff --git a/src/fourier/healpix.hpp b/src/fourier/healpix.hpp index bd868e7..460935d 100644 --- a/src/fourier/healpix.hpp +++ b/src/fourier/healpix.hpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include namespace CosmoTool { @@ -23,7 +25,7 @@ namespace CosmoTool public: typedef typename SpectrumFunction::FourierMapType FourierMapType; typedef boost::shared_ptr ptr_map; - + typedef typename SpectrumFunction::SpectrumFunctionPtr SpectrumFunctionPtr; HealpixSpectrum(long Lmax) : cls(Lmax+1) {} @@ -33,6 +35,15 @@ namespace CosmoTool long size() const { return cls.size(); } void newRandomFourier(gsl_rng *rng, FourierMapType& like_map) const; + SpectrumFunctionPtr copy() const { + HealpixSpectrum *s = new HealpixSpectrum(cls.size()-1); + s->cls = cls; + return SpectrumFunctionPtr(s); + } + + void sqrt() { + std::transform(cls.begin(), cls.end(), cls.begin(), std::ptr_fun(std::sqrt)); + } void mul(FourierMapType& m) const; void mul_sqrt(FourierMapType& m) const;