# 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](https://dominik1123.gitbook.io/hanna/patterns#illegal-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](https://pypi.org/project/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`](https://docs.scipy.org/doc/scipy/reference/constants.html#scipy.constants.physical_constants). These constants can be refered to by their name using the syntax `{name of constant}`.

```python
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+$')`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dominik1123.gitbook.io/hanna/formulas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
