Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_mat.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2010-20 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_mat.h"
17 
18 // .PORTABILITY : ansi C++
19 
20 //***************************************************************************
21 //member functions
22 //-a_mat--------------------------------------------------------------------
23 a_mat::a_mat(UI i, UI j, double v)
24 {
25  if ((i<1)||(j<1)) throw range_error();
26  maxi_ = i;
27  maxj_ = j;
28  x_ = new std::valarray<double>(v,i*j);
29 }
30 //-a_mat--------------------------------------------------------------------
31 a_mat::a_mat(int i, int j, double v)
32 {
33  if ((i<1)||(j<1)) throw range_error();
34  maxi_ = UI(i);
35  maxj_ = UI(j);
36  x_ = new std::valarray<double>(v,i*j);
37 }
38 //-a_mat--------------------------------------------------------------------
40 {
41  maxi_ = m.maxi();
42  maxj_ = m.maxj();
43  x_ = new std::valarray<double>(maxi_*maxj_);
44  (*x_) = m.x();
45 }
46 //-a_mat--------------------------------------------------------------------
48 {
49  delete x_;
50 }
51 //-operator=-----------------------------------------------------------------
53 {
54  maxi_ = m.maxi();
55  maxj_ = m.maxj();
56  (*x_) = m.x();
57  return (*this);
58 }
59 //-operator=-----------------------------------------------------------------
61 {
62  (*x_) = -(*x_);
63  return (*this);
64 }
65 //-operator==----------------------------------------------------------------
66 bool a_mat::operator==(const a_mat& a)
67 {
68  if (a.maxi() != maxi_) return false;
69  if (a.maxj() != maxj_) return false;
70  for (UI i = 0; i < a.size(); i++)
71  if (a.x()[i] != (*x_)[i]) return false;
72  return true;
73 }
74 //-operator+=----------------------------------------------------------------
76 {
77  if (m.maxi() != maxi_) throw compatibility_error();
78  if (m.maxj() != maxj_) throw compatibility_error();
79  (*x_) += m.x();
80  return (*this);
81 }
82 //----------------------------------------------------------------
84 {
85  a_mat b= *this;
86  return b+m;
87 }
88 //-operator-=----------------------------------------------------------------
90 {
91  if (m.maxi() != maxi_) throw compatibility_error();
92  if (m.maxj() != maxj_) throw compatibility_error();
93  (*x_) -= m.x();
94  return (*this);
95 }
96 //----------------------------------------------------------------
98 {
99  a_mat b= *this;
100  return b-m;
101 }
102 //-operator*=----------------------------------------------------------------
103 a_mat & a_mat::operator*=(const double v)
104 {
105  (*x_) *= v;
106  return (*this);
107 }
108 //----------------------------------------------------------------
109 a_mat a_mat::operator*(const double v)
110 {
111  a_mat b= *this;
112  return v*b;
113 }
114 //-operator/=----------------------------------------------------------------
115 a_mat & a_mat::operator/=(const double v)
116 {
117  (*x_) /= v;
118  return (*this);
119 }
120 //----------------------------------------------------------------
121 a_mat a_mat::operator/(const double v)
122 {
123  a_mat b= *this;
124  return (1./v)*b;
125 }
126 //-transpose-----------------------------------------------------------------
128 {
129  a_mat r(maxj_,maxi_);
130  for (UI i = 0; i<maxi_; i++)
131  {
132  for (UI j = 0; j<maxj_; j++)
133  r(j,i) = (*x_)[i*maxj_+j];
134  }
135  return r;
136 }
137 //-swapc---------------------------------------------------------------------
138 void a_mat::swapc(UI c1, UI c2)
139 {
140  double v;
141  for (UI i = 0; i<maxi_; i++)
142  {
143  v= (*x_)[i*maxj_+c1];
144  (*x_)[i*maxj_+c1] = (*x_)[i*maxj_+c2];
145  (*x_)[i*maxj_+c2] = v;
146  }
147 }
148 //-swapr---------------------------------------------------------------------
149 void a_mat::swapr(UI r1, UI r2)
150 {
151  double v;
152  int rr1 = r1*maxj_;
153  int rr2 = r2*maxj_;
154  for (UI j = 0; j<maxj_; j++)
155  {
156  v= (*x_)[rr1+j];
157  (*x_)[rr1+j] = (*x_)[rr2+j];
158  (*x_)[rr2+j] = v;
159  }
160 }
161 //-sub_matrix----------------------------------------------------------------
162 a_mat a_mat::sub_matrix(const UI i0, const UI j0) const
163 {
164  if ((i0>maxi_-1)||(j0>maxj_-1)) throw range_error();
165  if ((maxi_<2)||(maxj_<2)) throw compatibility_error();
166  a_mat N(maxi_-1,maxj_-1);
167  int ni = -1;
168  for (UI i = 0; i<maxi_; i++)
169  {
170  if (i != i0)
171  {
172  ni += 1;
173  double nj = -1;
174  for (UI j = 0; j<maxj_; j++)
175  {
176  if (j != j0)
177  {
178  nj += 1;
179  N(ni,nj) = (*this)(i,j);
180  }
181  }
182  }
183  }
184  return N;
185 }
186 //***************************************************************************
187 //-operator>>----------------------------------------------------------------
188 std::istream& operator>> (std::istream& i, UI& v)
189 {
190  int v2;
191  i >> v2;
192  v = UI(v2);
193  return i;
194 }
195 //-operator<<----------------------------------------------------------------
196 std::ostream& operator<< (std::ostream& o, const UI& v)
197 {
198  int v2 = int(v);
199  o << v2;
200  return o;
201 }
202 //-operator<<----------------------------------------------------------------
203 /*std::ostream& operator<< (std::ostream& o, UI v)
204  {
205  int v2 = int(v);
206  o << v2;
207  return o;
208  }*/
209 //-operator>>----------------------------------------------------------------
210 //---------------------------------------------------------------------------
211 void a_mat::read(std::istream &in)
212 {
213  int i0,j0;
214  in >> i0;
215  in >> j0;
216  if ((i0 != maxi_)||(j0 != maxj_)) throw a_mat::range_error();
217  for (UI j=0; j < maxi_; j++)
218  {
219  for (UI k=0; k < maxj_; k++)
220  in >> this->operator()(j,k);
221  }
222 }
223 //---------------------------------------------------------------------------
224 void a_mat::write(std::ostream &o) const
225 {
226  o << (int)maxi_ << " " << (int)maxj_ << std::endl;
227  for (UI i=0; i<maxi_; i++)
228  {
229  for (UI j=0; j<maxj_; j++)
230  o << this->operator()(i,j) << " ";
231  o << std::endl;
232  }
233 }
234 /*
235  std::istream& operator>> (std::istream& i, a_mat & m)
236  {
237  int i0,j0;
238  i >> i0;
239  i >> j0;
240  if ((i0 != m.maxi_)||(j0 != m.maxj_)) throw a_mat::range_error();
241  for (UI j=0; j < m.maxi_; j++)
242  {
243  for (UI k=0; k < m.maxj_; k++)
244  i >> m(j,k);
245  }
246  return i;
247  }
248 //-operator<<----------------------------------------------------------------
249 std::ostream& operator<< (std::ostream& o, const a_mat & m)
250 {
251 o << (int)m.maxi() << " " << (int)m.maxj() << std::endl;
252 for (UI i=0; i<m.maxi_; i++)
253 {
254 for (UI j=0; j<m.maxj_; j++)
255 o << m(i,j) << " ";
256 o << std::endl;
257 }
258 return o;
259 }
260 */
261 //***************************************************************************
262 //-operator+-----------------------------------------------------------------
264 {
265  a_mat r = a;
266  return r += b;
267 }
268 //-operator------------------------------------------------------------------
270 {
271  a_mat r = a;
272  return r -= b;
273 }
274 //-operator*-----------------------------------------------------------------
276 {
277  if (a.maxj() != b.maxi()) throw a_mat::compatibility_error();
278  a_mat r(a.maxi(),b.maxj());
279  double v;
280  for (UI i = 0; i < a.maxi(); i++)
281  {
282  for (UI j = 0; j < b.maxj(); j++)
283  {
284  v = 0;
285  for (UI k = 0; k < a.maxj(); k++)
286  v += a(i,k)*b(k,j);
287  r(i,j) = v;
288  }
289  }
290  return r;
291 }
292 //-operator*-----------------------------------------------------------------
293 a_mat operator*(a_mat & a, const double v)
294 {
295  a_mat r = a;
296  return r *= v;
297 }
298 //-operator*-----------------------------------------------------------------
299 a_mat operator*(const double v, a_mat & a)
300 {
301  a_mat r = a;
302  return r *= v;
303 }
304 //-operator/-----------------------------------------------------------------
305 a_mat operator/(a_mat & a, const double v)
306 {
307  a_mat r = a;
308  return r /= v;
309 }
a_mat operator/(a_mat &a, const double v)
Definition: a_mat.cxx:305
a_mat operator-(a_mat &a, a_mat &b)
Definition: a_mat.cxx:269
a_mat operator*(a_mat &a, a_mat &b)
Definition: a_mat.cxx:275
a_mat operator+(a_mat &a, a_mat &b)
Definition: a_mat.cxx:263
std::ostream & operator<<(std::ostream &o, const UI &v)
Definition: a_mat.cxx:196
std::istream & operator>>(std::istream &i, UI &v)
Definition: a_mat.cxx:188
unsigned int UI
Definition: a_mat.h:23
Definition: a_mat.h:42
a_mat operator*(double v)
Definition: a_mat.cxx:109
a_mat & operator*=(double v)
Definition: a_mat.cxx:103
std::valarray< double > * x_
Definition: a_mat.h:91
a_mat(UI i, UI j, double v=0)
Definition: a_mat.cxx:23
a_mat & operator=(const a_mat &m)
Definition: a_mat.cxx:52
void swapc(UI c1, UI c2)
Definition: a_mat.cxx:138
a_mat operator/(double v)
Definition: a_mat.cxx:121
UI maxj_
Definition: a_mat.h:89
a_mat operator+(const a_mat &a)
Definition: a_mat.cxx:83
std::valarray< double > & x() const
Definition: a_mat.h:53
a_mat sub_matrix(const UI i, const UI j) const
Definition: a_mat.cxx:162
a_mat & operator/=(double v)
Definition: a_mat.cxx:115
a_mat transpose()
Definition: a_mat.cxx:127
bool operator==(const a_mat &a)
Definition: a_mat.cxx:66
UI maxi_
Definition: a_mat.h:88
a_mat & operator-=(const a_mat &a)
Definition: a_mat.cxx:89
a_mat & operator+=(const a_mat &a)
Definition: a_mat.cxx:75
virtual void read(std::istream &i)
Definition: a_mat.cxx:211
UI size() const
Definition: a_mat.h:51
a_mat & operator-()
Definition: a_mat.cxx:60
UI maxi() const
Definition: a_mat.h:49
UI maxj() const
Definition: a_mat.h:50
virtual void write(std::ostream &o) const
Definition: a_mat.cxx:224
void swapr(UI r1, UI r2)
Definition: a_mat.cxx:149
~a_mat()
Definition: a_mat.cxx:47