Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

{-|

Module : 1JC3-Assign1.Assign_1.hs
Copyright : (c) Curtis D'Alves 2022
License : GPL (see the LICENSE file)
Maintainer : none
Stability : experimental
Portability : portable

Description:
Assignment 1 - McMaster CS 1JC3 2022
-}
module Assign_1 where

-----------------------------------------------------------------------------------
------------------------
-- INSTRUCTIONS README!!!
-----------------------------------------------------------------------------------
------------------------
-- 1) DO NOT DELETE/ALTER ANY CODE ABOVE THESE INSTRUCTIONS AND DO NOT ADD ANY
IMPORTS
-- 2) DO NOT REMOVE / ALTER TYPE DECLERATIONS (I.E THE LINE WITH THE :: ABOUT THE
FUNCTION DECLERATION)
-- IF YOU ARE UNABLE TO COMPLETE A FUNCTION, LEAVE IT'S ORIGINAL IMPLEMENTATION
(I.E. THROW AN ERROR)
-- 3) MAKE SURE THE PROJECT COMPILES (I.E. RUN STACK BUILD AND MAKE SURE THERE ARE
NO ERRORS) BEFORE
-- SUBMITTING, FAILURE TO DO SO WILL RESULT IN A MARK OF 0
-- 4) REPLACE macid = "TODO" WITH YOUR ACTUAL MACID (EX. IF YOUR MACID IS jim THEN
macid = "jim")
-----------------------------------------------------------------------------------
------------------------

-- Name: Mariam Ismail


-- Date: Oct 2nd 2022
macid :: String
macid = "ismaim34"

(***) :: Double -> Double -> Double


x *** y = if x >= 0 then x ** y else -( (-x) ** y)

{- -----------------------------------------------------------------
- cubicQ
- -----------------------------------------------------------------
- Description:
- computes cubicQ from a b and c
-}
cubicQ :: Double -> Double -> Double -> Double
cubicQ a b c = ((3 * a * c) - (b**2)) / (9 * (a**2))

{- -----------------------------------------------------------------
- cubicR
- -----------------------------------------------------------------
- Description:
- computes cubicR from a b c and d
-}
cubicR :: Double -> Double -> Double -> Double -> Double
cubicR a b c d = ((9 * a * b * c) - (27 * a**2 * d) - (2 * b**3)) / (54 * a**3)

{- -----------------------------------------------------------------
- cubicDisc
- -----------------------------------------------------------------
- Description:
- computes the discriminant from q and r
-}
cubicDisc :: Double -> Double -> Double
cubicDisc q r = q**3 + r**2
{- -----------------------------------------------------------------
- cubeRoot
- -----------------------------------------------------------------
- Description:
- computes cube root of a
-}
cubeRoot :: Double -> Double
cubeRoot a = a *** (1/3)
{- -----------------------------------------------------------------
- cubicS
- -----------------------------------------------------------------
- Description:
- computes cubicS from q and r
-}
cubicS :: Double -> Double -> Double
cubicS q r = cubeRoot (r + (q ** 3 + (r ** 2)) ** (1/2))

{- -----------------------------------------------------------------
- cubicT
- -----------------------------------------------------------------
- Description:
- computes cubicT from q and r
-}
cubicT :: Double -> Double -> Double
cubicT q r = cubeRoot (r - (q ** 3 + (r ** 2)) ** (1/2))

{- -----------------------------------------------------------------
- cubicRealSolutions
- -----------------------------------------------------------------
- Description:
- cube roots
-}
cubicRealSolutions :: Double -> Double -> Double -> Double -> [Double]
cubicRealSolutions a b c d
| isNaN disc = []
| disc === 0 = [x1,x2,x2]
| disc > 0 = [x1]
| otherwise = []
where
disc = cubicDisc q r
s = cubicS q r
t = cubicT q r
q = cubicQ a b c
r = cubicR a b c d
x1 = (s + t) - (b/(3*a))
x2 = (-1*((s + t)/2)) - (b/(3*a))

(===) :: Double -> Double -> Bool


x === y = let
tol = 1e-3
in abs (x-y) <= tol
{- -----------------------------------------------------------------
- Test Cases
- -----------------------------------------------------------------
-}

-- TODO Add Test Cases for each of your functions below here

You might also like