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: aips2-request@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 <scimath/Functionals/CompiledFunction.h> |
---|
30 | #include <scimath/Mathematics/AutoDiffMath.h> |
---|
31 | |
---|
32 | #include <casa/iostream.h> |
---|
33 | |
---|
34 | namespace casa { //# NAMESPACE CASA - BEGIN |
---|
35 | |
---|
36 | CompiledSpectralElement::CompiledSpectralElement() {} |
---|
37 | |
---|
38 | CompiledSpectralElement::CompiledSpectralElement( |
---|
39 | const String& function, |
---|
40 | const Vector<Double>& param |
---|
41 | ) : SpectralElement(SpectralElement::COMPILED, param)/*, _function(function)*/ { |
---|
42 | _setFunction(function); |
---|
43 | /* |
---|
44 | if (! comp->setFunction(_function)) { |
---|
45 | throw AipsError( |
---|
46 | "CompiledSpectralElement: An illegal functional string " |
---|
47 | "was specified for a compiled SpectralElement" |
---|
48 | ); |
---|
49 | } |
---|
50 | if(comp->nparameters() != param.size()) { |
---|
51 | throw AipsError( |
---|
52 | "CompiledSpectralElement: Number of parameters in the " |
---|
53 | "compiled function does not match number of input parameters" |
---|
54 | ); |
---|
55 | } |
---|
56 | */ |
---|
57 | } |
---|
58 | |
---|
59 | CompiledSpectralElement::CompiledSpectralElement( |
---|
60 | SpectralElement::Types type, const Vector<Double>& param |
---|
61 | ) : SpectralElement(type, param)/*, _function("")*/ {} |
---|
62 | |
---|
63 | CompiledSpectralElement::CompiledSpectralElement( |
---|
64 | SpectralElement::Types type, uInt nParam |
---|
65 | ) : SpectralElement(type, Vector<Double>(nParam))/*, _function("")*/ {} |
---|
66 | |
---|
67 | CompiledSpectralElement::CompiledSpectralElement( |
---|
68 | const CompiledSpectralElement& other |
---|
69 | ) : SpectralElement(other)/*, _function(other._function)*/ {} |
---|
70 | |
---|
71 | CompiledSpectralElement::~CompiledSpectralElement() {} |
---|
72 | |
---|
73 | SpectralElement* CompiledSpectralElement::clone() const { |
---|
74 | return new CompiledSpectralElement(*this); |
---|
75 | } |
---|
76 | |
---|
77 | CompiledSpectralElement& CompiledSpectralElement::operator=( |
---|
78 | const CompiledSpectralElement &other |
---|
79 | ) { |
---|
80 | if (this != &other) { |
---|
81 | SpectralElement::operator=(other); |
---|
82 | //_function = other._function; |
---|
83 | } |
---|
84 | return *this; |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | Bool CompiledSpectralElement::operator==( |
---|
89 | const CompiledSpectralElement& other |
---|
90 | ) const { |
---|
91 | return SpectralElement::operator==(other);// && _function == other._function; |
---|
92 | |
---|
93 | } |
---|
94 | */ |
---|
95 | |
---|
96 | /* |
---|
97 | Double CompiledSpectralElement::operator()(const Double x) const { |
---|
98 | CompiledFunction<Double> comp; |
---|
99 | comp.setFunction(_function); |
---|
100 | comp.parameters().setParameters(get()); |
---|
101 | return comp(x); |
---|
102 | } |
---|
103 | */ |
---|
104 | |
---|
105 | const String& CompiledSpectralElement::getFunction() const { |
---|
106 | // return _function; |
---|
107 | std::tr1::shared_ptr<Function<Double, Double> > f = _getFunction(); |
---|
108 | return dynamic_cast<CompiledFunction<Double> *>(SpectralElement::_getFunction().get())->getText(); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void CompiledSpectralElement::_setFunction(const String& function) { |
---|
113 | //_function = function; |
---|
114 | std::tr1::shared_ptr<Function<Double, Double> > f = _getFunction(); |
---|
115 | CompiledFunction<Double> *cf = f.get() == 0 |
---|
116 | ? new CompiledFunction<Double>() |
---|
117 | : dynamic_cast<CompiledFunction<Double> *>(f.get()); |
---|
118 | if (! cf->setFunction(function)) { |
---|
119 | throw AipsError( |
---|
120 | "CompiledSpectralElement: An illegal functional string " |
---|
121 | "was specified for a compiled SpectralElement" |
---|
122 | ); |
---|
123 | } |
---|
124 | if (f.get() == 0) { |
---|
125 | f.reset(cf); |
---|
126 | } |
---|
127 | uInt n = get().size(); |
---|
128 | if(n > 0 && cf->nparameters() != n) { |
---|
129 | throw AipsError( |
---|
130 | "CompiledSpectralElement: Number of parameters in the " |
---|
131 | "compiled function does not match number of input parameters" |
---|
132 | ); |
---|
133 | } |
---|
134 | SpectralElement::_setFunction(f); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | Bool CompiledSpectralElement::toRecord(RecordInterface& out) const { |
---|
139 | SpectralElement::toRecord(out); |
---|
140 | out.define(RecordFieldId("compiled"), getFunction()); |
---|
141 | return True; |
---|
142 | } |
---|
143 | |
---|
144 | ostream &operator<<(ostream& os, const CompiledSpectralElement& elem) { |
---|
145 | os << SpectralElement::fromType((elem.getType())) << " element: " << endl; |
---|
146 | os << " Function: " << elem.getFunction() << endl; |
---|
147 | const Vector<Double> p = elem.get(); |
---|
148 | for (uInt i=0; i<p.size(); i++) { |
---|
149 | os << "p" << i << ": " << p[i] << endl; |
---|
150 | } |
---|
151 | return os; |
---|
152 | } |
---|
153 | |
---|
154 | } //# NAMESPACE CASA - END |
---|
155 | |
---|
156 | |
---|