@@ -70,6 +70,21 @@ function prependPar(ex::Expr, prefix, parameters=[], inputs=[])
7070 end
7171end
7272
73+ """
74+ e = castToValueType(ex,value)
75+
76+ Cast `ex` to `typeof(value)` if this is a numeric data datatype (`eltype(value) <: Number`).
77+ As a result, the generated code of `ex` will have the correct type instead of `Any`, so will be more efficient.
78+ """
79+ function castToValueType (ex,value)
80+ if eltype (value) <: Number
81+ valueType = typeof (value)
82+ :($ valueType ($ ex))
83+ else
84+ ex
85+ end
86+ end
87+
7388"""
7489 e = makeDerVar(ex)
7590
@@ -78,9 +93,19 @@ Recursively converts der(x) to Symbol(:(der(x))) in expression `ex`
7893* `ex`: Expression or array of expressions
7994* `return `e`: ex with der(x) converted
8095"""
81- makeDerVar (ex, parameters, inputs, evaluateParameters= false ) = if typeof (ex) in [Symbol, Expr] &&
82- ( ex in keys (parameters) || ex in keys (inputs) ); prependPar (ex, :(_p), parameters, inputs)
83- else ex end
96+ function makeDerVar (ex, parameters, inputs, evaluateParameters= false )
97+ if typeof (ex) in [Symbol, Expr]
98+ if ex in keys (parameters)
99+ castToValueType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
100+ elseif ex in keys (inputs)
101+ prependPar (ex, :(_p), parameters, inputs)
102+ else
103+ ex
104+ end
105+ else
106+ ex
107+ end
108+ end
84109
85110function makeDerVar (ex:: Expr , parameters, inputs, evaluateParameters= false )
86111 if ex. head == :call && ex. args[1 ] == :der
@@ -89,7 +114,7 @@ function makeDerVar(ex::Expr, parameters, inputs, evaluateParameters=false)
89114 if evaluateParameters
90115 parameters[ex]
91116 else
92- prependPar (ex, :(_p), parameters, inputs)
117+ castToValueType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
93118 end
94119 elseif isexpr (ex, :.) && ex in keys (inputs)
95120 if evaluateParameters
@@ -134,7 +159,12 @@ function prepend(ex::Expr, prefix)
134159 Expr (ex. head, ex. args[1 ], [prepend (arg, prefix) for arg in ex. args[2 : end ]]. .. )
135160 end
136161 elseif ex. head == :macrocall
137- eval (ex)
162+ if length (ex. args) >= 1 && ex. args[1 ] == Symbol (" @u_str" )
163+ # Do not expand units, such as u"s", because otherwise logCode=true results in wrong code, because show(u"N") is N and not u"N".
164+ ex
165+ else
166+ eval (ex)
167+ end
138168 else
139169 Expr (ex. head, [prepend (arg, prefix) for arg in ex. args]. .. )
140170 end
0 commit comments