Skip to content
Mike Anderson edited this page Jul 2, 2013 · 10 revisions

The purpose of this page is to collect a few "typical" use cases that demonstrate how Expresso might be used. Feel free to expand existing use cases or add additional use cases based on your experience / needs.

Rearranging a symbolic expression

Context: user is working at the REPL, wants to do some basic algebra.

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

Context: user is writing a program that needs to numerically determine the minimum of some function, and needs to work out an equation for the derivative of the function in order to do this.

User has an expression with several variables (some 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

Context: Expresso user is writing "spreadsheet style" features into an end-user application which will be used by end-users who don't understand List s-expressions. He needs to implement regular infix style syntax.

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. This needs to run as fast as possible (perhaps the matrices are very large, or a large number of matrices need to be processed).

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]