Skip to content

Meta Variables

In ConvDO, all the operation is performed on ScalarField or VectorValue:

ConvDO.conv_operators.ScalarField ¤

Bases: CommutativeValue

ScalarField is a class for scalar fields.

Parameters:

Name Type Description Default
value Optional[Tensor]

The value of the scalar field. Defaults to None. It can be changed by calling the register_value method. The shape of the tensor should be (1,1,H,W) or (H,W).

None
domain Optional[Domain]

The domain of the scalar field. Defaults to UnconstrainedDomain().

UnconstrainedDomain()
Source code in ConvDO/conv_operators.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class ScalarField(CommutativeValue):
    r"""ScalarField is a class for scalar fields.

    Args:
        value (Optional[torch.Tensor], optional): The value of the scalar field. Defaults to None.
            It can be changed by calling the `register_value` method.
            The shape of the tensor should be (1,1,H,W) or (H,W).
        domain (Optional[Domain], optional): The domain of the scalar field. Defaults to UnconstrainedDomain().
    """

    def __init__(self,value:Optional[torch.Tensor]=None,
                 domain:Optional[Domain]=UnconstrainedDomain()) -> None:
        self.value=None
        if value is not None:
            self.register_value(value)
        self.domain=domain

    def register_value(self,value: torch.Tensor):
        r"""Register the value of the scalar field.

        Args:
            value (torch.Tensor): The value of the scalar field.
        """

        if len(value.shape)==2:
            value=value.unsqueeze(0).unsqueeze(0)
        self.value=value

    def __add__(self, other):
        if isinstance(other,ScalarField):
            return ScalarField(self.value+other.value,self.domain+other.domain)
        else:
            return ScalarField(self.value+other,self.domain+other)

    def __mul__(self, other):
        if isinstance(other,ScalarField):
            return ScalarField(self.value*other.value,self.domain*other.domain)
        else:
            return ScalarField(self.value*other,self.domain*other)   

    def __pow__(self, other):
        return ScalarField(self.value**other,self.domain**other)

    def __truediv__(self, other):
        if isinstance(other,ScalarField):
            return ScalarField(self.value/other.value,self.domain/other.domain)
        else:
            try:
                return ScalarField(self.value/other,self.domain/other)
            except:
                return NotImplemented

    def __rtruediv__(self, other):
        if isinstance(other,ScalarField):
            return ScalarField(other.value/self.value,other.domain/self.domain)
        else:
            try:
                return ScalarField(other/self.value,other/self.domain)
            except:
                return NotImplemented
value instance-attribute ¤
value = None
domain instance-attribute ¤
domain = domain
__radd__ ¤
__radd__(other)
Source code in ConvDO/meta_type.py
10
11
def __radd__(self, other):
    return self+other
__iadd__ ¤
__iadd__(other)
Source code in ConvDO/meta_type.py
13
14
def __iadd__(self,other):
    return self+other
__sub__ ¤
__sub__(other)
Source code in ConvDO/meta_type.py
16
17
18
19
20
def __sub__(self,other):
    try:
        return self+(-1*other)
    except Exception:
        return NotImplemented
__rsub__ ¤
__rsub__(other)
Source code in ConvDO/meta_type.py
22
23
24
25
26
def __rsub__(self,other):
    try:
        return other+(-1*self)
    except Exception:
        return NotImplemented
__isub__ ¤
__isub__(other)
Source code in ConvDO/meta_type.py
28
29
def __isub__(self,other):
    return self-other    
__rmul__ ¤
__rmul__(other)
Source code in ConvDO/meta_type.py
31
32
def __rmul__(self,other):
    return self*other       
__imul__ ¤
__imul__(other)
Source code in ConvDO/meta_type.py
34
35
def __imul__(self,other):
    return self*other
__init__ ¤
__init__(
    value: Optional[torch.Tensor] = None,
    domain: Optional[Domain] = UnconstrainedDomain(),
) -> None
Source code in ConvDO/conv_operators.py
18
19
20
21
22
23
def __init__(self,value:Optional[torch.Tensor]=None,
             domain:Optional[Domain]=UnconstrainedDomain()) -> None:
    self.value=None
    if value is not None:
        self.register_value(value)
    self.domain=domain
register_value ¤
register_value(value: torch.Tensor)

Register the value of the scalar field.

Parameters:

Name Type Description Default
value Tensor

The value of the scalar field.

required
Source code in ConvDO/conv_operators.py
25
26
27
28
29
30
31
32
33
34
def register_value(self,value: torch.Tensor):
    r"""Register the value of the scalar field.

    Args:
        value (torch.Tensor): The value of the scalar field.
    """

    if len(value.shape)==2:
        value=value.unsqueeze(0).unsqueeze(0)
    self.value=value
__add__ ¤
__add__(other)
Source code in ConvDO/conv_operators.py
36
37
38
39
40
def __add__(self, other):
    if isinstance(other,ScalarField):
        return ScalarField(self.value+other.value,self.domain+other.domain)
    else:
        return ScalarField(self.value+other,self.domain+other)
__mul__ ¤
__mul__(other)
Source code in ConvDO/conv_operators.py
42
43
44
45
46
def __mul__(self, other):
    if isinstance(other,ScalarField):
        return ScalarField(self.value*other.value,self.domain*other.domain)
    else:
        return ScalarField(self.value*other,self.domain*other)   
__pow__ ¤
__pow__(other)
Source code in ConvDO/conv_operators.py
48
49
def __pow__(self, other):
    return ScalarField(self.value**other,self.domain**other)
__truediv__ ¤
__truediv__(other)
Source code in ConvDO/conv_operators.py
51
52
53
54
55
56
57
58
def __truediv__(self, other):
    if isinstance(other,ScalarField):
        return ScalarField(self.value/other.value,self.domain/other.domain)
    else:
        try:
            return ScalarField(self.value/other,self.domain/other)
        except:
            return NotImplemented
__rtruediv__ ¤
__rtruediv__(other)
Source code in ConvDO/conv_operators.py
60
61
62
63
64
65
66
67
def __rtruediv__(self, other):
    if isinstance(other,ScalarField):
        return ScalarField(other.value/self.value,other.domain/self.domain)
    else:
        try:
            return ScalarField(other/self.value,other/self.domain)
        except:
            return NotImplemented

ConvDO.meta_type.VectorValue ¤

A class to represent a 2D vector. Supports basic operations such as addition, subtraction, multiplication, division, and dot product. The vector is represented as a tuple (ux,uy), where ux and uy are the x and y components of the vector, respectively. The x/y components of the vector can be any type that supports the basic operations.

Parameters:

Name Type Description Default
ux Any

x-component of the vector

required
uy Any

y-component of the vector

required
Source code in ConvDO/meta_type.py
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class VectorValue():
    """
    A class to represent a 2D vector.
    Supports basic operations such as addition, subtraction, multiplication, division, and dot product.
    The vector is represented as a tuple (`ux`,`uy`), where `ux` and `uy` are the x and y components of the vector, respectively.
    The x/y components of the vector can be any type that supports the basic operations.

    Args:
        ux (Any): x-component of the vector
        uy (Any): y-component of the vector
    """

    def __init__(self,ux,uy) -> None:
        self.ux=ux
        self.uy=uy

    def __str__(self) -> str:
        return "({},{})".format(self.ux,self.uy)

    # + : vector+scalar,vector+vector    
    def __add__(self, other):
        if isinstance(other,VectorValue):
            return VectorValue(self.ux+other.ux,self.uy+other.uy)
        else:
            try:
                return VectorValue(self.ux+other,self.uy+other)
            except Exception:
                return NotImplemented

    def __radd__(self, other):
        return self+other

    def __iadd__(self,other):
        return self+other

    def __sub__(self,other):
        try:
            return self+(-1*other)
        except Exception:
            return NotImplemented

    def __rsub__(self,other):
        try:
            return other+(-1*self)
        except Exception:
            return NotImplemented

    def __isub__(self,other):
        return self-other

    # * : vectorvector, scalarvector            
    def __mul__(self,other):
        if isinstance(other,VectorValue):
            return TensorValue(self.ux*other.ux,self.ux*other.uy,self.uy*other.ux,self.uy*other.uy)
        else:
            try:
                return VectorValue(self.ux*other,self.uy*other)
            except Exception:
                return NotImplemented  

    def __rmul__(self,other):
        try:
            return VectorValue(self.ux*other,self.uy*other)
        except Exception:
            return NotImplemented         

    def __imul__(self,other):
        return self*other

    def __pow__(self, other):
        if isinstance(other,int) and other>1:
            return_value=self*self
            for i in range(other-2):
                return_value=return_value*self
            return return_value
        else:
            raise NotImplementedError("Operation not supported")

    # @ (dot product): vector@vector, vector@tensor

    def __matmul__(self,other):
        if isinstance(other,VectorValue):
            return self.ux*other.ux+self.uy*other.uy
        if isinstance(other,TensorValue):
            return VectorValue(self.ux*other.uxx+self.uy*other.uxy,self.ux*other.uyx+self.uy*other.uyy)

    def __truediv__(self, other):
        try:
            return VectorValue(self.ux/other,self.uy/other)
        except Exception:
            return NotImplemented
ux instance-attribute ¤
ux = ux
uy instance-attribute ¤
uy = uy
__init__ ¤
__init__(ux, uy) -> None
Source code in ConvDO/meta_type.py
50
51
52
def __init__(self,ux,uy) -> None:
    self.ux=ux
    self.uy=uy
__str__ ¤
__str__() -> str
Source code in ConvDO/meta_type.py
54
55
def __str__(self) -> str:
    return "({},{})".format(self.ux,self.uy)
__add__ ¤
__add__(other)
Source code in ConvDO/meta_type.py
58
59
60
61
62
63
64
65
def __add__(self, other):
    if isinstance(other,VectorValue):
        return VectorValue(self.ux+other.ux,self.uy+other.uy)
    else:
        try:
            return VectorValue(self.ux+other,self.uy+other)
        except Exception:
            return NotImplemented
__radd__ ¤
__radd__(other)
Source code in ConvDO/meta_type.py
67
68
def __radd__(self, other):
    return self+other
__iadd__ ¤
__iadd__(other)
Source code in ConvDO/meta_type.py
70
71
def __iadd__(self,other):
    return self+other
__sub__ ¤
__sub__(other)
Source code in ConvDO/meta_type.py
73
74
75
76
77
def __sub__(self,other):
    try:
        return self+(-1*other)
    except Exception:
        return NotImplemented
__rsub__ ¤
__rsub__(other)
Source code in ConvDO/meta_type.py
79
80
81
82
83
def __rsub__(self,other):
    try:
        return other+(-1*self)
    except Exception:
        return NotImplemented
__isub__ ¤
__isub__(other)
Source code in ConvDO/meta_type.py
85
86
def __isub__(self,other):
    return self-other
__mul__ ¤
__mul__(other)
Source code in ConvDO/meta_type.py
89
90
91
92
93
94
95
96
def __mul__(self,other):
    if isinstance(other,VectorValue):
        return TensorValue(self.ux*other.ux,self.ux*other.uy,self.uy*other.ux,self.uy*other.uy)
    else:
        try:
            return VectorValue(self.ux*other,self.uy*other)
        except Exception:
            return NotImplemented  
__rmul__ ¤
__rmul__(other)
Source code in ConvDO/meta_type.py
 98
 99
100
101
102
def __rmul__(self,other):
    try:
        return VectorValue(self.ux*other,self.uy*other)
    except Exception:
        return NotImplemented         
__imul__ ¤
__imul__(other)
Source code in ConvDO/meta_type.py
104
105
def __imul__(self,other):
    return self*other
__pow__ ¤
__pow__(other)
Source code in ConvDO/meta_type.py
107
108
109
110
111
112
113
114
def __pow__(self, other):
    if isinstance(other,int) and other>1:
        return_value=self*self
        for i in range(other-2):
            return_value=return_value*self
        return return_value
    else:
        raise NotImplementedError("Operation not supported")
__matmul__ ¤
__matmul__(other)
Source code in ConvDO/meta_type.py
118
119
120
121
122
def __matmul__(self,other):
    if isinstance(other,VectorValue):
        return self.ux*other.ux+self.uy*other.uy
    if isinstance(other,TensorValue):
        return VectorValue(self.ux*other.uxx+self.uy*other.uxy,self.ux*other.uyx+self.uy*other.uyy)
__truediv__ ¤
__truediv__(other)
Source code in ConvDO/meta_type.py
124
125
126
127
128
def __truediv__(self, other):
    try:
        return VectorValue(self.ux/other,self.uy/other)
    except Exception:
        return NotImplemented