General Purpose Geodetic Library
SgMatrix.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_MATRIX_H
24 #define SG_MATRIX_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 <math.h>
40 
41 #include <stdio.h>
42 #include <iostream>
43 
44 #include <SgMathSupport.h>
45 #include <SgVector.h>
46 
47 
48 
49 class SgUtMatrix;
50 class SgSymMatrix;
51 
52 /***==============================================================================================*/
57 /*================================================================================================*/
58 class SgMatrix
59 {
60  //friend class SgUtMatrix;
61 public:
62  //
63  // constructors/destructors:
64  //
68  inline SgMatrix();
69 
77  inline SgMatrix(unsigned int NRow, unsigned int NCol, bool IsNeedClear = true);
78 
83  inline SgMatrix(const SgMatrix& M);
84 
88  inline ~SgMatrix();
89 
90 
91  //
92  // Interfaces:
93  //
96  inline unsigned int nRow() const;
97 
100  inline unsigned int nCol() const;
101 
106  inline double& operator()(unsigned int i, unsigned int j);
107 
112  inline double getElement(unsigned int i, unsigned int j) const;
113 
119  inline void setElement(unsigned int i, unsigned int j, double d);
120 
121 
122 
123  //
124  // Functions:
125  //
128  inline void clear();
129 
132  inline SgMatrix T() const;
133 
137  SgMatrix& operator=(const SgMatrix& M);
138 
142  inline SgMatrix& operator=(double d);
143 
147  inline SgMatrix& operator+=(const SgMatrix& M);
148 
152  inline SgMatrix& operator-=(const SgMatrix& M);
153 
157  inline SgMatrix& operator*=(double d);
158 
162  inline SgMatrix& operator/=(double d);
163 
167  inline bool operator==(const SgMatrix& M) const;
168 
172  inline bool operator!=(const SgMatrix& M) const;
173 
174 
175 
176  //
177  // Friends:
178  //
182  friend inline SgMatrix operator-(const SgMatrix& M);
183 
188  friend inline SgMatrix operator/(const SgMatrix& M, double d);
189 
194  friend inline SgMatrix operator*(const SgMatrix& M, double d);
195 
200  friend inline SgMatrix operator*(double d, const SgMatrix& M);
201 
206  friend inline SgMatrix operator+(const SgMatrix& M1, const SgMatrix& M2);
207 
212  friend inline SgMatrix operator-(const SgMatrix& M1, const SgMatrix& M2);
213 
218  friend SgVector operator*(const SgMatrix& M, const SgVector& V);
219 
224  // friend inline SgMatrix operator*(const SgMatrix& M1, const SgMatrix& M2);
225  friend SgMatrix calcProduct_mat_x_mat(const SgMatrix& M1, const SgMatrix& M2);
226  friend SgMatrix calcProduct_mat_x_mat(const SgUtMatrix& U1, const SgMatrix& M2);
227  friend SgMatrix calcProduct_mat_x_mat(const SgMatrix& M1, const SgSymMatrix& P2);
228 
233  friend SgMatrix calcProduct_matT_x_mat(const SgMatrix& M1, const SgMatrix& M2);
234 
239  friend SgMatrix calcProduct_mat_x_matT(const SgMatrix& M1, const SgMatrix& M2);
240 
245  friend SgVector calcProduct_matT_x_vec(const SgMatrix& M, const SgVector& V);
246 
247  inline double** &base() {return B_;};
248 
249  inline const double* const* base_c() const {return B_;};
250 
251  //
252  // I/O:
253  //
254  // ...
255 
256 
257 
258 protected:
259  unsigned int NRow_;
260  unsigned int NCol_;
261  double **B_;
262  double dTmp_;
263 };
264 /*==============================================================================================*/
265 
266 
267 
268 
269 /*================================================================================================*/
270 /* */
271 /* SgMatrix's inline members: */
272 /* */
273 /*================================================================================================*/
274 //
275 //
276 // CONSTRUCTORS:
277 //
278 // An empty constructor:
280 {
281  B_ = NULL;
282  NCol_ = NRow_ = 0;
283  dTmp_ = 0.0;
284 };
285 
286 
287 
288 // A regular constructor:
289 inline SgMatrix::SgMatrix(unsigned int NRow, unsigned int NCol, bool IsNeedClear)
290 {
291 /*
292 #ifdef DEBUG
293  if (NRow == 0)
294  std::cerr << "WARNING: SgMatrix::SgMatrix(unsigned int NRow, unsigned int NCol, bool IsNeedClear): "
295  << "row index is zero\n";
296  if (NCol == 0)
297  std::cerr << "WARNING: SgMatrix::SgMatrix(unsigned int NRow, unsigned int NCol, bool IsNeedClear): "
298  << "column index is zero\n";
299 #endif //DEBUG
300 */
301  dTmp_ = 0.0;
302  NRow_ = NRow;
303  B_ = new double*[NCol_=NCol];
304  double **w = B_;
305  for (unsigned int i=0; i<NCol_; i++, w++)
306  {
307  *w = new double[NRow_];
308  if (IsNeedClear)
309  memset((void*)(*w), 0, sizeof(double)*NRow_);
310  };
311 };
312 
313 
314 
315 // A copying constructor:
316 inline SgMatrix::SgMatrix(const SgMatrix& M)
317 {
318  dTmp_ = 0.0;
319  NRow_ = M.NRow_;
320  B_ = new double*[NCol_=M.NCol_];
321  double **w=B_, **q=M.B_;
322  for (unsigned int i=0; i<NCol_; i++,w++,q++)
323  {
324  *w = new double[NRow_];
325  memcpy((void*)(*w), (const void*)(*q), NRow_*sizeof(double));
326  };
327 };
328 
329 
330 
331 // A destructor:
333 {
334  if (B_)
335  {
336  double **w=B_;
337  for (unsigned int i=0; i<NCol_; i++,w++)
338  delete[] *w;
339  delete[] B_;
340  B_ = NULL;
341  NCol_ = NRow_ = 0;
342  };
343 };
344 
345 
346 
347 
348 //
349 // INTERFACES:
350 //
351 // returns number of rows:
352 inline unsigned int SgMatrix::nRow() const
353 {
354  return NRow_;
355 };
356 
357 
358 
359 // returns number of columns:
360 inline unsigned int SgMatrix::nCol() const
361 {
362  return NCol_;
363 };
364 
365 
366 
367 //
368 inline double& SgMatrix::operator()(unsigned int i, unsigned int j)
369 {
370 #ifdef DEBUG
371  if (NRow_<=i)
372  std::cerr << "WARNING: double& SgMatrix::operator()(unsigned int i, unsigned int j): "
373  << "row index [" << i << "] out of range [0.." << NRow_-1 << "].\n";
374  if (NCol_<=j)
375  std::cerr << "WARNING: double& SgMatrix::operator()(unsigned int i, unsigned int j): "
376  << "column index [" << j << "] out of range [0.." << NCol_-1 << "].\n";
377 #endif //DEBUG
378 
379  return (i<NRow_ && j<NCol_)?*(*(B_+j)+i):dTmp_;
380 };
381 
382 
383 
384 //
385 inline double SgMatrix::getElement(unsigned int i, unsigned int j) const
386 {
387 #ifdef DEBUG
388  if (NRow_<=i)
389  std::cerr << "WARNING: double SgMatrix::getElement(unsigned int i, unsigned int j) const: "
390  << "row index [" << i << "] out of range [0.." << NRow_-1 << "].\n";
391  if (NCol_<=j)
392  std::cerr << "WARNING: double SgMatrix::getElement(unsigned int i, unsigned int j) const: "
393  << "column index [" << j << "] out of range [0.." << NCol_-1 << "].\n";
394 #endif //DEBUG
395 
396  return (i<NRow_ && j<NCol_)?*(*(B_+j)+i):0.0;
397 };
398 
399 
400 
401 //
402 inline void SgMatrix::setElement(unsigned int i, unsigned int j, double d)
403 {
404 #ifdef DEBUG
405  if (NRow_<=i)
406  std::cerr << "WARNING: void SgMatrix::setElement(unsigned int i, unsigned int j, double d): "
407  << "row index [" << i << "] out of range [0.." << NRow_-1 << "].\n";
408  if (NCol_<=j)
409  std::cerr << "WARNING: void SgMatrix::setElement(unsigned int i, unsigned int j, double d): "
410  << "column index [" << j << "] out of range [0.." << NCol_-1 << "].\n";
411 #endif //DEBUG
412 
413  if (i<NRow_ && j<NCol_)
414  *(*(B_+j)+i) = d;
415 };
416 
417 
418 
419 
420 //
421 // FUNCTIONS:
422 //
423 //
424 // Fills all elements with zeros:
425 inline void SgMatrix::clear()
426 {
427  double **w=B_;
428  for (unsigned int i=0; i<NCol_; i++, w++)
429  memset((void*)(*w), 0, sizeof(double)*NRow_);
430 };
431 
432 
433 
434 // fills all elements with a scalar:
435 inline SgMatrix& SgMatrix::operator=(double d)
436 {
437  double **w=B_, *ww;
438  unsigned int i, j;
439  for (i=0; i<NCol_; i++,w++)
440  for (ww=*w,j=0; i<NRow_; j++,w++)
441  *ww++ = d;
442  return *this;
443 };
444 
445 
446 
447 // increment:
449 {
450  double **w=B_, **q=M.B_, *ww, *qq;
451  unsigned int NCol=std::min(NCol_,M.NCol_), NRow=std::min(NRow_,M.NRow_), i, j;
452  for (i=0; i<NCol; i++,w++,q++)
453  for (ww=*w,qq=*q,j=0; j<NRow; j++)
454  *ww++ += *qq++;
455  return *this;
456 };
457 
458 
459 
460 // decrement:
462 {
463  double **w=B_, **q=M.B_, *ww, *qq;
464  unsigned int NCol=std::min(NCol_,M.NCol_), NRow=std::min(NRow_,M.NRow_), i, j;
465  for (i=0; i<NCol; i++,w++,q++)
466  for (ww=*w,qq=*q,j=0; j<NRow; j++)
467  *ww++-=*qq++;
468  return *this;
469 };
470 
471 
472 
473 // multiply by a scalar:
475 {
476  double **w=B_, *ww;
477  unsigned int i, j;
478  for (i=0; i<NCol_; i++,w++)
479  for (ww=*w,j=0; j<NRow_; j++)
480  *ww++ *= d;
481  return *this;
482 };
483 
484 
485 
486 // divide by a scalar:
488 {
489  double **w=B_, *ww;
490  unsigned int i, j;
491  for (i=0; i<NCol_; i++,w++)
492  for (ww=*w,j=0; j<NRow_; j++)
493  *ww++ /= d;
494  return *this;
495 };
496 
497 
498 
499 // returns transposed matrix:
500 inline SgMatrix SgMatrix::T() const
501 {
502  SgMatrix M(NCol_, NRow_, false);
503  double **a=M.B_, *aa;
504  unsigned int i, j;
505  for (j=0; j<M.NCol_; j++,a++)
506  for (aa=*a,i=0; i<M.NRow_; i++,aa++)
507  *aa = *(*(B_+i)+j);
508  return M;
509 };
510 
511 
512 
513 // is equal?
514 inline bool SgMatrix::operator==(const SgMatrix& M) const
515 {
516  bool Is = true;
517  if (NRow_ != M.NRow_ || NCol_ != M.NCol_) return !Is;
518  // here the dimensions are the same:
519  double **w=B_, **q=M.B_, *ww, *qq;
520  unsigned int i, j;
521  for (i=0; Is && i<NCol_; i++,w++,q++)
522  for (ww=*w,qq=*q,j=0; Is && j<NRow_; j++)
523  Is = Is && *ww++==*qq++;
524  return Is;
525 };
526 
527 
528 
529 // is not equal?
530 inline bool SgMatrix::operator!=(const SgMatrix& M) const
531 {
532  return !(*this==M);
533 };
534 
535 
536 
537 
538 //
539 // FRIENDS:
540 //
541 //
542 // minus sign:
543 inline SgMatrix operator-(const SgMatrix& M)
544 {
545  return SgMatrix(M)*=-1.0;
546 };
547 
548 
549 
550 // matrix / scalar
551 inline SgMatrix operator/(const SgMatrix& M, double d)
552 {
553  return SgMatrix(M)/=d;
554 };
555 
556 
557 
558 // matrix x scalar
559 inline SgMatrix operator*(const SgMatrix& M, double d)
560 {
561  return SgMatrix(M)*=d;
562 };
563 
564 
565 
566 // scalar x matrix
567 inline SgMatrix operator*(double d, const SgMatrix& M)
568 {
569  return SgMatrix(M)*=d;
570 };
571 
572 
573 
574 // matrix + matrix
575 inline SgMatrix operator+(const SgMatrix& M1, const SgMatrix& M2)
576 {
577  return SgMatrix(M1)+=M2;
578 };
579 
580 
581 
582 // matrix - matrix
583 inline SgMatrix operator-(const SgMatrix& M1, const SgMatrix& M2)
584 {
585  return SgMatrix(M1)-=M2;
586 };
587 /*================================================================================================*/
588 
589 
590 
591 
592 /*================================================================================================*/
593 //
594 // aux functions:
595 //
596 // output to std stream:
601 std::ostream &operator<<(std::ostream& s, const SgMatrix& M);
602 
603 
604 
605 /*================================================================================================*/
606 #endif // SG_MATRIX_H
SgMatrix operator/(const SgMatrix &M, double d)
Definition: SgMatrix.h:551
SgMatrix operator+(const SgMatrix &M1, const SgMatrix &M2)
Definition: SgMatrix.h:575
std::ostream & operator<<(std::ostream &s, const SgMatrix &M)
Definition: SgMatrix.cpp:228
SgMatrix operator*(const SgMatrix &M, double d)
Definition: SgMatrix.h:559
SgMatrix operator-(const SgMatrix &M)
Definition: SgMatrix.h:543
friend SgMatrix operator/(const SgMatrix &M, double d)
Definition: SgMatrix.h:551
bool operator==(const SgMatrix &M) const
Definition: SgMatrix.h:514
double dTmp_
Local temporary variable.
Definition: SgMatrix.h:262
friend SgMatrix operator+(const SgMatrix &M1, const SgMatrix &M2)
Definition: SgMatrix.h:575
friend SgMatrix calcProduct_mat_x_matT(const SgMatrix &M1, const SgMatrix &M2)
Definition: SgMatrix.cpp:165
void clear()
Definition: SgMatrix.h:425
friend SgVector calcProduct_matT_x_vec(const SgMatrix &M, const SgVector &V)
Definition: SgMatrix.cpp:192
unsigned int NCol_
An number of columns in a matrix.
Definition: SgMatrix.h:260
SgMatrix & operator*=(double d)
Definition: SgMatrix.h:474
SgMatrix & operator+=(const SgMatrix &M)
Definition: SgMatrix.h:448
SgMatrix()
Definition: SgMatrix.h:279
unsigned int nRow() const
Definition: SgMatrix.h:352
SgMatrix & operator/=(double d)
Definition: SgMatrix.h:487
friend SgMatrix operator*(const SgMatrix &M, double d)
Definition: SgMatrix.h:559
friend SgMatrix calcProduct_matT_x_mat(const SgMatrix &M1, const SgMatrix &M2)
Definition: SgMatrix.cpp:131
double & operator()(unsigned int i, unsigned int j)
Definition: SgMatrix.h:368
friend SgMatrix operator-(const SgMatrix &M)
Definition: SgMatrix.h:543
SgMatrix & operator-=(const SgMatrix &M)
Definition: SgMatrix.h:461
SgMatrix & operator=(const SgMatrix &M)
Definition: SgMatrix.cpp:39
void setElement(unsigned int i, unsigned int j, double d)
Definition: SgMatrix.h:402
friend SgMatrix calcProduct_mat_x_mat(const SgMatrix &M1, const SgMatrix &M2)
Definition: SgMatrix.cpp:94
const double *const * base_c() const
Definition: SgMatrix.h:249
double getElement(unsigned int i, unsigned int j) const
Definition: SgMatrix.h:385
bool operator!=(const SgMatrix &M) const
Definition: SgMatrix.h:530
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
~SgMatrix()
Definition: SgMatrix.h:332
SgMatrix T() const
Definition: SgMatrix.h:500
unsigned int NRow_
An number of rows in a matrix.
Definition: SgMatrix.h:249
double **& base()
Definition: SgMatrix.h:247