affine
Static affine-in-state/control checker for symbolic expressions.
Provides :func:is_affine_in_state_control and :func:is_constant, which
are used by :mod:openscvx.symbolic.canonicalize to verify that user
.convex() constraints can be lowered to a :class:~openscvx.solvers.cones.ConeConstraint
without requiring a non-linear solver.
Both functions walk the expression AST without executing any JAX code, so they are safe to call before JIT compilation.
is_affine_in_state_control(expr: 'Expr') -> bool
¶
Return True iff expr is affine in
:class:~openscvx.symbolic.expr.State and
:class:~openscvx.symbolic.expr.Control variables.
Recognises:
- Leaf nodes: :class:
Constant, :class:Parameter→ constant (affine). - :class:
State, :class:Control→ linear. - :class:
Add, :class:Sub→ affine iff all operands are affine. - :class:
Neg→ affine iff operand is affine. - :class:
Mul→ affine iff at most one factor is non-constant (and the non-constant factor, if present, is itself affine). - :class:
MatMul→ affine iff one side is constant and the other is affine. - :class:
Div→ affine iff numerator is affine and denominator is constant. - :class:
Transpose, :class:Sum, :class:Diag→ affine iff operand is affine. - :class:
Index, :class:Concat, :class:Stack, :class:Hstack, :class:Vstack, :class:Block→ affine iff all children are affine. - Anything else (norms, trig, powers, …) → not affine.
Source code in openscvx/symbolic/affine.py
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 | |
is_constant(expr: 'Expr') -> bool
¶
Return True iff expr has no :class:~openscvx.symbolic.expr.State
or :class:~openscvx.symbolic.expr.Control leaves.
Parameters that are constant across a trajectory are treated as constant;
:class:~openscvx.symbolic.expr.Variable objects are treated as
non-constant (conservative assumption).