Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the infix-math Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 04:50:43 2022 GMT+0.
Next: Systems, Previous: The infix-math Reference Manual, Up: The infix-math Reference Manual [Contents][Index]
Infix-Math is a library that provides a special-purpose syntax for transcribing mathematical formulas into Lisp.
Bitter experience has taught me that the more the formula on screen resembles the formula on paper, the better. The more the formula on screen resembles the formula on paper, the easier it is to prevent bugs from transcription errors. The easier it is to prevent transcription errors, the easier it is to trace the source of any bugs that do occur – because sometimes the formula is wrong.
(Having to transcribe formulas from crooked, blurry scans of ancient pre-LaTeX typescripts is bad enough without having to parse operator precedence in your head.)
Even if you end up rewriting the formula for speed or numerical stability, having the specification in an executable form is invaluable for reference and testing.
The macro $
is the entry point into Infix-Math.
($ 2 + 2) => 4
($ 1 + 2 * 3) => 7
Operator precedence parsing in Infix-Math is reliable – it uses Dijkstra’s shunting yard algorithm.
The parser automatically descends into function argument lists, which means that the total number of parentheses is never greater than it would be in a purely infix language.
($ (tan pi * (p - 1/2)))
≡ (tan (* pi (- p 1/2)))
≅ tan(pi*(p-0.5))
Common subexpression elimination is automatic and aggressive. All forms are assumed to be pure. Math does not have side effects.
(macroexpand '($ 2 ^ 2x * 2 ^ 2x)
=> ‘(let ((#:subexp11325 (^ 2 (* 2 x))))
(* #:subexp11325 #:subexp11325))
Infix-Math knows about the following arithmetic and bitwise operators, in descending order of precedence.
Operations at the same level of precedence are always evaluated left-to-right.
(+ 0.1d0 (+ 0.2d0 0.3d0)) => 0.6d0
(+ (+ 0.1d0 0.2d0) 0.3d0) => 0.6000000000000001D0
($ 0.1d0 + 0.2d0 + 0.3d0) => 0.6000000000000001D0
Parentheses can be used for grouping.
($ 0.1d0 + (0.2d0 + 0.3d0)) => 0.6d0
Variables can be written with literal numbers as coefficients.
($ 2x) => 10
($ -2x) => 10
Literal coefficients have very high priority.
($ 2 ^ 2 * x) ≡ (* (expt 2 2) x) => 20
($ 2 ^ 2x) ≡ (expt 2 (* 2 x)) => 1024
A literal coefficient of 1 can be omitted.
($ -x) ≡ ($ -1x) ≡ (* -1 x)
Literal coefficients are parsed as decimals, rather than floats.
($ 1.5x) ≡ (* 3/2 x)
You can also use fractions as literal coefficients.
($ 1/3x) ≡ (* 1/3 x)
Among other things, literal coefficients are very convenient for units of measurement.
(The idea for literal coefficients comes from Julia.)
Infix-Math exports only five symbols: $
, ^
, over
, and two macros
for declaring operators: declare-unary-operator
and
declare-binary-operator
.
The symbol ^
is just a shorthand for expt
.
($ 1 + 2 * 3 ^ 4) => 163
(^
is from Dylan.)
The symbol over
represents the same operation as /
, but at a much
lower priority. Using over
lets you avoid introducing parentheses
for grouping when transcribing fractions.
(setf x 5)
($ x * 2 / x * 3) ≡ (* (/ (* x 2) x) 3) => 6
($ (x * 2) / (x * 3)) ≡ (/ (* x 2) (* x 3)) => 2/3
($ x * 2 over x * 3) ≡ (/ (* x 2) (* x 3)) => 2/3
You can also spell over
with a series of dashes or underscores.
($ x * 2
-----
x * 3)
=> 2/3
If you want more math symbols, the package infix-math/symbols
provides a few more.
You can use Infix-Math to turn your REPL into a calculator.
First, load the infix-math/calc
system:
(asdf:load-system "infix-math/calc")
Then, at the REPL, start the calculator:
(infix-math/calc:calc)
This will put you at a calculator prompt. You can type in mathematical expressions directly:
$> 2 + 2
4
A single form entered at the REPL is interpreted as ordinary CL.
$> *package*
:infix-math/calc-user
You can assign to variables using the <-
operator.
$> x <- 2 + 2
4
$> x
4
Certain one-letter variables are provided for you to assign to, such as x
, y
, and z
. You can see the full list by evaluating :v
at the calculator prompt.
To quit, use :q
. The value of the last expression evaluated will be returned.
$> 2 + 2
4
$> :q
4
CL-USER> *
4
Infix-Math is easily to extend. In fact, you may not even need to extend it.
Any symbol that consists entirely of operator characters is interpreted as an infix operator, with the highest non-unary priority. Operator characters are anything but dashes, underscores, whitespace or alphanumeric characters.
(defun <*> (x y)
"Matrix multiplication, maybe."
...)
(macroexpand '($ x * y <*> z)) => (* x (<*> y z))
(This approach is taken from Haskell.)
You can use any function as an infix operator by surrounding its name with dots.
(defun choose (n k)
"Binomial coefficient, maybe."
...)
(macroexpand '($ n .choose. k)) => '(choose n k)
Again, the operator has the highest non-unary priority.
(This approach is taken from Haskell and Fortran.)
If you need more flexibility, declare the operators using
declare-binary-operator
or declare-unary-operator
.
To declare a unary operator:
(declare-unary-operator √)
To copy the precedence of another operator:
(declare-binary-operator <*> :from *)
To declare an operator right-associative:
(declare-binary-operator ?
:from *
:right-associative t)
Next: Files, Previous: Introduction, Up: The infix-math Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
Next: infix-math/infix-math, Previous: Systems, Up: Systems [Contents][Index]
An extensible infix syntax for math in Common Lisp.
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
asdf-package-system (system).
infix-math/infix-math (system).
Next: infix-math/symbols, Previous: infix-math, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
Next: infix-math/data, Previous: infix-math/infix-math, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
Previous: infix-math/symbols, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
Next: Packages, Previous: Systems, Up: The infix-math Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: infix-math/infix-math/file-type.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
infix-math (system).
Next: infix-math/symbols/file-type.lisp, Previous: infix-math/infix-math.asd, Up: Lisp [Contents][Index]
infix-math/infix-math (system).
$ (macro).
Next: infix-math/data/file-type.lisp, Previous: infix-math/infix-math/file-type.lisp, Up: Lisp [Contents][Index]
infix-math/symbols (system).
Previous: infix-math/symbols/file-type.lisp, Up: Lisp [Contents][Index]
infix-math/data (system).
Next: Definitions, Previous: Files, Up: The infix-math Reference Manual [Contents][Index]
Packages are listed by definition order.
Next: infix-math/symbols, Previous: Packages, Up: Packages [Contents][Index]
infix-math
$ (macro).
Next: infix-math/data, Previous: infix-math/infix-math, Up: Packages [Contents][Index]
common-lisp.
Previous: infix-math/symbols, Up: Packages [Contents][Index]
Next: Indexes, Previous: Packages, Up: The infix-math Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Next: Macros, Previous: Public Interface, Up: Public Interface [Contents][Index]
Next: Compiler macros, Previous: Symbol macros, Up: Public Interface [Contents][Index]
Compile a mathematical formula in infix notation.
Pretend unary operators are binary operators.
Next: Ordinary functions, Previous: Macros, Up: Public Interface [Contents][Index]
Next: Types, Previous: Compiler macros, Up: Public Interface [Contents][Index]
Previous: Ordinary functions, Up: Public Interface [Contents][Index]
Previous: Public Interface, Up: Definitions [Contents][Index]
Next: Symbol macros, Previous: Internals, Up: Internals [Contents][Index]
Basic C-style operator precedence, with some differences.
The use of MIN, MAX, GCD and LCM as infix operators is after Dijkstra (see EWD 1300). Perl 6 is also supposed to use them this way, and I have adopted its precedence levels.
Table of operator precedence.
Next: Macros, Previous: Special variables, Up: Internals [Contents][Index]
Next: Ordinary functions, Previous: Symbol macros, Up: Internals [Contents][Index]
Expand -x into (- x) and 2x into (* 2 x).
Literal coefficients have the same precedence as unary operators.
Literal coefficients are assumed to be in base 10.
Does SYM start and end with an operator char?
Previous: Ordinary functions, Up: Internals [Contents][Index]
Previous: Definitions, Up: The infix-math Reference Manual [Contents][Index]
Jump to: | $
%
&
(
<
>
^
×
÷
√
A B C D E F L M N O P R S T U V |
---|
Jump to: | $
%
&
(
<
>
^
×
÷
√
A B C D E F L M N O P R S T U V |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | *
E I S Π |
---|
Jump to: | *
E I S Π |
---|
Jump to: | F I O P S T |
---|
Jump to: | F I O P S T |
---|