Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
reset
<function reset at 0x7f127d4c20c8>
#Exercise 1:Synthetic (long) 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*S[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, 11, 23]
#Answer: quotient: 3x^2+8x+11; remainder: 23. Check: simplify(expand((3*x^2+8*x+11)*(x-2)+23))# This is the given polynomial.
3*x^3 + 2*x^2 - 5*x + 1
# Remark: How to make a list of all divisors of an integer S=divisors(45);S;P=[-c for c in S];P;S+P
[1, 3, 5, 9, 15, 45] [-1, -3, -5, -9, -15, -45] [1, 3, 5, 9, 15, 45, -1, -3, -5, -9, -15, -45]
################# #Exercise 2. Finding integer roots of monic polynomials #(a) Make a function find_int_roots(A) 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 #//Input: The input is a polynomial #//Output: The output are the roots of the polynomial if it can be simplified. All roots inputted into the polynomial should return 0. def poly_val(A,c):# val=A[0] for j in range(1,len(A)): val=val*c+A[j] return val #Example poly_val([3,5,-2],1/3)# Show your own example
0
#Answer: 1/3 is the root of the polynomial p=3x^2+5x-2 #//Input:Input is a polynomial #//Output: The output are the roots of the polynomial. If the roots are put into the polynomial, then the output, both times, should be 0. 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 #Example:p=x^4+2*x^3-x^2-8*x-12 L=[1,2,-1,-8,-12];find_int_roots(L)# Show your own example #Answer to part (a): integer zeros of the polynomial p are
[2, -2]
#(b) Use the function long_division to find the polynomial p(x)/((x-2)(x+2)) # My example: p=x^4+2*x^3-x^2-8*x-12 (the polynomial used in (a) part of this exercise) # Show your own example L=[1,2,-1,-8,-12] L1=long_div(L, 2);L1 #List of coefficients of p/(x-2)
[1, 4, 7, 6, 0]
L2=long_div(L1,-2);L2 #Answer to part (b): p(x)/(x^2-4)=x^2+2x+3
[1, 2, 3, 0, 0]
######################### #Exercise 3. 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 a cubic polynomial of your choice. #I experimented with SageMath commands for this task interactively: reset var('t c') p=x^3+3.5*x^2-2.1*x+4.0 # my example q=p.subs(x=t-c);q
<function reset at 0x7f127d4c20c8> (t, c) -(c - t)^3 + 3.50000000000000*(c - t)^2 + 2.10000000000000*c - 2.10000000000000*t + 4.00000000000000
r=q.expand();r
-c^3 + 3*c^2*t - 3*c*t^2 + t^3 + 3.50000000000000*c^2 - 7.00000000000000*c*t + 3.50000000000000*t^2 + 2.10000000000000*c - 2.10000000000000*t + 4.00000000000000
coef2=r.coefficient(t,n=2);coef2 #It is simple to solve the equation coef2=0 manually. In the next cell I asked SageMath to do this. It is more tricky but will be useful later in the course.
-3*c + 3.50000000000000
B=coef2.solve(c,solution_dict=True);B #It is convenient to use disctionary for accessing solution to equations (remember this!). The list B has only one element, the dictionary with key c and value 7/6
[{c: 7/6}]
cval=B[0][c];cval
7/6
r.subs(c=cval)# It works, no term with t^2
t^3 - 6.18333333333334*t + 9.62592592592592
# Assemble my interactive solution into a function. L must be a list of coefficients of a cubic polynoimial with quadratic term present. Therefore len(L)=4 and L[1] is a nonzero number. # //Input: # //Output: var('t c') def remove_xsquared(L): p=L[0]*x^3+L[1]*x^2+L[2]*x+L[3] q=p.subs(x=t-c) r=q.expand() coef2=r.coefficient(t,n=2) B=coef2.solve(c,solution_dict=True) cval=B[0][c] return r.subs(c=cval) #Example (Use your own example) remove_xsquared([8,-2,5,7])# It works, no term with t^2
(t, c) 8*t^3 + 29/6*t + 200/27
########################## #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 = 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]
S = [(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)]
point(S,aspect_ratio=1,size=30)