/
functions.py
187 lines (156 loc) · 5.03 KB
/
functions.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/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 Lesser 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
from .AmplitudeVector import AmplitudeVector
import libadcc
def dot(a, b):
"""
Form the scalar product between two tensors.
"""
return a.dot(b)
def copy(a):
"""
Return a copy of the input tensor.
"""
return a.copy()
def transpose(a, axes=None):
"""
Return the transpose of a tensor as a *copy*.
If axes is not given all axes are reversed.
Else the axes are expect as a tuple of indices,
e.g. (1,0,2,3) will permute first two axes in the
returned tensor.
"""
if axes:
return a.transpose(axes)
else:
return a.transpose()
def empty_like(a):
"""
Return an empty tensor of the same shape and symmetry as
the input tensor.
"""
return a.empty_like()
def zeros_like(a):
"""
Return a zero tensor of the same shape and symmetry as
the input tensor.
"""
return a.zeros_like()
def ones_like(a):
"""
Return tensor of the same shape and symmetry as
the input tensor, but initialised to 1,
that is the canonical blocks are 1 and the
other ones are symmetry-equivalent (-1 or 0)
"""
return a.ones_like()
def nosym_like(a):
"""
Return tensor of the same shape, but without the
symmetry setup of the input tensor.
"""
return a.nosym_like()
def contract(contraction, a, b, out=None):
"""
Form a single, einsum-like contraction, that is contract
tensor a and be to form out via a contraction defined
by the first argument string, e.g. "ab,bc->ac"
or "abc,bcd->ad".
Note: The contract function is experimental. Its interface can change
and the function may disappear in the future.
"""
if out is None:
return libadcc.contract(contraction, a, b)
else:
return libadcc.contract_to(contraction, a, b, out)
def add(a, b, out=None):
"""
Return the elementwise sum of two objects
If out is given the result will be written to the
latter tensor.
"""
if out is None:
return a + b
if isinstance(a, AmplitudeVector):
for block in a.blocks:
add(a[block], b[block], out[block])
else:
return libadcc.add(a, b, out)
def subtract(a, b, out=None):
"""
Return the elementwise difference of two objects
If out is given the result will be written to the
latter tensor.
"""
if out is None:
return a - b
if isinstance(a, AmplitudeVector):
for block in a.blocks:
subtract(a[block], b[block], out[block])
else:
return libadcc.subtract(a, b, out)
def multiply(a, b, out=None):
"""
Return the elementwise product of two objects
If out is given the result will be written to the
latter tensor.
Note: If out is not given, the symmetry of the
contained objects will be destroyed!
"""
if out is None:
return a * b
if isinstance(a, AmplitudeVector):
for block in a.blocks:
multiply(a[block], b[block], out[block])
else:
return libadcc.multiply(a, b, out)
def divide(a, b, out=None):
"""
Return the elementwise division of two objects
If out is given the result will be written to the
latter tensor.
Note: If out is not given, the symmetry of the
contained objects will be destroyed!
"""
if out is None:
return a / b
if isinstance(a, AmplitudeVector):
for block in a.blocks:
divide(a[block], b[block], out[block])
else:
return libadcc.divide(a, b, out)
def linear_combination(coefficients, tensors):
"""
Form a linear combination from a list of tensors.
If coefficients is a 1D array, just form a single
linear combination, else return a list of vectors
representing the linear combination by reading
the coefficients row-by-row.
"""
if len(tensors) == 0:
raise ValueError("List of tensors cannot be empty")
if len(tensors) != len(coefficients):
raise ValueError("Number of coefficient values does not match "
"number of tensors.")
ret = zeros_like(tensors[0])
return ret.add_linear_combination(coefficients, tensors)