0
votes

hey there i'm a student in computer science , we are asked to build a generic matrix using the vector class and we are not allowed to use "new" and "delete" at all. I don't know how to start it properly we can use these libraries:

cassert

vector

cstdlib

cmath

I searched all over the Internet but didn't found a class that uses the vector as the matrix (index of matrix is mat[i*cols+j]) and doesn't use new and delete function or allocate memory:

examples of what we are supposed to check:(int google test)

"Matrix.hpp"

template < class T ><class T> class Matrix {
    private:
        int rows;
        int cols;
        vector< T > mat;

    public:
        Matrix(int row, int col,const T& mat2):rows(row),cols(col) {
            mat.resize(rows*cols);
            //?
        };

        Matrix(int row, int col);
        virtual ~Matrix();
        Matrix(const Matrix< T >& rhs);
        Matrix< T > transpose();
        Matrix< T >& operator=(const Matrix< T >& rhs);
        bool operator==(const Matrix< T >& rhs)const;
        const int getRowNum() const;
        const int getColNum() const;
        Matrix< T >& operator+(const Matrix< T >& rhs);
};

"gtest/gtest.h"

Matrix<int> mat1(1, 1, std::vector<int>(1,2));

EXPECT_EQ(1, mat1.getRowNum());
EXPECT_EQ(1, mat1.getColNum());

int t= 0;
EXPECT_TRUE(mat1.hasTrace(t));
EXPECT_TRUE(mat1.isSquareMatrix());

Matrix<int> transpose_mat= mat1;
EXPECT_EQ(transpose_mat, mat1.transpose());

Matrix<int> mat2(1, 1, std::vector<int>(1,3));
Matrix<int> add_mat(1, 1, std::vector<int> (1,5));
EXPECT_EQ(add_mat, mat1+mat2);

Matrix<int> multi_mat(1, 1, std::vector<int>(1,6));
EXPECT_EQ(multi_mat, mat1*mat2);

Matrix<int> scalar_multi_mat(1, 1, std::vector<int>(1,4));
EXPECT_EQ(scalar_multi_mat, mat1*2);

This is what we are supposed to do:

Matrix interface: You must define and implement the generic class file Matrix.hpp Matrix. The matrix will be generic and limbs will not necessarily integers but generic type numbers. For the purpose of due diligence, you Squirrels are entitled to assume that operators will have the necessary exercise as being above. Also you can assume the Squirrels be used in relation to the rest of the matrix are such that the order of the atomic steps of connecting or twice) a long chain of calculations (does not matter. For example (a + b) + c == a + (b + c) the exercise or functions that you can use the default language is given), all generically, of course (:

  • Constructor without parameters. The builder builds a matrix of 1x1 With organ is conducted T where T is the type that is stored in the matrix.
  • The constructor gets the number of rows, number of columns and the vector with the values ​​of the matrix filling) saw the driver call this constructor does not realize, the department should be structured so that default is good enough
  • (Constructor Copy) copy constructor (a different matrix receiver) does not realize, the department should be structured so that default is good enough
  • (The assignment operator) do not realize, the department must be built so that default is good enough
  • Plus (+) operator to connect matrices.
  • Multiplication (*) operator matrix multiplication. Matrix of the object it does the function is left matrix multiplication.
  • (Swap function matrix) transpose
  • (Aqaba function) HasTrace (reference to the organ recipient generic type and a task which the value of the trace (if any) of the matrix and returns a Boolean value: true if a square matrix, and another false case put the value additive identity organ received about .reference
  • The four functions above) connection, transpose and HasTrace double (not from the object it was applied function. In addition, you can add more public functions or Privacy desired, according to what you think is useful to the department.

If someone can help me, I will be grateful!

2
Given that most matrices won't be resized, dynamic allocation at all seems silly. - RamblingMad
we cat use std::vector or std::list thats all , a posted below what i have done this far but i don't know how to continue , for example we need to use the copy constructor but a default one, what does it mean? - raphael Zanzouri
@CoffeeandCode, not all matrices are the fixed 3x3 or 4x4 ones used in 3D graphics. Matrices are used in many disciplines other than video games. How do you know the matrix dimensions and data are not loaded from disk? - Emile Cormier

2 Answers

1
votes

You can start by considering that an NxM matrix can be represented as a vector with M*N elements. Operations such as addition and subtraction become simply equivalent to vector addition and subtraction.

More general operations, though will require a simple transformation to convert a matrix index [a, b] into a vector index [c].

Given a matrix index [a, b] you can find the corresponding vector element by using:

c = a*N + b

Similarly, converting from a vector index [c] to the matrix index [a, b] by:

a = c / N
b = c % N

Here the / and % are integer division and modulus.

0
votes
template < class T ><class T> class Matrix {

 private:

 int rows;
 int cols;
 vector< T > mat;

 public:


 Matrix(int row, int col,const T& mat2):rows(row),cols(col) {
   mat.resize(rows*cols);
   //?
 };

 Matrix(int row, int col);
 virtual ~Matrix();
 Matrix(const Matrix< T >& rhs);
 Matrix< T > transpose();
 Matrix< T >& operator=(const Matrix< T >& rhs);
 bool operator==(const Matrix< T >& rhs)const;
 const int getRowNum() const;
 const int getColNum() const;
 Matrix< T >& operator+(const Matrix< T >& rhs);
};