0
# Mathematical Functions and Expressions
1
2
Built-in mathematical functions and expression utilities for constructing complex objective functions and constraints. Includes trigonometric, logarithmic, logical operations, and symbolic manipulation capabilities for nonlinear optimization problems.
3
4
## Capabilities
5
6
### Mathematical Functions
7
8
Standard mathematical functions for nonlinear expressions including logarithmic, exponential, and trigonometric functions.
9
10
```python { .api }
11
def log(x):
12
"""
13
Natural logarithm function.
14
15
Args:
16
x: Expression or numeric value
17
18
Returns:
19
Expression: log(x)
20
"""
21
22
def log10(x):
23
"""
24
Base-10 logarithm function.
25
26
Args:
27
x: Expression or numeric value
28
29
Returns:
30
Expression: log10(x)
31
"""
32
33
def exp(x):
34
"""
35
Exponential function.
36
37
Args:
38
x: Expression or numeric value
39
40
Returns:
41
Expression: exp(x)
42
"""
43
44
def sqrt(x):
45
"""
46
Square root function.
47
48
Args:
49
x: Expression or numeric value
50
51
Returns:
52
Expression: sqrt(x)
53
"""
54
55
def ceil(x):
56
"""
57
Ceiling function.
58
59
Args:
60
x: Expression or numeric value
61
62
Returns:
63
Expression: ceil(x)
64
"""
65
66
def floor(x):
67
"""
68
Floor function.
69
70
Args:
71
x: Expression or numeric value
72
73
Returns:
74
Expression: floor(x)
75
"""
76
```
77
78
### Trigonometric Functions
79
80
Trigonometric and hyperbolic functions for trigonometric optimization problems and periodic constraints.
81
82
```python { .api }
83
def sin(x):
84
"""
85
Sine function.
86
87
Args:
88
x: Expression or numeric value (radians)
89
90
Returns:
91
Expression: sin(x)
92
"""
93
94
def cos(x):
95
"""
96
Cosine function.
97
98
Args:
99
x: Expression or numeric value (radians)
100
101
Returns:
102
Expression: cos(x)
103
"""
104
105
def tan(x):
106
"""
107
Tangent function.
108
109
Args:
110
x: Expression or numeric value (radians)
111
112
Returns:
113
Expression: tan(x)
114
"""
115
116
def sinh(x):
117
"""
118
Hyperbolic sine function.
119
120
Args:
121
x: Expression or numeric value
122
123
Returns:
124
Expression: sinh(x)
125
"""
126
127
def cosh(x):
128
"""
129
Hyperbolic cosine function.
130
131
Args:
132
x: Expression or numeric value
133
134
Returns:
135
Expression: cosh(x)
136
"""
137
138
def tanh(x):
139
"""
140
Hyperbolic tangent function.
141
142
Args:
143
x: Expression or numeric value
144
145
Returns:
146
Expression: tanh(x)
147
"""
148
149
def asin(x):
150
"""
151
Inverse sine function.
152
153
Args:
154
x: Expression or numeric value
155
156
Returns:
157
Expression: asin(x)
158
"""
159
160
def acos(x):
161
"""
162
Inverse cosine function.
163
164
Args:
165
x: Expression or numeric value
166
167
Returns:
168
Expression: acos(x)
169
"""
170
171
def atan(x):
172
"""
173
Inverse tangent function.
174
175
Args:
176
x: Expression or numeric value
177
178
Returns:
179
Expression: atan(x)
180
"""
181
182
def asinh(x):
183
"""
184
Inverse hyperbolic sine function.
185
186
Args:
187
x: Expression or numeric value
188
189
Returns:
190
Expression: asinh(x)
191
"""
192
193
def acosh(x):
194
"""
195
Inverse hyperbolic cosine function.
196
197
Args:
198
x: Expression or numeric value
199
200
Returns:
201
Expression: acosh(x)
202
"""
203
204
def atanh(x):
205
"""
206
Inverse hyperbolic tangent function.
207
208
Args:
209
x: Expression or numeric value
210
211
Returns:
212
Expression: atanh(x)
213
"""
214
```
215
216
### Logical Operations
217
218
Logical operators and conditional expressions for constraint programming and logical modeling.
219
220
```python { .api }
221
def land(*args):
222
"""
223
Logical AND operation.
224
225
Args:
226
*args: Boolean expressions or values
227
228
Returns:
229
Expression: Logical AND of arguments
230
"""
231
232
def lor(*args):
233
"""
234
Logical OR operation.
235
236
Args:
237
*args: Boolean expressions or values
238
239
Returns:
240
Expression: Logical OR of arguments
241
"""
242
243
def lnot(arg):
244
"""
245
Logical NOT operation.
246
247
Args:
248
arg: Boolean expression or value
249
250
Returns:
251
Expression: Logical NOT of argument
252
"""
253
254
def xor(*args):
255
"""
256
Logical XOR operation.
257
258
Args:
259
*args: Boolean expressions or values
260
261
Returns:
262
Expression: Logical XOR of arguments
263
"""
264
265
def implies(a, b):
266
"""
267
Logical implication operation.
268
269
Args:
270
a: Boolean expression (antecedent)
271
b: Boolean expression (consequent)
272
273
Returns:
274
Expression: a implies b
275
"""
276
277
def equivalent(*args):
278
"""
279
Logical equivalence operation.
280
281
Args:
282
*args: Boolean expressions
283
284
Returns:
285
Expression: All arguments are equivalent
286
"""
287
288
def Expr_if(condition, then_expr, else_expr):
289
"""
290
Conditional expression (ternary operator).
291
292
Args:
293
condition: Boolean condition
294
then_expr: Expression if condition is true
295
else_expr: Expression if condition is false
296
297
Returns:
298
Expression: Conditional expression
299
"""
300
```
301
302
### Constraint Helper Functions
303
304
Helper functions for creating complex logical constraints and cardinality constraints.
305
306
```python { .api }
307
def exactly(n, *args):
308
"""
309
Exactly n of the arguments must be true.
310
311
Args:
312
n (int): Required number of true arguments
313
*args: Boolean expressions
314
315
Returns:
316
Expression: Exactly n constraint
317
"""
318
319
def atleast(n, *args):
320
"""
321
At least n of the arguments must be true.
322
323
Args:
324
n (int): Minimum number of true arguments
325
*args: Boolean expressions
326
327
Returns:
328
Expression: At least n constraint
329
"""
330
331
def atmost(n, *args):
332
"""
333
At most n of the arguments must be true.
334
335
Args:
336
n (int): Maximum number of true arguments
337
*args: Boolean expressions
338
339
Returns:
340
Expression: At most n constraint
341
"""
342
343
def all_different(*args):
344
"""
345
All arguments must have different values.
346
347
Args:
348
*args: Variable expressions
349
350
Returns:
351
Expression: All different constraint
352
"""
353
354
def count_if(condition, *args):
355
"""
356
Count how many arguments satisfy the condition.
357
358
Args:
359
condition: Boolean condition function
360
*args: Expressions to evaluate
361
362
Returns:
363
Expression: Count expression
364
"""
365
366
def inequality(body=None, lower=None, upper=None):
367
"""
368
Create inequality expression.
369
370
Args:
371
body: Main expression
372
lower: Lower bound
373
upper: Upper bound
374
375
Returns:
376
Expression: Inequality constraint
377
"""
378
```
379
380
### Expression Analysis and Manipulation
381
382
Utilities for analyzing expression properties, extracting values, and symbolic manipulation.
383
384
```python { .api }
385
def value(expr, exception=True):
386
"""
387
Extract numeric value from expression.
388
389
Args:
390
expr: Pyomo expression or component
391
exception (bool): Raise exception if cannot evaluate
392
393
Returns:
394
float: Numeric value of expression
395
"""
396
397
def is_constant(expr):
398
"""
399
Check if expression is constant.
400
401
Args:
402
expr: Expression to check
403
404
Returns:
405
bool: True if expression is constant
406
"""
407
408
def is_fixed(expr):
409
"""
410
Check if expression has fixed value.
411
412
Args:
413
expr: Expression to check
414
415
Returns:
416
bool: True if expression is fixed
417
"""
418
419
def is_variable_type(expr):
420
"""
421
Check if expression is a variable type.
422
423
Args:
424
expr: Expression to check
425
426
Returns:
427
bool: True if expression is variable type
428
"""
429
430
def is_potentially_variable(expr):
431
"""
432
Check if expression could contain variables.
433
434
Args:
435
expr: Expression to check
436
437
Returns:
438
bool: True if potentially variable
439
"""
440
441
def polynomial_degree(expr):
442
"""
443
Determine polynomial degree of expression.
444
445
Args:
446
expr: Expression to analyze
447
448
Returns:
449
int or None: Polynomial degree, None if not polynomial
450
"""
451
452
def differentiate(expr, wrt, nth=1):
453
"""
454
Symbolic differentiation of expression.
455
456
Args:
457
expr: Expression to differentiate
458
wrt: Variable to differentiate with respect to
459
nth (int): Order of differentiation
460
461
Returns:
462
Expression: Derivative expression
463
"""
464
465
def taylor_series_expansion(expr, point, variables, order=1):
466
"""
467
Taylor series expansion of expression.
468
469
Args:
470
expr: Expression to expand
471
point: Expansion point
472
variables: Variables for expansion
473
order (int): Order of expansion
474
475
Returns:
476
Expression: Taylor series approximation
477
"""
478
```
479
480
### Aggregation Functions
481
482
Optimized functions for summation, product, and other aggregation operations over indexed expressions.
483
484
```python { .api }
485
def quicksum(args, start=0, linear=None):
486
"""
487
Efficient summation of expressions.
488
489
Args:
490
args: Iterable of expressions to sum
491
start: Initial value for the sum (default 0)
492
linear: DEPRECATED parameter for linear expressions
493
494
Returns:
495
Expression: Sum of expressions
496
"""
497
498
def prod(args):
499
"""
500
Product of expressions.
501
502
Args:
503
args: Iterable of expressions to multiply
504
505
Returns:
506
Expression: Product of expressions
507
"""
508
509
def sum_product(*args):
510
"""
511
Generalized dot product operation.
512
513
Args:
514
*args: Sequences of expressions to multiply and sum
515
516
Returns:
517
Expression: Sum of products
518
"""
519
520
def dot_product(a, b):
521
"""
522
Dot product of two sequences.
523
524
Args:
525
a: First sequence of expressions
526
b: Second sequence of expressions
527
528
Returns:
529
Expression: Dot product
530
"""
531
532
def summation(a, b):
533
"""
534
Alias for sum_product.
535
536
Args:
537
a: First sequence of expressions
538
b: Second sequence of expressions
539
540
Returns:
541
Expression: Sum of products
542
"""
543
544
def sequence(*args):
545
"""
546
Create arithmetic progression.
547
548
Args:
549
*args: Start, stop, step parameters
550
551
Returns:
552
Generator: Arithmetic sequence
553
"""
554
```
555
556
### Model Display Utilities
557
558
Utilities for displaying and debugging model structure and expression content.
559
560
```python { .api }
561
def display(model, ostream=None):
562
"""
563
Display model structure and values.
564
565
Args:
566
model: Pyomo model or component
567
ostream: Output stream (default: stdout)
568
"""
569
```
570
571
## Usage Examples
572
573
### Nonlinear Optimization
574
575
```python
576
from pyomo.environ import *
577
578
model = ConcreteModel()
579
model.x = Var(bounds=(0.1, 10))
580
model.y = Var(bounds=(0.1, 10))
581
582
# Nonlinear objective with mathematical functions
583
model.obj = Objective(
584
expr=log(model.x) + sin(model.y) + exp(model.x * model.y),
585
sense=minimize
586
)
587
588
# Nonlinear constraints
589
model.con1 = Constraint(expr=sqrt(model.x**2 + model.y**2) <= 5)
590
model.con2 = Constraint(expr=cos(model.x) + sin(model.y) >= 0.5)
591
```
592
593
### Logical Constraints
594
595
```python
596
from pyomo.environ import *
597
598
model = ConcreteModel()
599
model.x = Var([1, 2, 3], domain=Binary)
600
601
# At least 2 variables must be selected
602
model.atleast_con = Constraint(
603
expr=atleast(2, model.x[1], model.x[2], model.x[3])
604
)
605
606
# Exactly one variable must be selected
607
model.exactly_con = Constraint(
608
expr=exactly(1, model.x[1], model.x[2], model.x[3])
609
)
610
611
# Implication constraint
612
model.y = Var(domain=Binary)
613
model.implication = Constraint(
614
expr=implies(model.x[1], model.y)
615
)
616
```
617
618
### Conditional Expressions
619
620
```python
621
from pyomo.environ import *
622
623
model = ConcreteModel()
624
model.x = Var(domain=NonNegativeReals)
625
model.y = Var(domain=Binary)
626
627
# Conditional objective based on binary variable
628
model.obj = Objective(
629
expr=Expr_if(
630
model.y == 1,
631
then_expr=model.x**2, # Quadratic if y=1
632
else_expr=model.x # Linear if y=0
633
),
634
sense=minimize
635
)
636
```
637
638
### Expression Analysis
639
640
```python
641
from pyomo.environ import *
642
643
model = ConcreteModel()
644
model.x = Var(initialize=2.5)
645
model.y = Var(initialize=1.0)
646
647
expr = model.x**2 + 2*model.x*model.y + 3
648
649
# Check expression properties
650
print(f"Is constant: {is_constant(expr)}")
651
print(f"Polynomial degree: {polynomial_degree(expr)}")
652
print(f"Current value: {value(expr)}")
653
654
# Fix variables and re-evaluate
655
model.x.fix()
656
model.y.fix()
657
print(f"Value after fixing: {value(expr)}")
658
```