# 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 math

def find_roots(a,b,c):
    #check if quadratic quadratic function
    if (a == 0):
        if(b==0):
            print("Error: This is not a function!")
        else:
            #linear function find root & give warning
            print("WARNING: This is the not a quadratic function!")
            print("The root of this linear function is:" )
            print(-c/b)
    else:
        # calculate the discriminant
        d = (b**2) - (4*a*c)

        # find roots of quadratic
        if (d<0):
            sqrt = cmath.sqrt(d)
            root1 = (-b - sqrt)/(2*a)
            root2 = (-b + sqrt)/(2*a)
            print("The roots of this function are complex:" )
        elif (d>0):
            sqrt = math.sqrt(d)
            root1 = (-b -sqrt)/(2*a)
            root2 = (-b + sqrt)/(2*a)
            print("The roots of this function are real:" )
        print(root1)
        print (root2)

find_roots(0,0.5,2)
find_roots(1,0,-1)
```

    WARNING: This is the not a quadratic function!
    The root of this linear function is:
    -4.0
    The roots of this function are real:
    -1.0
    1.0


f11de631966dd1cb1ac6d95acbfe537ec894ae1f
ipynb
Jupyter Notebook
lectures/L5/Exercise_2-Final.ipynb
HeyItsRiddhi/cs207_riddhi_shah
18d7d6f1fcad213ce35a93ee33c03620f8b06b65
MIT
lectures/L5/Exercise_2-Final.ipynb
HeyItsRiddhi/cs207_riddhi_shah
18d7d6f1fcad213ce35a93ee33c03620f8b06b65
MIT
lectures/L5/Exercise_2-Final.ipynb
HeyItsRiddhi/cs207_riddhi_shah
18d7d6f1fcad213ce35a93ee33c03620f8b06b65
MIT
Qwen/Qwen-72B
1. YES
2. YES