@@ -25,6 +25,7 @@ def get_edge_vectors(
2525 shifts : np .ndarray , # [n_edges, 3]
2626 cell : Optional [np .ndarray ], # [n_graph, 3, 3]
2727 n_edge : np .ndarray , # [n_graph]
28+ use_np : bool = False ,
2829) -> Tuple [np .ndarray , np .ndarray ]:
2930 """Compute positions of sender and receiver nodes of each edge.
3031
@@ -55,6 +56,8 @@ def get_edge_vectors(
5556 Output `S` of `ase.neighborlist.primitive_neighbor_list`.
5657 cell: The cell of each graph. Array of shape ``[n_graph, 3, 3]``.
5758 n_edge: The number of edges of each graph. Array of shape ``[n_graph]``.
59+ use_np: Whether to use `numpy` or `jax.numpy` for the computation. Default
60+ is `False`, which means `jax.numpy` is used.
5861
5962 Returns:
6063 The positions of the sender and receiver nodes of each edge.
@@ -63,15 +66,21 @@ def get_edge_vectors(
6366 vectors_receivers = positions [receivers ] # [n_edges, 3]
6467
6568 if cell is not None :
66- num_edges = receivers .shape [0 ]
67- shifts = jnp .einsum (
69+ if use_np :
70+ np_ = np
71+ kwargs_num_edges = {}
72+ else :
73+ np_ = jnp
74+ kwargs_num_edges = {"total_repeat_length" : receivers .shape [0 ]}
75+
76+ shifts = np_ .einsum (
6877 "ei,eij->ej" ,
6978 shifts , # [n_edges, 3]
70- jnp .repeat (
79+ np_ .repeat (
7180 cell , # [n_graph, 3, 3]
7281 n_edge , # [n_graph]
7382 axis = 0 ,
74- total_repeat_length = num_edges ,
83+ ** kwargs_num_edges ,
7584 ), # [n_edges, 3, 3]
7685 ) # [n_edges, 3]
7786 vectors_senders -= shifts # minus sign to match results with ASE
@@ -86,6 +95,7 @@ def get_edge_relative_vectors(
8695 shifts : np .ndarray , # [n_edges, 3]
8796 cell : Optional [np .ndarray ], # [n_graph, 3, 3]
8897 n_edge : np .ndarray , # [n_graph]
98+ use_np : bool = False ,
8999) -> np .ndarray :
90100 """Compute the relative edge vectors from senders to receivers.
91101
@@ -108,6 +118,8 @@ def get_edge_relative_vectors(
108118 functionality, and labelled `S` by ASE.
109119 cell: The unit cells of each graph, an array of shape `[n_graph, 3, 3]`.
110120 n_edge: The number of edges for each graph, an array of shape `[n_graph]`.
121+ use_np: Whether to use `numpy` or `jax.numpy` for the computation. Default
122+ is `False`, which means `jax.numpy` is used.
111123
112124 Returns:
113125 The relative edge vectors, labelled `D` by ASE.
@@ -119,5 +131,6 @@ def get_edge_relative_vectors(
119131 shifts = shifts ,
120132 cell = cell ,
121133 n_edge = n_edge ,
134+ use_np = use_np ,
122135 )
123136 return vectors_receivers - vectors_senders
0 commit comments