Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <ostream>
- #include "fraction.h"
- #include "Matrice.h"
- Matrix::Matrix()
- : height(0),
- width(0),
- data(NULL)
- {}
- Matrix::Matrix(int aHeight, int aWidth)
- : height(aHeight),
- width(aWidth),
- data(NULL)
- {
- setSize(aHeight, aWidth);
- }
- Matrix::Matrix(const Matrix& copy)
- :height(copy.height),
- width(copy.width),
- data(NULL)
- {
- setSize(copy.height, copy.width);
- for (int r = 0; r < height; ++r)
- {
- data[r] = new Fraction[width];
- for (int c = 0; c < width; ++c)
- {
- data[r][c] = copy.data[r][c];
- }
- }
- }
- Matrix::Matrix(Matrix&& other)
- : Matrix()
- {
- swap(*this, other);
- }
- void Matrix::setSize(int aHeight, int aWidth)
- {
- //Checks that we are not resizing to the same size
- //or haven't created date yet
- //and that new size is valid.
- if ((height != aHeight || width != aWidth) &&
- aHeight > 0 && aWidth > 0)
- {
- //Create new Data. We need this regardless if we're growing, shrinking or creating new data.
- Fraction** newData = new Fraction*[aHeight];
- for (int r = 0; r < aHeight; ++r)
- {
- newData[r] = new Fraction[aWidth];
- //Initialize explicitly to zero even if Fraction ctor alreader
- //does it. It show the intention of the programmer.
- for (int c = 0; c < width; ++c)
- {
- data[r][c] = 0;
- }
- }
- //Copy Data whatever amount of data we can keep.
- if (data != NULL)
- {
- int minHeight = std::min(height, aHeight);
- int minWidth = std::min(width, aWidth);
- for (int r = 0; r < minHeight; ++r)
- {
- for (int c = 0; c < minWidth; ++c)
- {
- newData[r][c] = data[r][c];
- }
- }
- for (int r = 0; r < height; ++r)
- {
- delete[] data[r];
- data[r] = 0;
- }
- delete[] data;
- }
- //Assign new data
- data = newData;
- }
- height = aHeight;
- width = aWidth;
- }
- void Matrix::setValue(int row, int col, const Fraction& value)
- {
- data[row][col] = value;
- }
- Fraction Matrix::getValue(int row, int col) const
- {
- return data[row][col];
- }
- Matrix::~Matrix()
- {
- if (data != NULL)
- {
- for (int r = 0; r < height; ++r)
- {
- delete[] data[r];
- data[r] = 0;
- }
- delete[] data;
- data = 0;
- }
- }
- Matrix& Matrix::operator=(Matrix rhs)
- {
- swap(*this, rhs);
- return *this;
- }
- Matrix& Matrix::operator+(const Matrix& rhs)const
- {
- Matrix result;
- if (height == rhs.height && width == rhs.width)
- {
- result.setSize(height, width);
- for (int r = 0; r < rhs.height; ++r)
- {
- for (int c = 0; c < rhs.width; ++c)
- {
- result.data[r][c] = this->data[r][c] + rhs.data[r][c];
- }
- }
- }
- return result;
- }
- Matrix& Matrix::operator-(const Matrix& rhs)const
- {
- Matrix result;
- if (height == rhs.height && width == rhs.width)
- {
- for (int r = 0; r < rhs.height; ++r)
- {
- for (int c = 0; c < rhs.width; ++c)
- {
- result.data[r][c] = this->data[r][c] - rhs.data[r][c];
- }
- }
- }
- return result;
- }
- Matrix& Matrix::operator*(const Matrix& rhs)const
- {
- Matrix result;
- if (this->width == rhs.height)
- {
- result.setSize(height, rhs.width);
- for (int r = 0; r < result.height; ++r)
- {
- for (int c = 0; c < result.width; ++c)
- {
- for (int i = 0; i < width; i++)
- {
- result.data[r][c] += this->data[r][i] * rhs.data[i][c];
- }
- }
- }
- }
- else
- {
- return result;
- }
- return result;
- }
- void swap(Matrix& first, Matrix& second)
- {
- std::swap(first.height, second.height);
- std::swap(first.width, second.width);
- std::swap(first.data, second.data);
- }
- std::ostream& operator<<(std::ostream& out, const Matrix& rhs)
- {
- if (rhs.data != NULL)
- {
- for (int r = 0; r < rhs.height; ++r)
- {
- for (int c = 0; c < rhs.width; ++c)
- {
- out << rhs.data[r][c] << "\t";
- }
- out << "\n";
- }
- }
- else
- {
- out << "Matrix is empty \n";
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement