mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 03:53:41 +02:00
feat: linalg.einsum now works with real, complex and float matrices
This commit is contained in:
parent
fe1aeb5354
commit
3f6703ad6a
1 changed files with 71 additions and 40 deletions
|
@ -174,35 +174,6 @@ def matmul(*operands):
|
||||||
return derived_array(multi_dot, operands)
|
return derived_array(multi_dot, operands)
|
||||||
|
|
||||||
|
|
||||||
def _exp_to_jack(matrix):
|
|
||||||
base_matrix = np.empty_like(matrix)
|
|
||||||
for index, entry in np.ndenumerate(matrix):
|
|
||||||
base_matrix[index] = entry.export_jackknife()
|
|
||||||
return base_matrix
|
|
||||||
|
|
||||||
|
|
||||||
def _imp_from_jack(matrix, name, idl):
|
|
||||||
base_matrix = np.empty_like(matrix)
|
|
||||||
for index, entry in np.ndenumerate(matrix):
|
|
||||||
base_matrix[index] = import_jackknife(entry, name, [idl])
|
|
||||||
return base_matrix
|
|
||||||
|
|
||||||
|
|
||||||
def _exp_to_jack_c(matrix):
|
|
||||||
base_matrix = np.empty_like(matrix)
|
|
||||||
for index, entry in np.ndenumerate(matrix):
|
|
||||||
base_matrix[index] = entry.real.export_jackknife() + 1j * entry.imag.export_jackknife()
|
|
||||||
return base_matrix
|
|
||||||
|
|
||||||
|
|
||||||
def _imp_from_jack_c(matrix, name, idl):
|
|
||||||
base_matrix = np.empty_like(matrix)
|
|
||||||
for index, entry in np.ndenumerate(matrix):
|
|
||||||
base_matrix[index] = CObs(import_jackknife(entry.real, name, [idl]),
|
|
||||||
import_jackknife(entry.imag, name, [idl]))
|
|
||||||
return base_matrix
|
|
||||||
|
|
||||||
|
|
||||||
def jack_matmul(*operands):
|
def jack_matmul(*operands):
|
||||||
"""Matrix multiply both operands making use of the jackknife approximation.
|
"""Matrix multiply both operands making use of the jackknife approximation.
|
||||||
|
|
||||||
|
@ -215,6 +186,31 @@ def jack_matmul(*operands):
|
||||||
For large matrices this is considerably faster compared to matmul.
|
For large matrices this is considerably faster compared to matmul.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def _exp_to_jack(matrix):
|
||||||
|
base_matrix = np.empty_like(matrix)
|
||||||
|
for index, entry in np.ndenumerate(matrix):
|
||||||
|
base_matrix[index] = entry.export_jackknife()
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
|
def _imp_from_jack(matrix, name, idl):
|
||||||
|
base_matrix = np.empty_like(matrix)
|
||||||
|
for index, entry in np.ndenumerate(matrix):
|
||||||
|
base_matrix[index] = import_jackknife(entry, name, [idl])
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
|
def _exp_to_jack_c(matrix):
|
||||||
|
base_matrix = np.empty_like(matrix)
|
||||||
|
for index, entry in np.ndenumerate(matrix):
|
||||||
|
base_matrix[index] = entry.real.export_jackknife() + 1j * entry.imag.export_jackknife()
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
|
def _imp_from_jack_c(matrix, name, idl):
|
||||||
|
base_matrix = np.empty_like(matrix)
|
||||||
|
for index, entry in np.ndenumerate(matrix):
|
||||||
|
base_matrix[index] = CObs(import_jackknife(entry.real, name, [idl]),
|
||||||
|
import_jackknife(entry.imag, name, [idl]))
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
if any(isinstance(o.flat[0], CObs) for o in operands):
|
if any(isinstance(o.flat[0], CObs) for o in operands):
|
||||||
name = operands[0].flat[0].real.names[0]
|
name = operands[0].flat[0].real.names[0]
|
||||||
idl = operands[0].flat[0].real.idl[name]
|
idl = operands[0].flat[0].real.idl[name]
|
||||||
|
@ -251,12 +247,40 @@ def einsum(subscripts, *operands):
|
||||||
Obs valued.
|
Obs valued.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if any(isinstance(o.flat[0], CObs) for o in operands):
|
def _exp_to_jack(matrix):
|
||||||
name = operands[0].flat[0].real.names[0]
|
base_matrix = []
|
||||||
idl = operands[0].flat[0].real.idl[name]
|
for index, entry in np.ndenumerate(matrix):
|
||||||
else:
|
base_matrix.append(entry.export_jackknife())
|
||||||
name = operands[0].flat[0].names[0]
|
return np.asarray(base_matrix).reshape(matrix.shape + base_matrix[0].shape)
|
||||||
idl = operands[0].flat[0].idl[name]
|
|
||||||
|
def _exp_to_jack_c(matrix):
|
||||||
|
base_matrix = []
|
||||||
|
for index, entry in np.ndenumerate(matrix):
|
||||||
|
base_matrix.append(entry.real.export_jackknife() + 1j * entry.imag.export_jackknife())
|
||||||
|
return np.asarray(base_matrix).reshape(matrix.shape + base_matrix[0].shape)
|
||||||
|
|
||||||
|
def _imp_from_jack(matrix, name, idl):
|
||||||
|
base_matrix = np.empty(shape=matrix.shape[:-1], dtype=object)
|
||||||
|
for index in np.ndindex(matrix.shape[:-1]):
|
||||||
|
base_matrix[index] = import_jackknife(matrix[index], name, [idl])
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
|
def _imp_from_jack_c(matrix, name, idl):
|
||||||
|
base_matrix = np.empty(shape=matrix.shape[:-1], dtype=object)
|
||||||
|
for index in np.ndindex(matrix.shape[:-1]):
|
||||||
|
base_matrix[index] = CObs(import_jackknife(matrix[index].real, name, [idl]),
|
||||||
|
import_jackknife(matrix[index].imag, name, [idl]))
|
||||||
|
return base_matrix
|
||||||
|
|
||||||
|
for op in operands:
|
||||||
|
if isinstance(op.flat[0], CObs):
|
||||||
|
name = op.flat[0].real.names[0]
|
||||||
|
idl = op.flat[0].real.idl[name]
|
||||||
|
break
|
||||||
|
elif isinstance(op.flat[0], Obs):
|
||||||
|
name = op.flat[0].names[0]
|
||||||
|
idl = op.flat[0].idl[name]
|
||||||
|
break
|
||||||
|
|
||||||
conv_operands = []
|
conv_operands = []
|
||||||
for op in operands:
|
for op in operands:
|
||||||
|
@ -267,15 +291,22 @@ def einsum(subscripts, *operands):
|
||||||
else:
|
else:
|
||||||
conv_operands.append(op)
|
conv_operands.append(op)
|
||||||
|
|
||||||
result = np.einsum(subscripts, *conv_operands)
|
tmp_subscripts = ','.join([o + '...' for o in subscripts.split(',')])
|
||||||
|
extended_subscripts = '->'.join([o + '...' for o in tmp_subscripts.split('->')[:-1]] + [tmp_subscripts.split('->')[-1]])
|
||||||
|
jack_einsum = np.einsum(extended_subscripts, *conv_operands)
|
||||||
|
|
||||||
if result.dtype == complex:
|
if jack_einsum.dtype == complex:
|
||||||
return _imp_from_jack_c(result, name, idl)
|
result = _imp_from_jack_c(jack_einsum, name, idl)
|
||||||
elif result.dtype == float:
|
elif jack_einsum.dtype == float:
|
||||||
return _imp_from_jack(result, name, idl)
|
result =_imp_from_jack(jack_einsum, name, idl)
|
||||||
else:
|
else:
|
||||||
raise Exception("Result has unexpected datatype")
|
raise Exception("Result has unexpected datatype")
|
||||||
|
|
||||||
|
if result.shape == ():
|
||||||
|
return result.flat[0]
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def inv(x):
|
def inv(x):
|
||||||
"""Inverse of Obs or CObs valued matrices."""
|
"""Inverse of Obs or CObs valued matrices."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue