-
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.
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
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.
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: 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:
- Data Loaders โ load tabulated datasets from datapacks
- Formic API Home