The ratmath Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The ratmath Reference Manual

This is the ratmath Reference Manual, version 1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 05:45:12 2022 GMT+0.

Table of Contents


1 Introduction

Ratmath

Ratmath is a collection of utilities for working with rational numbers, approximations, and intervals in Common Lisp. Functions are included to calculate the closest single approximant given a maximum numerator/denominator (rat) or the smallest bounded rational interval (infsup). The infsup function can find the floating point constant pi with a denominator limit of, e.g. 256, to be within the interval 688/219 to 355/113, and the with-interval-math function can use this interval instead of exact values to carry out further interval arithmetic to yield interval results.

A Better (rational NUM) and (rationalize NUM) Function

In its simplest use case, it is an extension of Common Lisp's rationlize that can take keyword arguments :limd and :limn representing the maximum numerator/denominator:

CL-USER> (ratmath:rat pi :limd 255)
355/113

Calculating Rational Intervals

Interval arithmetic is a powerful and convenient way of doing calculations in engineering but is seldom accomodated in the base functionality of computer languages. Wikipedia

There are other libraries for interval math using floats, but this library instead uses rationals, a numerical data type not commonly appreciated as a base numerical type amongst the other languages. With Common Lisp's built-in infinite size integers, there is no limit to the precision of rational numbers which simplifies a lot of the inherent complexity of writing floating point interval libraries which have to worry about round off on every operation.

The disadvantage of using rationals rather than floats is that some calculations can end up generating extremely large numerators and denominators quickly. Conveniently, these large numbers can be rationally approximated into intervals of smaller numbers at any time in the middle or at the end of a calculation. As as example, the number representing e * pi using high precision rational constants on e and pi yields a rational result which has 72-bit numerator (which would cause overflow in anything other than Common Lisp). This bignum rational could be reduced to the following 16-bit rational interval with ratmath:

CL-USER> (* (rational pi) (rational (exp 1.0)))
2520484590068807905375/295147905179352825856
CL-USER> (ratmath:infsup * :limd 65535 :limn 65535)
(23749/2781 . 50077/5864)
32894771015794204823/36893488147419103232

You can usually ignore the second value returned from infsup. It represents an alpha, or a number between 0 and 1 representing the location of the original number inside the returned interval. The alpha calculation returns a rational that may be of higher order than even the original rational. For most cases, it would be recommended to coerce this rational into a float or use the rat function to reduce its precision.

Approximations From Rational Intervals

There are routines to work in reverse as well. That is, to go from an interval to the simplest rational number within that interval. The Common Lisp builtin rational differs only from its cousin rationalize by calculating a rational approximation from an interval about the original floating point number equal to the epsilon of the floating point representation. Here's how you do something similar, but with an explicitly bounded interval with ratmath:

CL-USER> (ratmath:rat '(110/333 . 221/333))
1/2

Trivial Interval Arithmetic

Intervals can be useful outside of rational approximation and used in the general arithmetic of non-exact measurements and numbers. For instance, on a calculation returning voltage measurements of an analog input, one usually needs to plug in numbers representing the voltage reference and any input resistor divider ratios. Using exact numbers for these is suboptimal as the truth is that these specifications are fuzzy and have non-zero tolerances. Interval math can be used instead to output an interval representing where the analog input true value might actually be between.

An interval at its core is simply a cons of two numbers. e.g. '(99/20 . 101/20). To facilitate creation, it is also possible to create the same interval with alternative notation:

CL-USER> (ratmath:infsup 5 :abstol 5/100)             ;; 5, +/-.05
(99/20 . 101/20)
CL-USER> (ratmath:infsup 99/20 :abstol '(0 . 1/10))   ;; 99/20, -0/+.1
(99/20 . 101/20)
CL-USER> (ratmath:infsup 101/20 :abstol '(1/10 . 0))  ;; 101/20, -.1/+0
(99/20 . 101/20)
CL-USER> (ratmath:infsup 5 :tol 1/100)                ;; 5, +/-1%
(99/20 . 101/20)
CL-USER> (ratmath:infsup 5 :tol '(0 . 1/100))         ;; 5, -0%/+1%
(5 . 101/20)

The gist is that :tol takes either an interval or an exact representing the percent tolerance and :abstol uses absolute values +/- rather than percentages. Refer to the documentation of the infsup function for further details.

with-interval-math Blocks

From within a ratmath:with-interval-math block, some of the basic arithmetic operations are replaced with interval aware variants. For instance:

CL-USER> (ratmath:with-interval-math
           (* '(1 . 2) 5 (ratmath:infsup 3.3 :tol .01)))
(16.335 . 33.329998)

Notice that the usual rules of floating point contagion apply. Since the infsup was given arguments in floats, the result is a float. We can turn this float back into rationals or exacts with:

CL-USER> (ratmath:infsup '(16.335 . 33.329998) :limd 100)
(49/3 . 3333/100)
CL-USER> (ratmath:rat *)
17

The rat function converts the interval '(16.335 . 33.329998) into the simplest number within. For this interval, it is 17. Simplest may not be the best term here, the full truth is its first number on the infinite Stern-Brocot fraction tree that is recursively traversed in the process of analyzing the best convergents and semi-convergents of a given continued fraction. What I mean to illustrate here is that, under this definition, this number will rarely be the midpoint of the interval, and also that mathematically correct rational approximation is not as trivial as it may seem at first glance. There is some really interesting algorithms and concepts (to math geeks, anyway) this library is abstracting that many programs might gloss over. In fact, in the beginning of 2020, the Linux kernel doesn't even have a fully correct implementation which it uses (amongst other things) to calculate correct PLL dividers for CPU clock rates. One that was close was published and posted, but abandoned for non-obvious reasons probably having to do with Linux kernel politics and style (or perhaps some microbenchmark suffered).

Syntactic Sugar

Within a with-interval-math block, the full power of the Common Lisp macro system is utilized to realize a cute feature regarding the parsing of numeric literals. This can be illustrated most succinctly in the following single-line piece of code:

CL-USER> (ratmath:with-interval-math (* 2 ~5))
(9 . 11)

The with-interval-math turns the numeric literal ~5 automatically into the interval '(4.5 . 5.5) which is then multiplied by exactly 2 to return the interval '(9 . 11). The rules of conversion for ~ literals should not be surprising. It should be noted that the interval will always result in rationals even when normal numeric literal parsing rules would have it be a float. The other key to remember regarding the tilde literal is they are parsed according to the rules of significant figures. e.g. ~5 is '(4.5 . 5.5) but ~5.00 is '(4.995 . 5.005) Exponential notation is honored as one might predict, ~1e3 is '(500 . 1500), always as rationals.

These numeric literals can also be parsed outside of a with-interval-math block by passing a string to parse-interval. In parse-interval the tilde character ornamentation is not necessary.

The tilde '~' character is also a function, but it behaves differently depending on the context. Inside a with-interval-math block it turns an exact into an interval and behaves the same way as the infsup function. Outside of this block, it turns intervals into exacts according to some &key argument controls. Refer to the documentation of the ~ function for full details, but here are a few examples:

Random numbers for (e.g.) Monte Carlo analysis using a uniform probability distribution within the interval:

CL-USER> (ratmath:~ '(1 . 2) :random t)  ;; this will always be a float
1.3231988

The :discrete boolean means to only return upper or lower limit. Assumes a 50/50 chance to return either, unless the :alpha option is supplied.

CL-USER> (ratmath:~ '(1 . 2) :random t :discrete t) ;; random select upper/lower
1
CL-USER> (ratmath:~ '(1 . 2) :random t :discrete t :alpha 3/4) ;; 75% chance of upper
2

Several types of calculations are only interested in worst-case/best-case which will be either the upper or lower limit of the interval, e.g.:

CL-USER> (ratmath:~ '(1/14 . 3/7) :upper t)
3/7
CL-USER> (ratmath:~ '(1/14 . 3/7) :lower t)
1/14

Other Tricks and Functions (useful in the REPL)

A common application of these algorithms are to print out a progression of rational approximations of a given number as the size of the numerator and denominator terms is allowed to increase. The following prints out the best convergents and semi-convergents and their error in PPM or PPB (parts-per-million/billion) on the way to system's pi constant:

CL-USER> (ratmath:fractions pi :order 10)
2'd3/1'd1 45070.341 PPM
5'd22/3'd7 -402.499 PPM
8'd179/6'd57 395.270 PPM
9'd355/7'd113 -8.491D+1 PPB
16'd52163/15'd16604 8.474D+1 PPB
19'd312689/17'd99532 -9.277D-3 PPB
20'd833719/19'd265381 2.774D-3 PPB
21'd1146408/19'd364913 -5.128D-4 PPB
23'd5419351/21'd1725033 -7.088D-6 PPB
27'd80143857/25'd25510582 1.453D-7 PPB
28'd245850922/27'd78256779 -1.410D-8 PPB
30'd817696623/28'd260280919 1.531D-9 PPB
32'd2698940791/30'd859099536 1.074D-10 PPB
34'd9978066541/32'd3176117225 -9.265D-12 PPB
35'd32633140414/34'd10387451211 3.833D-13 PPB
38'd238410049439/37'd75888275702 -2.047D-14 PPB
40'd747863288731/38'd238052278317 -2.855D-15 PPB
41'd1257316528023/39'd400216280932 4.860D-16 PPB
43'd5777129400823/41'd1838917402045 5.350D-17 PPB
44'd10296942273623/42'd3277618523158 6.901D-19 PPB
50'd884279719003555/49'd281474976710656 0.000D-1 PPB

The :order 10 argument says to only print a best convergent/semi-convergent at most once per decimal order of magnitude. This avoids flooding the output with semi-convergents. If not specified, the default order is 2, which prints the best approximation for every bit size width in the numerator and denominator.

The ##'d syntax at the beginning of each number is Verilog syntax to describe the bit width of the constant. I wrote these routines originally to facilitate writing Verilog in FPGAs for fractional clock dividers and PWM controllers. The Verilog syntax for size is more convenient than manually counting digits to figure how big of a register I have to instantiate.

It is worth noting that over half of the above convergents to pi will vary from implementation to implementation. The pi constant is defined as a floating point number in Common Lisp and the default behavior of ratmath is to convert floats into rationals using the rational function rather than rationalize, which takes into consideration the host machines limits of floating point precision. (i.e. epsilon) If we instead prefer to stop the approximation at the end of the floating point constant's precision, we can use:

CL-USER> (ratmath:fractions (rationalize pi) :order 10)
2'd3/1'd1 45070.341 PPM
5'd22/3'd7 -402.499 PPM
8'd179/6'd57 395.270 PPM
9'd355/7'd113 -8.491D+1 PPB
16'd52163/15'd16604 8.474D+1 PPB
19'd312689/17'd99532 -9.277D-3 PPB
20'd833719/19'd265381 2.774D-3 PPB
21'd1146408/19'd364913 -5.127D-4 PPB
23'd5419351/21'd1725033 -7.074D-6 PPB
27'd80143857/25'd25510582 1.594D-7 PPB
28'd245850922/27'd78256779 0.000D-1 PPB

Pipe functions

A few public ratmath functions utilize a pattern unique to Common Lisp and return whats described in chapter 25, "Streams and Delayed Evaluation", in the book "Lisp, 3rd Edition" by Winston/Horn. The chapter describes a stream data structure (that I have renamed to "pipe" to avoid name clash confusion), that utilizes Lisp's lexical closures to create something akin to an infinite list. I have modeled much of the internal implementation of this library around the simple concepts described therein. Utilities to manipulate, map, filter, and transform these data structures are included in pipe.lisp and are made public in the ratmath package, though not specifically relating to the ratmath primary interfaces. These routines are superficially documented in pipe.lisp, though depending on familiarity with the above book or Lisp's lexical enclosures and functional programming paradigms, might either seem extremely trivial or extremely obtuse. This library was coded partly as an athletic challenge/experiment in avoiding all usage of iteration/loops/assignments in favor of recursion and functional programming. This (arbitrary) implementation constraint was simultaneously pleasing and frustrating and the pipe construct was an invaluable aid in realizing this.

The most likely useful pipe function is ratmath:rat-pipe, which takes as argument an input number and optional exponent base number which defaults to 2. This optional arg n filters out semi-convergents from the same power-of-n and only outputs the best approximation for each power-of-n increase in denominator/numerator.

CL-USER> (ratmath:rat-pipe pi 10)
(3 #<COMPILED-LEXICAL-CLOSURE (:INTERNAL RATMATH::PIPE-TRANSFORM) #x15B1D73E>)

Seeing the REPL output for this function shows that function returns a pipe. In the above, the number 3 is the first element and the second element is actually not an element at all, but a lexical closure that upon funcalling, will calculate and expose another rational + lexical closure, ad infinitum.

Since we know that this pipe is not infinite and has an end, we can safely convert the pipe into an ordinary list with (ratmath:pipe-to-list *) Note that the real pi, as an irrational number, has an infinite continued fraction and therefore an infinite pipe of rational approximations, but we are doing a rational approximation on Common Lisp's floating point constant which has finite precision and, therefore, a finite continued fraction.

CL-USER> (ratmath:pipe-to-list *)
(3 22/7 179/57 355/113 52163/16604 312689/99532 833719/265381 1146408/364913 
5419351/1725033 80143857/25510582 245850922/78256779 817696623/260280919
2698940791/859099536 9978066541/3176117225 32633140414/10387451211
238410049439/75888275702 747863288731/238052278317 1257316528023/400216280932
5777129400823/1838917402045 10296942273623/3277618523158
884279719003555/281474976710656)

The rat-pipe does allow for irrational number approximation, you simply have to create an infinite pipe of continued fraction terms to send as the first argument instead of an atomic real number. An example generator is in fractions.lisp in the function ratmath:napiers-constant-generator which returns the infinite terms of the continued fraction expansion of Euler's number e

CL-USER> (ratmath:rat-pipe (ratmath:napiers-constant-generator))
(3 #<COMPILED-LEXICAL-CLOSURE (:INTERNAL RATMATH::PIPE-TRANSFORM) #x15C8111E>)
CL-USER> (ratmath::pipe-sink-until 
          (lambda (x) (> (integer-length (denominator x)) 4096)) 
          *)
(41720850604724578859 .... ;; cut to not flood output with a 4096bit rational

Generating 8kbit integer rationals like this makes a good benchmark of Common Lisp and how well it handles its bignums. Letting your rationals grow in size to bignum territory likely will hurt performance significantly. Indeed, I would predict large gains in speed sprinking declare forms throughout the implementation of ratmath signifying intent to only ever use fixnums inputs and outputs.

Another useful pipe function generates lists of ordered rational fractions called Farey sequences. Wikipedia. The function ratmath:farey-pipe is used mostly for the implementation of infsup, but it is possible to use on its own, e.g. to list out a farey sequence of order 7 (no fraction denominator greater than 7):

CL-USER> (ratmath:pipe-to-list (ratmath:farey-pipe 7))
(1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1 7/6 
6/5 5/4 4/3 7/5 3/2 5/3 7/4 2 7/3 5/2 3 7/2 4 5 6 7)

Todo


2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 ratmath

Math utilities for working with rational numbers and intervals.

Author

Jesse Off <jesseoff@me.com>

License

MIT

Version

1

Source

ratmath.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 ratmath/ratmath.asd

Source

ratmath.asd.

Parent Component

ratmath (system).

ASDF Systems

ratmath.


3.1.2 ratmath/package.lisp

Source

ratmath.asd.

Parent Component

ratmath (system).

Packages

ratmath.


3.1.3 ratmath/fifo.lisp

Dependency

package.lisp (file).

Source

ratmath.asd.

Parent Component

ratmath (system).

Internals

3.1.4 ratmath/pipe.lisp

Dependency

fifo.lisp (file).

Source

ratmath.asd.

Parent Component

ratmath (system).

Public Interface
Internals

Previous: , Up: Lisp   [Contents][Index]

3.1.5 ratmath/ratmath.lisp

Dependency

pipe.lisp (file).

Source

ratmath.asd.

Parent Component

ratmath (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 ratmath

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Macros

Macro: infsup (&rest args)

Returns infimum/supremum intervals. All strings and numbers in tree are converted keeping everything else intact. Exacts are fuzzified according to :tol and :abstol first, and then potentially widened further with :limd and :limn, which performs a rational approximation. Intervals already present in the structure, whether by explicit cons or via a string, will not be fuzzified further by :abstol and :tol, but they may be widened if rational approximation is called out via :limd/:limn args. Exact rationals will only convert to intervals if 1) their numerators/denominators exceed :limn/:limd or 2) an :open t arg is given. When the arg is an exact and rational approximation is called out, a 2nd value, alpha, is returned which is a number from 0-1 representing the location of the original exact in the returned interval.

Package

ratmath.

Source

ratmath.lisp.

Macro: pipe-cons (object pipe)
Package

ratmath.

Source

pipe.lisp.

Macro: pipe-sink-until-condition (pipe &rest conds)

Exposes elements of pipe until one of the condition types in conds is signalled. Returns nil for end of pipe, or 3 values: #1 being the interrupted pipe, #2 being the condition object, and #3 being the particular condition clause type in the original args that matched.

Package

ratmath.

Source

pipe.lisp.

Macro: rat (&rest args)

Performs a rational approximation of an exact number or interval. Takes keyword args :limn and :limd to represent max numerator and max denominator. Also will parse numeric strings into intervals ala parse-interval. Always returns an exact (i.e. not another interval).

Package

ratmath.

Source

ratmath.lisp.

Macro: with-interval-math (&rest body)

Replaces arithmetic calls like *-/+ with interval*-/+ and turns ~##.## symbols into literal rational intervals with implied precision based on the number of digits after the decimal point. (as in parse-interval)

Package

ratmath.

Source

ratmath.lisp.


5.1.2 Ordinary functions

Function: farey-pipe ()

Returns a farey sequence; 2nd value is an encapsulated reverse sequence

Package

ratmath.

Source

ratmath.lisp.

Function: fractions (arg &key order)

Prints out rational approximations, one per line, with the PPM or PPB error.

Package

ratmath.

Source

ratmath.lisp.

Function: hull (&rest args)

Takes exacts and intervals as arguments and returns an interval enclosure containing the min/max. If the range between min and max is 0, hull will return an exact.

Package

ratmath.

Source

ratmath.lisp.

Function: interval (l u)

Constructs an interval from the given lower/upper bounds

Package

ratmath.

Source

ratmath.lisp.

Function: list-to-pipe ()

Returns a pipe from a list input argument.

Package

ratmath.

Source

pipe.lisp.

Function: lower (x)

Returns the lower limit of an interval

Package

ratmath.

Source

ratmath.lisp.

Function: napiers-constant-generator ()

Returns an infinite/irrational continued fraction expansion of Euler’s number ’e’

Package

ratmath.

Source

ratmath.lisp.

Function: parse-interval ()

Turns strings of rationals or floats into rational intervals. Infers interval radius from number specification. e.g. 1.000 implies an interval of [.9995, 1.0005) whereas just 1 implies [.5, 1.5). Exponent notation is also recognized; 1e3 is [500, 1500) whereas 1000 is [999.5, 1000.5). A rational specified as 22/7 is converted as (43/14, 45/14).

Package

ratmath.

Source

ratmath.lisp.

Function: pipe-append ()

Appends two pipes together

Package

ratmath.

Source

pipe.lisp.

Function: pipe-apply ()

Runs procedure on every element as they are exposed, but does not transform the element.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-end-after ()

Runs test on each element. When it returns t, the pipe is truncated after that element

Package

ratmath.

Source

pipe.lisp.

Function: pipe-end-before ()

Runs test on each element. When it returns t, the pipe is truncated before that element

Package

ratmath.

Source

pipe.lisp.

Function: pipe-endp (pipe)
Package

ratmath.

Source

pipe.lisp.

Function: pipe-filter ()

If procedure returns t, that particular pipe element is removed from the sequence.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-first (pipe)
Package

ratmath.

Source

pipe.lisp.

Function: pipe-head ()

Truncates a pipe after n (default: 1) elements

Package

ratmath.

Source

pipe.lisp.

Function: pipe-last ()
Package

ratmath.

Source

pipe.lisp.

Function: pipe-mapc ()

Runs function on each element. Returns nothing.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-printer (pipe)
Package

ratmath.

Source

pipe.lisp.

Function: pipe-rest ()

Analogous to the rest function except works on a pipe rather than list

Package

ratmath.

Source

pipe.lisp.

Function: pipe-signaler ()

For each condition object in pipe, set up some useful restarts and signal it. If nothing handles it, the default behavior is to ignore. If the use-value restart is invoked, that value will be returned as a pipe datum element.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-sink ()

Exposes elements of pipe forever, ignoring the elements. Similar to pipe-mapc with a no-op procedure. Useful to provoke the pipeline processing of an infinite pipe.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-sink-until ()

Exposes elements of pipe until test returns t. When that happens, returns the (non-empty) pipe. If the pipe goes empty, returns nil. The test procedure is called with the current element as arg.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-to-list ()

Returns a list from the given pipe input argument. Infinite recursion results if the pipe is infinite.

Package

ratmath.

Source

pipe.lisp.

Function: pipe-transform ()

Runs procedure on each element of pipe; replacing each original element

Package

ratmath.

Source

pipe.lisp.

Function: pipe-uniq ()

Removes duplicates according to optional predicate func. Only dups in sequence are removed.

Package

ratmath.

Source

pipe.lisp.

Function: rat-pipe ()

Returns a pipe of best rational approximations for every power-of-mult numerator/denominator. If arg is not a number, assumes it is a continued fraction pipe.

Package

ratmath.

Source

ratmath.lisp.

Function: upper (x)

Returns the upper limit of an interval

Package

ratmath.

Source

ratmath.lisp.

Function: ~ (&rest args)

Converts intervals into an exacts. :random picks a random value within the interval instead of the midpoint. :rat picks the first convergent rational fraction (which may not be the midpoint) :upper or :lower selects the upper or lower limit. :alpha modifies whats considered the midpoint of the interval. :discrete requests to only return one of either the lower or upper interval limit. By default, returns the midpoint. If passed in a list/tree, recursively modifies everything that looks like an interval and leaves everything else intact.

Package

ratmath.

Source

ratmath.lisp.

Function: ~= (a b)

Compares either exacts or intervals for possible equality.

Package

ratmath.

Source

ratmath.lisp.


5.1.3 Standalone methods

Method: print-object ((obj convergent) stream)
Source

ratmath.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Macros

Macro: best-convergent-test-fn (&rest args)
Package

ratmath.

Source

ratmath.lisp.

Macro: encapsulate (form)
Package

ratmath.

Source

pipe.lisp.

Macro: expose (procedure)
Package

ratmath.

Source

pipe.lisp.

Macro: fifo-get (f)

Removes and returns object from fifo. If fifo becomes empty, struct is destroyed, i.e. setf nil

Package

ratmath.

Source

fifo.lisp.

Macro: fifo-put (f obj)

Adds obj to fifo, instantiating new fifo struct if necessary. Returns new count of fifo

Package

ratmath.

Source

fifo.lisp.

Macro: interval~ (&rest args)

When the ~ function is encounted within a with-interval-math block, it converts exacts to intervals. Outside of the lexical scope of a with-interval-math block, ~ converts intervals into exacts.

Package

ratmath.

Source

ratmath.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.2 Ordinary functions

Function: %infsup (x &key limn limd tol abstol open test-fn)
Package

ratmath.

Source

ratmath.lisp.

Function: %rat (x &key limn limd test-fn)
Package

ratmath.

Source

ratmath.lisp.

Function: best-convergent ()

From a rationalized continued-fraction-pipe arg, returns best convergent honoring limits. 2nd value being the next best ignoring limits.

Package

ratmath.

Source

ratmath.lisp.

Function: calc-stern-brocot (n)
Package

ratmath.

Source

ratmath.lisp.

Function: continued-fraction-pipe ()

Returns a pipe of continued fraction terms from the input rational arg.

Package

ratmath.

Source

ratmath.lisp.

Reader: convergent-a (instance)
Writer: (setf convergent-a) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

a.

Reader: convergent-cf (instance)
Writer: (setf convergent-cf) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

cf.

Function: convergent-denominator (c)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-equal (a b)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-fract (c)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-numerator (c)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-p (object)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-q (c)
Package

ratmath.

Source

ratmath.lisp.

Reader: convergent-q-offset (instance)
Writer: (setf convergent-q-offset) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

q-offset.

Reader: convergent-r0 (instance)
Writer: (setf convergent-r0) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

r0.

Reader: convergent-r1 (instance)
Writer: (setf convergent-r1) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

r1.

Reader: convergent-rat (instance)
Writer: (setf convergent-rat) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

rat.

Function: convergent-ratio (c)
Package

ratmath.

Source

ratmath.lisp.

Function: convergent-stern-brocot (c)

From a convergent struct, make a Stern-Brocot struct by calculating the left and right parents

Package

ratmath.

Source

ratmath.lisp.

Function: convergents-pipe ()

From a continued-fraction-pipe return a pipe of the resultant convergents.

Package

ratmath.

Source

ratmath.lisp.

Function: copy-convergent (instance)
Package

ratmath.

Source

ratmath.lisp.

Function: copy-fifo (instance)
Package

ratmath.

Source

fifo.lisp.

Function: copy-stern-brocot (instance)
Package

ratmath.

Source

ratmath.lisp.

Function: eval-constants (args &optional pure-function-symbolp)
Package

ratmath.

Source

ratmath.lisp.

Function: fifo-count (f)
Package

ratmath.

Source

fifo.lisp.

Reader: fifo-data (instance)
Writer: (setf fifo-data) (instance)
Package

ratmath.

Source

fifo.lisp.

Target Slot

data.

Function: fifo-endp (f)
Package

ratmath.

Source

fifo.lisp.

Function: fifo-head (f)
Package

ratmath.

Source

fifo.lisp.

Reader: fifo-n (instance)
Writer: (setf fifo-n) (instance)
Package

ratmath.

Source

fifo.lisp.

Target Slot

n.

Function: fifo-p (object)
Package

ratmath.

Source

fifo.lisp.

Reader: fifo-tail (instance)
Writer: (setf fifo-tail) (instance)
Package

ratmath.

Source

fifo.lisp.

Target Slot

tail.

Function: functionalp (fn &optional args)
Package

ratmath.

Source

ratmath.lisp.

Function: infsup-limd (arg &key limd limn test-fn open)

Returns an infimum/supremum interval; 2nd value is alpha (>.5 when supremum is closer)

Package

ratmath.

Source

ratmath.lisp.

Function: infsup-tol (x abstol &optional tol)

Returns an infimum/supremum interval given a tolerance.

Package

ratmath.

Source

ratmath.lisp.

Function: interval* (&rest args)
Package

ratmath.

Source

ratmath.lisp.

Function: interval+ (&rest args)
Package

ratmath.

Source

ratmath.lisp.

Function: interval- (&rest args)
Package

ratmath.

Source

ratmath.lisp.

Function: interval/ (&rest args)
Package

ratmath.

Source

ratmath.lisp.

Function: intervalabs (x)
Package

ratmath.

Source

ratmath.lisp.

Function: intervalexp (x)
Package

ratmath.

Source

ratmath.lisp.

Function: intervalexpt (x y)
Package

ratmath.

Source

ratmath.lisp.

Function: intervallog (x &optional n)
Package

ratmath.

Source

ratmath.lisp.

Function: intervalsqrt (x)
Package

ratmath.

Source

ratmath.lisp.

Function: make-convergent (cf r0 r1 a &optional q-offset)
Package

ratmath.

Source

ratmath.lisp.

Function: make-fifo (&key data tail n)
Package

ratmath.

Source

fifo.lisp.

Function: make-semi-convergent (q-offset c)
Package

ratmath.

Source

ratmath.lisp.

Function: make-stern-brocot (left-parent right-parent)
Package

ratmath.

Source

ratmath.lisp.

Function: my- (a b)
Package

ratmath.

Source

ratmath.lisp.

Function: my-min (&rest rest)
Package

ratmath.

Source

ratmath.lisp.

Function: my/ (a b)
Package

ratmath.

Source

ratmath.lisp.

Function: rationalize-continued-fraction-pipe (generator-pipe)

The normal continued-fraction-pipes have each term annotated with a lexical closure that returns the remainder to the terminal value. This value is used to qualify visiting iffy semi-convergents in a denominator/numerator limited rational approximation. With irrational numbers there is no end-value so we can give downpipe logic no insight as to whether an iffy semi-convergent is worthy.

Package

ratmath.

Source

ratmath.lisp.

Function: semi-convergent-closest-to-lim ()

From a specific convergent, check semi-convergents for one below num/denom limits

Package

ratmath.

Source

ratmath.lisp.

Function: semi-convergent-iffy-p (c)

Iffy semi-convergents are the exactly 1/2 CF semi-convergent of even terms.

Package

ratmath.

Source

ratmath.lisp.

Function: semi-convergent-p (c)

Semi-convergents are convergents with a truncated (up to 1/2) CF term

Package

ratmath.

Source

ratmath.lisp.

Function: stern-brocot-denominator (sb)
Package

ratmath.

Source

ratmath.lisp.

Function: stern-brocot-fract (sb)
Package

ratmath.

Source

ratmath.lisp.

Reader: stern-brocot-left-parent (instance)
Writer: (setf stern-brocot-left-parent) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

left-parent.

Function: stern-brocot-numerator (sb)
Package

ratmath.

Source

ratmath.lisp.

Function: stern-brocot-p (object)
Package

ratmath.

Source

ratmath.lisp.

Function: stern-brocot-ratio (sb)
Package

ratmath.

Source

ratmath.lisp.

Reader: stern-brocot-right-parent (instance)
Writer: (setf stern-brocot-right-parent) (instance)
Package

ratmath.

Source

ratmath.lisp.

Target Slot

right-parent.

Function: truncate-within-interval ()

Takes 2 continued-fraction-pipes and returns one that stops at the simplest rational inbetween

Package

ratmath.

Source

ratmath.lisp.


5.2.3 Structures

Structure: convergent

Represents a (semi-)convergent in the rational approximation of a continued fraction.

Package

ratmath.

Source

ratmath.lisp.

Direct superclasses

structure-object.

Direct methods

print-object.

Direct slots
Slot: cf
Readers

convergent-cf.

Writers

(setf convergent-cf).

Slot: r0
Readers

convergent-r0.

Writers

(setf convergent-r0).

Slot: r1
Readers

convergent-r1.

Writers

(setf convergent-r1).

Slot: q-offset
Type

integer

Initform

0

Readers

convergent-q-offset.

Writers

(setf convergent-q-offset).

Slot: a
Readers

convergent-a.

Writers

(setf convergent-a).

Slot: rat
Readers

convergent-rat.

Writers

(setf convergent-rat).

Structure: fifo
Package

ratmath.

Source

fifo.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: data
Type

list

Readers

fifo-data.

Writers

(setf fifo-data).

Slot: tail
Type

list

Readers

fifo-tail.

Writers

(setf fifo-tail).

Slot: n
Type

fixnum

Initform

0

Readers

fifo-n.

Writers

(setf fifo-n).

Structure: stern-brocot

Stern-Brocot left/right parent used to address a node in the Stern-Brocot fraction tree.

Package

ratmath.

Source

ratmath.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: left-parent
Readers

stern-brocot-left-parent.

Writers

(setf stern-brocot-left-parent).

Slot: right-parent
Readers

stern-brocot-right-parent.

Writers

(setf stern-brocot-right-parent).


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   %   (   ~  
B   C   E   F   H   I   L   M   N   P   R   S   T   U   W  
Index Entry  Section

%
%infsup: Private ordinary functions
%rat: Private ordinary functions

(
(setf convergent-a): Private ordinary functions
(setf convergent-cf): Private ordinary functions
(setf convergent-q-offset): Private ordinary functions
(setf convergent-r0): Private ordinary functions
(setf convergent-r1): Private ordinary functions
(setf convergent-rat): Private ordinary functions
(setf fifo-data): Private ordinary functions
(setf fifo-n): Private ordinary functions
(setf fifo-tail): Private ordinary functions
(setf stern-brocot-left-parent): Private ordinary functions
(setf stern-brocot-right-parent): Private ordinary functions

~
~: Public ordinary functions
~=: Public ordinary functions

B
best-convergent: Private ordinary functions
best-convergent-test-fn: Private macros

C
calc-stern-brocot: Private ordinary functions
continued-fraction-pipe: Private ordinary functions
convergent-a: Private ordinary functions
convergent-cf: Private ordinary functions
convergent-denominator: Private ordinary functions
convergent-equal: Private ordinary functions
convergent-fract: Private ordinary functions
convergent-numerator: Private ordinary functions
convergent-p: Private ordinary functions
convergent-q: Private ordinary functions
convergent-q-offset: Private ordinary functions
convergent-r0: Private ordinary functions
convergent-r1: Private ordinary functions
convergent-rat: Private ordinary functions
convergent-ratio: Private ordinary functions
convergent-stern-brocot: Private ordinary functions
convergents-pipe: Private ordinary functions
copy-convergent: Private ordinary functions
copy-fifo: Private ordinary functions
copy-stern-brocot: Private ordinary functions

E
encapsulate: Private macros
eval-constants: Private ordinary functions
expose: Private macros

F
farey-pipe: Public ordinary functions
fifo-count: Private ordinary functions
fifo-data: Private ordinary functions
fifo-endp: Private ordinary functions
fifo-get: Private macros
fifo-head: Private ordinary functions
fifo-n: Private ordinary functions
fifo-p: Private ordinary functions
fifo-put: Private macros
fifo-tail: Private ordinary functions
fractions: Public ordinary functions
Function, %infsup: Private ordinary functions
Function, %rat: Private ordinary functions
Function, (setf convergent-a): Private ordinary functions
Function, (setf convergent-cf): Private ordinary functions
Function, (setf convergent-q-offset): Private ordinary functions
Function, (setf convergent-r0): Private ordinary functions
Function, (setf convergent-r1): Private ordinary functions
Function, (setf convergent-rat): Private ordinary functions
Function, (setf fifo-data): Private ordinary functions
Function, (setf fifo-n): Private ordinary functions
Function, (setf fifo-tail): Private ordinary functions
Function, (setf stern-brocot-left-parent): Private ordinary functions
Function, (setf stern-brocot-right-parent): Private ordinary functions
Function, best-convergent: Private ordinary functions
Function, calc-stern-brocot: Private ordinary functions
Function, continued-fraction-pipe: Private ordinary functions
Function, convergent-a: Private ordinary functions
Function, convergent-cf: Private ordinary functions
Function, convergent-denominator: Private ordinary functions
Function, convergent-equal: Private ordinary functions
Function, convergent-fract: Private ordinary functions
Function, convergent-numerator: Private ordinary functions
Function, convergent-p: Private ordinary functions
Function, convergent-q: Private ordinary functions
Function, convergent-q-offset: Private ordinary functions
Function, convergent-r0: Private ordinary functions
Function, convergent-r1: Private ordinary functions
Function, convergent-rat: Private ordinary functions
Function, convergent-ratio: Private ordinary functions
Function, convergent-stern-brocot: Private ordinary functions
Function, convergents-pipe: Private ordinary functions
Function, copy-convergent: Private ordinary functions
Function, copy-fifo: Private ordinary functions
Function, copy-stern-brocot: Private ordinary functions
Function, eval-constants: Private ordinary functions
Function, farey-pipe: Public ordinary functions
Function, fifo-count: Private ordinary functions
Function, fifo-data: Private ordinary functions
Function, fifo-endp: Private ordinary functions
Function, fifo-head: Private ordinary functions
Function, fifo-n: Private ordinary functions
Function, fifo-p: Private ordinary functions
Function, fifo-tail: Private ordinary functions
Function, fractions: Public ordinary functions
Function, functionalp: Private ordinary functions
Function, hull: Public ordinary functions
Function, infsup-limd: Private ordinary functions
Function, infsup-tol: Private ordinary functions
Function, interval: Public ordinary functions
Function, interval*: Private ordinary functions
Function, interval+: Private ordinary functions
Function, interval-: Private ordinary functions
Function, interval/: Private ordinary functions
Function, intervalabs: Private ordinary functions
Function, intervalexp: Private ordinary functions
Function, intervalexpt: Private ordinary functions
Function, intervallog: Private ordinary functions
Function, intervalsqrt: Private ordinary functions
Function, list-to-pipe: Public ordinary functions
Function, lower: Public ordinary functions
Function, make-convergent: Private ordinary functions
Function, make-fifo: Private ordinary functions
Function, make-semi-convergent: Private ordinary functions
Function, make-stern-brocot: Private ordinary functions
Function, my-: Private ordinary functions
Function, my-min: Private ordinary functions
Function, my/: Private ordinary functions
Function, napiers-constant-generator: Public ordinary functions
Function, parse-interval: Public ordinary functions
Function, pipe-append: Public ordinary functions
Function, pipe-apply: Public ordinary functions
Function, pipe-end-after: Public ordinary functions
Function, pipe-end-before: Public ordinary functions
Function, pipe-endp: Public ordinary functions
Function, pipe-filter: Public ordinary functions
Function, pipe-first: Public ordinary functions
Function, pipe-head: Public ordinary functions
Function, pipe-last: Public ordinary functions
Function, pipe-mapc: Public ordinary functions
Function, pipe-printer: Public ordinary functions
Function, pipe-rest: Public ordinary functions
Function, pipe-signaler: Public ordinary functions
Function, pipe-sink: Public ordinary functions
Function, pipe-sink-until: Public ordinary functions
Function, pipe-to-list: Public ordinary functions
Function, pipe-transform: Public ordinary functions
Function, pipe-uniq: Public ordinary functions
Function, rat-pipe: Public ordinary functions
Function, rationalize-continued-fraction-pipe: Private ordinary functions
Function, semi-convergent-closest-to-lim: Private ordinary functions
Function, semi-convergent-iffy-p: Private ordinary functions
Function, semi-convergent-p: Private ordinary functions
Function, stern-brocot-denominator: Private ordinary functions
Function, stern-brocot-fract: Private ordinary functions
Function, stern-brocot-left-parent: Private ordinary functions
Function, stern-brocot-numerator: Private ordinary functions
Function, stern-brocot-p: Private ordinary functions
Function, stern-brocot-ratio: Private ordinary functions
Function, stern-brocot-right-parent: Private ordinary functions
Function, truncate-within-interval: Private ordinary functions
Function, upper: Public ordinary functions
Function, ~: Public ordinary functions
Function, ~=: Public ordinary functions
functionalp: Private ordinary functions

H
hull: Public ordinary functions

I
infsup: Public macros
infsup-limd: Private ordinary functions
infsup-tol: Private ordinary functions
interval: Public ordinary functions
interval*: Private ordinary functions
interval+: Private ordinary functions
interval-: Private ordinary functions
interval/: Private ordinary functions
intervalabs: Private ordinary functions
intervalexp: Private ordinary functions
intervalexpt: Private ordinary functions
intervallog: Private ordinary functions
intervalsqrt: Private ordinary functions
interval~: Private macros

L
list-to-pipe: Public ordinary functions
lower: Public ordinary functions

M
Macro, best-convergent-test-fn: Private macros
Macro, encapsulate: Private macros
Macro, expose: Private macros
Macro, fifo-get: Private macros
Macro, fifo-put: Private macros
Macro, infsup: Public macros
Macro, interval~: Private macros
Macro, pipe-cons: Public macros
Macro, pipe-sink-until-condition: Public macros
Macro, rat: Public macros
Macro, with-interval-math: Public macros
make-convergent: Private ordinary functions
make-fifo: Private ordinary functions
make-semi-convergent: Private ordinary functions
make-stern-brocot: Private ordinary functions
Method, print-object: Public standalone methods
my-: Private ordinary functions
my-min: Private ordinary functions
my/: Private ordinary functions

N
napiers-constant-generator: Public ordinary functions

P
parse-interval: Public ordinary functions
pipe-append: Public ordinary functions
pipe-apply: Public ordinary functions
pipe-cons: Public macros
pipe-end-after: Public ordinary functions
pipe-end-before: Public ordinary functions
pipe-endp: Public ordinary functions
pipe-filter: Public ordinary functions
pipe-first: Public ordinary functions
pipe-head: Public ordinary functions
pipe-last: Public ordinary functions
pipe-mapc: Public ordinary functions
pipe-printer: Public ordinary functions
pipe-rest: Public ordinary functions
pipe-signaler: Public ordinary functions
pipe-sink: Public ordinary functions
pipe-sink-until: Public ordinary functions
pipe-sink-until-condition: Public macros
pipe-to-list: Public ordinary functions
pipe-transform: Public ordinary functions
pipe-uniq: Public ordinary functions
print-object: Public standalone methods

R
rat: Public macros
rat-pipe: Public ordinary functions
rationalize-continued-fraction-pipe: Private ordinary functions

S
semi-convergent-closest-to-lim: Private ordinary functions
semi-convergent-iffy-p: Private ordinary functions
semi-convergent-p: Private ordinary functions
stern-brocot-denominator: Private ordinary functions
stern-brocot-fract: Private ordinary functions
stern-brocot-left-parent: Private ordinary functions
stern-brocot-numerator: Private ordinary functions
stern-brocot-p: Private ordinary functions
stern-brocot-ratio: Private ordinary functions
stern-brocot-right-parent: Private ordinary functions

T
truncate-within-interval: Private ordinary functions

U
upper: Public ordinary functions

W
with-interval-math: Public macros

Jump to:   %   (   ~  
B   C   E   F   H   I   L   M   N   P   R   S   T   U   W