-----------------------------------------------------------------------------
-- |
-- Module      : Chapter02
-- Description : Writing Basic Functions
--
-- Copyright   : (c) Eric Zoerner, 2023
-- License     : BSD3
-- Maintainer  : eric.zoerner@proton.me
--
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wno-name-shadowing #-}

module Chapter02 where

------------------
-- * Exercise 2.1
------------------

-- | @f(x) = √ (1 + x)@
--
-- >>> f 0
-- 1.0
-- >>> f 1
-- 1.4142135623730951
-- >>> f 3
-- 2.0
f :: Floating a => a -> a
f :: forall a. Floating a => a -> a
f a
x = forall a. Floating a => a -> a
sqrt (a
1 forall a. Num a => a -> a -> a
+ a
x)

------------------
-- * Exercise 2.2.
------------------

-- | Standard Earth gravity acceleration at sea level in m/s².
stdGravityMPS2 :: Double
stdGravityMPS2 :: Double
stdGravityMPS2 = Double
9.80665

-- | Assuming an initial velocity of 30 m\/s, accepts as input the time
-- (after the rock was thrown) in seconds and gives
-- as output the height of the rock in meters.
yRock30 :: Double -> Double
yRock30 :: Double -> Double
yRock30 Double
t = Double
0.5 forall a. Num a => a -> a -> a
* (-Double
stdGravityMPS2) forall a. Num a => a -> a -> a
* Double
t forall a. Floating a => a -> a -> a
** Double
2 forall a. Num a => a -> a -> a
+ Double
30 forall a. Num a => a -> a -> a
* Double
t

------------------
-- * Exercise 2.3.
------------------

-- | Accepts as input the time (after the rock was thrown) in seconds and
-- gives as output the upward velocity of the rock in meters per second.
-- A downward velocity is returned as a negative number.
vRock30 :: Double -> Double
vRock30 :: Double -> Double
vRock30 Double
t = Double
30 forall a. Num a => a -> a -> a
- Double
stdGravityMPS2 forall a. Num a => a -> a -> a
* Double
t

------------------
-- * Exercise 2.4.
------------------

-- | Computes the sine of an angle given in degrees.
--
-- >>> sinDeg 30
-- 0.49999999999999994
sinDeg :: Double -> Double
sinDeg :: Double -> Double
sinDeg Double
deg = forall a. Floating a => a -> a
sin (Double
deg forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ Double
180)

------------------
-- * Exercise 2.5.
------------------

-- | @f(x) = ∛x@
f25 :: Double -> Double
f25 :: Double -> Double
f25 Double
x = Double
x forall a. Floating a => a -> a -> a
** (Double
1 forall a. Fractional a => a -> a -> a
/ Double
3)

-- | @g(ɣ) = e^ɣ+8^ɣ@
g :: Double -> Double
g :: Double -> Double
g Double
ɣ = forall a. Floating a => a -> a
exp Double
ɣ forall a. Num a => a -> a -> a
+ Double
8 forall a. Floating a => a -> a -> a
** Double
ɣ

-- | @h(x) = 1/√((x-5)²+16)@
h :: Double -> Double
h :: Double -> Double
h Double
x = Double
1 forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
sqrt ((Double
x forall a. Num a => a -> a -> a
- Double
5) forall a. Floating a => a -> a -> a
** Double
2 forall a. Num a => a -> a -> a
+ Double
16)

-- | @ɣ(β) = 1/√(1 - β²)@
ɣ :: Double -> Double
ɣ :: Double -> Double
ɣ Double
β = Double
1 forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
sqrt (Double
1 forall a. Num a => a -> a -> a
- Double
β forall a. Floating a => a -> a -> a
** Double
2)

-- | @U(x) = 1/(10+x)+1(10-x)@
bigU :: Double -> Double
bigU :: Double -> Double
bigU Double
x = (Double
1 forall a. Fractional a => a -> a -> a
/ (Double
10 forall a. Num a => a -> a -> a
- Double
x)) forall a. Num a => a -> a -> a
+ (Double
1 forall a. Fractional a => a -> a -> a
/ (Double
10 forall a. Num a => a -> a -> a
- Double
x))

-- | @L(l) = √(l(l + 1))@
bigL :: Double -> Double
bigL :: Double -> Double
bigL Double
l = forall a. Floating a => a -> a
sqrt (Double
l forall a. Num a => a -> a -> a
* (Double
l forall a. Num a => a -> a -> a
+ Double
1))

-- | @E(x) = 1 / (|x|³)@
bigEx :: Double -> Double
bigEx :: Double -> Double
bigEx Double
x = Double
1 forall a. Fractional a => a -> a -> a
/ forall a. Num a => a -> a
abs Double
x forall a. Floating a => a -> a -> a
** Double
3

-- | @E(z) = 1 \/ (z²+4)^(3\/2)@
bigEz :: Double -> Double
bigEz :: Double -> Double
bigEz Double
z = Double
1 forall a. Fractional a => a -> a -> a
/ ((Double
z forall a. Floating a => a -> a -> a
** Double
2 forall a. Num a => a -> a -> a
+ Double
4) forall a. Floating a => a -> a -> a
** (Double
3 forall a. Fractional a => a -> a -> a
/ Double
2))

------------------
-- * Exercise 2.6.
------------------
{- $ex26

__(a)__ @\b -> 1 / sqrt (1 - b ** 2)@

__(b)__

>>> (\b -> 1 / sqrt (1 - b ** 2)) 0.8
1.666666666666667
 -}