Skip to content
Real Ant Engineer edited this page Oct 29, 2025 · 8 revisions

๐Ÿงฎ Math Utilities

The Formic API provides a set of mathematical utilities for handling tabulated functions โ€” functions defined by discrete data points and evaluated via interpolation. These utilities are essential for physics simulations, thermodynamic tables, and any situation where continuous formulas are too complex or unknown.


๐Ÿ“Š Overview

Class Purpose
OneDTabulatedFunction Represents a 1D function defined by sampled points
ReversibleOneDTabulatedFunction A 1D function with both forward and inverse lookup
TwoDTabulatedFunction Represents a 2D function with bilinear interpolation

๐Ÿ”น OneDTabulatedFunction

A one-dimensional tabulated function f(x) built from discrete (x, y) pairs. It supports both linear and logarithmic step modes for sampling or evaluation.

๐Ÿงฉ Example Usage

TreeMap<Float, Float> data = new TreeMap<>();
data.put(100f, 1.2f);
data.put(200f, 1.8f);
data.put(300f, 2.5f);

OneDTabulatedFunction func = new OneDTabulatedFunction(
    data,
    100f, // step size
    StepMode.LINEAR,
    true  // clamp to range
);

float result = func.evaluate(250f); // Interpolated value โ‰ˆ 2.15

๐Ÿ’ก Step Modes

  • LINEAR โ€” uniform spacing in regular space
  • LOG โ€” uniform spacing in logarithmic space (useful for exponential relationships)

๐Ÿ”น ReversibleOneDTabulatedFunction

A bidirectional version of the 1D function, which automatically builds both f(x) and its inverse fโปยน(y). This is particularly useful when modeling relationships like temperature โ†” pressure or density โ†” energy.

๐Ÿง  Core Idea

This class constructs two interpolated functions:

  • f(x) โ€” forward table (e.g. temperature โ†’ pressure)
  • inverse_f(x) โ€” reverse table (e.g. pressure โ†’ temperature)

Both are generated with configurable step modes and sampling resolutions.

๐Ÿงฉ Example Usage

// Define a simple relationship: P = x^2
ReversibleOneDTabulatedFunction pressureTemperature =
    new ReversibleOneDTabulatedFunction(
        x -> x * x,         // f(x)
        0f, 100f,           // domain
        StepMode.LINEAR,    // forward step mode
        1f,                 // forward step
        StepMode.LOG,       // inverse step mode
        0.02f               // inverse step
    );

float P = pressureTemperature.getF(50f);       // Forward: P = 2500
float T = pressureTemperature.getInverseF(2500f); // Inverse: T โ‰ˆ 50

โš™๏ธ How It Works

  1. Samples the forward function f(x) between min and max using the chosen StepMode.
  2. Inverts the mapping to produce a smooth lookup from y โ†’ x.
  3. Interpolates during evaluation for continuous results.

๐Ÿ”น TwoDTabulatedFunction

Represents a two-dimensional tabulated surface f(x, y) โ€” ideal for simulations like:

  • Pressure as a function of temperature and density
  • Reaction rates across two parameters
  • Terrain, atmosphere, or fluid property maps

๐Ÿงฉ Example Usage

TwoDTabulatedFunction table = new TwoDTabulatedFunction(
    new float[]{0, 50, 100},          // X-axis (temperature)
    new float[]{0, 1, 2},             // Y-axis (density)
    new float[][]{
        {1.0f, 1.5f, 2.0f},           // f(x=0)
        {2.0f, 2.5f, 3.0f},           // f(x=50)
        {3.0f, 3.5f, 4.0f}            // f(x=100)
    }
);

float value = table.evaluate(25f, 1.5f); // Bilinear interpolation

๐Ÿง  Summary

Utility Type Key Feature
OneDTabulatedFunction 1D Simple interpolation over sampled data
ReversibleOneDTabulatedFunction 1D Supports both forward and inverse lookup
TwoDTabulatedFunction 2D Bilinear interpolation across a grid

๐Ÿ”— Related Pages:


Clone this wiki locally