Showing posts with label coefficients. Show all posts
Showing posts with label coefficients. Show all posts

Sunday, May 19, 2024

Python and Calculator Basic: Transforming Quadratic Polynomials

Python and Calculator Basic: Transforming Quadratic Polynomials

Calculators: Casio fx-CG 50, TI-84 Plus CE


Problem


Sometimes we are called to simplify quadratic polynomials, like so:


A * x^2 + B * x + C = (S * x + T)^2 + U


where A, B, C, S, T, and U are constants, which can be complex. In this problem, we are given A, B, and C, determine S, T, and U.


Observe that:

(S * x + T)^2 + U = S^2 * x^2 + 2 * S * T * x + T^2 + U


Then, matching this to the left side:

A * x^2 + B * x + C = S^2 * x^2 + 2 * S * T * x + T^2 + U


x^2 coefficient: A = S^2 which implies that S = ±√A

x coefficient: B = 2 * S * T which implies that T = B / (2 * S)

Constant coefficient: C = T^2 + U which implies that U = C – T^2


(For today’s blog, I am just going to use the principal square root, S = √A. Taking the negative square root will also provide accurate results.)



Example: Transform x^2 + 6 * x + 8 into the form (S * x + T)^2 + U.


Note that A = 1, B = 6, and C = 8.

Then:

S = √1 = 1

T = 6 / ( 2 * 1) = 3

U = 8 – 3^2 = -1


x^2 + 6 * x + 8 = (x + 3)^2 – 1



Code: Python


This code was entered on a fx-CG 50, but it should work on all calculators and platforms with Python. No modules are needed.


Title: quadtrans.py

711 bytes


from math import *

print(“Quadratic \nTransformation”)

print(“1. -> (s*x+t)**2+u”)

print(“2. -> a*x**2+b*x+c”)

ch=int(input(“choice? “))


if ch==1:

  print(“a*x**2+b*x+c ->”)

  a=eval(input(“a? “))

  b=eval(input(“b? “))

  c=eval(input(“c? “))

  print(“Principal Root”)

  print(“-> (s*x+t)**2+u”)

  s=a**(1/2)

  t=b/(2*s)

  u=c-t**2

  print(“s= “+str(s))

  print(“t= “+str(t))

  print(“u= “+str(u))

elif ch==2:

  print(“(s*x+t)**2+u ->”)

  s=eval(input(“s? “))

  t=eval(input(“t? “))

  u=eval(input(“u? “))

  print(“-> a*x**2+b*c+c”)

  a=s**2

  b=2*s*t

  c=t**2+u

  print(“a= “+str(a))

  print(“b= “+str(b))

  print(“c= “+str(c))

else:

  print(“Not a valid choice.”)


Basic Code: Casio fx-CG 50

Title: QUADTRNS, 316 bytes


a+bi

Menu “QUADRATIC TRANS.”, “-> (S×x+T)²+U”, 1, “-> A×x²+B×x+C”, 2


Lbl 1

ClrText

A×x²+B×x+C ->”

A”? → A

B”? → B

C”? → C

PRINCIPAL ROOT” ◢

A → S

B÷(2×S) → T

C–T² → U

ClrText

-> (S×x+T)²+U”

S=”

S ◢

T=”

T ◢

U=”

U

Stop


Lbl 2

(S×x+T)²+U ->”

S”? → S

T”? → T

U”? → U

S² → A

2×S×T → B

T²+U → C

-> A×x²+B×x+C”

A=”

A ◢

B=”

B ◢

C=”

C

Stop


Basic Code: TI-84 Plus CE

Title: QUADTRNS (318 bytes)


a+bi

Menu (“QUADRATIC TRANS.”, “-> (S*X+T)²+U”, 1, “-> A*X²+B*X+C”, 2)


Lbl 1

ClrHome

Disp “A*X²+B*X+C ->”

Prompt A, B, C

Disp “PRINCIPAL ROOT”

Wait 0.5

(A) → S

B/(2*S) → T

C–T² → U

ClrHome

Disp “-> (S*X+T)²+U”

Disp “S= “+toString(S)

Disp “T= “+toString(T)

Disp “U= “+toString(U)

Stop


Lbl 2

Disp “(S*X+T)²+U ->”

Prompt S, T, U

S² → A

2*S*T → B

T²+U → C

ClrHome

Disp “-> A*X²+B*X+C”

Disp “A= “+toString(A)

Disp “B= “+toString(B)

Disp “C= “+toString(C)

Stop



Examples


4 * x^2 + 8 * x + 36 < - > (2 * x + 2)^2 + 32


A = 4, B = 8. C = 36

S = 2, T= 2, U = 32


x^2 – 8 * x + 3 < - > (x – 4)^2 – 13


A = 1, B = -8, C = 3

S = 1, T = -4, U = -13


The program allows for complex and imaginary coefficients:


-4 * x^2 + 8 * x + 16 < - > (2i * x – 2i)^2 + 20


A = -4, B = 8, C = 16

S = 2i, T = -2i, U = 20



Hope you find this useful. Until next time,


Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Thursday, January 10, 2019

HP 42S Polynomial Operations

HP 42S Polynomial Operations

Note: These programs presented in today's blog are can also be programmed on the Free42 app and the DM 42.  The default setting, 25 memory registers, is assumed to be set.  The programs are written use R24 as a counter. 

This program uses indirect storage (R24).  The maximum order is 23.

HP 42S Program SPOLY

This program asks for the order of a polynomial and then asks the user to store the coefficients.  The order of registers are coefficients of powers of x in descending order from n to 0. 

For example, for a cubic polynomial, the coefficients are stored as such:
R01 = coefficient of x^3
R02 = coefficient of x^2
R03 = coefficient of x
R04 = constant coefficient

Program:

00 { 67-Byte Prgm }
01▸LBL "SPOLY"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1
08 +
09 1á´‡-3
10 ×
11 1
12 +
13 STO 24
14▸LBL 00
15 "X↑"
16 RCL 00
17 RCL 24
18 IP
19 -
20 1
21 +
22 ARCL ST X
23 ├" ="
24 CLX
25 PROMPT
26 STO IND 24
27 ISG 24
28 GTO 00
29 "DONE"
30 AVIEW
31 RTN
32 END

Example:  Store the polynomial p(x) = 2*x^3 - x^2 + 5*x + 7

Keystrokes:  [ XEQ ] (SPOLY)
"ORDER?" 3 [ R/S ]
"X↑3.0000=" 2 [R/S]
"X↑2.0000=" 1 [ +/- ] [R/S]
"X↑1.0000=" 5 [R/S]
"X↑0.0000=" 7  [R/S]
"DONE"

Results
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

HP 42S Program HORNER

The HORNER evaluates the polynomial p(x).  The assumes that the order of registers are coefficients of powers of x in descending order from n to 0.  The user is asked about the order of the polynomial and the value of x.

Program:

00 { 65-Byte Prgm }
01▸LBL "HORNER"
02 CLX
03 "X?"
04 PROMPT
05 STO 00
06 CLX
07 "ORDER?"
08 PROMPT
09 IP
10 1á´‡-3
11 ×
12 1
13 +
14 STO 24
15 RCL 00
16 RCL× IND 24
17 ISG 24
18▸LBL 01
19 RCL+ IND 24
20 RCL× 00
21 ISG 24
22 GTO 01
23 RCL+ IND 24
24 "P(X)="
25 AVIEW
26 RTN
27 .END.

Example:  Let p(x) be the polynomial, p(x) = 2*x^3 - x^2 + 5*x + 7
Calculate p(-3).

The coefficients are have already been stored (from last example).
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

Keystrokes:  [XEQ] (HORN)
"X?" 3 [ +/- ] [ R/S ]
"ORDER?" 3  [ R/S ]

Result:  -71.0000

HP 42S Program DX_PX

This program calculates coefficients of the derivative of the polynomial p(x), using the form:

d/dx x^n = n * x^(n-1)

The order of registers are coefficients of powers of x in descending order from n to 0. 

Program:

00 { 79-Byte Prgm }
01▸LBL "DX_PX"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1á´‡-3
08 ×
09 1
10 +
11 STO 24
12▸LBL 02
13 RCL IND 24
14 RCL 00
15 RCL 24
16 IP
17 -
18 1
19 +
20 ×
21 STO IND 24
22 "X↑"
23 RCL 00
24 RCL 24
25 IP
26 -
27 ARCL ST X
28 X<>Y
29 AVIEW
30 STOP
31 ISG 24
32 GTO 02
33 1
34 RCL+ 00
35 STO 24
36 CLX
37 STO IND 24
38 "DONE"
39 AVIEW
40 RTN
41 .END.

Example:  Calculate the derivative of p(x) = 2*x^3 - x^2 + 5*x + 7.  Assume that coefficients from the previous example.

Keystrokes:  [ XEQ ] (DX_PX)
"ORDER?" 3 [R/S]

Results:
"X↑2.0000=" 6.0000 [R/S]
"X↑1.0000=" -2.0000 [R/S]
"X↑0.0000=" 5.0000  [R/S]
"DONE"

dp/dx = 6*x^2 - 2*x + 5

Registers:
R1 = 6.0000
R2 = -2.0000
R3 = 5.0000
R4 = 0.0000

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Sunday, May 29, 2016

HP Prime: Basic CAS Commands for Polynomials and Rational Expressions

HP Prime:  Basic CAS Commands for Polynomials and Rational Expressions



Define the following variables:

poly:  a polynomial of the form a_n*x^n + a_n-1*x^(n-1) + …. + a_1*x + a_0
rat:  a rational function consisting of the polynomials p(x)/q(x)
var: variable

Simplification Modes

Before we start, I want to comment on simplification modes.  

Simplification Modes:

None:  No simplification is executed
Minimum:  Simple simplification of results.  Additional simplification may be desired.
Maximum:  Full simplification of results

Below is a comparison between Minimum and Maximum modes.



To change Simplification mode, press [Shift], [CAS] (CAS Settings), and select a Simplification settings.  Select the Simplification drop down box and select the desired mode.    

Note:  The following examples are executed in Maximum simplification mode.

Part Extraction:  Coefficients, Numerator, Denominator

Coefficients

coef:  [Toolbox], (CAS), 6. Polynomial, 2.  Coefficients

Syntax:
coef(poly, var):  returns all the coefficients of a polynomial in a vector
coef(poly, var, n): return the coefficient of a polynomial in a vector of the specific power x^n

Examples:
coef(2x^2 + 3x – 1, x) returns [2, 3, -1]
coef(2x^2 + 3x – 1, x, 2) returns 2
coef(2x^2 + 3x – 1, x, 1) returns 3
coef(2x^2 + 3x – 1, x, 0) returns -1

Numerator and Denominator

numer: [Toolbox], (CAS), 1. Algebra, 8. Extract, 1.  Numerator
denom: [Toolbox], (CAS), 1. Algebra, 8. Extract, 2. Denominator

Syntax:
numer(rat)
denom(rat)

Example:
f≔(2x^2-3)/(x^2+1)
numer(f) returns 2*x^2 – 3
denom(f) returns x^2 + 1
purge(f) \\ this is to erase f

Polynomial Creation

Create a symbolic polynomial with a list of coefficients

poly2symb: [Toolbox], (CAS), 6. Polynomial, 7.  Create, 2. Coefs→Poly

Syntax:

poly2symb(vector of coefficients, var)

Example:

poly2symb( [8, -1, 0, 6], t) returns 8*t^3 – t^2 + 6

The inverse operation is the symb2poly(poly), which can be accessed by [Toolbox], (CAS), 6. Polynomial, 7. Create, 1. Poly→Create. 

Create a polynomial from a list of roots

This involves a two-step processes:
1. pcoeff( vector of roots )
2. poly2symb( result from step 1, var )

Access pcoeff: [Toolbox], (CAS), 6. Polynomial, 7. Create, 3.  Roots → Coef

You can combine the two steps by typing:  poly2symb(pcoeff( vector of roots), var).  Simplification may be required

Example:

pcoeff([-2, 5, 0, 6]) returns [1, -9, 8, 60, 0]
poly2symb([1, -9, 8, 60, 0],t) returns t^4 – 9*t^3 +8*t^2 + 60*t

poly2symb( pcoeff([2, -5, 0, 6]), t) returns 
t^4 – 9*t^3 + 8*t^2 + 60*t

Degree of a Polynomial

degree: [Toolbox], (CAS), 6, Polynomial, 8. Algebra, 3. Degree

Syntax:

degree(poly)

Example:
f: = 4*x^3 – 2*x^2 + 8*x – 8
degree(f) returns 3

You can factor a polynomial by x^n where n is the degree of the polynomial by using factor_xn.

factor_xn:  [Toolbox], (CAS), 6. Polynomial, 8. Algebra, 4. Factor By Degree
Caution:  This command works when the Simplification mode is turned to Minimum or Off.    

Example:
(after turning Simplification to Minimum)
factor_xn(f) returns x^3 * (4 – 2/x + 8/x^2 – 8/x^3)
purge(f)

Partial Fraction of Rational Functions

partfrac: [Toolbox], (CAS), 1. Algebra, 7. Partial Fraction

Syntax:

partfrac(rat)

Example:

partfrac( (x^4 – 3*x^3)/(x^2 -1) ) returns 
x^2 -3*x + 1 - 1/(x-1) - 2/(x+1)

Determining the Number of Zeros (Roots)

The sturmab command determines the number of zeros giving an interval.

sturmab:  [Toolbox], (CAS), 6. Polynomial, 8. Algebra, 6.  Zero Count

Syntax:

sturmab(poly, var, min, max)

The min and max can be complex numbers.

Examples:

sturmab(x^3 – 4*x^2 + 6*x – 4, x, -5, 5) returns 1  \\ 1 real root
sturmab(x^3 – 4*x^2 + 6*x – 4, x, -5-5*i, 5+5*i) returns 3  \\ 1 real, 2 complex roots

Polynomial Functions

Path: [Toolbox], (CAS), 6, Polynomial, 9. Special, then:
4.  Hermite \\ Syntax:  hermite(n)
7.  Legendre \\ Syntax:  legendre(n)
8.  Chebyshev Tn (1st Kind)  \\ Syntax:  tchebyshev1(n)
9.  Chebyshev Un (2nd Kind)  \\ Syntax:  tchebyshev2(n)

Where n is an integer.  You can specify a variable by adding var as a second argument. (x is the default variable)

Examples:
hermite(4) returns 16*x^4 – 48*x^2 + 12
legendre(4) returns (35*x^4 – 30*x^3 + 3)/8
tchebyshev1(4) returns 8*x^4 – 8*x^2 + 1
tchebyshev2(4) returns 16*x^4 – 12*x^2 + 1

This covers some of the basic CAS commands for polynomials and rational functions.  Hope you find this helpful,

Eddie


This blog is property of Edward Shore, 2016.








My International Casio Collection (So Far)

 Pictured: Pic 1:  Casio fx-991CN X (China)  Pic 2:  Casio fx-570SPX II Iberia (Spain) Pic 3:  Casio fx-92 Collège (France) All original co...