Healpix C++  3.82
alm.h
Go to the documentation of this file.
1 /*
2  * This file is part of Healpix_cxx.
3  *
4  * Healpix_cxx is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Healpix_cxx is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Healpix_cxx; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * For more information about HEALPix, see http://healpix.sourceforge.net
19  */
20 
21 /*
22  * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
23  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
24  * (DLR).
25  */
26 
27 /*! \file alm.h
28  * Class for storing spherical harmonic coefficients.
29  *
30  * Copyright (C) 2003-2017 Max-Planck-Society
31  * \author Martin Reinecke
32  */
33 
34 #ifndef PLANCK_ALM_H
35 #define PLANCK_ALM_H
36 
37 #include <vector>
38 #include "arr.h"
39 
40 /*! Base class for calculating the storage layout of spherical harmonic
41  coefficients. */
42 class Alm_Base
43  {
44  protected:
45  int lmax, mmax, tval;
46 
47  public:
48  /*! Returns the total number of coefficients for maximum quantum numbers
49  \a l and \a m. */
50  static tsize Num_Alms (int l, int m);
51 
52  /*! Constructs an Alm_Base object with given \a lmax and \a mmax. */
53  Alm_Base (int lmax_=0, int mmax_=0)
54  : lmax(lmax_), mmax(mmax_), tval(2*lmax+1) {}
55 
56  /*! Changes the object's maximum quantum numbers to \a lmax and \a mmax. */
57  void Set (int lmax_, int mmax_)
58  {
59  lmax=lmax_;
60  mmax=mmax_;
61  tval=2*lmax+1;
62  }
63 
64  /*! Returns the maximum \a l */
65  int Lmax() const { return lmax; }
66  /*! Returns the maximum \a m */
67  int Mmax() const { return mmax; }
68 
69  /*! Returns an array index for a given m, from which the index of a_lm
70  can be obtained by adding l. */
71  int index_l0 (int m) const
72  { return ((m*(tval-m))>>1); }
73 
74  /*! Returns the array index of the specified coefficient. */
75  int index (int l, int m) const
76  { return index_l0(m) + l; }
77 
78  /*! Returns \a true, if both objects have the same \a lmax and \a mmax,
79  else \a false. */
80  bool conformable (const Alm_Base &other) const
81  { return ((lmax==other.lmax) && (mmax==other.mmax)); }
82 
83  /*! Swaps the contents of two Alm_Base objects. */
84  void swap (Alm_Base &other);
85  };
86 
87 /*! Class for storing spherical harmonic coefficients. */
88 template<typename T> class Alm: public Alm_Base
89  {
90  private:
91  arr<T> alm;
92 
93  public:
94  /*! Constructs an Alm object with given \a lmax and \a mmax. */
95  Alm (int lmax_=0, int mmax_=0)
96  : Alm_Base(lmax_,mmax_), alm (Num_Alms(lmax,mmax)) {}
97 
98  /*! Deletes the old coefficients and allocates storage according to
99  \a lmax and \a mmax. */
100  void Set (int lmax_, int mmax_)
101  {
102  Alm_Base::Set(lmax_, mmax_);
103  alm.alloc(Num_Alms(lmax,mmax));
104  }
105 
106  /*! Deallocates the old coefficients and uses the content of \a data
107  for storage. \a data is deallocated during the call. */
108  void Set (arr<T> &data, int lmax_, int mmax_)
109  {
110  planck_assert (Num_Alms(lmax_,mmax_)==data.size(),"wrong array size");
111  Alm_Base::Set(lmax_, mmax_);
112  alm.transfer(data);
113  }
114 
115  /*! Sets all coefficients to zero. */
116  void SetToZero ()
117  { alm.fill (0); }
118 
119  /*! Multiplies all coefficients by \a factor. */
120  template<typename T2> void Scale (const T2 &factor)
121  { for (tsize m=0; m<alm.size(); ++m) alm[m]*=factor; }
122  /*! \a a(l,m) *= \a factor[l] for all \a l,m. */
123  template<typename T2> void ScaleL (const arr<T2> &factor)
124  {
125  planck_assert(factor.size()>tsize(lmax),
126  "alm.ScaleL: factor array too short");
127  for (int m=0; m<=mmax; ++m)
128  for (int l=m; l<=lmax; ++l)
129  operator()(l,m)*=factor[l];
130  }
131  /*! \a a(l,m) *= \a factor[l] for all \a l,m. */
132  template<typename T2> void ScaleL (const std::vector<T2> &factor)
133  {
134  planck_assert(factor.size()>tsize(lmax),
135  "alm.ScaleL: factor array too short");
136  for (int m=0; m<=mmax; ++m)
137  for (int l=m; l<=lmax; ++l)
138  operator()(l,m)*=factor[l];
139  }
140  /*! \a a(l,m) *= \a factor[m] for all \a l,m. */
141  template<typename T2> void ScaleM (const arr<T2> &factor)
142  {
143  planck_assert(factor.size()>tsize(mmax),
144  "alm.ScaleM: factor array too short");
145  for (int m=0; m<=mmax; ++m)
146  for (int l=m; l<=lmax; ++l)
147  operator()(l,m)*=factor[m];
148  }
149  /*! Adds \a num to a_00. */
150  template<typename T2> void Add (const T2 &num)
151  { alm[0]+=num; }
152 
153  /*! Returns a reference to the specified coefficient. */
154  T &operator() (int l, int m)
155  { return alm[index(l,m)]; }
156  /*! Returns a constant reference to the specified coefficient. */
157  const T &operator() (int l, int m) const
158  { return alm[index(l,m)]; }
159 
160  /*! Returns a pointer for a given m, from which the address of a_lm
161  can be obtained by adding l. */
162  T *mstart (int m)
163  { return &alm[index_l0(m)]; }
164  /*! Returns a pointer for a given m, from which the address of a_lm
165  can be obtained by adding l. */
166  const T *mstart (int m) const
167  { return &alm[index_l0(m)]; }
168 
169  /*! Returns a constant reference to the a_lm data. */
170  const arr<T> &Alms () const { return alm; }
171 
172  /*! Swaps the contents of two Alm objects. */
173  void swap (Alm &other)
174  {
175  Alm_Base::swap(other);
176  alm.swap(other.alm);
177  }
178 
179  /*! Adds all coefficients from \a other to the own coefficients. */
180  void Add (const Alm &other)
181  {
182  planck_assert (conformable(other), "A_lm are not conformable");
183  for (tsize m=0; m<alm.size(); ++m)
184  alm[m] += other.alm[m];
185  }
186  };
187 
188 #endif
T & operator()(int l, int m)
Definition: alm.h:154
void Set(arr< T > &data, int lmax_, int mmax_)
Definition: alm.h:108
void ScaleL(const arr< T2 > &factor)
Definition: alm.h:123
void Set(int lmax_, int mmax_)
Definition: alm.h:100
void Set(int lmax_, int mmax_)
Definition: alm.h:57
Definition: alm.h:88
void Scale(const T2 &factor)
Definition: alm.h:120
T * mstart(int m)
Definition: alm.h:162
int Lmax() const
Definition: alm.h:65
int index(int l, int m) const
Definition: alm.h:75
int Mmax() const
Definition: alm.h:67
Definition: alm.h:42
void ScaleL(const std::vector< T2 > &factor)
Definition: alm.h:132
static tsize Num_Alms(int l, int m)
Definition: alm.cc:39
void Add(const Alm &other)
Definition: alm.h:180
void swap(Alm &other)
Definition: alm.h:173
const T * mstart(int m) const
Definition: alm.h:166
int index_l0(int m) const
Definition: alm.h:71
Alm(int lmax_=0, int mmax_=0)
Definition: alm.h:95
bool conformable(const Alm_Base &other) const
Definition: alm.h:80
const arr< T > & Alms() const
Definition: alm.h:170
void Add(const T2 &num)
Definition: alm.h:150
void swap(Alm_Base &other)
Definition: alm.cc:45
void ScaleM(const arr< T2 > &factor)
Definition: alm.h:141
void SetToZero()
Definition: alm.h:116
Alm_Base(int lmax_=0, int mmax_=0)
Definition: alm.h:53

Generated on Thu Jul 28 2022 17:32:07 for Healpix C++