Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
reset
<function reset at 0x7f5cab46af50>
#Exercise 1:Synthetic division # Make a function long_div(L, c) that takes a list L=[a_n,_(n-1),...,a_0] of length n+1 of coefficients of a polynomial p and a number c and returns the list B of length n+1. The first n elements of B are the coefficients of the quotient (p(x))/(x-c). The last element on the list S is the remainder. Test your function on example of your choice. #//Input: #//Output: def long_div(L, c): S=[L[0]] for j in range(1,len(L)): temp=L[j]+c*L[j-1] #See Lecture 4 for the formula that defines coefficient of the quotient S.append(temp) return S L=[3,2,-5,1];# p=3x^3+2x^2-5x+1 long_div(L, 2)
[3, 8, -1, -9]
#Answer: quotient: 3x^2-4x-9; remainder: 11. Check: simplify(expand((3*x^2+8*x+11)*(x-2)+23))
3*x^3 + 2*x^2 - 5*x + 1
################# #Exercise 2. Finding integer roots of monic polynomials #(a) Make a function find_int_roots(L) that takes a list L of coefficients of a monic polynomial p with integer constant term and returns the list S of integer roots of the polynomial. If the polynomial does not have integer roots, the function returns the empty list. Test your function on the polynomial p(x)=x^4+2 x^3-x^2-8 x-12. Find all integer roots. #Your code will be more readable if you define first a function that evaluates a polynomial def poly_val(A,c):# See pseudocode in Lecture 1 n=len(A) val=A[0] for j in range(1,n): val=(val*c+A[j]) return val def find_int_roots(L): #L=[1,a_(n-1),...,a_0],a_0 must be an integer divs_pos=divisors(L[-1])# L[-1] is the last element on the list divs_neg=[-c for c in divs_pos] divs=divs_pos + divs_neg int_roots=[] for c in divs: if poly_val(L,c)==0: int_roots.append(c) return int_roots
poly_val([1,2,-1,-8,-12],1)
-18
L=[1,2,-1,-8,-12];find_int_roots(L) #Answer to example in (a):integer zeros of p(x)=x^4+2*x^3-x^2-8*x-12 are
[2, -2]
#(b) Use your function constructed in Exercise 1 to find the polynomial p(x)/((x-2)(x+2)). #I use the function: p(x)=x^4+2 x^3-x^2-8 x-12 L=[1,2,-1,-8,-12] L1=long_div(L,2);L1
[1, 4, 3, -10, -28]
L2=long_div(L1,-2);L2 #Answer to (b): p(x)/(x^2-4)=?.
[1, 2, -5, -16, -8]
#Exercise 3 (manual). Describe a set of points in the pq-plane that does not lie on any tangent to the envelope. What does it mean for the corresponding quadratic equations? #There is no real root? #It has infinity quadratic equations reset var('t c') p=x^3+3*x^2-2*x+4 q=p.sub(x=t-c);q
<function reset at 0x7fef23eeef50> (t, c)
Error in lines 4-4 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "sage/structure/element.pyx", line 489, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4599) return self.getattr_from_category(name) File "sage/structure/element.pyx", line 502, in sage.structure.element.Element.getattr_from_category (build/cythonized/sage/structure/element.c:4708) return getattr_from_other_class(self, cls, name) File "sage/cpython/getattr.pyx", line 394, in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:2607) raise AttributeError(dummy_error_message) AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'sub'
r=q.expand();r
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 'q' is not defined
#########################
##REMARK. #You can ask SageMath to construct the list of polynomial coefficients using more involved structure called polynomial ring var('x') _.<x> = PolynomialRing(RR)#This line says that you will make polynomials with real coefficients in powers of the variable x p=x^3-2*x^2+3*x-7 L=p.coefficients();L# This command returns the list of coefficients of p
x [-7.00000000000000, 3.00000000000000, -2.00000000000000, 1.00000000000000]
L.reverse();L# This command reverses the order of the list elements
[1.00000000000000, -2.00000000000000, 3.00000000000000, -7.00000000000000]
#With these commands you can modify the codes of several exercises in assignments. For example, synthetic_div. //Input: polynomial q with real coefficients and a real number c //Output: coefficients (or approximations) of the polynomial q/(x-c) and the remainder def long_div2(q, c): L=q.?();L.reverse() S=[L[0]] for j in ?: temp=? S.append(temp) return S synthetic_div2(p,2)
[1.00000000000000, 0.000000000000000, 3.00000000000000, -1.00000000000000]
#Result: (x^3-2*x^2+3*x-7)/(x-2)=x^2+3,remainder -1 #Check (with SageMath "black box" command) f(x)=x^3-2*x^2+3*x-7 g(x)=x-2 f.maxima_methods().divide(g)
[x^2 + 3, -1]
#You do not need poly_val function when you work with polynomial instead of the list of its coefficients. Here is p(2): p.subs(x=2)
-1.00000000000000
#########################
#Exercise 4. Make a function remove_xsquared(L) that takes a polynomial with real coefficients and returns the number c such that substitution x = t-c eliminates the quadratic term in p. Check your code on the polynomial p(x)=x^3+3.5 x^(2)-2.1*x+4.0. #I experimented with SageMath commands for this task interactively: var('t c') q=p.subs(x=t-c);r=q.expand();r.simplify()
(t, c) -1.0*c^3 + 3.0*c^2*t - 3.0*c*t^2 + 1.0*t^3 - 2.0*c^2 + 4.0*c*t - 2.0*t^2 - 3.0*c + 3.0*t - 7.0
r.coefficient(t,n=2)# What does this comman do?
-3.00000000000000*c - 2.00000000000000
c=-2/3;r.subs(c=-2/3)# No term with t^2. It works.
1.00000000000000*t^3 + 1.66666666666667*t - 5.59259259259259
#Now it is your turn to combine the commands into the function remove_xsquared(q) #//Input: #//Output: def remove_xsquared(q): var('t c') r=q.subs(x=t-c) ...
##########################
#Exercise 5. Make a function roots_of_unity(n) that takes a natural number n and returns a list of all n^th roots of unity. Use your function for n = 7 and plot corresponding regular heptagon inscribed into the unit circle. Note that a heptagon is not constructible polygon, that is, it cannot be constructed with compass and straightedge. #// #// def roots_of_unity(n): L=[N(cos(2*pi*k/n)+I*sin(2*pi*k/n)) for k in range(n)]#Encode the formula for roots of unity from Lecture 4 return L roots_of_unity(7);L
[1.00000000000000, 0.623489801858734 + 0.781831482468030*I, -0.222520933956314 + 0.974927912181824*I, -0.900968867902419 + 0.433883739117558*I, -0.900968867902419 - 0.433883739117558*I, -0.222520933956314 - 0.974927912181824*I, 0.623489801858734 - 0.781831482468030*I] [1.00000000000000, 0.623489801858734 + 0.781831482468030*I, -0.222520933956314 + 0.974927912181824*I, -0.900968867902419 + 0.433883739117558*I, -0.900968867902419 - 0.433883739117558*I, -0.222520933956314 - 0.974927912181824*I, 0.623489801858734 - 0.781831482468030*I]
[(L[j].real(),L[j].imag())for j in range(7)];S
[(1.00000000000000, 0), (0.623489801858734, 0.781831482468030), (-0.222520933956314, 0.974927912181824), (-0.900968867902419, 0.433883739117558), (-0.900968867902419, -0.433883739117558), (-0.222520933956314, -0.974927912181824), (0.623489801858734, -0.781831482468030)]
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 'S' is not defined
point(S,aspect_ratio=1,size=30)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 'S' is not defined
#Use the function for k=7 and plot the heptagon using the roots of unity to specify the coordinates of the vertices.
############################
#Mini project #Follow the directions in Lecture 4.
︠6ec9afc3-e934-42bb-89af-d803aa451cef︠ ︠27622957-6ee4-4455-9efe-9a2c71996845︠