### Exercise 1 ### ### 1 ### # Create a parametric plot given in function of a parametric symbolic variable # t. t = var('t') C_param = parametric_plot((t * (t^2 - 1), t^2 + 1), (t, -sqrt(2), sqrt(2)), color = 'blue') # Create the polynomial ring on 3 variables x, y and t over the field of # rational numbers. P. = PolynomialRing(QQ, 3) # Create the two polynomials p and q in this ring defining the above defined # parametric curve and compute their resultant r in t (r == 0 being a cartesian # equation for the above defined curve). p = x - (t * (t^2 - 1)) q = y - (t^2 + 1) r = p.resultant(q, t) C_cart = implicit_plot(SR(r), (x, -1.5, 1.5), (y, 1, 3), color = 'red', axes = True) ### 2 ### # Create a parametric plot given in function of a parametric symbolic variable # t. t = var('t') C_param = parametric_plot((2 * t / (t^2 + 1), (1 - t^2) / (t^2 + 1)), (t, -100, 100), color = 'blue', plot_points = 10000) # Create the polynomial ring on 3 variables x, y and t over the field of # rational numbers. P. = PolynomialRing(QQ, 3) # Create the two polynomials p and q in this ring defining the above defined # parametric curve and compute their resultant r in t (r == 0 being a cartesian # equation for the above defined curve). p = x * (t^2 + 1) - 2 * t q = y * (t^2 + 1) - (1 - t^2) r = p.resultant(q, t) C_cart = implicit_plot(SR(r), (x, -1, 1), (y, -1, 1), color = 'red', plot_points = 1000, axes = True) ### Exercise 2 ### ## # Find the list of intersection points of two curves, each defined as the set of # roots in a given ring of a multivariate polynomial on two variables over a # given ring. # # @param p A multivariate polynomial on two variables over a given ring. # @pre `p` must be a multivariate polynomial on x, y over a given ring such that # the method computing the resultant was implemented. # @param q A multivariate polynomial on two variables over a given ring. # @pre `q` must be a multivariate polynomial on x, y over a given ring such that # the method computing the resultant was implemented. # @param A A ring. # @pre `A` must a ring over which the resultant of p and q with respect to x and # with respect to y is well defined. # @param epsilon The threshold under which one shall consider a floating point # number to be equal to zero when considering its absolute value. # @pre `epsilon` must be a positive floating point number. # @return A list of pairs corresponding to the set of intersection points in A^2 # of the two curves defined by p and q respectively. def find_intersection(p, q, A, epsilon = 10^-10): B. = PolynomialRing(A) R_x = [e[0] for e in (p.resultant(q, y)(u, 0)).roots()] R_y = [e[0] for e in (p.resultant(q, x)(0, u)).roots()] S = [] for a in R_x: for b in R_y: if abs(p(a, b)) < epsilon and abs(q(a, b)) < epsilon: S.append((a, b)) return S ### 1 ### # Plot the two curves. x_min = -10 x_max = 10 y_min = -10 y_max = 80 P. = PolynomialRing(QQ, 2) c = x * y - 4 d = (x - 3) * (x^2 - 16) - y C = implicit_plot(c, (x, x_min, x_max), (y, y_min, y_max), color = 'blue', axes = True, aspect_ratio = 'automatic') D = implicit_plot(d, (x, x_min, x_max), (y, y_min, y_max), color = 'red', axes = True, aspect_ratio = 'automatic') # Compute the resultant of p and q in y and x to get a polynomial equation # verified by the abscissas and the ordinates of the points in C \cap D, # respectively. r_x = c.resultant(d, y) r_y = c.resultant(d, x) # Find the list of intersection points using these two equations. S = find_intersection(c, d, RR) I = list_plot(S, pointsize = 20, color = 'green', aspect_ratio = 'automatic') ### 2 ### # Plot the two curves. x_min = -6 x_max = 6 y_min = -5 y_max = 5 P. = PolynomialRing(QQ, 2) c = (x - 2)^2 + y^2 - 4 d = (x - 3) * (x^2 - 16) - y C = implicit_plot(c, (x, x_min, x_max), (y, y_min, y_max), color = 'blue', axes = True) D = implicit_plot(d, (x, x_min, x_max), (y, y_min, y_max), color = 'red', axes = True) # Compute the resultant of p and q in y and x to get a polynomial equation # verified by the abscissas and the ordinates of the points in C \cap D, # respectively. r_x = c.resultant(d, y) r_y = c.resultant(d, x) # Find the list of intersection points using these two equations. S = find_intersection(c, d, RR) I = list_plot(S, pointsize = 20, color = 'green', aspect_ratio = 'automatic') ### Exercise 3 ### # Plot the two surfaces. x_min = -2 x_max = 2 y_min = -2 y_max = 2 z_min = -2 z_max = 2 P. = PolynomialRing(QQ, 3) s = x^2 + y^2 + z^2 - 1 c = x^2 - x + y^2 S = implicit_plot3d(s, (x, x_min, x_max), (y, y_min, y_max), (z, z_min, z_max), color = 'blue', plot_points = (100, 100, 100)) C = implicit_plot3d(c, (x, x_min, x_max), (y, y_min, y_max), (z, z_min, z_max), color = 'red', plot_points = (100, 100, 100)) # Get the projection of S \cap C on each of the planes Oxy, Oxz and Oyz by # computing (and then plotting the curves defined as the set of roots of) the # resultant of s and c in z, y and x, respectively. # NOTE: To plot the projections on Oxy and Oxz, we have to use the # not-so-elegant trick below (i.e., getting an expression for the ordinate of # the curve in function of the abscissa), because the function "implicit_plot" # does not seem to plot anything for p_xy and p_xz. p_xy = s.resultant(c, z) p_xz = s.resultant(c, y) p_yz = s.resultant(c, x) C_xy = plot([e.right() for e in solve(SR(p_xy), var('y'))], (0, 1), color = 'blue', plot_points = 1000) C_xz = plot([e.right() for e in solve(SR(p_xz), var('z'))], (-1, 1), color = 'red') C_yz = implicit_plot(SR(p_yz), (y, -1, 1), (z, -1, 1), color = 'green', plot_points = 1000, axes = True) ### Exercise 4 ### # We first create the needed polynomial ring and polynomials. P. = QQ[] p1 = 2 * s - c * y p2 = x^2 + y^2 - b^2 p3 = (c - x)^2 + y^2 - a^2 # Then we eliminate x and y. r1 = p2.resultant(p3, x).resultant(p1, y) # We see that we can divide by c^2 in the equation r1 == 0, provided that # c != 0 (this can be seen by factoring r1 first). r2 = P(r1 / c^2) # We now see that r2 == 0 is equivalent to 16 * s^2 == r3. r3 = (r2 - 16 * s^2) * -1 # And we can finally get the formula by factoring r3. s = var('s') F = 16 * s^2 == SR(r3).factor()