A blog is that is all about mathematics and calculators, two of my passions in life.
Saturday, September 30, 2017
Adventures in Python: Installing and Matplotlib Package
Thursday, August 21, 2014
Pythonista 2.7 and HP 35S: Given roots of a polynomial, find the coefficients of a polynomial
General
Variables:
Number of Roots: N
Roots: R, S, T, U
Coefficients: A, B, C, D, E
N = 2, roots R and S:
(x - R) * (x - S) → A * x^2 + B * x + C
Formulas:
A = 1
B = -(R + S)
C = R * S
N = 3, roots R, S, and T:
(x - R) * (x - S) * (x - T) → A * x^3 + B * x^2 + C * x + D
Formulas:
A = 1
B = -(R + S + T)
C = R * S + R * T + S * T
N = 4, roots R, S, T, and U:
(x - R) * (x - S) * (x - T) * (x - U) → A * x^4 + B * x^3 + C * x^2 + D * x + E
Formulas:
A = 1
B = -(R + S + T + U)
C = R * S + R * T + R * U + S * T + S * U + T * U
D = -(R * S * T + R * S * U + R * T * U + S * T * U)
E = R * S * T * U
HP-35S: Coefficients To Roots
Program:
C001 LBL C
C002 SF 10 // SF, decimal point, 0
C003 NO OF ROOTS // enter message as an equation
C004 CF 10 // CF, decimal point, 0
C005 INPUT N
C006 4 // error checking
C007 x
C009 R-down
C010 2
C011 x>y?
C012 GTO C113
C013 1 // main routine
C014 STO A
C015 INPUT R
C016 INPUT S
C017 RCL N
C018 3
C019 x=y?
C020 GTO C033
C021 R-down
C022 4
C023 x=y?
C024 GTO C054
C025 RCL R // two roots
C026 RCL+ S
C027 +/-
C028 STO B
C029 RCL R
C030 RCLx S
C031 STO C
C032 GTO C101
C033 INPUT T // three roots
C034 RCL R
C035 RCL+ S
C036 RCL+ T
C037 +/-
C038 STO B
C039 RCL R
C040 RCLx S
C041 RCL R
C042 RCLx T
C043 +
C044 RCL S
C045 RCLx T
C046 +
C047 STO C
C048 RCL R
C049 RCLx S
C050 RCLx T
C051 +/-
C052 STO D
C053 GTO C101
C054 INPUT T // four roots
C055 INPUT U
C056 +
C057 RCL+ S
C058 RCL+ R
C059 +/-
C060 STO B
C061 RCL R
C062 RCLx S
C063 RCL R
C064 RCLx T
C065 +
C066 RCL R
C067 RCLx U
C068 +
C069 RCL S
C070 RCLx T
C071 +
C072 RCL S
C073 RCLx U
C074 +
C075 RCL T
C076 RCLx U
C077 +
C078 STO C
C079 RCL R
C080 RCLx S
C081 RCLx T
C082 RCL R
C083 RCLx S
C084 RCLx U
C085 +
C086 RCL R
C087 RCLx T
C088 RCLx U
C089 +
C090 RCL S
C091 RCLx T
C092 RCLx U
C093 +
C094 +/-
C095 STO D
C096 RCL R
C097 RCLx S
C098 RCLx T
C099 RCLx U
C100 STO E
C101 VIEW A // results
C102 VIEW B
C103 VIEW C
C104 RCL N
C105 3
C106 x≤y?
C107 VIEW D
C108 RCL N
C109 4
C110 x=y?
C111 VIEW E
C112 RTN
C113 0 // invoking the error condition
C114 1/x
Pythonista
Input: Enter a vector of coefficients, up to 4 roots
Output: A list of coefficients, in descending order
Note: the triple periods indicate a tab (...)
# let roots be the list of roots, up to 4
# EWS 2014-08-20
import math
roots=input('List of Roots (up to 4):')
n=len(roots)
poly=[1]
# check for order
if n==2:
...# quadratic
...poly.append(-(roots[0]+roots[1]))
...poly.append(roots[0]*roots[1])
...print('List of coefficients: ',poly)
elif n==3:
...# cubic
...temp=-(roots[0]+roots[1]+roots[2])
...poly.append(temp)
...temp=roots[0]*roots[1]+roots[0]*roots[2]+roots[1]*roots[2]
...poly.append(temp)
...temp=-roots[0]*roots[1]*roots[2]
...poly.append(temp)
...print('List of coefficients: ',poly)
elif n==4:
...# quartic
...temp=-(roots[0]+roots[1]+roots[2]+roots[3])
...poly.append(temp)
...temp=roots[0]*roots[1]+roots[0]*roots[2]+roots[0]*roots[3]+roots[1]*roots[2]+roots[1]*roots[3]+roots[2]*roots[3]
...poly.append(temp)
...temp=-(roots[0]*roots[1]*roots[2]+roots[0]*roots[1]*roots[3]+roots[0]*roots[2]*roots[3]+roots[1]*roots[2]*roots[3])
...poly.append(temp)
...temp=roots[0]*roots[1]*roots[2]*roots[3]
...poly.append(temp)
...print('List of Coefficients: ',poly)
else:
...print('Error: not a valid list')
Examples:
Quadratic:
R = 2, S = -4
A = 1, B = 2, C = -8
Cubic:
R = 3, S = -1, T = -3
A = 1, B = 1, C = -9, D = -9
Quartic:
R = 3, S = -1, T = -3, U = 4
A = 1, B = -3, C = -13, D = 27, E = 36
Eddie
This blog is property of Edward Shore. 2014
Saturday, August 16, 2014
Pythonista 2.7: List of Primes, Rotating a List, Maximum of a List, Vandermonde Matrix
List of Primes
# list of primes to n
# 8/13/2014
import math
n=input('maximum n (n>2):')
# start the list of primes
l=[2]
# test all integers from 3 to n
for k in range(3,n+1):
# set flag
x=0
for j in range(2,k-1):
# test for composite menu
if math.fmod(k,j)==0:
x=1
if x==0:
# add to list that k is prime
l.append(k)
# return list of primes
print l
Example: n = 44 returns
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43]
I am getting used to the way the range command operates and how the indices go from 0 to n-1, instead of how programming calculators have it: 0 to n.
Rotate a List to the Right
# rotate a list to the right r places
import math
l=input('list: ')
r=input('number of places: ')
n=len(l)
# rotation loop
for k in range(r):
# remember range starts at 0
# remove last element
w=l.pop()
# insert that element at position 0
l.insert(0,w)
# print rotated list
print l
Example:
List: [0, 4, -6, 8]
Number of Places: 2; return [-6, 8, 0, 4]
Number of Places: 3; return [4, -6, 8, 0]
Maximum of a List
import math
l=input('list: ')
# sort the list - without a loop
l.sort()
print l.pop()
Example: [12, 16, 28, 3, 4] returns 28
Vamdermonde Matrix
# building the Vandermonde matrix a row at a time
import math
v=input('vector: ')
n=len(v)
print('Vandermonde Matrix')
# main routine
# power
for i in range(n):
l=[]
# element build
for k in range(n):
l.append(math.pow(v[k],i))
print l
Example:
[0.8, 0.6, -0.5] returns
[1, 0.8, 0.64]
[1, 0.6, 0.36]
[1, -0.5, 0.25]
Enjoy!
Have a great weekend!
Eddie
This blog is property of Edward Shore. 2014
Thursday, July 31, 2014
Roots of the Cubic Equation - Pythonista 2.7
Roots of the Cubic Equation - Pythonista 2.7
Script:
# Cubic Equations: 2014-07-30 EWS
# need the root
import math
print('ax^3+bx^2+cx+d=0','a!=0')
a=float(input('a='))
b=float(input('b='))
c=float(input('c='))
d=float(input('d='))
# is a root 1?
test=a+b+c+d
if test==0:
r=1
if test!=0:
# first root by Newton's Method
xn=1
x1=xn-(a*xn**3+b*xn**2+c*xn+d)/(3*a*xn**2+2*b*xn+c)
while math.fabs(xn-x1)>1e-13:
xn=x1
x1=xn-(a*xn**3+b*xn**2+c*xn+d)/(3*a*xn**2+2*b*xn+c)
r=x1
print('r=',r)
# second and third roots
ap=a
bp=a*r+b
cp=a*r**2+b*r+c
disc=bp**2-4*ap*cp
if disc<0:
real=-bp/(2.*ap)
imag=math.sqrt(math.fabs(disc))/(2.*ap)
print(r)
print(real,'+/-',imag,'i')
else:
s=(-bp+math.sqrt(disc))/(2.*a)
t=(-bp-math.sqrt(disc))/(2.*a)
print(r)
print(s)
print(t)
Examples:
x^3 - 4*x^2 - 17*x + 60 = 0
a = 1, b = -4, c = -17, d = 60
Roots: 3, 4, 5
2*x^3 + x^2 - 2*x + 1 = 0
a = 2, b = 1, c = -2, d = 1
Roots (approximately): -1.43756, 0.46878 ± 0.35784i
This blog is property of Edward Shore. 2014
Friday, July 25, 2014
Pythonista 2.7: Triangle: Lengths of the side and angle given three points
Pythonista Program: fans.py
Version 2.7
Draws a triangle given three points: S, A, and B with coordinates (sx, sy), (ax, ay), and (bx, by), respectively. Lines formed are SA, SB, and AB. This program finds the lengths of SA, SB, and AB, calculates the angle between SA and SB, and draws the triangle.
Note, the modules math and canvas are used. I think the math module is universal but the canvas module seems to exclusive to Pythonista. Not 100% sure. So if you don't have the canvas module, you can omit the second part of the script out. You will just have text.
Example (see screen shot 2):
S = (10,6)
A = (24,25)
B = (12, -2)
Lengths:
SA = 23.600847442411894
SB = 8.246211251235321
AB = 29.546573405388315
Angle = 129.57940471623766°
Scrpit: fans.py
import math
# find distances and angle
# vertex
print('Vertex Point s')
sx=float(input('sx= '))
sy=float(input('sy= '))
# outer points
print('Point A')
ax=float(input('ax= '))
ay=float(input('ay= '))
print('Point B')
bx=float(input('bx= '))
by=float(input('by= '))
# calculate side lengths sa, sb, and ab
# using the hypot function
sa=math.hypot(sx-ax,sy-ay)
sb=math.hypot(sx-bx,sy-by)
ab=math.hypot(ax-bx,ay-by)
print('|SA|', sa)
print('|SB|', sb)
print('|AB|', ab)
# calcuate the angle between sa and sb
ang=math.acos((sa**2+sb**2-ab**2)/(2.*sa*sb))
ang=math.degrees(ang)
print('Angle beteeen SA & SB:', ang)
# drawing lines - Pythonista
import canvas
# set canvas of 100 x 100 to allow room
canvas.set_size(100,100)
# use pixel (50,50) as the origin
# set stroke line
canvas.set_line_width(.25)
# draw lines
# lines sa and sb are blue
canvas.set_stroke_color(0,0,1)
canvas.draw_line(sx+50,sy+50,ax+50,ay+50)
canvas.draw_line(sx+50,sy+50,bx+50,by+50)
# line ab is dark green
canvas.set_stroke_color(0,0.5,0)
canvas.draw_line(ax+50,ay+50,bx+50,by+50)
# draw axis - gray
canvas.set_stroke_color(0.5,0.5,0.5)
canvas.draw_line(0,50,100,50)
canvas.draw_line(50,0,50,100)
Kind of a little intro to Pythonista, at least for me.
Eddie
This blog is property of Edward Shore. 2014
Tuesday, July 22, 2014
My experience with Python so far (Pythonista iOS App 2.7)
Overall, my experience with Python has not been a great one, especially trying it with using iOS apps. (Update: but it's getting better - see below)
The iOS Python 3.2 app by Jonathan Hosmer crashes a lot on my iPad (3rd generation). Not worth the $2.99 in my opinion.
I also tried the pythoni3.3 iOS app by XiaoWen Huang, which is free to download but has in app purchases. Sadly, this app fails in even in giving simple instructions as the help file will tell you everything of the history and how to use it on other machines, but forget it if you want to find how to invoke the interpreter on the app itself. And it crashes if I try to erase the word "in".
Then there is Pythonista 2.7 which has a great interface. However, I am not sure if the mathematics used by this app is not solid or I lack some obvious command. For instance:
Solving 2 by 2 Systems
Solve for x and y:
Ax + By = E
Cx + Dy = F
where:
x = (D*E-B*F)/(A*D-B*C)
y = (A*F-C*E)/(A*D-B*C)
Below is the code for it:
# solving 2 x 2 systems
print('Ax+By=E, Cx+Dy=F')
A=float(input('A='))
B=float(input('B='))
C=float(input('C='))
D=float(input('D='))
E=float(input('E='))
F=float(input('F='))
det=A*D-B*C
x=(D*E-B*F)/det
y=(A*F-C*E)/det
print(det)
print(x)
print(y)
Example:
A = 2
B = 3.5
C = -1
D = 0.4
E = 0.6
F = -2
The results I get are these:
x = 1.68372093023, y = -7.90697674429
Which is fine. I get comparable results when using other calculators and the MathStudio app to verify my answer.
However, the volume of a sphere tells another story.
Volume of the Sphere
Formula = 4/3 * π * r^3
Here is the program and results for r = 3. First the screenshots:
Script:
import math
r=float(input('Radius='))
vol=4/3*math.pi*r**3
print('volume = ',vol)
r = 2.45; result Pythonista gives: 46.200654262773026
Verifying the answer with an fx-5800p and HP Prime I get: 61.60087235 (correct answer)
r = 3; result Pythonista gives: 84.82300164692441
However, my calculators return 113.0973355.
Again, am I missing something obvious? Or is Pythonista (maybe the language itself) not reliable?
(Updated) Thank you so much Bhuvanesh Bhatt!
Here is the corrected code:
import math
r=float(input('Radius='))
vol=4/3.*math.pi*r**3
print('volume = ',vol)
In version 2.7, which Pythonista runs, the slash operator (/) is treated as integer divsion. So a decimal point after the denominator is required in order to use floating divsion. In version 3, the extra decimal point is not required.
Under the updated code, I get the correct answers. Thanks once again Bhuvanesh!
Let me know your experiences in the comments below. Thanks as always, Eddie.
This blog is property of Edward Shore. 2014
RPN HP 12C: Fibonacci and Lucas Sequences
RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...