Commit bb69856f8c2513fd066bf3777a7226b13f63e47a
1 parent
c2c67291
Add Makefile.am files for example C++ executables (#1806)
Showing
16 changed files
with
586 additions
and
24 deletions
.gitignore
| ... | ... | @@ -91,15 +91,15 @@ test/test_csv*.dat |
| 91 | 91 | examples/python/*.xml |
| 92 | 92 | examples/python/*.html |
| 93 | 93 | examples/cpp/*/*.fits |
| 94 | -examples/cpp/createfits/createfits | |
| 95 | -examples/cpp/createskymap/createskymap | |
| 96 | -examples/cpp/createxml/createxml | |
| 97 | -examples/cpp/createxml/my_xml_file.xml | |
| 98 | -examples/cpp/readmodel/readmodel | |
| 99 | -examples/cpp/numerics/numerics | |
| 100 | -examples/cpp/optimize/optimize | |
| 101 | -examples/cpp/interpolate/interpolate | |
| 102 | -examples/cpp/readmodel/readmodel | |
| 94 | +examples/cpp/createfits | |
| 95 | +examples/cpp/createskymap | |
| 96 | +examples/cpp/createxml | |
| 97 | +examples/cpp/my_xml_file.xml | |
| 98 | +examples/cpp/readmodel | |
| 99 | +examples/cpp/numerics | |
| 100 | +examples/cpp/optimize | |
| 101 | +examples/cpp/interpolate | |
| 102 | +examples/cpp/readmodel | |
| 103 | 103 | |
| 104 | 104 | # Binaries |
| 105 | 105 | test/test_GApplication | ... | ... |
Makefile.am
configure.ac
| ... | ... | @@ -1394,6 +1394,8 @@ AC_CONFIG_FILES([Makefile |
| 1394 | 1394 | test/test_python.sh |
| 1395 | 1395 | pyext/Makefile |
| 1396 | 1396 | pyext/setup.py |
| 1397 | + examples/Makefile | |
| 1398 | + examples/cpp/Makefile | |
| 1397 | 1399 | gammalib.pc |
| 1398 | 1400 | testconf.sh], |
| 1399 | 1401 | [if test -f test/test_python.sh; then chmod +x test/test_python.sh; fi; | ... | ... |
examples/Makefile.am
0 → 100644
examples/cpp/Makefile.am
0 → 100644
| 1 | +## Process this file with automake to produce Makefile.in | |
| 2 | + | |
| 3 | +INCLUDES = -I$(top_srcdir)/include | |
| 4 | + | |
| 5 | +# Program name | |
| 6 | +bin_PROGRAMS = createfits \ | |
| 7 | + createskymap \ | |
| 8 | + createxml \ | |
| 9 | + interpolate \ | |
| 10 | + numerics \ | |
| 11 | + optimize \ | |
| 12 | + readmodel | |
| 13 | + | |
| 14 | +# Program definition | |
| 15 | +createfits_SOURCES = createfits.cpp | |
| 16 | +createfits_LDFLAGS = @LDFLAGS@ | |
| 17 | +createfits_CPPFLAGS = @CPPFLAGS@ | |
| 18 | +createfits_LDADD = $(top_srcdir)/src/libgamma.la | |
| 19 | + | |
| 20 | +# Program definition | |
| 21 | +createskymap_SOURCES = createskymap.cpp | |
| 22 | +createskymap_LDFLAGS = @LDFLAGS@ | |
| 23 | +createskymap_CPPFLAGS = @CPPFLAGS@ | |
| 24 | +createskymap_LDADD = $(top_srcdir)/src/libgamma.la | |
| 25 | + | |
| 26 | +# Program definition | |
| 27 | +createxml_SOURCES = createxml.cpp | |
| 28 | +createxml_LDFLAGS = @LDFLAGS@ | |
| 29 | +createxml_CPPFLAGS = @CPPFLAGS@ | |
| 30 | +createxml_LDADD = $(top_srcdir)/src/libgamma.la | |
| 31 | + | |
| 32 | +# Program definition | |
| 33 | +interpolate_SOURCES = interpolate.cpp | |
| 34 | +interpolate_LDFLAGS = @LDFLAGS@ | |
| 35 | +interpolate_CPPFLAGS = @CPPFLAGS@ | |
| 36 | +interpolate_LDADD = $(top_srcdir)/src/libgamma.la | |
| 37 | + | |
| 38 | +# Program definition | |
| 39 | +numerics_SOURCES = numerics.cpp | |
| 40 | +numerics_LDFLAGS = @LDFLAGS@ | |
| 41 | +numerics_CPPFLAGS = @CPPFLAGS@ | |
| 42 | +numerics_LDADD = $(top_srcdir)/src/libgamma.la | |
| 43 | + | |
| 44 | +# Program definition | |
| 45 | +optimize_SOURCES = optimize.cpp | |
| 46 | +optimize_LDFLAGS = @LDFLAGS@ | |
| 47 | +optimize_CPPFLAGS = @CPPFLAGS@ | |
| 48 | +optimize_LDADD = $(top_srcdir)/src/libgamma.la | |
| 49 | + | |
| 50 | +# Program definition | |
| 51 | +readmodel_SOURCES = readmodel.cpp | |
| 52 | +readmodel_LDFLAGS = @LDFLAGS@ | |
| 53 | +readmodel_CPPFLAGS = @CPPFLAGS@ | |
| 54 | +readmodel_LDADD = $(top_srcdir)/src/libgamma.la | ... | ... |
examples/cpp/createfits.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * createfits.cpp - Create FITS file * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file createfits.cpp | |
| 23 | + * @brief Create FITS file | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Create FITS file | |
| 33 | + * | |
| 34 | + * This code illustrates the creation of a FITS file. | |
| 35 | + ***************************************************************************/ | |
| 36 | +int main(void) { | |
| 37 | + | |
| 38 | + // Allocate FITS object | |
| 39 | + GFits fits; | |
| 40 | + | |
| 41 | + // Create FITS image | |
| 42 | + GFitsImageDouble image(20,10); | |
| 43 | + for (int x = 0; x < 20; ++x) { | |
| 44 | + for (int y = 0; y < 10; ++y) { | |
| 45 | + image(x,y) = x+100.0*y; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + // Append image to FITS object | |
| 50 | + fits.append(image); | |
| 51 | + | |
| 52 | + // Create binary table | |
| 53 | + GFitsBinTable table; | |
| 54 | + GFitsTableDoubleCol column("ENERGY", 10, 3); | |
| 55 | + for (int row = 0; row < 10; ++row) { | |
| 56 | + for (int index = 0; index < 3; ++index) { | |
| 57 | + column(row, index) = row*100.0+index; | |
| 58 | + } | |
| 59 | + } | |
| 60 | + table.append(column); | |
| 61 | + | |
| 62 | + // Append table to FITS object | |
| 63 | + fits.append(table); | |
| 64 | + | |
| 65 | + // Save file to disk | |
| 66 | + fits.saveto("my_fits_file.fits", true); | |
| 67 | + | |
| 68 | + // Close FITS object | |
| 69 | + fits.close(); | |
| 70 | + | |
| 71 | + // Exit | |
| 72 | + return 0; | |
| 73 | +} | |
| 74 | + | ... | ... |
examples/cpp/createfits/Makefile deleted
examples/cpp/createfits/Makefile.am
0 → 100644
| 1 | +## Process this file with automake to produce Makefile.in | |
| 2 | + | |
| 3 | +INCLUDES = -I$(top_srcdir)/include | |
| 4 | + | |
| 5 | +# Program name | |
| 6 | +bin_PROGRAMS = createfits | |
| 7 | + | |
| 8 | +# Program definition | |
| 9 | +createfits_SOURCES = createfits.cpp | |
| 10 | +createfits_LDFLAGS = @LDFLAGS@ | |
| 11 | +createfits_CPPFLAGS = @CPPFLAGS@ | |
| 12 | +createfits_LDADD = $(top_srcdir)/src/libgamma.la | |
| 13 | + | ... | ... |
examples/cpp/createskymap.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * createskymap.cpp - Create sky map * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013-2015 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file createskymap.cpp | |
| 23 | + * @brief Create sky map | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Create sky map | |
| 33 | + * | |
| 34 | + * This code illustrates the creation of a sky map. | |
| 35 | + ***************************************************************************/ | |
| 36 | +int main(void) { | |
| 37 | + | |
| 38 | + // Create a SNR shell model. The shell is centred on RA=0.3 deg and | |
| 39 | + // Dec=0.1 deg. The shell has an inner radius of 0.5 deg and a width | |
| 40 | + // of 0.1 deg. | |
| 41 | + GSkyDir centre; | |
| 42 | + centre.radec_deg(0.3, 0.1); | |
| 43 | + GModelSpatialRadialShell model(centre, 0.5, 0.1); | |
| 44 | + | |
| 45 | + // Create an empty sky map in celestial coordinates using a cartesian | |
| 46 | + // projection. The image is centred on RA=Dec=0, has a bin size of | |
| 47 | + // 0.01 degrees and 300 pixels in RA and Dec | |
| 48 | + double ra(0.0); | |
| 49 | + double dec(0.0); | |
| 50 | + double binsz(0.01); | |
| 51 | + int npix(300); | |
| 52 | + GSkyMap image("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1); | |
| 53 | + | |
| 54 | + // Fill the sky map with the model image | |
| 55 | + GEnergy energy; // Dummy (not relevant as model is not energy dependent) | |
| 56 | + GTime time; // Dummy (not relevant as model is not time dependent) | |
| 57 | + for (int index = 0; index < image.npix(); ++index) { | |
| 58 | + GSkyDir dir = image.inx2dir(index); // sky coordinate of pixel | |
| 59 | + double theta = centre.dist(dir); // distance in radians | |
| 60 | + image(index) = model.eval(theta, energy, time); | |
| 61 | + } | |
| 62 | + | |
| 63 | + // Save the image to a FITS file | |
| 64 | + image.save("my_sky_map.fits", true); | |
| 65 | + | |
| 66 | + // Exit | |
| 67 | + return 0; | |
| 68 | +} | |
| 69 | + | ... | ... |
examples/cpp/createskymap/Makefile deleted
examples/cpp/createxml.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * createxml.cpp - Create XML file * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file createxml.cpp | |
| 23 | + * @brief Create XML file | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Create XML file | |
| 33 | + * | |
| 34 | + * This code illustrates the creation of a XML file. | |
| 35 | + ***************************************************************************/ | |
| 36 | +int main(void) { | |
| 37 | + | |
| 38 | + // Allocate XML object | |
| 39 | + GXml xml; | |
| 40 | + | |
| 41 | + // Create base elements | |
| 42 | + GXmlComment comment("Now 2 nodes with spatial and spectral info"); | |
| 43 | + GXmlElement spatial("spatial type=\"Position\""); | |
| 44 | + GXmlElement spectral("spectrum type=\"PowerLaw\""); | |
| 45 | + GXmlElement text("text"); | |
| 46 | + GXmlPI pi("<?process now?>"); | |
| 47 | + | |
| 48 | + // Append spatial parameters | |
| 49 | + spatial.append(GXmlElement("parameter ra=\"83.0\"")); | |
| 50 | + spatial.append(GXmlElement("parameter dec=\"22.0\"")); | |
| 51 | + | |
| 52 | + // Append spectral parameters | |
| 53 | + spectral.append(GXmlElement("parameter prefactor=\"1e-7\"")); | |
| 54 | + spectral.append(GXmlElement("parameter index=\"-2.1\"")); | |
| 55 | + | |
| 56 | + // Append text | |
| 57 | + text.append(GXmlText("Finish with text")); | |
| 58 | + | |
| 59 | + // Append elements | |
| 60 | + xml.append(comment); | |
| 61 | + xml.append(spatial); | |
| 62 | + xml.append(spectral); | |
| 63 | + xml.append(text); | |
| 64 | + xml.append(pi); | |
| 65 | + | |
| 66 | + // Manipulate source element | |
| 67 | + xml.element("spatial", 0)->append(GXmlElement("parameter ra=\"83.0\"")); | |
| 68 | + xml.element("spatial", 0)->append(GXmlElement("parameter dec=\"22.0\"")); | |
| 69 | + | |
| 70 | + // Show alternative access | |
| 71 | + GXmlNode* node = xml.append(spatial); | |
| 72 | + node->append(GXmlElement("parameter ra=\"83.0\"")); | |
| 73 | + node->append(GXmlElement("parameter dec=\"22.0\"")); | |
| 74 | + | |
| 75 | + // Save XML document to file | |
| 76 | + xml.save("my_xml_file.xml"); | |
| 77 | + | |
| 78 | + // Exit | |
| 79 | + return 0; | |
| 80 | +} | |
| 81 | + | ... | ... |
examples/cpp/interpolate.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * interpolate.cpp - Illustrates interpolation * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file interpolate.cpp | |
| 23 | + * @brief Illustrates interpolation | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Interpolation between nodes | |
| 33 | + * | |
| 34 | + * This code illustrates the interpolation between nodes. | |
| 35 | + ***************************************************************************/ | |
| 36 | +int main(void) { | |
| 37 | + | |
| 38 | + // Create some nodes | |
| 39 | + double x_i[] = {1.0, 4.0, 6.0}; | |
| 40 | + double y_i[] = {8.0, 7.0, 2.0}; | |
| 41 | + | |
| 42 | + // Create node array from x_i values | |
| 43 | + GNodeArray nodes(3, x_i); | |
| 44 | + | |
| 45 | + // Get interpolated values | |
| 46 | + for (double x = 0; x < 10.0; x += 0.5) { | |
| 47 | + nodes.set_value(x); | |
| 48 | + double y = y_i[nodes.inx_left()] * nodes.wgt_left() + | |
| 49 | + y_i[nodes.inx_right()] * nodes.wgt_right(); | |
| 50 | + std::cout << "x=" << x << " : y=" << y << std::endl; | |
| 51 | + } | |
| 52 | + | |
| 53 | + // Exit | |
| 54 | + return 0; | |
| 55 | +} | ... | ... |
examples/cpp/numerics.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * numerics.cpp - Illustrates numerical class usage * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013-2014 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file numerics.cpp | |
| 23 | + * @brief Illustrates numerical class usage | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Gaussian function | |
| 33 | + * | |
| 34 | + * This code illustrates the definition of a Gaussian function. | |
| 35 | + ***************************************************************************/ | |
| 36 | +class function : public GFunction { | |
| 37 | +public: | |
| 38 | + function(const double& sigma) : m_a(1.0/(sigma*std::sqrt(gammalib::twopi))), | |
| 39 | + m_sigma(sigma) {} | |
| 40 | + double eval(const double& x) { return m_a*std::exp(-0.5*x*x/(m_sigma*m_sigma)); } | |
| 41 | +protected: | |
| 42 | + double m_a; //!< Amplitude parameter | |
| 43 | + double m_sigma; //!< Width parameter | |
| 44 | +}; | |
| 45 | + | |
| 46 | + | |
| 47 | +/***********************************************************************//** | |
| 48 | + * @brief Integrate function | |
| 49 | + * | |
| 50 | + * This code illustrates the integration of a function. | |
| 51 | + ***************************************************************************/ | |
| 52 | +int main(void) { | |
| 53 | + | |
| 54 | + // Create instance of function | |
| 55 | + function fct(3.0); | |
| 56 | + | |
| 57 | + // Create an integral object based on the function | |
| 58 | + GIntegral integral(&fct); | |
| 59 | + | |
| 60 | + // Set relative integration precision | |
| 61 | + integral.eps(1.0e-8); | |
| 62 | + | |
| 63 | + // Integrate over the interval [-10,10] | |
| 64 | + double result = integral.romberg(-15.0, +15.0); | |
| 65 | + | |
| 66 | + // Print result (should be basically 1) | |
| 67 | + std::cout << "Integral: " << result << std::endl; | |
| 68 | + | |
| 69 | + // Create a derivative object based on the function | |
| 70 | + GDerivative derivative(&fct); | |
| 71 | + | |
| 72 | + // Print derivatives | |
| 73 | + std::cout << "Derivative(0): " << derivative.value(0.0) << std::endl; | |
| 74 | + std::cout << "Derivative(3): " << derivative.value(3.0) << std::endl; | |
| 75 | + std::cout << "Derivative(-3): " << derivative.value(-3.0) << std::endl; | |
| 76 | + | |
| 77 | + // Exit | |
| 78 | + return 0; | |
| 79 | +} | ... | ... |
examples/cpp/optimize.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * optimize.cpp - Illustrates optimizer class usage * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013-2014 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file optimize.cpp | |
| 23 | + * @brief Illustrates optimizer class usage | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include "GammaLib.hpp" | |
| 29 | + | |
| 30 | + | |
| 31 | +/***********************************************************************//** | |
| 32 | + * @brief Parabola function | |
| 33 | + * | |
| 34 | + * This code illustrates the definition of a parabola function. | |
| 35 | + ***************************************************************************/ | |
| 36 | +class parabola : public GOptimizerFunction { | |
| 37 | +public: | |
| 38 | + parabola(void) : m_value(0), m_gradient(1), m_curvature(1,1) {} | |
| 39 | + void eval(const GOptimizerPars& pars); | |
| 40 | + double value(void) const { return m_value; } | |
| 41 | + GVector* gradient(void) { return &m_gradient; } | |
| 42 | + GMatrixSparse* curvature(void) { return &m_curvature; } | |
| 43 | +protected: | |
| 44 | + double m_value; //!< Function value | |
| 45 | + GVector m_gradient; //!< Function gradient vector | |
| 46 | + GMatrixSparse m_curvature; //!< Curvature matrix | |
| 47 | +}; | |
| 48 | +void parabola::eval(const GOptimizerPars& pars) | |
| 49 | +{ | |
| 50 | + const double a = 2.0; | |
| 51 | + const double b = -4.0; | |
| 52 | + const double c = 2.0; | |
| 53 | + double x = pars[0]->value(); | |
| 54 | + m_value = a*x*x + b*x + c; | |
| 55 | + m_gradient[0] = 2.0*a*x + b; | |
| 56 | + m_curvature(0,0) = m_gradient[0] * m_gradient[0]; | |
| 57 | +}; | |
| 58 | + | |
| 59 | + | |
| 60 | +/***********************************************************************//** | |
| 61 | + * @brief Optimize function | |
| 62 | + * | |
| 63 | + * This code illustrates the optimization of a function. | |
| 64 | + ***************************************************************************/ | |
| 65 | +int main(void) { | |
| 66 | + | |
| 67 | + // Allocate logger | |
| 68 | + GLog log; | |
| 69 | + log.cout(true); | |
| 70 | + | |
| 71 | + // Allocate optimizer | |
| 72 | + //GOptimizerLM opt(log); | |
| 73 | + GOptimizerLM opt; | |
| 74 | + | |
| 75 | + // Allocate function | |
| 76 | + parabola fct; | |
| 77 | + | |
| 78 | + // Allocate parameters and set initial value | |
| 79 | + GOptimizerPars pars(1); | |
| 80 | + pars[0]->value(1.5); | |
| 81 | + | |
| 82 | + // Optimize parameters | |
| 83 | + opt.optimize(fct, pars); | |
| 84 | + | |
| 85 | + // Print derivatives | |
| 86 | + std::cout << "Function value .....: " << fct.value() << std::endl; | |
| 87 | + std::cout << "Parameter value ....: " << pars[0]->value() << std::endl; | |
| 88 | + | |
| 89 | + // Exit | |
| 90 | + return 0; | |
| 91 | +} | ... | ... |
examples/cpp/readmodel.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * readmodel.cpp - Illustrates how to read a model * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2013 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file readmodel.cpp | |
| 23 | + * @brief Illustrates how to read a model | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#include <iostream> | |
| 29 | +#include "GammaLib.hpp" | |
| 30 | + | |
| 31 | + | |
| 32 | +/***********************************************************************//** | |
| 33 | + * @brief Dummy read model test | |
| 34 | + ***************************************************************************/ | |
| 35 | +int main(void) { | |
| 36 | + GModels models("source.xml"); | |
| 37 | + std::cout << models << std::endl; | |
| 38 | + return 0; | |
| 39 | +} | |
| 40 | + | ... | ... |
examples/cpp/source.xml
0 → 100644
| 1 | +<?xml version="1.0" standalone="no"?> | |
| 2 | +<source_library title="source library"> | |
| 3 | + <source name="1FGL J0005.7+3815" type="PointSource"> | |
| 4 | + <spectrum type="PowerLaw"> | |
| 5 | + <parameter scale="1e-07" name="Prefactor" min="1e-07" max="1000.0" value="1.73" free="1"/> | |
| 6 | + <parameter scale="1.0" name="Index" min="-5.0" max="+5.0" value="-2.1" free="1"/> | |
| 7 | + <parameter scale="1.0" name="Scale" min="10.0" max="1000000.0" value="100.0" free="0"/> | |
| 8 | + </spectrum> | |
| 9 | + <spatialModel type="SkyDirFunction"> | |
| 10 | + <parameter free="0" max="360" min="-360" name="RA" scale="1" value="83.6331" /> | |
| 11 | + <parameter free="0" max="90" min="-90" name="DEC" scale="1" value="22.0145" /> | |
| 12 | + </spatialModel> | |
| 13 | + </source> | |
| 14 | +</source_library> | ... | ... |