overloading operators '+' and '=' C++ - c++

I've recently started learning C++, right now I'm working on a Matrix Class. I'm trying to overload operators and it turned out to be more difficult than I thought. So I've overloaded '=' and '+', first one works perfectly fine when I just want to set one matrix equal to another, but when I do something like 'matrix = matrix1 + matrix2' it crashes with no error. I'd be very thankful if someone helps me. Here's my code:
class Matrix
{
private:
int lines , columns;
int *Matrix_Numbers;
public:
Matrix();
Matrix(int n , int m)
{
lines = n , columns = m;
Matrix_Numbers = new int[lines * columns];
}
Matrix & operator = (Matrix &mat);
Matrix & operator + (Matrix &mat);
~Matrix()
{
delete Matrix_Numbers;
}
};
Matrix & Matrix::operator = (Matrix &mat)
{
this -> lines = mat.lines;
this -> columns = mat.columns;
int i , j;
for(i = 0 ; i < lines ; i++)
{
for(j = 0 ; j < columns ; j++)
{
this -> Matrix_Numbers[i * (this -> columns) + j] = mat(i , j);
}
}
return *this;
}
Matrix & Matrix::operator + (Matrix &mat)
{
Matrix result(lines , columns);
if(mat.lines == lines && mat.columns == columns)
{
int i , j;
for(i = 0 ; i < lines ; i++)
{
for(j = 0 ; j < columns ; j++)
{
result.Matrix_Numbers[i * columns + j] = Matrix_Numbers[i *
columns + j] + mat(i , j);
}
}
}
else
{
cout << "Error" << endl;
}
return result;
}
Of course it's just a part of my code, there's more, but I thought that this is the broken part. Let me know if you need more information:)

Your operator+ method returns a reference to the result. The only problem is that result is a local variable which means it gets destroyed upon the method returning. You should return it by value instead and let the compiler optimize stuff.
Like others pointed out in the comments, you should use const whenever possible so your operator+ can actually be called with constant parameters.

Related

Trying to add two vectors but it is adding in the wrong order

In this function i'm working on the goal is to add the coefficients of polynomials, the coefficients are stored in vectors. P is a private member of the class Polynom that I have made. "RHS" is not a member of the class - it is another instance of the class, so I can still access all of its private members, like RHS.P[i]. This is the code that I have so far.
`
Polynom Polynom::operator+(const Polynom& RHS) cons
vector <int> temp;
vector <int> temp2;
vector <int> temp3;
for (int i = 0; i <= P.size()-1; i ++)
{
temp.push_back(P[i]);
}
for (int i = 0; i <= RHS.P.size()-1; i ++)
{
temp2.push_back(RHS.P[i]);
}
int largerPoly = P.size()-1;
for (int i = 0; i <= P.size()-1; i++)
{
if(largerPoly == RHS.P.size()-1)
{
temp3.push_back(temp2[largerPoly - i]);
}
else
{
temp3.push_back(temp[i]);
}
largerPoly --;
}
return Polynom(temp3);
`
So for example if I input in my P vector: (4)(2)(-1)(2)(0)(0)(2)
And in the other vector: (-4)(0)(1)
The resulting vector should be: (4)(2)(-1)(2)(-4)(0)(3) (1+2)=3 (0+-4)=-4
Which basically means I want to add the vector from the back (hopefully that makes sense)
unfortunately that's not the output i'm getting with this code. Any tips on what I am missing?
Make it easier and simply split the two cases:
Polynom Polynom::operator+(const Polynom& arg) const
{
const auto &lhs = this->P; // define some shorthand refs
const auto &rhs = RHS.P;
std::vector<int> res;
// case 1: lhs is larger
if (lhs.size() > rhs.size()) {
res.assign(lhs.begin(), lhs.end()); // copy lhs into res
auto offset = lhs.size() - rhs.size();
for (auto i = 0; i < rhs.size(); i++) {
res[i + offset] += rhs[i];
}
}
// case 2: rhs is larger
else {
res.assign(rhs.begin(), rhs.end()); // copy lhs into res
auto offset = rhs.size() - lhs.size();
for (auto i = 0; i < lhs.size(); i++) {
res[i + offset] += lhs[i];
}
}
return Polynom(res);
}
Using references also means, that we do not have to copy the whole data in several local variables.
Store your coefficients in the reverse order.
x^3 + x + 1 == 1 + x + x^3 <=> [1, 1, 0, 1]
And now all your terms will always line up and the coefficient of x^n is at index n.

Sum of independent diagonal in a matrix

I'm currently studying for an exam and I'm trying to deal with dynamical matrix. I've come across a problem regarding calculating the sum of every diagonal of a matrix whose values and size are chosen by the user.
The intent of my program is to print, thanks to a function, whose parameters are the matrix and its size, the value of every diagonal sum. I'll show you the code and describe it in depth.
----------------
| 52 | 35 | 5 | Example of matrix.
---------------- Imagine the first diagonal to be the one which goes right-to-left
| 2 | 71 | 1 | and only consists in the number "47".
---------------- The second diagonal would be the one which goes right-to-left and
| 3 | 60 | 25 | consists in the number "15" and "79".
---------------- So to get the sum of the second diagonal it would be:
| 79 | 55 | 98 |
---------------- sum = m[n_rows - 1][diag - 2] + m[n_rows - 2][diag - 1]
| 47 | 15 | 66 |
---------------- When diag > columns, in order to avoid error regarding matrix size,
I should lower the quantity "n_rows - 1" by the quantity "diag - n_columns".
This is what I thought to do, according to my description:
void diag_matrix(int** m, int righe, int colonne){//righe = rows, colonne = columns.
//M is the matrix.
// diag is the number of the diagonal I'm considering.
for(int diag = 1; diag < (righe + colonne); diag++){
int sum = 0;// the sum
int i = 0;// the counter of the cicle
int l = 0;// this is the value to riallign the row in case diag > column
int temp = diag;//I use this variable not to modify the value of diag.
// What I want is: when the column-index/row-index of the matrix reaches 0, the cicle will interrupt (after final iteration);
while(righe - i - l - 1 > 0 || diag - 1 - i > 0){
if (diag > colonne){//this condition changes l-value only if diag value is greater than column. Explanation outside the code
l = diag - colonne;//this is the value to subtract to row-index
temp = colonne;//this position is necessary to set column-index to its maxium.
}
sum = sum + m[righe - 1 - l - i][temp -1 - i];//pretty clear I think.
i++;//the i is incremented by one.
}// end of while-statement
cout << "Somma Diagonale " << diag << " = " << sum << ".\n";
}// end of for-statement
}//end of function declaration
Obviously it does not work, but I can't figure out the problem.
(There used to be a paragraph here, but on a second look, you didn’t make the mistake it was talking about.)
Since you didn’t post to Code Reviews, here’s a solution instead of a detailed code review. (If you want to make the original approach work, I’d suggest single-stepping through it in a debugger and checking where your variables first get the wrong value.) It’s got a lot of boilerplate to make it compile and run, but the part you’ll be most interested in is diag_sums() and its comments.
One idea here is to use OOP to automatically check the bounds of your array accesses. The latter is very important for catching off-by-one errors and the like. You can turn it off in production if you want, but you really don’t want to silence warnings when your program has a buffer overrun. Other optimizations here include locality of access for the data, and strength reduction on the operations: rather than check on each iteration whether we’ve hit the right edge and the bottom edge, we can simply calculate the length of each diagonal in advance.
Since the definition of diagonal number k of matrix a with M rows is equivalent to: all elements a[i][j] such that such that M - k = i - j, the algorithm ensures correctness by maintaining the invariant, which holds whenever we add 1 to both i and j, starting when either i or j is 0, and stopping whenever i = M or j = N, that is, traversing each step of the diagonal from the left or top edge to the right or bottom edge, whichever comes first.
#include <assert.h>
#include <iostream>
#include <stddef.h>
#include <stdlib.h>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
template <typename T>
class matrix {
public:
matrix( const ptrdiff_t rows,
const ptrdiff_t cols,
std::vector<T>&& elems )
: rows_(rows), cols_(cols), elems_(elems)
{
assert( rows_ > 0 );
assert( cols_ > 0 );
assert( elems_.size() == static_cast<size_t>(rows_*cols_) );
}
matrix( const ptrdiff_t rows,
const ptrdiff_t cols,
const std::vector<T>& elems )
: matrix( rows, cols, std::move(std::vector<T>(elems)) )
{}
matrix( const matrix<T>& ) = default;
matrix( matrix<T>&& ) = default;
matrix& operator= ( const matrix<T>& ) = default;
matrix& operator= ( matrix<T>&& ) = default;
T& operator() ( const ptrdiff_t m, const ptrdiff_t n )
{
assert( m >= 0 && m < rows_ );
assert( n >= 0 && n < cols_ );
return elems_[static_cast<size_t>(m*cols_ + n)];
}
const T& operator() ( const ptrdiff_t m, const ptrdiff_t n ) const
{
/* Because this call does not modify any data, and the only reason the
* member function above cannot be const is that it returns a non-const
* reference to an element of elems, casting away the const qualifier
* internally and then returning a const reference is a safe way to
* re-use the code.
*/
matrix<T>& nonconst = *const_cast<matrix<T>*>(this);
return nonconst(m,n);
}
ptrdiff_t rows() const { return rows_; }
ptrdiff_t cols() const { return cols_; }
private:
ptrdiff_t rows_;
ptrdiff_t cols_;
std::vector<T> elems_;
};
template<typename T>
std::ostream& operator<< ( std::ostream& out, const matrix<T>& x )
/* Boilerplate to print a matrix. */
{
const ptrdiff_t m = x.rows(), n = x.cols();
for ( ptrdiff_t i = 0; i < m; ++i ) {
out << x(i,0);
for ( ptrdiff_t j = 1; j < n; ++j )
out << ' ' << x(i,j);
out << '\n';
} // end for
return out;
}
using elem_t = int;
std::vector<elem_t> diag_sums( const matrix<elem_t>& a )
/* Return a vector of all the diagonal sums of a.
*
* The first diagonal sum is a(rows-1,0)
* The second is a(rows-2,0) + a(rows-1,1)
* The third is a(rows-3,0) + a(rows-2,1) + a(rows-1,2)
* And so on. I.e., the kth diagonal is the sum of all elements a(i,j) such
* that i - j == rows - k.
*
* If a is a M×N matrix, there are M diagonals starting in column zero, and
* N-1 diagonals (excluding the one containing a(0,0) so we don't count it
* twice) starting in row 0. We process them bottom to top, then left to
* right.
*
* The number of elements in a diagonal starting at a(i,0) is min{M-i, N}. The
* number of elements in a diagonal starting at a(0,j) is min{M, N-j}. This is
* because a diagonal stops at either the bottom edge or the left edge of a.
*/
{
const ptrdiff_t m = a.rows(), n = a.cols();
std::vector<elem_t> result;
result.reserve( static_cast<size_t>(m + n - 1) );
for ( ptrdiff_t i = m-1; i > 0; --i ) {
elem_t sum = 0;
const ptrdiff_t nk = (m-i) < n ? (m-i) : n;
for ( ptrdiff_t k = 0; k < nk; ++k )
sum += a(i+k, k);
result.emplace_back(sum);
} // end for i
for ( ptrdiff_t j = 0; j < n; ++j ) {
elem_t sum = 0;
const ptrdiff_t nk = m < (n-j) ? m : (n-j);
for ( ptrdiff_t k = 0; k < nk; ++k )
sum += a(k, j+k);
result.emplace_back(sum);
} // end for j
return result;
}
matrix<elem_t> read_input_matrix( const int row, const int column )
/* Reads in row*column consecutive elements from cin and packs them into a
* matrix<elem_t>.
*/
{
assert(row > 0);
assert(column > 0);
const ptrdiff_t nelements = row*column;
assert(nelements > 0); // Check for overflow.
std::vector<elem_t> result;
result.reserve(static_cast<size_t>(nelements));
for ( ptrdiff_t i = nelements; i > 0; --i ) {
int x;
cin >> x;
assert(cin.good());
result.push_back(x);
}
return matrix<elem_t>( row,
column,
std::move(result) );
}
template<typename T>
bool print_sequence( const T& container )
/* Prints the contents of a container in the format
* "{47, 94, 124, 160, 148, 36, 5}".
*/
{
cout << "{";
if ( container.begin() != container.end() )
cout << *container.begin();
for ( auto it = container.begin() + 1; it < container.end(); ++it )
cout << ", " << *it;
cout << "}\n";
return cout.good();
}
/* A simple test driver that reads in the number of rows, the number of
* columns, and then row*columns int values, from standard input. It
* then passes the result to diag_matrix(), E.g.:
*
* 5 3
* 52 35 5
* 2 71 1
* 3 60 25
* 79 55 98
* 47 15 66
*/
int main()
{
int rows, columns;
cin >> rows;
cin >> columns;
assert(cin.good());
const matrix<elem_t> input_matrix = read_input_matrix( rows, columns );
// cout << input_matrix; // Instrumentation.
const std::vector<elem_t> sums = diag_sums(input_matrix);
print_sequence(sums);
return EXIT_SUCCESS;
}
You could also just do print_sequence(diag_sums(read_input_matrix( rows, columns ))).
You can simplify your code finding the starting position of each diagonal and then stepping through the matrix as long as the coordinates stay inside the matrix.
Something like this:
#include <iostream>
using namespace std;
void diag_matrix(int** m, int rows, int cols)
{
for (int diag = 1; diag < rows + cols; diag++)
{
int x, y;
if (diag < rows)
{
y = rows - diag;
x = 0;
}
else
{
y = 0;
x = diag - rows;
}
int sum = 0;
cout << "Summing diagonal #" << diag << ":";
while ((x < cols) && (y < rows))
{
sum += m[y][x];
cout << " " << m[y][x];
x++;
y++;
}
cout << " result: " << sum << "." << endl;
}
}
int main(int argc, char* argv[])
{
int rows = 5, cols = 3;
int **m = new int*[rows];
for (int i = 0; i < rows; i++)
m[i] = new int[cols];
m[0][0] = 52; m[0][1] = 35; m[0][2] = 5;
m[1][0] = 2; m[1][1] = 71; m[1][2] = 1;
m[2][0] = 3; m[2][1] = 60; m[2][2] = 25;
m[3][0] = 79; m[3][1] = 55; m[3][2] = 98;
m[4][0] = 47; m[4][1] = 15; m[4][2] = 66;
diag_matrix(m, rows, cols);
for (int i = 0; i < rows; i++)
delete[] m[i];
delete[] m;
return 0;
}

Looking for help about a FFT template

I am currently working on a problem that requires FFT for convolution, however when I brought in my FFT template from my archive I realize that there's something wrong with the output.
eg:
Input: (0, 0) (0, 0) (4166667, 0) (1, 0)
Correct output: (4166668, 0) (-4166667, 1) (4166666, 0) (-4166667, -1)
Template output: (4166668, 0) (-4166667, -1) (4166666, 0) (-4166667, 1)
The code:
#define MAXN
#define ld long double
#define op operator
struct base {
typedef ld T; T re, im;
base() :re(0), im(0) {}
base(T re) :re(re), im(0) {}
base(T re, T im) :re(re), im(im) {}
base op + (const base& o) const { return base(re + o.re, im + o.im); }
base op - (const base& o) const { return base(re - o.re, im - o.im); }
base op * (const base& o) const { return base(re * o.re - im * o.im, re * o.im + im * o.re); }
base op * (ld k) const { return base(re * k, im * k); }
base conj() const { return base(re, -im); }
};
base w[MAXN]; //omega lookup table
int rev[MAXN]; //reverse lookup table
void build_rev(int k) {
static int rk = -1;
if( k == rk )return ; rk = k;
for(int i = 1; i < (1<<k); i++) {
int j = rev[i-1], t = k-1;
while( t >= 0 && ((j>>t)&1) ) { j ^= 1 << t; --t; }
if( t >= 0 ) { j ^= 1 << t; --t; }
rev[i] = j;
}
}
void fft(base *a, int k) {
build_rev(k); int n = 1 << k;
for(int i = 0; i < n; i++) if( rev[i] > i ) swap(a[i], a[rev[i]]);
for(int l = 2, lll = 1; l <= n; l += l, lll += lll) {
if( w[lll].re == 0 && w[lll].im == 0 ) {
ld angle = PI / lll;
base ww( cosl(angle), sinl(angle) );
if( lll > 1 ) for(int j = 0; j < lll; ++j) {
if( j & 1 ) w[lll + j] = w[(lll+j)/2] * ww;
else w[lll + j] = w[(lll+j)/2];
} else w[lll] = base(1, 0);
}
for(int i = 0; i < n; i += l)
for(int j = 0; j < lll; j++){
base v = a[i + j], u = a[i + j + lll] * w[lll + j];
a[i + j] = v + u; a[i + j + lll] = v - u;
}
}
}
//ideone compiled example: http://ideone.com/8PTjW5
I tried to check the bit reverse and the root of unity table yet I didn't find any problems in those two parts. I also checked some online materials to verify the steps but there's nothing odd looking to me.
Would someone mind help me to find out what's the problem in this template?
Thanks in advance.
Edit: I decided to rely on another template at the end, thanks for all the reply from everyone.
It looks like you have the wrong sign for your weights (which means you're probably doing an inverse FFT instead of a forward FFT - remember that for the forward transform the weights are exp(-j * theta)). Try changing:
base ww( cosl(angle), sinl(angle) );
to:
base ww( cosl(angle), -sinl(angle) );
This seems to give the correct results for your simple test case. I haven't tried testing it with anything more demanding.
Coincidentally another user recently made exactly the same mistake in a MATLAB implementation. I guess that - sign is easy to miss.
Note also that your code is quite inefficient - you might want to consider using a simple, proven FFT library, like KissFFT instead.

Multiply two int arrays

I have a type BigInt which stores large numbers as an array of digits (0-9) in a char array called privately m_digitArray.
I am overloading arithmetic and relational operators to help in development. However, I could not get the multiplicative operator *= to work for me. The code I posted works for single digit operations in int * BigInt only while I'd prefer it work for any length number and for all int * BigInt, BigInt * int, and especially BigInt * BigInt operations. Like it works for 6 * bigInt (with a value of 6) = 36; but not 11 * bigInt (with a value of 10).
BigInt.cpp
Overloaded operator in question
BigInt BigInt::operator *= (const BigInt &rhs){
int size = m_digitArraySize + rhs.getSize();
int* C = new int[size];
int s = size-1;
for(int j= rhs.getSize() - 1; j >= 0; j--){
int carry = 0;
int shift = s;
for(int i = m_digitArraySize - 1; i >= 0; i--){
int m = getDigit(i) * rhs.getDigit(j);
int sum = m + C[shift] + carry;
int num = sum % 10;
int c = sum/10;
C[shift] = num;
carry = c;
shift--;
}
C[shift]= C[shift] + carry;
s--;
}
reallocateArray(size);
// for(int i = 0; i < size < ++i){
// m_digitArray[i] = '0' + C[i];
// }
// Nothing being returned, printing to debug
for (int i = 0; i < size; ++i)
{
cout << C[i];
}
return *this;
}
// Overload the * operator for BigInt * BigInt operations
BigInt operator * (const BigInt &lhs, const BigInt &rhs){
return BigInt(lhs) *= rhs;
}
// Overload the * operator for BigInt * int operations
BigInt operator * (const BigInt &lhs, int num){
return BigInt(lhs) *= num;
}
// Overload the * operator for int * BigInt operations
BigInt operator * (int num, const BigInt &rhs){
return BigInt(rhs) *= num;
}
You have an error in the logic in that you're coding operator * as something that implements operator *= to do it's work. Your problem is that calling:
x = y * z
shouldn't change the value of "y". That's an unwanted side-effect. But if you define operator* for your BigInt as using *= to get the result, that's exactly what it would do. Also if you do that, then calling
x = z * y
will cause different behaviour to "x = y * z", because now z is changed instead of y, which violates basic commutative laws of mathematics.
Start with operator* and operator=, then construct operator *= by chaining together calls to those.
operator* should create a new BigInt, then multiply the two input BigInts (passed as "const BigInt&"), then return the newly-created BigInt. To optimize this, you look into move constructors. but it's not necessary.
operator= is your copy assignment function, it should be later built to use move semantics, but that's also not critical
Then after that you can use *= to combine the two other functions, by merely writing something simple such as:
BigInt BigInt::operator*=(const BigInt &rhs)
{
(*this) = (*this) * rhs;
return (*this);
}
Well I was able to figure it out after some help. Here's the solution to the problem:
BigInt BigInt::operator *= (const BigInt &rhs){
// Create new BigInt to make changes non-destructively
BigInt numbers;
// A safe capacity size for the new BigInt
int size = m_digitArraySize + rhs.getSize();
// If either number is negative, set self to negative
if(!m_isPositive || !rhs.isPositive())
numbers.initializeArray(size, false);
else
numbers.initializeArray(size, true);
// Go through the multiplier
for(int i = 0; i < rhs.getSize(); ++i){
int carry = 0;
// Go through the multiplicand
for(int j = 0; j < m_digitArraySize; ++j){
// The product of multiplicand and multiplier plus the sum of the previous digits and the carry
int product = (getDigit(j) * rhs.getDigit(i)) + numbers.getDigit(i + j) + carry;
// Reset carry, necessary if the product is just a digit
carry = 0;
// Set carry to the tens digit
carry = product / 10;
// Set product to the units digit
product = product % 10;
// Save to the new BigInt
numbers.setDigit(i +j, product);
}
// Inner loop cuts off near the end, continue the loop if there is a carry
int nextDigit = i + m_digitArraySize;
while(carry!=0){
int new_value = numbers.getDigit(nextDigit) + carry;
carry = 0;
carry = new_value / 10;
new_value = new_value % 10;
numbers.setDigit(nextDigit, new_value);
++nextDigit;
}
}
// Remove excess zeros
numbers.normalizeArray();
*this = numbers;
return *this;
}

Overloading the * operator to multiply two polynomials

Beginner at C++ here. I specifically need help trying to figure out what is wrong with my overloaded * operator which is supposed to multiply two polynomials of a class Poly I made. My other overloaded operators appear to work just fine. Here is the original question:
P(x) = 2x4 + 3x3 – 12x2 + x – 19 (4th order polynomial)
or
P(x) = 2x7 + 5x5 – 7x2 + x – 19 (7th order polynomial)
Where the coefficients for the first and second equations can be described by the following array of integers
'Coeff1[ ] = {-19, 1, -12, 3, 2}'
'Coeff2[[ ] = {-19, 1, -7, 0, 0, 5, 0, 2}'
Design and code a polynomial class in C++ that has the following properties:
class Poly{
private:
int order; //order of the polynomial
int *coeff; // pointer to array of coeff on the heap
// size of coeff array predicated on (order + 1)
public:
Poly( ); //default constructor – order=0 & coeff[0] =1
Poly(int Order , int Default = 1) ;// creates Nth order poly
// and inits all coeffs
Poly(int Order, int *Coeff); //creates an Nth polynomial & inits
~Poly( ); // destructor
::::::: // copy constructor
//mutators & accessors
void set( ){// Query user for coefficient values);
void set(int coeff[ ], int size); // input coeffs via external coeff vector
int getOrder( )const; // get order of polynomial
int * get( ); //returns pointer to coeff array
//overloaded operators
Poly operator+( const Poly &rhs); // add two polynomials
Poly operator-( const Poly &rhs); // subt two polynomials
Poly operator*( const int scale); // scale a polynomial
Poly operator*(const Poly &rhs); // mult two polynomials
bool operator==(const Poly &rhs); // equality operator
const int & operator[ ](int I)const; // return the Ith coefficient
int & operator[ ](int I); // return the Ith coefficient
int operator( )(int X); // evaluate P(x) according
Poly & operator=(const Poly & rhs);
friend ostream & operator<<(ostream & Out, const Poly &rhs);
//other member functions
};
Demonstrate the following operations for the following Polynomials:
P1(x) = 2x4 + 3x3 – 12x2 + x – 19 (4th order polynomial)
P2(x) = 2x7 + 7x5 – 6x2 + x – 19 (7th order polynomial)
//display the following results for the polynomials defined above
o P3 = P1 + P2;
o P3 = P2 – P1;
o P3 = P1*10;
o P3 = 10*P1;
o P3 = P1*P2;
o bool flag = (P1==P2);
o P1[3] = P2[5]; // assign the 5th coefficient of P2 to 3rd coefficient of P1
o int Z = P1(int X = 5); // evaluate Polynomial for input X
// suggest using Horner’s method
o The displayed polynomial for P2 should be printed as follows
2X^7 + 7X^5 – 6X^2 + 1X – 1
Heres my code so far:
#include <iostream>
#include <cmath>
using namespace std;
class Poly
{
private:
int order;
int *coeff;
public:
Poly();
Poly(int Order, int Default=1);
Poly(int Order, int *Coeff);
Poly(const Poly &copy);
~Poly();
void set(); //ask the user for the coefficient values
void set(int *Coeff, int size); //put the coefficient values in a external coeff vector
int getOrder() const; //gets the order of the polynomial
int* get() const; //returns pointer to coeff array
Poly operator +(const Poly &rhs);
Poly operator -(const Poly &rhs);
Poly operator *(const int scale);
Poly operator *(const Poly &rhs);
bool operator ==(const Poly &rhs);
const int & operator [](int access) const;
int & operator [](int access);
int operator ()(int X);
Poly & operator =(const Poly &rhs);
friend ostream & operator <<(ostream &Out, const Poly &rhs);
};
int main() {
int coeff1[] = {-19,1,-12,3,2};
int coeff2[] = {-19,1,-7,0,0,5,0,2};
Poly P1(4,coeff1);
Poly P2(7, coeff2);
Poly P3;
cout << "P1: " << P1 << endl;
cout << "P2: " << P2 << endl;
P3 = P1 * P2;
cout << "P1 * P2: " << P3 << endl;
return 0;
}
Poly::Poly() : order(0)
{
coeff = new int[1];
coeff[0] = 1;
}
Poly::Poly(int Order, int Default) : order(Order)
{
coeff = new int[order+1];
for (int i = 0; i < order+1; i++){
coeff[i] = Default;
}
}
Poly::Poly(int Order, int *Coeff) : order(Order), coeff(Coeff)
{
}
Poly::Poly(const Poly & copy)
{
order = copy.getOrder();
coeff = new int[order+1];
for (int i = 0; i < order+1; i++){
coeff[i] = copy.get()[i];
}
}
Poly::~Poly()
{
//if(coeff){
//delete [] coeff;
//}
}
void Poly::set()
{
cout << "Enter your coefficients:\n";
for (int i = 0; i < order+1; i++){
cin >> coeff[i];
}
}
void Poly::set(int *Coeff, int size)
{
delete [] coeff;
coeff = new int[size];
order = size;
for (int i = 0; i < order+1; i++){
coeff[i] = Coeff[i];
}
}
int Poly::getOrder() const
{
return order;
}
int* Poly::get() const
{
return coeff;
}
Poly Poly::operator +(const Poly &rhs)
{
int length = max(order+1, rhs.getOrder()+1);
int *answer = new int[length];
for (int i = 0; i < length + 1; i++){
answer[i] = coeff[i] + rhs.get()[i];
}
if (order > rhs.getOrder()){
for (int i = order+1 - rhs.getOrder()+1 ; i < order+1; i++){
answer[i] = coeff[i];
}
}
else if (order < rhs.getOrder()){
for (int i = rhs.getOrder()+1 - order+1; i < rhs.getOrder()+1; i++){
answer[i] = rhs.get()[i];
}
}
return Poly(length-1, answer);
}
Poly Poly::operator -(const Poly &rhs)
{
int length = max(order+1, rhs.getOrder()+1);
int *answer = new int[length];
for (int i = 0; i < order+1 && i < rhs.getOrder() + 1; i++){
answer[i] = coeff[i] - rhs.get()[i];
}
if (order > rhs.getOrder()){
for (int i = order+1-rhs.getOrder()+1; i < order+1; i++){
answer[i] = coeff[i];
}
}
else if (order < rhs.getOrder()){
for (int i = rhs.getOrder()+1 - order+1; i < rhs.getOrder()+1; i++){
answer[i] = 0 - rhs.get()[i];
}
}
return Poly(length-1, answer);
}
Poly Poly::operator *(const int scale)
{
int *answer = new int[order+1];
for (int i = 0; i < order+1; i++){
answer[i] = coeff[i] * scale;
}
return Poly(order, answer);
}
Poly Poly::operator *(const Poly &rhs)
{
int *shorter = NULL;
int *longer = NULL;
int s = 0;
int l = 0;
if(order < rhs.getOrder()){
shorter = coeff;
s = order;
longer = rhs.coeff;
l = rhs.order;
} else {
shorter = rhs.coeff;
s = rhs.order;
longer = coeff;
l = order;
}
Poly sum = Poly(l, longer) * shorter[0];
int *prod;
int nl;
for (int i = 1; i <= s; i++){
nl = l + i;
prod = new int[nl + 1];
for(int j = 0; j < i; j++){
prod[j] = 0;
}
for(int k = 0; k <= l; k++){
prod[k+i] = shorter[i] * longer[k];
}
sum = sum + Poly(nl, prod);
}
return sum;
}
bool Poly::operator ==(const Poly &rhs)
{
bool result;
if (order == rhs.order){
result = true;
for(int i = 0; i<order+1; i++){
if (coeff[i] != rhs.get()[i]){
result = false;
}
}
}else result = false;
return result;
}
const int& Poly::operator[](int access) const
{
return coeff[order + 1 - access];
}
int& Poly::operator [](int access)
{
return coeff[order + 1 - access];
}
int Poly::operator ()(int x)
{
int total = 0;
for(int i = 0; i < order + 1; i++){
total += coeff[i] * pow(x, i);
}
return total;
}
Poly &Poly::operator =(const Poly &rhs)
{
order = rhs.getOrder();
coeff = rhs.get();
return *this;
}
ostream& operator <<(ostream & Out, const Poly &rhs)
{
Out << rhs.get()[rhs.getOrder()] << "x^" << rhs.getOrder(); //first
for (int i = rhs.getOrder()-1; i > 0; i--){
if (rhs.get()[i] < 0 || rhs.get()[i] > 1) {
if(rhs.get()[i] > 0){
Out << " + ";
}
Out << rhs.get()[i] << "x^" << i << " ";
}else if (rhs.get()[i] == 1){
Out << "+ x ";
}else if (rhs.get()[i] == 1){
Out << "- x";
}
}
if (rhs.get()[rhs.getOrder() - rhs.getOrder()] > 0) {
Out << " + " << rhs.get()[rhs.getOrder() - rhs.getOrder()]; //last
}else Out << rhs.get()[rhs.getOrder() - rhs.getOrder()]; //last
Out << endl;
return Out;
}
`
Here is my current output. The answer I keep getting is half of the correct answer but I can't seem to get the first half.
P1: 2x^4 + 3x^3 -12x^2 + x -19
P2: 2x^7 + 5x^5 -7x^2 + x -19
P1 * P2: -114x^5 + 49x^4 -76x^3 + 362x^2 -38x^1 + 361
Any help is appreciated. Thank you
First, stop manually managing memory. Replace order and coeff with std::vector<int> values;. This bundles size() for order, and handles memory management for you.
If you don't know how to use std::vector yet, learn: it is far easier than learning how to write your own Poly class.
The next step in implementing Poly * Poly is to implement Poly& operator*=( Poly const& rhs );
The final step is friend Poly operator*( Poly lhs, Poly const& rhs ) { return std::move(lhs*=rhs); } There is little reason to use more than one line, and it can be inline.
That leaves operator*=.
The first step in implementing *= it so implement operator+(Poly const&, Poly const&), again via Poly& operator+=(Poly const&). As addition is easy, I will leave that to you.
The next step is scalar multiplication. Implement Poly& operator*=(int), and from it friend Poly operator*(Poly lhs, int x) { return std::move( lhs*=x ); } and friend Poly operator*(int x, Poly rhs) { return std::move( rhs*= x ); }. This is called 'scalar multiplication'.
Once we have those, *= becomes easy.
Store a copy of our initial value. (Poly init = std::move(*this);)
Create a return value (empty).
For each coefficient on the right hand side, do retval += coeff * init;
return *this = std::move(retval);
This is a sketch of the solution. To solve it really, you'll want to implement tests at each phase. Because I implemented *= in terms of other operations, testing each of those operations to make sure they work is key to having a debuggable *=. Then you test *=, then you test *, and then you are done.
If you are compiling in a compliant C++ compiler, a nice thing about using std::vector is that your default copy, move, assign and move-assign operations do the right thing, as does your default destructor. Seek to manage resources with specialized resource management classes, and follow The Rule of Zero, and you will have less pain.
Note that the above *= is not much easier to write than *, but in general *= is easier, so you should get into the habit anyhow.
Finally, note that this allocates more memory than is required, and is not optimal. It is, however, easy to get correct. After you have something like the above implemented, you can make it more optimal a number of ways. You can use karatsuba multiplication, you could use expression templates to avoid intermediate multiplication on the result += coeff * init; lines, you could reserve the right amount of space in result, or you could start playing with indexes manually.
The first step should be correctness, because if you have a correct algorithm you can at the very least use it to test your more optimal (trickier) algorithms.
Your code (for the polynomial multiplication) contains no comments and is rather opaque and hard to read/understand. So, I refuse to compile it (mentally) and can only guess, but it seems you don't know how multiplication of polynomials is defined, since your product polynomial is set to have order = min(order(A), order(B)) + 1, while correct is order(A)+order(B).
I suggest, you
1 make sure you understand polynomial multiplication before starting to code
2 write clear code with minimal instructions and useful comments (or better: useful variable names)
3 manage memory via the C++ standard library (using std::vector<>)
4 structure your code (write polynomial& polynomial::operator+=(polynomial const&) and then define the product as a standalone (can be a friend) via
polynomial operator*(polynomial const&a,polynomial const&b)
{
auto c=a;
c*=b;
return std::move(c);
}
though you may want to improve on this particular design (avoiding the re-allocation the is necessary in the above code in the c*=b operation).
I think this:
sum = sum + Poly(length, prod);
should be
sum = sum + Poly(length + i - 1, prod);
Also, the loop on i should stop at the shortest coeff array length.
Here's a modified version of the function:
Poly Poly::operator *(const Poly &rhs)
{
int *shorter = NULL;
int *longer = NULL;
int s = 0;
int l = 0;
if(order < rhs.order){
shorter = coeff;
s = order;
longer = rhs.coeff;
l = rhs.order;
} else {
shorter = rhs.coeff;
s = rhs.order;
longer = coeff;
l = order;
}
Poly sum = Poly(l, longer) * shorter[0];
int *prod;
int nl;
for (int i = 1; i <= s; i++){
nl = l + i;
prod = new int[nl + 1];
for(int j = 0; j < i; j++){
prod[j] = 0;
}
for(int k = 0; k <= l; k++){
prod[k+i] = shorter[i] * longer[k];
}
sum = sum + Poly(nl, prod);
}
return sum;
}
Note how it is based on order values rather than coeff array lengths (contrarily to the fix I indicated at the top of this answer).
If it doesn't work for you, then you may have other bugs in the code you didn't provide, or your algorithms work with array lengths, so you might have to adjust either to get things working.
Finally, as it has been said in other answers, you should use the tools provided by the standard library instead of handling array allocation by hand.