SheafSystem  0.0.0.0
general_matrix_1x2.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_1x2.
19 
20 
21 #ifndef GENERAL_MATRIX_1X2_IMPL_H
22 #define GENERAL_MATRIX_1X2_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 1;
89 }
90 
92 template <typename T>
93 int
96 {
97  // Preconditions:
98 
99  // Body:
100 
101  // Postconditions:
102 
103  // Exit:
104 
105  return 2;
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_1x2<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_1x2<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_1x2<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_1x2<T> result;
299  multiply(xscalar, result);
300 
301  // Postconditions:
302 
303  // Exit:
304 
305  return result;
306 }
307 
309 template <typename T>
310 void
312 multiply(const general_matrix_2x1<T>& xother, T& xresult) const
313 {
314  // Preconditions:
315 
316  // Body:
317 
318  // Multiply [A][B] where [A] = *this & [B] = xother.
319 
320  const general_matrix_1x2<T>& lm = *this;
321 
322  T a00 = lm[0][0];
323  T a01 = lm[0][1];
324 
325  T b00 = xother[0][0];
326  T b10 = xother[1][0];
327 
328  xresult = a00*b00 + a01*b10;
329 
330  // Postconditions:
331 
332  // Exit:
333 
334 }
335 
337 template <typename T>
338 T
340 multiply(const general_matrix_2x1<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_1x2<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_2x2<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_1x2<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_2x3<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 
477 template <typename T>
478 void
481 {
482  // Preconditions:
483 
484  // Body:
485 
486  const general_matrix_1x2<T>& lm = *this;
487 
488  xresult[0][0] = lm[0][0];
489  xresult[1][0] = lm[0][1];
490 
491  // Postconditions:
492 
493  // Exit:
494 
495 }
496 
498 template <typename T>
501 transpose() const
502 {
503  // Preconditions:
504 
505  // Body:
506 
507  general_matrix_2x1<T> result;
508  transpose(result);
509 
510  // Postconditions:
511 
512  // Exit:
513 
514  return result;
515 }
516 
517 
518 
519 // =============================================================================
520 // NON-MEMBER FUNCTIONS
521 // =============================================================================
522 
523 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
524 
526 template <typename T>
527 std::ostream& operator<<(std::ostream& xos, const general_matrix_1x2<T>& xm)
528 {
529  // Preconditions:
530 
531  // Body:
532 
533  int nrows = xm.number_of_rows();
534  int ncols = xm.number_of_columns();
535 
536  for(int i=0; i<nrows; ++i)
537  {
538  for(int j=0; j<ncols; ++j)
539  {
540  xos << " " << xm[i][j];
541  }
542  xos << std::endl;
543  }
544 
545  // Postconditions:
546 
547  // Exit:
548 
549  return xos;
550 }
551 
552 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
553 
554 //==============================================================================
555 //==============================================================================
556 
557 } // namespace fiber_bundle
558 
559 #endif // GENERAL_MATRIX_1X2_IMPL_H
560 
T components[2]
Linear storage array.
general_matrix_2x1< T > transpose() const
The transpose of the matrix (auto-allocated).
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.
static int number_of_columns()
The number of columns.
General matrix with 1 row and 2 columns.
General matrix with 2 rows and 2 columns.
void assign(const T &xvalue)
Assign all elements of this matrix to the value xvalue.
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.
static int d()
Dimension of the underlying elements.
void multiply(const T &xscalar, general_matrix_1x2< T > &xresult) const
This matrix multiplied by a scalar (pre-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.
static int number_of_columns()
The number of columns.
int row_index(int xrow) const
Index for row xrow in the linear storage array.
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.