base_field_type.py 1.35 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
import numpy as np
4 5


theos's avatar
theos committed
6
class FieldType(object):
7
    def __init__(self, shape, dtype):
8 9 10 11 12 13
        try:
            new_shape = tuple([int(i) for i in shape])
        except TypeError:
            new_shape = (int(shape), )
        self._shape = new_shape

14 15
        self._dtype = np.dtype(dtype)

theos's avatar
theos committed
16 17
        self._dof = self._get_dof()

18 19 20 21 22 23 24 25
    @property
    def shape(self):
        return self._shape

    @property
    def dtype(self):
        return self._dtype

26 27 28 29
    @property
    def dof(self):
        return self._dof

theos's avatar
theos committed
30
    def _get_dof(self):
31 32 33 34 35
        if issubclass(self.dtype.type, np.complexfloating):
            multiplicator = 2
        else:
            multiplicator = 1

theos's avatar
theos committed
36
        dof = multiplicator*reduce(lambda x, y: x*y, self.shape)
37 38
        return dof

39 40 41 42 43 44 45 46 47 48 49 50
    def process(self, method_name, array, inplace=True, **kwargs):
        try:
            result_array = self.__getattr__(method_name)(array,
                                                         inplace,
                                                         **kwargs)
        except AttributeError:
            if inplace:
                result_array = array
            else:
                result_array = array.copy()

        return result_array
51 52 53

    def complement_cast(self, x, axis=None):
        return x
54 55 56

    def dot_contraction(self, x, axes):
        raise NotImplementedError