Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the infix-math Reference Manual, generated automatically by Declt version 3.0 "Montgomery Scott" on Tue Dec 22 13:52:07 2020 GMT+0.
• Introduction | What infix-math is all about | |
• Systems | The systems documentation | |
• Files | The files documentation | |
• Packages | The packages documentation | |
• Definitions | The symbols documentation | |
• Indexes | Concepts, functions, variables and data types |
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: Top [Contents][Index]
The main system appears first, followed by any subsystem dependency.
• The infix-math system | ||
• The infix-math/infix-math system | ||
• The infix-math/data system | ||
• The infix-math/symbols system |
Next: The infix-math/infix-math system, Previous: Systems, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
An extensible infix syntax for math in Common Lisp.
asdf-package-system
infix-math/infix-math (system)
infix-math.asd (file)
Next: The infix-math/data system, Previous: The infix-math system, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
infix-math.asd (file)
lisp.lisp (file)
Next: The infix-math/symbols system, Previous: The infix-math/infix-math system, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
infix-math.asd (file)
lisp.lisp (file)
Previous: The infix-math/data system, Up: Systems [Contents][Index]
Paul M. Rodriguez <pmr@ruricolist.com>
MIT
infix-math.asd (file)
lisp.lisp (file)
Files are sorted by type and then listed depth-first from the systems components trees.
• Lisp files |
• The infix-math.asd file | ||
• The infix-math/infix-math/lisp.lisp file | ||
• The infix-math/data/lisp.lisp file | ||
• The infix-math/symbols/lisp.lisp file |
Next: The infix-math/infix-math/lisp․lisp file, Previous: Lisp files, Up: Lisp files [Contents][Index]
infix-math.asd
Next: The infix-math/data/lisp․lisp file, Previous: The infix-math․asd file, Up: Lisp files [Contents][Index]
infix-math/infix-math (system)
infix-math.lisp
$ (macro)
Next: The infix-math/symbols/lisp․lisp file, Previous: The infix-math/infix-math/lisp․lisp file, Up: Lisp files [Contents][Index]
infix-math/data (system)
data.lisp
Previous: The infix-math/data/lisp․lisp file, Up: Lisp files [Contents][Index]
infix-math/symbols (system)
symbols.lisp
Next: Definitions, Previous: Files, Up: Top [Contents][Index]
Packages are listed by definition order.
• The infix-math/infix-math package | ||
• The infix-math/data package | ||
• The infix-math/symbols package |
Next: The infix-math/data package, Previous: Packages, Up: Packages [Contents][Index]
lisp.lisp (file)
infix-math
$ (macro)
Next: The infix-math/symbols package, Previous: The infix-math/infix-math package, Up: Packages [Contents][Index]
lisp.lisp (file)
Previous: The infix-math/data package, Up: Packages [Contents][Index]
lisp.lisp (file)
common-lisp
Definitions are sorted by export status, category, package, and then by lexicographic order.
• Exported definitions | ||
• Internal definitions |
Next: Internal definitions, Previous: Definitions, Up: Definitions [Contents][Index]
• Exported symbol macros | ||
• Exported macros | ||
• Exported compiler macros | ||
• Exported functions | ||
• Exported types |
Next: Exported macros, Previous: Exported definitions, Up: Exported definitions [Contents][Index]
lisp.lisp (file)
pi
Next: Exported compiler macros, Previous: Exported symbol macros, Up: Exported definitions [Contents][Index]
Compile a mathematical formula in infix notation.
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Pretend unary operators are binary operators.
lisp.lisp (file)
Next: Exported functions, Previous: Exported macros, Up: Exported definitions [Contents][Index]
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Next: Exported types, Previous: Exported compiler macros, Up: Exported definitions [Contents][Index]
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Previous: Exported functions, Up: Exported definitions [Contents][Index]
lisp.lisp (file)
Previous: Exported definitions, Up: Definitions [Contents][Index]
• Internal special variables | ||
• Internal symbol macros | ||
• Internal macros | ||
• Internal functions | ||
• Internal types |
Next: Internal symbol macros, Previous: Internal definitions, Up: Internal definitions [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.
lisp.lisp (file)
Table of operator precedence.
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Next: Internal macros, Previous: Internal special variables, Up: Internal definitions [Contents][Index]
lisp.lisp (file)
(exp 1.0d0)
lisp.lisp (file)
(sqrt -1)
Next: Internal functions, Previous: Internal symbol macros, Up: Internal definitions [Contents][Index]
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Next: Internal types, Previous: Internal macros, Up: Internal definitions [Contents][Index]
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
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.
lisp.lisp (file)
Does SYM start and end with an operator char?
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
lisp.lisp (file)
Previous: Internal functions, Up: Internal definitions [Contents][Index]
lisp.lisp (file)
Previous: Definitions, Up: Top [Contents][Index]
• Concept index | ||
• Function index | ||
• Variable index | ||
• Data type index |
Next: Function index, Previous: Indexes, Up: Indexes [Contents][Index]
Jump to: | F I L |
---|
Jump to: | F I L |
---|
Next: Variable index, Previous: Concept index, Up: Indexes [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 type index, Previous: Function index, Up: Indexes [Contents][Index]
Jump to: | *
E I S Π |
---|
Jump to: | *
E I S Π |
---|
Previous: Variable index, Up: Indexes [Contents][Index]
Jump to: | I O P S T |
---|
Jump to: | I O P S T |
---|