logic
Logical and control flow operations for symbolic expressions.
This module provides logical and control flow operations used in optimization problems, enabling conditional logic in dynamics and constraints. These operations are JAX-only and not supported in CVXPy lowering.
All
¶
Bases: Expr
Logical AND reduction over predicates. Wraps jnp.all.
Reduces one or more Inequality predicates to a single scalar boolean using AND semantics. This is useful for:
- Combining multiple scalar predicates:
All([x >= 0, x <= 10]) - Reducing a vector predicate:
All(position >= lower_bound)
After evaluation, returns True only if ALL predicates are satisfied.
Attributes:
| Name | Type | Description |
|---|---|---|
predicates |
List of Inequality constraints to combine with AND. |
Example
Combining scalar predicates::
in_range = ox.All([x >= 0.0, x <= 10.0])
ox.Cond(in_range, 1.0, 0.0)
Reducing a vector predicate::
all_positive = ox.All(position >= 0.0) # position is shape (3,)
ox.Cond(all_positive, safe_value, unsafe_value)
Note
This operation is only supported for JAX lowering. CVXPy lowering will raise NotImplementedError since logical reductions are not DCP-compliant.
Source code in openscvx/symbolic/expr/logic.py
__init__(predicates: Union[Inequality, List[Inequality]])
¶
Initialize an All expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicates
|
Union[Inequality, List[Inequality]]
|
Single Inequality or list of Inequalities to combine. For a single vector Inequality, reduces across all elements. For a list, combines all predicates with AND. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If predicates is not an Inequality or list of Inequalities |
ValueError
|
If predicates list is empty |
Source code in openscvx/symbolic/expr/logic.py
canonicalize() -> Expr
¶
check_shape() -> Tuple[int, ...]
¶
Check shape and return scalar output shape.
All always reduces to a scalar boolean.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
Empty tuple () representing scalar output |
Source code in openscvx/symbolic/expr/logic.py
Any
¶
Bases: Expr
Logical OR reduction over predicates. Wraps jnp.any.
Reduces one or more Inequality predicates to a single scalar boolean using OR semantics. This is useful for:
- Combining multiple scalar predicates:
Any([in_region_a, in_region_b]) - Reducing a vector predicate:
Any(position >= threshold)
After evaluation, returns True if ANY predicate is satisfied.
Attributes:
| Name | Type | Description |
|---|---|---|
predicates |
List of Inequality constraints to combine with OR. |
Example
Combining scalar predicates (OR logic)::
in_any_region = ox.Any([in_region_a, in_region_b])
ox.Cond(in_any_region, region_value, default_value)
Reducing a vector predicate::
any_above = ox.Any(position >= threshold) # position is shape (3,)
ox.Cond(any_above, triggered_value, normal_value)
Note
This operation is only supported for JAX lowering. CVXPy lowering will raise NotImplementedError since logical reductions are not DCP-compliant.
Source code in openscvx/symbolic/expr/logic.py
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 | |
__init__(predicates: Union[Inequality, List[Inequality]])
¶
Initialize an Any expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicates
|
Union[Inequality, List[Inequality]]
|
Single Inequality or list of Inequalities to combine. For a single vector Inequality, reduces across all elements. For a list, combines all predicates with OR. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If predicates is not an Inequality or list of Inequalities |
ValueError
|
If predicates list is empty |
Source code in openscvx/symbolic/expr/logic.py
canonicalize() -> Expr
¶
check_shape() -> Tuple[int, ...]
¶
Check shape and return scalar output shape.
Any always reduces to a scalar boolean.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
Empty tuple () representing scalar output |
Source code in openscvx/symbolic/expr/logic.py
Cond
¶
Bases: Expr
Conditional expression for JAX-traceable branching.
Implements a conditional expression that selects between two branches based
on a predicate. This wraps jax.lax.cond to enable conditional logic in
symbolic expressions for dynamics and constraints.
The predicate can be:
- A single Inequality constraint (created with <= or >=)
- A list of Inequality constraints (AND semantics, shorthand for All([...]))
- An All expression for explicit AND semantics
- An Any expression for OR semantics
- None for purely node-based switching (requires node_ranges)
After canonicalization, each constraint is in the form lhs <= 0, so the
predicate evaluates to True when the constraint is satisfied (lhs <= 0) and
False when violated (lhs > 0).
The true and false branches must have broadcastable shapes (following JAX/NumPy broadcasting rules).
Optionally, the conditional can be restricted to specific node ranges using
the node_ranges parameter. Outside these ranges, the false branch is
always evaluated.
Attributes:
| Name | Type | Description |
|---|---|---|
predicate |
The predicate expression (All, Any, or single Inequality). |
|
true_branch |
Expression to evaluate when predicate is True |
|
false_branch |
Expression to evaluate when predicate is False |
|
node_ranges |
Optional list of (start, end) tuples specifying node ranges where the conditional is active. None means active at all nodes. |
Example
Conditional velocity limit based on distance::
distance = ox.Norm(position - obstacle)
expr = ox.Cond(
distance <= safety_threshold, # predicate: True when close
5.0, # true branch: slow speed
10.0 # false branch: fast speed
)
Multiple predicates with AND semantics (explicit)::
expr = ox.Cond(
ox.All([x >= 0.0, x <= 10.0]), # True when x in [0, 10]
1.0, # in range
0.0 # out of range
)
Multiple predicates with OR semantics::
expr = ox.Cond(
ox.Any([in_region_a, in_region_b]), # True if in either region
region_value,
default_value
)
Reduce vector predicate::
expr = ox.Cond(
ox.All(position >= lower_bound), # True if all elements satisfy
safe_value,
unsafe_value
)
Conditional active only during specific trajectory phases::
expr = ox.Cond(
distance <= safety_threshold,
5.0,
10.0,
node_ranges=[(0, 2), (5, 7)] # active at nodes 0-1 and 5-6
)
Purely node-based switching (no predicate)::
expr = ox.Cond(
None, # no predicate
boost_thrust, # true branch at specified nodes
coast_thrust, # false branch elsewhere
node_ranges=[(0, 10), (20, 30)] # boost at nodes 0-9 and 20-29
)
Note
This operation is only supported for JAX lowering. CVXPy lowering will raise NotImplementedError since conditional logic is not DCP-compliant.
Source code in openscvx/symbolic/expr/logic.py
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 | |
__init__(pred: Union[Inequality, List[Inequality], All, Any, None], true_branch: Union[Expr, float, int, np.ndarray], false_branch: Union[Expr, float, int, np.ndarray], node_ranges: Optional[List[Tuple[int, int]]] = None)
¶
Initialize a conditional expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Union[Inequality, List[Inequality], All, Any, None]
|
Predicate for the conditional. Can be: - Single Inequality (e.g., x <= 5) - List of Inequalities (AND semantics, shorthand for All([...])) - All expression for explicit AND - Any expression for OR semantics - None for purely node-based switching (requires node_ranges) |
required |
true_branch
|
Union[Expr, float, int, ndarray]
|
Expression to evaluate when predicate is True |
required |
false_branch
|
Union[Expr, float, int, ndarray]
|
Expression to evaluate when predicate is False |
required |
node_ranges
|
Optional[List[Tuple[int, int]]]
|
Optional list of (start, end) tuples specifying node ranges where the conditional is active. Each tuple defines a half-open interval [start, end) of node indices. Outside these ranges, the false branch is always evaluated. None means active at all nodes. Required when pred is None. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If pred is not a valid predicate type |
ValueError
|
If node_ranges contains invalid ranges or pred=None without node_ranges |
Source code in openscvx/symbolic/expr/logic.py
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 | |
canonicalize() -> Expr
¶
Canonicalize by canonicalizing all children, preserving node_ranges.
Source code in openscvx/symbolic/expr/logic.py
check_shape() -> Tuple[int, ...]
¶
Check and return the output shape of the conditional.
The predicate must be scalar (or reduce to scalar via All/Any), and the true and false branches must have broadcastable shapes. The output shape is the broadcasted shape of the two branches.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
The broadcasted shape of true_branch and false_branch |
Raises:
| Type | Description |
|---|---|
ValueError
|
If predicate is not scalar or branches have incompatible shapes |
Source code in openscvx/symbolic/expr/logic.py
children()
¶
Return the child expressions: predicate (if any), true branch, and false branch.