RTXI  2.1
poly.cpp
Go to the documentation of this file.
1 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 //
3 // File = poly.cpp
4 //
5 // class that implements a polynomial with
6 // real-valued coefficients
7 //
8
9 #include "poly.h"
10 #include <math.h>
11
12 //======================================================
13 // default constructor
14
16 {
17  Degree = 0;
18  Coefficient = new double[1];
19  Coefficient[0] = 0.0;
20  return;
21 };
22
23 //===================================================================
24 // copy constructor
25
27 {
28  Degree = original.Degree;
29  Coefficient = new double[Degree + 1];
30
31  for (int i = 0; i <= Degree; i++) {
32  Coefficient[i] = original.Coefficient[i];
33  }
34  return;
35 };
36
37 //===================================================================
38 // conversion constructor
39
41 {
42  Degree = original.Degree;
43  Coefficient = new double[Degree + 1];
44
45  for (int i = 0; i <= Degree; i++) {
46  Coefficient[i] = real(original.Coeff[i]);
47  }
48  return;
49 };
50
51 //===================================================================
52 // constructor for initializing a binomial
53
54 Polynomial::Polynomial(const double coeff_1, const double coeff_0)
55 {
56  Degree = 1;
57  Coefficient = new double[2];
58
59  Coefficient[0] = coeff_0;
60  Coefficient[1] = coeff_1;
61
62  return;
63 }
64
65 //=================================================================
66 // assignment operator
67
70 {
71  if (Coefficient != right.Coefficient) {
72  //-------------------------------------------------------------
73  // Get rid of old coefficient array to make way for a new one
74  // of the correct length for the new polynomial being assigned
75
76  delete[] Coefficient;
77
78  Degree = right.Degree;
79  Coefficient = new double[Degree + 1];
80
81  for (int i = 0; i <= Degree; i++) {
82  Coefficient[i] = right.Coefficient[i];
83  }
84  }
85  return *this;
86 }
87
88 //===================================================================
89 // multiply assign operator
90
93 {
94  //-----------------------------------------------------
95  // save pointer to original coefficient array so that
96  // this array can be deleted once no longer needed
97
98  double* orig_coeff = Coefficient;
99  int orig_degree = Degree;
100
101  //-------------------------------------------------------
102  // create new longer array to hold the new coefficients
103
104  Degree += right.Degree;
105  Coefficient = new double[Degree + 1];
106
107  for (int i = 0; i <= Degree; i++)
108  Coefficient[i] = 0.0;
109
110  //---------------------------------
111  // perform multiplication
112
113  for (int rgt_indx = 0; rgt_indx <= right.Degree; rgt_indx++) {
114  for (int orig_indx = 0; orig_indx <= orig_degree; orig_indx++) {
115  Coefficient[orig_indx + rgt_indx] +=
116  (orig_coeff[orig_indx] * right.Coefficient[rgt_indx]);
117  }
118  }
119
120  return *this;
121 }
122
123 //===================================================================
124 // divide assign operator
125
126 Polynomial&
128 {
129  //-----------------------------------------------------
130  // save pointer to original coefficient array so that
131  // this array can be deleted once no longer needed
132
133  double* orig_coeff = Coefficient;
134  int orig_degree = Degree;
135
136  //-------------------------------------------------------
137  // create new longer array to hold the new coefficients
138
139  Degree += right.Degree;
140  Coefficient = new double[Degree + 1];
141
142  for (int i = 0; i <= Degree; i++)
143  Coefficient[i] = 0.0;
144
145  //---------------------------------
146  // perform multiplication
147
148  for (int rgt_indx = 0; rgt_indx <= right.Degree; rgt_indx++) {
149  for (int orig_indx = 0; orig_indx <= orig_degree; orig_indx++) {
150  Coefficient[orig_indx + rgt_indx] +=
151  (orig_coeff[orig_indx] * right.Coefficient[rgt_indx]);
152  }
153  }
154
155  return *this;
156 }
157
158 //=========================================================
159 // dump polynomial to an output stream
160
161 void
162 Polynomial::DumpToStream(std::ofstream* output_stream)
163 {
164  (*output_stream) << "Degree = " << Degree << std::endl;
165
166  for (int i = Degree; i >= 0; i--) {
167  (*output_stream) << "Coeff[" << i << "] = " << Coefficient[i] << std::endl;
168  }
169  return;
170 }
171
172 //====================================================
173 //
174
175 int
177 {
178  return (Degree);
179 }
180
181 //==================================================
182 //
183
184 double
186 {
187  return Coefficient[k];
188 }
189
190 //\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$
void DumpToStream(std::ofstream *output_stream)
Definition: poly.cpp:162
Polynomial & operator/=(const Polynomial &right)
Definition: poly.cpp:127
Polynomial()
Definition: poly.cpp:15
double * Coefficient
Definition: poly.h:45
complex * Coeff
Definition: cmpxpoly.h:65
double GetCoefficient(int k)
Definition: poly.cpp:185
Polynomial & operator*=(const Polynomial &right)
Definition: poly.cpp:92
int GetDegree(void)
Definition: poly.cpp:176
double real(const complex &_z)
Definition: complex.h:98
int Degree
Definition: poly.h:44
Polynomial & operator=(const Polynomial &right)
Definition: poly.cpp:69