Skip to content

improve .values() on meshes #958

@the-hampel

Description

@the-hampel

After adding the recent feature to obtain a np.array of all mesh values via .values() (81abab7 and 1ba2695) it would be helpful to improve the functionality for MeshImFreq further. Right now calling .values() on a MeshImFreq gives back an array of mesh points:

mesh = MeshImFreq(beta=40, statistic='Fermion', n_iw=2)
print(mesh.values())
mesh_cmplx = 
> [MatsubaraFreq(n: -2, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: -1, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: 0, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: 1, beta: 40.0, statistic: Fermion)]

It would be helpful to return back directly a list of complex numbers. This can right now be achieved only via:

np.vectorize(lambda x: x.imag)(mesh.values())
> array([-0.235619, -0.07854 ,  0.07854 ,  0.235619])

Calling .imag directly on the array does not work, as numpy does not know how to use the dispatcher on a triqs mesh point object. I tried to find a solution for this on the python level but it seems no so straight forward to teach numpy to do this. If someone knows this would be the easiest! Other options:
(a) create a class that inherits from np.ndarray that is returned when .values() is called. Then this new class has a member attribute called .imag that will just do the np.vectorize call.
(b) implement a real C++ function that returns the values as an array (right now this only lives in the meshes_desc file) and add an optional keyword that switches between returning meshpoints or cmplx numbers, i.e. something like this:

print(mesh.values(return_mesh_points=True))
> [MatsubaraFreq(n: -2, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: -1, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: 0, beta: 40.0, statistic: Fermion) MatsubaraFreq(n: 1, beta: 40.0, statistic: Fermion)]
print(mesh.values(return_mesh_points=False))
> array([-0.235619, -0.07854 ,  0.07854 ,  0.235619])

where the default can be to return mesh points.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions