-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
array.py
445 lines (344 loc) · 13.4 KB
/
array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import numpy as _onp
import casadi as _cas
from typing import List, Dict, Union, Sequence
from aerosandbox.numpy.determine_type import is_casadi_type
def array(array_like, dtype=None):
"""
Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html
"""
if is_casadi_type(
array_like, recursive=False
): # If you were literally given a CasADi array, just return it
# Handles inputs like cas.DM([1, 2, 3])
return array_like
elif not is_casadi_type(array_like, recursive=True) or dtype is not None:
# If you were given a list of iterables that don't have CasADi types:
# Handles inputs like [[1, 2, 3], [4, 5, 6]]
return _onp.array(array_like, dtype=dtype)
else:
# Handles inputs like [[opti_var_1, opti_var_2], [opti_var_3, opti_var_4]]
def make_row(contents: List):
try:
return _cas.horzcat(*contents)
except (TypeError, Exception):
return contents
return _cas.vertcat(*[make_row(row) for row in array_like])
def concatenate(arrays: Sequence, axis: int = 0):
"""
Join a sequence of arrays along an existing axis. Returns a NumPy array if possible; if not, returns a CasADi array.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
"""
if not is_casadi_type(arrays, recursive=True):
return _onp.concatenate(arrays, axis=axis)
else:
if axis == 0:
return _cas.vertcat(*arrays)
elif axis == 1:
return _cas.horzcat(*arrays)
else:
raise ValueError(
"CasADi-backend arrays can only be 1D or 2D, so `axis` must be 0 or 1."
)
def stack(arrays: Sequence, axis: int = 0):
"""
Join a sequence of arrays along a new axis. Returns a NumPy array if possible; if not, returns a CasADi array.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.stack.html
"""
if not is_casadi_type(arrays, recursive=True):
return _onp.stack(arrays, axis=axis)
else:
### Validate stackability
for array in arrays:
if is_casadi_type(array, recursive=False):
if not array.shape[1] == 1:
raise ValueError("Can only stack Nx1 CasADi arrays!")
else:
if not len(array.shape) == 1:
raise ValueError(
"Can only stack 1D NumPy ndarrays alongside CasADi arrays!"
)
if axis == 0 or axis == -2:
return _cas.transpose(_cas.horzcat(*arrays))
elif axis == 1 or axis == -1:
return _cas.horzcat(*arrays)
else:
raise ValueError(
"CasADi-backend arrays can only be 1D or 2D, so `axis` must be 0 or 1."
)
def hstack(arrays):
if not is_casadi_type(arrays, recursive=True):
return _onp.hstack(arrays)
else:
raise ValueError(
"Use `np.stack()` or `np.concatenate()` instead of `np.hstack()` when dealing with mixed-backend arrays."
)
def vstack(arrays):
if not is_casadi_type(arrays, recursive=True):
return _onp.vstack(arrays)
else:
raise ValueError(
"Use `np.stack()` or `np.concatenate()` instead of `np.vstack()` when dealing with mixed-backend arrays."
)
def dstack(arrays):
if not is_casadi_type(arrays, recursive=True):
return _onp.dstack(arrays)
else:
raise ValueError(
"Use `np.stack()` or `np.concatenate()` instead of `np.dstack()` when dealing with mixed-backend arrays."
)
def length(array) -> int:
"""
Returns the length of an 1D-array-like object. An extension of len() with slightly different functionality.
Args:
array:
Returns:
"""
if not is_casadi_type(array, recursive=False):
try:
return len(array)
except TypeError:
return 1
else:
if array.shape[0] != 1:
return array.shape[0]
else:
return array.shape[1]
def diag(v, k=0):
"""
Extract a diagonal or construct a diagonal array.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.diag.html
"""
if not is_casadi_type(v, recursive=False):
return _onp.diag(v, k=k)
else:
if 1 in v.shape: # If v is a 1D array, construct a diagonal matrix
if v.shape[0] == 1:
v = v.T
if k == 0:
return _cas.diag(v)
else:
n = v.shape[0]
res = type(v).zeros(n + abs(k), n + abs(k))
for i in range(n):
if k >= 0:
res[i, i + k] = v[i]
else:
res[i - k, i] = v[i]
return res
elif v.shape[0] == v.shape[1]: # If v is a square matrix, extract the diagonal
n = v.shape[0]
if k >= 0:
return array([v[i, i + k] for i in range(n - k)])
else:
return array([v[i - k, i] for i in range(n + k)])
else:
raise NotImplementedError(
"Haven't yet added logic for non-square matrices."
)
def roll(a, shift, axis: int = None):
"""
Roll array elements along a given axis.
Elements that roll beyond the last position are re-introduced at the first.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.roll.html
Parameters
----------
a : array_like
Input array.
shift : int or tuple of ints
The number of places by which elements are shifted. If a tuple, then axis must be a tuple of the same size,
and each of the given axes is shifted by the corresponding number. If an int while axis is a tuple of ints,
then the same value is used for all given axes.
axis : int or tuple of ints, optional
Axis or axes along which elements are shifted. By default, the array is flattened before shifting,
after which the original shape is restored.
Returns
-------
res : ndarray
Output array, with the same shape as a.
"""
if not is_casadi_type(a, recursive=False):
return _onp.roll(a, shift, axis=axis)
else:
if axis is None:
a_flat = reshape(a, -1)
result = roll(a_flat, shift, axis=0)
return reshape(result, a.shape)
elif isinstance(axis, int):
shift = shift % a.shape[axis] # shift can be negative
if shift != 0:
slice1 = [slice(None)] * 2
slice1[axis] = slice(-shift, None)
slice2 = [slice(None)] * 2
slice2[axis] = slice(-shift)
result = concatenate([a[tuple(slice1)], a[tuple(slice2)]], axis=axis)
else:
result = a
return result
elif isinstance(axis, tuple):
result = a
if not isinstance(shift, tuple):
shift = (shift,) * len(axis)
for ax, sh in zip(axis, shift):
result = roll(result, sh, ax)
return result
else:
raise ValueError("'axis' must be None, an integer or a tuple of integers")
def max(a, axis=None):
"""
Return the maximum of an array or maximum along an axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.max.html
"""
if not is_casadi_type(a, recursive=False):
return _onp.max(
a,
axis=axis,
)
else:
if axis is None:
return _cas.mmax(a)
if axis == 0:
if a.shape[1] == 1:
return _cas.mmax(a)
else:
return array([_cas.mmax(a[:, i]) for i in range(a.shape[1])])
elif axis == 1:
if a.shape[0] == 1:
return _cas.mmax(a)
else:
return array([_cas.mmax(a[i, :]) for i in range(a.shape[0])])
else:
raise ValueError(f"Invalid axis {axis} for CasADi array.")
def min(a, axis=None):
"""
Return the minimum of an array or minimum along an axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.min.html
"""
if not is_casadi_type(a, recursive=False):
return _onp.min(
a=a,
axis=axis,
)
else:
if axis is None:
return _cas.mmin(a)
if axis == 0:
if a.shape[1] == 1:
return _cas.mmin(a)
else:
return array([_cas.mmin(a[:, i]) for i in range(a.shape[1])])
elif axis == 1:
if a.shape[0] == 1:
return _cas.mmin(a)
else:
return array([_cas.mmin(a[i, :]) for i in range(a.shape[0])])
else:
raise ValueError(f"Invalid axis {axis} for CasADi array.")
def reshape(a, newshape, order="C"):
"""
Gives a new shape to an array without changing its data.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.reshape.html
"""
if not is_casadi_type(a, recursive=False):
return _onp.reshape(a, newshape, order=order)
else:
if isinstance(newshape, int):
newshape = (newshape, 1)
elif len(newshape) == 1:
newshape = (newshape[0], 1)
elif len(newshape) == 2:
newshape = tuple(newshape)
elif len(newshape) > 2:
raise ValueError(
"CasADi data types are limited to no more than 2 dimensions."
)
if order == "C":
return _cas.reshape(a.T, newshape[::-1]).T
elif order == "F":
return _cas.reshape(a, newshape)
else:
raise NotImplementedError("Only C and F orders are supported.")
def ravel(a, order="C"):
"""
Returns a contiguous flattened array.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.ravel.html
"""
if not is_casadi_type(a, recursive=False):
return _onp.ravel(a, order=order)
else:
return reshape(a, -1, order=order)
def tile(A, reps):
"""
Construct an array by repeating A the number of times given by reps.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.tile.html
"""
if not is_casadi_type(A, recursive=False):
return _onp.tile(A, reps)
else:
if len(reps) == 1:
return _cas.repmat(A, reps[0], 1)
elif len(reps) == 2:
return _cas.repmat(A, reps[0], reps[1])
else:
raise ValueError(
"Cannot have >2D arrays when using CasADi numeric backend!"
)
def zeros_like(a, dtype=None, order="K", subok=True, shape=None):
"""Return an array of zeros with the same shape and type as a given array."""
if not is_casadi_type(a, recursive=False):
return _onp.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
else:
return _onp.zeros(shape=length(a))
def ones_like(a, dtype=None, order="K", subok=True, shape=None):
"""Return an array of ones with the same shape and type as a given array."""
if not is_casadi_type(a, recursive=False):
return _onp.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
else:
return _onp.ones(shape=length(a))
def empty_like(prototype, dtype=None, order="K", subok=True, shape=None):
"""Return a new array with the same shape and type as a given array."""
if not is_casadi_type(prototype, recursive=False):
return _onp.empty_like(
prototype, dtype=dtype, order=order, subok=subok, shape=shape
)
else:
return zeros_like(prototype)
def full_like(a, fill_value, dtype=None, order="K", subok=True, shape=None):
"""Return a full array with the same shape and type as a given array."""
if not is_casadi_type(a, recursive=False):
return _onp.full_like(
a, fill_value, dtype=dtype, order=order, subok=subok, shape=shape
)
else:
return fill_value * ones_like(a)
def assert_equal_shape(
arrays: Union[List[_onp.ndarray], Dict[str, _onp.ndarray]],
) -> None:
"""
Assert that all of the given arrays are the same shape. If this is not true, raise a ValueError.
Args: arrays: The arrays to be evaluated.
Can be provided as a:
* List, in which case a generic ValueError is thrown
* Dictionary consisting of name:array pairs for key:value, in which case the names are given in the ValueError.
Returns: None. Throws an error if leng
"""
try:
names = arrays.keys()
arrays = list(arrays.values())
except AttributeError:
names = None
def get_shape(array):
try:
return array.shape
except AttributeError: # If it's a float/int
return ()
shape = get_shape(arrays[0])
for array in arrays[1:]:
if not get_shape(array) == shape:
if names is None:
raise ValueError("The given arrays do not have the same shape!")
else:
namelist = ", ".join(names)
raise ValueError(
f"The given arrays {namelist} do not have the same shape!"
)