def include_equations(self, *args, **kwargs): if callable(getattr(super(), 'include_equations', None)): super().include_equations(*args, **kwargs) ode = kwargs.pop('ode', None) x = kwargs.pop('x', None) if ode is None and x is not None: raise ValueError("`ode` is None but `x` is not None") # if is in the list form if isinstance(ode, collections.abc.Sequence): ode = vertcat(*ode) if isinstance(x, collections.abc.Sequence): x = vertcat(*x) # if ode was passed but not x, try to guess the x if x is None and ode is not None: # Check if None are all sequential, ortherwise we don't know who it belongs first_none = list(self._ode.values()).index(None) if not all(eq is None for eq in islice(self._ode.values(), 0, first_none)): raise ValueError( "ODE should be inserted on the equation form or in the list form." "You can't mix both without explicit passing the states associated with the equation." ) x = vertcat(*list(self._ode.keys())[first_none:first_none + ode.numel()]) if len(args) > 0 and ode is None: x = SX([]) ode = SX([]) # get ode and x from equality equations for eq in args: if isinstance(eq, EqualityEquation): if isinstance(eq.lhs, Derivative): ode = vertcat(ode, eq.rhs) x = vertcat(x, eq.lhs.inner) # actually include the equations if ode is not None and ode.numel() > 0: for x_i in vertcat(x).nz: if self._ode[x_i] is not None: raise Warning( f'State "{x_i}" already had an ODE associated, overriding it!' ) ode_dict = dict(self._ode) ode_dict.update({x_i: ode[ind] for ind, x_i in enumerate(x.nz)}) self._ode = ode_dict
def __init__(self, **kwargs): super().__init__(**kwargs) self.u = SX([]) self._parametrized_controls = [] self.u_par = vertcat(self.u) self.u_expr = vertcat(self.u)
def seq_to_SX_matrix(seq): """ In many cases this is equivalent to cs.vertcat. """ n = len(seq) # leading element: e0 = SX(seq[0]) if e0.shape == (1, 1): # we have a sequence of scalars and create a column vector res = SX(n, 1) for i, elt in enumerate(seq): res[i, 0] = elt return res else: # we assume we have a sequence of vectors and want to concatenate them (colstack) n1, n2 = e0.shape res = SX(n1, n2 * n) for i, elt in enumerate(seq): res[:, i] = elt return res
def include_equations(self, *args, **kwargs): if callable(getattr(super(), 'include_equations', None)): super().include_equations(*args, **kwargs) alg = kwargs.pop('alg', None) if len(args) > 0 and alg is None: alg = SX([]) # in case a list of equations `y == x + u` has been passed for eq in args: if is_equality(eq): alg = vertcat(alg, eq.dep(0) - eq.dep(1)) if isinstance(alg, collections.abc.Sequence): alg = vertcat(*alg) if alg is not None: self.alg = vertcat(self.alg, alg)
def __init__(self, **kwargs): super().__init__(**kwargs) self.p = SX([]) self.theta = SX([])
def __init__(self, **kwargs): super().__init__(**kwargs) self.x = SX([]) self.x_0 = SX([]) self._ode = dict()
def __init__(self, **kwargs): super().__init__(**kwargs) self.alg = SX([]) self.y = SX([])