mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 03:53:41 +02:00
feat: einsum function added to linalg module
This commit is contained in:
parent
6bc8102f87
commit
fe1aeb5354
1 changed files with 38 additions and 0 deletions
|
@ -239,6 +239,44 @@ def jack_matmul(*operands):
|
||||||
return _imp_from_jack(r, name, idl)
|
return _imp_from_jack(r, name, idl)
|
||||||
|
|
||||||
|
|
||||||
|
def einsum(subscripts, *operands):
|
||||||
|
"""Wrapper for numpy.einsum
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
subscripts : str
|
||||||
|
Subscripts for summation (see numpy documentation for details)
|
||||||
|
operands : numpy.ndarray
|
||||||
|
Arbitrary number of 2d-numpy arrays which can be real or complex
|
||||||
|
Obs valued.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if any(isinstance(o.flat[0], CObs) for o in operands):
|
||||||
|
name = operands[0].flat[0].real.names[0]
|
||||||
|
idl = operands[0].flat[0].real.idl[name]
|
||||||
|
else:
|
||||||
|
name = operands[0].flat[0].names[0]
|
||||||
|
idl = operands[0].flat[0].idl[name]
|
||||||
|
|
||||||
|
conv_operands = []
|
||||||
|
for op in operands:
|
||||||
|
if isinstance(op.flat[0], CObs):
|
||||||
|
conv_operands.append(_exp_to_jack_c(op))
|
||||||
|
elif isinstance(op.flat[0], Obs):
|
||||||
|
conv_operands.append(_exp_to_jack(op))
|
||||||
|
else:
|
||||||
|
conv_operands.append(op)
|
||||||
|
|
||||||
|
result = np.einsum(subscripts, *conv_operands)
|
||||||
|
|
||||||
|
if result.dtype == complex:
|
||||||
|
return _imp_from_jack_c(result, name, idl)
|
||||||
|
elif result.dtype == float:
|
||||||
|
return _imp_from_jack(result, name, idl)
|
||||||
|
else:
|
||||||
|
raise Exception("Result has unexpected datatype")
|
||||||
|
|
||||||
|
|
||||||
def inv(x):
|
def inv(x):
|
||||||
"""Inverse of Obs or CObs valued matrices."""
|
"""Inverse of Obs or CObs valued matrices."""
|
||||||
return _mat_mat_op(anp.linalg.inv, x)
|
return _mat_mat_op(anp.linalg.inv, x)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue