Commit cfe786b1 authored by Viacheslav Ostroukh's avatar Viacheslav Ostroukh 🚲
Browse files

conjugate array-like in herm_conj

If value passed to `herm_conj()` function can't be conjugated as an
array or a number, attempt to convert it to a tinyarray is performed.
In that way array-like list of lists/tuples can also be conjugated
correctly.

Fixes #406
parent 157fc88f
Pipeline #74785 passed with stages
in 8 minutes and 21 seconds
......@@ -346,3 +346,10 @@ Changes in Kwant 1.4.2
1.2.6. SInce Qsymm is an optional dependency of Kwant, this
backwards-incompatible change only affects Kwant users who use it.
- Minor bugs were fixed in the KPM module.
Changes after Kwant 1.4.2
-------------------------
- Builder properly handles Hermitian conjugation of array-like values.
Before, if the value was provided not as a true array but, for example, as a
list of lists, it was not conjugated, which could lead to generation of a
non-Hermitian Hamiltonian matrix without emitting any error.
......@@ -13,6 +13,7 @@ import collections
import copy
from functools import total_ordering, wraps, update_wrapper
from itertools import islice, chain
import numbers
import inspect
import tinyarray as ta
import numpy as np
......@@ -499,8 +500,27 @@ def herm_conj(value):
"""
if hasattr(value, 'conjugate'):
value = value.conjugate()
if hasattr(value, 'transpose'):
elif not isinstance(value, numbers.Number):
# Fallback for the case of array-like: see issue 406
# https://gitlab.kwant-project.org/kwant/kwant/-/issues/406
# If we get a list of lists or another array-like, conjugate()
# attribute won't be present. To be able to conjugate them, we try to
# convert them to tinyarrays
try:
value = ta.array(value).conjugate()
except Exception:
pass
if hasattr(value, 'shape'):
if len(value.shape) > 2:
is_ta = isinstance(value, (
ta.ndarray_int, ta.ndarray_float, ta.ndarray_complex))
value = np.swapaxes(value, -1, -2)
if is_ta:
value = ta.array(value)
else:
value = value.transpose()
return value
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment