NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

MATH_MAP

With MATH_MAP you define where a value lives by giving a formula: the cell is computed from the input values. No mapping table is needed for this.

Placing values by formula

A MATH_MAP consists of input variables and three integer expressions: for the row u, for the position V and for the side (NL or NR). To write and read, you only give the input values – for example the station, year and day of a measurement. NHyDB computes where the value lives, the same way every time.

V is a continuous position across all realms. If a formula reaches beyond the 10¹⁰⁰ positions of one realm, NHyDB distributes the values to further realms by itself.

Check beforehand

Before anything is written, a formula can be tested on sample data – without any database. The check counts valid and invalid inputs and shows whether two inputs would land in the same cell.

An example

Daily values from several measuring stations: station s becomes the row, and the position is computed from year y and day d.

import nhydb
from nhydb.math import MathMap, dry_run

# Rule: station s, year y, day d  ->  row u = s, position V = (y - 2020) * 366 + d
rule = MathMap.compile("measurement", ("s", "y", "d"),
                       u="s", V="(y - 2020) * 366 + d", side="NL")
report = dry_run(rule, [(1, 2025, 1), (1, 2025, 2), (1, 2025, 2), (0, 2025, 5)])
print(report.valid, report.invalid, report.collisions)

with nhydb.create("weather.nhydb") as db:
    db.create_math_map_layout("measurement", ("s", "y", "d"), u="s", V="(y - 2020) * 366 + d",
                              side="NL", payload_type="f64", layout_id=1)
    db.put_math_batch(1, [((1, 2025, 1), 12.5), ((1, 2025, 2), 13.0),
                          ((2, 2025, 1), 9.5)], commit=True)
    print(db.get_math(1, (1, 2025, 2)), db.get_math(1, (3, 2025, 1)))
    cell = db.get_math_cell(1, (1, 2025, 2))
    print(cell["_u"], cell["_V_global"], cell["_lane_name"])

Output:

3 1 1
13.0 None
1 1832 NL

The check finds one invalid input (station 0 would give row 0) and one double assignment. The value for station 1 on day 2 of 2025 is in row 1 at position 5 · 366 + 2 = 1832.

A safeguarded calculation language

The formulas are integer expressions in Python notation with a few permitted functions. They are not executed as Python code but evaluated by NHyDB itself. Intermediate results have a size limit, so that a formula cannot overload the computer.

Good to know

  • A cell is assigned only once. A second value for the same cell is rejected unless you explicitly allow overwriting.
  • Once the first value has been written, the formula of a layout is fixed.
  • The realms of a MATH_MAP belong to it alone.

The formula is the order: if you know the input values, you know the cell.

# MATH_MAP With MATH_MAP you define where a value lives by giving a formula: the cell is computed from the input values. No mapping table is needed for this. ## Placing values by formula A MATH_MAP consists of input variables and three integer expressions: for the row **u**, for the position **V** and for the **side** (NL or NR). To write and read, you only give the input values – for example the station, year and day of a measurement. NHyDB computes where the value lives, the same way every time. V is a continuous position across all realms. If a formula reaches beyond the 10¹⁰⁰ positions of one realm, NHyDB distributes the values to further realms by itself. ## Check beforehand Before anything is written, a formula can be tested on sample data – without any database. The check counts valid and invalid inputs and shows whether two inputs would land in the same cell. ## An example Daily values from several measuring stations: station s becomes the row, and the position is computed from year y and day d. ```pythonimport nhydbfrom nhydb.math import MathMap, dry_run # Rule: station s, year y, day d -> row u = s, position V = (y - 2020) * 366 + drule = MathMap.compile("measurement", ("s", "y", "d"), u="s", V="(y - 2020) * 366 + d", side="NL")report = dry_run(rule, [(1, 2025, 1), (1, 2025, 2), (1, 2025, 2), (0, 2025, 5)])print(report.valid, report.invalid, report.collisions) with nhydb.create("weather.nhydb") as db: db.create_math_map_layout("measurement", ("s", "y", "d"), u="s", V="(y - 2020) * 366 + d", side="NL", payload_type="f64", layout_id=1) db.put_math_batch(1, [((1, 2025, 1), 12.5), ((1, 2025, 2), 13.0), ((2, 2025, 1), 9.5)], commit=True) print(db.get_math(1, (1, 2025, 2)), db.get_math(1, (3, 2025, 1))) cell = db.get_math_cell(1, (1, 2025, 2)) print(cell["_u"], cell["_V_global"], cell["_lane_name"])``` Output: ```text3 1 113.0 None1 1832 NL``` The check finds one invalid input (station 0 would give row 0) and one double assignment. The value for station 1 on day 2 of 2025 is in row 1 at position 5 · 366 + 2 = 1832. ## A safeguarded calculation language The formulas are integer expressions in Python notation with a few permitted functions. They are not executed as Python code but evaluated by NHyDB itself. Intermediate results have a size limit, so that a formula cannot overload the computer. ## Good to know - A cell is assigned only once. A second value for the same cell is rejected unless you explicitly allow overwriting.- Once the first value has been written, the formula of a layout is fixed.- The realms of a MATH_MAP belong to it alone. > The formula is the order: if you know the input values, you know the cell.