Skip to content

Commit

Permalink
added ot_base.is_singlet to get rid of lattice.otype.data_otype() == …
Browse files Browse the repository at this point in the history
…gpt.ot_singlet
  • Loading branch information
Daniel Knuettel authored and Daniel Knuettel committed Mar 11, 2024
1 parent 5bff988 commit a6253ca
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/gpt/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def mat_backward(dst, src):

def coordinate_mask(field, mask):
assert isinstance(mask, numpy.ndarray)
assert field.otype.data_otype() == gpt.ot_singlet
assert field.otype.is_singlet

x = gpt.coordinates(field)
field[x] = mask.astype(field.grid.precision.complex_dtype).reshape((len(mask), 1))
Expand Down
2 changes: 1 addition & 1 deletion lib/gpt/core/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __mul__(self, l):
if uf & gpt.factor_unary.BIT_CONJ != 0:
lhs = lhs.conj()
res = gpt.tensor(np.tensordot(lhs, l.array, axes=mt[1]), mt[0]())
if res.otype == gpt.ot_singlet:
if res.otype.is_singlet:
res = complex(res.array)
return res
assert 0
Expand Down
4 changes: 2 additions & 2 deletions lib/gpt/core/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def __itruediv__(self, expr):
return self

def __lt__(self, other):
assert self.otype.data_otype() == gpt.ot_singlet
assert other.otype.data_otype() == gpt.ot_singlet
assert self.otype.is_singlet
assert other.otype.is_singlet
res = gpt.lattice(self)
params = {"operator": "<"}
cgpt.binary(res.v_obj[0], self.v_obj[0], other.v_obj[0], params)
Expand Down
1 change: 1 addition & 0 deletions lib/gpt/core/object_type/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ot_base:

# list of object types to which I can convert and converter function
ctab = {}
is_singlet = False

# safe cast of data_alias
def data_otype(self):
Expand Down
20 changes: 1 addition & 19 deletions lib/gpt/core/object_type/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,7 @@ class ot_singlet(ot_base):
mtab = {
"ot_singlet": (lambda: ot_singlet(), None),
}

# FIXME: I am not sure if this is a good idea wrt. parellelism.
# Make singlet and all its derived subclasses singleton.
_instance = None
def __new__(cls, *args, **kwargs):
if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance

# There are many LoCs like
# res.otype == g.ot_singlet
# This will allow this behaviour to continue.
def __eq__(self, o):
if isinstance(o, type):
return type(self) == o
# This works because singleton.
if self is o:
return True
return o.__eq__(self)
is_singlet = True

def data_otype(self):
return ot_singlet()
Expand Down
4 changes: 2 additions & 2 deletions lib/gpt/core/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def adj(self):
return tensor(np.transpose(self.array.conj(), self.otype.transposed), self.otype)

def reduced(self):
if self.otype.data_otype() == gpt.ot_singlet:
if self.otype.is_singlet:
return complex(self.array)
return self

Expand All @@ -92,7 +92,7 @@ def trace(self, t):
if ct[0] is not None:
res = tensor(np.trace(res.array, offset=0, axis1=ct[0], axis2=ct[1]), ct[2]())

if res.otype == gpt.ot_singlet:
if res.otype.is_singlet:
res = complex(res.array)
return res

Expand Down
2 changes: 1 addition & 1 deletion lib/gpt/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def to_num(x):

# tensor
def value_to_tensor(val, otype):
if otype.data_otype() == gpt.ot_singlet:
if otype.is_singlet:
# this is not ideal, can we do a subclass of complex that preserves otype info?
return complex(val)
return gpt.tensor(val, otype)
Expand Down

0 comments on commit a6253ca

Please sign in to comment.