Skip to content
Advertisement

Tag: numpy

Cast a Python class to Numpy Array

Can I cast a python class to a numpy array? In the last step, I would like the to obtain an array np.array([X.x, X.y]). Instead, I get array(X(x=0, y=0), dtype=object). Can I provide method for the dataclass so that the casting works as desired (or overload one of the existing methods of the dataclass)? Answer From docstring of numpy.array we

Faster matrix calculation in numpy

Is there some faster variant of computing the following matrix (from this paper), given a nxn matrix M and a n-vector X: ? I currently compute it as follows: This is very slow, but I suspect there is a nicer “numpythonic” way of doing this instead of looping… EDIT: While all answers work and are much faster than my naive

Advertisement