SheafSystem  0.0.0.0
general_matrix_2x3.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_2x3.
19 
20 
21 #ifndef GENERAL_MATRIX_2X3_IMPL_H
22 #define GENERAL_MATRIX_2X3_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_1x3_H
37 #include "SheafSystem/general_matrix_1x3.h"
38 #endif
39 
40 #ifndef GENERAL_MATRIX_2X1_H
41 #include "SheafSystem/general_matrix_2x1.h"
42 #endif
43 
44 #ifndef GENERAL_MATRIX_2X2_H
45 #include "SheafSystem/general_matrix_2x2.h"
46 #endif
47 
48 #ifndef GENERAL_MATRIX_2x3_H
49 #include "SheafSystem/general_matrix_2x3.h"
50 #endif
51 
52 #ifndef GENERAL_MATRIX_3X1_H
53 #include "SheafSystem/general_matrix_3x1.h"
54 #endif
55 
56 #ifndef GENERAL_MATRIX_3X2_H
57 #include "SheafSystem/general_matrix_3x2.h"
58 #endif
59 
60 #ifndef GENERAL_MATRIX_3X3_H
61 #include "SheafSystem/general_matrix_3x3.h"
62 #endif
63 
64 #ifndef STD_SSTREAM_H
65 #include "SheafSystem/std_sstream.h"
66 #endif
67 
68 #ifndef STD_CMATH_H
69 #include "SheafSystem/std_cmath.h"
70 #endif
71 
72 namespace fiber_bundle
73 {
74 
75 using namespace sheaf;
76 
77 //==============================================================================
78 // MEMBER FUNCTIONS
79 //==============================================================================
80 
81 
83 template <typename T>
84 int
87 {
88  // Preconditions:
89 
90  // Body:
91 
92  // Postconditions:
93 
94  // Exit:
95 
96  return 2;
97 }
98 
100 template <typename T>
101 int
104 {
105  // Preconditions:
106 
107  // Body:
108 
109  // Postconditions:
110 
111  // Exit:
112 
113  return 3;
114 }
115 
117 template <typename T>
118 int
120 d()
121 {
122  // Preconditions:
123 
124  // Body:
125 
126  // Postconditions:
127 
128  // Exit:
129 
130  return 6;
131 }
132 
134 template <typename T>
135 T*
137 operator[](int nrow)
138 {
139  // Preconditions:
140 
141  require(nrow >= 0 && nrow < number_of_rows());
142 
143  // Body:
144 
145  T* result = &components[row_index(nrow)];
146 
147  // Postconditions:
148 
149  ensure(result != 0);
150 
151  // Exit:
152 
153  return result;
154 }
155 
156 template <typename T>
157 const T*
159 operator[](int nrow) const
160 {
161  // Preconditions:
162 
163  require(nrow >= 0 && nrow < number_of_rows());
164 
165  // Body:
166 
167  const T* result = &components[row_index(nrow)];
168 
169  // Postconditions:
170 
171  ensure(result != 0);
172 
173  // Exit:
174 
175  return result;
176 }
177 
179 template <typename T>
181 operator T* ()
182 {
183  // Preconditions:
184 
185  // Body:
186 
187  //cout << "general_matrix_2x3<T>::operator T* () " << std::endl;
188 
189  T* result = components;
190 
191  // Postconditions:
192 
193  ensure(result != 0);
194 
195  // Exit:
196 
197  return result;
198 }
199 
201 template <typename T>
203 operator const T* () const
204 {
205  // Preconditions:
206 
207  // Body:
208 
209  //cout << "general_matrix_2x3<T>::operator const T* () const " << std::endl;
210 
211  const T* result = components;
212 
213  // Postconditions:
214 
215  ensure(result != 0);
216 
217  // Exit:
218 
219  return result;
220 }
221 
223 template <typename T>
224 int
226 row_index(int xrow) const
227 {
228 
229  // Preconditions:
230 
231  require(xrow >= 0 && xrow < number_of_rows());
232 
233  // Body:
234 
235  int result = number_of_columns()*xrow;
236 
237  // Postconditions:
238 
239  ensure(result == number_of_columns()*xrow);
240 
241  // Exit:
242 
243  return result;
244 }
245 
247 template <typename T>
250 row(int xrow) const
251 {
252  // Preconditions:
253 
254  require(xrow >= 0 && xrow < number_of_rows());
255 
256  // Body:
257 
258  general_matrix_1x3<T> result;
259 
260  int lindex = row_index(xrow);
261  result.components[0] = components[lindex++];
262  result.components[1] = components[lindex++];
263  result.components[2] = components[lindex];
264 
265  // Postconditions:
266 
267  // Exit:
268 
269  return result;
270 
271 }
272 
274 template <typename T>
277 column(int xcolumn) const
278 {
279  // Preconditions:
280 
281  require(xcolumn >= 0 && xcolumn < number_of_columns());
282 
283  // Body:
284 
285  general_matrix_2x1<T> result;
286 
287  int lindex = xcolumn;
288  result.components[0] = components[lindex];
289  lindex += 3; //+= number_of_columns();
290  result.components[1] = components[lindex];
291 
292  // Postconditions:
293 
294  // Exit:
295 
296  return result;
297 }
298 
300 template <typename T>
301 void
303 assign(const T& xvalue)
304 {
305  // Preconditions:
306 
307  // Body:
308 
309  for(int i=0; i<d(); ++i)
310  {
311  components[i] = xvalue;
312  }
313 
314  // Postconditions:
315 
316  ensure_for_all(i, 0, d(), components[i] == xvalue);
317 
318  // Exit:
319 
320  //return result;
321 }
322 
323 
325 template <typename T>
326 void
328 multiply(const T& xscalar, general_matrix_2x3<T>& xresult) const
329 {
330  // Preconditions:
331 
332  // Body:
333 
334  for(int i=0; i<d(); ++i)
335  {
336  xresult.components[i] = xscalar*components[i];
337  }
338 
339  // Postconditions:
340 
341  //@issue $$ISSUE: We can't make the following comparison because of
342  // floating point roundoff.
343  //ensure_for_all(i, 0, d(), xresult.components[i] == xscalar*components[i]);
344 
345  // Exit:
346 
347 }
348 
350 template <typename T>
353 multiply(const T& xscalar) const
354 {
355  // Preconditions:
356 
357  // Body:
358 
359  general_matrix_2x3<T> result;
360  multiply(xscalar, result);
361 
362  // Postconditions:
363 
364  // Exit:
365 
366  return result;
367 }
368 
370 template <typename T>
371 void
374  general_matrix_2x3<T>& xresult) const
375 {
376  // Preconditions:
377 
378  // Body:
379 
380  // Multiply [A][B] where [A] = *this & [B] = xother.
381 
382  const general_matrix_2x3<T>& lm = *this;
383 
384  int nra = number_of_rows();
385  int nca = number_of_columns();
386  int ncb = xother.number_of_columns();
387 
388  for(int i=0; i<nra; ++i)
389  {
390  for(int j=0; j<ncb; ++j)
391  {
392  T sum = T(0);
393 
394  for(int k=0; k<nca; ++k)
395  {
396  sum += lm[i][k]*xother[k][j];
397  }
398 
399  xresult[i][j] = sum;
400  }
401  }
402 
403  // Postconditions:
404 
405  // Exit:
406 
407 }
408 
410 template <typename T>
413 multiply(const general_matrix_3x3<T>& xother) const
414 {
415  // Preconditions:
416 
417  // Body:
418 
419  general_matrix_2x3<T> result;
420  multiply(xother, result);
421 
422  // Postconditions:
423 
424  // Exit:
425 
426  return result;
427 }
428 
430 template <typename T>
431 void
434  general_matrix_2x2<T>& xresult) const
435 {
436  // Preconditions:
437 
438  // Body:
439 
440  // Multiply [A][B] where [A] = *this & [B] = xother.
441 
442  const general_matrix_2x3<T>& lm = *this;
443 
444  int nra = number_of_rows();
445  int nca = number_of_columns();
446  int ncb = xother.number_of_columns();
447 
448  for(int i=0; i<nra; ++i)
449  {
450  for(int j=0; j<ncb; ++j)
451  {
452  T sum = T(0);
453 
454  for(int k=0; k<nca; ++k)
455  {
456  sum += lm[i][k]*xother[k][j];
457  }
458 
459  xresult[i][j] = sum;
460  }
461  }
462 
463  // Postconditions:
464 
465  // Exit:
466 
467 }
468 
470 template <typename T>
473 multiply(const general_matrix_3x2<T>& xother) const
474 {
475  // Preconditions:
476 
477  // Body:
478 
479  general_matrix_2x2<T> result;
480  multiply(xother, result);
481 
482  // Postconditions:
483 
484  // Exit:
485 
486  return result;
487 }
488 
490 template <typename T>
491 void
494  general_matrix_2x1<T>& xresult) const
495 {
496  // Preconditions:
497 
498  // Body:
499 
500  // Multiply [A][B] where [A] = *this & [B] = xother.
501 
502  const general_matrix_2x3<T>& lm = *this;
503 
504  int nra = number_of_rows();
505  int nca = number_of_columns();
506  int ncb = xother.number_of_columns();
507 
508  for(int i=0; i<nra; ++i)
509  {
510  for(int j=0; j<ncb; ++j)
511  {
512  T sum = T(0);
513 
514  for(int k=0; k<nca; ++k)
515  {
516  sum += lm[i][k]*xother[k][j];
517  }
518 
519  xresult[i][j] = sum;
520  }
521  }
522 
523  // Postconditions:
524 
525  // Exit:
526 
527 }
528 
530 template <typename T>
533 multiply(const general_matrix_3x1<T>& xother) const
534 {
535  // Preconditions:
536 
537  // Body:
538 
539  general_matrix_2x1<T> result;
540  multiply(xother, result);
541 
542  // Postconditions:
543 
544  // Exit:
545 
546  return result;
547 }
548 
550 template <typename T>
551 void
554 {
555  // Preconditions:
556 
557  // Body:
558 
559  const general_matrix_2x3<T>& lm = *this;
560 
561  int nrows = xresult.number_of_rows();
562  int ncols = xresult.number_of_columns();
563 
564  for(int i=0; i<nrows; ++i)
565  {
566  for(int j=0; j<ncols; ++j)
567  {
568  xresult[i][j] = lm[j][i];
569  }
570  }
571 
572  // Postconditions:
573 
574  // Exit:
575 
576 }
577 
579 template <typename T>
582 transpose() const
583 {
584  // Preconditions:
585 
586  // Body:
587 
588  general_matrix_3x2<T> result;
589  transpose(result);
590 
591  // Postconditions:
592 
593  // Exit:
594 
595  return result;
596 }
597 
598 
599 // =============================================================================
600 // NON-MEMBER FUNCTIONS
601 // =============================================================================
602 
603 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
604 
606 template <typename T>
607 std::ostream& operator<<(std::ostream& xos, const general_matrix_2x3<T>& xm)
608 {
609  // Preconditions:
610 
611  // Body:
612 
613  int nrows = xm.number_of_rows();
614  int ncols = xm.number_of_columns();
615 
616  for(int i=0; i<nrows; ++i)
617  {
618  for(int j=0; j<ncols; ++j)
619  {
620  xos << " " << xm[i][j];
621  }
622  xos << std::endl;
623  }
624 
625  // Postconditions:
626 
627  // Exit:
628 
629  return xos;
630 }
631 
632 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
633 
634 //==============================================================================
635 //==============================================================================
636 
637 } // namespace fiber_bundle
638 
639 #endif // GENERAL_MATRIX_2X3_IMPL_H
640 
static int number_of_columns()
The number of columns.
static int number_of_columns()
The number of columns.
T components[6]
Linear storage array.
general_matrix_2x1< T > column(int xcolumn) const
A 2x1 matrix containing the elements or column xcolumn.
static int number_of_rows()
The number of rows.
General matrix with 3 rows and 2 columns.
General matrix with 2 rows and 2 columns.
T components[2]
Linear storage array.
static int number_of_rows()
The number of rows.
void assign(const T &xvalue)
The adjoint of the matrix (pre-allocated).
static int d()
Dimension of the underlying elements.
general_matrix_3x2< T > transpose() const
The transpose of the matrix (auto-allocated).
General matrix with 3 rows and 1 column.
General matrix with 2 rows and 1 column.
void multiply(const T &xscalar, general_matrix_2x3< T > &xresult) const
This matrix multiplied by a scalar (pre-allocated).
int row_index(int xrow) const
Index for row xrow in the linear storage array.
T components[3]
Linear storage array.
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 3 columns.
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.
static int number_of_columns()
The number of 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.
general_matrix_1x3< T > row(int xrow) const
A 1x3 matrix containing the elements or row xrow.