schtopps
schtopps

Reputation: 2849

python/numpy: Using own data structure with np.allclose() ? Where to look for the requirements / what are they?

I'm implementing a Matrix Product State class, which is some kind of special tensor decomposition scheme in python/numpy for fast algorithm prototyping.

I don't think that there already is such a thing out there, and I want to do it myself to get a proper understanding of the scheme.

What I want to have is that, if I store a given tensor T in this format as T_mps, I can access the reconstructed elements by T_mps[ [i0, i1, ..., iL] ]. This is achieved by the getitem(self, key) method and works fine.

Now I want to use numpy.allclose(T, mps_T) to see if my decomposition is correct.

But when I do this I get a type error for my own type:

TypeError: function not supported for these types, and can't coerce safely to supported types

I looked at the documentation of allclose and there it is said, that the function works for "array like" objects. Now, what is this "array like" concept and where can I find its specification ?

Maybe I'm better off, implementing my own allclose method ? But that would somewhat be reinventing the wheel, wouldn't it ?

Appreciate any help Thanks in advance

Upvotes: 1

Views: 477

Answers (1)

Robert Kern
Robert Kern

Reputation: 13430

The term "arraylike" is used in the numpy documentation to mean "anything that can be passed to numpy.asarray() such that it returns an appropriate numpy.ndarray." Most sequences with proper __len__() and __getitem__() methods work okay. Note that the __getitem__(i) must be able to accept a single integer index in range(len(self)), not just a list of indices as you seem to indicate. The result from this __getitem__(i) must either be an atomic value that numpy knows about, like a float or an int, or be another sequence as above. Without more details about your Matrix Product State implementation, that's about all I can help.

Upvotes: 3

Related Questions