tag:blogger.com,1999:blog-7056990295646173627.post4752205248188349525..comments2020-02-02T17:06:11.199+00:00Comments on Learning Clojure: Clojure Dojo 4: Symbolic DifferentiationJohn Lawrence Aspdenhttp://www.blogger.com/profile/02587130870181071109noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-7056990295646173627.post-65200185448957072932012-08-31T20:10:16.257+01:002012-08-31T20:10:16.257+01:00(ns symdiff.core
(:use [clojure.core.match :only...(ns symdiff.core<br /> (:use [clojure.core.match :only (match)]<br /> clojure.math.numeric-tower))<br /><br />(defn deriv [exp var]<br /> (match [exp]<br /> [(a :guard number?)] 0<br /> [(a :guard symbol?)] (if (= exp var) 1 0)<br /> [(['+ a b] :seq )] (list '+ (deriv a var) (deriv b var))<br /> [(['* a b] :seq )] (list '+ (list '* (deriv a var) b) (list '* a (deriv b var)))<br /> [(['expt a b] :seq )] (list '* b (list '* (list 'expt a (- b 1)) (deriv a var)))<br /> :else nil))Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-19917768466429050332011-08-05T22:39:45.661+01:002011-08-05T22:39:45.661+01:00Nice work! I am looking forward to an expanded ver...Nice work! I am looking forward to an expanded version of the symbolic differentiator. Could you explain the `list` and the `'` in `(defn make-add [ a b ] (list '+ a b))` and later on? What is going on there?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-19656641677165019282011-02-26T11:39:30.693+00:002011-02-26T11:39:30.693+00:00I may grab this code some day, once upon a time I ...I may grab this code some day, once upon a time I started writing something similar... got bored and moved to another project (a <a href="http://www.mostlymaths.net/2009/06/lisp-raytracing-again.html" rel="nofollow">Lisp raytracer</a>, I think it was, which sadly is still unfinished too... real work always has preference :/).<br /><br />Thanks for sharing, <br /><br />RubenRuben Berenguelhttps://www.blogger.com/profile/05271991635456932663noreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-74200832812053600382011-02-17T11:08:06.702+00:002011-02-17T11:08:06.702+00:00Javier, that would be very neat and efficient, but...Javier, that would be very neat and efficient, but the differentiator would only be able to differentiate polynomials then. <br /><br />This one can differentiate (exp (sin (/ 1 x))) with only small modifications. It actually has a lot in common with an interpreter for a computer language! <br /><br />I shall add the extra functions for 'the second simplest possible symbolic differentiator' and make a new post. Thanks!John Lawrence Aspdenhttps://www.blogger.com/profile/02587130870181071109noreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-45385345057816640042011-02-16T20:10:57.173+00:002011-02-16T20:10:57.173+00:00and what about representing polynomies as a vector...and what about representing polynomies as a vector of its coefficients? (i used it in a prolog homework: https://gist.github.com/795648Anonymoushttps://www.blogger.com/profile/03081252416288495482noreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-9189253879110865722011-02-16T20:08:29.257+00:002011-02-16T20:08:29.257+00:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/03081252416288495482noreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-78326896835952134182010-02-23T18:23:47.046+00:002010-02-23T18:23:47.046+00:00Sharp eyes! Well spotted. Will correct.
The littl...Sharp eyes! Well spotted. Will correct.<br /><br />The little nests of functions for the add/multiply structures are candidates for generic versions. <br /><br />When we put in subtraction and division this will become more necessary, and if I can find a way of using a macro to do it better than I could with a generic function, that's episode 5....<br /><br />I was hoping that I could use a macro for defining the function and its derivative from the source code version, but the run-time eval seems to work perfectly well, so it wouldn't be a particularly good example.John Lawrence Aspdenhttps://www.blogger.com/profile/02587130870181071109noreply@blogger.comtag:blogger.com,1999:blog-7056990295646173627.post-39808298793185456562010-02-23T09:41:08.232+00:002010-02-23T09:41:08.232+00:00There's a possible mistake in the addition? cl...There's a possible mistake in the addition? clause - I suspect you meant to use add1 and add2 rather than mul1 and mul2. It still works, of course, because addN and mulN do the same things, suggesting you could shorten this by two precious lines using generalised argN functions.Anonymousnoreply@blogger.com