00001 /*
00002 File: Mat2.cc
00003
00004 Function: Implements Mat2.h
00005
00006 Author(s): Andrew Willmott
00007
00008 Copyright: (c) 1995-2000, Andrew Willmott
00009
00010 Notes:
00011
00012 */
00013
00014 #include "vl/Mat2.h"
00015 #include <ctype.h>
00016 #include <iomanip.h>
00017
00018
00019 Bool TMat2::operator == (const TMat2 &m) const
00020 {
00021 return(row[0] == m[0] && row[1] == m[1]);
00022 }
00023
00024 Bool TMat2::operator != (const TMat2 &m) const
00025 {
00026 return(row[0] != m[0] || row[1] != m[1]);
00027 }
00028
00029
00030 TMReal det(const TMat2 &m)
00031 {
00032 return(dot(m[0], cross(m[1])));
00033 }
00034
00035 TMat2 inv(const TMat2 &m)
00036 {
00037 TMReal mDet;
00038 TMat2 result;
00039
00040 result[0][0] = m[1][1]; result[0][1] = -m[0][1];
00041 result[1][0] = -m[1][0]; result[1][1] = m[0][0];
00042
00043 mDet = m[0][0] * result[0][0] + m[0][1] * result[1][0];
00044 Assert(mDet != 0.0, "(Mat2::inv) matrix is non-singular");
00045 result /= mDet;
00046
00047 return(result);
00048 }
00049
00050 TMat2 oprod(const TMVec2 &a, const TMVec2 &b)
00051 // returns outerproduct of a and b: a * trans(b)
00052 {
00053 TMat2 result;
00054
00055 result[0] = a[0] * b;
00056 result[1] = a[1] * b;
00057
00058 return(result);
00059 }
00060
00061 ostream &operator << (ostream &s, const TMat2 &m)
00062 {
00063 Int w = s.width();
00064
00065 return(s << '[' << m[0] << endl << setw(w) << m[1] << ']' << endl);
00066 }
00067
00068 istream &operator >> (istream &s, TMat2 &m)
00069 {
00070 TMat2 result;
00071 Char c;
00072
00073 // Expected format: [[1 2] [3 4]]
00074 // Each vector is a row of the row matrix.
00075
00076 while (s >> c && isspace(c)) // ignore leading white space
00077 ;
00078
00079 if (c == '[')
00080 {
00081 s >> result[0] >> result[1];
00082
00083 if (!s)
00084 {
00085 cerr << "Expected number while reading matrix\n";
00086 return(s);
00087 }
00088
00089 while (s >> c && isspace(c))
00090 ;
00091
00092 if (c != ']')
00093 {
00094 s.clear(ios::failbit);
00095 cerr << "Expected ']' while reading matrix\n";
00096 return(s);
00097 }
00098 }
00099 else
00100 {
00101 s.clear(ios::failbit);
00102 cerr << "Expected '[' while reading matrix\n";
00103 return(s);
00104 }
00105
00106 m = result;
00107 return(s);
00108 }
00109
00110
00111 TMat2 &TMat2::MakeRot(Real theta)
00112 {
00113 TMReal c, s;
00114
00115 SetReal(s, sin(theta));
00116 SetReal(c, cos(theta));
00117
00118 #ifdef VL_ROW_ORIENT
00119 row[0][0] = c; row[0][1] = s;
00120 row[1][0] = -s; row[1][1] = c;
00121 #else
00122 row[0][0] = c; row[0][1] = -s;
00123 row[1][0] = s; row[1][1] = c;
00124 #endif
00125
00126 return(SELF);
00127 }
00128
00129 TMat2 &TMat2::MakeScale(const TMVec2 &s)
00130 {
00131 row[0][0] = s[0]; row[0][1] = vl_0;
00132 row[1][0] = vl_0; row[1][1] = s[1];
00133
00134 return(SELF);
00135 }
00136