# Chalk Python SDK Reference: Math Functions
source: https://docs.chalk.ai/api-docs#section-math-functions

## `abs(expr)`
source: https://docs.chalk.ai/api-docs#abs

Compute the absolute value of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the absolute value of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   amount: float
   amount_abs: float = F.abs(_.amount)
```

## `acos(expr)`
source: https://docs.chalk.ai/api-docs#acos

Compute the arccosine of an angle in radians.

**Parameters**

- `expr`: `Underscore | Any`
  The angle in radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Triangle:
   id: str
   cos_angle: float
   angle: float = F.acos(_.cos_angle)
```

## `asin(expr)`
source: https://docs.chalk.ai/api-docs#asin

Compute the arcsine of an angle in radians.

**Parameters**

- `expr`: `Underscore | Any`
  The angle in radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Triangle:
   id: str
   sin_angle: float
   angle: float = F.asin(_.sin_angle)
```

## `bankers_round(value, digits)`
source: https://docs.chalk.ai/api-docs#bankers_round

Round a number to the nearest integer. Values exactly halfway round to the nearest even integer.

**Parameters**

- `value`: `Any`
  The number to round.
- `digits`: `int | None` = `None`
  The number of significant digits to round to. If [`None`](https://docs.python.org/3/library/constants.html#None), the number is banker's rounded to the nearest integer.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   amount: float
   rounded_amount: int = F.bankers_round(_.amount)
```

## `ceil(expr)`
source: https://docs.chalk.ai/api-docs#ceil

Compute the ceiling of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the ceiling of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   amount: float
   amount_ceil: float = F.ceil(_.amount)
```

## `clamp(expr, min_val, max_val)`
source: https://docs.chalk.ai/api-docs#clamp

Clamp a value between a minimum and maximum range.

**Parameters**

- `expr`: `Underscore | Any`
  The value to clamp.
- `min_val`: `Underscore | Any`
  The minimum value.
- `max_val`: `Underscore | Any`
  The maximum value.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class RiskModel:
   id: str
   raw_score: float
   risk_score: float = F.clamp(_.raw_score, 0.0, 1.0)
```

## `cos(expr)`
source: https://docs.chalk.ai/api-docs#cos

Compute the cosine of an angle in radians.

**Parameters**

- `expr`: `Underscore | Any`
  The angle in radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Triangle:
   id: str
   angle: float
   cos_angle: float = F.cos(_.angle)
```

## `cosine_similarity(a, b)`
source: https://docs.chalk.ai/api-docs#cosine_similarity

Compute the cosine similarity between two vectors.

**Parameters**

- `a`: `Underscore`
  The first vector.
- `b`: `Underscore`
  The second vector.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   embedding: Vector[1536]
@features
class Merchant:
   id: str
   embedding: Vector[1536]
@features
class UserMerchant:
   id: str
   user_id: User.id
   user: User
   merchant_id: Merchant.id
   merchant: Merchant
   similarity: float = F.cosine_similarity(_.user.embedding, _.merchant.embedding)
```

## `e()`
source: https://docs.chalk.ai/api-docs#e

Return Euler's number (e ≈ 2.718).

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class CompoundInterest:
   id: str
   rate: float
   time: float
   e_constant: float = F.e()
```

## `exp(expr)`
source: https://docs.chalk.ai/api-docs#exp

Returns Euler’s number raised to the power of x.

**Parameters**

- `expr`: `Underscore | Any`
  The exponent to raise Euler's number to.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class CompoundGrowth:
   id: str
   growth_rate: float
   compound_factor: float = F.exp(_.growth_rate)
```

## `floor(expr)`
source: https://docs.chalk.ai/api-docs#floor

Compute the floor of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the floor of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   amount: float
   amount_floor: float = F.floor(_.amount)
```

## `from_base(value, base)`
source: https://docs.chalk.ai/api-docs#from_base

Convert a number in a base to an integer.

**Parameters**

- `value`: `Any`
  The number to convert.
- `base`: `int | Underscore`
  The base of the number. Must be between 2 and 36.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   base_16: str
   base_10: int = F.from_base(_.base_16, 16)
```

## `greatest(args)`
source: https://docs.chalk.ai/api-docs#greatest

Return the greatest value among the arguments, ignoring null values.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Portfolio:
   id: str
   return_1y: float
   return_3y: float
   return_5y: float
   best_return: float = F.greatest(_.return_1y, _.return_3y, _.return_5y)
```

## `haversine(lat1, lon1, lat2, lon2, unit)`
source: https://docs.chalk.ai/api-docs#haversine

Compute the haversine distance (in kilometers) between two points on Earth. By default the inputs should be in radians, but alternate input units can be specified through the `unit` parameter.

**Parameters**

- `lat1`: `Underscore | Any`
  The latitude of the first point in radians.
- `lon1`: `Underscore | Any`
  The longitude of the first point in radians.
- `lat2`: `Underscore | Any`
  The latitude of the second point in radians.
- `lon2`: `Underscore | Any`
  The longitude of the second point in radians.
- `unit`: `'degrees' | 'radians'` = `'radians'`
  The unit of the input [lat1, lon1, lat2, lon2]. The default is radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Location:
   id: str
   lat1: float
   lon1: float
   lat2: float
   lon2: float
   distance: float = F.haversine(_.lat1, _.lon1, _.lat2, _.lon2, unit="degrees")
```

## `least(args)`
source: https://docs.chalk.ai/api-docs#least

Return the smallest value among the arguments, ignoring null values.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Portfolio:
   id: str
   return_1y: float
   return_3y: float
   return_5y: float
   worst_return: float = F.least(_.return_1y, _.return_3y, _.return_5y)
```

## `ln(expr)`
source: https://docs.chalk.ai/api-docs#ln

Compute the natural logarithm of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the natural logarithm of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Triangle:
   id: str
   hypotenuse: float
   log_hypotenuse: float = F.ln(_.hypotenuse)
```

## `max(values)`
source: https://docs.chalk.ai/api-docs#max

Returns the maximum value in a list of values. This function is meant to be supplied with several columns, not with a single has-many or [`DataFrame`](https://docs.chalk.ai/api-docs#DataFrame).

Input [`None`](https://docs.python.org/3/library/constants.html#None) values are ignored. If all inputs are [`None`](https://docs.python.org/3/library/constants.html#None), then [`None`](https://docs.python.org/3/library/constants.html#None) is returned.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   score_1: float
   score_2: float
   max_score: float = F.max(_.score_1, _.score_2)
```

## `min(values)`
source: https://docs.chalk.ai/api-docs#min

Returns the minimum value in a list of values. This function is meant to be supplied with several columns, not with a single has-many or [`DataFrame`](https://docs.chalk.ai/api-docs#DataFrame).

Input [`None`](https://docs.python.org/3/library/constants.html#None) values are ignored. If all inputs are [`None`](https://docs.python.org/3/library/constants.html#None), then [`None`](https://docs.python.org/3/library/constants.html#None) is returned.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   score_1: float
   score_2: float
   min_score: float = F.min(_.score_1, _.score_2)
```

## `mod(dividend, divisor)`
source: https://docs.chalk.ai/api-docs#mod

Compute the remainder of a division.

**Parameters**

- `dividend`: `Underscore | Any`
  The dividend.
- `divisor`: `Underscore | Any`
  The divisor.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: datetime
   day_of_week_monday: int = F.day_of_week(_.date)
   day_of_week_sunday: int = F.mod(_.day_of_week_monday, 7) + 1
```

## `pi()`
source: https://docs.chalk.ai/api-docs#pi

Return the value of π (pi ≈ 3.14159).

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Geometry:
   id: str
   radius: float
   circumference: float = 2 * F.pi() * _.radius
```

## `power(a, b)`
source: https://docs.chalk.ai/api-docs#power

Raise a to the power of b. Alias for `a ** b`.

**Parameters**

- `a`: `Underscore | Any`
  The base.
- `b`: `Underscore | Any`
  The exponent.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Merchant:
   id: str
   amount_std: float
   amount_var: float = F.power(_.amount_std, 2)
```

## `radians(expr)`
source: https://docs.chalk.ai/api-docs#radians

Convert degrees to radians.

**Parameters**

- `expr`: `Underscore | Any`
  The number to convert to radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class FullCircle:
   id: str
   degrees: float
   radians: float = F.radians(_.degrees)
```

## `rand()`
source: https://docs.chalk.ai/api-docs#rand

Return a random number between 0 and 1.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Sampling:
   id: str
   random_weight: float = F.rand()
```

## `round(value, digits)`
source: https://docs.chalk.ai/api-docs#round

Round a number to the nearest integer.

**Parameters**

- `value`: `Any`
  The number to round.
- `digits`: `int | None` = `None`
  The number of significant digits to round to. If [`None`](https://docs.python.org/3/library/constants.html#None), the number is rounded to the nearest integer.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   amount: float
   rounded_amount: int = F.round(_.amount)
```

## `safe_divide(x, y)`
source: https://docs.chalk.ai/api-docs#safe_divide

Computes x / y, returning None if y is 0.

**Parameters**

- `x`: `Underscore | Any`
  The numerator.
- `y`: `Underscore | Any`
  The denominator.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Merchant:
   id: str
   a: float
   b: float
   amount_std: float = F.safe_divide(_.a, _.b)
```

## `sigmoid(expr)`
source: https://docs.chalk.ai/api-docs#sigmoid

Compute the sigmoid of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the sigmoid of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Sigmoid:
   id: str
   x: float
   sigmoid_of_x: float = F.sigmoid(_.x)
```

## `sin(expr)`
source: https://docs.chalk.ai/api-docs#sin

Compute the sine of an angle in radians.

**Parameters**

- `expr`: `Underscore | Any`
  The angle in radians.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Triangle:
   id: str
   angle: float
   sin_angle: float = F.sin(_.angle)
```

## `sqrt(expr)`
source: https://docs.chalk.ai/api-docs#sqrt

Compute the square root of a number.

**Parameters**

- `expr`: `Underscore | Any`
  The number to compute the square root of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Merchant:
   id: str
   amount_var: float
   amount_std: float = F.sqrt(_.amount_var)
```

## `to_base(number, base)`
source: https://docs.chalk.ai/api-docs#to_base

Convert a number to its string representation in the specified base.

**Parameters**

- `number`: `Underscore | Any`
  The number to convert.
- `base`: `Underscore | Any`
  The base to convert to (2-36).

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Encoding:
   id: str
   decimal_id: int
   hex_id: str = F.to_base(_.decimal_id, 16)
```
