Line data Source code
1 : //# SpectralElement.cc: Describes (a set of related) spectral lines 2 : //# Copyright (C) 2001,2004 3 : //# Associated Universities, Inc. Washington DC, USA. 4 : //# 5 : //# This library is free software; you can redistribute it and/or modify it 6 : //# under the terms of the GNU Library General Public License as published by 7 : //# the Free Software Foundation; either version 2 of the License, or (at your 8 : //# option) any later version. 9 : //# 10 : //# This library is distributed in the hope that it will be useful, but WITHOUT 11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 13 : //# License for more details. 14 : //# 15 : //# You should have received a copy of the GNU Library General Public License 16 : //# along with this library; if not, write to the Free Software Foundation, 17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 18 : //# 19 : //# Correspondence concerning AIPS++ should be addressed as follows: 20 : //# Internet email: casa-feedback@nrao.edu. 21 : //# Postal address: AIPS++ Project Office 22 : //# National Radio Astronomy Observatory 23 : //# 520 Edgemont Road 24 : //# Charlottesville, VA 22903-2475 USA 25 : //# 26 : 27 : #include <components/SpectralComponents/CompiledSpectralElement.h> 28 : 29 : #include <casacore/scimath/Functionals/CompiledFunction.h> 30 : #include <casacore/scimath/Mathematics/AutoDiffMath.h> 31 : 32 : #include <iostream> 33 : 34 : using namespace casacore; 35 : namespace casa { //# NAMESPACE CASA - BEGIN 36 : 37 0 : CompiledSpectralElement::CompiledSpectralElement() {} 38 : 39 0 : CompiledSpectralElement::CompiledSpectralElement( 40 : const String& function, 41 : const Vector<Double>& param 42 0 : ) : SpectralElement(SpectralElement::COMPILED, param)/*, _function(function)*/ { 43 0 : _setFunction(function); 44 : /* 45 : if (! comp->setFunction(_function)) { 46 : throw AipsError( 47 : "CompiledSpectralElement: An illegal functional string " 48 : "was specified for a compiled SpectralElement" 49 : ); 50 : } 51 : if(comp->nparameters() != param.size()) { 52 : throw AipsError( 53 : "CompiledSpectralElement: Number of parameters in the " 54 : "compiled function does not match number of input parameters" 55 : ); 56 : } 57 : */ 58 0 : } 59 : 60 0 : CompiledSpectralElement::CompiledSpectralElement( 61 : SpectralElement::Types type, const Vector<Double>& param 62 0 : ) : SpectralElement(type, param)/*, _function("")*/ {} 63 : 64 0 : CompiledSpectralElement::CompiledSpectralElement( 65 : SpectralElement::Types type, uInt nParam 66 0 : ) : SpectralElement(type, Vector<Double>(nParam))/*, _function("")*/ {} 67 : 68 0 : CompiledSpectralElement::CompiledSpectralElement( 69 : const CompiledSpectralElement& other 70 0 : ) : SpectralElement(other)/*, _function(other._function)*/ {} 71 : 72 0 : CompiledSpectralElement::~CompiledSpectralElement() {} 73 : 74 0 : SpectralElement* CompiledSpectralElement::clone() const { 75 0 : return new CompiledSpectralElement(*this); 76 : } 77 : 78 0 : CompiledSpectralElement& CompiledSpectralElement::operator=( 79 : const CompiledSpectralElement &other 80 : ) { 81 0 : if (this != &other) { 82 0 : SpectralElement::operator=(other); 83 : //_function = other._function; 84 : } 85 0 : return *this; 86 : } 87 : 88 : /* 89 : Bool CompiledSpectralElement::operator==( 90 : const CompiledSpectralElement& other 91 : ) const { 92 : return SpectralElement::operator==(other);// && _function == other._function; 93 : 94 : } 95 : */ 96 : 97 : /* 98 : Double CompiledSpectralElement::operator()(const Double x) const { 99 : CompiledFunction<Double> comp; 100 : comp.setFunction(_function); 101 : comp.parameters().setParameters(get()); 102 : return comp(x); 103 : } 104 : */ 105 : 106 0 : const String& CompiledSpectralElement::getFunction() const { 107 : // return _function; 108 0 : std::shared_ptr<Function<Double, Double> > f = _getFunction(); 109 0 : return dynamic_cast<CompiledFunction<Double> *>(SpectralElement::_getFunction().get())->getText(); 110 0 : } 111 : 112 : 113 0 : void CompiledSpectralElement::_setFunction(const String& function) { 114 : //_function = function; 115 0 : std::shared_ptr<Function<Double, Double> > f = _getFunction(); 116 0 : CompiledFunction<Double> *cf = f ? dynamic_cast<CompiledFunction<Double> *>(f.get()) 117 0 : : new CompiledFunction<Double>( ); 118 0 : if (! cf->setFunction(function)) { 119 0 : throw AipsError( 120 : "CompiledSpectralElement: An illegal functional string " 121 : "was specified for a compiled SpectralElement" 122 0 : ); 123 : } 124 0 : if ( ! f ) { 125 0 : f.reset(cf); 126 : } 127 0 : uInt n = get().size(); 128 0 : if(n > 0 && cf->nparameters() != n) { 129 0 : throw AipsError( 130 : "CompiledSpectralElement: Number of parameters in the " 131 : "compiled function does not match number of input parameters" 132 0 : ); 133 : } 134 0 : SpectralElement::_setFunction(f); 135 0 : } 136 : 137 : 138 0 : Bool CompiledSpectralElement::toRecord(RecordInterface& out) const { 139 0 : SpectralElement::toRecord(out); 140 0 : out.define(RecordFieldId("compiled"), getFunction()); 141 0 : return true; 142 : } 143 : 144 0 : ostream &operator<<(ostream& os, const CompiledSpectralElement& elem) { 145 0 : os << SpectralElement::fromType((elem.getType())) << " element: " << endl; 146 0 : os << " Function: " << elem.getFunction() << endl; 147 0 : const Vector<Double> p = elem.get(); 148 0 : for (uInt i=0; i<p.size(); i++) { 149 0 : os << "p" << i << ": " << p[i] << endl; 150 : } 151 0 : return os; 152 0 : } 153 : 154 : } //# NAMESPACE CASA - END 155 : 156 :