# -*- coding: utf-8 -*-# noinspection PyUnresolvedReferencesr"""Each space is an instance of a particular space class which inherits :class:`SpaceBase`. .. autoclass:: src.spaces.base.SpaceBase :members: mesh, manifold, m, n, make_form"""fromtools.frozenimportFrozenfromsrc.configimportget_embedding_space_dimfromsrc.form.mainimportFormfromsrc.spaces.finiteimportSpaceFiniteSettingfromtools.miscellaneous.random_importstring_digitsfromtimeimporttime
[docs]classSpaceBase(Frozen):""""""def__init__(self,mesh,orientation):""""""self._objective=None# when initializing, it has no objective instance. And when we generate an objective of a# abstract space, we store the last objective one with this attribute.self._mesh=meshassertorientationin('inner','outer','i','o',None,'None'), \
f"orientation={orientation} is wrong, must be one of ('inner', 'outer', 'i', 'o', None)."iforientation=='i':orientation='inner'eliforientation=='o':orientation='outer'eliforientationin(None,'unknown','None'):orientation='unknown'else:passself._orientation=orientationself._finite=None# the finite setting@propertydef_pure_lin_repr(self):""""""raiseNotImplementedError()@propertydefmesh(self):"""The mesh I am built on."""returnself._mesh@propertydefmanifold(self):"""The manifold I am built on."""returnself.mesh.manifold@propertydeforientation(self):"""My orientation."""returnself._orientation@propertydefopposite_orientation(self):ifself.orientation=='inner':return'outer'elifself.orientation=='outer':return'inner'elifself.orientation=='unknown':return'unknown'@propertydefn(self):"""the dimensions of the mesh I am living in."""returnself.mesh.ndim@propertydefm(self):"""the dimensions of the space I am living in."""returnget_embedding_space_dim()
[docs]defmake_form(self,sym_repr,lin_repr,dual_representation=False):"""Define a form which is an element of this space. Parameters ---------- sym_repr : str The symbolic representation of the form. lin_repr : str The linguistic representation of the form. dual_representation : bool, optional Whether the output form uses dual representation? The default value is ``False``. Returns ------- form : :class:`src.form.main.Form` The output form. """assertisinstance(sym_repr,str),f"symbolic representation must be a str."form=Form(self,sym_repr,lin_repr,True,# is_root)ifdual_representation:form.set_dual_representation(True)else:passreturnform
defmake_random_form(self):"""Usually used as intermediate forms."""sym=string_digits(8)t=time()random_sym_repr=sym+str(t)[-5:]random_lin_repr=str(hash(random_sym_repr))returnself.make_form(random_sym_repr,random_lin_repr)def__eq__(self,other):""""""returnself.__repr__()==other.__repr__()@staticmethoddef_is_space():"""A private signature/tag."""returnTrue@propertydeffinite(self):ifself._finiteisNone:self._finite=SpaceFiniteSetting(self)returnself._finitedefd(self):"""d (self)"""fromsrc.spaces.operatorsimportdreturnd(self)