Internals
This section describes functions that are internal to the library.
Expression Handling
MathematicalSystems._corresponding_type
— Function_corresponding_type(AT::Type{<:AbstractSystem}, fields::Tuple)
Return the system type whose field names match those in fields
.
Input
AT
– abstract system type, which can be eitherAbstractContinuousSystem
orAbstractDiscreSystem
fields
– tuple of field names
Output
The system type (either discrete or continuous, depending on AT
) whose fields names correspond to those in fields
, or an error if the fields
do not match any known system type.
Examples
julia> using MathematicalSystems: _corresponding_type
julia> _corresponding_type(AbstractContinuousSystem, ((:A),))
LinearContinuousSystem
julia> _corresponding_type(AbstractContinuousSystem, ((:A), (:B), (:X), (:U)))
ConstrainedLinearControlContinuousSystem
MathematicalSystems._capture_dim
— Function_capture_dim(expr)
Return the tuple containing the dimension(s) in expr
.
Input
expr
– symbolic expression that can be of any of the following forms::x
or:(x)
– state dimension:(x, u)
– state and input dimension:(x, u, w)
– state, input and noise dimensions
Output
- The scalar
x
ifexpr
specifies the state dimension. - The vector
[x, u]
ifexpr
specifies state and input dimension. - The vector
[x, u, w]
ifexpr
specifies state, input and noise dimensions.
MathematicalSystems.extract_dyn_equation_parameters
— Functionextract_dyn_equation_parameters(equation, state, input, noise, dim, AT)
Extract the value and field parameter from the dynamic equation equation
according to the variable state
, input
and noise
.
For the right-hand side of the dynamic equation, this function returns a vector of tuples containing some elements from the list
(:A_user, :A)
(:B_user, :B)
(:c_user, :c)
(:D_user, :D)
(:f_user, :f)
(:statedim_user :statedim)
(:inputdim_user :inputdim)
(:noisedim_user :noisedim)
and for the left-hand side, it returns either an empty vector Any[]
or [(:E_user, :E)]
where the first argument of the tuple corresponds to the value and the second argument of the tuple corresponds to the field parameter.
Input
equation
– dynamic equationstate
– state variableinput
– input variablenoise
– noise variabledim
– dimensionalityAT
– abstract system type
Output
Two arrays of tuples containing the value and field parameters for the right-hand and left-hand side of the dynamic equation equation
.
MathematicalSystems.add_asterisk
— Functionadd_asterisk(summand, state::Symbol, input::Symbol, noise::Symbol)
Checks if expression summand
contains state
, input
or noise
at its end. If so, a multiplication expression, e.g. Expr(:call, :*, :A, :x) is created. If not,
summand` is returned.
Input
summand
– expressionsstate
– state variableinput
– input variablenoise
– noise variable
Output
Multiplication expression or symbol.
Example
julia> using MathematicalSystems: add_asterisk
julia> add_asterisk(:(A1*x), :x, :u, :w)
:(A1 * x)
julia> add_asterisk(:(c1), :x, :u, :w)
:c1
julia> add_asterisk(:(Ax1), :x1, :u, :w)
:(A * x1)
julia> add_asterisk(:(Awb), :x1, :u, :wb)
:(A * wb)
julia> add_asterisk(:(A1u), :x, :u, :w)
:(A1 * u)
julia> add_asterisk(:(A1ub), :x, :u, :w)
:A1ub
MathematicalSystems._sort
— Function_sort(parameters::Vector, order::Tuple)
Filter and sort the vector parameters
according to order
.
Input
parameters
– vector of tuplesorder
– tuple of symbols
Output
A new vector of tuples corresponding to parameters
filtered and sorted according to order
.
Examples
julia> using MathematicalSystems: _sort
julia> parameters= [(:U1, :U), (:X1, :X), (:W1, :W)];
julia> _sort(parameters, (:X, :U, :W))
3-element Vector{Tuple{Any, Symbol}}:
(:X1, :X)
(:U1, :U)
(:W1, :W)
julia> parameters = [(:const, :c), (:A, :A)];
julia> _sort(parameters, (:A, :B, :c, :D))
2-element Vector{Tuple{Any, Symbol}}:
(:A, :A)
(:const, :c)
Notes
parameters
is a vector that contains tuples whose second element is considered for the sorting according to order
.
If a value of order
is not contained in parameters
, the corresponding entry of order
will be omitted.
Querying Expressions
MathematicalSystems.is_equation
— Functionis_equation(expr)
Return true
if the given expression expr
corresponds to an equation lhs = rhs
and false
otherwise. This function just detects the presence of the symbol =
.
Input
expr
– expression
Output
A Bool
indicating whether expr
is an equation or not.
MathematicalSystems.extract_sum
— Functionextract_sum(summands, state::Symbol, input::Symbol, noise::Symbol)
Extract the variable name and field name for every element of summands
which corresponds to the elements of the right-hand side of an affine system.
Input
summands
– array of expressionsstate
– state variableinput
– input variablenoise
– noise variable
Output
Array of tuples of symbols with variable name and field name.
Example
julia> using MathematicalSystems: extract_sum
julia> extract_sum([:(A1*x)], :x, :u, :w)
1-element Vector{Tuple{Any, Symbol}}:
(:(hcat(A1)), :A)
julia> extract_sum([:(A1*x), :(B1*u), :c], :x, :u, :w)
3-element Vector{Tuple{Any, Symbol}}:
(:(hcat(A1)), :A)
(:(hcat(B1)), :B)
(:(vcat(c)), :c)
julia> extract_sum([:(A1*x7), :( B1*u7), :( B2*w7)], :x7, :u7, :w7)
3-element Vector{Tuple{Any, Symbol}}:
(:(hcat(A1)), :A)
(:(hcat(B1)), :B)
(:(hcat(B2)), :D)
Notes
If an element of summands
is a multiplication expression lhs*rhs
, return lhs
as variable name and :A
as field name if rhs==state
, :B
as field name if rhs==input
and :D
as field name if rhs==noise
.
If an element of summands
is a symbol, and not equal to input
or noise
, the symbol is the variable name and the field name is :c
. If it is equal to input
, the variable name is a IdentityMultiple(I,state_dim)
where state_dim
is extracted from the state matrix (i.e. take the symbol lhs
of lhs*rhs
where rhs==state
which corresponds to the state matrix and generate the expression state_dim = size(lhs,1)
which is evaluated in the scope where @system
is called) and the field name is :B
.
Similiarily, if the element is equal to noise
, the variable name is IdentityMultiple(I, state_dim)
and the field name is :D
.
Evaluation of AbstractSystem at Given State
MathematicalSystems._instantiate
— Function_instantiate(system::AbstractSystem, x::AbstractVector;
[check_constraints]=true)
Return the result of instantiating an AbstractSystem
at the current state.
Input
system
–AbstractSystem
x
– state (it should be any vector type)check_constraints
– (optional, default:true
) check if the state belongs to the state set
Output
The result of applying the system to state x
.
Notes
The _instantiate
method generalizes the successor
of an AbstractDiscreteSystem
and the vector_field
of an AbstractContinuousSystem
into a single method.
_instantiate(system::AbstractSystem, x::AbstractVector, u::AbstractVector;
[check_constraints]=true)
Return the result of instantiating an AbstractSystem
at the current state and applying one input.
Input
system
–AbstractSystem
x
– state (it should be any vector type)u
– input (it should be any vector type) or noise, ifsystem
is not controlledcheck_constraints
– (optional, default:true
) check if the state belongs to the state set
Output
The result of applying the system to state x
and input u
.
Notes
If the system is not controlled but noisy, the input u
is interpreted as noise.
The _instantiate
method generalizes the successor
of an AbstractDiscreteSystem
and the vector_field
of an AbstractContinuousSystem
into a single method.
_instantiate(system::AbstractSystem,
x::AbstractVector, u::AbstractVector, w::AbstractVector; [check_constraints]=true)
Return the result of instantiating an AbstractSystem
at the current state and applying two inputs to an AbstractSystem
.
Input
system
–AbstractSystem
x
– state (it should be any vector type)u
– input (it should be any vector type)w
– noise (it should be any vector type)check_constraints
– (optional, default:true
) check if the state belongs to the state set
Output
The result of applying the system to state x
, input u
and noise w
.
Notes
The _instantiate
method generalizes the successor
of an AbstractDiscreteSystem
and the vector_field
of an AbstractContinuousSystem
into a single method.
Naming Convention for Systems' Fields
Systems' fields should be accessed externally by their respective getter functions. Internally to the library, the following naming conventions are used.
Field | Description | Getter function |
---|---|---|
A | state matrix | state_matrix |
B | input matrix | input_matrix |
c | affine term | affine_term |
D | noise matrix | noise_matrix |
X | state constraints | stateset |
U | input constraints | inputset |
W | disturbance set | noiseset |