Skip to content

Commit

Permalink
expr: add support for trig functions - sin, cos, tan & their inverses
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilgarg28 committed Jan 3, 2025
1 parent 670843c commit a484816
Showing 18 changed files with 713 additions and 120 deletions.
3 changes: 3 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@ AES
APIs
ARNs
aren
arcsin
arccos
arctan
AST
AdministratorAccess
AllowKinesisAccess
6 changes: 6 additions & 0 deletions docs/api.yml
Original file line number Diff line number Diff line change
@@ -146,12 +146,18 @@ sidebar:
title: "Num Expressions"
pages:
- "api-reference/expressions/num/abs"
- "api-reference/expressions/num/arcsin"
- "api-reference/expressions/num/arccos"
- "api-reference/expressions/num/arctan"
- "api-reference/expressions/num/ceil"
- "api-reference/expressions/num/cos"
- "api-reference/expressions/num/floor"
- "api-reference/expressions/num/log"
- "api-reference/expressions/num/pow"
- "api-reference/expressions/num/round"
- "api-reference/expressions/num/sin"
- "api-reference/expressions/num/sqrt"
- "api-reference/expressions/num/tan"
- "api-reference/expressions/num/to_string"

- slug: "api-reference/expressions/str"
128 changes: 128 additions & 0 deletions docs/examples/api-reference/expressions/num.py
Original file line number Diff line number Diff line change
@@ -223,3 +223,131 @@ def test_pow():
0.25,
]
# /docsnip


def test_sin():
# docsnip sin
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.sin()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, np.pi / 2, np.pi])})
assert expr.eval(df, schema={"x": float}).tolist() == [
pytest.approx(0.0),
pytest.approx(1.0),
pytest.approx(0.0),
]
# /docsnip


def test_cos():
# docsnip cos
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.cos()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, np.pi / 2, np.pi])})
assert expr.eval(df, schema={"x": float}).tolist() == [
pytest.approx(1.0),
pytest.approx(0.0),
pytest.approx(-1.0),
]
# /docsnip


def test_tan():
# docsnip tan
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.tan()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, np.pi / 4, np.pi])})
assert expr.eval(df, schema={"x": float}).tolist() == [
pytest.approx(0.0),
pytest.approx(1.0),
pytest.approx(0.0),
]
# /docsnip


def test_arcsin():
# docsnip arcsin
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.arcsin()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, 1, -1, 2])})
assert expr.eval(df, schema={"x": float}).tolist() == [
0.0,
pytest.approx(np.pi / 2),
pytest.approx(-np.pi / 2),
pd.NA, # nan in pandas, arcsin of number greater than 1
]
# /docsnip


def test_arccos():
# docsnip arccos
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.arccos()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, 1, -1, 2])})
assert expr.eval(df, schema={"x": float}).tolist() == [
pytest.approx(np.pi / 2),
pytest.approx(0.0),
pytest.approx(np.pi),
pd.NA, # nan in pandas, arccos of number greater than 1
]
# /docsnip


def test_arctan():
# docsnip arctan
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").num.arctan()

assert expr.typeof(schema={"x": int}) == float
assert expr.typeof(schema={"x": Optional[int]}) == Optional[float]
assert expr.typeof(schema={"x": float}) == float
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([0, 1, -1])})
assert expr.eval(df, schema={"x": float}).tolist() == [
0.0,
pytest.approx(np.pi / 4),
pytest.approx(-np.pi / 4),
]
# /docsnip
33 changes: 33 additions & 0 deletions docs/pages/api-reference/expressions/num/arccos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Arccos
order: 0
status: published
---

### Arccos

Function in `num` namespace to get the value of the inverse trigonometric
cosine function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the arccos of the input data (in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>

:::info
Invoking `arccos` outside the range [-1, 1] will return `NaN`.
:::

<pre snippet="api-reference/expressions/num#arccos"
status="success" message="Getting arccos of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>

35 changes: 35 additions & 0 deletions docs/pages/api-reference/expressions/num/arcsin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Arcsin
order: 0
status: published
---

### Arcsin

Function in `num` namespace to get the value of the inverse trigonometric
sine function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the arcsin of the input data (in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>


:::info
Invoking `arcsin` outside the range [-1, 1] will return `NaN`.
:::


<pre snippet="api-reference/expressions/num#arcsin"
status="success" message="Getting arcsin of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>

29 changes: 29 additions & 0 deletions docs/pages/api-reference/expressions/num/arctan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Arctan
order: 0
status: published
---

### Arctan

Function in `num` namespace to get the value of the inverse trigonometric
tangent function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the arctan of the input data (in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>


<pre snippet="api-reference/expressions/num#arctan"
status="success" message="Getting arctan of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>
29 changes: 29 additions & 0 deletions docs/pages/api-reference/expressions/num/cos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Cos
order: 0
status: published
---

### Cos

Function in `num` namespace to get the value of the cosine function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the cos of the input data (expected
to be in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>


<pre snippet="api-reference/expressions/num#cos"
status="success" message="Getting cos of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>
29 changes: 29 additions & 0 deletions docs/pages/api-reference/expressions/num/sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Sin
order: 0
status: published
---

### Sin

Function in `num` namespace to get the value of the sinusoidal function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the sin of the input data (expected
to be in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>


<pre snippet="api-reference/expressions/num#sin"
status="success" message="Getting sin of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>
29 changes: 29 additions & 0 deletions docs/pages/api-reference/expressions/num/tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Tan
order: 0
status: published
---

### Tan

Function in `num` namespace to get the value of the trigonometric tangent function.

#### Returns
<Expandable type="Expr">
Returns an expression object denoting the tan of the input data (expected
to be in radians).

The data type of the resulting expression is `float` if the input is `int` or
`float` and `Optional[float]` if the input is `Optional[int]` or `Optional[float]`.
</Expandable>


<pre snippet="api-reference/expressions/num#tan"
status="success" message="Getting tan of a number">
</pre>

#### Errors
<Expandable title="Invoking on a non-numeric type">
Error during `typeof` or `eval` if the input expression is not of type int,
float, optional int or optional float.
</Expandable>
3 changes: 3 additions & 0 deletions fennel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [1.5.67] - 2025-01-01
- Add support for trigonometric functions in expressions - `sin`, `cos`, `tan`,
`arcsin`, `arccos`, `arctan`

## [1.5.66] - 2025-01-01
- Add support for deleted datasets in mock client.
Loading

0 comments on commit a484816

Please sign in to comment.