I think you could achieve it by getting the contiguous parts and appending them together. So if you wanted the first and third columns of a Matrix, you could do
In [21]: m
Out[21]:
⎡0 1 2 3⎤
⎢ ⎥
⎢1 2 3 4⎥
⎢ ⎥
⎢2 3 4 5⎥
⎢ ⎥
⎣3 4 5 6⎦
In [22]: m[:,0].row_join(m[:, 2])
Out[22]:
⎡0 2⎤
⎢ ⎥
⎢1 3⎥
⎢ ⎥
⎢2 4⎥
⎢ ⎥
⎣3 5⎦
Is this what you wanted?
Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
It can be done by definition of new method in Matrix class, for example
(without checking input data):
In [1]: def extract(self, rowsList, colsList):
...: return Matrix(len(rowsList), len(colsList), lambda i,j:
self.mat[rowsList[i]*self.cols + colsList[j]])
In [5]: Matrix.extract = extract
Then we can use modification:
In [6]: m = Matrix(4, 3, lambda i, j: i*3 + j)
In [7]: m
Out[7]:
⎡0 1 2 ⎤
⎢ ⎥
⎢3 4 5 ⎥
⎢ ⎥
⎢6 7 8 ⎥
⎢ ⎥
⎣9 10 11⎦
In [8]: m.extract([0,1,3],[0,1])
Out[8]:
⎡0 1 ⎤
⎢ ⎥
⎢3 4 ⎥
⎢ ⎥
⎣9 10⎦
In [9]: m.extract([0,3],[0,2])
Out[9]:
⎡0 2 ⎤
⎢ ⎥
⎣9 11⎦
Aaron Meurer
It is in attachment now.
Some technical notes:
I have done it relative to "git://github.com/sympy/sympy.git" with the
aim of "git format-patch" command in this way:
> git clone git://github.com/sympy/sympy.git
> cd sympy
> git checkout -b matrix_extract
then I have implemented extract method in "sympy/matrices/matrices.py"
> ./bin/test
achieved that test_code_quality.py was not angry
> ./bin/doctest
There are many fails in doctest reported by the red, but not in matrices.py.
> git status
> git commit -a -m "implementation of extract method of matrices"
> git format-patch master --stdout >
extract_method_of_matrices_implemetation.patch
And send you this path created.
Is it right way?
12.01.2011 01:37, Aaron S. Meurer пишет:
Do not worry about the doctest failures. Those are because of issue 2041 (http://code.google.com/p/sympy/issues/detail?id=2041), which will be fixed *very* soon anyway (you can get around it currently by running the doctests in Python 2.5).
This is great. It even works if you give the rows or columns out of order, or if you duplicate them:
In [3]: m = Matrix(5, 6, lambda i, j: i + j)
In [4]: m
Out[4]:
⎡0 1 2 3 4 5⎤
⎢ ⎥
⎢1 2 3 4 5 6⎥
⎢ ⎥
⎢2 3 4 5 6 7⎥
⎢ ⎥
⎢3 4 5 6 7 8⎥
⎢ ⎥
⎣4 5 6 7 8 9⎦
In [6]: m.extract([0, 4, 3], [2, 3])
Out[6]:
⎡2 3⎤
⎢ ⎥
⎢6 7⎥
⎢ ⎥
⎣5 6⎦
In [7]: m.extract([0, 0, 3], [2, 3])
Out[7]:
⎡2 3⎤
⎢ ⎥
⎢2 3⎥
⎢ ⎥
⎣5 6⎦
One last thing: can you write a regular test for this? Just create a function test_extract() in test_matrices.py with assert statements like the others there, and test the functionality (you can test the exceptions by using the raises() function). ./bin/test matrices will run the test. Go ahead and just make it as another patch.
I am otherwise +1 on your patch, so once you add tests I can push it in.
Aaron Meurer
> <extract_method_of_matrices_implemetation.patch>
I have made the patch of the realization of function "test_extract()"
not separately. It's merged with the previous one ("extract()" method
patch), so two files are affected by it now.
12.01.2011 06:23, Aaron S. Meurer пишет:
Aaron Meurer
> <extract_method_of_matrices_with_test.patch>