-
Notifications
You must be signed in to change notification settings - Fork 0
Math
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.
| 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 |
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.
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-
LINEARโ uniform spacing in regular space -
LOGโ uniform spacing in logarithmic space (useful for exponential relationships)
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.
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.
// 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-
Samples the forward function
f(x)betweenminandmaxusing the chosenStepMode. -
Inverts the mapping to produce a smooth lookup from
y โ x. - Interpolates during evaluation for continuous results.
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
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| 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:
- Data Loaders โ load tabulated datasets from datapacks
- Formic API Home