larcs.org 54 KB

LARCS

Introduction

CLOSED: [2016-12-14 Wed 14:59]

LARCS is a Computer Algebra System, written from scratch, using the technique of literate programming to accomplish the goal. The purpose of this is threefold: first, as a learning exercise; second, to develop a CAS that is built around a clean, readable and modular design, rather than efficiency; and third, to demonstrate the abilities of Literate Programming in general, and Org-Mode in particular. There is another motive, many CASs are non-free, and those that aren't don't always provide a useful programming interface, and with the goal of a clean, readable and modular design, this can be accomplished.

What's in a name?

CLOSED: [2016-12-14 Wed 15:13]

While the Introduction describes the purpose of LARCS, it does not describe the name. LARCS stands for Lisp Automated Rewrite and Calculation System. This name describes the system quite accurately, as LARCS was written in Lisp, provides an automated system for rewriting equations into various forms, and calculating given an equation and values of the various variables, and acts as a coherent grouping of libraries forming a system.

TOC   ignore

WORKING Expression Types [1/4]

All expressions are built from various <expression> objects. These objects are specialized to contain only certain, specific information, relevant to a particular expression. The most basic expressions (besides expression itself) are <atomic> (holding things like numbers or variables) and <compound> (holding things like multiplicatinos, divisions, exponents, trigonometric expressions, arithmetic expressions, etc.). All subtypes of <expression> must know if they are atomic, so we define a generic for this, they must also tell if they are eqal (a form of equality), and be able to perform substitution, evaluation, and simplification, however, the latter three are implemented elsewhere. The organization of the various types may be found in Figure~fig:expression-types.

Note, by default, eqal will handle type-error by trying to switch the order of the arguments. If this fails, it will warn that an applicable method for the given types, and return nil.

  @export
  (defclass <expression> () ())

  @export
  (defgeneric atomicp (expression))

  @export
  (defgeneric eqal (expression-a expression-b))

  (defmethod eqal ((expression-a <expression>) (expression-b <expression>))
    (if (> (length (compute-applicable-methods #'eqal (list expression-b expression-a))) 2)
        (eqal expression-b expression-a)
        (warn "No definition of `eqal' for types `~a' and `~a'."
              (type-of expression-a)
              (type-of expression-b))))

  (defmethod eqal :around ((expression-a <expression>) (expression-b <expression>))
    (handler-case (call-next-method)
      (type-error (err)
        (declare (ignore err))
        (eqal expression-b expression-a))))

  @export
  (defgeneric copy-expression (expression))

  @export
  (defgeneric substitute-expression (expression with in))

  @export
  (defgeneric evaluate (expression &key substitutions-list resolve-constants-light-p resolve-constants-hard-p))

  @export
  (defgeneric simplify (expression &key resolve-constants-light-p resolve-constants-hard-p))

  @export
  (defgeneric to-sexp (expression &optional resolve-constants-p))
  digraph {
          expression [label = "<expression>"];

          atomic [label = "<atomic>"];
          number [label = "<number>"];
          variable [label = "<variable>"];
          constant [label = "<constant>"];

          compound [label = "<compound>"];
          mult [label = "<multiplication>"];
          div [label = "<division>"];
          expt [label = "<expt>"];
          exp [label = "<exp>"];
          trig [label = "<trig>"];
          log [label = "<log>"];
          ln [label = "<ln>"];
          add [label = "<addition>"];
          sub [label = "<subtraction>"];

          polyTerm [label = "<polynomial-term>"];

          expression -> atomic;
          atomic -> number -> constant;
          atomic -> variable -> constant;

          expression -> compound;
          compound -> mult -> polyTerm;
          compound -> div;
          compound -> expt -> exp;
          expt -> polyTerm;
          compound -> trig;
          compound -> add;
          compound -> sub;
          compound -> log -> ln;

          variable -> polyTerm;

  }

img/types.png

Atomic Types [3/3]

Of the various types, there are three atomic types, <number>, <variable> and <constant> They are very similar, only differing on the :type their value slot has. As all <atomic> subtypes are, in fact, atomic (save for some later blended types), we can simply return t for atomicp. Further, all <atomic> have a definite value, so we should be able to retrieve this.

  @export
  (defclass <atomic> (<expression>) ())

  @export
  (defgeneric value (expression &key resolve-constants))

  (defmethod atomicp ((expression <atomic>))
    (declare (ignore expression))
    t)

Numbers

CLOSED: [2019-01-16 Wed 16:01]

As an <atomic>, <number> is rather simple, having only one slot, value, readable using the value method, and initialized with :value. This value must be of type number. Two <number> objects are equal, if their value is =.

  @export
  (defclass <number> (<atomic>)
    ((value :initarg :value
            :type number)))

  (defmethod value ((expression <number>) &key resolve-constants)
    (declare (ignore resolve-constants))
    (slot-value expression 'value))

  (defmethod eqal ((expression-a <number>) (expression-b <number>))
    (= (value expression-a) (value expression-b)))

  (defmethod to-sexp ((expression <number>) &optional resolve-constants-p)
    (declare (ignorable resolve-constants-p))
    (slot-value expression 'value))

  (defmethod copy-expression ((expression <number>))
    (make-instance '<number> :value (slot-value expression 'value)))

Variables

CLOSED: [2019-01-16 Wed 16:13]

<variable> is similar to <number>, with the only difference being name must be a symbol or keyword. Two <variable> objects are eqal if the name of each is equal.

  @export
  (defclass <variable> (<atomic>)
    ((name :initarg :name
           :type (or symbol keyword))))

  (defmethod value ((expression <variable>) &key resolve-constants)
    (declare (ignore resolve-constants))
    (slot-value expression 'name))

  (defmethod eqal ((expression-a <variable>) (expression-b <variable>))
    (equal (value expression-a)
            (value expression-b)))

  (defmethod to-sexp ((expression <variable>) &optional resolve-constants-p)
    (declare (ignore resolve-constants-p))
    (slot-value expression 'name))

  (defmethod copy-expression ((expression <variable>))
    (make-instance '<variable>
                   :name (slot-value expression 'name)))

Constants

CLOSED: [2019-01-16 Wed 16:23]

<constant> is a bit different from the rest – it has a name, as well as a number, and the value depends on whether or not you want to "resolve" the constant. If the constant is to be resolved, the numeric value is returned, otherwise, the name is returned.

Equality of constants is a bit different, and is checked as follows:

<constant>, <constant>

Check if name is equal.

<constant>, <variable>

Check if name is equal.

<constant>, <number>

Check if value is =.

  @export
  (defclass <constant> (<number> <variable>)
    ((name :initarg :name
           :type (or symbol keyword))
     (value :initarg :value
            :type number)))

  (defmethod value ((expression <constant>) &key resolve-constants)
    (if resolve-constants
        (slot-value expression 'value)
        (slot-value expression 'name)))

  (defmethod eqal ((expression-a <constant>) (expression-b <constant>))
    (equal (slot-value expression-a 'name)
           (slot-value expression-b 'name)))

  (defmethod eqal ((expression-a <constant>) (expression-b <variable>))
    (equal (slot-value expression-a 'name)
           (slot-value expression-b 'name)))

  (defmethod eqal ((expression-a <constant>) (expression-b <number>))
    (= (slot-value expression-a 'value)
       (slot-value expression-b 'value)))

  (defmethod to-sexp ((expression <constant>) &optional resolve-constants-p)
    (if resolve-constants-p
        (slot-value expression 'value)
        (slot-value expression 'name)))

  (defmethod copy-expression ((expression <constant>))
    (make-instance '<constant>
                   :name (slot-value expression 'name)
                   :value (slot-value expression 'value)))

WORKING Compound Types [5/7]

Compound types are the majority of expressions. An atom is well and good, but often says relatively little. We use compound types to form equations from other expressions. These are not, then, atomicp.

  @export
  (defclass <compound> (<expression>) ())

  (defmethod atomicp ((expression <compound>))
    (declare (ignore expression))
    nil)

Additions

CLOSED: [2019-01-17 Thu 09:53]

<addition> is simple, an <addition> has only a list of terms. As with <multiplication>, two <addition> objects are equal if they have the same number of terms and all terms are the same, order not withstanding (as addition is commutative).

  @export
  (defclass <addition> (<compound>)
    ((terms :reader terms
            :initarg :terms
            :type (list <expression>))))

  (defmethod eqal ((expression-a <addition>) (expression-b <addition>))
    (let ((terms-a (terms expression-a))
          (terms-b (terms expression-b)))
      (and (= (length terms-a)
            (length terms-b))
         (null (set-difference terms-a terms-b
                               :test #'eqal)))))

  (defmethod to-sexp ((expression <addition>) &optional resolve-constants-p)
    `(+ ,@(mapcar #'(lambda (exp) (to-sexp exp resolve-constants-p))
                  (slot-value expression 'terms))))

Subtractions

CLOSED: [2019-01-17 Thu 10:01]

Subtractions again, contain only a list of terms. However, unlike other types having only a list of terms, the terms are not a set, and therefore order as well as content must be verified to check eqal-ness.

  @export
  (defclass <subtraction> (<compound>)
    ((terms :reader terms
            :initarg :terms
            :type (list <expression>))))

  (defmethod eqal ((expression-a <subtraction>) (expression-b <subtraction>))
    (let ((a-terms (terms expression-a))
          (b-terms (terms expression-b)))
      (and (= (length a-terms)
            (length b-terms))
         (do* ((keep (eqal (first a-terms) (first b-terms)) (eqal (first a) (first b)))
               (a a-terms (rest a))
               (b b-terms (rest b)))
              ((or (not keep) (null a) (null b)) keep)))))

  (defmethod to-sexp ((expression <subtraction>) &optional resolve-constants-p)
    `(- ,@(mapcar #'(lambda (exp) (to-sexp exp resolve-constants-p))
                  (slot-value expression 'terms))))

Multiplications

CLOSED: [2019-01-17 Thu 10:02]

Multiplication is one of the more frequently used expression types, and is, surprisingly, simple. A <multiplication> has only one slot, terms, the various expressions to be multiplied. Further, if two <multiplication> objects have the same number of terms, and there is no set-difference given eqal between the two lists of terms, the two <multiplication> objects are eqal.

  @export
  (defclass <multiplication> (<compound>)
    ((terms :reader terms
            :initarg :terms
            :type (list <expression>))))

  (defmethod eqal ((expression-a <multiplication>) (expression-b <multiplication>))
    (and (= (length (terms expression-a))
          (length (terms expression-b)))
       (null (set-difference (terms expression-a) (terms expression-b)
                             :test #'eqal))))

  (defmethod to-sexp ((expression <multiplication>) &optional resolve-constants-p)
    `(* ,@(mapcar #'(lambda (exp) (to-sexp exp resolve-constants-p))
                  (slot-value expression 'terms))))

Divisions

CLOSED: [2019-01-17 Thu 10:12]

Division is similar to <multiplication>, although instead of having terms it has numerator and denominator. A <division> is equal to another if the numerator of the first is eqal to the second and likewise for the denominator.

  @export
  (defclass <division> (<compound>)
    ((numerator :reader div-numerator
                :initarg :numerator
                :type <expression>)
     (denominator ::reader div-denominator
                  :initarg :denominator
                  :type <expression>)))

  (defmethod eqal ((expression-a <division>) (expression-b <division>))
    (and (eqal (div-numerator expression-a)
             (div-numerator expression-b))
       (eqal (div-denominator expression-a)
             (div-denominator expression-b))))

  (defmethod to-sexp ((expression <division>) &optional resolve-constants-p)
    `(/ ,(to-sexp (slot-value expression 'numerator) resolve-constants-p)
        ,(to-sexp (slot-value expression 'denominator) resolve-constants-p)))

Exponentials

CLOSED: [2019-05-02 Thu 18:40]

There are two primary forms of exponential – the natural (exp) and the general (expt). Given this, there are two classes, with <exp> being a subclass of <expt>, mirroring the generality of exponentials in general. Two <expt> objects are eqal iff their base and exponent are eqal.

  @export
  (defclass <expt> (<compound>)
    ((base :reader base
           :initarg :base
           :type <expression>)
     (exponent :reader exponent
               :initarg :exponent
               :type <expression>)))

  (defmethod eqal ((expression-a <expt>) (expression-b <expt>))
    (and (eqal (base expression-a)
             (base expression-b))
       (eqal (exponent expression-a)
             (exponent expression-b))))

  (defmethod to-sexp ((expression <expt>) &optional resolve-constants-p)
    `(expt ,(to-sexp (base expression) resolve-constants-p)
           ,(to-sexp (slot-value expression 'exponent) resolve-constants-p)))

  @export
  (defclass <exp> (<expt>)
    ((exponent :reader exponent
               :initarg :exponent
               :type <expression>)))

  (defmethod base ((exp <exp>))
    (make-instance '<constant>
                   :name :e
                   :value 2.71828))

  (defmethod to-sexp ((expression <exp>) &optional resolve-constants-p)
    `(exp (to-sexp (slot-value expression 'exponent) resolve-constants-p)))

WORKING Logarithmics

<logarithmic> and <nat-logarithmic> are very much like the above definition of <expt> and <exp>, with similar conditions for eqal, the only difference is semantic, being that logarithmic expressions are evaluated differently.

  @export
  (defclass <logarithmic> (<compound>)
    ((base :reader base
           :initarg :base
           :type <expression>)
     (log-of :reader log-of
             :initarg :log-of
             :type <expression>)))

  @export
  (defclass <nat-logarithmic> (<logarithmic>)
    ((log-of :reader log-of
             :initarg :log-of
             :type <expression>)))

  (defmethod base ((expression <nat-logarithmic>))
    (make-instance '<constant>
                   :name :e
                   :value 2.7818))

  (defmethod eqal ((expression-a <logarithmic>) (expression-b <logarithmic>))
    (and (eqal (base expression-a)
             (base expression-b))
       (eqal (log-of expression-a)
             (log-of expression-b))))

WORKING Trigonometric

Trigonemtric functions are also a bit weird – as given a "normal" trig function, e.g., $\n{θ}$,here's the normal, the inverse, the hyperbolic, and the hyperbolic inverse. As such, we have a superclass for each, and then equality, which checks type and eqal-ness of the inner expression.

  @export
  (defclass <trig> (<expression>)
    ((expression :reader expression
                 :initarg :expression
                 :type <expression>)))

  @export
  (defclass <inverse-trig> (<trig>) ())

  @export
  (defclass <hyperbolic-trig> (<trig>) ())

  (defmethod eqal ((expression-a <trig>) (expression-b <trig>))
    (and (equal (type-of expression-a)
               (type-of expression-b))
       (eqal (expression expression-a)
             (expression expression-b))))

  @export
  (defclass <sin> (<trig>) ())

  @export
  (defclass <cos> (<trig>) ())

  @export
  (defclass <tan> (<trig>) ())

  @export
  (defclass <csc> (<trig>) ())

  @export
  (defclass <sec> (<trig>) ())

  @export
  (defclass <cot> (<trig>) ())

  @export
  (defclass <asin> (<inverse-trig>) ())

  @export
  (defclass <acos> (<inverse-trig>) ())

  @export
  (defclass <atan> (<inverse-trig>) ())

  @export
  (defclass <acsc> (<inverse-trig>) ())

  @export
  (defclass <asec> (<inverse-trig>) ())

  @export
  (defclass <acot> (<inverse-trig>) ())

  @export
  (defclass <sinh> (<hyperbolic-trig>) ())

  @export
  (defclass <cosh> (<hyperbolic-trig>) ())

  @export
  (defclass <tanh> (<hyperbolic-trig>) ())

  @export
  (defclass <csch> (<hyperbolic-trig>) ())

  @export
  (defclass <sech> (<hyperbolic-trig>) ())

  @export
  (defclass <coth> (<hyperbolic-trig>) ())

  @export
  (defclass <asinh> (<hyperbolic-trig> <inverse-trig>) ())

  @export
  (defclass <acosh> (<hyperbolic-trig> <inverse-trig>) ())

  @export
  (defclass <atanh> (<hyperbolic-trig> <inverse-trig>) ())

  @export
  (defclass <acsch> (<hyperbolic-trig> <inverse-trig>) ())

  @export
  (defclass <asech> (<hyperbolic-trig> <inverse-trig>) ())

  @export
  (defclass <acoth> (<hyperbolic-trig> <inverse-trig>) ())

WORKING Blended Types – Polynomial Terms

The <polynomial-term> is one of the more interesting types. It's a variable, a multiplication and an exponent, all rolled into one. Therefore, it needs to implement all of the functionality of those. So, while some of the generics are obvious, i.e., exponent, others are less so. Things like value, base and terms must be written directly, and these can later be used, for instance, <polynomial-term> can easily be converted to <multiplication>, and an example of this is provided.

  @export
  (defclass <polynomial-term> (<multiplication> <expt> <variable>)
    ((coefficient :reader coefficient
                  :initarg :coefficient
                  :type <number>)
     (variable :reader term-variable
               :initarg :variable
               :type <variable>)
     (exponent :reader exponent
               :initarg :exponent
               :type <number>)))

  (defmethod value ((expression <polynomial-term>) &key resolve-constants)
    (declare (ignore resolve-constants))
    (value (slot-value expression 'variable)))

  (defmethod base ((expression <polynomial-term>))
    (slot-value expression 'variable))

  (defmethod terms ((expression <polynomial-term>))
    (list
     (slot-value expression 'coefficient)
     (make-instance '<expt>
                    :base (base expression)
                    :exponent (exponent expression))))

  (defmethod update-instance-for-different-class ((prev <polynomial-term>) (current <multiplication>) &key)
    (setf (slot-value current 'terms)
          (terms prev)))

Object Protocol


WORKING Expression Typing [0/8]

To be able to provide various forms of matching and manipulation, the type of an expression must be determined. This is done by analyzing the contents of the expression. To accomplish this, there must be a way to define a classifier, store all possible classifiers, check a classifier and produce a classification. To provide more flexibility in programming, there is also a special version of case, called classification-case and a when-pattern macro called when-classified-as.

Define Classification

Classifications are defined as define-classification. This macro takes a name, which is the name of the classification, and a body, which is classified within a function. Inside the function, the following are bound: expression, the expression to be classified; and, length, which is the length of the expression if it's a list, otherwise, 0 if it's atomic. A cons cell containing the name of the classification and the name of the classifier is pushed onto classification storage, and the classifier name is exported.

  (defmacro define-classification (name &body body)
    (check-type name symbol)
    (let ((classifier-name (symbolicate name '-classifier)))
      `(progn
         (defun ,classifier-name (expression &aux (length (if (listp expression) (length expression) 0)))
           (declare (ignorable length))
           ,@body)
         (pushnew '(,name . ,classifier-name) *classifications*)
         (export ',name)
         ',name)))

Check Classification

To classify an expression, the expression and name of the possible classification is passed in. If the given name of the classification is *, then t is returned, as this is a catch all; otherwise the classification is retrieved by name, and the expression is passed to the classifier, which will return either t or nil.

  (defun classified-as-p (expression classification)
    (if (eq '* classification)
        t
        (funcall (cdr (assoc classification *classifications*))
                 expression)))

Classify Expression

While being able to check if an expression is given a specific classification is vital, for some things, being able to see what all possible classifications for an expression are can be quite useful. To do this, an expression is passed in, and for each possible classification in the classification storage, it is checked to see whether or not the classification is possible. If it is, the classification is pushed on to a list of valid classifications. When the possible classifications are exhausted, the list of valid classifications is reversed and returned.

  (defun classify (expression)
    (let ((classifications '()))
      (dolist (possible
                ,*classifications*
               (reverse classifications))
        (let ((name (car possible))
              (checker (cdr possible)))
          (when (funcall checker expression)
            (push name classifications))))))

Classification Case

Because case is such a useful tool, and because it provides a way to ensure that an expression doesn't fall through when acting on it, I've written the classification-case macro. It takes an expression, named var and a list of cases, in the form of (classification body-form-1 body-form-2 body-form-n). It transforms the cases, converting them to the form ((classified-as-p expression 'type) body-form-1 body-form-2 body-form-n). It finally expands to a cond in which the-classification is bound to the full and complete classification of the passed expression.

  (defmacro classification-case (var &rest cases)
    (let ((conditions (map 'list #'(lambda (case)
                                     (destructuring-bind (type &body body) case
                                       (if (eq type 't)
                                           `((classified-as-p ,var '*) ,@body)
                                           `((classified-as-p ,var ',type) ,@body))))
                           cases)))
      `(let ((the-classification (classify ,var)))
         (declare (ignorable the-classification))
         (cond
           ,@conditions))))

When Classified

Another utility macro is when-classified-as, which takes a classification, an expressiond named variable and a body. It expands fairly simply to a when form, with the predicate taking the following form (classified-as-p variable 'classification), wrapping around the passed in body.

  (defmacro when-classified-as (classification variable &body body)
    `(when (classified-as-p ,variable ',classification)
       ,@body))

Classifications [13/13]

I define the following classifications:

Numerics

All numbers

Variables

Any symbols

Non-atomics

Anything that isn't simply a number or a variable

Additives

Expressions that are adding multiple terms

Subtractives

Expressions subtracting multiple terms

Powers

Expressions of the form $x$,here $xis a variable, and $nis a numeric.

Exponentials

Expressions of the form $x$ $e$,here $xand $yare generic expressions, and $eis Euler's constant.

Logarithmics

Expressions of the form of $\ x$ $\g_b x$,here $xand $bare generic expressions.

Rationals

Expressions of the form $\ac{f(x)}{g(x)}$.

Polynomial Terms

Any integers, multiplicatives of the form $nm$ powers of the form $x$,here $xis a variable and $nand $mare numerics.

Polynomials

Additives or Subtractives consisting solely of Polynomial Terms.

Trigonometrics

The trig functions: $\n$,cos$,tan$,csc$,sec$ d $\t$.

  <<et-classify-numbers>>
  <<et-classify-variables>>
  <<et-classify-non-atomics>>
  <<et-classify-additives>>
  <<et-classify-subtractives>>
  <<et-classify-powers>>
  <<et-classify-exponentials>>
  <<et-classify-multiplicatives>>
  <<et-classify-logarithmics>>
  <<et-classify-rationals>>
  <<et-classify-polynomial-term>>
  <<et-classify-polynomials>>
  <<et-classify-trigonometrics>>

Numbers

CLOSED: [2016-06-14 Tue 23:58]

A number is defined as anything that satisfies the built-in numberp. This includes integers, rationals, floats and complex numbers.

  (define-classification numeric
    (numberp expression))

Variables

CLOSED: [2016-06-15 Wed 00:00]

Variables are defined as anything that satisfies the Common Lisp predicate, symbolp.

  (define-classification variable
    (symbolp expression))

Non Atomics

CLOSED: [2016-06-15 Wed 00:02]

Non-atomic is a classification for anything other than numerics and variables. It is defined as anything that satisfies the predicate listp.

  (define-classification non-atomic
    (listp expression))

Additives

CLOSED: [2016-06-15 Wed 00:03]

When an expression is non-atomic, and the first element is the symbol +, it is classified as an additive expression.

  (define-classification additive
    (when-classified-as non-atomic expression
      (eq '+ (first expression))))

Subtractive

CLOSED: [2016-06-15 Wed 00:06]

A non-atomic expression for which the first element is the symbol - is a subtractive expression.

  (define-classification subtractive
    (when-classified-as non-atomic expression
      (eq '- (first expression))))

Powers

CLOSED: [2016-06-15 Wed 00:07]

A power is any expression that is non-atomic, the first element is the symbol expt, the second is a variable and the third is a numeric.

  (define-classification power
    (when-classified-as non-atomic expression
      (and (eq 'expt (first expression))
         (classified-as-p (second expression) 'variable)
         (classified-as-p (third expression) 'numeric))))

Exponentials

CLOSED: [2016-06-15 Wed 00:11]

There are two types of exponentials, natural and non-natural. Natural exponentials are defined as being non-atomic, two elements long, and the first element being exp. Non-natural exponentials are defined similarly, but are three elements long, and the first of which is the symbol expt.

  (define-classification natural-exponential
    (when-classified-as non-atomic expression
      (and (= 2 length)
         (eq 'exp (first expression)))))

  (define-classification exponential
    (when-classified-as non-atomic expression
      (and (= 3 length)
         (eq 'expt (first expression)))))

Multiplicatives

CLOSED: [2016-06-15 Wed 00:12]

A multiplicative expression is non-atomic, of any length, and the first element is the symbol *.

  (define-classification multiplicative
    (when-classified-as non-atomic expression
      (eq '* (first expression))))

Logarithmics

CLOSED: [2016-06-15 Wed 00:14]

There are two types of logarithmic classifications, natural and non-natural. Natural logarithmics are non-atomic, two elements long, and the first element is the symbol log. Natural logarithmics are also non-atomic, but they are three elements long, starting with the symbol log.

  (define-classification natural-logarithmic
    (when-classified-as non-atomic expression
      (and (= 2 length)
         (eq 'log (first expression)))))

  (define-classification logarithmic
    (when-classified-as non-atomic expression
      (and (= 3 length)
         (eq 'log (first expression)))))

Rationals

CLOSED: [2016-06-15 Wed 00:15]

Rationals are non-atomic, three elements long, and the first element is the symbol /.

  (define-classification rational
    (when-classified-as non-atomic expression
      (and (= 3 length)
         (eq '/ (first expression)))))

Polynomial Terms

CLOSED: [2016-06-15 Wed 00:17]

Polynomials are a compound classification:

  • Numerics

  • Variables

  • Powers

  • Multiplicatives that are a numeric and a variable

  • Multiplicatives that are a numeric and a power

  (define-classification polynomial-term
    (or (classified-as-p expression 'numeric)
       (classified-as-p expression 'variable)
       (classified-as-p expression 'power)
       (and (classified-as-p expression 'multiplicative)
          (= (length (rest expression)) 2)
          (or (and (classified-as-p (second expression) 'numeric)
                (or (classified-as-p (third expression) 'power)
                   (classified-as-p (third expression) 'variable)))
             (and (classified-as-p (third expression) 'numeric)
                (or (classified-as-p (second expression) 'power)
                   (classified-as-p (second expression) 'variable)))))))

Polynomials

CLOSED: [2016-06-15 Wed 00:19]

Polynomials are compound classifications that are defined as expressions which are either additive or subtrative, for which each term is a polynomial term.

  (define-classification polynomial
    (when-classified-as non-atomic expression
      (and (or (eq '- (first expression))
            (eq '+ (first expression)))
         (reduce #'(lambda (a b)
                     (and a b))
                 (map 'list
                   #'(lambda (the-expression)
                       (classified-as-p the-expression 'polynomial-term))
                   (rest expression))))))

Trigonometrics

CLOSED: [2016-06-15 Wed 00:22]

Trigonometrics are defined as non atomic expressions that are two elements long, for which the first element of the expression is either sin, cos, tan, csc, sec, or cot. For each of these there is a classification seperate from the generic trigonometric classification.

  (define-classification trigonometric
    (when-classified-as non-atomic expression
      (member (first expression) '(sin cos tan csc sec cot))))

  (define-classification sin
    (when-classified-as non-atomic expression
      (eq 'sin (first expression))))

  (define-classification cos
    (when-classified-as non-atomic expression
      (eq 'cos (first expression))))

  (define-classification tan
    (when-classified-as non-atomic expression
      (eq 'tan (first expression))))

  (define-classification csc
    (when-classified-as non-atomic expression
      (eq 'csc (first expression))))

  (define-classification sec
    (when-classified-as non-atomic expression
      (eq 'sec (first expression))))

  (define-classification cot
    (when (classified-as-p expression 'non-atomic)
      (eq 'cot (first expression))))

Classification Storage

Classifications are stored in an alist, with the key being the name of the classification, and the value being the classifier itself. These cons cells are stored in the *classifications* variable.

  (defvar *classifications* '())

Assembly

This assembles the classification library, which in the #:larcs.classify package. It correctly resolves the order of the code, taking it from simple blocks to a complete file.

  (in-package #:larcs.classify)
  <<et-classification-storage>>
  <<et-define-classification>>
  <<et-check-classification>>
  <<et-classify-expression>>
  <<et-classification-case>>
  <<et-when-classified>>
  <<et-possible-classifications>>

WORKING Symbolic Differentiation [0/4]

While this isn't exactly algebra, differentiation is important mathematically. This is done rather simply using rules to rewrite an initial expression forming the derivative.

WORKING Rule Definition [0/3]

CLOSED: [2016-08-16 Tue 22:10]

Rule definition is how this system works. This is done rather simply, and is comprised of definition, retrieval and storage portions.

Definition

Rules are defined using the define-derivative macro, which takes an expression type, an arguments list, and a body. If the expression type is not already in the expansions map, it pushes the expression type and expansion name onto the the mapping. Following that, it defines a function for the expansion, using the given arguments list as the lambda-list and the given body for the body for the function.

  (defmacro define-derivative (expression-type (&rest arguments-list) &body body)
    (let ((expansion-name (symbolicate expression-type '-expansion)))
      `(progn
         (when (not (member ',expression-type (mapcar #'car *rules*)))
           (setq *rules* (append *rules* '((,expression-type . ,expansion-name)))))
         (defun ,expansion-name (,@arguments-list)
           ,@body))))

Retrieval

Rule retrieval works by matching a rewrite rule to an expression by classification. This is done by iterating through the possible classification-rewrite rule pairs, and if the expression matches the classification, returing the rewrite rule to be used.

  (defun get-rule (expression)
    (cdr (first (remove-if #'(lambda (pair)
                               (let ((type (first pair)))
                                 (not (classified-as-p expression type))))
                           ,*rules*))))

Storage

Rules are stored rather simply, in a list of cons cells, with the CAR being the classification, and the CDR being the actualy rewrite function. They are found in the variable *rules*.

  (defvar *rules* '())

WORKING Rules [0/9]

The process of symbolic derivation is carried out through rules. These are defined in the following sections, taking apart the equations and putting them back together again as their derivatives.

  <<sd-numbers>>
  <<sd-variables>>
  <<sd-polynomial-terms>>
  <<sd-multiplicatives>>
  <<sd-rationals>>
  <<sd-additives>>
  <<sd-subtractives>>
  <<sd-exponentials-and-logarithmics>>
  <<sd-trigonometric-functions>>

Numbers

Numbers are perhaps one of the simplest rules to define, ignore everything and simply return 0, which is, by definition, the derivative of any bare number.

  (define-derivative numeric (&rest junk)
    (declare (ignorable junk))
    0)

Variables

As with Numbers, Variables are just as simple, if something is simply a bare variable, the derivative of such is 1.

  (define-derivative variable (&rest junk)
    (declare (ignorable junk))
    1)

Polynomial Terms

A Polynomial Term is a bit more complex than the previous two, the rewrite rule has to be able to get the variable, coefficient and current power of the polynomial term. Given this information, there are three possible cases:

The power is equal to 1.

The coefficient is returned.

The power is equal to 2.

The coefficient times the variable (in symbolic form) is returned.

The power is greater than 2.

This comes in the form of $()x^{(n-1)}$.

  (define-derivative polynomial-term (&rest term)
    (let* ((coefficient (coefficient term))
           (variable (term-variable term))
           (power (get-power term)))
      (cond
        ((= 1 power)
         coefficient)
        ((= 2 power)
         `(* ,(* coefficient power) ,variable))
        (t
         `(* ,(* coefficient power) (expt ,variable ,(1- power)))))))

Multiplicatives

Differentiation of Multiplicative equations are performed by the product rule. This is defined as $\ac{\mathrm{d}}{\mathrm{d}x} f(x) ⋅ g(x) = f(x) ⋅ g(x) + f(x) ⋅ g(x)$.There are some minor exceptions, if $f)$ d $g)$ e numeric, then the result is the product of the two; if either $f)$ $g)$ numeric and the other is not, then the numeric is placed in front of the other derivative of the remainder.

  (define-derivative multiplicative (function first &rest rest)
    (declare (ignore function))
    (if (= 1 (length rest))
        (let ((second (first rest)))
          (cond
            ((and (classified-as-p first 'numeric)
                (classified-as-p second 'numeric))
             (* first second))
            ((classified-as-p first 'numeric)
             `(* ,first ,(differentiate second)))
            ((classified-as-p second 'numeric)
             `(* ,second ,(differentiate first)))
            (t
             `(+ (* ,first ,(differentiate second))
                 (* ,second ,(differentiate first))))))
        (differentiate `(* ,first (* ,@rest)))))

Rationals

This follows the quotient rule, which is defined as $\ac{\mathrm{d}}{\mathrm{d}x} \frac{f(x)}{g(x)} = \frac{f(x)g(x) - f(x)g(x)}{g(x)^2}$.

  (define-derivative rational (function numerator denominator)
    (declare (ignore function))
    `(/ (- (* ,numerator ,(differentiate denominator))
           (* ,denominator ,(differentiate numerator)))
        (expt ,denominator 2)))

Additives

This is quite simple, differentiate each term and add them together. This is accomplished using map.

  (define-derivative additive (function &rest terms)
    (declare (ignore function))
    `(+ ,@(map 'list #'(lambda (term) (differentiate term)) terms)))

Subtractives

Following the same pattern as for additives, subtractives map over, deriving each term and prepending the - symbol to render the derivative of the originally passed subtractive equation.

  (define-derivative subtractive (function &rest terms)
    (declare (ignore function))
    `(- ,@(map 'list #'(lambda (term) (differentiate term)) terms)))

Exponentials and Logarithmics

There are four types of functions that are supported:

Natural Exponentials

Effectively this returns itself.

Exponentials

The original function multiplied by the log of the base.

Natural Logarithmics

The derivative of the function being passed to $\$ er the same function.

Logarithmics

The derivative of the natural log of the expression over the log of the base times the expression.

  (define-derivative natural-exponential (function expression)
    (declare (ignore function))
    `(exp ,expression))

  (define-derivative exponential (function base power)
    (declare (ignore function))
    (if (numberp power)
        (if (listp base)
            `(* ,power (expt ,base ,(1- power)) ,(differentiate base))
            `(* ,power (expt ,base ,(1- power))))
        `(* (expt ,base ,power) (log ,base))))

  (define-derivative natural-logarithmic (function expression)
    (declare (ignore function))
    `(/ ,(differentiate expression) ,expression))

  (define-derivative logarithmic (function number base)
    (declare (ignore function))
    `(/ ,(differentiate (cons 'log number)) (* (log ,base) ,number)))

Trigonometric Functions

The following trig functions are supported:

Sine

$f′}(x) ⋅ cos(f(x))$

Cosine

$^f(x) ⋅ -sin(f(x))$

Tangent

$f′}(x) ⋅ {sec(f(x))}^2$

Cosecant

$f′}(x) ⋅ ≤ft( csc(f(x)) - cot(f(x)) \right)$

Cotangent

$f′}(x) ⋅ -{csc(f(x))}^2$

  (define-derivative sin (function expression)
    (declare (ignore function))
    `(* ,(differentiate expression) (cos ,expression)))

  (define-derivative cos (function expression)
    (declare (ignore function))
    `(* ,(differentiate expression) (- (sin ,expression))))

  (define-derivative tan (function expression)
    (declare (ignore function))
    `(* ,(differentiate expression) (expt (sec ,expression) 2)))

  (define-derivative csc (function expression)
    (declare (ignore function))
    `(* ,(differentiate expression) (- (csc ,expression)) (cot ,expression)))

  (define-derivative cot (function expression)
    (declare (ignore function))
    `(* ,(differentiate expression) (- (expt (csc ,expression) 2))))

Driver

This is the derivative driver, differentiate, which in the end is called recursively. It takes an expression (called function), finds a rule using the get-rule function, and applies the rule to the function, ensuring that the function is passed as a list.

  (defun differentiate (function)
    (let ((rule (get-rule function)))
      (when rule
        (apply rule (ensure-list function)))))

Assembly

This assembles the package, placing the contents in the correct order and puts them in the file larcs-differentiate.lisp.

  (in-package #:larcs.differentiate)
  <<sd-rule-storage>>
  <<sd-rule-definition>>
  <<sd-rule-retrieval>>
  <<sd-rules>>
  <<sd-derivative-driver>>

WORKING Library Assembly [0/2]

LARCS is primarily a library, and as such, the formal interface for the library, and a way to load it must be provided. This is done here by defining packages centrally and defining an ASDF system to load the library quickly and easily.

WORKING Package Definitions [0/1]

As all of the packages are defined centrally, this makes resolving inter-package dependencies much easier and convenient.

  <<types-package-def>>

Types

  (defpackage #:larcs.types
    (:use #:cl)
    (:import-from #:annot
                  #:enable-annot-syntax)
    (:nicknames #:types))
  (in-package #:larcs.types)
  (enable-annot-syntax)

  <<type-basic>>

  <<type-atomic>>
  <<type-numbers>>
  <<type-variables>>
  <<type-constants>>

  <<type-compound>>
  <<type-additions>>
  <<type-subtractions>>
  <<type-multiplications>>
  <<type-division>>
  <<type-exponentials>>
  <<type-logarithmics>>
  <<type-trigs>>

  <<type-poly-term>>

System Definition

This defines the LARCS Library system, allowing the functionality to be loaded by other systems or to provide a way to make development quickly and easily.

  (asdf:defsystem #:larcs-lib
    :description "A CAS Library for use within Lisp software."
    :author "Samuel W. Flint <swflint@flintfam.org>"
    :licence "GNU GPLv3 or later"
    :depends-on (#:alexandria
                 #:cl-annot)
    :serial t
    :components ((:file "larcs-packages")
                 (:file "larcs-types-basic")))

Push to bottom   ignore

Version Information

This document is version src_sh{git describe –always –long –dirty –abbrev=10 –tags}.