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

A two-dimensional tabulated function f(x, y) defined over a grid of sampled data. The function uses bilinear interpolation to evaluate smooth intermediate values.

Perfect for:

Pressure/temperature/density surfaces

Reaction rate tables

Atmospheric or fluid simulation datasets

โš™๏ธ Constructors

public TwoDTabulatedFunction(
    TreeMap<Float, TreeMap<Float, Float>> table,
    float xStep, float yStep,
    StepMode xMode, StepMode yMode,
    boolean clamp
)

Creates a function directly from a nested table.

๐Ÿงฉ Static Helper: populate

public static TwoDTabulatedFunction populate(
    BiFunction<Float, Float, Float> f,
    float xStart, float yStart,
    float xEnd, float yEnd,
    int xNbr, int yNbr,
    StepMode xMode, StepMode yMode,
    boolean clamp
)

Generates a regular 2D table by sampling a mathematical function over a defined grid. This is ideal for generating simulation data procedurally at runtime or during data generation.

โœ… Example Usage

// Example: f(x, y) = x * y (a simple plane)
TwoDTabulatedFunction table = TwoDTabulatedFunction.populate(
    (x, y) -> x * y,          // Function definition
    0f, 0f,                   // Start (x, y)
    10f, 10f,                 // End (x, y)
    11, 11,                   // Grid resolution
    StepMode.LINEAR,          // Step mode for X
    StepMode.LINEAR,          // Step mode for Y
    true                      // Clamp to bounds
);

float result = table.evaluate(5.5f, 3.2f); // Interpolates โ‰ˆ 17.6

๐Ÿ”— Related Pages:


Clone this wiki locally