Exemple #1
0
    def __array__(self, dtype=None):
        if not self.is_indexed():
            ans = NumericNDArray(shape=(1, ), dtype=object)
            ans[0] = self
            return ans

        _dim = self.dim()
        if _dim is None:
            raise TypeError(
                "Cannot convert a non-dimensioned Pyomo IndexedComponent "
                "(%s) into a numpy array" % (self, ))
        bounds = self.index_set().bounds()
        if not isinstance(bounds[0], Sequence):
            bounds = ((bounds[0], ), (bounds[1], ))
        if any(b != 0 for b in bounds[0]):
            raise TypeError("Cannot convert a Pyomo IndexedComponent "
                            "(%s) with bounds [%s, %s] into a numpy array" %
                            (self, bounds[0], bounds[1]))
        shape = tuple(b + 1 for b in bounds[1])
        ans = NumericNDArray(shape=shape, dtype=object)
        for k, v in self.items():
            ans[k] = v
        return ans
Exemple #2
0
 def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
     return NumericNDArray.__array_ufunc__(None, ufunc, method, *inputs,
                                           **kwargs)