Symbolic algebraic expressions
(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
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?
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
procedure
(simplify e) → symalg-expr?
e : symalg-expr?
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?
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
e : symalg-expr?
procedure
e : symalg-expr?
procedure
e : symalg-expr?
procedure
e : symalg-expr?