| 1 | #include "machdefs.h"
 | 
|---|
| 2 | #include <string.h>
 | 
|---|
| 3 | #include "cvector.h"
 | 
|---|
| 4 | #include "generalfit.h"
 | 
|---|
| 5 | #include "tvector.h"
 | 
|---|
| 6 | 
 | 
|---|
| 7 | //++
 | 
|---|
| 8 | // Class        OVector
 | 
|---|
| 9 | // Lib  Outils++
 | 
|---|
| 10 | // include      cvector.h
 | 
|---|
| 11 | //
 | 
|---|
| 12 | //    Vecteur colonne, en fait une matrice avec une seule colonne, pour faire
 | 
|---|
| 13 | //    des opérations matricielles.      
 | 
|---|
| 14 | //--
 | 
|---|
| 15 | 
 | 
|---|
| 16 | //++
 | 
|---|
| 17 | // Links        Parents
 | 
|---|
| 18 | // OMatrix
 | 
|---|
| 19 | //--
 | 
|---|
| 20 | 
 | 
|---|
| 21 | //++
 | 
|---|
| 22 | // Titre        Constructeurs
 | 
|---|
| 23 | //--
 | 
|---|
| 24 | 
 | 
|---|
| 25 | //++
 | 
|---|
| 26 | OVector::OVector(int n)
 | 
|---|
| 27 | //
 | 
|---|
| 28 | //      Constructeur, n = nombre d'éléments.
 | 
|---|
| 29 | //--
 | 
|---|
| 30 | : OMatrix(n, 1)
 | 
|---|
| 31 | {END_CONSTRUCTOR}
 | 
|---|
| 32 | 
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //++
 | 
|---|
| 35 | OVector::OVector(int n, double* values)
 | 
|---|
| 36 | //
 | 
|---|
| 37 | //      Constructeur, à partir des valeurs. Pas d'allocation.
 | 
|---|
| 38 | //--
 | 
|---|
| 39 | : OMatrix(n, 1, values)
 | 
|---|
| 40 | {END_CONSTRUCTOR}
 | 
|---|
| 41 | 
 | 
|---|
| 42 | 
 | 
|---|
| 43 | //++
 | 
|---|
| 44 | OVector::OVector(const OVector& v)
 | 
|---|
| 45 | //
 | 
|---|
| 46 | //      Constructeur par copie.
 | 
|---|
| 47 | //--
 | 
|---|
| 48 | : OMatrix(v)
 | 
|---|
| 49 | {END_CONSTRUCTOR}
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | //++
 | 
|---|
| 53 | OVector::OVector(const OMatrix& a)
 | 
|---|
| 54 | //
 | 
|---|
| 55 | //      Constructeur par "copie" à partir d'une matrice, qui doit avoir une seule colonne.
 | 
|---|
| 56 | //
 | 
|---|
| 57 | //      *Exceptions :*
 | 
|---|
| 58 | //      sizeMismatchErr    si la matrice a plus d'une colonne
 | 
|---|
| 59 | //--
 | 
|---|
| 60 | : OMatrix(a.nr,1)      // OMatrix(a) ne marche pas, pourquoi ???? bug gcc ???
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   if (a.nc != 1) THROW(sizeMismatchErr);
 | 
|---|
| 63 |   memcpy(data, a.data, a.nr * sizeof(double));
 | 
|---|
| 64 |   END_CONSTRUCTOR
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | //++
 | 
|---|
| 68 | OVector::OVector(const TVector<r_8>& v)
 | 
|---|
| 69 | //
 | 
|---|
| 70 | //      Constructeur par "copie" a partir d'un TVector<r_8>.
 | 
|---|
| 71 | //      Attention, les donnees sont partagees.
 | 
|---|
| 72 | //--
 | 
|---|
| 73 | : OMatrix(v)
 | 
|---|
| 74 | {
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | //++
 | 
|---|
| 78 | // Titre        Opérateurs
 | 
|---|
| 79 | //--
 | 
|---|
| 80 | 
 | 
|---|
| 81 | //++
 | 
|---|
| 82 | // double& operator()(int n)
 | 
|---|
| 83 | // double const& operator()(int n) const
 | 
|---|
| 84 | //      Accès aux éléments.
 | 
|---|
| 85 | //--
 | 
|---|
| 86 | 
 | 
|---|
| 87 | 
 | 
|---|
| 88 | //++
 | 
|---|
| 89 | OVector& OVector::operator = (double x)
 | 
|---|
| 90 | //
 | 
|---|
| 91 | //      Affectation à partir d'un scalaire. Tous les éléments prennent cette valeur.
 | 
|---|
| 92 | //--
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   for (int i=0; i<nr; i++)
 | 
|---|
| 95 |     (*this)(i) = x ;
 | 
|---|
| 96 |   return *this;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | //++
 | 
|---|
| 100 | double operator* (const OVector& v1, const OVector& v2)
 | 
|---|
| 101 | //
 | 
|---|
| 102 | //      Produit scalaire entre deux vecteurs.
 | 
|---|
| 103 | //--
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   if (v1.nr != v2.nr) THROW(sizeMismatchErr);
 | 
|---|
| 106 |   double *p    = v1.data;
 | 
|---|
| 107 |   double *pEnd = v1.data + v1.ndata;
 | 
|---|
| 108 |   double *q    = v2.data;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   double r = 0;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   while (p<pEnd)
 | 
|---|
| 113 |     r += *p++ * *q++;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   return r;
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | //++
 | 
|---|
| 119 | // OVector operator* (const OVector& v, double b)
 | 
|---|
| 120 | //      multiplication par un scalaire.
 | 
|---|
| 121 | //--
 | 
|---|
| 122 | 
 | 
|---|
| 123 | //++
 | 
|---|
| 124 | // OVector operator* (const OMatrix& a, const OVector& b)
 | 
|---|
| 125 | //      produit matrice*vecteur.
 | 
|---|
| 126 | //--
 | 
|---|
| 127 | 
 | 
|---|
| 128 | //++
 | 
|---|
| 129 | // Titres       Méthodes
 | 
|---|
| 130 | //--
 | 
|---|
| 131 | 
 | 
|---|
| 132 | //++
 | 
|---|
| 133 | // int NElts() const
 | 
|---|
| 134 | //
 | 
|---|
| 135 | //      Nombre de lignes du vecteur.
 | 
|---|
| 136 | //--
 | 
|---|
| 137 | 
 | 
|---|
| 138 | //++
 | 
|---|
| 139 | // void Realloc(int n, bool force=false)
 | 
|---|
| 140 | //
 | 
|---|
| 141 | //      Change la taille du vecteur. Réallocation physique seulement si
 | 
|---|
| 142 | //      pas assez de place, ou forcée si force=true.
 | 
|---|
| 143 | //--
 | 
|---|
| 144 | 
 | 
|---|
| 145 | //++
 | 
|---|
| 146 | double LinSolveInPlace(OMatrix& a, OVector& b)
 | 
|---|
| 147 | //
 | 
|---|
| 148 | //      Résolution du système A*C = B
 | 
|---|
| 149 | //--
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   if (a.NCol() != b.NRows()) THROW(sizeMismatchErr);
 | 
|---|
| 152 |   if (a.NCol() != a.NRows()) THROW(sizeMismatchErr);
 | 
|---|
| 153 | 
 | 
|---|
| 154 |   return OMatrix::GausPiv(a,b);
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | 
 | 
|---|
| 158 | //++
 | 
|---|
| 159 | double LinSolve(const OMatrix& a, const OVector& b, OVector& c)
 | 
|---|
| 160 | //
 | 
|---|
| 161 | //      Résolution du système A*C = B, avec C retourné dans B
 | 
|---|
| 162 | //--
 | 
|---|
| 163 | { 
 | 
|---|
| 164 |   if (a.NCol() != b.NRows()) THROW(sizeMismatchErr);
 | 
|---|
| 165 |   if (a.NCol() != a.NRows()) THROW(sizeMismatchErr);
 | 
|---|
| 166 |   c = b;
 | 
|---|
| 167 |   OMatrix a1(a);
 | 
|---|
| 168 |   return OMatrix::GausPiv(a1,c);
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | //////////////////////////////////////////////////////////
 | 
|---|
| 172 | //++
 | 
|---|
| 173 | OVector OVector::FitResidus(GeneralFit& gfit,double xorg,double dx)
 | 
|---|
| 174 | //
 | 
|---|
| 175 | //      Retourne une classe contenant les residus du fit ``gfit''.
 | 
|---|
| 176 | //      La coordonnee de l'element (i) est -> x = xorg + i*dx
 | 
|---|
| 177 | //--
 | 
|---|
| 178 | {
 | 
|---|
| 179 | if(NElts()<=0)
 | 
|---|
| 180 |   throw(SzMismatchError("OVector::FitResidus: size mismatch\n"));
 | 
|---|
| 181 | GeneralFunction* f = gfit.GetFunction();
 | 
|---|
| 182 | if(f==NULL)
 | 
|---|
| 183 |   throw(NullPtrError("OVector::FitResidus: NULL pointer\n"));
 | 
|---|
| 184 | OVector par = gfit.GetParm();
 | 
|---|
| 185 | OVector v(*this);
 | 
|---|
| 186 | for(int i=0;i<NElts();i++) {
 | 
|---|
| 187 |   double x = xorg+i*dx;
 | 
|---|
| 188 |   v(i) -= f->Value(&x,par.Data());
 | 
|---|
| 189 | }
 | 
|---|
| 190 | return v;
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | //++
 | 
|---|
| 194 | OVector OVector::FitFunction(GeneralFit& gfit,double xorg,double dx)
 | 
|---|
| 195 | //
 | 
|---|
| 196 | //      Retourne une classe contenant la fonction du fit ``gfit''.
 | 
|---|
| 197 | //      La coordonnee de l'element (i) est -> x = xorg + i*dx
 | 
|---|
| 198 | //--
 | 
|---|
| 199 | {
 | 
|---|
| 200 | if(NElts()<=0)
 | 
|---|
| 201 |   throw(SzMismatchError("OVector::FitResidus: size mismatch\n"));
 | 
|---|
| 202 | GeneralFunction* f = gfit.GetFunction();
 | 
|---|
| 203 | if(f==NULL)
 | 
|---|
| 204 |   throw(NullPtrError("OVector::FitResidus: NULL pointer\n"));
 | 
|---|
| 205 | OVector par = gfit.GetParm();
 | 
|---|
| 206 | OVector v(*this);
 | 
|---|
| 207 | for(int i=0;i<NElts();i++) {
 | 
|---|
| 208 |   double x = xorg+i*dx;
 | 
|---|
| 209 |   v(i) = f->Value(&x,par.Data());
 | 
|---|
| 210 | }
 | 
|---|
| 211 | return v;
 | 
|---|
| 212 | }
 | 
|---|