|
15 | 15 | from .pyOpt_constraint import Constraint |
16 | 16 | from .pyOpt_error import Error |
17 | 17 | from .pyOpt_objective import Objective |
18 | | -from .pyOpt_utils import ICOL, IDATA, INFINITY, IROW, convertToCOO, convertToCSR, mapToCSR, scaleColumns, scaleRows |
| 18 | +from .pyOpt_utils import ( |
| 19 | + ICOL, |
| 20 | + IDATA, |
| 21 | + INFINITY, |
| 22 | + IROW, |
| 23 | + _broadcast_to_array, |
| 24 | + convertToCOO, |
| 25 | + convertToCSR, |
| 26 | + mapToCSR, |
| 27 | + scaleColumns, |
| 28 | + scaleRows, |
| 29 | +) |
19 | 30 | from .pyOpt_variable import Variable |
20 | 31 | from .types import Dict1DType, Dict2DType, NumpyType |
21 | 32 |
|
@@ -229,71 +240,11 @@ def addVarGroup( |
229 | 240 | if varType not in ["c", "i", "d"]: |
230 | 241 | raise Error("Type must be one of 'c' for continuous, 'i' for integer or 'd' for discrete.") |
231 | 242 |
|
232 | | - # ------ Process the value argument |
233 | | - value = np.atleast_1d(value).real |
234 | | - if len(value) == 1: |
235 | | - value = value[0] * np.ones(nVars) |
236 | | - elif len(value) == nVars: |
237 | | - pass |
238 | | - else: |
239 | | - raise Error( |
240 | | - f"The length of the 'value' argument to addVarGroup is {len(value)}, " |
241 | | - + f"but the number of variables in nVars is {nVars}." |
242 | | - ) |
243 | | - |
244 | | - if lower is None: |
245 | | - lower = [None for i in range(nVars)] |
246 | | - elif np.isscalar(lower): |
247 | | - lower = lower * np.ones(nVars) |
248 | | - elif len(lower) == nVars: |
249 | | - lower = np.atleast_1d(lower).real |
250 | | - else: |
251 | | - raise Error( |
252 | | - "The 'lower' argument to addVarGroup is invalid. " |
253 | | - + f"It must be None, a scalar, or a list/array or length nVars={nVars}." |
254 | | - ) |
255 | | - |
256 | | - if upper is None: |
257 | | - upper = [None for i in range(nVars)] |
258 | | - elif np.isscalar(upper): |
259 | | - upper = upper * np.ones(nVars) |
260 | | - elif len(upper) == nVars: |
261 | | - upper = np.atleast_1d(upper).real |
262 | | - else: |
263 | | - raise Error( |
264 | | - "The 'upper' argument to addVarGroup is invalid. " |
265 | | - + f"It must be None, a scalar, or a list/array or length nVars={nVars}." |
266 | | - ) |
267 | | - |
268 | | - # ------ Process the scale argument |
269 | | - if scale is None: |
270 | | - scale = np.ones(nVars) |
271 | | - else: |
272 | | - scale = np.atleast_1d(scale) |
273 | | - if len(scale) == 1: |
274 | | - scale = scale[0] * np.ones(nVars) |
275 | | - elif len(scale) == nVars: |
276 | | - pass |
277 | | - else: |
278 | | - raise Error( |
279 | | - f"The length of the 'scale' argument to addVarGroup is {len(scale)}, " |
280 | | - + f"but the number of variables in nVars is {nVars}." |
281 | | - ) |
282 | | - |
283 | | - # ------ Process the offset argument |
284 | | - if offset is None: |
285 | | - offset = np.ones(nVars) |
286 | | - else: |
287 | | - offset = np.atleast_1d(offset) |
288 | | - if len(offset) == 1: |
289 | | - offset = offset[0] * np.ones(nVars) |
290 | | - elif len(offset) == nVars: |
291 | | - pass |
292 | | - else: |
293 | | - raise Error( |
294 | | - f"The length of the 'offset' argument to addVarGroup is {len(offset)}, " |
295 | | - + f"but the number of variables is {nVars}." |
296 | | - ) |
| 243 | + value = _broadcast_to_array("value", value, nVars) |
| 244 | + lower = _broadcast_to_array("lower", lower, nVars, allow_none=True) |
| 245 | + upper = _broadcast_to_array("upper", upper, nVars, allow_none=True) |
| 246 | + scale = _broadcast_to_array("scale", scale, nVars) |
| 247 | + offset = _broadcast_to_array("offset", offset, nVars) |
297 | 248 |
|
298 | 249 | # Determine if scalar i.e. it was called from addVar(): |
299 | 250 | scalar = kwargs.pop("scalar", False) |
|
0 commit comments