MAIDR JavaScript API
    Preparing search index...

    Class MathUtilAbstract

    Mathematical utility functions for common operations across the codebase. These utilities help reduce code duplication while maintaining type safety.

    Index

    Methods

    • How an axis's extent reads: a span, or the one value it never leaves.

      5 to 5 is true and says nothing. A rug plot's cross axis prints it because both bindings read a rug as a point trace with a constant on that axis (#1132); a parallel-coordinates column that never varies prints it because the column really is constant; a one-bin histogram prints it because there is one bin. In every case the reader is told a range and handed a point.

      That constant is also why pitch carries nothing on such an axis: every sample sonifies at the bottom of the range, because the range has no height. Saying so is the smallest thing that removes the surprise -- a reader who has been told the axis does not move knows the identical tones are the chart rather than a fault -- and it needs no new announcement convention, because these are stats that already existed and were merely uninformative (#1136).

      What it deliberately does not do is change what pitch means mid-chart. Mapping it to a varying axis instead would hand a reader who has learnt "pitch is y" a chart where pitch is x, unannounced, which is the kind of silent mode change #855 and #947 were both about. That option, and a trace type of its own, are still open on #1132.

      Not for every a to b in a description. A choropleth's jump, a flow's source and target, and a ridgeline's narrowest and widest modes are a pair or a path rather than one axis's extent, and constant x would be the wrong sentence for all three.

      Parameters

      • min: number

        The axis minimum

      • max: number

        The axis maximum

      Returns string

      constant <value> when the axis never moves, <min> to <max> otherwise

    • Safely finds the minimum value from an array of numbers. Returns Infinity for empty arrays (mathematically correct: empty set has no minimum). This prevents subtle bugs where 0 might be confused with actual data.

      Parameters

      • values: number[]

        Array of numbers to find minimum from

      Returns number

      The minimum value or Infinity if array is empty

    • Safely finds the maximum value from an array of numbers. Returns -Infinity for empty arrays (mathematically correct: empty set has no maximum). This prevents subtle bugs where 0 might be confused with actual data.

      Parameters

      • values: number[]

        Array of numbers to find maximum from

      Returns number

      The maximum value or -Infinity if array is empty

    • Finds the minimum value from a 2D array of numbers.

      Parameters

      • values: number[][]

        2D array of numbers

      Returns number

      The minimum value across all nested arrays

    • Finds the maximum value from a 2D array of numbers.

      Parameters

      • values: number[][]

        2D array of numbers

      Returns number

      The maximum value across all nested arrays

    • Finds min and max from an array in a single pass. More efficient than separate min/max calls for large arrays. Returns { min: Infinity, max: -Infinity } for empty arrays.

      Parameters

      • values: number[]

        Array of numbers

      Returns { min: number; max: number }

      Object with min and max properties

    • Finds min and max from a 2D array in a single pass.

      Parameters

      • values: number[][]

        2D array of numbers

      Returns { min: number; max: number }

      Object with min and max properties

    • How an axis's extent reads when the extent may not exist.

      safeMin and safeMax answer Infinity and -Infinity for an empty array by design, and a non-finite coordinate makes both NaN. Handing either pair to spanned produces the string Infinity to -Infinity or NaN to NaN, and a string is exactly what the description dialog's own non-finite blanking cannot catch -- it tests numbers, so the placeholder sails through and is printed and spoken.

      missing is the word the announcements already use for a value that is not there (see FormatUtil.wrapFormat), so the two surfaces agree.

      A genuinely constant axis is not this case: constant 0 is finite, true, and spanned's to say.

      Parameters

      • min: number

        The axis minimum

      • max: number

        The axis maximum

      Returns string

      The span, or missing when there is no finite extent

    • Pearson's product-moment correlation over paired samples, or null when the pairs cannot support the claim.

      What a sighted reader takes from a scatter cloud before any individual point: whether it tilts up, tilts down, or does not tilt. A reader who walks 150 points one at a time has heard 150 numbers and still not been told the relationship they were plotted to show.

      Two-pass and mean-centred rather than the textbook one-pass form (sum(xy) - n*meanX*meanY), which cancels catastrophically when the values are large beside their spread -- a Manhattan plot's genomic positions on x, or epoch-millisecond timestamps -- and can return an r outside [-1, 1] or a negative radicand. Two passes over an in-memory array cost nothing beside the sorts a trace has already paid to build itself.

      Pairs where either coordinate is non-finite are skipped rather than treated as zero: a missing y is not a y of 0, and traces deliberately keep gaps as NaN. Null comes back when:

      • fewer than three pairs survive. Any two distinct points lie exactly on a line, so r would be +/-1 by construction and say nothing about the data;
      • either axis has zero variance, where r is 0/0. An axis that does not move cannot correlate with anything, and the description says so already, in the constant span spanned gives it;
      • the result is not finite.

      Clamped to [-1, 1] because float error routinely yields 1.0000000000000002 on a perfect line, which would print as that and fall outside every strength band a caller tests.

      Parameters

      • xs: readonly number[]

        The x coordinate of each pair

      • ys: readonly number[]

        The y coordinate of each pair, index-aligned with xs

      Returns number | null

      Pearson's r in [-1, 1], or null when there is nothing to claim

    • Counts as whole percentages of their total, adjusted so they add up to 100.

      Largest-remainder apportionment, not four independent roundings. The shares of a scatter's quadrants are read out one after another, and a reader who adds 20, 18, 43 and 20 to 101 has no way to see that the extra point is rounding rather than a chart they have misunderstood -- the arithmetic is the only check they have. Rounding each share on its own lands off 100 routinely: three counts of one in seven give 14, 14, 14 and then 57, which sums to 99.

      The unit is handed to whichever share has the largest discarded fraction, which is the standard rule and the one that moves each share least.

      An empty set of counts, or one summing to zero, comes back as zeroes: there is no share of nothing, and the caller decides whether to say so.

      Parameters

      • counts: readonly number[]

        The counts to apportion. Negative entries are not expected and are counted as given.

      Returns number[]

      One whole percentage per count, summing to exactly 100 whenever the counts sum to more than zero.

    • How many pairs pearson was able to use -- both coordinates finite.

      Reported alongside r so a reader is told the sample the coefficient was actually computed over, which on a layer with gaps is not the point count the summary states above it.

      Parameters

      • xs: readonly number[]

        The x coordinate of each pair

      • ys: readonly number[]

        The y coordinate of each pair, index-aligned with xs

      Returns number

      The number of usable pairs

    • Clamps a value into the inclusive [min, max] range.

      Parameters

      • value: number

        The value to clamp

      • min: number

        Lower bound (inclusive)

      • max: number

        Upper bound (inclusive)

      Returns number

    • Linearly maps a value from one numeric range to another. Collapses to toMin when the source range is zero-width to avoid NaN.

      Parameters

      • value: number

        The value in the source range

      • fromMin: number

        Lower bound of the source range

      • fromMax: number

        Upper bound of the source range

      • toMin: number

        Lower bound of the target range

      • toMax: number

        Upper bound of the target range

      Returns number