254
254
# # Bounds ======================================================================
255
255
struct VariableBounds end
256
256
Symbolics. option_to_metadata_type (:: Val{:bounds} ) = VariableBounds
257
- getbounds (x:: Num ) = getbounds (Symbolics. unwrap (x))
258
257
259
258
"""
260
259
getbounds(x)
@@ -266,10 +265,35 @@ Create parameters with bounds like this
266
265
@parameters p [bounds=(-1, 1)]
267
266
```
268
267
"""
269
- function getbounds (x)
268
+ function getbounds (x:: Union{Num, Symbolics.Arr, SymbolicUtils.Symbolic} )
269
+ x = unwrap (x)
270
270
p = Symbolics. getparent (x, nothing )
271
- p === nothing || (x = p)
272
- Symbolics. getmetadata (x, VariableBounds, (- Inf , Inf ))
271
+ if p === nothing
272
+ bounds = Symbolics. getmetadata (x, VariableBounds, (- Inf , Inf ))
273
+ if symbolic_type (x) == ArraySymbolic () && Symbolics. shape (x) != Symbolics. Unknown ()
274
+ bounds = map (bounds) do b
275
+ b isa AbstractArray && return b
276
+ return fill (b, size (x))
277
+ end
278
+ end
279
+ else
280
+ # if we reached here, `x` is the result of calling `getindex`
281
+ bounds = @something Symbolics. getmetadata (x, VariableBounds, nothing ) getbounds (p)
282
+ idxs = arguments (x)[2 : end ]
283
+ bounds = map (bounds) do b
284
+ if b isa AbstractArray
285
+ if Symbolics. shape (p) != Symbolics. Unknown () && size (p) != size (b)
286
+ throw (DimensionMismatch (" Expected array variable $p with shape $(size (p)) to have bounds of identical size. Found $bounds of size $(size (bounds)) ." ))
287
+ end
288
+ return b[idxs... ]
289
+ elseif symbolic_type (x) == ArraySymbolic ()
290
+ return fill (b, size (x))
291
+ else
292
+ return b
293
+ end
294
+ end
295
+ end
296
+ return bounds
273
297
end
274
298
275
299
"""
@@ -280,7 +304,7 @@ See also [`getbounds`](@ref).
280
304
"""
281
305
function hasbounds (x)
282
306
b = getbounds (x)
283
- isfinite (b[1 ]) || isfinite (b[2 ])
307
+ any ( isfinite . (b[1 ]) . || isfinite . (b[2 ]) )
284
308
end
285
309
286
310
# # Disturbance =================================================================
0 commit comments