Skip to content

Trouble handling complex-valued variables #11

@ThomasA

Description

@ThomasA

For example in functions._soft_threshold, a complex-valued input vector z will not be handled correctly although comments in the code suggest that it should. The reason seems to be that in this line sz becomes real due to the np.abs:

sz = np.maximum(np.abs(z) - T, 0)

Later when the final value of sz gets assigned, the fact that sz is already a np.float64 (real) causes the imaginary part to be discarded. This is confirmed by a ComplexWarning: Casting complex values to real discards the imaginary part when running the function with a complex-valued z:

sz[:] = np.nan_to_num(1. * sz / (sz + T) * z)

This can be easily fixed by over-writing sz with the new value instead of indexing into it (sz[:]):

sz = np.nan_to_num(1. * sz / (sz + T) * z)

But are there other good reasons not to do this? Another solution could be to change line 89 to:

sz = np.maximum(np.abs(z) - T, 0, dtype = z.dtype)

I would like to hear your thoughts on this, as I am hoping to contribute changes to (at least parts of) PyUNLocBox to correctly support complex-valued variables.

Metadata

Metadata

Assignees

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