SheafSystem  0.0.0.0
general_matrix_2x1.impl.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for general_matrix_2x1.
19 
20 
21 #ifndef GENERAL_MATRIX_2X1_IMPL_H
22 #define GENERAL_MATRIX_2X1_IMPL_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 #ifndef ASSERT_CONTRACT_H
29 #include "SheafSystem/assert_contract.h"
30 #endif
31 
32 #ifndef ERROR_MESSAGE_H
33 #include "SheafSystem/error_message.h"
34 #endif
35 
36 #ifndef GENERAL_MATRIX_1X2_H
37 #include "SheafSystem/general_matrix_1x2.h"
38 #endif
39 
40 #ifndef GENERAL_MATRIX_1X3_H
41 #include "SheafSystem/general_matrix_1x3.h"
42 #endif
43 
44 #ifndef GENERAL_MATRIX_2X1_H
45 #include "SheafSystem/general_matrix_2x1.h"
46 #endif
47 
48 #ifndef GENERAL_MATRIX_2X2_H
49 #include "SheafSystem/general_matrix_2x2.h"
50 #endif
51 
52 #ifndef GENERAL_MATRIX_2X3_H
53 #include "SheafSystem/general_matrix_2x3.h"
54 #endif
55 
56 #ifndef STD_SSTREAM_H
57 #include "SheafSystem/std_sstream.h"
58 #endif
59 
60 #ifndef STD_CMATH_H
61 #include "SheafSystem/std_cmath.h"
62 #endif
63 
64 namespace fiber_bundle
65 {
66 
67 using namespace sheaf;
68 
69 //==============================================================================
70 // MEMBER FUNCTIONS
71 //==============================================================================
72 
73 
75 template <typename T>
76 int
79 {
80  // Preconditions:
81 
82  // Body:
83 
84  // Postconditions:
85 
86  // Exit:
87 
88  return 2;
89 }
90 
92 template <typename T>
93 int
96 {
97  // Preconditions:
98 
99  // Body:
100 
101  // Postconditions:
102 
103  // Exit:
104 
105  return 1;
106 }
107 
109 template <typename T>
110 int
112 d()
113 {
114  // Preconditions:
115 
116  // Body:
117 
118  // Postconditions:
119 
120  // Exit:
121 
122  return 2;
123 }
124 
126 template <typename T>
127 T*
129 operator[](int nrow)
130 {
131  // Preconditions:
132 
133  require(nrow >= 0 && nrow < number_of_rows());
134 
135  // Body:
136 
137  T* result = &components[row_index(nrow)];
138 
139  // Postconditions:
140 
141  ensure(result != 0);
142 
143  // Exit:
144 
145  return result;
146 }
147 
148 template <typename T>
149 const T*
151 operator[](int nrow) const
152 {
153  // Preconditions:
154 
155  require(nrow >= 0 && nrow < number_of_rows());
156 
157  // Body:
158 
159  const T* result = &components[row_index(nrow)];
160 
161  // Postconditions:
162 
163  ensure(result != 0);
164 
165  // Exit:
166 
167  return result;
168 }
169 
171 template <typename T>
173 operator T* ()
174 {
175  // Preconditions:
176 
177  // Body:
178 
179  //cout << "general_matrix_2x1<T>::operator T* () " << std::endl;
180 
181  T* result = components;
182 
183  // Postconditions:
184 
185  ensure(result != 0);
186 
187  // Exit:
188 
189  return result;
190 }
191 
193 template <typename T>
195 operator const T* () const
196 {
197  // Preconditions:
198 
199  // Body:
200 
201  //cout << "general_matrix_2x1<T>::operator const T* () const " << std::endl;
202 
203  const T* result = components;
204 
205  // Postconditions:
206 
207  ensure(result != 0);
208 
209  // Exit:
210 
211  return result;
212 }
213 
215 template <typename T>
216 int
218 row_index(int xrow) const
219 {
220 
221  // Preconditions:
222 
223  require(xrow >= 0 && xrow < number_of_rows());
224 
225  // Body:
226 
227  //int result = number_of_columns()*nrow;
228  //int result = 1*xrow;
229  int result = xrow;
230 
231  // Postconditions:
232 
233  ensure(result == number_of_columns()*xrow);
234 
235  // Exit:
236 
237  return result;
238 }
239 
241 template <typename T>
242 void
244 assign(const T& xvalue)
245 {
246  // Preconditions:
247 
248  // Body:
249 
250  for(int i=0; i<d(); ++i)
251  {
252  components[i] = xvalue;
253  }
254 
255  // Postconditions:
256 
257  ensure_for_all(i, 0, d(), components[i] == xvalue);
258 
259  // Exit:
260 
261 }
262 
264 template <typename T>
265 void
267 multiply(const T& xscalar, general_matrix_2x1<T>& xresult) const
268 {
269  // Preconditions:
270 
271  // Body:
272 
273  for(int i=0; i<d(); ++i)
274  {
275  xresult.components[i] = xscalar*components[i];
276  }
277 
278  // Postconditions:
279 
280  //@issue $$ISSUE: We can't make the following comparison because of
281  // floating point roundoff.
282  //ensure_for_all(i, 0, d(), xresult.components[i] == xscalar*components[i]);
283 
284  // Exit:
285 
286 }
287 
289 template <typename T>
292 multiply(const T& xscalar) const
293 {
294  // Preconditions:
295 
296  // Body:
297 
298  general_matrix_2x1<T> result;
299  multiply(xscalar, result);
300 
301  // Postconditions:
302 
303  // Exit:
304 
305  return result;
306 }
307 
309 template <typename T>
310 void
313  general_matrix_2x2<T>& xresult) const
314 {
315  // Preconditions:
316 
317  // Body:
318 
319  // Multiply [A][B] where [A] = *this & [B] = xother.
320 
321  const general_matrix_2x1<T>& lm = *this;
322 
323  int nra = number_of_rows();
324  int nca = number_of_columns();
325  int ncb = xother.number_of_columns();
326 
327  for(int i=0; i<nra; ++i)
328  {
329  for(int j=0; j<ncb; ++j)
330  {
331  T sum = T(0);
332 
333  for(int k=0; k<nca; ++k)
334  {
335  sum += lm[i][k]*xother[k][j];
336  }
337 
338  xresult[i][j] = sum;
339  }
340  }
341 
342  // Postconditions:
343 
344  // Exit:
345 
346 }
347 
349 template <typename T>
352 multiply(const general_matrix_1x2<T>& xother) const
353 {
354  // Preconditions:
355 
356  // Body:
357 
358  general_matrix_2x2<T> result;
359  multiply(xother, result);
360 
361  // Postconditions:
362 
363  // Exit:
364 
365  return result;
366 }
367 
369 template <typename T>
370 void
373  general_matrix_2x3<T>& xresult) const
374 {
375  // Preconditions:
376 
377  // Body:
378 
379  // Multiply [A][B] where [A] = *this & [B] = xother.
380 
381  const general_matrix_2x1<T>& lm = *this;
382 
383  int nra = number_of_rows();
384  int nca = number_of_columns();
385  int ncb = xother.number_of_columns();
386 
387  for(int i=0; i<nra; ++i)
388  {
389  for(int j=0; j<ncb; ++j)
390  {
391  T sum = T(0);
392 
393  for(int k=0; k<nca; ++k)
394  {
395  sum += lm[i][k]*xother[k][j];
396  }
397 
398  xresult[i][j] = sum;
399  }
400  }
401 
402  // Postconditions:
403 
404  // Exit:
405 
406 }
407 
409 template <typename T>
412 multiply(const general_matrix_1x3<T>& xother) const
413 {
414  // Preconditions:
415 
416  // Body:
417 
418  general_matrix_2x3<T> result;
419  multiply(xother, result);
420 
421  // Postconditions:
422 
423  // Exit:
424 
425  return result;
426 }
427 
429 template <typename T>
430 void
433 {
434  // Preconditions:
435 
436  // Body:
437 
438  const general_matrix_2x1<T>& lm = *this;
439 
440  xresult[0][0] = lm[0][0];
441  xresult[0][1] = lm[1][0];
442 
443  // Postconditions:
444 
445  // Exit:
446 
447 }
448 
450 template <typename T>
453 transpose() const
454 {
455  // Preconditions:
456 
457  // Body:
458 
459  general_matrix_1x2<T> result;
460  transpose(result);
461 
462  // Postconditions:
463 
464  // Exit:
465 
466  return result;
467 }
468 
469 
470 // =============================================================================
471 // NON-MEMBER FUNCTIONS
472 // =============================================================================
473 
474 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
475 
477 template <typename T>
478 std::ostream& operator<<(std::ostream& xos, const general_matrix_2x1<T>& xm)
479 {
480  // Preconditions:
481 
482  // Body:
483 
484  int nrows = xm.number_of_rows();
485  int ncols = xm.number_of_columns();
486 
487  for(int i=0; i<nrows; ++i)
488  {
489  for(int j=0; j<ncols; ++j)
490  {
491  xos << " " << xm[i][j];
492  }
493  xos << std::endl;
494  }
495 
496  // Postconditions:
497 
498  // Exit:
499 
500  return xos;
501 }
502 
503 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
504 
505 //==============================================================================
506 //==============================================================================
507 
508 } // namespace fiber_bundle
509 
510 #endif // GENERAL_MATRIX_2X1_IMPL_H
511 
static int number_of_columns()
The number of columns.
static int d()
Dimension of the underlying elements.
T * operator[](int xrow)
Pointer to the first element in row xrow of this matrix. Facilitates accessing elements via matrix[i]...
static int number_of_rows()
The number of rows.
General matrix with 1 row and 2 columns.
General matrix with 2 rows and 2 columns.
void multiply(const T &xscalar, general_matrix_2x1< T > &xresult) const
This matrix multiplied by a scalar (pre-allocated).
T components[2]
Linear storage array.
static int number_of_columns()
The number of columns.
int row_index(int xrow) const
Index for row xrow in the linear storage array.
static int number_of_columns()
The number of columns.
General matrix with 2 rows and 1 column.
static int number_of_rows()
The number of rows.
general_matrix_1x2< T > transpose() const
The transpose of the matrix (auto-allocated).
General matrix with 1 row and 3 columns.
Namespace for the sheaves component of the sheaf system.
General matrix with 2 rows and 3 columns.
SHEAF_DLL_SPEC void multiply(const vd &x0, const vd_value_type &x1, vd &xresult, bool xauto_access)
Vector x0 multiplied by scalar x1 (pre-allocated version for persistent types).
Definition: vd.cc:1924
Namespace for the fiber_bundles component of the sheaf system.
void assign(const T &xvalue)
Assign all elements of this matrix to the value xvalue.