Topic

Chains and regions

Three or more values joined by colons are a betweenness test: R`0:${x}:1` is true when 0≤x≤10 \le x \le 1. Since 0.9.0 a chain can have strict links (:!), open ends (_) and alternatives (||), and Region reads the same text as a set.

Betweenness

A chain holds when its values run in one direction, ascending or descending, with ties allowed. An interval operand counts with both its ends, so 0:(1/4:1/2):1 asks that the whole interval lie inside [0,1][0, 1].

import { R } from "@ratmath/core";

const x = R`1/3`;
R`0:${x}:1`;           // true:  0 ≤ x ≤ 1
R`1:3:2`;              // false: 3 is not between 1 and 2
R`3:2:1`;              // true:  descending counts too
R`0:(1/4:1/2):1`;      // true:  both ends of the interval are inside

A chain is a test, not a number: R`(1:2:3) + 1` throws a TypeError. In TypeScript, use R.between`…`, which is typed as boolean and throws if its text is not a chain.

Strict links, open ends, unions

chainmeans
2:!x:82<x≤82 < x \le 8: :! forbids a tie across that link
2:x:!82≤x<82 \le x < 8
_:x:8x≤8x \le 8: _ beside a colon is an unbounded end
2:x:_x≥2x \ge 2
_:x:!-1 || 1:!x:_x<−1x < -1 or x>1x > 1: || joins chains

A chain with _ always reads ascending (otherwise _:x:8 would hold for every xx). _ anywhere else throws, as do two-value forms such as _:5 or 2:!5, which have no middle value to test.

const x = R`5`;
R`2:!${x}:8`;                   // true
R`_:${x}:!5`;                   // false: x < 5 fails
R`_:${x}:!-1 || 1:!${x}:_`;     // true

Regions: chains as sets

R.region and Region.parse read a chain on a named variable as a set of rationals, without a value for it. One other name in the bounds may be an integer variable, provided each bound is affine in it (like 2n2n or 3(n−1)3(n - 1)); the region is then the union over all integers nn:

2n:x:!2n+1  =  ⋃n∈Z [2n, 2n+1)  =  ⋯∪[−2,−1)∪[0,1)∪[2,3)∪⋯ \texttt{2n:x:!2n+1} \;=\; \bigcup_{n \in \mathbb Z}\, [2n,\ 2n+1) \;=\; \cdots \cup [-2, -1) \cup [0, 1) \cup [2, 3) \cup \cdots
import { R, Region } from "@ratmath/core";

const even = R.region`2n:x:!2n+1`;   // 2n ≤ x < 2n+1 for some integer n
even.contains(R`1/2`);              // true
even.match(R`5/2`);                 // { n: 1 }: which n put it there
even.within(0, 3).toString();       // "[0,1) U [2,3)"
even.breakpoints(0, 3);             // [0, 1, 2, 3]
Region.parse("a:x:b", { params: { a: 1, b: "5/2" } }).contains(R`2`);   // true

intervals(lo, hi) lists the pieces in a window, each { lo, hi, loStrict, hiStrict, n } (null for an unbounded end), and within(lo, hi) returns them as a RationalIntervalSet. Bounds may use + - * /, integer powers, parentheses and juxtaposition (2n). A region reads each chain ascending (lo:x:hi), so 8:x:2 is empty, unlike a plain R chain. A coefficient written next to the integer variable binds tighter than /: 1/2n means 1/(2n)1/(2n), which is not affine and is refused; write n/2 for half of nn. This is what @ratmath/analytic uses for piecewise functions like x^2 if 0:x:!1; 1 otherwise.

Regions on several variables

Join chains with && (and) and || (or) to describe a region in the plane or in space. Region.parse and R.region return a MultiRegion when the text uses && or several variables, or when you pass variables, which also fixes their order. Every bound must be affine (degree one) in the variables, the parameters and the integers, so a region is a finite union of convex polygons or polytopes and all the work is exact. Curved bounds such as x^2 are refused; those belong to @ratmath/analytic.

import { R, Region } from "@ratmath/core";

const T = Region.parse("0:x:1 && 0:y:x", { variables: ["x", "y"] });   // a triangle
T.contains([R`1/2`, R`1/4`]);        // true; a point is an array in variable order, or { x, y }
const [cell] = T.cells();
cell.vertices.map((v) => v.point);   // [1, 1], [0, 0], [1, 0], counter-clockwise
cell.measure;                        // 1/2, the exact area
T.section({ x: R`1/2` }, [0, 1]);    // [0, 1/2]: the y values above x = 1/2

const squares = R.region`n:x:!n+1 && m:y:!m+1`;
squares.match([R`5/2`, R`-1/3`]);   // { n: 2, m: -1 }
squares.cells({ x: [0, 2], y: [0, 2] }).length;   // 9 pieces, 4 of them squares

Bounds may use any variable, written in any order: 0:x:y && 0:y:1 is the same triangle, and cells describes each piece in the declared order (Fourier–Motzkin elimination). Each cell has its bounds (for each variable, lower and upper bounds that are affine in the earlier variables, which is the shape an iterated integral needs), its constraints, its vertices with included false for a corner a strict link leaves out, a bounding box, and its length or area as measure (null in three or more variables). An unbounded region needs a box: cells({ x: [0, 10], y: [0, 10] }). Each integer is pinned by one chain, which may bring in at most one integer that no other chain pins; match finds it as the one-variable region does. Pieces from different || alternatives may overlap, and the closed box can cut a piece down to an edge or a corner, which then has measure 0. breakpoints("x", box) lists the corner coordinates along one variable, where pieces start, end or turn.

Try itEdit the code and run it with @ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs