Created
March 31, 2016 18:06
-
-
Save amalex5/541dc739201bbd8e26037b6738cdf75b to your computer and use it in GitHub Desktop.
symbolic differentiation is SO EASY in haskell!
This file contains 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
-- i mean, this is basically just a high school formula sheet | |
-- that happens to also be valid haskell | |
-- (we're differentiating against "Symbol") | |
diff (Symbol) = Val 1 | |
diff (Neg x) = Neg (diff x) | |
diff (Add x y) = Add (diff x) (diff y) | |
diff (Mul x y) = Add (Mul (diff x) y ) (Mul x (diff y)) | |
diff (Sub x y) = diff (Add x (Neg y)) | |
diff (Pow x (Val n)) = Mul (Val n) (Pow x (Val (n - 1) ) ) | |
diff (Div x y) = diff (Mul x (Pow y (Val (-1)) ) ) | |
diff (Exp x) = Mul (Exp x) (diff x) | |
diff (Sin x) = Mul (Cos x) (diff x) | |
diff (Cos x) = Neg (Mul (Sin x) (diff x) ) | |
diff (Fxn xs y) = Mul (Fxn (xs ++ "\'") y) (diff y) | |
-- most of this stuff is written in polish notation, but if we want to be even clearer | |
-- we can write it in infix notation: | |
diff (x `Mul` y) = ((diff x) `Mul` y ) `Add` (x `Mul` (diff y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have the part for the codes Add, Pow and Mul?