I think that
sage: x = M.list()
will do what you want.
I'm not sure why list(M) doesn't do "the right thing":
sage: list(M)
[(x1 + x2, x1*x2)]
To get what you want using list(), do
sage: x = list(M)
sage: f1=x[0][0]
sage: f2=x[0][1]
HTH
Justin
--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
--
They said it couldn't be done, but sometimes,
it doesn't work out that way.
- Casey Stengel
--
> On Dec 29, 2008, at 3:32 PM, Santanu Sarkar wrote:
>
>> I write a program in SAGE as follows:
>> R.<x1,x2>=QQ[]
>> M=matrix(R,1,2,[x1+x2,x1*x2])
>> may i do following steps to extract polynomials from matrix?
>> 1) x = list(M)
>> 2) f1 = x[0]
>> 3) f2 = x[1]
>> is f1 & f2 are polynomials?
>> if not how i can get them? please help me!
>
> I think that
>
> sage: x = M.list()
>
> will do what you want.
>
> I'm not sure why list(M) doesn't do "the right thing":
>
> sage: list(M)
> [(x1 + x2, x1*x2)]
list(M) gives a list of rows, not entries.
sage: R.<x1,x2>=QQ[]
sage: M=matrix(R,1,2,[x1+x2,x1*x2])
sage: M[0]
(x1 + x2, x1*x2)
sage: parent(M[0])
Ambient free module of rank 2 over the integral domain Multivariate
Polynomial Ring in x1, x2 over Rational Field
sage: list(M)[0]
(x1 + x2, x1*x2)
Same with indexing with using one index. Since matrices are two-
dimensional, this seems to be the "right thing" for me.
- Robert
>
> Maybe I missed the point here but after
The point was a minor one...
> R.<x1,x2>=QQ[]
> M=matrix(R,1,2,[x1+x2,x1*x2])
>
> you can get at the entries like this:
> sage: M[0,0]
> x1 + x2
> sage: M[0,1]
> x1*x2
For the OP, it was surprising that "list(M)" did not return a list of
the entries of M. The reason is that (for Python, I think), M is a
list of "rows". The expression "M.list()" does return a list of the
entries of M, which is what one might naively expect of the former
expression.
In the world of objects, its not always clear what the object is, I
guess.
Justin
--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
--
Democracy is two wolves and a lamb
voting on what to have for lunch.
Liberty is a well-armed lamb contesting
the vote.
If X is a python object, then list(X) literally makes the list list
X[0], X[1], etc., until the Python interpreter gets and IndexError.
It doesn't call "X.list()".
I think that list(X) and X.list() are different for matrices is an
unfortunate inconsistency. The best fix I can think of would be to
eliminate the list method entirely, and have a method
X.list_of_entries() or X.entries(). Just changing .list()'s
definition would be dangerous, since a lot of code probably depends on
the current definition. Changing list(X) to behave differently would
likely require making X[0] return the first entry of X rather than the
first row, which would be really confusing.
That X[0] returns the first row instead of the first column is a
completely arbitrary choice. It had to be made, so it was made.
William
+1 to (deprecating then removing) removing X.list(), and replacing it
with X.entries().
> That X[0] returns the first row instead of the first column is a
> completely arbitrary choice. It had to be made, so it was made.
I don't think it's quite arbitrary, M[a][b] == M[a,b] seems a lot
more natural than the other way, and the latter ordering, though
perhaps arbitrary, seems to be the more conventional one.
- Robert
Very good point. We *must* remember to make X.list() use the
deprecation warning system, and only remove it after 6 months. Could
you make a trac ticket?
>
>> That X[0] returns the first row instead of the first column is a
>> completely arbitrary choice. It had to be made, so it was made.
>
>
> I don't think it's quite arbitrary, M[a][b] == M[a,b] seems a lot
> more natural than the other way, and the latter ordering, though
> perhaps arbitrary, seems to be the more conventional one.
>
> - Robert
Very good point. It's just as arbitrary as the standard mathematical
notation $a_{ij}$ for the ij entry of a matrix. So it is consistant
with standard mathematical notation, which means it isn't "totally
arbitrary" after all.
William
When I was finishing this, I realized that matrices have a dict() method
which returns the entries in "sparse" format, as a dictionary. That
naturally suggests that m.list() should return the entries as a list.
Should we deprecate the dict() method too? Instead, we could have
m.entries() take an optional "sparse" keyword argument, so that
m.entries(sparse=True) returns a dictionary of the nonzero entries.
Thanks,
Jason