Utilities
Helper functions and utilities to be used and re-used in multiple areas.
Import as:
assume_units(value, unit)
Take a vale with or without unit and return it with a unit.
If a value is given without units, assume the given units. If the value is given with units, return it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to be converted |
required |
unit
|
Quantity
|
Unit to be assumed |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value with assumed units |
Source code in src/rttools/utils.py
kron_delta(ind1, ind2)
Calculate Kronecker-delta for variables i,j.
Compare two indexes and return 0 if the same, otherwise 1. If an ndarray is given, return an ndarray comparing each index individually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ind1
|
int, ndarray<int>
|
Index(es) |
required |
ind2
|
int, ndarray<int>
|
Index(es) |
required |
Returns:
| Type | Description |
|---|---|
int, ndarray<int>
|
1 if ind 1 is identical to ind2, otherwise 0 |
Raises:
| Type | Description |
|---|---|
ValueError
|
The input indexes have different shape. |
Source code in src/rttools/utils.py
reduce_decimal(num, prec=2)
Return a given number with precision or cut trailing zeros if possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
Any
|
Number given, anything that can be turned into a decimal. |
required |
prec
|
int
|
given precision. |
2
|
Returns:
| Type | Description |
|---|---|
str
|
Number with at most the given precision. |