@@ -48,14 +48,17 @@ Recursively converts der(x) to Symbol(:(der(x))) in expression `ex`
48
48
* `ex`: Expression or array of expressions
49
49
* `return `e`: ex with der(x) converted
50
50
"""
51
- makeDerVar (ex) = ex
52
- function makeDerVar (ex:: Expr )
51
+ makeDerVar (ex, parameters) = if typeof (ex) in [Symbol, Expr] && ex in parameters; prepend (ex, :(_p)) else ex end
52
+
53
+ function makeDerVar (ex:: Expr , parameters= [])
53
54
if ex. head == :call && ex. args[1 ] == :der
54
55
Symbol (ex)
56
+ elseif isexpr (ex, :.) && ex in parameters
57
+ prepend (ex, :(_p))
55
58
elseif ex. head == :.
56
59
Symbol (ex)
57
60
else
58
- Expr (ex. head, [makeDerVar (arg) for arg in ex. args]. .. )
61
+ Expr (ex. head, [makeDerVar (arg, parameters ) for arg in ex. args]. .. )
59
62
end
60
63
end
61
64
@@ -124,8 +127,18 @@ function findIncidence!(ex::Expr, incidence::Array{Incidence,1})
124
127
end
125
128
elseif ex. head == :.
126
129
push! (incidence, ex)
130
+ # if ex.args[2].value != :all
131
+ # push!(incidence, ex.args[1])
132
+ # end
133
+ elseif ex. head == :generator
134
+ vars = [v. args[1 ] for v in ex. args[2 : end ]]
135
+ incid = Incidence[]
136
+ [findIncidence! (e, incid) for e in ex. args]
137
+ unique! (incid)
138
+ setdiff! (incid, vars)
139
+ push! (incidence, incid... )
127
140
else
128
- # For example: =, vect, hcat, block
141
+ # For example: =, vect, hcat, block, ref
129
142
[findIncidence! (e, incidence) for e in ex. args]
130
143
end
131
144
nothing
@@ -227,13 +240,21 @@ function linearFactor(ex::Expr, x)
227
240
rest = sub (LHS[1 ], RHS[1 ])
228
241
factor = sub (LHS[2 ], RHS[2 ])
229
242
(rest, factor, LHS[3 ] && RHS[3 ])
230
- elseif isexpr (ex, :vect )
243
+ elseif isexpr (ex, :vect ) || isexpr (ex, :vcat ) || isexpr (ex, :hcat ) || isexpr (ex, :row )
231
244
arguments = ex. args[2 : end ]
232
245
factored = [linearFactor (a, x) for a in arguments]
233
246
linears = [f[3 ] for f in factored]
234
247
(ex, 0 , all (linears))
235
248
else
236
- (ex, 0 , false )
249
+ # @warn "Unknown expression type" ex
250
+ # dump(ex)
251
+ incidence = Incidence[]
252
+ findIncidence! (ex, incidence)
253
+ if x in incidence
254
+ (ex, 0 , false )
255
+ else
256
+ (ex, 0 , true )
257
+ end
237
258
end
238
259
end
239
260
0 commit comments