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 (linear) height=5 terms=10>
┌──────┬──────┬──────────────────────────────┐
│ 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 (linear) height=2 terms=3>
┌─────┬────────────┐
│ 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:
>>> m = pf.Model()
>>> m.v1 = pf.Variable()
>>> m.v2 = pf.Variable()
>>> expr = pf.Param({"dim1": [1, 2, 3], "value": [1, 2, 3]})
>>> 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):
...
RuntimeError: Cannot evaluate the expression 'expr'. It seems that you forgot to call .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
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 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 | |
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 = pf.Param(
... {
... "city": ["Toronto", "Vancouver", "Boston"],
... "year": [2024, 2024, 2024],
... "population": [10, 2, 8],
... }
... )
>>> cities_and_countries = pl.DataFrame(
... {
... "city": ["Toronto", "Vancouver", "Boston"],
... "country": ["Canada", "Canada", "USA"],
... }
... )
>>> pop_data.map(cities_and_countries)
<Expression (parameter) height=2 terms=2>
┌──────┬─────────┬────────────┐
│ year ┆ country ┆ expression │
│ (1) ┆ (2) ┆ │
╞══════╪═════════╪════════════╡
│ 2024 ┆ Canada ┆ 12 │
│ 2024 ┆ USA ┆ 8 │
└──────┴─────────┴────────────┘
>>> pop_data.map(cities_and_countries, drop_shared_dims=False)
<Expression (parameter) height=3 terms=3>
┌───────────┬──────┬─────────┬────────────┐
│ 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
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
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 (linear) height=5 terms=8>
┌──────┬──────┬──────────────────────────────────┐
│ 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
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 | |
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 = pf.Param(
... {
... "time": ["mon", "tue", "wed", "mon", "tue"],
... "place": [
... "Toronto",
... "Toronto",
... "Toronto",
... "Vancouver",
... "Vancouver",
... ],
... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6],
... }
... )
>>> expr
<Expression (parameter) height=5 terms=5>
┌──────┬───────────┬────────────┐
│ time ┆ place ┆ expression │
│ (3) ┆ (2) ┆ │
╞══════╪═══════════╪════════════╡
│ mon ┆ Toronto ┆ 1000000 │
│ tue ┆ Toronto ┆ 3000000 │
│ wed ┆ Toronto ┆ 2000000 │
│ mon ┆ Vancouver ┆ 1000000 │
│ tue ┆ Vancouver ┆ 2000000 │
└──────┴───────────┴────────────┘
>>> expr.sum("time")
<Expression (parameter) height=2 terms=2>
┌───────────┬────────────┐
│ place ┆ expression │
│ (2) ┆ │
╞═══════════╪════════════╡
│ Toronto ┆ 6000000 │
│ Vancouver ┆ 3000000 │
└───────────┴────────────┘
>>> expr.sum()
<Expression (parameter) terms=1>
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 = pf.Param(
... {
... "time": ["mon", "tue", "wed", "mon", "tue"],
... "place": [
... "Toronto",
... "Toronto",
... "Toronto",
... "Vancouver",
... "Vancouver",
... ],
... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6],
... }
... )
>>> expr
<Expression (parameter) height=5 terms=5>
┌──────┬───────────┬────────────┐
│ 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 (parameter) height=2 terms=2>
┌───────────┬────────────┐
│ 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, _compute_all_rows: bool = True) -> 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
|
_compute_all_rows
|
bool
|
If |
True
|
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
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 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 | |
within(set: SetTypes) -> Expression
Filters this expression to only include the dimensions within the provided set.
Examples:
>>> general_expr = pf.Param({"dim1": [1, 2, 3], "value": [1, 2, 3]})
>>> filter_expr = pf.Param({"dim1": [1, 3], "value": [5, 6]})
>>> 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