28 #ifndef SCIMATH_LSQFIT_H
29 #define SCIMATH_LSQFIT_H
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/Utilities/RecordTransformable.h>
34 #include <casacore/scimath/Fitting/LSQMatrix.h>
35 #include <casacore/scimath/Fitting/LSQTraits.h>
456 std::complex<U> *
sol,
468 std::complex<U> *
sol,
513 template <
class U,
class V>
514 void makeNorm(
const V &cEq,
const U &weight,
const U &obs,
516 template <
class U,
class V>
517 void makeNorm(
const V &cEq,
const U &weight,
const U &obs,
520 template <
class U,
class V>
522 const std::complex<U> &obs,
524 template <
class U,
class V>
526 const std::complex<U> &obs,
529 template <
class U,
class V>
531 const std::complex<U> &obs,
534 template <
class U,
class V>
536 const std::complex<U> &obs,
539 template <
class U,
class V>
541 const std::complex<U> &obs,
545 template <
class U,
class V,
class W>
547 const V &cEq,
const U &weight,
const U &obs,
549 template <
class U,
class V,
class W>
551 const V &cEq,
const V &cEq2,
552 const U &weight,
const U &obs,
const U &obs2,
554 template <
class U,
class V,
class W>
556 const V &cEq,
const U &weight,
const U &obs,
559 template <
class U,
class V,
class W>
561 const V &cEq,
const U &weight,
562 const std::complex<U> &obs,
564 template <
class U,
class V,
class W>
566 const V &cEq,
const U &weight,
567 const std::complex<U> &obs,
570 template <
class U,
class V,
class W>
572 const V &cEq,
const U &weight,
573 const std::complex<U> &obs,
576 template <
class U,
class V,
class W>
578 const V &cEq,
const U &weight,
579 const std::complex<U> &obs,
582 template <
class U,
class V,
class W>
584 const V &cEq,
const U &weight,
585 const std::complex<U> &obs,
589 template <
class U,
class V>
590 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
591 const U &weight,
const U &obs,
593 template <
class U,
class V>
594 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
595 const U &weight,
const U &obs,
598 template <
class U,
class V>
599 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
601 const std::complex<U> &obs,
603 template <
class U,
class V>
604 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
606 const std::complex<U> &obs,
609 template <
class U,
class V>
610 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
612 const std::complex<U> &obs,
615 template <
class U,
class V>
616 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
618 const std::complex<U> &obs,
621 template <
class U,
class V>
622 void makeNorm(
const std::vector<std::pair<uInt, V> > &cEq,
624 const std::complex<U> &obs,
628 template <
class U,
class V,
class W>
630 const V &cEq,
const U &weight,
633 template <
class U,
class V,
class W>
635 const V &cEq,
const V &cEq2,
const U &weight,
636 const U &obs,
const U &obs2,
663 template <
class U,
class V>
665 template <
class U,
class V>
667 const std::complex<U> &obs);
668 template <
class U,
class V,
class W>
670 const V &cEq,
const U &obs);
671 template <
class U,
class V,
class W>
674 const std::complex<U> &obs);
675 template <
class U,
class V>
677 template <
class U,
class V>
679 const std::complex<U> &obs);
680 template <
class U,
class V,
class W>
682 const V &cEq,
const U &obs);
683 template <
class U,
class V,
class W>
686 const std::complex<U> &obs);
704 return mergeIt(other, nIndex, nEqIndex); }
706 const std::vector<uInt> &nEqIndex) {
707 return mergeIt(other, nIndex, &nEqIndex[0]); }
710 std::vector<uInt> ix(nIndex);
711 for (
uInt i=0; i<nIndex; ++i) ix[i] = nEqIndex[i];
712 return mergeIt(other, nIndex, &ix[0]); }
939 const std::complex<Double> &y) {
940 return (x.real()*y.real() + x.imag()*y.imag()); };
942 const std::complex<Double> &y) {
943 return (x.imag()*y.real() - x.real()*y.imag()); };
945 const std::complex<Float> &y) {
946 return (x.real()*y.real() + x.imag()*y.imag()); };
948 const std::complex<Float> &y) {
949 return (x.imag()*y.real() - x.real()*y.imag()); };
994 #ifndef CASACORE_NO_AUTO_TEMPLATES
995 #include <casacore/scimath/Fitting/LSQFit2.tcc>
Type of complex numeric class indicator
Double stepfactor_p
Levenberg step factor.
void solve(U *sol)
Solve normal equations.
Double prec_p
Collinearity precision.
uInt nUnknowns() const
Get the number of unknowns.
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Complex, Bool doNorm=True, Bool doKnown=True)
void createNCEQ()
Create the solution equation area nceq_p and fill it.
Bool merge(const LSQFit &other)
Merge other LSQFit object (i.e.
void set(Int nUnknowns, Int nConstraints=0)
Bool invert(uInt &nRank, Bool doSVD=False)
Triangularize the normal equations and determine the rank nRank of the normal equations and,...
void extendConstraints(uInt n)
Extend the constraint equation area to the specify number of equations.
Bool solveLoop(uInt &nRank, U *sol, Bool doSVD=False)
Solve a loop in a non-linear set.
static const String state
Bool setConstraint(uInt n, const V &cEq, const std::complex< U > &obs)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const U &obs, LSQFit::Real, Bool doNorm=True, Bool doKnown=True)
static Separable SEPARABLE
Double * wsol_p
Work areas for interim solutions and covariance.
Bool getCovariance(std::complex< U > *covar)
static const String recid
Record field names.
void uncopy(Double *beg, const Double *end, U &sol, LSQComplex)
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const U &obs, Bool doNorm=True, Bool doKnown=True)
ReadyCode ready_p
Indicate the non-linear state.
Bool getErrors(std::complex< U > *errors)
void set(uInt nUnknowns, uInt nConstraints=0)
Set new sizes (default is for Real)
Bool getConstraint(uInt n, U *cEq) const
Get the n-th (from 0 to the rank deficiency, or missing rank, see e.g.
Bool solveLoop(Double &fit, uInt &nRank, U &sol, Bool doSVD=False)
void save(Bool all=True)
Save current status (or part)
void init()
Initialise areas.
uInt nIterations() const
Get number of iterations done.
void makeNorm(const V &cEq, const U &weight, const std::complex< U > &obs, Bool doNorm=True, Bool doKnown=True)
static const String nonlin
void set(Double factor=1e-6, Double LMFactor=1e-3)
Set new factors (collinearity factor, and Levenberg-Marquardt LMFactor)
Double startnon_p
Levenberg start factor.
LSQFit(uInt nUnknowns, const LSQComplex &, uInt nConstraints=0)
Allow explicit Complex specification.
Bool invertRect()
Invert rectangular matrix (i.e.
Double normInfKnown(const Double *known) const
Get the infinite norm of the known vector.
uInt r_p
Rank of normal equations (normally n_p)
static const String constr
LSQFit * nar_p
Save area for non-linear case (size determined internally)
Double normSolution(const Double *sol) const
Get the norm of the current solution vector.
Bool solveLoop(Double &fit, uInt &nRank, std::complex< U > *sol, Bool doSVD=False)
void solveIt()
Solve normal equations.
uInt nnc_p
Current length nceq_p.
uInt state_p
Bits set to indicate state.
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::AsReal, Bool doNorm=True, Bool doKnown=True)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const std::complex< U > &obs, Bool doNorm=True, Bool doKnown=True)
Double getChi() const
Get chi^2 (both are identical); the standard deviation (per observation) and the standard deviation p...
Bool getConstraint(uInt n, U &cEq) const
void makeNorm(const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::AsReal, Bool doNorm=True, Bool doKnown=True)
uInt nun_p
Number of unknowns.
Double * constr_p
Constraint equation area (nun_p*ncon_p))
void toAipsIO(AipsIO &) const
Save or restore using AipsIO.
LSQFit(const LSQFit &other)
Copy constructor (deep copy)
uInt n_p
Matrix size (will be n_p = nun_p + ncon_p)
Bool getErrors(U &errors)
Bool solveLoop(Double &fit, uInt &nRank, U *sol, Bool doSVD=False)
uInt * piv_p
Pivot table (n_p)
Bool addConstraint(const V &cEq, const U &obs)
void uncopy(Double *beg, const Double *end, U *sol, LSQReal)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Complex, Bool doNorm=True, Bool doKnown=True)
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const std::complex< U > &obs, Bool doNorm=True, Bool doKnown=True)
LSQFit & operator=(const LSQFit &other)
Assignment (deep copy)
Double epsval_p
Test value for [incremental] solution in non-linear loop.
LSQMatrix * nceq_p
Normal combined with constraint equations for solutions (triangular nnc_p*nnc_p)
Bool addConstraint(const V &cEq, const std::complex< U > &obs)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const U &obs, Bool doNorm=True, Bool doKnown=True)
Bool merge(const LSQFit &other, uInt nIndex, const W &nEqIndex)
Double * lar_p
Save area for non-symmetric (i.e.
LSQFit(uInt nUnknowns, const LSQReal &, uInt nConstraints=0)
Allow explicit Real specification.
Bool solveLoop(uInt &nRank, std::complex< U > *sol, Bool doSVD=False)
uInt ncon_p
Number of constraints.
static Double realMC(const std::complex< Double > &x, const std::complex< Double > &y)
Calculate the real or imag part of x*conj(y)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Separable, Bool doNorm=True, Bool doKnown=True)
Bool getConstraint(uInt n, std::complex< U > *cEq) const
Bool addConstraint(uInt nIndex, const W &cEqIndex, const V &cEq, const U &obs)
Bool getCovariance(U *covar)
Get the covariance matrix (of size nUnknowns * nUnknowns)
static const String known
Bool setConstraint(uInt n, uInt nIndex, const W &cEqIndex, const V &cEq, const std::complex< U > &obs)
Bool solveItLoop(Double &fit, uInt &nRank, Bool doSVD=False)
One non-linear LM loop.
Bool toRecord(String &error, RecordInterface &out) const
Create a record from an LSQFit object.
Double * error_p
Counts for errors (N_ErrorField)
void solve(std::complex< U > *sol)
void setMaxIter(uInt maxiter=0)
Set maximum number of iterations.
Bool getErrors(U *errors)
Get main diagonal of covariance function (of size nUnknowns)
void set(uInt nUnknowns, const LSQReal &, uInt nConstraints=0)
Double * rowru(uInt i) const
void makeNormSorted(uInt nIndex, const W &cEqIndex, const V &cEq, const V &cEq2, const U &weight, const U &obs, const U &obs2, Bool doNorm=True, Bool doKnown=True)
const std::string & readyText() const
static const String errors
uInt nConstraints() const
Get the number of constraints.
void copy(const Double *beg, const Double *end, U *sol, LSQComplex)
void makeNorm(const V &cEq, const U &weight, const U &obs, LSQFit::Real, Bool doNorm=True, Bool doKnown=True)
void makeNorm(const V &cEq, const U &weight, const U &obs, Bool doNorm=True, Bool doKnown=True)
Make normal equations using the cEq condition equation (cArray) (with nUnknowns elements) and a weigh...
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Conjugate, Bool doNorm=True, Bool doKnown=True)
void makeNorm(const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Separable, Bool doNorm=True, Bool doKnown=True)
void set(Int nUnknowns, const LSQComplex &, Int nConstraints=0)
void copy(const LSQFit &other, Bool all=True)
Copy data.
LSQFit()
Default constructor (empty, only usable after a set(nUnknowns))
Double * sol_p
Solution area (n_p)
Bool setConstraint(uInt n, uInt nIndex, const W &cEqIndex, const V &cEq, const U &obs)
void setEpsValue(Double epsval=1e-8)
Set new value solution test.
void getWorkSOL()
Get work areas for solutions, covariance.
const String & ident() const
Get identification of record.
static Double imagMC(const std::complex< Double > &x, const std::complex< Double > &y)
LSQFit(uInt nUnknowns, uInt nConstraints=0)
Construct an object with the number of unknowns and constraints, using the default collinearity facto...
Bool merge(const LSQFit &other, uInt nIndex, const std::vector< uInt > &nEqIndex)
uInt niter_p
Iteration count for non-linear solution.
void makeNorm(const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Complex, Bool doNorm=True, Bool doKnown=True)
LSQFit::ReadyCode isReady() const
Ask the state of the non-linear solutions.
Bool setConstraint(uInt n, const V &cEq, const U &obs)
Add a new constraint equation (updating nConstraints); or set a numbered constraint equation (0....
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const V &cEq2, const U &weight, const U &obs, const U &obs2, Bool doNorm=True, Bool doKnown=True)
void makeNorm(const std::vector< std::pair< uInt, V > > &cEq, const U &weight, const std::complex< U > &obs, LSQFit::AsReal, Bool doNorm=True, Bool doKnown=True)
uInt maxiter_p
Maximum number of iterations for non-linear solution.
void setBalanced(Bool balanced=False)
Set the expected form of the normal equations.
void fromAipsIO(AipsIO &)
Bool balanced_p
Indicator for a well balanced normal equation.
void set(uInt nUnknowns, const LSQComplex &, uInt nConstraints=0)
void restore(Bool all=True)
Restore current status.
Double * rowrt(uInt i) const
Get pointer in rectangular array.
StateBit
Bits that can be set/referenced.
@ NONLIN
Non-linear solution.
@ TRIANGLE
Triangularised.
@ N_StateBit
Filler for cxx2html.
@ INVERTED
Inverted matrix present.
void uncopy(Double *beg, const Double *end, U &sol, LSQReal)
ErrorField
Offset of fields in error_p data area.
@ NC
Number of condition equations.
@ SUMWEIGHT
Sum weights of condition equations.
@ N_ErrorField
Number of error fields.
@ SUMLL
Sum known terms squared.
Bool merge(const LSQFit &other, uInt nIndex, const uInt *nEqIndex)
Bool solveLoop(uInt &nRank, U &sol, Bool doSVD=False)
void makeNorm(const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Conjugate, Bool doNorm=True, Bool doKnown=True)
Double * known_p
Known part equations (n_p)
void uncopy(Double *beg, const Double *end, U *sol, LSQComplex)
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const U &obs, LSQFit::Real, Bool doNorm=True, Bool doKnown=True)
Double nonlin_p
Levenberg current factor.
static Float imagMC(const std::complex< Float > &x, const std::complex< Float > &y)
static const String startnon
uInt getDeficiency() const
Get the rank deficiency Warning: Note that the number is returned assuming real values; For complex ...
void copyDiagonal(U &errors, LSQComplex)
static Real REAL
And values to use.
Bool mergeIt(const LSQFit &other, uInt nIndex, const uInt *nEqIndex)
Merge sparse normal equations.
ReadyCode
State of the non-linear solution.
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Separable, Bool doNorm=True, Bool doKnown=True)
void reset()
Reset status to empty.
void copy(const Double *beg, const Double *end, U &sol, LSQReal)
Copy date from beg to end; converting if necessary to complex data.
Double getWeightedSD() const
Bool fromRecord(String &error, const RecordInterface &in)
Create an LSQFit object from a record.
static Conjugate CONJUGATE
void makeNorm(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const std::complex< U > &obs, LSQFit::Conjugate, Bool doNorm=True, Bool doKnown=True)
static Float realMC(const std::complex< Float > &x, const std::complex< Float > &y)
void setEpsDerivative(Double epsder=1e-8)
Set new derivative test.
void solveMR(uInt nin)
Solve missing rank part.
void set(Int nUnknowns, const LSQReal &, Int nConstraints=0)
void debugIt(uInt &nun, uInt &np, uInt &ncon, uInt &ner, uInt &rank, Double *&nEq, Double *&known, Double *&constr, Double *&er, uInt *&piv, Double *&sEq, Double *&sol, Double &prec, Double &nonlin) const
Debug:
void deinit()
De-initialise area.
LSQMatrix * norm_p
Normal equations (triangular nun_p * nun_p)
Bool addConstraint(uInt nIndex, const W &cEqIndex, const V &cEq, const std::complex< U > &obs)
void makeNormSorted(uInt nIndex, const W &cEqIndex, const V &cEq, const U &weight, const U &obs, Bool doNorm=True, Bool doKnown=True)
void copy(const Double *beg, const Double *end, U *sol, LSQReal)
Double epsder_p
Test value for known vector in non-linear loop.
void copy(const Double *beg, const Double *end, U &sol, LSQComplex)
void copyDiagonal(U &errors, LSQReal)
String: the storage and methods of handling collections of characters.
const Double e
e and functions thereof:
this file contains all the compiler specific defines
bool Bool
Define the standard types used by Casacore.
LatticeExprNode all(const LatticeExprNode &expr)
Simple classes to overload templated memberfunctions.