Skip to content

Commit

Permalink
remove unnecessary setter
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkoham committed Dec 7, 2021
1 parent 8beeb63 commit 8af7388
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,119 @@ class HyperCubicLattice(Lattice):
The boundary conditions are open for all the directions.
"""

def __init__(
self,
size: Tuple[int, ...],
edge_parameter: Union[complex, Tuple[complex, ...]] = 1.0,
onsite_parameter: complex = 0.0,
boundary_condition: Union[
BoundaryCondition, Tuple[BoundaryCondition, ...]
] = BoundaryCondition.OPEN,
) -> None:
"""
Args:
size: Lengths of each dimension.
edge_parameter: Weights on the edges in each direction.
When it is a single value, it is interpreted as a tuple of the same length as `size`
consisting of the same values.
Defaults to 1.0.
onsite_parameter: Weight on the self-loops, which are edges connecting a node to itself.
This is uniform over the lattice points.
Defaults to 0.0.
boundary_condition: Boundary condition for each dimension.
The available boundary conditions are:
BoundaryCondition.OPEN, BoundaryCondition.PERIODIC.
When it is a single value, it is interpreted as a tuple of the same length as `size`
consisting of the same values.
Defaults to BoundaryCondition.OPEN.
Raises:
ValueError: When edge parameter or boundary condition is a tuple,
the length of that is not the same as that of size.
"""

self.dim = len(size)

self.size = size

# edge parameter
if isinstance(edge_parameter, (int, float, complex)):
edge_parameter = (edge_parameter,) * self.dim
elif isinstance(edge_parameter, tuple):
if len(edge_parameter) != self.dim:
raise ValueError(
"size mismatch, "
f"`edge_parameter`: {len(edge_parameter)}, `size`: {self.dim}."
"The length of `edge_parameter` must be the same as that of size."
)

self._edge_parameter = edge_parameter

self._onsite_parameter = onsite_parameter

# boundary condition
if isinstance(boundary_condition, BoundaryCondition):
boundary_condition = (boundary_condition,) * self.dim
elif isinstance(boundary_condition, tuple):
if len(boundary_condition) != self.dim:
raise ValueError(
"size mismatch, "
f"`boundary_condition`: {len(boundary_condition)}, `size`: {self.dim}."
"The length of `boundary_condition` must be the same as that of size."
)

self._boundary_condition = boundary_condition

graph = PyGraph(multigraph=False)
graph.add_nodes_from(range(np.prod(size)))

# add edges excluding the boundary edges
bulk_edge_list = self._bulk_edges()
graph.add_edges_from(bulk_edge_list)

# add self-loops.
self_loop_list = self._self_loops()
graph.add_edges_from(self_loop_list)

# add edges that cross the boundaries
boundary_edge_list = self._boundary_edges()
graph.add_edges_from(boundary_edge_list)

# a list of edges that depend on the boundary condition
self.boundary_edges = [(edge[0], edge[1]) for edge in boundary_edge_list]

super().__init__(graph)

# default position for one and two-dimensional cases.
self.pos = self._default_position()

@property
def edge_parameter(self) -> Union[complex, Tuple[complex, ...]]:
"""Weights on the edges in each direction.
Returns:
the parameter for the edges.
"""
return self._edge_parameter

@property
def onsite_parameter(self) -> complex:
"""Weight on the self-loops
Returns:
the parameter for the self-loops.
"""
return self._onsite_parameter

@property
def boundary_condition(self) -> Union[BoundaryCondition, Tuple[BoundaryCondition, ...]]:
"""Boundary condition for each dimension.
Returns:
the boundary condition.
"""
return self._boundary_condition

def _coordinate_to_index(self, coord: np.ndarray) -> int:
"""Convert the coordinate of a lattice point to an integer for labeling.
When size=(l0, l1, l2, ...), then a coordinate (x0, x1, x2, ...) is converted as
Expand All @@ -57,8 +170,8 @@ def _coordinate_to_index(self, coord: np.ndarray) -> int:
coord: Input coordinate to be converted.
Returns:
int: Return x0 + x1*l0 + x2*l0*l1 + ...
when coord=np.array([x0, x1, x2...]) and size=(l0, l1, l2, ...).
Return x0 + x1*l0 + x2*l0*l1 + ...
when coord=np.array([x0, x1, x2...]) and size=(l0, l1, l2, ...).
"""
size = self.size
dim = len(size)
Expand All @@ -71,7 +184,7 @@ def _self_loops(self) -> List[Tuple[int, int, complex]]:
List[Tuple[int, int, complex]] : List of the self-loops.
"""
num_nodes = np.prod(self.size)
return [(node_a, node_a, self.onsite_parameter) for node_a in range(num_nodes)]
return [(node_a, node_a, self._onsite_parameter) for node_a in range(num_nodes)]

def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
"""Return a list consisting of the edges in the bulk, which don't cross the boundaries.
Expand All @@ -81,7 +194,7 @@ def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
"""
list_of_edges = []
size = self.size
edge_parameter = self.edge_parameter
edge_parameter = self._edge_parameter
dim = len(size)
coordinates = list(product(*map(range, size)))
# add edges excluding the boundary edges
Expand All @@ -105,8 +218,8 @@ def _boundary_edges(self) -> List[Tuple[int, int, complex]]:
"""
list_of_edges = []
size = self.size
edge_parameter = self.edge_parameter
boundary_condition = self.boundary_condition
edge_parameter = self._edge_parameter
boundary_condition = self._boundary_condition
dim = len(size)
for i in range(dim):
# add edges when the boundary condition is periodic.
Expand Down Expand Up @@ -143,7 +256,7 @@ def _default_position(self) -> Optional[Dict[int, List[float]]]:
When the dimension is larger than 2, it returns None.
"""
size = self.size
boundary_condition = self.boundary_condition
boundary_condition = self._boundary_condition
dim = len(size)
if dim == 1:
if boundary_condition[0] == BoundaryCondition.OPEN:
Expand Down Expand Up @@ -176,92 +289,6 @@ def _default_position(self) -> Optional[Dict[int, List[float]]]:

return pos

def __init__(
self,
size: Tuple[int, ...],
edge_parameter: Union[complex, Tuple[complex, ...]] = 1.0,
onsite_parameter: complex = 0.0,
boundary_condition: Union[
BoundaryCondition, Tuple[BoundaryCondition, ...]
] = BoundaryCondition.OPEN,
) -> None:
"""
Args:
size: Lengths of each dimension.
edge_parameter: Weights on the edges in each direction.
When it is a single value, it is interpreted as a tuple of the same length as `size`
consisting of the same values.
Defaults to 1.0.
onsite_parameter: Weight on the self-loops, which are edges connecting a node to itself.
This is uniform over the lattice points.
Defaults to 0.0.
boundary_condition: Boundary condition for each dimension.
The available boundary conditions are:
BoundaryCondition.OPEN, BoundaryCondition.PERIODIC.
When it is a single value, it is interpreted as a tuple of the same length as `size`
consisting of the same values.
Defaults to BoundaryCondition.OPEN.
Raises:
ValueError: When edge parameter or boundary condition is a tuple,
the length of that is not the same as that of size.
"""

self.dim = len(size)

self.size = size

# edge parameter
if isinstance(edge_parameter, (int, float, complex)):
edge_parameter = (edge_parameter,) * self.dim
elif isinstance(edge_parameter, tuple):
if len(edge_parameter) != self.dim:
raise ValueError(
"size mismatch, "
f"`edge_parameter`: {len(edge_parameter)}, `size`: {self.dim}."
"The length of `edge_parameter` must be the same as that of size."
)

self.edge_parameter = edge_parameter

self.onsite_parameter = onsite_parameter

# boundary condition
if isinstance(boundary_condition, BoundaryCondition):
boundary_condition = (boundary_condition,) * self.dim
elif isinstance(boundary_condition, tuple):
if len(boundary_condition) != self.dim:
raise ValueError(
"size mismatch, "
f"`boundary_condition`: {len(boundary_condition)}, `size`: {self.dim}."
"The length of `boundary_condition` must be the same as that of size."
)

self.boundary_condition = boundary_condition

graph = PyGraph(multigraph=False)
graph.add_nodes_from(range(np.prod(size)))

# add edges excluding the boundary edges
bulk_edge_list = self._bulk_edges()
graph.add_edges_from(bulk_edge_list)

# add self-loops.
self_loop_list = self._self_loops()
graph.add_edges_from(self_loop_list)

# add edges that cross the boundaries
boundary_edge_list = self._boundary_edges()
graph.add_edges_from(boundary_edge_list)

# a list of edges that depend on the boundary condition
self.boundary_edges = [(edge[0], edge[1]) for edge in boundary_edge_list]

super().__init__(graph)

# default position for one and two-dimensional cases.
self.pos = self._default_position()

def draw_without_boundary(
self,
self_loop: bool = False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def __init__(
BoundaryCondition.OPEN, BoundaryCondition.PERIODIC.
Defaults to BoundaryCondition.OPEN.
"""
self._num_nodes = num_nodes

super().__init__(
size=(num_nodes,),
edge_parameter=edge_parameter,
onsite_parameter=onsite_parameter,
boundary_condition=boundary_condition,
)

@property
def num_nodes(self) -> int:
"""Number of nodes
Returns:
The number of nodes for the line lattice
"""
return self._num_nodes
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"""The square lattice"""
from typing import Tuple, Union

from .hyper_cubic_lattice import HyperCubicLattice
from .boundary_condition import BoundaryCondition
from .hyper_cubic_lattice import HyperCubicLattice


class SquareLattice(HyperCubicLattice):
Expand Down Expand Up @@ -47,11 +47,29 @@ def __init__(
consisting of the same values.
Defaults to BoundaryCondition.OPEN.
"""
self.rows = rows
self.cols = cols
self._rows = rows
self._cols = cols
super().__init__(
size=(rows, cols),
edge_parameter=edge_parameter,
onsite_parameter=onsite_parameter,
boundary_condition=boundary_condition,
)

@property
def rows(self) -> int:
"""Length of the x direction
Returns:
the length
"""
return self._rows

@property
def cols(self) -> int:
"""Length of the y direction
Returns:
the length
"""
return self._cols

0 comments on commit 8af7388

Please sign in to comment.