# 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
import warnings

def solve_quadratic(a, b, c):
    if a == 0: 
        if b == 0: 
            if c == 0: # infinetly many roots for c = 0
                raise Exception('The input equation is "0=0". Any value is a solution!')
            else : # no root for c != 0
                raise Exception('The input equation is c=0 for your c != 0. No solutions！')
        else:
            warnings.warn('Soving a linear equation bx+c=0. You only get one root.')
            return (-c/b)
    delta = b*b - 4*a*c
    if delta >= 0: # 2 real roots
        r1 = (-b + math.sqrt(delta))/(2*a)
        r2 = (-b - math.sqrt(delta))/(2*a)
    else: # 2 complex roots
        r1 = (-b + cmath.sqrt(delta))/(2*a)
        r2 = (-b - cmath.sqrt(delta))/(2*a)
    return (r1, r2)
        
```


```python
# solve the linear equation
solve_quadratic(0,2,-4)
```

    /Users/jasminetong/anaconda/lib/python3.6/site-packages/ipykernel_launcher.py:12: UserWarning: Soving a linear equation bx+c=0. You only get one root.
      if sys.path[0] == '':





    2.0




```python
# solve the most normal quadratic equation
solve_quadratic(1,4,4)
```




    (-2.0, -2.0)




```python
# No solutions for the equation "4=0"
solve_quadratic(0,0,4)
```


```python

```

502ce957675be5a8d940494f52350bdbe0ae7e9b
ipynb
Jupyter Notebook
lectures/L5/Exercise_2-final.ipynb
JasmineeeeeTONG/CS207_coursework
666239ee5f8bd7cbe04725a52870191a3d40d8c2
MIT
lectures/L5/Exercise_2-final.ipynb
JasmineeeeeTONG/CS207_coursework
666239ee5f8bd7cbe04725a52870191a3d40d8c2
MIT
lectures/L5/Exercise_2-final.ipynb
JasmineeeeeTONG/CS207_coursework
666239ee5f8bd7cbe04725a52870191a3d40d8c2
MIT
Qwen/Qwen-72B
1. YES
2. YES