CS 320 Sage programming

0 comments

We will use Sage to explore the basics of symbolic computing and the basic functionality of a computer
algebra system.

Problem One.
Write a Sage function which takes on input as its argument an integer n and returns a random n degree
polynomial with randomly generated integer coefficients. The randomly generated integer coefficients
range from -100 to +100, inclusive. In addition to returning the randomly generated polynomial of degree n, the Sage function must return the number of real roots of the randomly generated polynomial.
In particular, this Sage function returns the randomly generated polynomial and the number of its real
roots in form of a Python tuple or a list.

Input example : program1(6), where n = 6.

Output example:
(89*x^6 – 19*x^5 – 80*x^4 – 76*x^3 + 54*x^2 – 39*x + 45, 2)
As your solution to this problem, provide the Sage function and the output for n = 4 and n = 10.

Problem Two. Write a Sage function which takes on input as its arguments a polynomial f with integer
coefficients and a list L. The polynomial f is a symbolic expression in the variable x. The list L consists
of an unspecified number of sub-lists, each of which contains two integers. These two integers represent
the x and y coordinates. As an example of L, consider L = [[x1, y1], [x2, y2], [x3, y3], …[xk, yk]]. This Sage
functions returns all sub-lists in L, for which f(xi) = yi
, for all xi and yi
in sub-lists of L.
Input example :

problem2( 2*x^2-2*x+3, [[0, 3], [1, 4], [2, 7], [3, 17], [4, 33], [5, 43], [6, 63],
[7, 87], [8, 116], [9, 147]])

Output example:

[[0, 3], [2, 7], [5, 43], [6, 63], [7, 87], [9, 147]]

As your solution to this problem, provide the Sage function and the output for

f = x
3 − x
2 − 1
and L = [[0, −1], [1, −2], [2, 4], [3, 17], [4, 48], [5, 99], [6, 189], [7, 332]].

Problem Three. Write a Sage function which takes on input as its argument a polynomial f. The
polynomial f is a symbolic expression with integer coefficients. This function returns the count of how
often the sign of the coefficients has changed.
Input example :

problem3(13*x^6 – 88*x^5 + 53*x^4 – 58*x^3 – 57*x^2 + 93*x + 22)

Output example:

4
As your solution to this problem, provide the Sage function and the output for
f = −27 ∗ x
7 + 3 ∗ x
6 − 28 ∗ x
5 + 44 ∗ x
4 − 11 ∗ x
3 − 71 ∗ x
2 + 6 ∗ x − 14

Problem Four. Two cars leave an intersection at the same time. One car heads north at the constant
speed of s1 (ft/sec). The second car heads east at the constant speed of s2 (ft/sec). Write a Sage function
which takes on input as its argument the constant speed s1 of the first car, the constant speed s2 of the
second car, h hours and m minutes, the time the two cars left the intersection. This Sage function returns
the distance (in miles) between the two cars after h hours and m minutes.

Input example : problem4(65, 65, 1, 12)

Output example: 75.2104485443873
As your solution to this problem, provide the Sage function and the output for problem4(25, 45, 1, 30),
where s1 = 25, s2 = 45, h = 1, and m = 30.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}