answersLogoWhite

0

#include<iostream>

#include<vector>

#include<random>

template<const size_t R, const size_t C>

class Matrix

{

public:

using row_type = int[C];

private: // attributes

int m_data[R][C];

public: // construction/assignment

Matrix ();

Matrix (const Matrix& source);

Matrix (Matrix&& source);

Matrix& operator= (const Matrix<R,C>& source);

Matrix& operator= (Matrix<R,C>&& source);

~Matrix () {}

public: // accessors

row_type& row (const size_t index) { return m_data[index]; }

const row_type& row (const size_t index) const { return m_data[index]; }

row_type& operator[] (const size_t index) { return m_data[index]; }

const row_type& operator[] (const size_t index) const { return m_data[index]; }

size_t size() const { return R * C; }

size_t rows() const { return R; }

size_t cols() const { return C; }

public: // operations

Matrix<R,C>& operator+= (const Matrix<R,C>&);

Matrix<R,C>& operator-= (const Matrix<R,C>&);

};

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix()

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = 0;

}

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix(const Matrix<R,C>& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = source.m_data[row][col];

}

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix(Matrix<R,C>&& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = std::move (source.m_data[row][col]);

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = source.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = std::move (source.m_data[row][col]);

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] += rhs.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] -= rhs.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs)

{

Matrix<R,C> sum (lhs);

return sum += rhs;

}

template<const size_t R, const size_t C>

Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs)

{

Matrix<R,C> sub (lhs);

return sub -= rhs;

}

template<const size_t R, const size_t C, const size_t R1, const size_t C1>

Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs)

{

static_assert (C==R1, "Matrix dimension mismatch!");

Matrix<R,C1> mul;

for (size_t x=0; x!=R; ++x)

{

for (size_t y=0; y!=C1; ++y)

{

int prod = 0;

for (size_t z=0; z!=C; ++z)

{

prod += lhs[x][z] * rhs[z][y];

}

mul[x][y] = prod;

}

}

return mul;

}

template<const size_t R, const size_t C>

std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m)

{

for (size_t row=0; row<R; ++row)

{

for (size_t col=0; col<C; ++col)

{

std::cout << m[row][col] << '\t';

}

std::cout << std::endl;

}

return os;

}

int main()

{

std::default_random_engine generator;

std::uniform_int_distribution<int> distribution (1,9);

const size_t rows = 2;

const size_t cols = 3;

Matrix<rows, cols> a, b;

for (size_t row=0; row<rows; ++row)

{

for (size_t col=0; col<cols; ++col)

{

a[row][col] = distribution (generator);

b[row][col] = distribution (generator);

}

}

std::cout << "Matrix a:\n\n" << a << '\n' << std::endl;

std::cout << "Matrix b:\n\n" << b << '\n' << std::endl;

std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl;

std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl;

Matrix<cols, rows> c;

for (size_t row=0; row<rows; ++row)

{

for (size_t col=0; col<cols; ++col)

{

c[col][row] = distribution (generator);

}

}

std::cout << "Matrix c:\n\n" << c << '\n' << std::endl;

std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

What is operator number?

multiplication-addition-subtraction-division


What is the name of a formula that uses more than one arithmetic operator?

MDAS (Multiplication, Division, Addition, Subtraction)


Symbol that specify addition substraction multiplication?

+ addition - subtraction* multiplication


What is an arithmetic operator in c program?

An arithmetic operator is any of the "atomic" operators to do the following math operations: + addition - subtraction / division * multiplication % modulus division


What is the inverse of subtraction?

Addition is the inverse of Subtraction. Division is the inverse of Multiplication. and then visa-versa. :-) Addition is the inverse of Subtraction. Division is the inverse of Multiplication. and then visa-versa. :-) the Answer is subtraction


When addition subtraction and multiplication are use in a problem in that order which get done first?

In mathematical operations, addition, subtraction, and multiplication are governed by the order of operations, often remembered by the acronym PEMDAS (Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right)). When addition, subtraction, and multiplication are used in a problem, multiplication is performed first, followed by addition and subtraction, which are executed from left to right. Thus, in a sequence where these operations appear together, multiplication takes priority over addition and subtraction.


What is an arithmetic operation?

The basic arithmetic operations are addition, subtraction, multiplication and division, although this subject also includes more advanced operations, such as manipulations of percentages, square roots, exponentiation, and logarithmic functions.


What is a mathematical operation?

An operator is a mapping from one vector space to another.


For a new C-Sharp coder What do Operators do?

An operator in C# performs a specific operation like addition, subtraction, multiplication etc.


Subtraction is a form of addition and division is a form of multiplication give as example?

nope. Multiplication is a form of addition. Division is a form of subtraction.


What are the fundamental operation of math?

the fundamental operations in math are, addition +, subtraction -, division /,and multiplication x ..


What is the difference between the inverse of addition and the inverse of multiplication?

the inverse of addition is subtraction and the inverse of multiplication is division. Of course, multiplication is just repeated addition so division is just repeated subtraction!