--
-- Mathematical Functions
--

abs $x := if isRational x then b.abs x else x

neg $x := if isRational x then b.neg x else - x

exp $x :=
  if isFloat x
    then b.exp x
    else if isTerm x
      then match x as termExpr with
        | #0 -> 1
        | #1 -> e
        | mult $a #(i * pi) -> (-1) ^ a
        | _ -> `exp x
      else `exp x

log $x :=
  if isFloat x
    then b.log x
    else match x as mathExpr with
      | #1 -> 0
      | #e -> 1
      | _ -> `log x

cos $x :=
  if isFloat x
    then b.cos x
    else match x as mathExpr with
      | #0 -> 1
      | term $n [#π] -> (-1) ^ abs n
      | (mult _ #π) / #2 -> 0
      | _ -> `cos x

sin $x :=
  if isFloat x
    then b.sin x
    else match x as mathExpr with
      | #0 -> 0
      | mult _ #π -> 0
      | (mult $n #π) / #2 -> (-1) ^ ((abs n - 1) / 2)
      | _ -> `sin x

tan $x :=
  if isFloat x
    then b.tan x
    else match x as mathExpr with
      | #0 -> 0
      | _ -> `tan x

acos := b.acos
asin := b.asin
atan := b.atan

cosh $x :=
  if isFloat x
    then b.cosh x
    else match x as mathExpr with
      | #0 -> 1
      | _ -> `cosh x

sinh $x :=
  if isFloat x
    then b.sinh x
    else match x as mathExpr with
      | #0 -> 0
      | _ -> `sinh x

tanh $x :=
  if isFloat x
    then b.tanh x
    else match x as mathExpr with
      | #0 -> 0
      | _ -> `tanh x

acosh := b.acosh
asinh := b.asinh
atanh := b.atanh

sinc $x :=
  if isFloat x
    then if x = 0.0 then 1.0 else b.sin x / x
    else match x as mathExpr with
      | #0 -> 1
      | _ -> sin x / x

sigmoid $z := 1 / (1 + exp (- z))

kroneckerDelta := cambda js -> if all (= head js) (tail js) then 1 else 0

eulerTotientFunction $n := n * product (map (\p -> 1 - 1 / p) (unique (pF n)))

ε :=
  memoizedLambda n ->
    let (es, os) := evenAndOddPermutations' n
     in generateTensor
          (cambda is ->
            if member is es then 1 else if member is os then -1 else 0)
          (take n (repeat1 n))

ε' :=
  memoizedLambda n k ->
    let (es, os) := evenAndOddPermutations' n
     in generateTensor
          (cambda is ->
            match drop k is as list integer with
              | _ ++ $x :: _ ++ ?(< x) :: _ -> 0
              | _ ->
                if member is es then 1 else if member is os then -1 else 0)
          (take n (repeat1 n))
