1414 * Make sure to put a 0 coefficient in place of powers that are not used.
1515 *
1616 * Current features:
17- * o Print a human readable representation of a polynomial
17+ * o Print a human readable string of a polynomial of any variable (default of x)
1818 * o Evaluate a polynomial at any real number
1919 * o Return the degree of a polynomial
2020 * o Return the coefficients of a polynomial
21+ * o Return the variable of a polynomial
22+ * o Set the variable of an instantiated polynomial
2123 * o Polynomial differentiation (exact)
2224 * o Polynomial integration (indefinite integral)
2325 * o Polynomial addition
2426 * o Polynomial multiplication
2527 *
26- * Example :
28+ * Examples :
2729 * $polynomial = new Polynomial([1, -8, 12, 3]);
2830 * echo $polynomial; // prints 'x³ - 8x² + 12x + 3'
2931 * echo $polynomial(4); // prints -31
3335 * echo $polynomial->integrate(); // prints '0.25x⁴ - 2.6666666666667x³ + 6x² + 3x'
3436 * echo $polynomial->add($polynomial); // prints '2x³ - 16x² + 24x + 6'
3537 * echo $polynomial->multiply($polynomial); // prints 'x⁶ - 16x⁵ + 88x⁴ - 186x³ + 96x² + 72x + 9'
38+ * echo $polynomial->getVariable(); // prints 'x'
39+ * $polynomial->setVariable("r");
40+ * echo $polynomial; // prints 'r³ - 8r² + 12r + 3'
3641 *
3742 * https://en.wikipedia.org/wiki/Polynomial
3843 */
3944class Polynomial
4045{
4146 private $ degree ;
4247 private $ coefficients ;
48+ private $ variable ;
4349
4450 /**
4551 * @var array Unicode characters for exponents
@@ -54,7 +60,7 @@ class Polynomial
5460 * Example: new Polynomial([1, 2, 3]) will create
5561 * a polynomial that looks like x² + 2x + 3.
5662 */
57- public function __construct (array $ coefficients )
63+ public function __construct (array $ coefficients, $ variable = " x " )
5864 {
5965 // Remove coefficients that are leading zeros
6066 for ($ i = 0 ; $ i < count ($ coefficients ); $ i ++) {
@@ -69,6 +75,7 @@ public function __construct(array $coefficients)
6975
7076 $ this ->degree = count ($ coefficients ) - 1 ;
7177 $ this ->coefficients = $ coefficients ;
78+ $ this ->variable = $ variable ;
7279 }
7380
7481 /**
@@ -79,8 +86,10 @@ public function __construct(array $coefficients)
7986 *
8087 * @return string A human readable representation of the polynomial
8188 */
82- public function __toString ()
89+ public function __toString (): string
8390 {
91+ $ variable = $ this ->variable ;
92+
8493 // Start with an empty polynomial
8594 $ polynomial = '' ;
8695
@@ -111,19 +120,19 @@ public function __toString()
111120 $ coefficient = '' ;
112121 }
113122
114- // Generate the $term string. No x term if power = 0.
123+ // Generate the $term string. No $variable term if power = 0.
115124 if ($ power == 0 ) {
116125 $ term = "{$ sign } {$ coefficient }" ;
117126 } else {
118- $ term = "{$ sign } {$ coefficient }x {$ exponent } " ;
127+ $ term = "{$ sign } {$ coefficient }{ $ variable } {$ exponent } " ;
119128 }
120129
121130 // Add the current term to the polynomial
122131 $ polynomial .= $ term ;
123132 }
124133
125134 // Cleanup front and back; drop redundant ¹ and ⁰ terms from monomials
126- $ polynomial = trim (str_replace ([' x ¹ ' ,' x ⁰ ' ], ' x ' , $ polynomial ), '+ ' );
135+ $ polynomial = trim (str_replace ([$ variable . ' ¹ ' , $ variable . ' ⁰ ' ], $ variable . ' ' , $ polynomial ), '+ ' );
127136 $ polynomial = preg_replace ('/^\-\s/ ' , '- ' , $ polynomial );
128137
129138 $ polynomial = ($ polynomial !== '' ) ? $ polynomial : '0 ' ;
@@ -142,7 +151,7 @@ public function __toString()
142151 *
143152 * @return number The result of our polynomial evaluated at $x₀
144153 */
145- public function __invoke ($ x₀ )
154+ public function __invoke ($ x₀ ): float
146155 {
147156 // Set object parameters as local variables so they can be used with the use function
148157 $ degree = $ this ->degree ;
@@ -186,14 +195,34 @@ public function getCoefficients(): array
186195 return $ this ->coefficients ;
187196 }
188197
198+ /**
199+ * Getter method for the dependent variable of a polynomial
200+ *
201+ * @return string The dependent variable of a polynomial object
202+ */
203+ public function getVariable (): string
204+ {
205+ return $ this ->variable ;
206+ }
207+
208+ /**
209+ * Setter method for the dependent variable of a polynomial
210+ *
211+ * @param string The new dependent variable of a polynomial object
212+ */
213+ public function setVariable (string $ variable )
214+ {
215+ $ this ->variable = $ variable ;
216+ }
217+
189218 /**
190219 * Calculate the derivative of a polynomial and return it as a new polynomial
191220 * Example: $polynomial = new Polynomial([1, -8, 12, 3]); // x³ - 8x² + 12x + 3
192221 * $derivative = $polynomial->differentiate(); // 3x² - 16x + 12
193222 *
194223 * @return object The derivative of our polynomial object, also a polynomial object
195224 */
196- public function differentiate ()
225+ public function differentiate (): Polynomial
197226 {
198227 $ derivativeCoefficients = []; // Start with empty set of coefficients
199228
@@ -217,7 +246,7 @@ public function differentiate()
217246 *
218247 * @return object The integral of our polynomial object, also a polynomial object
219248 */
220- public function integrate ()
249+ public function integrate (): Polynomial
221250 {
222251 $ integralCoefficients = []; // Start with empty set of coefficients
223252
@@ -241,7 +270,7 @@ public function integrate()
241270 *
242271 * @return object The sum of our polynomial objects, also a polynomial object
243272 */
244- public function add (Polynomial $ polynomial )
273+ public function add (Polynomial $ polynomial ): Polynomial
245274 {
246275 // Calculate the degree of the sum of the polynomials
247276 $ sumDegree = max ($ this ->degree , $ polynomial ->degree );
@@ -280,7 +309,7 @@ public function add(Polynomial $polynomial)
280309 *
281310 * @return object The product of our polynomial objects, also a polynomial object
282311 */
283- public function multiply (Polynomial $ polynomial )
312+ public function multiply (Polynomial $ polynomial ): Polynomial
284313 {
285314 // Calculate the degree of the product of the polynomials
286315 $ productDegree = $ this ->degree + $ polynomial ->degree ;
0 commit comments