Skip to content
Mike Anderson edited this page Jun 28, 2013 · 10 revisions

Purpose of this page it to collect a few "typical" use cases:

Rearranging a symbolic expression

User has an expression in terms of one symbol, would like to rearrange it to express it in terms of another symbol:

(def E (ex (= y (/ x y))))

(rearrange [x] E)
=> (= x (* y y))

Computing the derivative of an expression

User has an expression with several variables (come of which may be constants). The user would like to get a new expression which is the derivative of the first, with respect to one of the variables:

(def E (ex (+ (* (+ 1 a) x x) (* (+ b c) x) a)))

(derivative [x] E)
=> (+ (* 2 (+ 1 a) x) (+ b c))

Solving an equation for a specific value

User has an expression representing an equation, would like to solve for values of a variable that satisfy the equation

(def E (ex (= [x y z] [z 2 x]))) 

(solve [y] E)
=> (2)

(solve [x] E)
=> undetermined

Note: need to think about possibility for zero or multiple solutions?

Parsing an equation from a string

User has a string version of an expression, in standard mathematical infix notation. User would like to convert this to a Clojure / expresso expression

(def E (ex-parse "2*x^2 + 3*y + z")
=> (+ (* 2 x x) (* 3 y) z)

Substitution of multiple values

User has an expression, and would like to substitute in a set of values / other expressions. Useful for parametric substitutions etc.

(def E (ex (+ (* x x) (* y y)))

(substitute E {'x (ex (cos t)) , 'y (ex (sin t))}
=> (+ (* (cos t) (cos t)) (* (sin t) (sin t)))

Constructing a polynomial

User wants to build polynomial expressions given vectors of coefficients, and decides to write a function to do this:

(defn build-polynomial [symbol coefficients]
  ....)

(build-polynomial 'x [1 2 0 4])
=> (+ 1 (* 2 x) (* 4 x x x))

Compiling optimised element-wise operations for matrices

User has a expression that needs to be applied to all elements of one or more matrices.

Assumes that a core.matrix implementation has the ability to compile an expresso expression to some optimised format for it's own matrix types. This might include compilation to native code or GPU accelerated functions, for example.

(def foo-expression (ex (+ (* x z) (* y (- 1 z)))))

(def foo-function (clore.core.matrix/compile-fn [x y z] :matrix-implementation-key foo-expression))

(foo-function M1 M2 M3)
=> [result matrix]
Clone this wiki locally