SheafSystem  0.0.0.0
general_matrix_1x3.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_1x3.
19 
20 
21 #ifndef GENERAL_MATRIX_1X3_IMPL_H
22 #define GENERAL_MATRIX_1X3_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 int
78 {
79  // Preconditions:
80 
81  // Body:
82 
83  // Postconditions:
84 
85  // Exit:
86 
87  return 1;
88 }
89 
91 template <typename T>
92 int
95 {
96  // Preconditions:
97 
98  // Body:
99 
100  // Postconditions:
101 
102  // Exit:
103 
104  return 3;
105 }
106 
108 template <typename T>
109 int
111 d()
112 {
113  // Preconditions:
114 
115  // Body:
116 
117  // Postconditions:
118 
119  // Exit:
120 
121  return 3;
122 }
123 
125 template <typename T>
126 T*
128 operator[](int nrow)
129 {
130  // Preconditions:
131 
132  require(nrow >= 0 && nrow < number_of_rows());
133 
134  // Body:
135 
136  T* result = &components[row_index(nrow)];
137 
138  // Postconditions:
139 
140  ensure(result != 0);
141 
142  // Exit:
143 
144  return result;
145 }
146 
147 template <typename T>
148 const T*
150 operator[](int nrow) const
151 {
152  // Preconditions:
153 
154  require(nrow >= 0 && nrow < number_of_rows());
155 
156  // Body:
157 
158  const T* result = &components[row_index(nrow)];
159 
160  // Postconditions:
161 
162  ensure(result != 0);
163 
164  // Exit:
165 
166  return result;
167 }
168 
170 template <typename T>
172 operator T* ()
173 {
174  // Preconditions:
175 
176  // Body:
177 
178  //cout << "general_matrix_1x3<T>::operator T* () " << std::endl;
179 
180  T* result = components;
181 
182  // Postconditions:
183 
184  ensure(result != 0);
185 
186  // Exit:
187 
188  return result;
189 }
190 
192 template <typename T>
194 operator const T* () const
195 {
196  // Preconditions:
197 
198  // Body:
199 
200  //cout << "general_matrix_1x3<T>::operator const T* () const " << std::endl;
201 
202  const T* result = components;
203 
204  // Postconditions:
205 
206  ensure(result != 0);
207 
208  // Exit:
209 
210  return result;
211 }
212 
214 template <typename T>
215 int
217 row_index(int xrow) const
218 {
219 
220  // Preconditions:
221 
222  require(xrow >= 0 && xrow < number_of_rows());
223 
224  // Body:
225 
226  int result = number_of_columns()*xrow;
227 
228  // Postconditions:
229 
230  ensure(result == number_of_columns()*xrow);
231 
232  // Exit:
233 
234  return result;
235 }
236 
238 template <typename T>
239 void
241 assign(const T& xvalue)
242 {
243  // Preconditions:
244 
245  // Body:
246 
247  for(int i=0; i<d(); ++i)
248  {
249  components[i] = xvalue;
250  }
251 
252  // Postconditions:
253 
254  ensure_for_all(i, 0, d(), components[i] == xvalue);
255 
256  // Exit:
257 
258 }
259 
260 
262 template <typename T>
263 void
265 multiply(const T& xscalar, general_matrix_1x3<T>& xresult) const
266 {
267  // Preconditions:
268 
269  // Body:
270 
271  for(int i=0; i<d(); ++i)
272  {
273  xresult.components[i] = xscalar*components[i];
274  }
275 
276  // Postconditions:
277 
278  //@issue $$ISSUE: We can't make the following comparison because of
279  // floating point roundoff.
280  //ensure_for_all(i, 0, d(), xresult.components[i] == xscalar*components[i]);
281 
282  // Exit:
283 
284 }
285 
287 template <typename T>
290 multiply(const T& xscalar) const
291 {
292  // Preconditions:
293 
294  // Body:
295 
296  general_matrix_1x3<T> result;
297  multiply(xscalar, result);
298 
299  // Postconditions:
300 
301  // Exit:
302 
303  return result;
304 }
305 
307 template <typename T>
308 void
310 multiply(const general_matrix_3x1<T>& xother, T& xresult) const
311 {
312  // Preconditions:
313 
314  // Body:
315 
316  // Multiply [A][B] where [A] = *this & [B] = xother.
317 
318  const general_matrix_1x3<T>& lm = *this;
319 
320  T a00 = lm[0][0];
321  T a01 = lm[0][1];
322  T a02 = lm[0][2];
323 
324  T b00 = xother[0][0];
325  T b10 = xother[1][0];
326  T b20 = xother[2][0];
327 
328  xresult = a00*b00 + a01*b10 + a02*b20;
329 
330  // Postconditions:
331 
332  // Exit:
333 
334 }
335 
337 template <typename T>
338 T
340 multiply(const general_matrix_3x1<T>& xother) const
341 {
342  // Preconditions:
343 
344  // Body:
345 
346  T result;
347  multiply(xother, result);
348 
349  // Postconditions:
350 
351  // Exit:
352 
353  return result;
354 }
355 
357 template <typename T>
358 void
361  general_matrix_1x2<T>& xresult) const
362 {
363  // Preconditions:
364 
365  // Body:
366 
367  // Multiply [A][B] where [A] = *this & [B] = xother.
368 
369  const general_matrix_1x3<T>& lm = *this;
370 
371  int nra = number_of_rows();
372  int nca = number_of_columns();
373  int ncb = xother.number_of_columns();
374 
375  for(int i=0; i<nra; ++i)
376  {
377  for(int j=0; j<ncb; ++j)
378  {
379  T sum = T(0);
380 
381  for(int k=0; k<nca; ++k)
382  {
383  sum += lm[i][k]*xother[k][j];
384  }
385 
386  xresult[i][j] = sum;
387  }
388  }
389 
390  // Postconditions:
391 
392  // Exit:
393 
394 }
395 
397 template <typename T>
400 multiply(const general_matrix_3x2<T>& xother) const
401 {
402  // Preconditions:
403 
404  // Body:
405 
406  general_matrix_1x2<T> result;
407  multiply(xother, result);
408 
409  // Postconditions:
410 
411  // Exit:
412 
413  return result;
414 }
415 
417 template <typename T>
418 void
421  general_matrix_1x3<T>& xresult) const
422 {
423  // Preconditions:
424 
425  // Body:
426 
427  // Multiply [A][B] where [A] = *this & [B] = xother.
428 
429  const general_matrix_1x3<T>& lm = *this;
430 
431  int nra = number_of_rows();
432  int nca = number_of_columns();
433  int ncb = xother.number_of_columns();
434 
435  for(int i=0; i<nra; ++i)
436  {
437  for(int j=0; j<ncb; ++j)
438  {
439  T sum = T(0);
440 
441  for(int k=0; k<nca; ++k)
442  {
443  sum += lm[i][k]*xother[k][j];
444  }
445 
446  xresult[i][j] = sum;
447  }
448  }
449 
450  // Postconditions:
451 
452  // Exit:
453 
454 }
455 
457 template <typename T>
460 multiply(const general_matrix_3x3<T>& xother) const
461 {
462  // Preconditions:
463 
464  // Body:
465 
466  general_matrix_1x3<T> result;
467  multiply(xother, result);
468 
469  // Postconditions:
470 
471  // Exit:
472 
473  return result;
474 }
475 
476 
478 template <typename T>
479 void
482 {
483  // Preconditions:
484 
485  // Body:
486 
487  const general_matrix_1x3<T>& lm = *this;
488 
489  xresult[0][0] = lm[0][0];
490  xresult[1][0] = lm[0][1];
491  xresult[2][0] = lm[0][2];
492 
493  // Postconditions:
494 
495  // Exit:
496 
497 }
498 
500 template <typename T>
503 transpose() const
504 {
505  // Preconditions:
506 
507  // Body:
508 
509  general_matrix_3x1<T> result;
510  transpose(result);
511 
512  // Postconditions:
513 
514  // Exit:
515 
516  return result;
517 }
518 
519 
520 // =============================================================================
521 // NON-MEMBER FUNCTIONS
522 // =============================================================================
523 
524 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
525 
527 template <typename T>
528 std::ostream& operator<<(std::ostream& xos, const general_matrix_1x3<T>& xm)
529 {
530  // Preconditions:
531 
532  // Body:
533 
534  int nrows = xm.number_of_rows();
535  int ncols = xm.number_of_columns();
536 
537  for(int i=0; i<nrows; ++i)
538  {
539  for(int j=0; j<ncols; ++j)
540  {
541  xos << " " << xm[i][j];
542  }
543  xos << std::endl;
544  }
545 
546  // Postconditions:
547 
548  // Exit:
549 
550  return xos;
551 }
552 
553 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
554 
555 //==============================================================================
556 //==============================================================================
557 
558 } // namespace fiber_bundle
559 
560 #endif // GENERAL_MATRIX_1X3_IMPL_H
static int number_of_columns()
The number of columns.
int row_index(int xrow) const
Index for row xrow in the linear storage array.
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_1x3< 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]...
General matrix with 3 rows and 1 column.
void assign(const T &xvalue)
Assign all elements of this matrix to the value xvalue.
static int number_of_rows()
The number of rows.
T components[3]
Linear storage array.
static int number_of_columns()
The number of columns.
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.
general_matrix_3x1< T > transpose() const
The transpose of the matrix (auto-allocated).
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
Namespace for the fiber_bundles component of the sheaf system.