SheafSystem  0.0.0.0
antisymmetric_matrix_2x2.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 antisymmetric_matrix_2x2.
19 
20 
21 #ifndef ANTISYMMETRIC_MATRIX_2X2_IMPL_H
22 #define ANTISYMMETRIC_MATRIX_2X2_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 ANTISYMMETRIC_MATRIX_2X2_H
37 #include "SheafSystem/antisymmetric_matrix_2x2.h"
38 #endif
39 
40 #ifndef GENERAL_MATRIX_1X2_H
41 #include "SheafSystem/general_matrix_1x2.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 
74 template <typename T>
75 antisymmetric_matrix_2x2<T>::
76 operator at2_e2_row_dofs_type<T>& () const
77 {
78  // Preconditions:
79 
80  // Body:
81 
82  T* lcomps = const_cast<T*>(components);
83 
84  at2_e2_row_dofs_type<T>& result =
85  reinterpret_cast<at2_e2_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 2;
109 }
110 
112 template <typename T>
113 int
116 {
117  // Preconditions:
118 
119  // Body:
120 
121  // Postconditions:
122 
123  // Exit:
124 
125  return 2;
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 1;
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 << "antisymmetric_matrix_2x2<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 << "antisymmetric_matrix_2x2<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()-1);
244 
245  // Body:
246 
247  int nrows = number_of_rows();
248  int result = (xrow*(2*nrows-1-xrow))/2 - 1 - xrow;
249 
250  //int result = (xrow*(3-xrow))/2 - 1 - xrow;
251 
252  // Postconditions:
253 
254  ensure(result == (xrow*(2*number_of_rows()-1-xrow))/2 - 1 - xrow);
255 
256  // Exit:
257 
258  return result;
259 }
260 
262 template <typename T>
264 operator general_matrix_2x2<T> () const
265 {
266  // Preconditions:
267 
268  // Body:
269 
270  general_matrix_2x2<T> result;
271 
272  const antisymmetric_matrix_2x2<T>& lm = *this;
273 
274  result[0][0] = 0.0;
275  result[0][1] = lm[0][1];
276  result[1][0] = -lm[0][1];
277  result[1][1] = 0.0;
278 
279  // Postconditions:
280 
281  ensure(unexecutable("result == *this"));
282 
283  // Exit:
284 
285  return result;
286 }
287 
289 template <typename T>
292 row(int xrow) const
293 {
294  // Preconditions:
295 
296  require(xrow >= 0 && xrow < number_of_rows());
297 
298  // Body:
299 
300  general_matrix_1x2<T> result;
301 
302  // Just brute force it for the 2x2 case.
303 
304  if(xrow == 0)
305  {
306  result.components[0] = 0.0; // [0][0];
307  result.components[1] = components[0]; // [0][1];
308  }
309  else // xrow == 1
310  {
311  result.components[0] = -components[0]; // [1][0];
312  result.components[1] = 0.0; // [1][1];
313  }
314 
315  // Postconditions:
316 
317  // Exit:
318 
319  return result;
320 
321 }
322 
324 template <typename T>
327 column(int xcolumn) const
328 {
329  // Preconditions:
330 
331  require(xcolumn >= 0 && xcolumn < number_of_columns());
332 
333  // Body:
334 
335  general_matrix_2x1<T> result;
336 
337  // Just brute force it for the 2x2 case.
338 
339  if(xcolumn == 0)
340  {
341  result.components[0] = 0.0; // [0][0];
342  result.components[1] = -components[0]; // [1][0];
343  }
344  else // xcolumn == 1
345  {
346  result.components[0] = components[0]; // [0][1];
347  result.components[1] = 0.0; // [1][1];
348  }
349 
350  // Postconditions:
351 
352  // Exit:
353 
354  return result;
355 
356 }
357 
359 template <typename T>
360 void
363 {
364  // Preconditions:
365 
366  // Body:
367 
368  const general_matrix_2x2<T>& lm = *this;
369 
370  T a00 = lm[0][0];
371  T a01 = lm[0][1];
372  T a10 = lm[1][0];
373  T a11 = lm[1][1];
374 
375  // Adjoint is the transpose of the cofactor matrix.
376 
377  xresult[0][0] = a11;
378  xresult[0][1] = -a01;
379  xresult[1][0] = -a10;
380  xresult[1][1] = a00;
381 
382  // Postconditions:
383 
384  // Exit:
385 }
386 
387 
389 template <typename T>
390 void
393 {
394  // Preconditions:
395 
396  // Body:
397 
398  // Get the cofactor.
399 
400  // Note that the result for a 2x2 is also
401  // an antisymmetric matrix.
402 
403  //xresult[0][0] = 0.0;
404  xresult[0][1] = -components[0];
405  //xresult[1][0] = components[0];
406  //xresult[1][1] = 0.0;
407 
408  // Postconditions:
409 
410  // Exit:
411 }
412 
414 template <typename T>
417 adjoint() const
418 {
419  // Preconditions:
420 
421  // Body:
422 
424  adjoint(result);
425 
426  // Postconditions:
427 
428  // Exit:
429 
430  return result;
431 }
432 
434 template <typename T>
435 void
437 assign(const T& xvalue)
438 {
439  // Preconditions:
440 
441  // Body:
442 
443  components[0] = xvalue;
444 
445  // Postconditions:
446 
447  // Exit:
448 
449  //return result;
450 }
451 
453 template <typename T>
454 void
456 determinant(T& xresult) const
457 {
458  // Preconditions:
459 
460  // Body:
461 
462  T a01 = components[0];
463 
464  xresult = a01*a01;
465 
466  // Postconditions:
467 
468  // Exit:
469 
470 }
471 
473 template <typename T>
474 T
476 determinant() const
477 {
478  // Preconditions:
479 
480  // Body:
481 
482  T result;
483 
484  determinant(result);
485 
486  // Postconditions:
487 
488  // Exit:
489 
490  return result;
491 }
492 
493 
495 template <typename T>
496 void
499 {
500  // Preconditions:
501 
502  // Body:
503 
504  const antisymmetric_matrix_2x2<T>& lm = *this;
505 
506  //double a00 = 0.0;
507  double a01 = lm[0][1];
508  //double a10 = -a01;
509  //double a11 = 0.0;
510 
511  //double determinant = a01*a01;
512  //double determinant = a00*a11 - a01*a10;;
513 
514  //if(determinant == 0.0)
515  if(a01 == 0.0)
516  {
517  post_fatal_error_message("No inverse; determinant is zero.");
518  }
519 
520  //xresult[0][0] = a11/determinant;
521  //xresult[0][1] = -a01/determinant;
522  //xresult[1][0] = -a10/determinant;
523  //xresult[1][1] = a00/determinant;
524 
525  xresult[0][1] = -1.0/a01;
526 
527  // Postconditions:
528 
529  // Exit:
530 }
531 
533 template <typename T>
536 inverse() const
537 {
538  // Preconditions:
539 
540  // Body:
541 
543  inverse(result);
544 
545  // Postconditions:
546 
547  // Exit:
548 
549  return result;
550 }
551 
553 template <typename T>
554 bool
557 {
558  // Preconditions:
559 
560  // Body:
561 
562  bool result = false;
563 
564  // Postconditions:
565 
566  ensure(result == false);
567 
568  // Exit:
569 
570  return result;
571 }
572 
573 
575 template <typename T>
576 void
578 multiply(const T& xscalar, antisymmetric_matrix_2x2<T>& xresult) const
579 {
580  // Preconditions:
581 
582  // Body:
583 
584  xresult.components[0] = xscalar*components[0];
585 
586  // Postconditions:
587 
588  //@issue $$ISSUE: We can't make the following comparison because of
589  // floating point roundoff.
590  //ensure_for_all(i, 0, d(), xresult.components[i] == xscalar*components[i]);
591 
592  // Exit:
593 
594 }
595 
597 template <typename T>
600 multiply(const T& xscalar) const
601 {
602  // Preconditions:
603 
604  // Body:
605 
607  multiply(xscalar, result);
608 
609  // Postconditions:
610 
611  // Exit:
612 
613  return result;
614 }
616 template <typename T>
617 void
620  general_matrix_2x1<T>& xresult) const
621 {
622  // Preconditions:
623 
624  // Body:
625 
626  // Multiply [A][B] where [A] = *this & [B] = xother.
627 
628  const antisymmetric_matrix_2x2<T>& lm = *this;
629 
630  //T a00 = 0.0;
631  T a01 = lm[0][1];
632  T a10 = -a01;
633  //T a11 = 0.0;
634 
635  T b00 = xother[0][0];
636  T b10 = xother[1][0];
637 
638  // xresult[0][0] = a00*b00 + a01*b10;
639  // xresult[1][0] = a10*b00 + a11*b10;
640 
641  xresult[0][0] = a01*b10;
642  xresult[1][0] = a10*b00;
643 
644  // Postconditions:
645 
646  // Exit:
647 
648 }
649 
651 template <typename T>
654 multiply(const general_matrix_2x1<T>& xother) const
655 {
656  // Preconditions:
657 
658  // Body:
659 
660  general_matrix_2x1<T> result;
661  multiply(xother, result);
662 
663  // Postconditions:
664 
665  // Exit:
666 
667  return result;
668 }
669 
671 template <typename T>
672 void
675  general_matrix_2x2<T>& xresult) const
676 {
677  // Preconditions:
678 
679  // Body:
680 
681  // Multiply [A][B] where [A] = *this & [B] = xother.
682 
683  const antisymmetric_matrix_2x2<T>& lm = *this;
684 
685  //T a00 = 0.0;
686  T a01 = lm[0][1];
687  T a10 = -a01;
688  //T a11 = 0.0;
689 
690  T b00 = xother[0][0];
691  T b01 = xother[0][1];
692  T b10 = xother[1][0];
693  T b11 = xother[1][1];
694 
695  // xresult[0][0] = a00*b00 + a01*b10;
696  // xresult[0][1] = a00*b01 + a01*b11;
697  // xresult[1][0] = a10*b00 + a11*b10;
698  // xresult[1][1] = a10*b01 + a11*b11;
699 
700  xresult[0][0] = a01*b10;
701  xresult[0][1] = a01*b11;
702 
703  xresult[1][0] = a10*b00;
704  xresult[1][1] = a10*b01;
705 
706 
707  // Postconditions:
708 
709  // Exit:
710 
711 }
712 
714 template <typename T>
717 multiply(const general_matrix_2x2<T>& xother) const
718 {
719  // Preconditions:
720 
721  // Body:
722 
723  general_matrix_2x2<T> result;
724  multiply(xother, result);
725 
726  // Postconditions:
727 
728  // Exit:
729 
730  return result;
731 }
732 
734 template <typename T>
735 void
738  general_matrix_2x3<T>& xresult) const
739 {
740  // Preconditions:
741 
742  // Body:
743 
744  // Multiply [A][B] where [A] = *this & [B] = xother.
745 
746  const antisymmetric_matrix_2x2<T>& lm = *this;
747 
748  //T a00 = 0.0;
749  T a01 = lm[0][1];
750  T a10 = -a01;
751  //T a11 = 0.0;
752 
753  T b00 = xother[0][0];
754  T b01 = xother[0][1];
755  T b02 = xother[0][2];
756 
757  T b10 = xother[1][0];
758  T b11 = xother[1][1];
759  T b12 = xother[1][2];
760 
761  // xresult[0][0] = a00*b00 + a01*b10;
762  // xresult[0][1] = a00*b01 + a01*b11;
763  // xresult[0][2] = a00*b02 + a01*b12;
764 
765  // xresult[1][0] = a10*b00 + a11*b10;
766  // xresult[1][1] = a10*b01 + a11*b11;
767  // xresult[1][2] = a10*b02 + a11*b12;
768 
769  xresult[0][0] = a01*b10;
770  xresult[0][1] = a01*b11;
771  xresult[0][2] = a01*b12;
772 
773  xresult[1][0] = a10*b00;
774  xresult[1][1] = a10*b01;
775  xresult[1][2] = a10*b02;
776 
777  // Postconditions:
778 
779  // Exit:
780 
781 }
782 
784 template <typename T>
787 multiply(const general_matrix_2x3<T>& xother) const
788 {
789  // Preconditions:
790 
791  // Body:
792 
793  general_matrix_2x3<T> result;
794  multiply(xother, result);
795 
796  // Postconditions:
797 
798  // Exit:
799 
800  return result;
801 }
802 
804 template <typename T>
805 void
808  general_matrix_2x2<T>& xresult) const
809 {
810  // Preconditions:
811 
812  // Body:
813 
814  // Multiply [A][B] where [A] = *this & [B] = xother.
815 
816  const antisymmetric_matrix_2x2<T>& lm = *this;
817 
818  //T a00 = 0.0;
819  T a01 = lm[0][1];
820  //T a10 = -a01;
821  //T a11 = 0.0;
822 
823  //T b00 = 0.0;
824  T b01 = xother[0][1];
825  //T b10 = -b01;
826  //T b11 = 0.0;
827 
828  // xresult[0][0] = a00*b00 + a01*b10;
829  // xresult[0][1] = a00*b01 + a01*b11;
830  // xresult[1][0] = a10*b00 + a11*b10;
831  // xresult[1][1] = a10*b01 + a11*b11;
832 
833  // In the 2x2 case the result is a diagonal matrix
834  // with identical elements.
835 
836  xresult[0][0] = -a01*b01;
837  xresult[0][1] = 0.0;
838  xresult[1][0] = 0.0;
839  xresult[1][1] = xresult[0][0];
840 
841  // Postconditions:
842 
843  // Exit:
844 
845 }
846 
848 template <typename T>
852 {
853  // Preconditions:
854 
855  // Body:
856 
857  general_matrix_2x2<T> result;
858  multiply(xother, result);
859 
860  // Postconditions:
861 
862  // Exit:
863 
864  return result;
865 }
866 
868 template <typename T>
869 void
871 trace(T& xresult) const
872 {
873  // Preconditions:
874 
875  // Body:
876 
877  xresult = T(0);
878 
879  // Postconditions:
880 
881  ensure(xresult == 0);
882 
883  // Exit:
884 }
885 
887 template <typename T>
888 T
890 trace() const
891 {
892  // Preconditions:
893 
894  // Body:
895 
896  T result;
897 
898  trace(result);
899 
900  // Postconditions:
901 
902  // Exit:
903 
904  return result;
905 }
906 
908 template <typename T>
909 void
912 {
913  // Preconditions:
914 
915  // Body:
916 
917  //const antisymmetric_matrix_2x2<T>& lm = *this;
918  //xresult[0][1] = -lm[0][1];
919 
920  xresult[0][1] = -components[0];
921 
922  // Postconditions:
923 
924  // Exit:
925 
926 }
927 
929 template <typename T>
932 transpose() const
933 {
934  // Preconditions:
935 
936  // Body:
937 
939  transpose(result);
940 
941  // Postconditions:
942 
943  // Exit:
944 
945  return result;
946 }
947 
948 // =============================================================================
949 // NON-MEMBER FUNCTIONS
950 // =============================================================================
951 
952 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
953 
955 template <typename T>
956 std::ostream& operator<<(std::ostream& xos, const antisymmetric_matrix_2x2<T>& xm)
957 {
958  // Preconditions:
959 
960  // Body:
961 
962  // Here we will actually print the elements on and below the diagonal
963  // even though we don't store them.
964 
965  xos << " " << 0.0 << " " << xm[0][1] << std::endl;
966  xos << " " << -xm[0][1] << " " << 0.0 << std::endl;
967 
968  // Postconditions:
969 
970  // Exit:
971 
972  return xos;
973 }
974 
975 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS
976 
977 //==============================================================================
978 //==============================================================================
979 
980 } // namespace fiber_bundle
981 
982 #endif // ANTISYMMETRIC_MATRIX_2X2_IMPL_H
983 
T determinant() const
The determinant of the matrix (auto-allocated).
static int number_of_columns()
The number of columns.
T components[2]
Linear storage array.
void multiply(const T &xscalar, antisymmetric_matrix_2x2< T > &xresult) const
This matrix multiplied by a scalar (pre-allocated).
static int d()
Dimension of the underlying elements.
general_matrix_1x2< T > row(int xrow) const
A 1x2 matrix containing the elements or row xrow.
General matrix with 1 row and 2 columns.
General matrix with 2 rows and 2 columns.
static int number_of_rows()
The number of rows.
T components[2]
Linear storage array.
general_matrix_2x2< T > adjoint() const
The adjoint of the matrix (auto-allocated).
antisymmetric_matrix_2x2< T > transpose() const
The transpose of the matrix (auto-allocated).
void trace(const S0 &x0, SR &xresult, bool xauto_access)
Definition: sec_st2.impl.h:99
General matrix with 2 rows and 1 column.
Antisymmetric matrix with 2 rows and 2 columns.
Row dofs type for class at2_e2.
Definition: at2_e2.h:51
general_matrix_2x1< T > column(int xcolumn) const
A 2x1 matrix containing the elements or column xcolumn.
antisymmetric_matrix_2x2< T > adjoint() const
The adjoint of the matrix (auto-allocated).
void determinant(const S0 &x0, SR &xresult, bool xauto_access)
Definition: sec_st2.impl.h:131
T * operator[](int xrow)
Pointer to the first element in row xrow of this matrix. Facilitates accessing elements via matrix[i]...
bool is_positive_definite() const
True if this matrix is positive definite. Always returns false for antisymmetric_matrices.
Namespace for the sheaves component of the sheaf system.
General matrix with 2 rows and 3 columns.
int row_index(int xrow) const
Index for row xrow in the linear storage array.
antisymmetric_matrix_2x2< T > inverse() const
The inverse of the matrix (auto-allocated).
void assign(const T &xscalar)
Assign all elements of this matrix to the value xvalue.
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.
SHEAF_DLL_SPEC void inverse(const gl2_lite &xlite, gl2_lite &xresult)
Inverse (pre-allocated version for volatile type).
Definition: gl2.cc:1484
T trace() const
The trace of the matrix (auto-allocated).