def test_automatic_promotions():
    class A: pass
    class B(A): pass
    a, b = A(), B()

    assert_allsame(promote(a, b), (a, b))
    assert_allsame(promote(a, a), (a, a))
    assert promote_type(A, B) is A
    assert promote_type(B, A) is A
Beispiel #2
0
 def __mul_matrix(self, other):
     outtype = promote_type(type(self), type(other))
     a, b, c, d, e, f, g, h, i = self.flat
     j, k, l, m, n, o, p, q, r = other.flat
     data = [
         a * j + b * m + c * p, a * k + b * n + c * q, a * l + b * o + c * r,
         d * j + e * m + f * p, d * k + e * n + f * q, d * l + e * o + f * r,
         g * j + h * m + i * p, g * k + h * n + i * q, g * l + h * o + i * r,
     ]
     return outtype.from_flat(data, copy=False)
def sub_elementwise_factory(argtypes, restype):
    T, S = argtypes
    if issubclass(T, S):
        return S.__subsame__
    if issubclass(S, T):
        return T.__subsame__
    if T.__origin__ is not S.__origin__:
        return NotImplemented
    elif T.shape != S.shape:
        return NotImplemented

    def func(u, v):
        return u.convert(dtype) - v.convert(dtype)

    dtype = promote_type(T.dtype, S.dtype)
    return func
def add_elementwise_factory(argtypes, restype):
    T, S = argtypes
    if issubclass(T, S):
        return S.__addsame__
    if issubclass(S, T):
        return T.__addsame__
    if T.__origin__ is not S.__origin__:
        return NotImplemented
    if T.shape != S.shape:
        return NotImplemented

    def func(u, v):
        return u.convert(dtype) + v.convert(dtype)

    dtype = promote_type(T.dtype, S.dtype)
    return func
Beispiel #5
0
    def __finalizetype__(cls):
        """
        Assure that the resulting type has the correct shape, size, dim,
        dtype
        """

        # Shape parameters
        if cls.__parameters__ is None or cls.shape is None:
            default_ns = default_smallvectors_type_ns(cls.__parameters__)
            for k, v in default_ns.items():
                if getattr(cls, k, None) is None:
                    setattr(cls, k, v)

        # Pick up flat object
        flat = mFlat if issubclass(cls, _Mutable) else Flat
        cls.__flat__ = flat

        # Floating parameter
        if cls.dtype is not None:
            cls._floating = promote_type(cls._float, cls.dtype)

        assert cls.dtype is not Any, cls
Beispiel #6
0
    def __finalizetype__(cls):
        """
        Assure that the resulting type has the correct shape, size, dim,
        dtype
        """

        # Shape parameters
        if cls.__parameters__ is None or cls.shape is None:
            default_ns = default_smallvectors_type_ns(cls.__parameters__)
            for k, v in default_ns.items():
                if getattr(cls, k, None) is None:
                    setattr(cls, k, v)

        # Pick up flat object
        flat = mFlat if issubclass(cls, _Mutable) else Flat
        cls.__flat__ = flat

        # Floating parameter
        if cls.dtype is not None:
            cls._floating = promote_type(cls._float, cls.dtype)

        assert cls.dtype is not Any, cls
Beispiel #7
0
    def inv(self):
        """
        Return the inverse matrix.
        """

        # Simple and naive matrix inversion using Gaussian elimination
        # Creates extended matrix
        N = self.nrows
        dtype = promote_type(float, self.dtype)
        matrix = self._mmat[N, N, dtype].from_flat(self.flat)
        matrix = matrix.append_col(self._identity(N))

        # Make left hand side upper triangular
        for i in range(0, N):
            # Search for maximum value in the truncated column and put it
            # in pivoting position
            trunc_col = list(matrix.col(i))[i:]
            _, idx = max([(abs(c), i) for (i, c) in enumerate(trunc_col)])
            matrix.iswap_rows(i, idx + i)

            # Find linear combinations that make all rows below the current one
            # become equal to zero in the current column
            Z = matrix[i, i]
            for k in range(i + 1, N):
                matrix.isum_row(k, matrix[i] * (-matrix[k, i] / Z))

            # Make the left hand side diagonal
            Z = matrix[i, i]
            for k in range(0, i):
                matrix.isum_row(k, matrix[i] * (-matrix[k, i] / Z))

        # Normalize by the diagonal
        for i in range(N):
            matrix.imul_row(i, 1 / matrix[i, i])

        out = matrix.select_cols(range(N, 2 * N))
        return self._mat[N, N, dtype].from_flat(out.flat)
def test_successfull_type_promotions_of_base_types():
    assert promote_type(int, float) is float
    assert promote_type(bool, complex) is complex