/
AmplitudeVector.py
162 lines (134 loc) · 6.09 KB
/
AmplitudeVector.py
1
2
3
4
5
6
7
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
## ---------------------------------------------------------------------
##
## Copyright (C) 2018 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import libadcc
BLOCK_LABELS = ["s", "d", "t"]
class AmplitudeVector:
def __init__(self, *tensors):
"""Initialise an AmplitudeVector from some blocks"""
self.tensors = list(tensors)
# TODO Attach some information about this Amplitude, e.g.
# is it CVS?
def to_cpp(self):
"""
Return the C++ equivalent of this object.
This is needed at the interface to the C++ code.
"""
return libadcc.AmplitudeVector(tuple(self.tensors))
@property
def blocks(self):
return [BLOCK_LABELS[i] for i in range(len(self.tensors))]
def __getitem__(self, index):
if isinstance(index, int):
return self.tensors[index]
elif isinstance(index, str):
if index not in BLOCK_LABELS:
raise ValueError("Invalid index, either a block string "
"like s,d,t, ... or an integer index "
"are expected.")
return self.__getitem__(BLOCK_LABELS.index(index))
def __setitem__(self, index, item):
if isinstance(index, int):
self.tensors[index] = item
elif isinstance(index, str):
if index not in BLOCK_LABELS:
raise ValueError("Invalid index, either a block string "
"like s,d,t, ... or an integer index "
"are expected.")
return self.__setitem__(BLOCK_LABELS.index(index), item)
def copy(self):
"""Return a copy of the AmplitudeVector"""
return AmplitudeVector(*tuple(t.copy() for t in self.tensors))
def ones_like(self):
"""Return an empty AmplitudeVector of the same shape and symmetry"""
return AmplitudeVector(*tuple(t.ones_like() for t in self.tensors))
def empty_like(self):
"""Return an empty AmplitudeVector of the same shape and symmetry"""
return AmplitudeVector(*tuple(t.empty_like() for t in self.tensors))
def nosym_like(self):
"""Return an empty AmplitudeVector of the same shape and symmetry"""
return AmplitudeVector(*tuple(t.nosym_like() for t in self.tensors))
def zeros_like(self):
"""Return an AmplitudeVector of the same shape and symmetry with
all elements set to zero"""
return AmplitudeVector(*tuple(t.zeros_like() for t in self.tensors))
def add_linear_combination(self, scalars, others):
"""Return an AmplitudeVector of the same shape and symmetry with
all elements set to zero"""
if not isinstance(others, list):
raise TypeError("Other is expected to be a list")
if len(others) != len(scalars):
raise ValueError("Length of scalars and others lists do not agree.")
alltensors = [[av[b] for av in others] for b in self.blocks]
return AmplitudeVector(*tuple(
t.add_linear_combination(scalars, ot)
for t, ot in zip(self.tensors, alltensors)
))
def dot(self, other):
"""Return the dot product with another AmplitudeVector
or the dot products with a list of AmplitudeVectors.
In the latter case a np.ndarray is returned.
"""
if isinstance(other, list):
# Make a list where the first index is all singles parts,
# the second is all doubles parts and so on
alltensors = [[av[b] for av in other] for b in self.blocks]
return sum(t.dot(ots) for t, ots in zip(self.tensors, alltensors))
else:
return sum(t.dot(ot) for t, ot in zip(self.tensors, other.tensors))
def __matmul__(self, other):
if isinstance(other, AmplitudeVector):
return self.dot(other)
if isinstance(other, list):
if all(isinstance(elem, AmplitudeVector) for elem in other):
return self.dot(other)
return NotImplemented
def __forward_to_blocks(self, fname, other):
if isinstance(other, AmplitudeVector):
ret = tuple(getattr(t, fname)(ot)
for t, ot in zip(self.tensors, other.tensors))
else:
ret = tuple(getattr(t, fname)(other) for t in self.tensors)
if any(r == NotImplemented for r in ret):
return NotImplemented
else:
return AmplitudeVector(*ret)
def __mul__(self, other):
return self.__forward_to_blocks("__mul__", other)
def __rmul__(self, other):
return self.__forward_to_blocks("__rmul__", other)
def __add__(self, other):
return self.__forward_to_blocks("__add__", other)
def __sub__(self, other):
return self.__forward_to_blocks("__sub__", other)
def __truediv__(self, other):
return self.__forward_to_blocks("__truediv__", other)
def __imul__(self, other):
return self.__forward_to_blocks("__imul__", other)
def __iadd__(self, other):
return self.__forward_to_blocks("__iadd__", other)
def __isub__(self, other):
return self.__forward_to_blocks("__isub__", other)
def __itruediv__(self, other):
return self.__forward_to_blocks("__itruediv__", other)
def __repr__(self):
return "AmplitudeVector(blocks=" + ",".join(self.blocks) + ")"