Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Christopher Olah May 23, 2012
file 43 lines (31 sloc) 1.212 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{-# LANGUAGE MultiParamTypeClasses, ViewPatterns, TemplateHaskell, QuasiQuotes, NoMonomorphismRestriction #-}

module Algebra.HaskSymb.BasicAlgs (simplify, expand, collectTerms, diff) where

import Prelude hiding ((+), (*))
import Algebra.HaskSymb.M.QQ
import Algebra.HaskSymb.Definitions

infixl 6 +
a+b = sumC' [a,b]
infixl 7 *
a*b = prodC' [a,b]

simplify = collectTerms . expand

--expand :: (SymbolicSum a, SymbolicProd a) => a -> a
expand [m| a+b |] = expand a + expand b
expand [m|a*(b+c)|] = expand (a*b) + expand (a*c)
expand a = a


collectTerms [m| aC*x + bC*x + c |] = collectTerms $ (aC+bC)*x + c
collectTerms [m| x + bC*x + c |] = collectTerms $ (constC 1 +bC)*x + c
collectTerms [m| x + x + c |] = collectTerms $ (constC 2)*x + c
collectTerms a = a

d a = collectTerms $ d' a
where
d' [m| a+b |] = d' a + d' b
d' [m| a*b |] = a * d' b + b * d' a
d' [m| aC |] = constC 0
d' a = diffC a

diff a b = collectTerms $ expand $ diff' a b
where
diff' var expr | var == expr = 1
diff' var [m| a + b|] = diff' var a + diff' var b
diff' var [m| a * b|] = a* (diff' var b) + b* (diff' var a)
diff' var [m| aC |] = 0
diff' var a = 0


Something went wrong with that request. Please try again.