Formulas

Formulas allow for using mathematical expressions for specifications. There are three different parameter types that support formulas:

  • Integer

  • Number

  • PhysicalQuantity

The formula syntax is different for each parameter type and becomes richer wen traversing the list from top to bottom.

Integer formulas

The Integer formula syntax allows any kind of mathematical expression that is valid Python syntax (including parentheses, e.g. "(2 + 3*4) * 5**6") and which will result in an integer value. For that reason the Integer formula syntax forbids true division expressions by default (e.g. 4 / 2, see Patterns). Floor division however is allowed (4 // 2).

Number formulas

The Number formula syntax extends the Integer formula syntax by true division as well as by references to a broad set of numpy functions (and the constant pi). These can be used directly within the expressions: (2 + 3) * 4 * sin(pi / 4). To see which numpy functions are available check Number.np_members.

PhysicalQuantity formulas

The PhysicalQuantity formula syntax extends the Number formula syntax by the possibility to reference constants defined in scipy.constants.physical_constants. These constants can be refered to by their name using the syntax {name of constant}.

class Particle(Configurable):
    rest_energy = PhysicalQuantity(unit='eV')
    
electron = Particle(ConfigTree(
    rest_energy='{electron mass} * {speed of light in vacuum}**2 [eV]'
))

Fine control for the formula syntax

By default the formula syntax for all parameters is activated. You can deactivate it by setting e.g. Integer.formulas = False. If you want to deactivate on a per instance basis you can do so via Integer(formulas=False). You can even customize what kind of formula syntax is allowed by providing a regular expression to formulas. This regular expression will be matched against the specified formula and raise a ValueError if the formula is invalid. If for example you want to allow only addition in the formulas for a specific Integer parameter you can do so via Integer(formulas=r'^(\d+\s*\+\s*)*\d+$').

Last updated