Symbolic algebraic expressions
1 Example
2 API Reference
parse-sexpr
parse-infix
symalg-expr?
simplify
differentiate
evaluate
infix
latex
sexpr
7.7

Symbolic algebraic expressions

Johannes Tax

 (require symalg) package: symalg

This library provides functions to parse and manipulate symbolic algebraic expressions. These expression can be constants, variables, arithmetic operations and exponentiations. Additionally trigonometric functions and logarithms are supported.

1 Example

> (define expr (parse-infix "3*x^2 - 4*x + cos(x)"))
> (define expr-deriv (simplify
                       (differentiate expr)))
> expr-deriv

(add

 (list

  (num -4)

  (mul (list (num 6) (sym 'x)))

  (mul (list (num -1) (sin_ (sym 'x))))))

> (infix expr-deriv)

"-4 + 6 * x + -1 * sin(x)"

> (sexpr expr-deriv)

'(+ -4 (* 6 x) (* -1 (sin x)))

> (latex expr-deriv)

"-4 + 6 x -\\sin(x)"

> (define f (evaluate expr-deriv))
> (f 3)

13.858879991940134

2 API Reference

procedure

(parse-sexpr s)  symalg-expr?

  s : any/c
Parses an s-expression and returns a corresponding symbolic algebraic expression. s can be an expression expr of the following form:

expr :

 

number?

 

| symbol?

 

| e

 

| pi

 

| (+ expr ...+)

 

| (- expr ...+)

 

| (* expr ...+)

 

| (/ expr expr)

 

| (expt expr expr)

 

| (log expr expr)

 

| (sin expr)

 

| (cos expr)

 

| (tan expr)

procedure

(parse-infix s)  symalg-expr?

  s : string?
Parses a string containing an infix expression and returns a corresponding symbolic algebraic expression. An infix expression can be an expression expr of the following form:

expr :

 

number?

 

| symbol?

 

| e

 

| pi

 

| (expr)

 

| expr + expr

 

| expr - expr

 

| expr * expr

 

| expr / expr

 

| expr ^ expr

 

| log(expr, expr)

 

| sin(expr)

 

| cos(expr)

 

| tan(expr)

procedure

(symalg-expr? e)  boolean?

  e : any/c
This predicate checks, if the argument denotes a symbolic algebraic expression that can be processed by the functions below.

procedure

(simplify e)  symalg-expr?

  e : symalg-expr?
Returns a simplified form of e. Simplification is mostly based on Joel S. Cohen’s Computer Algebra and Symbolic Computation.

Some examples:

> (infix (simplify (parse-infix "x+x")))

"2 * x"

> (infix (simplify (parse-infix "x^0")))

"1"

> (infix (simplify (parse-infix "2*x^2 + 4*x^2 + 5 - 6")))

"-1 + 6 * (x)^(2)"

> (infix (simplify (parse-infix "2*x^2 / x")))

"2 * x"

> (infix (simplify (parse-infix "2^x^4")))

"(2)^(4 * x)"

procedure

(differentiate e)  symalg-expr?

  e : symalg-expr?
The function differentiate computes the first derivation of a given symbolic algebraic expression.

Take into account that the resulting expression is not simplified automatically, a further call to simplify is necessary to bring it into a canonical form:

> (define expr (parse-infix "2*x^2 - x"))
> (infix (differentiate expr))

"(x)^(2) * 1 * 2 * (x)^(-1) + 0 * log(x) * 2 + (x)^(2) * 0 + 1 * -1 + x * 0"

> (infix (simplify (differentiate expr)))

"-1 + 4 * x"

procedure

(evaluate e)  (f number? ...+)

  e : symalg-expr?
Returns a function that evaluates the given symbolic algebraic expression. Parameters of the returned function are bound to variables (unbound symbols) of the symbolic algebraic expression in alphabetical order.

procedure

(infix e)  string?

  e : symalg-expr?
Returns the infix representation of a symbolic algebraic expression.

procedure

(latex e)  string?

  e : symalg-expr?
Returns the LaTeX math mode representation of a symbolic algebraic expression.

procedure

(sexpr e)  any/c

  e : symalg-expr?
Returns the s-expression representation of a symbolic algebraic expression.