lisp-cas.org_archive 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. # -*- mode: org -*-
  2. Archived entries from file /home/swflint/Projects/lisp-cas/lisp-cas.org
  3. * TODO Match Expressions
  4. :PROPERTIES:
  5. :ID: 39f69de5-6fcc-4ad4-984f-72fc0f77f11b
  6. :CREATED: <2016-06-11 Sat 22:20>
  7. :ARCHIVE_TIME: 2016-06-13 Mon 20:27
  8. :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org
  9. :ARCHIVE_OLPATH: Symbolic Differentiation/Expansions
  10. :ARCHIVE_CATEGORY: lisp-cas
  11. :ARCHIVE_TODO: TODO
  12. :END:
  13. To be able to apply an expansion, you need to determine eligibility. To do this, you need an expression that matches on two things, function name and arity. To generate this, it takes an operation name and the arity. Based on the arity type ($=$, $>$, $\leq$), it will construct a simple boolean statement in the format of $(function = operator) \land (argument-count == arity)$, where $==$ is one of the above arity types.
  14. #+Caption: Match Expressions
  15. #+Name: derive-match-expressions
  16. #+BEGIN_SRC lisp
  17. ;; (defun generate-match-expression (on arity &optional (type '=))
  18. ;; (check-type on symbol)
  19. ;; (check-type type (member = > >=))
  20. ;; (check-type arity (integer 0))
  21. ;; (case type
  22. ;; (=
  23. ;; `(and (eq function ',on)
  24. ;; (= arg-count ,arity)))
  25. ;; (>
  26. ;; `(and (eq function ',on)
  27. ;; (> arg-count ,arity)))
  28. ;; (>=
  29. ;; `(and (eq function ',on)
  30. ;; (>= arg-count ,arity)))))
  31. #+END_SRC
  32. * TODO Match Test
  33. :PROPERTIES:
  34. :ID: 9d165cb9-95f2-4006-a8a1-73a0750b2000
  35. :CREATED: <2016-04-30 Sat 16:19>
  36. :ARCHIVE_TIME: 2016-06-13 Mon 20:29
  37. :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org
  38. :ARCHIVE_OLPATH: Conversion from Symbolic Expressions to Typeset Display Formats/Matching And Generating
  39. :ARCHIVE_CATEGORY: lisp-cas
  40. :ARCHIVE_TODO: TODO
  41. :END:
  42. #+Caption: Generate Match Test
  43. #+Name: tex-gen-match-test
  44. #+BEGIN_SRC lisp
  45. ;; (defun generate-match-expression (op arity &optional (type '=))
  46. ;; (declare (symbol op type)
  47. ;; (integer arity))
  48. ;; (ecase type
  49. ;; (=
  50. ;; `(and (eq function ',op)
  51. ;; (= arg-count ,arity)))
  52. ;; (>
  53. ;; `(and (eq function ',op)
  54. ;; (> arg-count ,arity)))
  55. ;; (>=
  56. ;; `(and (eq function ',op)
  57. ;; (>= arg-count ,arity)))))
  58. #+END_SRC
  59. * WORKING Symbolic Differentiation [0/5]
  60. :PROPERTIES:
  61. :CREATED: <2016-06-09 Thu 09:21>
  62. :ID: 360bc5f4-39ac-4161-9326-00c3daaf368c
  63. :ARCHIVE_TIME: 2016-06-14 Tue 10:57
  64. :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org
  65. :ARCHIVE_CATEGORY: lisp-cas
  66. :ARCHIVE_TODO: WORKING
  67. :END:
  68. The calculation of derivatives has many uses. However, the calculation of derivatives can often be tedious. To make this faster, I've written the following program to make it faster.
  69. ** WORKING Expansions [0/3]
  70. CLOSED: [2016-06-09 Thu 09:22]
  71. :PROPERTIES:
  72. :CREATED: <2016-06-09 Thu 09:22>
  73. :END:
  74. This program works in terms of expansion functions, and application tests. That is to say, there is a test to see if the expansion is valid for the given expression.
  75. *** WORKING Definition
  76. :PROPERTIES:
  77. :ID: d7430ac9-cc9a-4942-a8c7-4d21c1705ad4
  78. :CREATED: <2016-06-11 Sat 22:20>
  79. :END:
  80. To define an expansion requires just a bit of syntactic sugar in the form of the ~defexpansion~ macro. This macro does 3 things, generate a test function, generate an expansion function and pushes the name of the expansion, the test function and the expansion function on to the rules list.
  81. To generate the test function, it uses the match-expression generator and wraps it into a function taking two arguments, a function and a list of arguments to the function. The test is then made, acting as predicate function for whether or not the expansion is applicable.
  82. To generate the expansion function, a series of expressions is used as the body of the function, with the function destructured to form the arguments.
  83. #+Caption: Expansion Definition
  84. #+Name: derive-expansion-definition
  85. #+BEGIN_SRC lisp
  86. (defmacro defexpansion (name (on arity &optional (type '=)) (&rest arguments) &body expansion)
  87. (let ((match-expression (generate-match-expression on arity type 'function 'arg-count))
  88. (test-name (symbolicate name '-test))
  89. (expansion-name (symbolicate name '-expansion)))
  90. `(progn
  91. (defun ,test-name (function &rest arguments &aux (arg-count (length arguments)))
  92. ,match-expression)
  93. (defun ,expansion-name (,@arguments)
  94. ,@expansion)
  95. (setf (aget *rules* ',name)
  96. (make-rule :name ',name
  97. :test-function #',test-name
  98. :expansion-function #',expansion-name))
  99. ',name)))
  100. #+END_SRC
  101. *** WORKING Retrieval
  102. :PROPERTIES:
  103. :ID: 71d8545b-d5d1-4179-a0b1-3539c8e68105
  104. :CREATED: <2016-06-11 Sat 22:20>
  105. :END:
  106. To allow for the use of expansions, you must be able to retrieve the correct one from the expansions list.
  107. To do so, you need the second element of the list that is the ~(name test expansion)~ for the rule. This is found by removing the expansions for which the test returns false for the given expression.
  108. #+Caption: Expansion Retrieval
  109. #+Name: derive-expansion-retrieval
  110. #+BEGIN_SRC lisp
  111. (defun get-expansion (expression)
  112. (rule-expansion-function (rest (first
  113. (remove-if-not #'(lambda (nte)
  114. (let ((test (rule-test-function (rest nte))))
  115. (apply test expression)))
  116. ,*rules*)))))
  117. #+END_SRC
  118. *** TODO Storage
  119. :PROPERTIES:
  120. :ID: 0cf2d0ad-cdd1-4a5e-a849-615961c2e869
  121. :CREATED: <2016-06-11 Sat 22:20>
  122. :END:
  123. One of the more important parts of the program is a way to store expansions. This is however, quite boring. It's just a global variable (~*rules*~), containing a list of lists having the form of ~(name test-lambda expander-lambda)~.
  124. #+Caption: Expansion Storage
  125. #+Name: derive-expansion-storage
  126. #+BEGIN_SRC lisp
  127. (defstruct (rule (:type list))
  128. name test-function expansion-function)
  129. (defvar *rules* '())
  130. #+END_SRC
  131. ** WORKING Rules [0/5]
  132. CLOSED: [2016-06-09 Thu 09:22]
  133. :PROPERTIES:
  134. :CREATED: <2016-06-09 Thu 09:22>
  135. :END:
  136. There are many rules for derivation of equations. These rules allow one to derive equations quickly and easily by matching equations up with relevant rules and applying those rules.
  137. *** TODO Multiplication
  138. :PROPERTIES:
  139. :ID: 15f0ba68-9335-4d97-b3c7-418187895706
  140. :CREATED: <2016-06-11 Sat 22:21>
  141. :END:
  142. The derivatives of multiplication follows two rules, the Constant Multiple rule:
  143. \[ \frac{d}{dx} cf(x) = c \cdot f^\prime(x) ,\]
  144. which is a specialized version of the more generalized Product Rule:
  145. \[ \frac{d}{dx} f(x) \cdot g(x) = f(x) \cdot g^\prime(x) + g(x) \cdot f^\prime(x) .\]
  146. There are two forms of the Product Rule as implemented, both matching on the ~*~ function, but taking a different number of arguments. The first takes 2 arguments, and is the main driver for derivation, following the two above rules. The second takes 3 or more, and modifies the arguments slightly so as to make it a derivative of two different equations.
  147. #+Caption: Rules for Multiplication
  148. #+Name: derive-multiplication
  149. #+BEGIN_SRC lisp
  150. (defexpansion mult/2 (* 2) (first second)
  151. (cond
  152. ((numberp first)
  153. `(* ,first ,(derive (ensure-list second))))
  154. ((numberp second)
  155. `(* ,second ,(derive (if (listp first) first (list second)))))
  156. (t
  157. `(+ (* ,first ,(derive (ensure-list second)))
  158. (* ,second ,(derive (ensure-list first)))))))
  159. (defexpansion mult/3+ (* 3 >=) (first &rest rest)
  160. (derive `(* ,first ,(cons '* rest))))
  161. #+END_SRC
  162. *** TODO Division
  163. :PROPERTIES:
  164. :ID: 483285d3-f035-4b50-9f3f-4389d01b7504
  165. :CREATED: <2016-06-11 Sat 22:21>
  166. :END:
  167. Division follows the Quotient Rule, which is as follows:
  168. \[ \frac{d}{dx} \frac{f(x)}{g(x)} = \frac{f^\prime(x) \cdot g(x) - g^\prime(x) \cdot f(x)}{(g(x))^2} .\]
  169. The rule matches on the ~/~ function, and takes 2 arguments, a numerator and a denominator, its expansion is as above.
  170. #+Caption: Rules for Division
  171. #+Name: derive-division
  172. #+BEGIN_SRC lisp
  173. (defexpansion div/2 (/ 2) (numerator denominator)
  174. `(/ (- (* ,numerator ,(derive (ensure-list denominator)))
  175. (* ,denominator ,(derive (ensure-list numerator))))
  176. (expt ,denominator 2)))
  177. #+END_SRC
  178. *** TODO Addition/Subtraction
  179. :PROPERTIES:
  180. :ID: b4f6b80a-0904-491a-a0ca-850dcb6809c5
  181. :CREATED: <2016-06-11 Sat 22:21>
  182. :END:
  183. Addition and subtraction of functions in derivatives is simple, simply add or subtract the derivatives of the functions, as shown here:
  184. \[ \frac{d}{dx} f_1(x) + f_2(x) + \cdots + f_n(x) = f_1^\prime(x) + f_2^\prime(x) + \cdots + f_n^\prime(x) \]
  185. and here:
  186. \[ \frac{d}{dx} f_1(x) - f_2(x) - \cdots - f_n(x) = f_1^\prime(x) - f_2^\prime(x) - \cdots - f_n^\prime(x) .\]
  187. This is accomplished by matching on either ~+~ or ~-~, and taking 2 or more arguments, deriving all of the passed in equations and applying the respective operation.
  188. #+Caption: Rules for Addition and Subtraction
  189. #+Name: derive-addition-subtraction
  190. #+BEGIN_SRC lisp
  191. (defexpansion plus/2+ (+ 2 >=) (&rest clauses)
  192. `(+ ,@(map 'list #'(lambda (clause)
  193. (if (listp clause)
  194. (derive clause)
  195. (derive (list clause))))
  196. clauses)))
  197. (defexpansion minus/2+ (- 2 >=) (&rest clauses)
  198. `(- ,@(map 'list #'(lambda (clause)
  199. (if (listp clause)
  200. (derive clause)
  201. (derive (list clause))))
  202. clauses)))
  203. #+END_SRC
  204. *** TODO Exponentials and Logarithms
  205. :PROPERTIES:
  206. :ID: eaed7558-82d0-4300-8e5f-eb48a06d4e64
  207. :CREATED: <2016-06-11 Sat 22:21>
  208. :END:
  209. The derivatives of exponential and logarithmic functions follow several rules. For $e^x$ or $a^x$, the "Xerox" rule is used:
  210. \[ \frac{d}{dx} e^x = e^x ,\]
  211. and
  212. \[ \frac{d}{dx} a^x = a^x \cdot \ln x .\]
  213. Logarithmic functions follow the forms as shown:
  214. \[ \frac{d}{dx} \ln x = \frac{x^\prime}{x} ,\]
  215. and
  216. \[ \frac{d}{dx} \log_b x = \frac{x^\prime}{\ln b \cdot x} .\]
  217. However, equations of the form $x^n$ follow this form (The Power Rule):
  218. \[ \frac{d}{dx} x^n = x^\prime \cdot n \cdot x^{n-1} .\]
  219. The following rules match based on the appropriate Lisp functions and the number of arguments taken based on whether or not you are performing natural or unnatural operations.
  220. #+Caption: Rules for Exponentials and Logarithms
  221. #+Name: derive-exponentials-logarithms
  222. #+BEGIN_SRC lisp
  223. (defexpansion exp/1 (exp 1) (expression)
  224. (if (listp expression)
  225. `(* (exp ,expression) ,(derive expression))
  226. (if (numberp expression)
  227. 0
  228. `(exp ,expression))))
  229. (defexpansion expt/2 (expt 2) (base exponent)
  230. (if (numberp exponent)
  231. (if (listp base)
  232. `(* ,exponent (expt ,base ,(1- exponent)) ,(derive base))
  233. `(* ,exponent (expt ,base ,(1- exponent))))
  234. `(* (expt ,base ,exponent) (log ,base))))
  235. (defexpansion log/1 (log 1) (expression)
  236. `(/ ,(derive (ensure-list expression)) ,expression))
  237. (defexpansion log/2 (log 2) (number base)
  238. (declare (ignorable number base))
  239. `(/ ,(derive (cons 'log number)) (* (log ,base) ,number)))
  240. #+END_SRC
  241. *** TODO Trigonometric
  242. :PROPERTIES:
  243. :ID: c0f40e80-8a19-4749-bc9b-b1e94ef6949a
  244. :CREATED: <2016-06-11 Sat 22:21>
  245. :END:
  246. The derivation of trigonometric functions is simply the application of the chain rule. As such, each of the trig functions has a different derivative, as shown here:
  247. \[ \frac{d}{dx} \sin x = x^\prime \cdot \cos x ,\]
  248. \[ \frac{d}{dx} \cos x = x^\prime \cdot -\sin x ,\]
  249. \[ \frac{d}{dx} \tan x = x^\prime \cdot \sec^2 x ,\]
  250. \[ \frac{d}{dx} \csc x = x^\prime \cdot -\csc x \cdot \cot x ,\]
  251. \[ \frac{d}{dx} \sec x = x^\prime \cdot \sec x \cdot \tan x ,\]
  252. and
  253. \[ \frac{d}{dx} \cot x = x^\prime \cdot -\csc^2 x .\]
  254. These rules all match on their respective trig function and substitute as appropriate.
  255. #+Caption: Rules for Trigonometric Functions
  256. #+Name: derive-trigonometrics
  257. #+BEGIN_SRC lisp
  258. (defexpansion sin/1 (sin 1) (arg)
  259. `(* (cos ,arg) ,(derive (ensure-list arg))))
  260. (defexpansion cos/1 (cos 1) (arg)
  261. `(* (- (sin ,arg)) ,(derive (ensure-list arg))))
  262. (defexpansion tan/1 (tan 1) (arg)
  263. `(* (expt (sec ,arg) 2) ,(derive (ensure-list arg))))
  264. (defexpansion csc/1 (csc 1) (arg)
  265. `(* (- (csc ,arg)) (cot ,arg) ,(derive (ensure-list arg))))
  266. (defexpansion sec/1 (sec 1) (arg)
  267. `(* (sec ,arg) (tan ,arg) ,(derive (ensure-list arg))))
  268. (defexpansion cot/1 (cot 1) (arg)
  269. `(* (- (expt (csc ,arg) 2)) ,(derive (ensure-list arg))))
  270. #+END_SRC
  271. ** TODO Derivative Driver
  272. :PROPERTIES:
  273. :ID: b03c5070-602a-412e-a6ce-3dda65630153
  274. :CREATED: <2016-06-09 Thu 09:22>
  275. :END:
  276. This function is probably the most important user-facing function in the package.
  277. Derive takes a list, and based on the first element in the list, and the length of the list, it will do one of the following things:
  278. - Number :: Return 0, the derivative of a number is 0, except in certain cases listed above.
  279. - Symbol, and length is 1 :: This is a variable. Return 1, $\frac{d}{dx}x=1$.
  280. - Expansion Function Available :: There is an expansion rule, use this to derive the equation.
  281. - No Expansion Rule :: Signal an error, equation was likely malformed.
  282. #+Caption: Derivative Driver
  283. #+Name: derive-derivative-driver
  284. #+BEGIN_SRC lisp
  285. (defun derive (function)
  286. (check-type function cons)
  287. (let ((op (first function)))
  288. (cond
  289. ((numberp op)
  290. 0)
  291. ((and (symbolp op)
  292. (= 1 (length function)))
  293. 1)
  294. (t
  295. (let ((expansion-function (get-expansion function)))
  296. (if (functionp expansion-function)
  297. (apply expansion-function (rest function))
  298. (error "Undefined expansion: ~a" op)))))))
  299. #+END_SRC
  300. ** TODO Miscellaneous Functions
  301. :PROPERTIES:
  302. :ID: 41439f82-466f-46a5-b706-df43e5f23650
  303. :CREATED: <2016-06-09 Thu 09:22>
  304. :END:
  305. As Common Lisp does not have cosecant or secant functions, and they appear in the definitions of the derivatives of some trigonometric functions, I define them here as follows:
  306. \[ \csc x = \frac{1}{\sin x} \]
  307. \[ \sec x = \frac{1}{\cos x} \]
  308. I also take the liberty of defining two macros, a ~define-equation-functions~ macro and ~take-derivative~. The first defines two functions, one that is the original equation, and the second being the derivative of the original equation. The ~take-derivative~ macro does simply that, but allows you to write the equation without having to quote it, providing a little bit of syntactic sugar.
  309. #+Caption: Miscellaneous Functions
  310. #+Name: derive-misc-functions
  311. #+BEGIN_SRC lisp
  312. (defun csc (x)
  313. "csc -- (csc x)
  314. Calculate the cosecant of x"
  315. (/ (sin x)))
  316. (defun sec (x)
  317. "sec -- (sec x)
  318. Calculate the secant of x"
  319. (/ (cos x)))
  320. (defmacro define-equation-functions (name variable equation)
  321. (let ((derivative-name (symbolicate 'd/d- variable '- name))
  322. (derivative (derive equation)))
  323. `(progn
  324. (defun ,name (,variable)
  325. ,equation)
  326. (defun ,derivative-name (,variable)
  327. ,derivative))))
  328. (defmacro take-derivative (equation)
  329. (let ((derivative (derive equation)))
  330. `',derivative))
  331. #+END_SRC
  332. ** TODO Assembly
  333. :PROPERTIES:
  334. :ID: e15262d2-23d5-4306-a68b-387a21265b6e
  335. :CREATED: <2016-06-09 Thu 09:22>
  336. :END:
  337. Now that the functions, macros and rules are defined, it's time to put them together into a package. This package has only one dependency, Common Lisp itself, and exports the following five symbols: ~derive~, ~csc~, ~sec~, ~define-equation-functions~ and ~take-derivative~.
  338. #+Caption: Packaging
  339. #+Name: derive-packaging
  340. #+BEGIN_SRC lisp :tangle "larcs-derive.lisp"
  341. (in-package #:larcs.derive)
  342. <<derive-expansion-storage>>
  343. <<derive-expansion-retrieval>>
  344. <<derive-match-expressions>>
  345. <<derive-expansion-definition>>
  346. <<derive-derivative-driver>>
  347. <<derive-multiplication>>
  348. <<derive-division>>
  349. <<derive-addition-subtraction>>
  350. <<derive-exponentials-logarithms>>
  351. <<derive-trigonometrics>>
  352. <<derive-misc-functions>>
  353. #+END_SRC