# Exercise 2
Write a function to compute the roots of a mathematical equation of the form
\begin{align}
  ax^{2} + bx + c = 0.
\end{align}
Your function should be sensitive enough to adapt to situations in which a user might accidentally set $a=0$, or $b=0$, or even $a=b=0$.  For example, if $a=0, b\neq 0$, your function should print a warning and compute the roots of the resulting linear function.  It is up to you on how to handle the function header: feel free to use default keyword arguments, variable positional arguments, variable keyword arguments, or something else as you see fit.  Try to make it user friendly.

Your function should return a tuple containing the roots of the provided equation.

**Hint:** Quadratic equations can have complex roots of the form $r = a + ib$ where $i=\sqrt{-1}$ (Python uses the notation $j=\sqrt{-1}$).  To deal with complex roots, you should import the `cmath` library and use `cmath.sqrt` when computing square roots.  `cmath` will return a complex number for you.  You could handle complex roots yourself if you want, but you might as well use available libraries to save some work.


```python
import cmath

def compute_roots(a, b, c):
    """ Returns roots for quadratic equation of form ax^2 + bx + c = 0 """
    if a == 0:
        if b == 0:
            if c == 0:
                raise ValueError('Infititely many solutions!')
            else:
                raise ValueError('No solutions!')
        else:
            return (-c/b,)
    
    descriminant = (b^2) - (4 * a * c)
    
    sol1 = -b + cmath.sqrt(descriminant) / (2 * a)
    sol2 = -b - cmath.sqrt(descriminant) / (2 * a)
    return sol1, sol2

a = 0
b = 2
c = 4
try:
    solution = compute_roots(0, 2, 4)
    print('Roots = {}'.format(solution))
except Exception as e:
    print(e)
```

    Roots = (-2.0,)



```python

```

08d7bf64db307869824b0030dcf95b3a58526220
ipynb
Jupyter Notebook
lectures/L5/Exercise_2.ipynb
nate-stein/cs207_nate_stein
f8ce68f9d839a0bd0ab4a2e1ebaa7ae985f6b5d0
MIT
lectures/L5/Exercise_2.ipynb
nate-stein/cs207_nate_stein
f8ce68f9d839a0bd0ab4a2e1ebaa7ae985f6b5d0
MIT
lectures/L5/Exercise_2.ipynb
nate-stein/cs207_nate_stein
f8ce68f9d839a0bd0ab4a2e1ebaa7ae985f6b5d0
MIT
Qwen/Qwen-72B
1. YES
2. YES