Page MenuHomec4science

Matrix2x2.cpp
No OneTemporary

File Metadata

Created
Sat, Jul 5, 02:02

Matrix2x2.cpp

/*
* Matrix2x2.cpp
*
*/
#include <iostream>
#include "Matrix2x2.hpp"
#include <cmath>
// Default constructor
Matrix2x2::Matrix2x2()
{
matrix[0] = 0.0;
matrix[1] = 0.0;
matrix[2] = 0.0;
matrix[3] = 0.0;
}
//
// Overriden copy constructor
Matrix2x2::Matrix2x2(const Matrix2x2& m) // Here reference in the solution
{
matrix[0] = m.Get(0,0);
matrix[1] = m.Get(0,1);
matrix[2] = m.Get(1,0);
matrix[3] = m.Get(1,1);
}
// Alternate constructor
Matrix2x2::Matrix2x2(const double m11, const double m12,
const double m21, const double m22)
{
matrix[0] = m11;
matrix[1] = m12;
matrix[2] = m21;
matrix[3] = m22;
}
// Deconstructor
Matrix2x2::~Matrix2x2()
{
}
// Method to get the matrix value
double Matrix2x2::Get(const int& i, const int& j) const
{
return matrix[2 * i + j];
}
// Method to compute the determinant of the matrix
double Matrix2x2::getDeterminant() const
{
return matrix[0]*matrix[3]-matrix[1]*matrix[2];
}
// Compute the inverse of the matrix
Matrix2x2 Matrix2x2::Inverse() const
{
double det = getDeterminant();
if (det == 0)
{
std::cout << "The exponant of the matrix is equal to 0 ! Cannot compute the inverse. Returning a zero matrix."
<< std::endl;
return Matrix2x2();
}
else
{
return Matrix2x2(Get(1, 1) / det, - Get(0, 1) / det,
- Get(1, 0) / det, Get(0, 0) / det);
}
}
// Overload assigment operator
Matrix2x2& Matrix2x2::operator=(const Matrix2x2& m)
{
matrix[0] = m.Get(0,0);
matrix[1] = m.Get(0,1);
matrix[2] = m.Get(1,0);
matrix[3] = m.Get(1,1);
return *this;
}
// Overload unary operator
Matrix2x2 Matrix2x2::operator-() const
{
return Matrix2x2(-matrix[0], -matrix[1], -matrix[2], -matrix[3]);
}
// Overload plus operator
Matrix2x2 Matrix2x2::operator+(const Matrix2x2& m) const
{
return Matrix2x2(matrix[0] + m.Get(0,0),
matrix[1] + m.Get(0,1),
matrix[2] + m.Get(1,0),
matrix[3] + m.Get(1,1));
}
// Overload moins operator
Matrix2x2 Matrix2x2::operator-(const Matrix2x2& m) const
{
return Matrix2x2(matrix[0] - m.Get(0,0),
matrix[1] - m.Get(0,1),
matrix[2] - m.Get(1,0),
matrix[3] - m.Get(1,1));
}
// Method to multiply by a constant
void Matrix2x2::Multiply(const double d)
{
matrix[0] *= d;
matrix[1] *= d;
matrix[2] *= d;
matrix[3] *= d;
}
std::ostream& operator<<(std::ostream& output, const Matrix2x2& m)
{
output << m.matrix[0] << " " << m.matrix[1] << std::endl;
output << m.matrix[2] << " " << m.matrix[3];
return output;
}

Event Timeline