Linear expressions¶
Linear expressions are linear combinations of iterates and function values, with
arithmetic and @ support, used to state objectives and performance measures.
LinearExpression
¶
A linear combination of function values, inner products (between points and/ or gradients) and constant scalars.
Attributes:
ndim : int
The dimension of underlying space, i.e., the size of each coefficient matrix.
is_leaf : bool
Indicates whether this expression is a primitive ("leaf") function value (i.e. not
itself built from other `LinearExpression` objects). If `is_leaf` is True, this
object is a function value defined from scratch.
is_binary : bool
Indicates whether a leaf expression is intended to be binary or not (i.e. constrained to only have values 0 or 1 in each dimension).
decomposition_dict : dict
A mapping from each leaf expression to its corresponding coefficient matrix. If
`is_leaf` is True, this dict maps the object to its own coefficient matrix.
counter : int
Tracks the number of leaf `LinearExpression` objects.
References
.. S. Diamond and S. Boyd. "CVXPY: A Python-Embedded Modeling Language for Convex
Optimization." Journal of Machine Learning Research, 17(83):1-5, 2016.
.. B. Goujaud, C. Moucer, F. Glineur, J. Hendrickx, A. Taylor, A. Dieuleveut (2022).
"PEPit: computer-assisted worst-case analyses of first-order optimization methods
in Python."
Source code in fomtools/linearexpression.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |
value
property
¶
value: ndarray
Value of the expression at the worst case found by the last solve.
For the MIP backend this is the solver's incumbent, a feasible point whose
objective is a lower bound on the worst case returned by solve.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the expression is not registered with a |
ValueError
|
If the expression mixes leaves of different |
NotImplementedError
|
With |
__add__
¶
__add__(other: Union[Self, array, list, int, float]) -> Self
Add two LinearExpression objects, ensuring they have the same dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
(LinearExpression, array, list, int, float)
|
The expression or a vector to add to this one. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
ValueError
If the objects (LinearExpression or a vector) to be added together does not have
the same dimension.
ValueError
If the object to be added is not of one of the following types: LinearExpression,
np.ndarray, float, int, or list.
Source code in fomtools/linearexpression.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
__eq__
¶
__eq__(rhs: Union[Self, ndarray, list]) -> EqualityConstraint
Build a equality (==) constraint between this expression and
another LinearExpression object or numpy vector.`
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
Union[Self, ndarray, list]
|
The right-hand side of the inequalit. Scalars are broadcast to the shape of
|
required |
Returns:
| Type | Description |
|---|---|
EqualityConstraint
|
A constraint object representing |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the shapes of |
Notes
This method does not evaluate the equality; it only builds a constraint for the solver.
Source code in fomtools/linearexpression.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
__ge__
¶
__ge__(rhs: Union[Self, ndarray, list]) -> InequalityConstraint
Build a greater-than-or-equality (>=) constraint between this expression and
another LinearExpression object or numpy vector.`
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
Union[Self, ndarray, list]
|
The right-hand side of the inequality. Scalars are broadcast to the shape of
|
required |
Returns:
| Type | Description |
|---|---|
InequalityConstraint
|
A constraint object representing |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the shapes of |
Notes
This method does not evaluate the inequality; it only builds a constraint for the solver.
Source code in fomtools/linearexpression.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
__getitem__
¶
__getitem__(item: Self | int)
Retrieve a coefficient matrix or a sliced LinearExpression corresponding
to item.
If the item is a leaf expression, this method returns its corresponding
coefficient matrix. If the item is an int or a slice, this method
returns a new LinearExpression instance representing a specified subset of
rows from all the corresponding coefficients in the decomposition dictionary.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the specified leaf expression is not found in this |
Source code in fomtools/linearexpression.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
__hash__
¶
__hash__()
Return a hash based on this expression's unique counter.
Each LinearExpression is assigned a unique, monotonically increasing counter at construction. We use a tuple hash to avoid collisions with integers, which is necessary because eq returns an EqualityConstraint (truthy) rather than a bool. Without this, dict[0] could incorrectly match a LinearExpression with counter=0.
Source code in fomtools/linearexpression.py
181 182 183 184 185 186 187 188 189 190 191 | |
__init__
¶
__init__(decomposition_dict: dict, const: ndarray = None, is_binary: bool = False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decomposition_dict
|
dict
|
A mapping from each leaf |
required |
const
|
ndarray or None
|
A one-dimensional array or None (default). If |
None
|
is_binary
|
bool
|
A flag to determine if a leaf element is intended to be binary (0 or 1 in each dimension) or not. Defaults to |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If all the coefficient matrices in the |
ValueError
|
If |
ValueError
|
If the number of rows in |
Source code in fomtools/linearexpression.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
__le__
¶
__le__(rhs: Union[Self, ndarray, list]) -> InequalityConstraint
Build a less-than-or-equality (<=) constraint between this expression and
another LinearExpression object or numpy vector.`
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
Union[Self, ndarray, list]
|
The right-hand side of the inequalit. Scalars are broadcast to the shape of
|
required |
Returns:
| Type | Description |
|---|---|
InequalityConstraint
|
A constraint object representing |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the shapes of |
Notes
This method does not evaluate the inequality; it only builds a constraint for the solver.
Source code in fomtools/linearexpression.py
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
__mul__
¶
__mul__(scalar: Union[float, int]) -> Self
Multiply this LinearExpression object by a scalar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalar
|
float or int
|
The numeric factor to multiply each coefficient matrix by. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
TypeError
If the scalar is not an instance of int or float.
Source code in fomtools/linearexpression.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
__neg__
¶
__neg__() -> Self
Compute the negation of this LinearExpression.
This method is invoked for unary negation (e.g., -expr). It returns a new
expression with each coefficient matrix in the decomposition multiplied by -1.
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Source code in fomtools/linearexpression.py
408 409 410 411 412 413 414 415 416 417 418 419 420 | |
__radd__
¶
__radd__(other: Union[Self, array, list]) -> Self
Reflected addition to support other + expression.
This method is invoked when a LinearExpression object appears on
the right-hand side of a + operation (e.g. np.array([1,2,3]) + expr).
It delegates to __add__, so that both np.array([1,2,3]) + expr and
expr + np.array([1,2,3]) behave identically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
(LinearExpression, array, list)
|
The expression or a vector to add to this one. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
ValueError
If the objects (LinearExpression or a vector) to be added together does not have
the same dimension.
ValueError
If the object to be added is not of one of the following types: LinearExpression,
np.ndarray, or list.
Source code in fomtools/linearexpression.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
__rmatmul__
¶
__rmatmul__(matrix: Union[ndarray, spmatrix]) -> Self
Multiply this LinearExpression object by another matrix.
This method is called when a LinearExpression appears on the right‐hand side
of the @ operator (e.g., M @ expr).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray or spmatrix
|
A matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in fomtools/linearexpression.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
__rmul__
¶
__rmul__(scalar: Union[float, int]) -> Self
Reflected multiplication to support scalar * expression.
This method is invoked when a LinearExpression object appears on the
right-hand side of a * operation (e.g. 3 * expr). It delegates to
to __mul__, so that both expr * 3 and 3 * expr behave identically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalar
|
float or int
|
The numeric factor to multiply each coefficient matrix by. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
TypeError
If the scalar is not an instance of int or float.
Source code in fomtools/linearexpression.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
__rsub__
¶
__rsub__(other: Self) -> Self
Reflected subtraction to support other - expression.
This method is invoked when a LinearExpression object appears on
the right-hand side of a - operation (e.g. np.array([1,2,3]) - expr).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
(LinearExpression, array, list)
|
The expression or a vector that this one is to be subtracted from. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two |
ValueError
|
If the object to be subtracted is not of one of the following types: |
Source code in fomtools/linearexpression.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
__sub__
¶
__sub__(other: Self) -> Self
Subtract one LinearExpression from another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
(LinearExpression, array, list)
|
The expression or a vector xto subtract from this one. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two |
ValueError
|
If the object to be subtracted is not of one of the following types: |
Source code in fomtools/linearexpression.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
__truediv__
¶
__truediv__(scalar: Union[float, int]) -> Self
Divide this LinearExpression by a scalar factor.
This method is invoked when the expression appears on the left-hand side
of a true division (e.g. expr / 3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalar
|
float or int
|
The numeric divisor to apply to every coefficient matrix. |
required |
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the |
ZeroDivisionError
|
If |
Source code in fomtools/linearexpression.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
bound_propagation
¶
bound_propagation() -> tuple[ndarray, ndarray]
Compute the elementwise lower and upper bounds of a linear expression given the bound constraints of its coefficient matrices.
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
A pair (lower, upper) representing the elementwise lower and upper bounds of the evaluated expression. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any computed lower bound exceeds its corresponding upper bound. |
Source code in fomtools/linearexpression.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
from_int
classmethod
¶
from_int(ndim: int, is_binary: bool = False, lb: float = -inf, ub: float = inf, branch_priority: int = 0) -> Self
Initialize a LinearExpression object with an integer shape. Internally, this
creates a decomposition dictionary that maps the LinearExpression object with a sparse
identity matrix of shape (shape, shape).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ndim
|
int
|
The dimension of underlying space, i.e., the size of each coefficient matrix. |
required |
is_binary
|
bool
|
Flag to determine if the leaf expression is binary or not (0 or 1 in each dimension). |
False
|
branch_priority
|
int
|
Branching priority hint for a binary leaf: branch-and-bound solvers that support priorities branch on higher values first. Solvers without such a notion ignore it. |
0
|
Returns:
| Type | Description |
|---|---|
LinearExpression
|
A new |
Source code in fomtools/linearexpression.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
update_bounds
¶
update_bounds(lb: ndarray, ub: ndarray) -> None
Update lower and upper bounds of the LinearExpression objects.
Source code in fomtools/linearexpression.py
628 629 630 631 632 | |