General Purpose Geodetic Library
SgVector.h
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of Space Geodetic Library. The library is used by
4  * nuSolve, a part of CALC/SOLVE system, and designed to make analysis of
5  * geodetic VLBI observations.
6  * Copyright (C) 2010-2020 Sergei Bolotin.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef SG_VECTOR_H
24 #define SG_VECTOR_H
25 
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 // check for mem* functions:
30 # ifndef HAVE_MEMSET
31 # error: "need right C-Library: Your C-Library doesn't have memset() function"
32 # endif //HAVE_MEMSET
33 # ifndef HAVE_MEMCPY
34 # error: "need right C-Library: Your C-Library doesn't have memcpy() function"
35 # endif //HAVE_MEMCPY
36 #endif
37 
38 
39 #include <stdlib.h>
40 #include <iostream>
41 #include <string.h>
42 
43 
44 #include <SgMathSupport.h>
45 
46 
47 
48 
49 class SgVector;
50 class SgMatrix;
51 class SgUtMatrix;
52 class SgSymMatrix;
53 
54 
55 extern const SgVector vZero;
56 /***===================================================================================================*/
61 /*=====================================================================================================*/
62 class SgVector
63 {
64  friend class SgMatrix;
65  friend class SgUtMatrix;
66 
67  public:
68  //
69  // constructors/destructors:
70  //
74  inline SgVector();
75 
82  inline SgVector(unsigned int N, bool IsNeedClear);
83 
88  inline SgVector(const SgVector& V);
89 
93  inline ~SgVector();
94 
95  inline void reSize(unsigned int n);
96 
97  //
98  // Interfaces:
99  //
102  inline unsigned int n() const;
103 
107  inline double& operator()(unsigned int i);
108 
113  inline void setElement(unsigned int i, double d);
114 
118  inline double getElement(unsigned int i) const;
119 
120 
121  //
122  // Functions:
123  //
126  inline double module() const;
127 
130  inline void clear();
131 
135  inline SgVector& operator=(const SgVector& V);
136 
140  inline SgVector& operator=(double d);
141 
145  inline SgVector& operator+=(const SgVector& V);
146 
150  inline SgVector& operator-=(const SgVector& V);
151 
155  inline SgVector& operator*=(double d);
156 
160  inline SgVector& operator/=(double d);
161 
165  inline bool operator==(const SgVector& V) const;
166 
170  inline bool operator!=(const SgVector& V) const;
171 
172 
173  //
174  // Friends:
175  //
179  friend inline SgVector operator-(const SgVector& V);
180 
185  friend inline SgVector operator/(const SgVector& V, double d);
186 
191  friend inline SgVector operator*(const SgVector& V, double d);
192 
197  friend inline SgVector operator*(double d, const SgVector& V);
198 
203  friend inline SgVector operator+(const SgVector& V1, const SgVector& V2);
204 
209  friend inline SgVector operator-(const SgVector& V1, const SgVector& V2);
210 
215  friend inline double operator*(const SgVector& V1, const SgVector& V2);
216 
221  friend SgVector operator*(const SgMatrix& M, const SgVector& V);
222 
227  friend SgVector operator*(const SgUtMatrix& R, const SgVector& V);
228 
233  friend SgVector operator*(const SgSymMatrix& P, const SgVector& V);
234 
239  friend SgVector calcProduct_matT_x_vec(const SgMatrix&, const SgVector&);
240 
246 
247  inline double* &base() {return B_;};
248  inline const double* base_c() const {return B_;};
249 
250  //
251  // I/O:
252  //
253  // ...
254 
255 
256  protected:
257  unsigned int N_;
258  double *B_;
259 };
260 /*=====================================================================================================*/
261 
262 
263 
264 
265 /*=====================================================================================================*/
266 /* */
267 /* SgVector's inline members: */
268 /* */
269 /*=====================================================================================================*/
270 //
271 // CONSTRUCTORS:
272 //
273 // An empty constructor:
275 {
276  B_ = NULL;
277  N_ = 0;
278 };
279 
280 
281 
282 // A regular constructor:
283 inline SgVector::SgVector(unsigned int N, bool IsNeedClear = true)
284 {
285  B_ = new double[N_=N];
286  if (IsNeedClear)
287  clear();
288 };
289 
290 
291 
292 // A copying constructor:
293 inline SgVector::SgVector(const SgVector& V)
294 {
295  B_ = new double[N_=V.N_];
296  memcpy((void*)B_, (const void*)V.B_, N_*sizeof(double));
297 };
298 
299 
300 
301 // A destructor:
303 {
304  if (B_)
305  delete[] B_;
306  B_ = NULL;
307  N_ = 0;
308 };
309 
310 
311 
312 //
313 inline void SgVector::reSize(unsigned int n)
314 {
315  if (B_)
316  delete[] B_;
317  B_ = new double[N_=n];
318  clear();
319 };
320 
321 
322 
323 //
324 // INTERFACES:
325 //
326 // dimension of a vector:
327 inline unsigned int SgVector::n() const
328 {
329  return N_;
330 };
331 
332 
333 
334 // a reference to i-th element (e.g., it is possible to write `v(i) = a'):
335 inline double& SgVector::operator()(unsigned int i)
336 {
337 #ifdef DEBUG
338  if (N_<=i)
339  std::cerr << "WARNING: double& SgVector::operator()(unsigned int i) :"
340  << " incompatible index of the vector (" << i << "), greater than " << N_ << "\n";
341 #endif //DEBUG
342  return (i<N_)?*(B_+i):*B_;
343 };
344 
345 
346 
347 // one part of operator(): set a value of i-th element:
348 inline void SgVector::setElement(unsigned int i, double d)
349 {
350 #ifdef DEBUG
351  if (N_<=i)
352  std::cerr << "WARNING: double& SgVector::setElement(unsigned int i, double d) :"
353  << " incompatible index of the vector (" << i << "), greater than " << N_ << "\n";
354 #endif //DEBUG
355  if(i<N_)
356  *(B_+i) = d;
357 };
358 
359 
360 
361 // second part of operator(): get a value of i-th element:
362 inline double SgVector::getElement(unsigned int i) const
363 {
364 #ifdef DEBUG
365  if (N_<=i)
366  std::cerr << "WARNING: double& SgVector::getElement(unsigned int i) const :"
367  << " incompatible index of the vector (" << i << "), greater than " << N_ << "\n";
368 #endif //DEBUG
369  return (i<N_)?*(B_+i):0.0;
370 };
371 
372 
373 
374 //
375 // FUNCTIONS:
376 //
377 // evaluates a length of a vector:
378 inline double SgVector::module() const
379 {
380  double d=0.0, *b=B_;
381  for(unsigned int i=0; i<N_; i++,b++)
382  d += *b**b;
383  return sqrt(d);
384 };
385 
386 
387 
388 // fills all elements with zeros:
389 inline void SgVector::clear()
390 {
391  memset((void*)B_, 0, sizeof(double)*N_);
392 };
393 
394 
395 
396 // copy one vector into another one:
398 {
399  if(N_!=V.N_) // adjust dimensions:
400  {
401  // delete previously allocated array:
402  if (B_)
403  delete[] B_;
404  // create a new one:
405  B_ = new double[N_=V.N_];
406  };
407 
408  //make a copy:
409  memcpy((void*)B_, (const void*)V.B_, N_*sizeof(double));
410  return *this;
411 };
412 
413 
414 
415 // fills all elements with a scalar:
416 inline SgVector& SgVector::operator=(double d)
417 {
418  double *b=B_;
419  for(unsigned int i=0; i<N_; i++)
420  *b++ = d;
421  return *this;
422 };
423 
424 
425 
426 // increment:
428 {
429 #ifdef DEBUG
430  if (N_!=V.N_)
431  std::cerr << "WARNING: double& SgVector::operator+=(const SgVector& V) :"
432  << " the dimensions of vectors ( " << N_ << " and " << V.N_ << " ) are different.\n";
433 #endif //DEBUG
434  double *b=B_, *v=V.B_;
435  unsigned int N=std::min(N_, V.N_);
436  for(unsigned int i=0; i<N; i++)
437  *b++ += *v++;
438  return *this;
439 };
440 
441 
442 
443 // decrement:
445 {
446 #ifdef DEBUG
447  if (N_!=V.N_)
448  std::cerr << "WARNING: double& SgVector::operator-=(const SgVector& V) :"
449  << " the dimensions of vectors ( " << N_ << " and " << V.N_ << " ) are different.\n";
450 #endif //DEBUG
451  double *b=B_, *v=V.B_;
452  unsigned int N=std::min(N_, V.N_);
453  for(unsigned int i=0; i<N; i++)
454  *b++ -= *v++;
455  return *this;
456 };
457 
458 
459 
460 // mult by a scalar:
462 {
463  double *b=B_;
464  for(unsigned int i=0; i<N_; i++)
465  *b++ *= d;
466  return *this;
467 };
468 
469 
470 
471 // divide by a scalar:
473 {
474  double *b=B_;
475  for(unsigned int i=0; i<N_; i++)
476  *b++ /= d;
477  return *this;
478 };
479 
480 
481 
482 // is equal?
483 inline bool SgVector::operator==(const SgVector& V) const
484 {
485  bool IsEqual=true;
486  if (N_!=V.N_) // nothing to complain, we can compare vectors that have different dimension,
487  return !IsEqual;// they just not equal.
488 
489  double *b=B_, *v=V.B_;
490  unsigned int i=0;
491  while (i++<N_ && (IsEqual = IsEqual && *b++==*v++)) ;
492  return IsEqual;
493 };
494 
495 
496 
497 // is not equal?
498 inline bool SgVector::operator!=(const SgVector& V) const
499 {
500  return !(*this==V);
501 };
502 
503 
504 
505 // minus sign:
506 inline SgVector operator-(const SgVector& V)
507 {
508  return SgVector(V)*=-1.0;
509 };
510 
511 
512 
513 // vector x scalar:
514 inline SgVector operator*(const SgVector& V, double d)
515 {
516  return SgVector(V)*=d;
517 };
518 
519 
520 
521 // scalar x vector:
522 inline SgVector operator*(double d, const SgVector& V)
523 {
524  return SgVector(V)*=d;
525 };
526 
527 
528 
529 // vector / scalar:
530 inline SgVector operator/(const SgVector& V, double d)
531 {
532  return SgVector(V)/=d;
533 };
534 
535 
536 
537 // vector + vector:
538 inline SgVector operator+(const SgVector& V1, const SgVector& V2)
539 {
540  return SgVector(V1)+=V2;
541 };
542 
543 
544 
545 // vector - vector:
546 inline SgVector operator-(const SgVector& V1, const SgVector& V2)
547 {
548  return SgVector(V1)-=V2;
549 };
550 
551 
552 
553 // vector x vector:
554 inline double operator*(const SgVector& V1, const SgVector& V2)
555 {
556 #ifdef DEBUG
557  if (V1.N_!=V2.N_)
558  std::cerr << "WARNING: double operator*(const SgVector& V1, const SgVector& V2) :"
559  << " the dimensions of vectors ( " << V1.N_ << " and " << V2.N_
560  << " ) are different.\n";
561 #endif //DEBUG
562  double *v1=V1.B_, *v2=V2.B_, d=0.0;
563  unsigned int N=std::min(V1.N_, V2.N_);
564  for(unsigned int i=0; i<N; i++)
565  d += *v1++**v2++;
566  return d;
567 };
568 /*=====================================================================================================*/
569 
570 
571 
572 
573 
574 /*=====================================================================================================*/
575 //
576 // aux functions:
577 //
578 // output to std stream:
583 std::ostream &operator<<(std::ostream& s, const SgVector& V);
584 
585 
586 
587 /*=====================================================================================================*/
588 #endif // SG_VECTOR_H
std::ostream & operator<<(std::ostream &s, const SgVector &V)
Definition: SgVector.cpp:35
SgVector operator/(const SgVector &V, double d)
Definition: SgVector.h:530
SgVector operator*(const SgVector &V, double d)
Definition: SgVector.h:514
SgVector operator+(const SgVector &V1, const SgVector &V2)
Definition: SgVector.h:538
SgVector operator-(const SgVector &V)
Definition: SgVector.h:506
const SgVector vZero
unsigned int n() const
Definition: SgUtMatrix.h:290
bool operator!=(const SgVector &V) const
Definition: SgVector.h:498
double * B_
A pointer on a first element of the vector.
Definition: SgVector.h:258
double & operator()(unsigned int i)
Definition: SgVector.h:335
SgVector & operator-=(const SgVector &V)
Definition: SgVector.h:444
~SgVector()
Definition: SgVector.h:302
friend SgVector calcProduct_matT_x_vec(const SgUtMatrix &, const SgVector &)
SgVector & operator=(const SgVector &V)
Definition: SgVector.h:397
unsigned int n() const
Definition: SgVector.h:327
const double * base_c() const
Definition: SgVector.h:248
void setElement(unsigned int i, double d)
Definition: SgVector.h:348
void reSize(unsigned int n)
Definition: SgVector.h:313
friend SgVector operator/(const SgVector &V, double d)
Definition: SgVector.h:530
double *& base()
Definition: SgVector.h:247
void clear()
Definition: SgVector.h:389
friend SgVector operator*(const SgVector &V, double d)
Definition: SgVector.h:514
friend SgVector operator+(const SgVector &V1, const SgVector &V2)
Definition: SgVector.h:538
SgVector & operator/=(double d)
Definition: SgVector.h:472
SgVector & operator*=(double d)
Definition: SgVector.h:461
bool operator==(const SgVector &V) const
Definition: SgVector.h:483
friend SgVector operator-(const SgVector &V)
Definition: SgVector.h:506
double module() const
Definition: SgVector.h:378
SgVector & operator+=(const SgVector &V)
Definition: SgVector.h:427
SgVector()
Definition: SgVector.h:274
friend SgVector calcProduct_matT_x_vec(const SgMatrix &, const SgVector &)
Definition: SgMatrix.cpp:192
double getElement(unsigned int i) const
Definition: SgVector.h:362
unsigned int N_
An number of elements in a vector (dimension).
Definition: SgVector.h:248