//$Header: /home/ben/Mapper/include/RCS/matrix2x2.h,v 6.4 2002/07/06 17:41:51 ben Exp $
#ifndef MATRIX2X2_H
#define MATRIX2X2_H
// Copyright Benedict Adamson 2002.
// This file is part of Mapper.
// Mapper is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// Mapper is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mapper; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <algorithm>
#include "vector2.h"
class matrix2x2
{
public: //static functions
static vector2 solve(
const matrix2x2 &M,
const vector2 &y
);
public: //constructors and destructors
inline matrix2x2(void);
inline matrix2x2(
const double k
);
inline matrix2x2(
const vector2 &r0,
const vector2 &r1
);
inline matrix2x2(
const double m00,
const double m01,
const double m10,
const double m11
);
inline matrix2x2(
const matrix2x2 &m
);
public: //attributes
inline vector2 &operator[](
const unsigned i
);
inline const vector2 &operator[](
const unsigned i
) const;
vector2 row0;
vector2 row1;
};
inline bool operator== (
const matrix2x2 &a,
const matrix2x2 &b
);
inline matrix2x2 operator- (
const matrix2x2 &a,
const matrix2x2 &b
);
inline matrix2x2 operator- (
const matrix2x2 &a
);
//matrix2x2 operator* (
// const matrix2x2 &a,
// const matrix2x2 &b
// );
inline matrix2x2 operator* (
const double k,
const matrix2x2 &x
);
inline matrix2x2 operator* (
const matrix2x2 &x,
const double k
)
{
return k*x;
}
inline vector2 operator* (
const matrix2x2 &M,
const vector2 &x
);
inline matrix2x2 operator/ (
const matrix2x2 &x,
const double k
);
inline matrix2x2 operator+ (
const matrix2x2 &x,
const matrix2x2 &y
);
inline matrix2x2 &operator +=(
matrix2x2 &x,
const matrix2x2 &y
);
inline matrix2x2 &operator -=(
matrix2x2 &x,
const matrix2x2 &y
);
inline matrix2x2 &operator *=(
matrix2x2 &x,
const double k
);
inline matrix2x2 &operator *=(
vector2 &x,
const matrix2x2 &M
);
inline double det(
const matrix2x2 &v
);
inline void transpose(
matrix2x2 &m
);
inline matrix2x2 transpose(
const matrix2x2 &m
);
inline matrix2x2::matrix2x2(void)
{
//Do nothing
}
inline matrix2x2::matrix2x2(
const double k
)
:
row0( k, 0 ),
row1( 0, k )
{
//Do nothing
}
inline matrix2x2::matrix2x2(
const double m00,
const double m01,
const double m10,
const double m11
)
:
row0( m00, m01 ),
row1( m10, m11 )
{
//Do nothing
}
inline matrix2x2::matrix2x2(
const vector2 &r0,
const vector2 &r1
)
:
row0( r0 ),
row1( r1 )
{
//Do nothing
}
inline matrix2x2::matrix2x2(
const matrix2x2 &m
)
:
row0( m.row0 ),
row1( m.row1 )
{
}
inline vector2 &matrix2x2::operator[](
const unsigned i
)
{
if( i == 0 )
return this->row0;
else
return this->row1;
;
}
inline const vector2 &matrix2x2::operator[](
const unsigned i
) const
{
if( i == 0 )
return this->row0;
else
return this->row1;
;
}
inline bool operator== (
const matrix2x2 &a,
const matrix2x2 &b
)
{
return a.row0 == b.row0 && a.row1 == b.row1;
}
inline matrix2x2 operator- (
const matrix2x2 &a,
const matrix2x2 &b
)
{
return matrix2x2(
a.row0 - b.row0,
a.row1 - b.row1
);
}
inline matrix2x2 operator- (
const matrix2x2 &a
)
{
return matrix2x2( -a.row0, -a.row1 );
}
inline matrix2x2 operator* (
const double k,
const matrix2x2 &v
)
{
return matrix2x2(
v.row0 * k,
v.row1 * k
);
}
inline vector2 operator* (
const matrix2x2 &M,
const vector2 &x
)
{
return vector2(
M.row0 * x,
M.row1 * x
);
}
inline matrix2x2 operator+ (
const matrix2x2 &a,
const matrix2x2 &b
)
{
return matrix2x2(
a.row0 + b.row0,
a.row1 + b.row1
);
}
inline matrix2x2 &operator +=(
matrix2x2 &a,
const matrix2x2 &b
)
{
a.row0 += b.row0;
a.row1 += b.row1;
return a;
}
inline matrix2x2 &operator -=(
matrix2x2 &a,
const matrix2x2 &b
)
{
a.row0 -= b.row0;
a.row1 -= b.row1;
return a;
}
inline matrix2x2 &operator *=(
matrix2x2 &a,
const double k
)
{
a.row0 *= k;
a.row1 *= k;
return a;
}
inline matrix2x2 operator/ (
const matrix2x2 &v,
const double k
)
{
return matrix2x2(
v.row0/k, v.row1/k
);
}
inline double det(
const matrix2x2 &v
)
{
return v.row0.x*v.row1.y - v.row0.y*v.row1.x;
}
inline void transpose(
matrix2x2 &m
)
{
swap< double >( m.row0.y, m.row1.x );
}
inline matrix2x2 transpose(
const matrix2x2 &m
)
{
return matrix2x2(
m.row0.x, m.row1.x,
m.row0.y, m.row1.y
);
}
#endif