A toy Haskell library for symbolic algebra, especially derivatives. I wrote it to learn to do a bunch of things, mainly.
I got a lot of the ideas and some of the code (I almost copied the simplify
function from him) from 5outh's tutorial, although I rewrote the code for derivatives on my own without looking at his.
I've implemented:
- Polynomials
- Trigonometric functions
exp
andlog
- Four fundamental operations and exponentiation
- Symbolic derivatives
- Simplification of expressions, using:
- A few trigonometric identities (stuff like
sin x sec x = tan x
) - Basic algebraic identities (
a^0 = 1
etc.)
- A few trigonometric identities (stuff like
To use it, clone the repo into a folder and run Main.hs
in ghci
:
$ git clone https://github.com/mrkgnao/pebble.git
$ cd pebble
$ ghci Main.hs
:+
,:-
,:*
,:/
and:^
are what you think they are- The same goes for
X
- Unary functions are given their Prelude names with an
uf
prefix, and you apply them to an expression with(Apply uffunc expr)
, whereuffunc
is something likeufsin
. - Operators have precedence, so you don't have to wrap everything in parens
X :^ X
isx^x
Apply ufsin (Apply uflog X)
issin (log x)
(Apply ufsin (2 :+ X)) :* (Apply ufcot X)
issin (2+x) cot x
The derivative
function computes the derivative of an expression, simplifies it as best as it can and pretty-prints it.
The bane of every eleventh-grader:
λ> derivative (X :^ X)
((x ^ x) * (1.0 + (log x)))
A composition of two functions:
λ> derivative ((Apply ufsin (Apply ufcos X)))
((cos (cos x)) * (-(sin x)))
This pushes the, uh, Synergized Algebraic Simplification Engine™ to its limit:
λ> derivative (Apply uflog (Apply ufsin X))
(cot x)
What do you think 'calculus' originally meant?