I want to create a numpy subclass specialized for symmetric matrices. I want it to be just like any numpy array with only 2 differences:
- It will have 2 fields for its eigendecomposition:
U,D. - On construction it will force symmetry.
Likemy_symmetric_arr() = 0.5*(m+m.T)wheremis the regular matrix created by the default constructor.
How can that be done in the simplest way?
Subclassing numpy arrays can be tricky, but fortunately is something you do only once in a while. Numpy documentation provides a good resource on the topic there
Here I give you an example from which you can build a symmetric array from an input array, and when you assign to slices of the array it will also assign the transposed slice
Here an example usage
That prints the following arrays
For the eigendecomposition I wouldn't keep that as an attribute of the array, numpy already provides the eigh for symmetric eigendecomposition.