Last active
August 13, 2016 12:02
-
-
Save ggreif/569d9353936113c355b574f8c62ee386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Prelude | |
-- * Power series | |
-- | |
-- treat power series representations of functions | |
-- like numbers and derivatives | |
-- ** Background | |
-- | |
-- http://www.cs.dartmouth.edu/~doug/powser.html | |
-- there is a video too: https://youtu.be/jaHoYy2rnUc | |
-- This is very related to automatic differentiation | |
-- http://conal.net/papers/beautiful-differentiation/ | |
data P a = P a (P a) | |
instance Num a => Num (P a) where | |
P a xa + P b xb = (a + b) `P` (xa + xb) | |
P a xa * P b xb = (a * b) `P` (xa * P b 0 + xb * P a 0) | |
fromInteger = (`P` 0) . fromInteger | |
signum (P a _) = signum a `P` 0 -- correct? | |
abs (P a _) = abs a `P` 0 -- correct? | |
negate (P a xa) = negate a `P` negate xa | |
-- * =toList= | |
toList (P a xa) = a : toList xa | |
fromList [] = 0 | |
fromList (a:xa) = P a $ fromList xa | |
-- * =toFunction= | |
-- power series expansion at x to degree n | |
toFunction :: Num a => Int -> P a -> (a -> a) | |
toFunction 0 (P a _) _ = a | |
toFunction n (P a xa) x = a + x * toFunction (n-1) xa x | |
instance (Eq a, Show a, Num a) => Show (P a) where | |
show (P a (P 0 _)) = show a | |
show (P a (P xa _)) = show a ++ "+" ++ show xa ++ "x" | |
-- * TODO | |
-- | |
-- - complete numerical classes | |
-- - make P =Symbol= tagged (to differentiate between /x/ and /y/) | |
-- - =ListLike=? https://hackage.haskell.org/package/ListLike/docs/Data-ListLike.html | |
-- - =IsLabel= to compute multivariate functions: https://www.reddit.com/r/haskell/comments/4x8tk8/overloadedlabels_considered_awesome/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment