Skip to content

Commit aa9bcc4

Browse files
authored
Merge pull request #17 from JuliaControl/codegenfix
Faster code gen
2 parents 11e0a29 + 394025b commit aa9bcc4

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

src/SymbolicControlSystems.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ $decl
350350
code *= " y[0] += ($(sp.ccode(-n)))*y[$(i)];\n"
351351
end
352352
code *= " return y[0];\n}"
353+
code = replace(code, "" => "_")
353354
println(code)
354355
try
355356
clipboard(code)
@@ -367,10 +368,7 @@ function ccode(sys::StateSpace{<:Discrete}; cse = true, function_name = "transfe
367368
x = [Sym("x[$(i-1)]") for i = 1:nx]
368369
# @show P
369370
if ControlSystemsBase.numeric_type(sys) <: SymPy.Sym
370-
P = Sym(sys)
371-
vars = P.free_symbols
372-
vars.remove(z)
373-
vars = collect(vars)
371+
vars = reduce(vcat, [collect(M.free_symbols) for M in (sys.A, sys.B, sys.C, sys.D)]) |> unique
374372
vars = sort(vars, by = string)
375373
var_str = ""
376374
for var in vars
@@ -434,7 +432,7 @@ $decl
434432
}
435433
"""
436434
code *= "\n}"
437-
435+
code = replace(code, "" => "_")
438436
println(code)
439437
try
440438
clipboard(code)
@@ -544,14 +542,15 @@ end
544542

545543
print_c_array(a::AbstractArray, args...; kwargs...) =
546544
print_c_array(stdout, a, args...; kwargs...)
547-
function print_c_array(io, a::AbstractVector, name = "vec"; cse = false, s = "", struct_name=nothing)
545+
function print_c_array(io, a::AbstractVector, name = "vec"; cse = false, s = "", struct_name=nothing, double=true)
548546
l = length(a)
549547
a = float.(a)
550548
if cse
551549
a = write_array_cse(io, a, name, s)
552550
end
553551
if struct_name === nothing
554-
s == "" && println(io, "double $name[$l];")
552+
ty = double ? "double" : "float"
553+
s == "" && println(io, "$ty $name[$l];")
555554
struct_name = ""
556555
else
557556
struct_name = struct_name*"->"
@@ -561,14 +560,15 @@ function print_c_array(io, a::AbstractVector, name = "vec"; cse = false, s = "",
561560
end
562561
println(io)
563562
end
564-
function print_c_array(io, a::AbstractMatrix, name = "mat"; cse = false, s = "", struct_name=nothing)
563+
function print_c_array(io, a::AbstractMatrix, name = "mat"; cse = false, s = "", struct_name=nothing, double=true)
565564
r, c = size(a)
566565
a = float.(a)
567566
if cse
568567
a = write_array_cse(io, a, name, s)
569568
end
570569
if struct_name === nothing
571-
s == "" && println(io, "double $name[$r][$c];")
570+
ty = double ? "double" : "float"
571+
s == "" && println(io, "$ty $name[$r][$c];")
572572
struct_name = ""
573573
else
574574
struct_name = struct_name*"->"
@@ -579,14 +579,15 @@ function print_c_array(io, a::AbstractMatrix, name = "mat"; cse = false, s = "",
579579
println(io)
580580
end
581581

582-
function print_c_array(io, a::AbstractArray{<:Any,3}, name = "array"; cse = false, s = "", struct_name=nothing)
582+
function print_c_array(io, a::AbstractArray{<:Any,3}, name = "array"; cse = false, s = "", struct_name=nothing, double=true)
583583
r, c, d = size(a)
584584
a = float.(a)
585585
if cse
586586
a = write_array_cse(io, a, name, s)
587587
end
588588
if struct_name === nothing
589-
s == "" && println(io, "double $name[$r][$c][$d];")
589+
ty = double ? "double" : "float"
590+
s == "" && println(io, "$ty $name[$r][$c][$d];")
590591
struct_name = ""
591592
else
592593
struct_name = struct_name*"->"

0 commit comments

Comments
 (0)