SheafSystem  0.0.0.0
general_matrix_3x1.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_3x1.
19 
20 
21 #ifndef GENERAL_MATRIX_3X1_IMPL_H
22 #define GENERAL_MATRIX_3X1_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_3X1_H
45 #include "SheafSystem/general_matrix_3x1.h"
46 #endif
47 
48 #ifndef GENERAL_MATRIX_3X2_H
49 #include "SheafSystem/general_matrix_3x2.h"
50 #endif
51 
52 #ifndef GENERAL_MATRIX_3X3_H
53 #include "SheafSystem/general_matrix_3x3.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 
74 template <typename T>
75 general_matrix_3x1<T>::
76 operator jcb_e13_row_dofs_type<T>& () const
77 {
78  // Preconditions:
79 
80  // Body:
81 
82  T* lcomps = const_cast<T*>(components);
83 
84  jcb_e13_row_dofs_type<T>& result =
85  reinterpret_cast<jcb_e13_row_dofs_type<T>&>(*lcomps);
86 
87  // Postconditions:
88 
89  // Exit:
90 
91  return result;
92 }
93 
95 template <typename T>
96 int
99 {
100  // Preconditions:
101 
102  // Body:
103 
104  // Postconditions:
105 
106  // Exit:
107 
108  return 3;
109 }
110 
112 template <typename T>
113 int
116 {
117  // Preconditions:
118 
119  // Body:
120 
121  // Postconditions:
122 
123  // Exit:
124 
125  return 1;
126 }
127 
129 template <typename T>
130 int
132 d()
133 {
134  // Preconditions:
135 
136  // Body:
137 
138  // Postconditions:
139 
140  // Exit:
141 
142  return 3;
143 }
144 
146 template <typename T>
147 T*
149 operator[](int nrow)
150 {
151  // Preconditions:
152 
153  require(nrow >= 0 && nrow < number_of_rows());
154 
155  // Body:
156 
157  T* result = &components[row_index(nrow)];
158 
159  // Postconditions:
160 
161  ensure(result != 0);
162 
163  // Exit:
164 
165  return result;
166 }
167 
168 template <typename T>
169 const T*
171 operator[](int nrow) const
172 {
173  // Preconditions:
174 
175  require(nrow >= 0 && nrow < number_of_rows());
176 
177  // Body:
178 
179  const T* result = &components[row_index(nrow)];
180 
181  // Postconditions:
182 
183  ensure(result != 0);
184 
185  // Exit:
186 
187  return result;
188 }
189 
191 template <typename T>
193 operator T* ()
194 {
195  // Preconditions:
196 
197  // Body:
198 
199  //cout << "general_matrix_3x1<T>::operator T* () " << std::endl;
200 
201  T* result = components;
202 
203  // Postconditions:
204 
205  ensure(result != 0);
206 
207  // Exit:
208 
209  return result;
210 }
211 
213 template <typename T>
215 operator const T* () const
216 {
217  // Preconditions:
218 
219  // Body:
220 
221  //cout << "general_matrix_3x1<T>::operator const T* () const " << std::endl;
222 
223  const T* result = components;
224 
225  // Postconditions:
226 
227  ensure(result != 0);
228 
229  // Exit:
230 
231  return result;
232 }
233 
235 template <typename T>
236 int
238 row_index(int xrow) const
239 {
240 
241  // Preconditions:
242 
243  require(xrow >= 0 && xrow < number_of_rows());
244 
245  // Body:
246 
247  //int result = number_of_columns()*xrow;
248  //int result = 1*xrow;
249  int result = xrow;
250 
251  // Postconditions:
252 
253  ensure(result == number_of_columns()*xrow);
254 
255  // Exit:
256 
257  return result;
258 }
259 
261 template <typename T>
262 void
264 assign(const T& xvalue)
265 {
266  // Preconditions:
267 
268  // Body:
269 
270  for(int i=0; i<d(); ++i)
271  {
272  components[i] = xvalue;
273  }
274 
275  // Postconditions:
276 
277  ensure_for_all(i, 0, d(), components[i] == xvalue);
278 
279  // Exit:
280 
281 }
282 
283 
285 template <typename T>
286 void
288 multiply(const T& xscalar, general_matrix_3x1<T>& xresult) const
289 {
290  // Preconditions:
291 
292  // Body:
293 
294  for(int i=0; i<d(); ++i)
295  {
296  xresult.components[i] = xscalar*components[i];
297  }
298 
299  // Postconditions:
300 
301  //@issue $$ISSUE: We can't make the following comparison because of
302  // floating point roundoff.
303  //ensure_for_all(i, 0, d(), xresult.components[i] == xscalar*components[i]);
304 
305  // Exit:
306 
307 }
308 
310 template <typename T>
313 multiply(const T& xscalar) const
314 {
315  // Preconditions:
316 
317  // Body:
318 
319  general_matrix_3x1<T> result;
320  multiply(xscalar, result);
321 
322  // Postconditions:
323 
324  // Exit:
325 
326  return result;
327 }
328 
329 
331 template <typename T>
332 void
335  general_matrix_3x2<T>& xresult) const
336 {
337  // Preconditions:
338 
339  // Body:
340 
341  // Multiply [A][B] where [A] = *this & [B] = xother.
342 
343  const general_matrix_3x1<T>& lm = *this;
344 
345  int nra = number_of_rows();
346  int nca = number_of_columns();
347  int ncb = xother.number_of_columns();
348 
349  for(int i=0; i<nra; ++i)
350  {
351  for(int j=0; j<ncb; ++j)
352  {
353  T sum = T(0);
354 
355  for(int k=0; k<nca; ++k)
356  {
357  sum += lm[i][k]*xother[k][j];
358  }
359 
360  xresult[i][j] = sum;
361  }
362  }
363 
364  // Postconditions:
365 
366  // Exit:
367 
368 }
369 
371 template <typename T>
374 multiply(const general_matrix_1x2<T>& xother) const
375 {
376  // Preconditions:
377 
378  // Body:
379 
380  general_matrix_3x2<T> result;
381  multiply(xother, result);
382 
383  // Postconditions:
384 
385  // Exit:
386 
387  return result;
388 }
389 
391 template <typename T>
392 void
395  general_matrix_3x3<T>& xresult) const
396 {
397  // Preconditions:
398 
399  // Body:
400 
401  // Multiply [A][B] where [A] = *this & [B] = xother.
402 
403  const general_matrix_3x1<T>& lm = *this;
404 
405  int nra = number_of_rows();
406  int nca = number_of_columns();
407  int ncb = xother.number_of_columns();
408 
409  for(int i=0; i<nra; ++i)
410  {
411  for(int j=0; j<ncb; ++j)
412  {
413  T sum = T(0);
414 
415  for(int k=0; k<nca; ++k)
416  {
417  sum += lm[i][k]*xother[k][j];
418  }
419 
420  xresult[i][j] = sum;
421  }
422  }
423 
424  // Postconditions:
425 
426  // Exit:
427 
428 }
429 
431 template <typename T>
434 multiply(const general_matrix_1x3<T>& xother) const
435 {
436  // Preconditions:
437 
438  // Body:
439 
440  general_matrix_3x3<T> result;
441  multiply(xother, result);
442 
443  // Postconditions:
444 
445  // Exit:
446 
447  return result;
448 }
449 
451 template <typename T>
452 void
455 {
456  // Preconditions:
457 
458  // Body:
459 
460  const general_matrix_3x1<T>& lm = *this;
461 
462  xresult[0][0] = lm[0][0];
463  xresult[0][1] = lm[1][0];
464  xresult[0][2] = lm[2][0];
465 
466  // Postconditions:
467 
468  // Exit:
469 
470 }
471 
473 template <typename T>
476 transpose() const
477 {
478  // Preconditions:
479 
480  // Body:
481 
482  general_matrix_1x3<T> result;
483  transpose(result);
484 
485  // Postconditions:
486 
487  // Exit:
488 
489  return result;
490 }
491 
492 
493 // =============================================================================
494 // NON-MEMBER FUNCTIONS
495 // =============================================================================
496 
497 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
498 
500 template <typename T>
501 std::ostream& operator<<(std::ostream& xos, const general_matrix_3x1<T>& xm)
502 {
503  // Preconditions:
504 
505  // Body:
506 
507  int nrows = xm.number_of_rows();
508  int ncols = xm.number_of_columns();
509 
510  for(int i=0; i<nrows; ++i)
511  {
512  for(int j=0; j<ncols; ++j)
513  {
514  xos << " " << xm[i][j];
515  }
516  xos << std::endl;
517  }
518 
519  // Postconditions:
520 
521  // Exit:
522 
523  return xos;
524 }
525 
526 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
527 
528 //==============================================================================
529 //==============================================================================
530 
531 } // namespace fiber_bundle
532 
533 #endif // GENERAL_MATRIX_3X1_IMPL_H
534 
int row_index(int xrow) const
Index for row xrow in the linear storage array.
static int number_of_columns()
The number of columns.
void assign(const T &xvalue)
Assign all elements of this matrix to the value xvalue.
General matrix with 3 rows and 2 columns.
General matrix with 1 row and 2 columns.
static int number_of_rows()
The number of rows.
void multiply(const T &xscalar, general_matrix_3x1< T > &xresult) const
This matrix multiplied by a scalar (pre-allocated).
static int number_of_columns()
The number of columns.
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_columns()
The number of columns.
Row dofs type for class jcb_e13.
Definition: jcb_e13.h:57
General matrix with 3 rows and 1 column.
static int number_of_rows()
The number of rows.
General matrix with 3 rows and 3 columns.
General matrix with 1 row and 3 columns.
Namespace for the sheaves component of the sheaf system.
T components[3]
Linear storage array.
static int d()
Dimension of the underlying elements.
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
general_matrix_1x3< T > transpose() const
The transpose of the matrix (auto-allocated).
Namespace for the fiber_bundles component of the sheaf system.