0

When I run this code

import numpy as np
from nptyping import NDArray, Shape, Int32
from typing import Any, Callable

class Obj:

    def f(self, x: NDArray[(1, ...), np.float64]) -> Any:
        return x[0]**2 + 3 * (x[1] - 1)**2

    def g(self, x: NDArray[(1, ...), np.float64]) -> Any:
        return 2 * (x[0] - 1)**2 + x[1]**2

    def Fs(self, x: NDArray[(1, ...), np.float64]) -> NDArray[(1, ...), np.float64]:
        return np.array([self.f(x), self.g(x)])

    def Fss(self):
        return np.array([self.f, self.g])

I get the following errors:

Traceback (most recent call last):
  File "/content/drive/MyDrive/DATN/sdm-main/obj_func.py", line 5, in <module>
    class Obj:
  File "/content/drive/MyDrive/DATN/sdm-main/obj_func.py", line 7, in Obj
    def f(self, x: NDArray[(1, ...), np.float64]) -> Any:
  File "/usr/local/lib/python3.9/dist-packages/nptyping/base_meta_classes.py", line 145, in __getitem__
    args = cls._get_item(item)
  File "/usr/local/lib/python3.9/dist-packages/nptyping/ndarray.py", line 74, in _get_item
    shape, dtype = cls._get_from_tuple(item)
  File "/usr/local/lib/python3.9/dist-packages/nptyping/ndarray.py", line 115, in _get_from_tuple
    shape = cls._get_shape(item[0])
  File "/usr/local/lib/python3.9/dist-packages/nptyping/ndarray.py", line 128, in _get_shape
    raise InvalidArgumentsError(
nptyping.error.InvalidArgumentsError: Unexpected argument '(1, Ellipsis)', expecting Shape[<ShapeExpression>] or Literal[<ShapeExpression>] or typing.Any

What can I do to make the code work?

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • What are you trying to specify with `(1, ...)`? Is there documented evidence that typing supports that? https://stackoverflow.com/a/68132027/901925 – hpaulj Apr 17 '23 at 18:39
  • Also https://stackoverflow.com/questions/71109838/numpy-typing-with-specific-shape-and-datatype. Any objections to marking this as duplicate? – hpaulj Apr 17 '23 at 19:50

0 Answers0