Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The main goal of this project is to define and implement a C++ class to compute

ID: 3587731 • Letter: T

Question

The main goal of this project is to define and implement a C++ class to compute with truncated power series. The UML class diagram of the Series class is drawn below.

Series

degree

coefficients

+ Series()

+ to string()

+ copy()

+ operator=

+ operator+

+ operator-

+ operator*

+ inverse()

+ operator/

+ ~Series()

The class Series represents a truncated power series with doubles as coefficients. A series s(t) truncated at degree d has d+1 coefficients and is written as s0+s1t+s2t 2+· · ·+sdt d+O(t d+1), where the coefficients sk are doubles, for k going from 0 to d. This d is called the degree of the series. The degree and coefficients of a series are private data attributes. The degree is of type size t. The type of the coefficients is vector, using the vector of the standard template library. The class has three constructors. The first constructor takes only the degree as input parameter and returns a series with zero coefficients. The second constructor takes as input the degree and the constant leading coefficient of the series. The third constructor also has as input the degree and allows to specify all coefficients of the series. The second parameter is the address of a sequence of doubles, as many as the degree plus one (that is: d + 1) of the series. The copy method copies the degree and the coefficients to the object the copy method is applied to. This copy method is used in the definition of the assignment operator. The addition and subtraction operators are straightforward, defined via the componentwise addition and subtraction. The multiplication of two series is the same as the multiplication of two polynomials, except that coefficients higher than the degree of the series need not be computed. The multiplicative inverse of a series is defined whenever the constant leading coefficient of the series is nonzero. The inverse x(t) of s(t) is defined via x(t) s(t) = 1 + O(t d+1). If the series s(t) has coefficients sk, then the coefficients xk of the inverse x(t) are computed as x0 = 1/s0, x1 = (s1x0)/s0, x2 = (s1x1 + s2x0)/s0, . . . xd = (s1xd1 + s2xd2 + · · · + sdx0)/s0. The division operator can then be implemented as the product of the numerator by the inverse of the denominator. The destructor in the class, defined by the ~Series method, sets the degree of the series to 1.

The arithmetical operations in the class Series are tested on random series of a fixed degree. In your implementation of the arithmetical operations you may therefore assume that both operands of the operators are series of the same degree. If the test program is saved in the current working directory under the name test series, then a session at the command prompt $ in a terminal window could run as follows: $ ./test_series Testing the arithmetic on truncated power series ... Give the degree : 3 A random series of degree 3, called A, series A : 8.4019e-01-7.8310e-01*t^1-9.1165e-01*t^2+3.3522e-01*t^3 + O(t^4) and another random series of degree 3, called B, series B : -2.7777e-01-4.7740e-01*t^1-3.6478e-01*t^2+9.5223e-01*t^3 + O(t^4) A + B : 5.6241e-01-1.2605e+00*t^1-1.2764e+00*t^2+1.2875e+00*t^3 + O(t^4) A + B - B : 8.4019e-01-7.8310e-01*t^1-9.1165e-01*t^2+3.3522e-01*t^3 + O(t^4) A*B : -2.3338e-01-1.8358e-01*t^1+3.2059e-01*t^2+1.4278e+00*t^3 + O(t^4) A*B/B : 8.4019e-01-7.8310e-01*t^1-9.1165e-01*t^2+3.3522e-01*t^3 + O(t^4) After the user is prompted for a degree, series with coefficients uniformly distributed in the interval [1, +1] are generated. Each time the program runs, the user should see different coefficients. The correctness of the operations follows from inspection: we see that A + B - B equals A and that A*B/B equals A. The test program then continues with a test on the square root computation on a random series of degree four. To compute the square root of a series s(t), five steps of Newton’s method are applied: f = s(t) x x, x = f /(2 x), xk+1 = xk + x, k = 0, 1, . . . , 4, where x0 = s(t), i.e.: the sequence starts at s(t). The program from above then continues in an example session as below: Testing Newton’s method on the square root of a random series : x = 8.4019e-01-7.8310e-01*t^1-9.1165e-01*t^2+3.3522e-01*t^3-2.7777e-01*t^4 + O(t^5) at step 0 : fx = 1.3427e-01+5.3280e-01*t^1+7.0181e-03*t^2-1.6559e+00*t^3-1.1708e-01*t^4 + O(t^5) dx = 7.9906e-02+3.9155e-01*t^1+4.5582e-01*t^2-1.6761e-01*t^3+1.3889e-01*t^4 + O(t^5) x = 9.2009e-01-3.9155e-01*t^1-4.5582e-01*t^2+1.6761e-01*t^3-1.3889e-01*t^4 + O(t^5) at step 1 : fx = -6.3850e-03-6.2574e-02*t^1-2.2616e-01*t^2-3.3017e-01*t^3-9.8715e-02*t^4 + O(t^5) dx = -3.4698e-03-3.5481e-02*t^1-1.3972e-01*t^2-2.5582e-01*t^3-2.2579e-01*t^4 + O(t^5) x = 9.1662e-01-4.2703e-01*t^1-5.9554e-01*t^2-8.8213e-02*t^3-3.6468e-01*t^4 + O(t^5) at step 2 : fx = -1.2039e-05-2.4622e-04*t^1-2.2285e-03*t^2-1.1690e-02*t^3-3.9241e-02*t^4 + O(t^5) dx = -6.5671e-06-1.3737e-04*t^1-1.2838e-03*t^2-7.0646e-03*t^3-2.5547e-02*t^4 + O(t^5) x = 9.1662e-01-4.2717e-01*t^1-5.9682e-01*t^2-9.5277e-02*t^3-3.9022e-01*t^4 + O(t^5) at step 3 : fx = -4.3127e-11-1.8042e-09*t^1-3.5732e-08*t^2-4.4551e-07*t^3-3.9247e-06*t^4 + O(t^5) dx = -2.3525e-11-9.9514e-10*t^1-1.9970e-08*t^2-2.5297e-07*t^3-2.2719e-06*t^4 + O(t^5) x = 9.1662e-01-4.2717e-01*t^1-5.9682e-01*t^2-9.5277e-02*t^3-3.9022e-01*t^4 + O(t^5) at step 4 : fx = 0.0000e+00+0.0000e+00*t^1+0.0000e+00*t^2+0.0000e+00*t^3-9.4369e-16*t^4 + O(t^5) dx = 0.0000e+00+0.0000e+00*t^1+0.0000e+00*t^2+0.0000e+00*t^3-5.1477e-16*t^4 + O(t^5) x = 9.1662e-01-4.2717e-01*t^1-5.9682e-01*t^2-9.5277e-02*t^3-3.9022e-01*t^4 + O(t^5) sqrt(x) = 9.1662e-01-4.2717e-01*t^1-5.9682e-01*t^2-9.5277e-02*t^3-3.9022e-01*t^4 + O(t^5) sqrt(x)**2 = 8.4019e-01-7.8310e-01*t^1-9.1165e-01*t^2+3.3522e-01*t^3-2.7777e-01*t^4 + O(t^5)

Explanation / Answer

#include<iostream>

#include<vector>

using namespace std;

class Series

{

private:

size_t degree;

vector<double> coefficients; // degree +1

public:

Series(int d)

{

this->degree = d;

for (int i = 0; i <= d; i++)

coefficients.push_back(0);

}

Series(int d, int sk)

{

this->degree = d;

for (int i = 0; i < d; i++)

coefficients.push_back(0);

coefficients.push_back(sk);

}

Series(int d, int *s)

{

this->degree = d;

for (int i = 0; i <= d; i++)

coefficients.push_back(s[i]);

}

Series& copy(const Series &sr)

{

this->degree = sr.degree;

for (int i = 0; i <= sr.degree; i++)

this->coefficients.push_back(sr.coefficients[i]);

return *this;

}

Series& operator=(const Series &sr)

{

return this->copy(sr);

}

Series& operator+(const Series &sr)

{

int degree = (this->degree> sr.degree)?this->degree:sr.degree;

Series res(degree);

for (int i = 0; i < degree; i++)

res.coefficients[i] = this->coefficients[i] + sr.coefficients[i];

return res;

}

Series& operator-(const Series &sr)

{

int degree = (this->degree> sr.degree) ? this->degree : sr.degree;

Series res(degree);

for (int i = 0; i < degree; i++)

res.coefficients[i] = this->coefficients[i] - sr.coefficients[i];

return res;

}

Series& operator*(const Series& sr)

{

}

Series& operator/(const Series& sr)

{

}

Series& inverse()

{

Series res(this->degree);

for (int i = 0; i < this->degree; i++)

{

double val = 0;

for (int j = 0; j < i; j++)

val += this->coefficients[i] * j;

res.coefficients[i] = (this->coefficients[i] ) / (this->coefficients[0]);

}

}

~Series()

{

degree = -1;

}

};