I have a polynomial of the type std::vector<T>
and a matrix of the type std::vector<std::vector<T>>
, with classes for both of them. The output (in my case void print()-function) for each of those classes works fine. Now I created a matrix consisting of multiple polynomials, like this, and I'm not entirely sure how to create an output for it.
Suppose I have a polynomial p1: Polynomial<T> p1{0,1,2}
. My Polynomial::print function interprets it correctly and returns: x^2+x+1
.
For a 'normal' matrix of the type Matrix<T> m1(2,2,0)
, a 2x2-matrix filled with 0's, it is correctly returned with Matrix::print() as well as:
0, 0
0, 0
Now, suppose I want a polynomial matrix m1: Matrix<Polynomial<T>> mp(2,2,p1)
, a 2x2-matrix filled polynomial p1 from before. The code is accepted, but now I want to print the matrix using matrix::print(), so that I get:
x^2+x+1, x^2+x+1
x^2+x+1, x^2+x+1
Minimal working example:
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
class Polynomial;
template <typename T>
class Matrix
{
public:
Matrix(std::vector<std::vector<T> > ma);
Matrix(int rows, int cols, T const & init);
Matrix(const Matrix & m);
~Matrix();
Matrix ();
void print();
friend void Polynomial<T>::print();
private:
std::vector<std::vector<T>> Ma;
};
template <typename T>
Matrix<T>::Matrix(std::vector<std::vector<T>> ma)
:Ma(ma)
{}
template <typename T>
Matrix<T>::Matrix(int rows, int cols, T const & init)
{
std::vector<std::vector<T>> b(rows, std::vector<T>(cols, init));
Ma=b;
}
template <typename T>
Matrix<T>::~Matrix()
{}
template <typename T>
Matrix<T>::Matrix() :
Matrix(1,1,0)
{}
template <typename T>
Matrix<T>::Matrix(const Matrix & m)
{
Ma = m.Ma;
}
template <typename T>
void Matrix<T>::print()
{
for (auto i = 0; i < Ma.size(); i++)
{
for (auto j = 0; j < Ma[i].size(); j++)
if ( j == Ma.size()-1 )
{
cout << Ma[i][j]; //This causes problems
}
else
cout << Ma[i][j] << ", \t";
cout << endl;
}
}
template <typename T>
class Polynomial
{
public:
Polynomial(std::vector<T> const& coef);
Polynomial(std::initializer_list<T> const& coef);
void print();
const int getdeg();
const T getKoeff(int index) const;
friend class Matrix<T>;
Polynomial ();
friend void print();
private:
std::vector<T> coefficient;
};
template <typename T>
Polynomial<T>::Polynomial(std::vector<T> const& coef) :
coefficient(coef)
{}
template <typename T>
Polynomial<T>::Polynomial(std::initializer_list<T> const& coef) :
coefficient(coef)
{}
template <typename T>
Polynomial<T>::Polynomial ()
{
coefficient = new std::vector<T> [coefficient.getdeg()];
coefficient[0]=0;
}
template <typename T>
void Polynomial<T>::print() //Reduced version for demonstration purposes, but
{
for ( int i = getdeg(); i >= 0; i-- )
{
cout << coefficient[i] << "x^" << i; //the output is always of the type cout << xyz
}
}
template <typename T>
const int Polynomial<T>::getdeg()
{
int g = coefficient.size()-1;
return g;
}
int main()
{
typedef double T;
Polynomial<T> p1{0,1,2};
p1.print();
Matrix<Polynomial<T>> mp(2, 3, Polynomial<T>{0});
// mp.print(); //When this is commented out chaos ensues
return 0;
}
2x^21x^10
is returned (Note: +&- excluded from the notation, since the code would be too long).
The matrix::print() might cause problems due to cout
having problems handling the result of the polynomial.
Anybody have an idea how to make matrix::print() give useful results for a polynomial matrix? Thank you in advance.