General Purpose Geodetic Library
SgUtMatrix.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_UT_MATRIX_H
24 #define SG_UT_MATRIX_H
25 
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 
32 #include <SgMathSupport.h>
33 #include <SgVector.h>
34 #include <SgMatrix.h>
35 
36 
37 /***===================================================================================================*/
43 class SgUtMatrix: public SgMatrix
44 {
45  public:
46  //
47  // constructors/destructors:
48  //
52  inline SgUtMatrix();
53 
60  inline SgUtMatrix(unsigned int N, bool IsNeedClear=true);
61 
66  inline SgUtMatrix(const SgUtMatrix& R);
67 
71  inline ~SgUtMatrix();
72 
73 
74 
75  //
76  // Interfaces:
77  //
80  inline unsigned int n() const;
81 
87  inline double& operator()(unsigned int i, unsigned int j);
88 
93  inline double getElement(unsigned int i, unsigned int j) const;
94 
100  inline void setElement(unsigned int i, unsigned int j, double d);
101 
102 
103 
104  //
105  // Functions:
106  //
109  inline void clear();
110 
113  inline SgMatrix T() const;
114 
118  SgUtMatrix& operator=(const SgUtMatrix& R);
119 
123  inline SgUtMatrix& operator=(double d);
124 
128  inline SgUtMatrix& operator+=(const SgUtMatrix& R);
129 
133  inline SgUtMatrix& operator-=(const SgUtMatrix& R);
134 
138  inline SgUtMatrix& operator*=(double d);
139 
143  inline SgUtMatrix& operator/=(double d);
144 
145 
146 
147  //
148  // Friends:
149  //
153  friend inline SgUtMatrix operator-(const SgUtMatrix& R);
154 
159  friend inline SgUtMatrix operator/(const SgUtMatrix& R, double d);
160 
165  friend inline SgUtMatrix operator*(const SgUtMatrix& R, double d);
166 
171  friend inline SgUtMatrix operator*(double d, const SgUtMatrix& R);
172 
177  friend inline SgUtMatrix operator+(const SgUtMatrix& R1, const SgUtMatrix& R2);
178 
183  friend inline SgUtMatrix operator-(const SgUtMatrix& R1, const SgUtMatrix& R2);
184 
189  friend SgVector operator*(const SgUtMatrix& R, const SgVector& V);
190 
191  friend SgMatrix calcProduct_mat_x_mat(const SgUtMatrix& R1, const SgMatrix& M2);
192 
193 
198  friend SgUtMatrix operator~(const SgUtMatrix& R);
199 
200 
201  inline double** &base() {return B_;};
202 
203 
204  inline const double* const* base_c() const {return B_;};
205  //
206  // I/O:
207  //
208  // ...
209 };
210 /*=====================================================================================================*/
211 
212 
213 
214 
215 /*=====================================================================================================*/
216 /* */
217 /* SgUtMatrix's inline members: */
218 /* */
219 /*=====================================================================================================*/
220 //
221 //
222 // CONSTRUCTORS:
223 //
224 // An empty constructor:
226  : SgMatrix()
227 {
228  // nothing to do
229 };
230 
231 
232 
233 // A regular constructor:
234 inline SgUtMatrix::SgUtMatrix(unsigned int N, bool IsNeedClear)
235  : SgMatrix()
236 {
237  NRow_ = NCol_ = N;
238  dTmp_ = 0.0;
239  B_ = new double*[NCol_];
240  double **w = B_;
241  for (unsigned int i=0; i<NCol_; i++,w++)
242  {
243  *w = new double[i+1];
244  if (IsNeedClear)
245  memset((void*)(*w), 0, sizeof(double)*(i+1));
246  };
247 };
248 
249 
250 
251 // A copying constructor:
253  : SgMatrix()
254 {
255  NRow_ = R.NRow_;
256  dTmp_ = 0.0;
257  B_ = new double*[NCol_=R.NCol_];
258  double **w=B_, **q=R.B_;
259  for (unsigned int i=0; i<NCol_; i++,w++,q++)
260  {
261  *w=new double[i+1];
262  memcpy((void*)(*w), (const void*)(*q), (i+1)*sizeof(double));
263  };
264 };
265 
266 
267 
268 // A destructor:
270 {
271  if (B_)
272  {
273  double **w=B_;
274  for (unsigned int i=0; i<NCol_; i++,w++)
275  delete[] *w;
276  delete[] B_;
277  B_ = NULL;
278  };
279  NRow_ = NCol_ = 0;
280  dTmp_ = 0.0;
281 };
282 
283 
284 
285 
286 //
287 // INTERFACES:
288 //
289 // number of elements in a row or column:
290 inline unsigned int SgUtMatrix::n() const
291 {
292  return NRow_;
293 };
294 
295 
296 
297 // returns a reference on (i,j)-th element:
298 inline double& SgUtMatrix::operator()(unsigned int i, unsigned int j)
299 {
300 #ifdef DEBUG
301  if (i>j)
302  std::cerr << "WARNING: double& SgUtMatrix::operator()(unsigned int, unsigned int):"
303  << " row's index [" << i << "] greater than the column index [" << j << "]\n";
304  if (NRow_<=i)
305  std::cerr << "WARNING: double& SgUtMatrix::operator()(unsigned int, unsigned int):"
306  << " row's index [" << i << "] out of range [0.." << NRow_-1 << "]\n";
307  if (NCol_<=j)
308  std::cerr << "WARNING: double& SgUtMatrix::operator()(unsigned int, unsigned int):"
309  << " column's index [" << j << "] out of range [0.." << NCol_-1 << "]\n";
310 #endif //DEBUG
311 
312  return (i<=j && j<NCol_)?*(*(B_+j)+i):dTmp_;
313 };
314 
315 
316 
317 //
318 inline double SgUtMatrix::getElement(unsigned int i, unsigned int j) const
319 {
320 #ifdef DEBUG
321  if (NRow_<=i)
322  std::cerr << "WARNING: SgUtMatrix::getElement(i,j):"
323  << " row index [" << i << "] out of range [0.." << NRow_-1 << "]\n";
324  if (NCol_<=j)
325  std::cerr << "WARNING: SgUtMatrix::getElement(i,j):"
326  << " column index [" << j << "] out of range [0.." << NCol_-1 << "]\n";
327  if (i>j)
328  std::cerr << "WARNING: SgUtMatrix::getElement(i,j):"
329  << " row's index [" << i << "] greater than the column index [" << j << "]\n";
330 #endif //DEBUG
331 
332  return (i<=j && j<NCol_)?*(*(B_+j)+i):0.0;
333 };
334 
335 
336 
337 //
338 inline void SgUtMatrix::setElement(unsigned int i, unsigned int j, double d)
339 {
340 #ifdef DEBUG
341  if (i>j)
342  std::cerr << "WARNING: void SgUtMatrix::set(unsigned int, unsigned int, double):"
343  << " row's index [" << i << "] greater than the column index [" << j << "]\n";
344  if (NRow_<=i)
345  std::cerr << "WARNING: void SgUtMatrix::set(unsigned int, unsigned int, double):"
346  << " row's index [" << i << "] out of range [0.." << NRow_-1 << "]\n";
347  if (NCol_<=j)
348  std::cerr << "WARNING: void SgUtMatrix::set(unsigned int, unsigned int, double):"
349  << " column's index [" << j << "] out of range [0.." << NCol_-1 << "]\n";
350 #endif //DEBUG
351 
352  if (i<=j && j<NCol_)
353  *(*(B_+j)+i) = d;
354 };
355 
356 
357 
358 
359 //
360 // FUNCTIONS:
361 //
362 //
363 // Fills all elements with zeros:
364 inline void SgUtMatrix::clear()
365 {
366  double **w=B_;
367  for (unsigned int i=0; i<NCol_; i++, w++)
368  memset((void*)(*w), 0, sizeof(double)*(i+1));
369 };
370 
371 
372 
373 // returns transposed matrix:
374 inline SgMatrix SgUtMatrix::T() const
375 {
376  SgMatrix M(NCol_,NRow_, false);
377  unsigned int i, j;
378  for (j=0; j<M.nCol(); j++)
379  for (i=0; i<M.nRow(); i++)
380  M.setElement(j,i, i<=j?*(*(B_+i)+j):0.0);
381  return M;
382 };
383 
384 
385 
386 // fill matrix with a constant value:
388 {
389  double **w=B_, *ww;
390  unsigned int i, j;
391  for (i=0; i<NCol_; i++,w++)
392  for (ww=*w,j=0; j<=i; j++,w++)
393  *ww++ = d;
394  return *this;
395 };
396 
397 
398 
399 // increment:
401 {
402 #ifdef DEBUG
403  if (NRow_!=R.NRow_)
404  std::cerr << "WARNING: SgUtMatrix& SgUtMatrix::operator+= (const SgUtMatrix&):"
405  << " ranges of matrices are different (rows): " << NRow_ << " and "
406  << R.NRow_ << "\n";
407  if (NCol_!=R.NCol_)
408  std::cerr << "WARNING: SgUtMatrix& SgUtMatrix::operator+= (const SgUtMatrix&):"
409  << " ranges of matrices are different (columns): " << NCol_ << " and "
410  << R.NCol_ << "\n";
411 #endif //DEBUG
412 
413  double **w=B_, **q=R.B_, *ww, *qq;
414  unsigned int N=std::min(NCol_,R.NCol_), i, j;
415  for (i=0; i<N; i++,w++,q++)
416  for (ww=*w,qq=*q,j=0; j<=i; j++)
417  *ww++ += *qq++;
418  return *this;
419 };
420 
421 
422 
423 // decrement:
425 {
426 #ifdef DEBUG
427  if(NRow_!=R.NRow_)
428  std::cerr << "WARNING: SgUtMatrix& SgUtMatrix::operator-= (const SgUtMatrix&):"
429  << " ranges of matrices are different (rows): " << NRow_ << " and " << R.NRow_
430  << "\n";
431  if(NCol_!=R.NCol_)
432  std::cerr << "WARNING: SgUtMatrix& SgUtMatrix::operator-= (const SgUtMatrix&):"
433  << " ranges of matrices are different (columns): " << NCol_ << " and " << R.NCol_
434  << "\n";
435 #endif //DEBUG
436 
437  double **w=B_, **q=R.B_, *ww, *qq;
438  unsigned int N=std::min(NCol_,R.NCol_), i, j;
439  for(i=0; i<N; i++,w++,q++)
440  for(ww=*w,qq=*q,j=0; j<=i; j++)
441  *ww++ -= *qq++;
442  return *this;
443 };
444 
445 
446 
447 // multiply by a scalar:
449 {
450  double **w=B_, *ww;
451  unsigned int i, j;
452  for (i=0; i<NCol_; i++,w++)
453  for (ww=*w,j=0; j<=i; j++)
454  *ww++ *= d;
455  return *this;
456 };
457 
458 
459 
460 // divide by a scalar:
462 {
463  double **w=B_, *ww;
464  unsigned int i, j;
465  for (i=0; i<NCol_; i++,w++)
466  for (ww=*w,j=0; j<=i; j++)
467  *ww++ /= d;
468  return *this;
469 };
470 
471 
472 
473 
474 //
475 // FRIENDS:
476 //
477 //
478 //
480 {
481  return SgUtMatrix(R)*=-1.0;
482 };
483 
484 
485 
486 //
487 inline SgUtMatrix operator/(const SgUtMatrix& R, double d)
488 {
489  return SgUtMatrix(R)/=d;
490 };
491 
492 
493 
494 //
495 inline SgUtMatrix operator*(const SgUtMatrix& R, double d)
496 {
497  return SgUtMatrix(R)*=d;
498 };
499 
500 
501 
502 //
503 inline SgUtMatrix operator*(double d, const SgUtMatrix& R)
504 {
505  return SgUtMatrix(R)*=d;
506 };
507 
508 
509 
510 //
511 inline SgUtMatrix operator+(const SgUtMatrix& R1, const SgUtMatrix& R2)
512 {
513  return SgUtMatrix(R1)+=R2;
514 };
515 
516 
517 
518 //
519 inline SgUtMatrix operator-(const SgUtMatrix& R1, const SgUtMatrix& R2)
520 {
521  return SgUtMatrix(R1)-=R2;
522 };
523 /*=====================================================================================================*/
524 
525 
526 
527 
528 
529 /*=====================================================================================================*/
530 //
531 // aux functions:
532 //
533 // output to std stream:
538 std::ostream &operator<<(std::ostream& s, const SgUtMatrix& R);
539 
540 
541 
547 SgVector& solveEquation(const SgUtMatrix& R, SgVector& x, const SgVector& z);
548 
549 
550 
551 /*=====================================================================================================*/
552 #endif // SG_UT_MATRIX_H
std::ostream & operator<<(std::ostream &s, const SgUtMatrix &R)
Definition: SgUtMatrix.cpp:205
SgVector & solveEquation(const SgUtMatrix &R, SgVector &x, const SgVector &z)
Definition: SgUtMatrix.cpp:166
SgUtMatrix operator+(const SgUtMatrix &R1, const SgUtMatrix &R2)
Definition: SgUtMatrix.h:511
SgUtMatrix operator*(const SgUtMatrix &R, double d)
Definition: SgUtMatrix.h:495
SgUtMatrix operator-(const SgUtMatrix &R)
Definition: SgUtMatrix.h:479
SgUtMatrix operator/(const SgUtMatrix &R, double d)
Definition: SgUtMatrix.h:487
double dTmp_
Local temporary variable.
Definition: SgMatrix.h:262
unsigned int NCol_
An number of columns in a matrix.
Definition: SgMatrix.h:260
unsigned int nRow() const
Definition: SgMatrix.h:352
void setElement(unsigned int i, unsigned int j, double d)
Definition: SgMatrix.h:402
double ** B_
A pointer on a pointer of a first element of the matrix.
Definition: SgMatrix.h:261
unsigned int nCol() const
Definition: SgMatrix.h:360
unsigned int NRow_
An number of rows in a matrix.
Definition: SgMatrix.h:249
SgUtMatrix & operator*=(double d)
Definition: SgUtMatrix.h:448
SgMatrix T() const
Definition: SgUtMatrix.h:374
SgUtMatrix & operator-=(const SgUtMatrix &R)
Definition: SgUtMatrix.h:424
friend SgUtMatrix operator+(const SgUtMatrix &R1, const SgUtMatrix &R2)
Definition: SgUtMatrix.h:511
friend SgUtMatrix operator*(const SgUtMatrix &R, double d)
Definition: SgUtMatrix.h:495
unsigned int n() const
Definition: SgUtMatrix.h:290
double & operator()(unsigned int i, unsigned int j)
Definition: SgUtMatrix.h:298
void setElement(unsigned int i, unsigned int j, double d)
Definition: SgUtMatrix.h:338
friend SgMatrix calcProduct_mat_x_mat(const SgUtMatrix &R1, const SgMatrix &M2)
Definition: SgUtMatrix.cpp:124
friend SgUtMatrix operator-(const SgUtMatrix &R)
Definition: SgUtMatrix.h:479
SgUtMatrix & operator=(const SgUtMatrix &R)
Definition: SgUtMatrix.cpp:39
const double *const * base_c() const
Definition: SgUtMatrix.h:204
SgUtMatrix & operator/=(double d)
Definition: SgUtMatrix.h:461
friend SgUtMatrix operator~(const SgUtMatrix &R)
Definition: SgUtMatrix.cpp:90
friend SgUtMatrix operator/(const SgUtMatrix &R, double d)
Definition: SgUtMatrix.h:487
double getElement(unsigned int i, unsigned int j) const
Definition: SgUtMatrix.h:318
void clear()
Definition: SgUtMatrix.h:364
SgUtMatrix & operator+=(const SgUtMatrix &R)
Definition: SgUtMatrix.h:400
double **& base()
Definition: SgUtMatrix.h:201