# -*- mode: org -*- Archived entries from file /home/swflint/Projects/lisp-cas/lisp-cas.org * TODO Match Expressions :PROPERTIES: :ID: 39f69de5-6fcc-4ad4-984f-72fc0f77f11b :CREATED: <2016-06-11 Sat 22:20> :ARCHIVE_TIME: 2016-06-13 Mon 20:27 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_OLPATH: Symbolic Differentiation/Expansions :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: TODO :END: 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. #+Caption: Match Expressions #+Name: derive-match-expressions #+BEGIN_SRC lisp ;; (defun generate-match-expression (on arity &optional (type '=)) ;; (check-type on symbol) ;; (check-type type (member = > >=)) ;; (check-type arity (integer 0)) ;; (case type ;; (= ;; `(and (eq function ',on) ;; (= arg-count ,arity))) ;; (> ;; `(and (eq function ',on) ;; (> arg-count ,arity))) ;; (>= ;; `(and (eq function ',on) ;; (>= arg-count ,arity))))) #+END_SRC * TODO Match Test :PROPERTIES: :ID: 9d165cb9-95f2-4006-a8a1-73a0750b2000 :CREATED: <2016-04-30 Sat 16:19> :ARCHIVE_TIME: 2016-06-13 Mon 20:29 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_OLPATH: Conversion from Symbolic Expressions to Typeset Display Formats/Matching And Generating :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: TODO :END: #+Caption: Generate Match Test #+Name: tex-gen-match-test #+BEGIN_SRC lisp ;; (defun generate-match-expression (op arity &optional (type '=)) ;; (declare (symbol op type) ;; (integer arity)) ;; (ecase type ;; (= ;; `(and (eq function ',op) ;; (= arg-count ,arity))) ;; (> ;; `(and (eq function ',op) ;; (> arg-count ,arity))) ;; (>= ;; `(and (eq function ',op) ;; (>= arg-count ,arity))))) #+END_SRC * WORKING Symbolic Differentiation [0/5] :PROPERTIES: :CREATED: <2016-06-09 Thu 09:21> :ID: 360bc5f4-39ac-4161-9326-00c3daaf368c :ARCHIVE_TIME: 2016-06-14 Tue 10:57 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: WORKING :END: 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. ** WORKING Expansions [0/3] CLOSED: [2016-06-09 Thu 09:22] :PROPERTIES: :CREATED: <2016-06-09 Thu 09:22> :END: 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. *** WORKING Definition :PROPERTIES: :ID: d7430ac9-cc9a-4942-a8c7-4d21c1705ad4 :CREATED: <2016-06-11 Sat 22:20> :END: 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. 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. 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. #+Caption: Expansion Definition #+Name: derive-expansion-definition #+BEGIN_SRC lisp (defmacro defexpansion (name (on arity &optional (type '=)) (&rest arguments) &body expansion) (let ((match-expression (generate-match-expression on arity type 'function 'arg-count)) (test-name (symbolicate name '-test)) (expansion-name (symbolicate name '-expansion))) `(progn (defun ,test-name (function &rest arguments &aux (arg-count (length arguments))) ,match-expression) (defun ,expansion-name (,@arguments) ,@expansion) (setf (aget *rules* ',name) (make-rule :name ',name :test-function #',test-name :expansion-function #',expansion-name)) ',name))) #+END_SRC *** WORKING Retrieval :PROPERTIES: :ID: 71d8545b-d5d1-4179-a0b1-3539c8e68105 :CREATED: <2016-06-11 Sat 22:20> :END: To allow for the use of expansions, you must be able to retrieve the correct one from the expansions list. 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. #+Caption: Expansion Retrieval #+Name: derive-expansion-retrieval #+BEGIN_SRC lisp (defun get-expansion (expression) (rule-expansion-function (rest (first (remove-if-not #'(lambda (nte) (let ((test (rule-test-function (rest nte)))) (apply test expression))) ,*rules*))))) #+END_SRC *** TODO Storage :PROPERTIES: :ID: 0cf2d0ad-cdd1-4a5e-a849-615961c2e869 :CREATED: <2016-06-11 Sat 22:20> :END: 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)~. #+Caption: Expansion Storage #+Name: derive-expansion-storage #+BEGIN_SRC lisp (defstruct (rule (:type list)) name test-function expansion-function) (defvar *rules* '()) #+END_SRC ** WORKING Rules [0/5] CLOSED: [2016-06-09 Thu 09:22] :PROPERTIES: :CREATED: <2016-06-09 Thu 09:22> :END: 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. *** TODO Multiplication :PROPERTIES: :ID: 15f0ba68-9335-4d97-b3c7-418187895706 :CREATED: <2016-06-11 Sat 22:21> :END: The derivatives of multiplication follows two rules, the Constant Multiple rule: \[ \frac{d}{dx} cf(x) = c \cdot f^\prime(x) ,\] which is a specialized version of the more generalized Product Rule: \[ \frac{d}{dx} f(x) \cdot g(x) = f(x) \cdot g^\prime(x) + g(x) \cdot f^\prime(x) .\] 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. #+Caption: Rules for Multiplication #+Name: derive-multiplication #+BEGIN_SRC lisp (defexpansion mult/2 (* 2) (first second) (cond ((numberp first) `(* ,first ,(derive (ensure-list second)))) ((numberp second) `(* ,second ,(derive (if (listp first) first (list second))))) (t `(+ (* ,first ,(derive (ensure-list second))) (* ,second ,(derive (ensure-list first))))))) (defexpansion mult/3+ (* 3 >=) (first &rest rest) (derive `(* ,first ,(cons '* rest)))) #+END_SRC *** TODO Division :PROPERTIES: :ID: 483285d3-f035-4b50-9f3f-4389d01b7504 :CREATED: <2016-06-11 Sat 22:21> :END: Division follows the Quotient Rule, which is as follows: \[ \frac{d}{dx} \frac{f(x)}{g(x)} = \frac{f^\prime(x) \cdot g(x) - g^\prime(x) \cdot f(x)}{(g(x))^2} .\] The rule matches on the ~/~ function, and takes 2 arguments, a numerator and a denominator, its expansion is as above. #+Caption: Rules for Division #+Name: derive-division #+BEGIN_SRC lisp (defexpansion div/2 (/ 2) (numerator denominator) `(/ (- (* ,numerator ,(derive (ensure-list denominator))) (* ,denominator ,(derive (ensure-list numerator)))) (expt ,denominator 2))) #+END_SRC *** TODO Addition/Subtraction :PROPERTIES: :ID: b4f6b80a-0904-491a-a0ca-850dcb6809c5 :CREATED: <2016-06-11 Sat 22:21> :END: Addition and subtraction of functions in derivatives is simple, simply add or subtract the derivatives of the functions, as shown here: \[ \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) \] and here: \[ \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) .\] 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. #+Caption: Rules for Addition and Subtraction #+Name: derive-addition-subtraction #+BEGIN_SRC lisp (defexpansion plus/2+ (+ 2 >=) (&rest clauses) `(+ ,@(map 'list #'(lambda (clause) (if (listp clause) (derive clause) (derive (list clause)))) clauses))) (defexpansion minus/2+ (- 2 >=) (&rest clauses) `(- ,@(map 'list #'(lambda (clause) (if (listp clause) (derive clause) (derive (list clause)))) clauses))) #+END_SRC *** TODO Exponentials and Logarithms :PROPERTIES: :ID: eaed7558-82d0-4300-8e5f-eb48a06d4e64 :CREATED: <2016-06-11 Sat 22:21> :END: The derivatives of exponential and logarithmic functions follow several rules. For $e^x$ or $a^x$, the "Xerox" rule is used: \[ \frac{d}{dx} e^x = e^x ,\] and \[ \frac{d}{dx} a^x = a^x \cdot \ln x .\] Logarithmic functions follow the forms as shown: \[ \frac{d}{dx} \ln x = \frac{x^\prime}{x} ,\] and \[ \frac{d}{dx} \log_b x = \frac{x^\prime}{\ln b \cdot x} .\] However, equations of the form $x^n$ follow this form (The Power Rule): \[ \frac{d}{dx} x^n = x^\prime \cdot n \cdot x^{n-1} .\] 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. #+Caption: Rules for Exponentials and Logarithms #+Name: derive-exponentials-logarithms #+BEGIN_SRC lisp (defexpansion exp/1 (exp 1) (expression) (if (listp expression) `(* (exp ,expression) ,(derive expression)) (if (numberp expression) 0 `(exp ,expression)))) (defexpansion expt/2 (expt 2) (base exponent) (if (numberp exponent) (if (listp base) `(* ,exponent (expt ,base ,(1- exponent)) ,(derive base)) `(* ,exponent (expt ,base ,(1- exponent)))) `(* (expt ,base ,exponent) (log ,base)))) (defexpansion log/1 (log 1) (expression) `(/ ,(derive (ensure-list expression)) ,expression)) (defexpansion log/2 (log 2) (number base) (declare (ignorable number base)) `(/ ,(derive (cons 'log number)) (* (log ,base) ,number))) #+END_SRC *** TODO Trigonometric :PROPERTIES: :ID: c0f40e80-8a19-4749-bc9b-b1e94ef6949a :CREATED: <2016-06-11 Sat 22:21> :END: 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: \[ \frac{d}{dx} \sin x = x^\prime \cdot \cos x ,\] \[ \frac{d}{dx} \cos x = x^\prime \cdot -\sin x ,\] \[ \frac{d}{dx} \tan x = x^\prime \cdot \sec^2 x ,\] \[ \frac{d}{dx} \csc x = x^\prime \cdot -\csc x \cdot \cot x ,\] \[ \frac{d}{dx} \sec x = x^\prime \cdot \sec x \cdot \tan x ,\] and \[ \frac{d}{dx} \cot x = x^\prime \cdot -\csc^2 x .\] These rules all match on their respective trig function and substitute as appropriate. #+Caption: Rules for Trigonometric Functions #+Name: derive-trigonometrics #+BEGIN_SRC lisp (defexpansion sin/1 (sin 1) (arg) `(* (cos ,arg) ,(derive (ensure-list arg)))) (defexpansion cos/1 (cos 1) (arg) `(* (- (sin ,arg)) ,(derive (ensure-list arg)))) (defexpansion tan/1 (tan 1) (arg) `(* (expt (sec ,arg) 2) ,(derive (ensure-list arg)))) (defexpansion csc/1 (csc 1) (arg) `(* (- (csc ,arg)) (cot ,arg) ,(derive (ensure-list arg)))) (defexpansion sec/1 (sec 1) (arg) `(* (sec ,arg) (tan ,arg) ,(derive (ensure-list arg)))) (defexpansion cot/1 (cot 1) (arg) `(* (- (expt (csc ,arg) 2)) ,(derive (ensure-list arg)))) #+END_SRC ** TODO Derivative Driver :PROPERTIES: :ID: b03c5070-602a-412e-a6ce-3dda65630153 :CREATED: <2016-06-09 Thu 09:22> :END: This function is probably the most important user-facing function in the package. 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: - Number :: Return 0, the derivative of a number is 0, except in certain cases listed above. - Symbol, and length is 1 :: This is a variable. Return 1, $\frac{d}{dx}x=1$. - Expansion Function Available :: There is an expansion rule, use this to derive the equation. - No Expansion Rule :: Signal an error, equation was likely malformed. #+Caption: Derivative Driver #+Name: derive-derivative-driver #+BEGIN_SRC lisp (defun derive (function) (check-type function cons) (let ((op (first function))) (cond ((numberp op) 0) ((and (symbolp op) (= 1 (length function))) 1) (t (let ((expansion-function (get-expansion function))) (if (functionp expansion-function) (apply expansion-function (rest function)) (error "Undefined expansion: ~a" op))))))) #+END_SRC ** TODO Miscellaneous Functions :PROPERTIES: :ID: 41439f82-466f-46a5-b706-df43e5f23650 :CREATED: <2016-06-09 Thu 09:22> :END: 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: \[ \csc x = \frac{1}{\sin x} \] \[ \sec x = \frac{1}{\cos x} \] 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. #+Caption: Miscellaneous Functions #+Name: derive-misc-functions #+BEGIN_SRC lisp (defun csc (x) "csc -- (csc x) Calculate the cosecant of x" (/ (sin x))) (defun sec (x) "sec -- (sec x) Calculate the secant of x" (/ (cos x))) (defmacro define-equation-functions (name variable equation) (let ((derivative-name (symbolicate 'd/d- variable '- name)) (derivative (derive equation))) `(progn (defun ,name (,variable) ,equation) (defun ,derivative-name (,variable) ,derivative)))) (defmacro take-derivative (equation) (let ((derivative (derive equation))) `',derivative)) #+END_SRC ** TODO Assembly :PROPERTIES: :ID: e15262d2-23d5-4306-a68b-387a21265b6e :CREATED: <2016-06-09 Thu 09:22> :END: 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~. #+Caption: Packaging #+Name: derive-packaging #+BEGIN_SRC lisp :tangle "larcs-derive.lisp" (in-package #:larcs.derive) <> <> <> <> <> <> <> <> <> <> <> #+END_SRC * WORKING Symbolic Form to Typeset [0/5] :PROPERTIES: :CREATED: <2016-06-09 Thu 09:23> :ID: ed9f4311-bf9f-42df-8f46-254658b93c10 :ARCHIVE_TIME: 2016-06-14 Tue 19:10 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: WORKING :END: The goal of this portion of the CAS is to produce \LaTeX{} formulae that can be inserted into a document for whatever reason, and it does so using rewrite rules, this time, rewriting s-expressions (~(+ (* 3 (expt x 3)) (expt x 2) (* 4 x) 22)~) to the \LaTeX{} equivalent, ~${{{{3} \cdot {{x ^ {3}}}}} + {{x ^ {2}}} + {{{4} \cdot {x}}} + {22}}$~ (${{{{3} \cdot {{x ^ {3}}}}} + {{x ^ {2}}} + {{{4} \cdot {x}}} + {22}}$). ** WORKING Matching And Generating [0/3] :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :END: *** TODO Define Rule :PROPERTIES: :ID: d4f77ac3-a059-4fb6-b936-1b9e972646ee :CREATED: <2016-04-30 Sat 16:19> :END: #+Caption: Define Matching Rule #+Name: tex-def-match-rule #+BEGIN_SRC lisp (defmacro defrule (name (on arity &optional type) (&rest arguments) &body rule) (let ((match-expression (generate-match-expression on arity type 'function 'arg-count)) (test-name (symbolicate name '-test)) (expansion-name (symbolicate name '-expansion))) `(progn (defun ,test-name (function &rest arguments &aux (arg-count (length arguments))) ,match-expression) (defun ,expansion-name (,@arguments) ,@rule) (setf (aget *rules* ',name) (make-rule :name ',name :test-function #',test-name :expansion-function #',expansion-name)) ',name))) #+END_SRC *** TODO Store Rules :PROPERTIES: :ID: 002ea704-4286-429f-9149-0f29fb73c503 :CREATED: <2016-04-30 Sat 16:19> :END: #+Caption: Rule Storage #+Name: tex-rule-storage #+BEGIN_SRC lisp (defstruct (rule (:type list)) name test-function expansion-function) (defvar *rules* '()) #+END_SRC *** TODO Retrieve Rule :PROPERTIES: :CREATED: <2016-04-30 Sat 15:25> :ID: e3f34100-d0a5-4039-8b9d-115cfcb0804e :END: #+Caption: Retrieve Rule #+Name: tex-retrieve-rule #+BEGIN_SRC lisp (defun get-expansion (expression) (rule-expansion-function (rest (first (remove-if-not #'(lambda (nte) (let ((test (rule-test-function (rest nte)))) (apply test expression))) ,*rules*))))) #+END_SRC ** WORKING Rules [0/10] :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :END: *** TODO Multiplication :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :ID: 5417a6bf-f265-418a-984b-6bfd14b79a80 :END: #+Caption: Multiplication Rule #+Name: tex-multiplication-rule #+BEGIN_SRC lisp (defrule multiplication (* 2 >=) (&rest elements) (format nil "{~{{~a}~^ \\cdot ~}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) #+END_SRC *** TODO Division :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :ID: 056aa99c-f2b9-4ab6-99ba-bfb87e3baed5 :END: #+Caption: Division Rule #+Name: tex-division-rule #+BEGIN_SRC lisp (defrule division (/ 2 =) (a b) (format nil "{\\frac{~a}{~a}}" (convert-to-tex (ensure-list a)) (convert-to-tex (ensure-list b)))) #+END_SRC *** TODO Addition :PROPERTIES: :ID: 68f3dac3-9f0a-4fee-8da6-a39f4491f3ce :CREATED: <2016-04-30 Sat 16:19> :END: #+Caption: Rule for addition #+Name: tex-addition-rule #+BEGIN_SRC lisp (defrule addition (+ 2 >=) (&rest elements) (format nil "{~{{~a}~^ + ~}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) #+END_SRC *** TODO Subtraction :PROPERTIES: :ID: 9a908130-af5e-4c87-bb07-13bd66c35fcf :CREATED: <2016-04-30 Sat 16:19> :END: #+Caption: Subtraction Rule #+Name: tex-subtraction-rule #+BEGIN_SRC lisp (defrule subtraction (- 2 >=) (&rest elements) (format nil "{~{{~a}~^ - ~}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) #+END_SRC *** TODO Exponentials and Logarithmics :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :ID: 269dc47f-5062-4081-a08e-d50188af6a57 :END: #+Caption: Exponentials and Logarithms #+Name: tex-exponentials-and-logarithms #+BEGIN_SRC lisp (defrule exp (exp 1 =) (expression) (format nil "{e^{~a}}" (convert-to-tex (ensure-list expression)))) (defrule expt (expt 2 =) (base exponent) (format nil "{~a ^ {~a}}" (convert-to-tex (ensure-list base)) (convert-to-tex (ensure-list exponent)))) (defrule natlog (log 1 =) (expression) (format nil "{\\ln {~a}}" (convert-to-tex (ensure-list expression)))) (defrule logarithm (log 2 =) (expression base) (format nil "{\\log_{~a}~a}" (convert-to-tex (ensure-list base)) (convert-to-tex (ensure-list expression)))) #+END_SRC *** TODO Trigonometrics :PROPERTIES: :CREATED: <2016-04-30 Sat 16:19> :ID: 837806c9-7174-43a3-80b2-355b645d46ed :END: #+Caption: Trigonometric Functions #+Name: tex-trigonometrics #+BEGIN_SRC lisp (defrule sin (sin 1 =) (arg) (format nil "{\\sin {~a}}" (convert-to-tex (ensure-list arg)))) (defrule cos (cos 1 =) (arg) (format nil "{\\cos {~a}}" (convert-to-tex (ensure-list arg)))) (defrule tan (tan 1 =) (arg) (format nil "{\\tan {~a}}" (convert-to-tex (ensure-list arg)))) (defrule csc (csc 1 =) (arg) (format nil "{\\csc {~a}}" (convert-to-tex (ensure-list arg)))) (defrule sec (sec 1 =) (arg) (format nil "{\\sec {~a}}" (convert-to-tex (ensure-list arg)))) (defrule cot (cot 1 =) (arg) (format nil "{\\cot {~a}}" (convert-to-tex (ensure-list arg)))) #+END_SRC *** TODO Logic :PROPERTIES: :CREATED: <2016-04-30 Sat 18:29> :ID: 74d12931-343f-4982-945d-738a3e38a1db :END: #+Caption: Logic Rules #+Name: tex-logic-rules #+BEGIN_SRC lisp (defrule and (and 2 >=) (&rest elements) (format nil "{~{{~a}~^ \\wedge ~}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) (defrule or (or 2 >=) (&rest elements) (format nil "{~{{~a}~^ \\vee ~}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) (defrule not (not 1 =) (&rest elements) (format nil "{\\not {~a}}" (map 'list #'convert-to-tex (map 'list #'ensure-list elements)))) #+END_SRC *** TODO Equality :PROPERTIES: :CREATED: <2016-04-30 Sat 18:29> :ID: f75273d2-d523-4404-925b-af6fd01c7520 :END: #+Caption: Equality Rules #+Name: tex-equality-rules #+BEGIN_SRC lisp (defrule = (= 2 =) (lhs rhs) (format nil "{{~a} = {~a}}" (convert-to-tex (ensure-list lhs)) (convert-to-tex (ensure-list rhs)))) #+END_SRC *** TODO Summation and Integration :PROPERTIES: :CREATED: <2016-04-30 Sat 18:30> :ID: dda2827a-cee5-4efc-bd9a-4dd953829b5c :END: #+Caption: Summation and Integration #+Name: tex-summation-and-integration #+BEGIN_SRC lisp (defrule sum (sum 3 =) (start stop expression) (format nil "{\\sum_{~a}^{~a} {~a}}" (convert-to-tex (ensure-list start)) (convert-to-tex (ensure-list stop)) (convert-to-tex (ensure-list expression)))) (defrule integrate (integrate 4 =) (from to expression wrt) (format nil "{\\int_{~a}^{~a} ~a\\,\mathrm{d}~a}" (convert-to-tex (ensure-list from)) (convert-to-tex (ensure-list to)) (convert-to-tex (ensure-list expression)) (convert-to-tex (ensure-list wrt)))) #+END_SRC *** TODO Specialty :PROPERTIES: :CREATED: <2016-04-30 Sat 18:30> :ID: f4e6b309-289d-4b32-bc55-4740ec86a113 :END: #+Caption: Specialty #+Name: tex-specialty #+BEGIN_SRC lisp (defrule parens (parens 2 =) (type inside) (let* ((types '((square . ("[" . "]")) (curly . ("{" . "}")) (smooth . ("(" . ")")))) (left (cadr (assoc type types))) (right (cddr (assoc type types)))) (format nil "{\\left~a {~a} \\right~a}" left (convert-to-tex (ensure-list inside)) right))) #+END_SRC ** TODO Conversion Driver :PROPERTIES: :ID: b395bdb7-7b98-49a1-b6d6-4256fb40d4fa :CREATED: <2016-04-30 Sat 16:19> :END: #+Caption: Conversion Driver #+Name: tex-conversion-driver #+BEGIN_SRC lisp (defvar *tex-outputp* nil) (declaim (special *tex-outputp*)) (defmacro with-tex-output (&body body) `(if *tex-outputp* (progn ,@body) (let ((*tex-outputp* t)) (format nil "$~a$" (progn ,@body))))) (defun convert-to-tex (function) (check-type function cons) (let ((op (first function))) (with-tex-output (cond ((numberp op) (format nil "~a" op)) ((and (symbolp op) (= 1 (length function))) (let ((symbol-pair (assoc op *special-symbols-to-sequences*))) (if (null symbol-pair) (string-downcase op) (cdr symbol-pair)))) (t (let ((expansion-function (get-expansion function))) (if (functionp expansion-function) (apply expansion-function (rest function)) (error "Undefined expansion for operation: ~a." op)))))))) #+END_SRC ** TODO Miscellaneous Functions :PROPERTIES: :CREATED: <2016-04-30 Sat 16:09> :ID: a4ab8a72-0b09-453c-b936-2470d5429c05 :END: #+Caption: Misc Functions #+Name: tex-misc-functions #+BEGIN_SRC lisp ;; (defvar *special-symbols-to-sequences* ;; '((alpha . "\\alpha") ;; (beta . "\\beta") ;; (gamma . "\\gamma") ;; (delta . "\\delta") ;; (epsilon . "\\epsilon") ;; (varepsilon . "\\varepsilon") ;; (zeta . "\\zeta") ;; (eta . "\\eta") ;; (theta . "\\theta") ;; (vartheta . "\\vartheta") ;; (gamma . "\\gamma") (kappa . "\\kappa") ;; (lambda . "\\lambda") ;; (mu . "\\mu") ;; (nu . "\\nu") ;; (xi . "\\xi") ;; (omicron . "\\o") ;; (pi . "\\pi") ;; (varpi . "\\varpi") ;; (rho . "\\rho") ;; (varrho . "\\varrho") ;; (sigma . "\\sigma") ;; (varsigm . "\\varsigm") ;; (tau . "\\tau") ;; (upsilon . "\\upsilon") ;; (phi . "\\phi") ;; (varphi . "\\varphi") ;; (chi . "\\chi") ;; (psi . "\\psi") ;; (omega . "\\omega") ;; (big-gamma . "\\Gamma") ;; (big-delta . "\\Delta") ;; (big-theta . "\\Theta") ;; (big-lambda . "\\Lambda") ;; (big-xi . "\\Xi") ;; (big-pi . "\\Pi") ;; (big-sigma . "\\Sigma") ;; (big-upsilon . "\\Upsilon") ;; (big-phi . "\\Phi") ;; (big-psi . "\\Psi") ;; (big-omega . "\\Omega"))) #+END_SRC ** TODO Assembly :PROPERTIES: :ID: fdef3016-cb12-43ad-ba5f-14dd6ccd973c :CREATED: <2016-04-30 Sat 16:25> :END: #+Caption: Packaging #+Name: tex-packaging #+BEGIN_SRC lisp :tangle "larcs-tex.lisp" (in-package #:larcs.to-tex) <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> #+END_SRC * TODO Manipulator Miscellaneous Functions :PROPERTIES: :CREATED: <2016-05-03 Tue 15:38> :ID: 20450528-d763-4c14-a085-5ac54d4d4b85 :ARCHIVE_TIME: 2016-06-27 Mon 18:44 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_OLPATH: Algebraic Manipulation/Expression Manipulators :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: TODO :END: This defines the ~*manipulator-map*~, where the manipulators for various functions are stored, and defines a function to generate an arguments list given a count of arguments. #+Caption: Misc Manipulator Functions #+Name: am-misc-manipulator-functions #+BEGIN_SRC lisp (defvar *manipulator-map* '()) #+END_SRC * TODO Match Expression Generation :PROPERTIES: :CREATED: <2016-06-13 Mon 17:18> :ID: f7876b1d-3b67-48c1-863a-85e1b3026ed6 :ARCHIVE_TIME: 2016-08-05 Fri 21:47 :ARCHIVE_FILE: ~/Projects/lisp-cas/lisp-cas.org :ARCHIVE_OLPATH: Common Functionality :ARCHIVE_CATEGORY: lisp-cas :ARCHIVE_TODO: TODO :END: 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. #+Caption: Match Expression Generation #+Name: common-match-expression-generation #+BEGIN_SRC lisp (defun generate-match-expression (on arity &optional (type '=) (function-var 'function) (arg-count-var 'arg-count)) (check-type on symbol) (check-type type (member = > >=)) (check-type arity (integer 0)) (case type (= `(and (eq ,function-var ',on) (= ,arg-count-var ,arity))) (> `(and (eq ,function-var ',on) (> ,arg-count-var ,arity))) (>= `(and (eq ,function-var ',on) (>= ,arg-count-var ,arity))))) #+END_SRC