Commit fc2d51637c6a081593f0c41c20935e1ae3226953
1 parent
bca7af9e
Add binary operators to GSkymap class.
Showing
3 changed files
with
96 additions
and
0 deletions
include/GSkymap.hpp
| ... | ... | @@ -112,6 +112,10 @@ public: |
| 112 | 112 | GSkymap& operator*=(const double& factor); |
| 113 | 113 | GSkymap& operator/=(const GSkymap& map); |
| 114 | 114 | GSkymap& operator/=(const double& factor); |
| 115 | + GSkymap operator+(const GSkymap& map) const; | |
| 116 | + GSkymap operator-(const GSkymap& map) const; | |
| 117 | + GSkymap operator*(const GSkymap& map) const; | |
| 118 | + GSkymap operator/(const GSkymap& map) const; | |
| 115 | 119 | double& operator()(const int& index, const int& map = 0); |
| 116 | 120 | const double& operator()(const int& index, const int& map = 0) const; |
| 117 | 121 | double& operator()(const GSkyPixel& pixel, const int& map = 0); |
| ... | ... | @@ -188,6 +192,74 @@ private: |
| 188 | 192 | |
| 189 | 193 | |
| 190 | 194 | /***********************************************************************//** |
| 195 | + * @brief Binary sky map addition | |
| 196 | + * | |
| 197 | + * @param[in] map Sky map. | |
| 198 | + * @return Sky map to which @p map was added. | |
| 199 | + * | |
| 200 | + * Returns the sum of two sky maps. | |
| 201 | + ***************************************************************************/ | |
| 202 | +inline | |
| 203 | +GSkymap GSkymap::operator+(const GSkymap& map) const | |
| 204 | +{ | |
| 205 | + GSkymap result = *this; | |
| 206 | + result += map; | |
| 207 | + return result; | |
| 208 | +} | |
| 209 | + | |
| 210 | + | |
| 211 | +/***********************************************************************//** | |
| 212 | + * @brief Binary sky map subtraction | |
| 213 | + * | |
| 214 | + * @param[in] map Sky map. | |
| 215 | + * @return Sky map to which @p map was added. | |
| 216 | + * | |
| 217 | + * Returns the difference of two sky maps. | |
| 218 | + ***************************************************************************/ | |
| 219 | +inline | |
| 220 | +GSkymap GSkymap::operator-(const GSkymap& map) const | |
| 221 | +{ | |
| 222 | + GSkymap result = *this; | |
| 223 | + result -= map; | |
| 224 | + return result; | |
| 225 | +} | |
| 226 | + | |
| 227 | + | |
| 228 | +/***********************************************************************//** | |
| 229 | + * @brief Binary sky map multiplication | |
| 230 | + * | |
| 231 | + * @param[in] map Sky map. | |
| 232 | + * @return Sky map multiplied by @p map. | |
| 233 | + * | |
| 234 | + * Returns the product of two sky maps. | |
| 235 | + ***************************************************************************/ | |
| 236 | +inline | |
| 237 | +GSkymap GSkymap::operator*(const GSkymap& map) const | |
| 238 | +{ | |
| 239 | + GSkymap result = *this; | |
| 240 | + result *= map; | |
| 241 | + return result; | |
| 242 | +} | |
| 243 | + | |
| 244 | + | |
| 245 | +/***********************************************************************//** | |
| 246 | + * @brief Binary sky map division | |
| 247 | + * | |
| 248 | + * @param[in] map Sky map. | |
| 249 | + * @return Sky map divided by @p map. | |
| 250 | + * | |
| 251 | + * Returns the ratio of two sky maps. | |
| 252 | + ***************************************************************************/ | |
| 253 | +inline | |
| 254 | +GSkymap GSkymap::operator/(const GSkymap& map) const | |
| 255 | +{ | |
| 256 | + GSkymap result = *this; | |
| 257 | + result /= map; | |
| 258 | + return result; | |
| 259 | +} | |
| 260 | + | |
| 261 | + | |
| 262 | +/***********************************************************************//** | |
| 191 | 263 | * @brief Return class name |
| 192 | 264 | * |
| 193 | 265 | * @return String containing the class name ("GSkymap"). | ... | ... |
pyext/GSkymap.i
| ... | ... | @@ -134,6 +134,10 @@ public: |
| 134 | 134 | GSkymap& operator-=(const double& value); |
| 135 | 135 | GSkymap& operator*=(const GSkymap& map); |
| 136 | 136 | GSkymap& operator*=(const double& factor); |
| 137 | + GSkymap operator+(const GSkymap& map) const; | |
| 138 | + GSkymap operator-(const GSkymap& map) const; | |
| 139 | + GSkymap operator*(const GSkymap& map) const; | |
| 140 | + GSkymap operator/(const GSkymap& map) const; | |
| 137 | 141 | double& operator()(const int& index, const int& map = 0); |
| 138 | 142 | double& operator()(const GSkyPixel& pixel, const int& map = 0); |
| 139 | 143 | double operator()(const GSkyDir& dir, const int& map = 0) const; | ... | ... |
test/test_GSky.cpp
| ... | ... | @@ -831,12 +831,15 @@ void TestGSky::test_GSkymap_wcs_io(void) |
| 831 | 831 | |
| 832 | 832 | /*************************************************************************** |
| 833 | 833 | * @brief GSkymap |
| 834 | + * | |
| 835 | + * This method tests the GSkymap class. | |
| 834 | 836 | ***************************************************************************/ |
| 835 | 837 | void TestGSky::test_GSkymap(void) |
| 836 | 838 | { |
| 837 | 839 | // Define several maps for comparison |
| 838 | 840 | GSkymap map_src("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10, 2); |
| 839 | 841 | GSkymap map_dst("CAR", "GAL", 0.0, 0.0, -0.1, 0.1, 100, 100, 2); |
| 842 | + GSkymap map_new("CAR", "GAL", 0.0, 0.0, -0.1, 0.1, 100, 100, 2); | |
| 840 | 843 | |
| 841 | 844 | // Fill map pixels |
| 842 | 845 | double total_src = 0.0; |
| ... | ... | @@ -848,61 +851,78 @@ void TestGSky::test_GSkymap(void) |
| 848 | 851 | } |
| 849 | 852 | |
| 850 | 853 | // Add pixels to destination map |
| 854 | + map_new = map_dst + map_src; | |
| 851 | 855 | map_dst += map_src; |
| 852 | 856 | |
| 853 | 857 | // Check total in destination map. Note that the total in the destination |
| 854 | 858 | // map should be 100 times the total in the source map as the operator |
| 855 | 859 | // is expected to perform strict bi-linear interpolation |
| 856 | 860 | double total_dst = 0.0; |
| 861 | + double total_new = 0.0; | |
| 857 | 862 | double total_ref = 0.0; |
| 858 | 863 | for (int pix = 0; pix < map_dst.npix(); ++pix) { |
| 859 | 864 | for (int k = 0; k < map_dst.nmaps(); ++k) { |
| 860 | 865 | total_dst += map_dst(pix,k); |
| 866 | + total_new += map_new(pix,k); | |
| 861 | 867 | total_ref += map_src(map_dst.pix2dir(pix),k); |
| 862 | 868 | } |
| 863 | 869 | } |
| 864 | 870 | total_dst /= 100.0; |
| 871 | + total_new /= 100.0; | |
| 865 | 872 | total_ref /= 100.0; |
| 866 | 873 | test_value(total_dst, total_ref, 1.0e-3, "Test operator+=(GSkymap)"); |
| 874 | + test_value(total_new, total_ref, 1.0e-3, "Test operator+(GSkymap)"); | |
| 867 | 875 | |
| 868 | 876 | // Subtract pixels from destination map |
| 877 | + map_new = map_dst - map_src; | |
| 869 | 878 | map_dst -= map_src; |
| 870 | 879 | |
| 871 | 880 | // Check total in destination map. Note that the total in the destination |
| 872 | 881 | // map should be zero. |
| 873 | 882 | total_dst = 0.0; |
| 883 | + total_new = 0.0; | |
| 874 | 884 | for (int pix = 0; pix < map_dst.npix(); ++pix) { |
| 875 | 885 | for (int k = 0; k < map_dst.nmaps(); ++k) { |
| 876 | 886 | total_dst += map_dst(pix,k); |
| 887 | + total_new += map_dst(pix,k); | |
| 877 | 888 | } |
| 878 | 889 | } |
| 879 | 890 | test_value(total_dst, 0.0, 1.0e-3, "Test operator-=(GSkymap)"); |
| 891 | + test_value(total_new, 0.0, 1.0e-3, "Test operator-(GSkymap)"); | |
| 880 | 892 | |
| 881 | 893 | // Check map multiplication |
| 882 | 894 | GSkymap test_map = map_src; |
| 883 | 895 | test_map *= map_src; |
| 896 | + map_new = map_src * map_src; | |
| 884 | 897 | double total_test = 0.0; |
| 885 | 898 | total_ref = 0.0; |
| 899 | + total_new = 0.0; | |
| 886 | 900 | for (int pix = 0; pix < test_map.npix(); ++pix) { |
| 887 | 901 | for (int k = 0; k < test_map.nmaps(); ++k) { |
| 888 | 902 | total_test += test_map(pix,k); |
| 903 | + total_new += map_new(pix,k); | |
| 889 | 904 | total_ref += map_src(pix,k) * map_src(pix,k); |
| 890 | 905 | } |
| 891 | 906 | } |
| 892 | 907 | test_value(total_test, total_ref, 1.0e-3, "Test operator*=(GSkymap)"); |
| 908 | + test_value(total_new, total_ref, 1.0e-3, "Test operator*(GSkymap)"); | |
| 893 | 909 | |
| 894 | 910 | // Check map division |
| 895 | 911 | test_map = map_src; |
| 896 | 912 | test_map /= map_src; |
| 913 | + map_new = map_src / map_src; | |
| 897 | 914 | total_test = 0.0; |
| 898 | 915 | total_ref = 0.0; |
| 916 | + total_new = 0.0; | |
| 899 | 917 | for (int pix = 0; pix < test_map.npix(); ++pix) { |
| 900 | 918 | for (int k = 0; k < test_map.nmaps(); ++k) { |
| 901 | 919 | total_test += test_map(pix,k); |
| 920 | + total_new += map_new(pix,k); | |
| 902 | 921 | total_ref += map_src(pix,k) / map_src(pix,k); |
| 903 | 922 | } |
| 904 | 923 | } |
| 905 | 924 | test_value(total_test, total_ref, 1.0e-3, "Test operator/=(GSkymap)"); |
| 925 | + test_value(total_new, total_ref, 1.0e-3, "Test operator/(GSkymap)"); | |
| 906 | 926 | |
| 907 | 927 | // Check map scaling |
| 908 | 928 | test_map = map_src; | ... | ... |