-
Notifications
You must be signed in to change notification settings - Fork 0
Math
Mathematical utilities for handling tabulated functions — functions defined by discrete data points and evaluated via interpolation.
| 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.
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.6A collection of numerical methods for finding roots, minima, and solving cubic equations.
| Method | Description |
|---|---|
dichotomy |
Finds a root of f(x) in [a, b] using the bisection method |
gradientDecent |
Finds the minimum of a function via adaptive gradient descent |
solveCubic |
Solves cubic equations of the form ax³ + bx² + cx + d = 0
|
// Solve f(x) = 0 for f(x) = x^2 - 4 → roots at ±2
float root = Solvers.dichotomy(
x -> x * x - 4,
0f, 5f, // interval
1e-4f // tolerance
);
System.out.println(root); // ≈ 2.0// Find the minimum of f(x) = (x - 3)^2
float minimum = Solvers.gradientDecent(
x -> (x - 3f) * (x - 3f),
0f, // start
0.1f, // step size
1e-3f // dx
);
System.out.println(minimum); // ≈ 3.0// Solve 2x³ - 4x² - 22x + 24 = 0
double[] roots = Solvers.solveCubic(2, -4, -22, 24);
System.out.println(Arrays.toString(roots));
// → [6.0, -1.999..., 2.0]🔗 Related Pages:
- Data Loaders — load tabulated datasets from datapacks
- Formic API Home