source: Sophya/trunk/SophyaLib/NTools/cvector.cc@ 753

Last change on this file since 753 was 508, checked in by ansari, 26 years ago

Vector/Matrix OVector/OMatrix cmv 25/10/99

File size: 4.0 KB
RevLine 
[244]1#include "machdefs.h"
[220]2#include <string.h>
3#include "cvector.h"
4#include "generalfit.h"
[303]5#include "tvector.h"
[220]6
7//++
[508]8// Class OVector
[220]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
[508]18// OMatrix
[220]19//--
20
21//++
22// Titre Constructeurs
23//--
24
25//++
[508]26OVector::OVector(int n)
[220]27//
28// Constructeur, n = nombre d'éléments.
29//--
[508]30: OMatrix(n, 1)
[220]31{END_CONSTRUCTOR}
32
33
34//++
[508]35OVector::OVector(int n, double* values)
[220]36//
37// Constructeur, à partir des valeurs. Pas d'allocation.
38//--
[508]39: OMatrix(n, 1, values)
[220]40{END_CONSTRUCTOR}
41
42
43//++
[508]44OVector::OVector(const OVector& v)
[220]45//
46// Constructeur par copie.
47//--
[508]48: OMatrix(v)
[220]49{END_CONSTRUCTOR}
50
51
52//++
[508]53OVector::OVector(const OMatrix& a)
[220]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//--
[508]60: OMatrix(a.nr,1) // OMatrix(a) ne marche pas, pourquoi ???? bug gcc ???
[220]61{
62 if (a.nc != 1) THROW(sizeMismatchErr);
63 memcpy(data, a.data, a.nr * sizeof(double));
64 END_CONSTRUCTOR
65}
66
67//++
[508]68OVector::OVector(const TVector<r_8>& v)
[303]69//
70// Constructeur par "copie" a partir d'un TVector<r_8>.
71// Attention, les donnees sont partagees.
72//--
[508]73: OMatrix(v)
[303]74{
75}
76
77//++
[220]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//++
[508]89OVector& OVector::operator = (double x)
[220]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//++
[508]100double operator* (const OVector& v1, const OVector& v2)
[220]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//++
[508]119// OVector operator* (const OVector& v, double b)
[220]120// multiplication par un scalaire.
121//--
122
123//++
[508]124// OVector operator* (const OMatrix& a, const OVector& b)
[220]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//++
[508]146double LinSolveInPlace(OMatrix& a, OVector& b)
[220]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
[508]154 return OMatrix::GausPiv(a,b);
[220]155}
156
157
158//++
[508]159double LinSolve(const OMatrix& a, const OVector& b, OVector& c)
[220]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;
[508]167 OMatrix a1(a);
168 return OMatrix::GausPiv(a1,c);
[220]169}
170
171//////////////////////////////////////////////////////////
172//++
[508]173OVector OVector::FitResidus(GeneralFit& gfit,double xorg,double dx)
[220]174//
175// Retourne une classe contenant les residus du fit ``gfit''.
[307]176// La coordonnee de l'element (i) est -> x = xorg + i*dx
[220]177//--
178{
[307]179if(NElts()<=0)
[508]180 throw(SzMismatchError("OVector::FitResidus: size mismatch\n"));
[220]181GeneralFunction* f = gfit.GetFunction();
[307]182if(f==NULL)
[508]183 throw(NullPtrError("OVector::FitResidus: NULL pointer\n"));
184OVector par = gfit.GetParm();
185OVector v(*this);
[220]186for(int i=0;i<NElts();i++) {
[307]187 double x = xorg+i*dx;
188 v(i) -= f->Value(&x,par.Data());
[220]189}
190return v;
191}
192
193//++
[508]194OVector OVector::FitFunction(GeneralFit& gfit,double xorg,double dx)
[220]195//
196// Retourne une classe contenant la fonction du fit ``gfit''.
[307]197// La coordonnee de l'element (i) est -> x = xorg + i*dx
[220]198//--
199{
[307]200if(NElts()<=0)
[508]201 throw(SzMismatchError("OVector::FitResidus: size mismatch\n"));
[220]202GeneralFunction* f = gfit.GetFunction();
[307]203if(f==NULL)
[508]204 throw(NullPtrError("OVector::FitResidus: NULL pointer\n"));
205OVector par = gfit.GetParm();
206OVector v(*this);
[220]207for(int i=0;i<NElts();i++) {
[307]208 double x = xorg+i*dx;
209 v(i) = f->Value(&x,par.Data());
[220]210}
211return v;
212}
Note: See TracBrowser for help on using the repository browser.