Select Git revision
Polynomial.cpp 13.63 KiB
#include <cmath>
#include <sstream>
#include <stack>
#include "Polynomial.h"
using namespace std;
const char Polynomial::coefName[3] = {'x', 'y', 'z'};
Polynomial::Polynomial(double coef,
int powerX,
int powerY,
int powerZ){
nMon = 1;
mon = new monomial_t[1];
mon[0].coef = coef;
mon[0].power[0] = powerX;
mon[0].power[1] = powerY;
mon[0].power[2] = powerZ;
}
Polynomial::Polynomial(const Polynomial& other){
nMon = other.nMon;
mon = copyMonomial(other.mon, nMon);
}
Polynomial::Polynomial(void){
nMon = 0;
mon = NULL;
}
Polynomial::~Polynomial(void){
if(mon)
delete[] mon;
}
void Polynomial::derivative(int dim){
// Take derivative //
for(int i = 0; i < nMon; i++){
mon[i].coef *= mon[i].power[dim];
mon[i].power[dim] -= 1;
}
// Remove zero monomials //
int N = 0;
stack<monomial_t*> s;
for(int i = 0; i < nMon; i++){
if(mon[i].coef != 0.0){
s.push(&mon[i]);
N++;
}
}
// If no monomial any more ---> return zero polynomial
if(!N){
delete[] mon;
mon = zeroPolynomial();
nMon = 1;
return;
}
// If no zero found ---> return;
if(N == nMon)
return;
// Else, remove them //