STATUS: Draft
import numpy as np
import sympy as sp
import pickle
from IPython.display import HTML
import ipywidgets as widgets
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
import pandas as pd
import itertools
# function to print latex
def renderListToLatex(e):
latex_rendering = []
for i in range(len(e)):
latex_rendering.append("$$" + sp.latex(e[i]) + "$$<br/>")
return(HTML("".join(latex_rendering[0:])))
Create needed variables
c_0, c_1, c_2, c_3, c_4, x, t, a_0, a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8, a_9, x_1, x_2, s_1, s_2 = sp.symbols('c_0, c_1, c_2, c_3, c_4, x, t, a_0, a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8, a_9, x_1, x_2, s_1, s_2')
Introduction
Recall from Solving Polynomials (7), the function $C$ that is equivalent using only $m_2$ and $m_3$.
$$C(m_2, m_3) \equiv(-1)^{m_3 + 1} \frac{(2 m_{2} + 3 m_{3})!}{(1 + m_{2} + 2 m_{3})!m_2!m_3!} \frac{c_0^{1 + m_{2} + 2 m_{3}} c_2^{m_2} c_3^{m_3} }{c_1^{2 m_{2} + 3 m_{3} + 1}}$$Recall the formula to get values of this this function.
def C(m2, m3, returnCoefficientsOnly = False, returnCoefficientsOnlyWithoutSigns = False):
c_0, c_1, c_2, c_3 = sp.symbols('c_0, c_1, c_2, c_3')
s1 = (-1)**(m3 + 1)
s2 = sp.factorial(2 * m2 + 3 * m3)
s3 = sp.factorial(1 + m2 + 2 * m3) * sp.factorial(m2) * sp.factorial(m3)
s4 = c_0**(1 + m2 + 2 * m3) * c_2**m2 *c_3**m3
s5 = c_1**(2 * m2 + 3 * m3 + 1)
if returnCoefficientsOnly:
s6 = s1 * (s2 / s3)
elif returnCoefficientsOnlyWithoutSigns:
s6 = (s2 / s3)
else:
s6 = s1 * (s2 / s3) * (s4 / s5)
return(s6)
Let g8 be a matrix of values.
g1 = np.arange(6)
g2 = [[C(j, i) for i in g1] for j in g1]
g3 = sp.Matrix(g2)
g3
Recall also that this value can be recovered as a sum and it is also one solution for a general cubic equation. Does it actually work in practice?
Let g4 be the equation where x is set to the sum of these values.
g4 = sp.Eq(x, np.sum(g3))
g4
Evaluate a cubic equation using a general
z = sp.symbols('z')
g5 = sp.Eq(1 + 5. * z - z**2 + 3 * z**3, 0)
g5
Let g6 be the solutions for z in g5.
g6 = sp.solve(g5, z)
g6
[-0.188828952032499, 0.261081142682916 - 1.3027289283418*I, 0.261081142682916 + 1.3027289283418*I]
Let g7 be the solutions using the derived formula, $C$.
g7 = {c_0:1, c_1:5, c_2: -1, c_3:3}
g8 = sp.N(g4.rhs.subs(g7))
g8
Note the answers are similiar.
Check another example
g9 = sp.Eq(1 + 15. * z - z**2 + 3 * z**3, 0)
g9
g10 = sp.solve(g9, z)
g10
[-0.0663151597975300, 0.199824246565432 - 2.23306359608708*I, 0.199824246565432 + 2.23306359608708*I]
Let g12 be an answer obtained from the formula $C$
g11 = {c_0:1, c_1:15, c_2: -1, c_3:3}
g12 = sp.N(g4.rhs.subs(g11))
g12
Note they are again similiar
# demonstrating a viable technology in some cases - we can solve a cubic
# equation with high school algebra - you don't need cubed roots / square roots
# note we don't get final solution
# variant
# try another examle
# note we are looking at non complex solutions
g11 = sp.Eq(1 + 3. * z - 4 * z**2 + 5 * z**3, 0)
g11
g12 = sp.solve(g11, z)
g12
[-0.236610107250854, 0.518305053625427 - 0.75936308841047*I, 0.518305053625427 + 0.75936308841047*I]
g13 = {c_0:1, c_1:3, c_2: -4, c_3:5}
g14 = sp.N(g4.rhs.subs(g13))
g14
# this is clearly incorrect
# why is this the case - consider the terms - denomitor has big power
# but it is a smaller number to large power makes it difficult - nature
# of certain choice - c_1 needs to be bigger than other 1
# try another varint
g15 = sp.Eq(2 + 3. * z - 7 * z**2 + z**3, 0)
g15
g16 = sp.solve(g15, z)
g16
[-0.355967716679387 + 0.e-20*I, 0.865675512845961 - 0.e-22*I, 6.49029220383343 - 0.e-21*I]
g17 = {c_0:2, c_1:3, c_2: -7, c_3:1}
g18 = sp.N(g4.rhs.subs(g17))
g18
# so very incorrect but....reconsider our method words well when coefficnt
# of z is bigger than others
# so perhaps we can transform uor equation to make this the case
# one more likely to produce solution
# let z = 1 / u
g15
# introduce new version whre z = 1 / u. Then solve for u
u = sp.symbols('u')
g19 = g15.subs(z, 1/u)
g20 = sp.Eq(sp.simplify(g19.lhs * u**3), 0)
g20
g21 = sp.solve(g20, u)
g21
[-2.80924351603682 + 0.e-23*I, 0.154076267846517 + 0.e-22*I, 1.15516724819031 - 0.e-23*I]
# and now wolve in our way
g22 = {c_0:1, c_1:-7, c_2: 3, c_3:2}
g23 = sp.N(g4.rhs.subs(g22))
g23
# so now similair. so
# now recover z
g24 = 1 / g23
g24
# this is a solution to original one. So yes our current solution doens't cover all
# solutions, but if we are flexible, we can move into position where formula
# might be ale to use this
# we don't yet know conditions as yet, but this technology does
# solve cubic equations
# need to turn back to coefficients - we know it work, time to move on