# 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 numpy as np
import cmath
```


```python
def find_roots(a, b, c):
    if 0 == a:
        if 0 == b:
            print('Warning: wrong input! a and b cannot be zero simultaneously!')
            if 0 == c:
                print('All x')
            return None
        else:
            print('Warning: a = 0; it is a linear function!')
            return [-c/b]
    else:
        return [(-b+cmath.sqrt(b*b-4*a*c))/(2*a), (-b-cmath.sqrt(b*b-4*a*c))/(2*a)]        
```


```python
# Test 1
roots = find_roots(1, 1, 1)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    The root(s) is (are) (-0.5+0.8660254037844386j), (-0.5-0.8660254037844386j).



```python
# Test 2
roots = find_roots(1, 2, 1)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    The root(s) is (are) (-1+0j), (-1+0j).



```python
# Test 3
roots = find_roots(1, 2, 1)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    The root(s) is (are) (-1+0j), (-1+0j).



```python
# Test 4
roots = find_roots(0, 0, 0)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    Warning: wrong input! a and b cannot be zero simultaneously!
    All x



```python
# Test 5
roots = find_roots(0, 1, 1)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    Warning: a = 0; it is a linear function!
    The root(s) is (are) -1.0.



```python
# Test 6
roots = find_roots(0, 0, 1)
if roots:
    print('The root(s) is (are) %s.' %', '.join([str(root) for root in roots]))
```

    Warning: wrong input! a and b cannot be zero simultaneously!


004b3c8d194546251484a46c6dd684f30c859ca3
ipynb
Jupyter Notebook
lectures/L5/Exercise_2-final.ipynb
xuwd11/cs207_Weidong_Xu
00442657239c7a4040501bf7fa0f6697c731fe94
MIT
lectures/L5/Exercise_2-final.ipynb
xuwd11/cs207_Weidong_Xu
00442657239c7a4040501bf7fa0f6697c731fe94
MIT
lectures/L5/Exercise_2-final.ipynb
xuwd11/cs207_Weidong_Xu
00442657239c7a4040501bf7fa0f6697c731fe94
MIT
Qwen/Qwen-72B
1. YES
2. YES