General Purpose Geodetic Library
SgSymMatrix.cpp
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 #include <iostream>
24 
25 #include <QtCore/QString>
26 
27 #include <SgSymMatrix.h>
28 
29 
30 
31 
32 /*=======================================================================================================
33 *
34 * METHODS:
35 *
36 *======================================================================================================*/
37 
38 
39 
40 
41 /*=======================================================================================================
42 *
43 * FRIENDS:
44 *
45 *======================================================================================================*/
46 //
47 // matrix x vector
49 {
50 #ifdef DEBUG
51  if (P.nCol()!=V.n())
52  std::cerr << "WARNING: SgVector operator*(const SgSymMatrix&, const SgVector&): "
53  << "incompatible ranges of matrix (" << P.nCol() << ") and vector ("
54  << V.n() << ")\n";
55 #endif //DEBUG
56 
57  unsigned int N=std::min(P.nRow(), V.n()), i, l;
58  SgVector X(N, false);
59  double d;
60  for (i=0; i<N; i++)
61  {
62  d = 0.0;
63  for (l=0; l<N; l++)
64  d += P.getElement(i,l)*V.getElement(l);
65  X.setElement(i, d);
66  };
67  return X;
68 };
69 
70 
71 
72 // matrix x matrix
74 {
75 #ifdef DEBUG
76  if (M1.nCol()!=P2.nRow()) // complain:
77  std::cerr << "WARNING: SgMatrix calcProduct_mat_x_mat(const SgMatrix& M1, "
78  << "const SgSymMatrix& P2): matrix size mismatch.\n";
79 #endif //DEBUG
80 
81  unsigned int N=std::min(M1.nCol(), P2.nRow()), i, j, l;
82  unsigned int NRow=M1.nRow(), NCol=P2.nCol();
83  SgMatrix M(NRow, NCol, false);
84 /*
85  // optimized version:
86  double **m=M.B_, **m1=M1.B_, **m2=M2.B_, *mm, *mm2;
87 
88  for (j=0; j<M.NCol_; j++,m++,m2++)
89  for (mm=*m,i=0; i<M.NRow_; i++,mm++)
90  for (mm2=*m2,*mm=0.0,l=0; l<N; l++,mm2++)
91  *mm += *(*(m1+l)+i)**mm2;
92 */
93  // that should be good for all types of matrices:
94  for (j=0; j<NCol; j++)
95  for (i=0; i<NRow; i++)
96  {
97 // long double d=0.0;
98  double d=0.0;
99  for (l=0; l<N; l++)
100  d += M1.getElement(i,l)*P2.getElement(l,j);
101 // M.setElement(i,j, ((double)d));
102  M.setElement(i,j, d);
103  };
104  return M;
105 };
106 
107 
108 
109 
110 
111 /*=====================================================================================================*/
112 //
113 // aux functions:
114 //
115 // i/o:
116 std::ostream &operator<<(std::ostream& s, const SgSymMatrix& P)
117 {
118  unsigned int i, j;
119  QString str("");
120  if (P.nCol()<55) // trying to write in conventional form:
121  {
122  for (i=0; i<P.nRow(); i++)
123  {
124  s << "|";
125  for (j=0; j<P.nCol(); j++)
126  s << " " << qPrintable(str.sprintf("%12.5e", P.getElement(i,j))) << " ";
127  s << "|\n";
128  };
129  }
130  else // will use a similar to SINEX format presentation:
131  {
132  for (j=0; j<P.nCol(); j++)
133  for (i=0; i<=j; i++)
134  s << i << " " << j << " " << P.getElement(i,j) << "\n";
135  };
136  return s;
137 };
138 /*=====================================================================================================*/
SgVector operator*(const SgSymMatrix &P, const SgVector &V)
Definition: SgSymMatrix.cpp:48
std::ostream & operator<<(std::ostream &s, const SgSymMatrix &P)
SgMatrix calcProduct_mat_x_mat(const SgMatrix &M1, const SgSymMatrix &P2)
Definition: SgSymMatrix.cpp:73
unsigned int nRow() const
Definition: SgMatrix.h:352
void setElement(unsigned int i, unsigned int j, double d)
Definition: SgMatrix.h:402
double getElement(unsigned int i, unsigned int j) const
Definition: SgMatrix.h:385
unsigned int nCol() const
Definition: SgMatrix.h:360
double getElement(unsigned int i, unsigned int j) const
Definition: SgSymMatrix.h:274
unsigned int n() const
Definition: SgVector.h:327
void setElement(unsigned int i, double d)
Definition: SgVector.h:348
double getElement(unsigned int i) const
Definition: SgVector.h:362