# Lecture 17: the First Midterm Exam¶

The first midterm exam covers the first two parts of the course.

## Questions on the Spring 2017 First Midterm Exam¶

The list of questions on the Spring 2017 first midterm exam are below. The exam is open book, open notes and open computer. All answers to the questions must be handwritten, submitted on paper.

1. Let $$N$$ be the number $$\exp(\pi)$$.

1. Write a rational approximation $$A$$ for $$N$$, accurate with 3 decimal places.

2. Give the Sage command(s) to verify that $$A$$ approximates $$N$$ indeed with 3 decimal places.

3. Compute a list of consecutively more accurate approximations for $$N$$.

The list should start with the number $$A$$ and have length 10.

Do not write the list, write only the Sage command(s) to compute this list.

2. Let $$p = x^3 + x + 1$$ be a polynomial over the finite field (also called a Galois field) of five numbers.

1. Write the Sage commands to define $$p$$ and to show that $$p$$ does not factor over this finite field.

2. Use $$p$$ to extend the finite field of five numbers.

Write the factorization of $$p$$ over this field extension.

Give all relevant Sage commands you used to obtain this factorization.

3. What is a symbolic ring? Give a good example of a use of a symbolic ring.

4. Consider a polynomial with integer coefficients in three variables, $$x$$, $$y$$, and $$z$$.

1. Define one normal form of such a polynomial.

2. Illustrate your definition with an example of a random polynomial.

3. What is the application of a normal form?

5. Let $$q = (x^2 + x - 1)/(x^2 - 2)$$. Draw the expression tree of $$q$$.

Do not write any Sage commands.

6. We have covered a couple of different ways to substitute. Describe two different ways and illustrate each way with an example. Explain on the examples why one way to substitute is appropriate for the example instead of the other way to substitute.

7. Let $$p = (x^2 - 3 y) (x + 2 y)$$ and $$q = x^3 + 2 x^2 y - 3 x y - 6 y^2$$.

Give the Sage commands to

1. transform $$p$$ into $$q$$; and

2. transform $$q$$ into $$p$$.

## Questions on the Fall 2018 First Midterm Exam¶

The list of questions on the Fall 2018 first midterm exam are below. The exam is open book, open notes and open computer. All answers to the questions must be handwritten, submitted on paper.

1. Let $$N$$ be the natural logarithm of 10.

1. Give a floating-point approximation $$A$$ of $$N$$, accurate with 5 decimal places.

2. Compute a rational approximation $$R$$ for $$N$$, accurate with 5 decimal places. Write the value for $$R$$. Write the Sage command(s) and its output to verify that $$R$$ approximates $$N$$ indeed with an accuracy of 5 decimal places.

3. Give the Sage command to compute a list of the first 10 rational approximations of $$N$$. Do not write the entire list, write only its last element.

2. Let x and y be variables, declared via x, y = var('x,y').

Explain the difference between y = 2; x = y and y = 2; x = 'y'.

Explain the relations between x and y in both cases.

3. Let $$p$$ be the polynomial $$p = x^8 + 2 x^7 + 5 x^6 - 3 x^5 - 4 x^4 - x^3 + x^2 + x - 2$$.

What is the smallest number of arithmetical operations needed to evaluate $$p$$?

What is the fastest way to evaluate $$p$$ in hardware arithmetic?

1. Give all Sage commands to convert $$p$$ into an expression best suited for fast evaluation. Do not write the output of the Sage commands.

2. Give timing results to compare the straightforward evaluation of $$p$$ with the evaluation of the form for fast evaluation.

4. For this question, write all relevant Sage commands and their output.

Define the polynomial $$p = 9 x^3 - 72 x^2 - 78 x + 3$$ over the field of rational coefficients.

1. Is $$p$$ irreducible? If not, what are the factors of $$p$$?

2. Extend the field of rational numbers with sufficiently many formal roots so $$p$$ factors as a product of linear polynomials over the field extension.

5. Type x, y = var('x, y'); q = (x-y+1)/(x^2+3*y) and draw the expression tree of q.

Give some relevant Sage commands you used to obtain your drawing.

6. For polynomials in several variables explain the difference between the pure lexicographic and the degree lexicographic order.

Illustrate the difference with good examples.

7. What are the commands to transform $${\displaystyle \frac{(x-y)^2}{(x+y)^2} + 1 \mbox{ into } \frac{(x-y)^2 + (x+y)^2}{(x+y)^2}}$$?

## Questions on the Spring 2019 First Midterm Exam¶

The list of questions on the Spring 2019 first midterm exam are below. The exam is open book, open notes and open computer. All answers to the questions must be handwritten, submitted on paper.

1. Consider $$c = \sqrt{\pi/3}$$.

1. Give a rational approximation to $$c$$, accurate with 5 decimal places.

2. What is the actual error of your rational approximation?

3. Give a list of rational approximations of $$c$$. The first number of the list is the same rational number as the one above, accurate to 5 decimal places. The last rational number has an accuracy of 10 decimal places.

2. For x = var('x'), why does Sage not automatically simplify sqrt(x^2) to x?

Illustrate your explanation with a good example.

3. Consider the efficient evaluation of $$p=x^6 + 2 x^5 + 3 x^4 + 4 x^3 + 5 x^2 + 6 x + 7$$.

1. Construct the Horner form for $$p$$.

How many arithmetical operations does it take to evaluate $$p$$?

2. What is the most efficient way to evaluate $$p$$ with hardware arithmetic?

Compare this most efficient way to the straightforward way to evaluate $$p$$.

4. Give all relevant commands for this problem. Consider the polynomial $$p = x^4 + x^2 + 1$$ over the field $${\mathbb Z}_2 = \{ 0, 1 \}$$, the field of bits, 0 and 1.

1. Is $$p$$ irreducible? If not, then what are its factors?

2. Extend the field $${\mathbb Z}_2$$ with sufficiently many formal roots so $$p$$ factors as a product of linear factors. Write the factorization of $$p$$.

5. Type x,y = var('x,y') followed by q = (x^2 - y + 1)/(y^2 - x + 1).

Draw the internal representation of q.

Give some relevant commands used to make your drawing.

6. Explain the meaning of QQ['x,y,z'], QQ['x']['y,z'], and QQ['z']['y']['x'].

Explain the differences between QQ['x,y,z'], QQ['x']['y,z'], and QQ['z']['y']['x'].

Illustrate the differences with good examples.

7. Write the commands to transform $${\displaystyle x + \frac{(x - y)^5}{(x + y)^5}}$$ into $${\displaystyle \frac{(x + y)^5 \cdot x + (x - y)^5}{(x + y)^5}}$$.

## Questions on the Summer 2022 First Midterm Exam¶

The exam in the summer 2022 took 100 minutes and was administered online, in two different versions. Questions had to be uploaded into one single Jupyter notebook into gradescope.

The list of the nine questions on the first version is below.

1. Let $$N = \cos(\pi/5)$$.

1. Compute a rational approximation for $$N$$, accurate to 2 decimal places.

2. Verify that the accuracy of your rational approximation is indeed 2 decimal places.

3. Compute the sequence of the first ten consecutive rational approximations for $$N$$, accurate with 2, 3, up to 11 decimal places.

2. Explain the difference between $$1.0 + 10^{-32}$$ and $$1 + 10^{-32}$$.

What would you need to do for SageMath to return the same, correct, and real value of those two sums?

3. Declare r and theta as variables in the RR domain.

Define n as the expression r*exp(I*theta). What are the real and imaginary parts of n?

4. Compute the Horner form of $$p = x^7 - 7 x^5 - 2 x^4 - x^3 - 4 x^2 - 2 x + 1.$$

Explain the use of the Horner form.

What is its advantage of the Horner form over representing $$p$$ as above?

5. Explain the difference between eval(x) and eval(preparse(x)).

Give a good example of x to illustrate the difference.

6. Consider the function

def python_sum(n):
"""
Returns the floating-point value of the sum of (3*k/n)**2,
for k from 1 to n-1, multiplied by 3/n.
"""
return float((3/n)*sum([(3*k/n)**2 for k in range(1, n)]))

1. Time the execution of python_sum(10**4).

2. Explain why the execution is slow.

3. Apply vectorization to write a faster version. Verify the correctness.

4. Time the execution of your vectorized function for n = 10**4 and compare with your timing of the original python_sum(10**4).

7. Consider the polynomial $$p = x^3 - x^2 - 3$$.

Compute the exact, symbolic, and numeric factorization for $$p$$.

8. Consider the expression:

$\frac{a - 2b}{3a + b + 2c},$

for $$a$$, $$b$$, $$c$$ defined as a, b, c = var('a, b, c').

Draw the internal representation of this expression.

9. Transform $$(y+1)^6 - x^4$$ into $$((y+1)^3 + x^2)((y+1)^3 - x^2)$$.

The list of the nine questions on the second version is below.

1. Let $$N = \log(\pi/2)$$.

1. Compute a nearby rational approximation for $$N$$, with the denominator bounded by 100.

2. What is the accuracy of your nearby rational approximation?

3. Compute the sequence of the first ten nearby rational approximations for $$N$$, where the size of the denominator of the $$k$$-th approximation is bounded by $$10^k$$, $$k=1,2, \ldots 10$$.

2. Explain the difference between 0 and 0.000.

Give a good example of a calculation which results in 0.000, to illustrate the difference with a result of a computation equal to 0.

3. Make eqn so print(eqn) shows cos(pi) == -1.

Without retyping eqn, change eqn so print(eqn) shows -1 == -1.

4. Consider the statements

a, b, c = var('a, b, c')
a = b; b = 2; c = b


Describe the relations between the three variables.

Execute the commands to verify the relations.

5. What does the preparse() function do?

Illustrate your explanation with a good example where preparse() is needed.

6. Consider the function

def python_sum(n):
"""
Returns the floating-point value of the sum of exp(-k/n)
for k from 1 to n-1, multiplied by 1/n.
"""
return float((1/n)*sum([exp(-k/n) for k in range(1, n)]))

1. Time the execution of python_sum(10**4).

2. Explain why the execution is slow.

3. Apply vectorization to write a faster version. Verify the correctness.

4. Time the execution of your vectorized function for n = 10**4 and compare with your timing of the original python_sum(10**4).

7. Consider the polynomial $$p = x^3 + x^2 + 3$$.

Compute the exact, symbolic, and numeric factorization for $$p$$.

8. Draw the binary expression tree defined by the fast callable

$\frac{a + 2b}{3a - b + 2c}.$
9. Transform $$((y+1)^3 + x^2)((y+1)^3 - x^2)$$ into $$(y+1)^6 - x^4$$.