Skip to content

Commit

Permalink
Enhance __setitem__ and __getitem__ methods to support Ellipsis index…
Browse files Browse the repository at this point in the history
…ing in CasadiLike class
  • Loading branch information
GiulioRomualdi committed Jan 9, 2025
1 parent c5da859 commit 03167f8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/adam/casadi/casadi_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,27 @@ def __truediv__(self, other: Union["CasadiLike", npt.ArrayLike]) -> "CasadiLike"

def __setitem__(self, idx, value: Union["CasadiLike", npt.ArrayLike]):
"""Overrides set item operator"""
self.array[idx] = value.array if type(self) is type(value) else value
if idx is Ellipsis:
self.array = value.array if isinstance(value, CasadiLike) else value
elif isinstance(idx, tuple) and Ellipsis in idx:
idx = tuple(slice(None) if i is Ellipsis else i for i in idx)
self.array[idx] = value.array if isinstance(value, CasadiLike) else value
else:
self.array[idx] = value.array if isinstance(value, CasadiLike) else value


def __getitem__(self, idx) -> "CasadiLike":
"""Overrides get item operator"""
return CasadiLike(self.array[idx])
if idx is Ellipsis:
# Handle the case where only Ellipsis is passed
return CasadiLike(self.array)
elif isinstance(idx, tuple) and Ellipsis in idx:
# Handle the case where Ellipsis is part of a tuple
idx = tuple(slice(None) if k is Ellipsis else k for k in idx)
return CasadiLike(self.array[idx])
else:
# For other cases, delegate to the CasADi object's __getitem__
return CasadiLike(self.array[idx])

@property
def T(self) -> "CasadiLike":
Expand Down

0 comments on commit 03167f8

Please sign in to comment.