Private API

This page lists non-exported (internal) symbols of CTParser.


From CTParser

__symgen

CTParser.__symgenFunction
__symgen(s...) -> Any

Generate a fresh symbol by concatenating the given components and a gensym() suffix.

This is used throughout the parser to create unique internal names that do not collide with user-defined identifiers.

as_range

CTParser.as_rangeFunction
as_range(x) -> Any

Return x itself if it is a range, or a one-element array [x].

This is a normalisation helper used when interpreting constraint indices.

concat

CTParser.concatFunction
concat(e1, e2) -> Expr

Concatenate two expressions without creating extra blocks (as Expr(:block, e1, e2) would do).

Example

julia> e1 = :(x = 1; y = 2)
quote
    x = 1
    #= REPL[3]:1 =#
    y = 2
end

julia> e2 = :(z = 3)
:(z = 3)

julia> concat(e1, e2)
quote
    x = 1
    #= REPL[3]:1 =#
    y = 2
    z = 3
end

julia> concat(e1, e1)
quote
    x = 1
    #= REPL[3]:1 =#
    y = 2
    x = 1
    #= REPL[3]:1 =#
    y = 2
end

constraint_type

CTParser.constraint_typeFunction
constraint_type(
    e,
    t,
    t0,
    tf,
    x,
    u,
    v
) -> Union{Symbol, Tuple{Symbol, Any}}

Return the type constraint among :initial, :final, :boundary, :control_range, :control_fun, :state_range, :state_fun, :mixed, :variable_range, :variable_fun (:other otherwise), together with the appropriate value (range, updated expression...) Expressions like u(t0) where u is the control and t0 the initial time return :other.

Example

julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u; v = :v

julia> constraint_type(:( ẏ(t) ), t, t0, tf, x, u, v)
:other

julia> constraint_type(:( ẋ(s) ), t, t0, tf, x, u, v)
:other

julia> constraint_type(:( x(0)' ), t, t0, tf, x, u, v)
:boundary

julia> constraint_type(:( x(t)' ), t, t0, tf, x, u, v)
:state_fun

julia> constraint_type(:( x(0) ), t, t0, tf, x, u, v)
(:initial, nothing)

julia> constraint_type(:( x[1:2:5](0) ), t, t0, tf, x, u, v)
(:initial, 1:2:5)

julia> constraint_type(:( x[1:2](0) ), t, t0, tf, x, u, v)
(:initial, 1:2)

julia> constraint_type(:( x[1](0) ), t, t0, tf, x, u, v)
(:initial, 1)

julia> constraint_type(:( 2x[1](0)^2 ), t, t0, tf, x, u, v)
:boundary

julia> constraint_type(:( x(tf) ), t, t0, tf, x, u, v)
(:final, nothing)
j
julia> constraint_type(:( x[1:2:5](tf) ), t, t0, tf, x, u, v)
(:final, 1:2:5)

julia> constraint_type(:( x[1:2](tf) ), t, t0, tf, x, u, v)
(:final, 1:2)

julia> constraint_type(:( x[1](tf) ), t, t0, tf, x, u, v)
(:final, 1)

julia> constraint_type(:( 2x[1](tf)^2 ), t, t0, tf, x, u, v)
:boundary

julia> constraint_type(:( x[1](tf) - x[2](0) ), t, t0, tf, x, u, v)
:boundary

julia> constraint_type(:( u[1:2:5](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2:5)

julia> constraint_type(:( u[1:2](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2)

julia> constraint_type(:( u[1](t) ), t, t0, tf, x, u, v)
(:control_range, 1)

julia> constraint_type(:( u(t) ), t, t0, tf, x, u, v)
(:control_range, nothing)

julia> constraint_type(:( 2u[1](t)^2 ), t, t0, tf, x, u, v)
:control_fun

julia> constraint_type(:( x[1:2:5](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2:5)

julia> constraint_type(:( x[1:2](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2)

julia> constraint_type(:( x[1](t) ), t, t0, tf, x, u, v)
(:state_range, 1)

julia> constraint_type(:( x(t) ), t, t0, tf, x, u, v)
(:state_range, nothing)

julia> constraint_type(:( 2x[1](t)^2 ), t, t0, tf, x, u, v)
:state_fun

julia> constraint_type(:( 2u[1](t)^2 * x(t) ), t, t0, tf, x, u, v)
:mixed

julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other

julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other

julia> constraint_type(:( 2u[1](t)^2 * x(t) + v ), t, t0, tf, x, u, v)
:mixed

julia> constraint_type(:( v[1:2:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:2:9)

julia> constraint_type(:( v[1:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:10)

julia> constraint_type(:( v[2] ), t, t0, tf, x, u, v)
(:variable_range, 2)

julia> constraint_type(:( v ), t, t0, tf, x, u, v)
(:variable_range, nothing)

julia> constraint_type(:( v^2  + 1 ), t, t0, tf, x, u, v)
:variable_fun
julia> constraint_type(:( v[2]^2 + 1 ), t, t0, tf, x, u, v)
:variable_fun

expr_it

CTParser.expr_itFunction
expr_it(e, _Expr, f) -> Any

Expr iterator: apply _Expr to nodes and f to leaves of the AST.

Example

julia> id(e) = expr_it(e, Expr, x -> x)

has

CTParser.hasFunction
has(e, e1) -> Union{Missing, Bool}

Return true if e contains e1.

Example

julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)

julia> has(e, 2)
true

julia> has(e, :x)
true

julia> has(e, :min)
true

julia> has(e, :( x[1](t)^2 ))
true

julia> !has(e, :( x[1](t)^3 ))
true

julia> !has(e, 3)
true

julia> !has(e, :max)
true

julia> has(:x, :x)
true

julia> !has(:x, 2)
true

julia> !has(:x, :y)
true
has(e, x, t) -> Union{Missing, Bool}

Return true if e contains a (...x...)(t) call.

Example

julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)

julia> has(e, :x, :t)
true

julia> has(e, :u, :t)
true

is_range

CTParser.is_rangeFunction
is_range(x) -> Union{Missing, Bool}

Return true if x represents a range.

This predicate is specialised for AbstractRange values and for expressions of the form i:j or i:p:j.

replace_call

CTParser.replace_callFunction
replace_call(e, x::Symbol, t, y) -> Any

Replace calls in e of the form (...x...)(t) by (...y...).

Example


julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;

julia> e = :( x[1](0) * 2x(tf) - x[2](tf) * 2x(0) )
:((x[1])(0) * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))

julia> x0 = Symbol(x, 0); e = replace_call(e, x, t0, x0)
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2x0))

julia> xf = Symbol(x, "f"); replace_call(ans, x, tf, xf)
:(x0[1] * (2xf) - xf[2] * (2x0))

julia> e = :( A*x(t) + B*u(t) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(A * x + B * u)

julia> e = :( F0(x(t)) + u(t)*F1(x(t)) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(F0(x) + u * F1(x))

julia> e = :( 0.5u(t)^2 ); replace_call(e, u, t, u)
:(0.5 * u ^ 2)
replace_call(e, x::Vector{Symbol}, t, y) -> Any

Replace calls in e of the form (...x1...x2...)(t) by (...y1...y2...) for all symbols x1, x2... in the vector x.

Example


julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;

julia> e = :( (x^2 + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])

julia> e = :( ((x^2)(t) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])

julia> e = :( ((x^2)(t0) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:((xx ^ 2)(t0) + uu[1])

subs

CTParser.subsFunction
subs(e, e1::Union{Real, Symbol}, e2) -> Any

Substitute expression e1 by expression e2 in expression e.

Examples

julia> e = :( ∫( r(t)^2 + 2u₁(t)) → min )
:(∫(r(t) ^ 2 + 2 * u₁(t)) → min)

julia> subs(e, :r, :( x[1] ))
:(∫((x[1])(t) ^ 2 + 2 * u₁(t)) → min)

julia> e = :( ∫( u₁(t)^2 + 2u₂(t)) → min )
:(∫(u₁(t) ^ 2 + 2 * u₂(t)) → min)

julia> for i ∈ 1:2
       e = subs(e, Symbol(:u, Char(8320+i)), :( u[$i] ))
       end; e
:(∫((u[1])(t) ^ 2 + 2 * (u[2])(t)) → min)

julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;

julia> e = :( x[1](0) * 2x(tf) - x[2](tf) * 2x(0) )
:((x[1])(0) * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))

julia> x0 = Symbol(x, 0); subs(e, :( $x[1]($(t0)) ), :( $x0[1] ))
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))

subs2

CTParser.subs2Function
subs2(e, x, y, j; k) -> Any

Substitute occurrences of symbol x in expression e with indexed access to y at time index j. Handles two patterns:

  • x[i] (scalar index) → y[i, j]
  • x[1:3] (range index) → [y[k, j] for k ∈ 1:3]

See also: subs2m.

Examples

julia> # Scalar indexing
julia> e = :(x0[1] * 2xf[3] - cos(xf[2]) * 2x0[2])
julia> subs2(subs2(e, :x0, :x, 0), :xf, :x, :N)
:(x[1, 0] * (2 * x[3, N]) - cos(x[2, N]) * (2 * x[2, 0]))

julia> # Range indexing
julia> e = :(x0[1:3])
julia> subs2(e, :x0, :x, 0; k = :k)
:([x[k, 0] for k ∈ 1:3])

julia> # Bare symbols are not substituted
julia> e = :(x0 * 2xf[3])
julia> subs2(subs2(e, :x0, :x, 0), :xf, :x, :N)
:(x0 * (2 * x[3, N]))

subs2m

CTParser.subs2mFunction
subs2m(e, x, y, j; k) -> Any

Substitute x[i] or x[rg] in e for the midpoint scheme:

  • x[i] → (y[i, j] + y[i, j + 1]) / 2 (scalar indexing)
  • x[rg] → [(y[k, j] + y[k, j + 1]) / 2 for k ∈ rg] (range indexing)

Bare symbols like x (without indexing) are NOT substituted.

See also: subs2.

Examples

julia> e = :(x0[1] * 2xf[3] - cos(xf[2]) * 2x0[2])
:(x0[1] * (2 * xf[3]) - cos(xf[2]) * (2 * x0[2]))

julia> subs2m(subs2m(e, :x0, :x, 0), :xf, :x, :N)
:(((x[1, 0] + x[1, 0 + 1]) / 2) * (2 * ((x[3, N] + x[3, N + 1]) / 2)) - cos((x[2, N] + x[2, N + 1]) / 2) * (2 * ((x[2, 0] + x[2, 0 + 1]) / 2)))

julia> e = :(x0 * 2xf[3] - cos(xf) * 2x0[2])
:(x0 * (2 * xf[3]) - cos(xf) * (2 * x0[2]))

julia> subs2m(subs2m(e, :x0, :x, 0), :xf, :x, :N)
:(x0 * (2 * ((x[3, N] + x[3, N + 1]) / 2)) - cos(xf) * (2 * ((x[2, 0] + x[2, 0 + 1]) / 2)))

julia> e = :(x0[1:3])
:(x0[1:3])

julia> subs2m(e, :x0, :x, 0)
:([((x[k, 0] + x[k, 0 + 1]) / 2) for k ∈ 1:3])

subs3

CTParser.subs3Function
subs3(e, x, y, i, j) -> Any

Substitute x[rg] by y[i, j], whatever rg, in e. (Note: rg is then expected to be used to loop on i.)

Examples

julia> e = :(x0[1:2:d] * 2xf[1:3])
:(x0[1:2:d] * (2 * xf[1:3]))

julia> subs3(e, :x0, :x, :i, 0)
:(x[i, 0] * (2 * xf[1:3]))

julia> subs3(e, :xf, :x, 1, :N)
:(x0[1:2:d] * (2 * x[1, N]))