1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
//# tFunctionHolder.cc: Test the one-dimensional scaled polynomial class
//# Copyright (C) 2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//#
//# This program is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
//# more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with this program; if not, write to the Free Software Foundation, Inc.,
//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#include <casacore/scimath/Functionals/FunctionHolder.h>
#include <casacore/scimath/Mathematics/AutoDiff.h>
#include <casacore/scimath/Mathematics/AutoDiffA.h>
#include <casacore/scimath/Mathematics/AutoDiffIO.h>
#include <casacore/scimath/Mathematics/AutoDiffMath.h>
#include <casacore/casa/BasicMath/Math.h>
#include <casacore/scimath/Functionals/UnaryFunction.h>
#include <casacore/scimath/Functionals/DiracDFunction.h>
#include <casacore/scimath/Functionals/GNoiseFunction.h>
#include <casacore/scimath/Functionals/KaiserBFunction.h>
#include <casacore/scimath/Functionals/SincFunction.h>
#include <casacore/scimath/Functionals/FunctionHolder.h>
#include <casacore/scimath/Functionals/Gaussian1D.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/iostream.h>
#include <casacore/casa/namespace.h>
// Make near zero zero
Double Y(const Double in) {
return (abs(in)<1e-15 ? Double(0.0) : in);
}
AutoDiff<Double> Y(AutoDiff<Double> in) {
in.value() = Y(in.value());
for (uInt i=0; i<in.nDerivatives(); ++i) {
in.deriv(i) = Y(in.deriv(i));
}
return in;
}
int main() {
cout << "---------------- test FunctionHolder ---------------" << endl;
cout << "------------------------ Unary ---------------------" << endl;
{
UnaryFunction<Double> fn(5, 7, 3);
for (Double x=3; x<11.2; x+=1.0) cout << "x=" << Y(x) << ": " <<
Y(fn(x)) << endl;
UnaryFunction<AutoDiff<Double> > fnd;
fnd[0] = AutoDiff<Double>(5, 3, 0);
fnd[1] = AutoDiff<Double>(7, 3, 1);
fnd[2] = AutoDiff<Double>(3, 3, 2);
for (Double x=3; x<11.2; x+=1.0) cout << "x=" << Y(x) << ": " <<
Y(fnd(x)) << endl;
}
cout << "------------------------ Dirac delta ----------------" << endl;
{
DiracDFunction<Double> fn(5, 7);
for (Double x=6; x<8.2; x+=1.0) cout << "x=" << Y(x) << ": " <<
Y(fn(x)) << endl;
DiracDFunction<AutoDiff<Double> > fnd;
fnd[0] = AutoDiff<Double>(5, 2, 0);
fnd[1] = AutoDiff<Double>(7, 2, 1);
for (Double x=6; x<8.2; x+=1.0) cout << "x=" << Y(x) << ": " <<
Y(fnd(x)) << endl;
}
cout << "------------------------ Normal noise ---------------" << endl;
{
GNoiseFunction<Double> fn(0, 2.0);
for (uInt i=0; i<10; ++i) cout << fn() << endl;
GNoiseFunction<AutoDiff<Double> > fnd;
for (uInt i=0; i<10; ++i) cout << fnd() << endl;
}
cout << "------------------------ Kaiser-Bessel --------------" << endl;
{
KaiserBFunction<Double> fn;
for (Double x=-1.2; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
Y(fn(x)) << endl;
for (Double x=6; x<8.2; x+=1.0) cout << "x=" << Y(x) << ": " <<
Y(fn(x)) << endl;
KaiserBFunction<AutoDiff<Double> > fnd;
fnd[0] = AutoDiff<Double>(1, 4, 0);
fnd[1] = AutoDiff<Double>(0, 4, 1);
fnd[2] = AutoDiff<Double>(1, 4, 2);
fnd[3] = AutoDiff<Double>(2.5, 4, 2);
for (Double x=-1.2; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
Y(fnd(x)) << endl;
}
cout << "------------------------ sinc -----------------------" << endl;
{
SincFunction<Double> fn;
for (Double x=-1.2; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
Y(fn(x)) << endl;
SincFunction<AutoDiff<Double> > fnd;
fnd[0] = AutoDiff<Double>(1, 3, 0);
fnd[1] = AutoDiff<Double>(0, 3, 1);
fnd[2] = AutoDiff<Double>(1, 3, 2);
for (Double x=-1.19999; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
Y(fnd(x)) << endl;
}
cout << "------------------------ Gaussian1D -----------------" << endl;
{
Gaussian1D<Double> fn;
FunctionHolder<Double> fh(fn);
for (Double x=-1.2; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
fh.asFunction()(x) << endl;
Gaussian1D<AutoDiff<Double> > fnd;
fnd[0] = AutoDiff<Double>(1, 3, 0);
fnd[1] = AutoDiff<Double>(0, 3, 1);
fnd[2] = AutoDiff<Double>(1, 3, 2);
/// FunctionHolder<AutoDiff<Double> > fhd(fnd);
for (Double x=-1.2; x<1.21; x+=0.2) cout << "x=" << Y(x) << ": " <<
/// fhd.asFunction()(x) << endl;
Y(fnd(x)) << endl;
}
cout << "-----------------------------------------------------" << endl;
cout << "OK" << endl;
return 0;
}
|