Expression
Bases: BaseOperableBlock
Represents a linear or quadratic mathematical expression.
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame(
... {
... "item": [1, 1, 1, 2, 2],
... "time": ["mon", "tue", "wed", "mon", "tue"],
... "cost": [1, 2, 3, 4, 5],
... }
... ).set_index(["item", "time"])
>>> m = pf.Model()
>>> m.Time = pf.Variable(df.index)
>>> m.Size = pf.Variable(df.index)
>>> expr = df["cost"] * m.Time + df["cost"] * m.Size
>>> expr
<Expression height=5 terms=10 type=linear>
┌──────┬──────┬──────────────────────────────┐
│ item ┆ time ┆ expression │
│ (2) ┆ (3) ┆ │
╞══════╪══════╪══════════════════════════════╡
│ 1 ┆ mon ┆ Time[1,mon] + Size[1,mon] │
│ 1 ┆ tue ┆ 2 Time[1,tue] +2 Size[1,tue] │
│ 1 ┆ wed ┆ 3 Time[1,wed] +3 Size[1,wed] │
│ 2 ┆ mon ┆ 4 Time[2,mon] +4 Size[2,mon] │
│ 2 ┆ tue ┆ 5 Time[2,tue] +5 Size[2,tue] │
└──────┴──────┴──────────────────────────────┘
Methods:
| Name | Description |
|---|---|
constant |
Creates a new expression equal to the given constant. |
degree |
Returns the degree of the expression (0=constant, 1=linear, 2=quadratic). |
evaluate |
Computes the value of the expression using the variables' solutions. |
map |
Replaces the dimensions that are shared with mapping_set with the other dimensions found in mapping_set. |
rolling_sum |
Calculates the rolling sum of the Expression over a specified window size for a given dimension. |
sum |
Sums an expression over specified dimensions. |
sum_by |
Like |
to_expr |
Returns the expression itself. |
to_str |
Converts the expression to a human-readable string, or several arranged in a table. |
within |
Filters this expression to only include the dimensions within the provided set. |
Attributes:
| Name | Type | Description |
|---|---|---|
constant_terms |
DataFrame
|
Returns all the constant terms in the expression. |
is_quadratic |
bool
|
Returns |
terms |
int
|
The number of terms across all subexpressions. |
variable_terms |
DataFrame
|
Returns all the non-constant terms in the expression. |
Source code in pyoframe/_core.py
constant_terms: pl.DataFrame
Returns all the constant terms in the expression.
is_quadratic: bool
Returns True if the expression is quadratic, False otherwise.
Computes in O(1) since expressions are quadratic if and only if self.data contain the QUAD_VAR_KEY column.
Examples:
terms: int
The number of terms across all subexpressions.
Expressions equal to zero count as one term.
Examples:
>>> import polars as pl
>>> m = pf.Model()
>>> m.v = pf.Variable({"t": [1, 2]})
>>> coef = pl.DataFrame({"t": [1, 2], "coef": [0, 1]})
>>> coef * (m.v + 4)
<Expression height=2 terms=3 type=linear>
┌─────┬────────────┐
│ t ┆ expression │
│ (2) ┆ │
╞═════╪════════════╡
│ 1 ┆ 0 │
│ 2 ┆ 4 + v[2] │
└─────┴────────────┘
>>> (coef * (m.v + 4)).terms
3
variable_terms: pl.DataFrame
Returns all the non-constant terms in the expression.
constant(constant: int | float) -> Expression
Creates a new expression equal to the given constant.
Examples:
Source code in pyoframe/_core.py
degree(return_str: bool = False) -> int | str
Returns the degree of the expression (0=constant, 1=linear, 2=quadratic).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_str
|
bool
|
If |
False
|
Examples:
>>> import pandas as pd
>>> m = pf.Model()
>>> m.v1 = pf.Variable()
>>> m.v2 = pf.Variable()
>>> expr = pd.DataFrame({"dim1": [1, 2, 3], "value": [1, 2, 3]}).to_expr()
>>> expr.degree()
0
>>> expr *= m.v1
>>> expr.degree()
1
>>> expr += (m.v2**2).over("dim1")
>>> expr.degree()
2
>>> expr.degree(return_str=True)
'quadratic'
Source code in pyoframe/_core.py
evaluate() -> pl.DataFrame
Computes the value of the expression using the variables' solutions.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A Polars |
Examples:
>>> m = pf.Model()
>>> m.X = pf.Variable({"dim1": [1, 2, 3]}, lb=10, ub=10)
>>> m.expr = 2 * m.X * m.X + 1
>>> m.expr.evaluate()
Traceback (most recent call last):
...
ValueError: Cannot evaluate the expression 'expr' before calling model.optimize().
>>> m.constant_expression = m.expr - 2 * m.X * m.X
>>> m.constant_expression.evaluate()
shape: (3, 2)
┌──────┬──────────┐
│ dim1 ┆ solution │
│ --- ┆ --- │
│ i64 ┆ f64 │
╞══════╪══════════╡
│ 1 ┆ 1.0 │
│ 2 ┆ 1.0 │
│ 3 ┆ 1.0 │
└──────┴──────────┘
>>> m.optimize()
>>> m.expr.evaluate()
shape: (3, 2)
┌──────┬──────────┐
│ dim1 ┆ solution │
│ --- ┆ --- │
│ i64 ┆ f64 │
╞══════╪══════════╡
│ 1 ┆ 201.0 │
│ 2 ┆ 201.0 │
│ 3 ┆ 201.0 │
└──────┴──────────┘
Source code in pyoframe/_core.py
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 | |
map(mapping_set: SetTypes, drop_shared_dims: bool = True) -> Expression
Replaces the dimensions that are shared with mapping_set with the other dimensions found in mapping_set.
This is particularly useful to go from one type of dimensions to another. For example, to convert data that is indexed by city to data indexed by country (see example).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping_set
|
SetTypes
|
The set to map the expression to. This can be a DataFrame, Index, or another Set. |
required |
drop_shared_dims
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Expression
|
A new Expression containing the result of the mapping operation. |
Examples:
>>> import polars as pl
>>> pop_data = pl.DataFrame(
... {
... "city": ["Toronto", "Vancouver", "Boston"],
... "year": [2024, 2024, 2024],
... "population": [10, 2, 8],
... }
... ).to_expr()
>>> cities_and_countries = pl.DataFrame(
... {
... "city": ["Toronto", "Vancouver", "Boston"],
... "country": ["Canada", "Canada", "USA"],
... }
... )
>>> pop_data.map(cities_and_countries)
<Expression height=2 terms=2 type=constant>
┌──────┬─────────┬────────────┐
│ year ┆ country ┆ expression │
│ (1) ┆ (2) ┆ │
╞══════╪═════════╪════════════╡
│ 2024 ┆ Canada ┆ 12 │
│ 2024 ┆ USA ┆ 8 │
└──────┴─────────┴────────────┘
>>> pop_data.map(cities_and_countries, drop_shared_dims=False)
<Expression height=3 terms=3 type=constant>
┌───────────┬──────┬─────────┬────────────┐
│ city ┆ year ┆ country ┆ expression │
│ (3) ┆ (1) ┆ (2) ┆ │
╞═══════════╪══════╪═════════╪════════════╡
│ Toronto ┆ 2024 ┆ Canada ┆ 10 │
│ Vancouver ┆ 2024 ┆ Canada ┆ 2 │
│ Boston ┆ 2024 ┆ USA ┆ 8 │
└───────────┴──────┴─────────┴────────────┘
Source code in pyoframe/_core.py
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 | |
rolling_sum(over: str, window_size: int) -> Expression
Calculates the rolling sum of the Expression over a specified window size for a given dimension.
This method applies a rolling sum operation over the dimension specified by over,
using a window defined by window_size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
over
|
str
|
The name of the dimension (column) over which the rolling sum is calculated. This dimension must exist within the Expression's dimensions. |
required |
window_size
|
int
|
The size of the moving window in terms of number of records. The rolling sum is calculated over this many consecutive elements. |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
A new Expression instance containing the result of the rolling sum operation. This new Expression retains all dimensions (columns) of the original data, with the rolling sum applied over the specified dimension. |
Examples:
>>> import polars as pl
>>> cost = pl.DataFrame(
... {
... "item": [1, 1, 1, 2, 2],
... "time": [1, 2, 3, 1, 2],
... "cost": [1, 2, 3, 4, 5],
... }
... )
>>> m = pf.Model()
>>> m.quantity = pf.Variable(cost[["item", "time"]])
>>> (m.quantity * cost).rolling_sum(over="time", window_size=2)
<Expression height=5 terms=8 type=linear>
┌──────┬──────┬──────────────────────────────────┐
│ item ┆ time ┆ expression │
│ (2) ┆ (3) ┆ │
╞══════╪══════╪══════════════════════════════════╡
│ 1 ┆ 1 ┆ quantity[1,1] │
│ 1 ┆ 2 ┆ quantity[1,1] +2 quantity[1,2] │
│ 1 ┆ 3 ┆ 2 quantity[1,2] +3 quantity[1,3] │
│ 2 ┆ 1 ┆ 4 quantity[2,1] │
│ 2 ┆ 2 ┆ 4 quantity[2,1] +5 quantity[2,2] │
└──────┴──────┴──────────────────────────────────┘
Source code in pyoframe/_core.py
sum(*over: str) -> Expression
Sums an expression over specified dimensions.
If no dimensions are specified, the sum is taken over all of the expression's dimensions.
Examples:
>>> expr = pl.DataFrame(
... {
... "time": ["mon", "tue", "wed", "mon", "tue"],
... "place": [
... "Toronto",
... "Toronto",
... "Toronto",
... "Vancouver",
... "Vancouver",
... ],
... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6],
... }
... ).to_expr()
>>> expr
<Expression height=5 terms=5 type=constant>
┌──────┬───────────┬────────────┐
│ time ┆ place ┆ expression │
│ (3) ┆ (2) ┆ │
╞══════╪═══════════╪════════════╡
│ mon ┆ Toronto ┆ 1000000 │
│ tue ┆ Toronto ┆ 3000000 │
│ wed ┆ Toronto ┆ 2000000 │
│ mon ┆ Vancouver ┆ 1000000 │
│ tue ┆ Vancouver ┆ 2000000 │
└──────┴───────────┴────────────┘
>>> expr.sum("time")
<Expression height=2 terms=2 type=constant>
┌───────────┬────────────┐
│ place ┆ expression │
│ (2) ┆ │
╞═══════════╪════════════╡
│ Toronto ┆ 6000000 │
│ Vancouver ┆ 3000000 │
└───────────┴────────────┘
>>> expr.sum()
<Expression terms=1 type=constant>
9000000
If the given dimensions don't exist, an error will be raised:
>>> expr.sum("city")
Traceback (most recent call last):
...
AssertionError: Cannot sum over ['city'] as it is not in ['time', 'place']
See Also
pyoframe.Expression.sum_by for summing over all dimensions except those that are specified.
Source code in pyoframe/_core.py
sum_by(*by: str)
Like Expression.sum, but the sum is taken over all dimensions except those specified in by (just like a group_by().sum() operation).
Examples:
>>> expr = pl.DataFrame(
... {
... "time": ["mon", "tue", "wed", "mon", "tue"],
... "place": [
... "Toronto",
... "Toronto",
... "Toronto",
... "Vancouver",
... "Vancouver",
... ],
... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6],
... }
... ).to_expr()
>>> expr
<Expression height=5 terms=5 type=constant>
┌──────┬───────────┬────────────┐
│ time ┆ place ┆ expression │
│ (3) ┆ (2) ┆ │
╞══════╪═══════════╪════════════╡
│ mon ┆ Toronto ┆ 1000000 │
│ tue ┆ Toronto ┆ 3000000 │
│ wed ┆ Toronto ┆ 2000000 │
│ mon ┆ Vancouver ┆ 1000000 │
│ tue ┆ Vancouver ┆ 2000000 │
└──────┴───────────┴────────────┘
>>> expr.sum_by("place")
<Expression height=2 terms=2 type=constant>
┌───────────┬────────────┐
│ place ┆ expression │
│ (2) ┆ │
╞═══════════╪════════════╡
│ Toronto ┆ 6000000 │
│ Vancouver ┆ 3000000 │
└───────────┴────────────┘
If the specified dimensions don't exist, an error will be raised:
>>> expr.sum_by("city")
Traceback (most recent call last):
...
ValueError: Cannot sum by ['city'] because it is not a valid dimension. The expression's dimensions are: ['time', 'place'].
>>> total_sum = expr.sum()
>>> total_sum.sum_by("time")
Traceback (most recent call last):
...
ValueError: Cannot sum a dimensionless expression.
See Also
pyoframe.Expression.sum for summing over specified dimensions.
Source code in pyoframe/_core.py
to_expr() -> Expression
to_str(str_col_name: str = 'expression', include_const_term: bool = True, return_df: bool = False) -> str | pl.DataFrame
Converts the expression to a human-readable string, or several arranged in a table.
Long expressions are truncated according to Config.print_max_terms and Config.print_polars_config.
str(pyoframe.Expression) is equivalent to pyoframe.Expression.to_str().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str_col_name
|
str
|
The name of the column containing the string representation of the expression (dimensioned expressions only). |
'expression'
|
include_const_term
|
bool
|
If |
True
|
return_df
|
bool
|
If |
False
|
Examples:
>>> import polars as pl
>>> m = pf.Model()
>>> x = pf.Set(x=range(1000))
>>> y = pf.Set(y=range(1000))
>>> m.V = pf.Variable(x, y)
>>> expr = 2 * m.V * m.V + 3
>>> print(expr.to_str())
┌────────┬────────┬──────────────────────────────┐
│ x ┆ y ┆ expression │
│ (1000) ┆ (1000) ┆ │
╞════════╪════════╪══════════════════════════════╡
│ 0 ┆ 0 ┆ 3 +2 V[0,0] * V[0,0] │
│ 0 ┆ 1 ┆ 3 +2 V[0,1] * V[0,1] │
│ 0 ┆ 2 ┆ 3 +2 V[0,2] * V[0,2] │
│ 0 ┆ 3 ┆ 3 +2 V[0,3] * V[0,3] │
│ 0 ┆ 4 ┆ 3 +2 V[0,4] * V[0,4] │
│ … ┆ … ┆ … │
│ 999 ┆ 995 ┆ 3 +2 V[999,995] * V[999,995] │
│ 999 ┆ 996 ┆ 3 +2 V[999,996] * V[999,996] │
│ 999 ┆ 997 ┆ 3 +2 V[999,997] * V[999,997] │
│ 999 ┆ 998 ┆ 3 +2 V[999,998] * V[999,998] │
│ 999 ┆ 999 ┆ 3 +2 V[999,999] * V[999,999] │
└────────┴────────┴──────────────────────────────┘
>>> expr = expr.sum("y")
>>> print(expr.to_str())
┌────────┬─────────────────────────────────────────────────────────────────────────────────────────┐
│ x ┆ expression │
│ (1000) ┆ │
╞════════╪═════════════════════════════════════════════════════════════════════════════════════════╡
│ 0 ┆ 3000 +2 V[0,0] * V[0,0] +2 V[0,1] * V[0,1] +2 V[0,2] * V[0,2] +2 V[0,3] * V[0,3] … │
│ 1 ┆ 3000 +2 V[1,0] * V[1,0] +2 V[1,1] * V[1,1] +2 V[1,2] * V[1,2] +2 V[1,3] * V[1,3] … │
│ 2 ┆ 3000 +2 V[2,0] * V[2,0] +2 V[2,1] * V[2,1] +2 V[2,2] * V[2,2] +2 V[2,3] * V[2,3] … │
│ 3 ┆ 3000 +2 V[3,0] * V[3,0] +2 V[3,1] * V[3,1] +2 V[3,2] * V[3,2] +2 V[3,3] * V[3,3] … │
│ 4 ┆ 3000 +2 V[4,0] * V[4,0] +2 V[4,1] * V[4,1] +2 V[4,2] * V[4,2] +2 V[4,3] * V[4,3] … │
│ … ┆ … │
│ 995 ┆ 3000 +2 V[995,0] * V[995,0] +2 V[995,1] * V[995,1] +2 V[995,2] * V[995,2] +2 V[995,3] * │
│ ┆ V[995,3] … │
│ 996 ┆ 3000 +2 V[996,0] * V[996,0] +2 V[996,1] * V[996,1] +2 V[996,2] * V[996,2] +2 V[996,3] * │
│ ┆ V[996,3] … │
│ 997 ┆ 3000 +2 V[997,0] * V[997,0] +2 V[997,1] * V[997,1] +2 V[997,2] * V[997,2] +2 V[997,3] * │
│ ┆ V[997,3] … │
│ 998 ┆ 3000 +2 V[998,0] * V[998,0] +2 V[998,1] * V[998,1] +2 V[998,2] * V[998,2] +2 V[998,3] * │
│ ┆ V[998,3] … │
│ 999 ┆ 3000 +2 V[999,0] * V[999,0] +2 V[999,1] * V[999,1] +2 V[999,2] * V[999,2] +2 V[999,3] * │
│ ┆ V[999,3] … │
└────────┴─────────────────────────────────────────────────────────────────────────────────────────┘
>>> expr = expr.sum("x")
>>> print(expr.to_str())
3000000 +2 V[0,0] * V[0,0] +2 V[0,1] * V[0,1] +2 V[0,2] * V[0,2] +2 V[0,3] * V[0,3] …
Source code in pyoframe/_core.py
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 | |
within(set: SetTypes) -> Expression
Filters this expression to only include the dimensions within the provided set.
Examples:
>>> import pandas as pd
>>> general_expr = pd.DataFrame(
... {"dim1": [1, 2, 3], "value": [1, 2, 3]}
... ).to_expr()
>>> filter_expr = pd.DataFrame({"dim1": [1, 3], "value": [5, 6]}).to_expr()
>>> general_expr.within(filter_expr).data
shape: (2, 3)
┌──────┬─────────┬───────────────┐
│ dim1 ┆ __coeff ┆ __variable_id │
│ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ u32 │
╞══════╪═════════╪═══════════════╡
│ 1 ┆ 1.0 ┆ 0 │
│ 3 ┆ 3.0 ┆ 0 │
└──────┴─────────┴───────────────┘
Comments