In python, how do I create two index slicing for my own matrix class? -
I'm trying to write my own matrix class in Python, in fact, for testing purposes this matrix class Is in C ++ and I am using SWIG to interface between two. However, for this question, it may be easy to consider the pure Python implementation of the matrix class.
I want to be able to call this matrix class and want to use two-indexed slicing action. For example, after we created 4x4 matrix of people,
& gt; & Gt; & Gt; A = matrix (4,4,1) I want to be able to get sub2x2 matrix:
& gt; & Gt; & Gt; A [1: 2,1: 2] I have heard about the code <__> Hollis __ , but it seems that this only allows for single skiing Gives, e.g. One [1: 2] . How can I do a two-index skiing so that I can call A [i: j, l: k] ? Thank you!
Note that __ getslice __ dislikes so consider the following minimum example Do: square fu: def __getitem __ (auto, * args): print args f = foo () f [1: 2,2: 4] It will print:
((piece (1, 2, none), piece (2, 4, none)), < p> If you have a look at, you will see that there are piece objects: .. Slice is expanded to extend the slice. Special read-only attributes: start is bottom bound; paused is upper bound; step is the phase value; Everybody is left none These types of attributes can be of any type. Here it should be clear how to implement your 2 index slice handling.
Comments
Post a Comment