Skip to content

Magnitude

The magnitude axis

The order of magnitude of each token: a number’s value, any other token’s byte length, on a log scale, with the change from the token before and the energy of the window behind it.

Source: src/magnitude.rs.

What it reads that nothing else does

timeout = 30 ; retries = 5 ; max_bytes = 5000000000 ; port = 8080

Every value here is a number. Shape reads one silhouette for all four, spectral one digit texture, and orbit folds none together. Magnitude reads them as m = 1.48, 0.70, 9.70, 3.91: max_bytes is six orders above its neighbours. The same reading finds an address among indices, a magic constant among small literals, a size field among flags, a price among quantities.

The readings

QuantityDefinitionReads
magnitude m(t)a number’s log10(|value|), any other token’s log2(byte_len)the scale of each token
gradientm[i] - m[i-1]the direction and rate of scale change
energysum(m^2) over a trailing window; total_energy over the whole streamwhere scale concentrates

A jump is a token whose gradient reaches the jump threshold, three orders by default; an outlier is a token more than outlier_sigma standard deviations from the stream’s mean magnitude.

Reading the axis

$ trex magnitude --text 'retries = 3 ; max_bytes = 5000000000'
trex magnitude: 36 bytes, 7 tokens, total energy 112.2, 3 scale-jump(s)
  peak magnitude: 9.70 at '5000000000'

--field prints the per-token magnitude, gradient and energy, --jumps the scale discontinuities, --energy the heaviest points by local energy, and --outliers the tokens whose magnitude is a stream outlier. In PowerShell each frame carries Offset, Text, Magnitude, Gradient and Energy, and -Detail adds a frame at every token.

In a pattern

\M{>k} and \M{<k} hold where a token’s magnitude crosses k; \N{mag>k} is a number atom with a magnitude predicate, and the relative forms \N{>+1} read it against a context.

$ trex scan '\M{>5}' --text 'retries = 3 ; max_bytes = 5000000000'
[26..36] "5000000000"

Data model

MagnitudeFrame, one token’s reading:

FieldTypeMeaning
magnitudef32a number’s log10(|value|), any other token’s log2(byte_len)
gradientf32the change from the previous token
energyf32local sum(m^2) over the trailing energy_window

MagnitudeField, keyed by byte offset:

FieldTypeMeaning
n_tokensusizetoken count
spansVec<(usize, usize)>byte span per token
framesVec<MagnitudeFrame>one per token
jumpsVec<usize>byte offsets where |gradient| reaches the threshold
total_energyf32sum(m^2) over the whole stream

Query API: magnitude_at(byte), gradient_at(byte), energy_of(start, end), outliers().

Algorithm

One pass over the token stream:

  1. magnitude: a number’s value parsed (decimal, hex, scientific, underscores stripped) to log10(|value|), else log2(byte_len);
  2. statistics: the stream’s mean and standard deviation of m, and total_energy, in the same scan;
  3. gradient: m[i] - m[i-1], the first token 0;
  4. jump: |gradient| >= jump_threshold records a discontinuity;
  5. energy: local sum(m^2) over the trailing energy_window.

Cost

KernelPer-token costBounded by
magnitudeO(1)numeric parse of the token text
statisticsO(1)one accumulation pass
gradientO(1)fixed
energyO(1) amortizedenergy_window