Polynomial Problem

6 views
Skip to first unread message

Santanu Sarkar

unread,
Dec 29, 2008, 3:32:01 PM12/29/08
to sage-s...@googlegroups.com
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!

Justin Walker

unread,
Dec 29, 2008, 4:38:30 PM12/29/08
to sage-s...@googlegroups.com

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
--


Robert Bradshaw

unread,
Dec 29, 2008, 4:53:05 PM12/29/08
to sage-s...@googlegroups.com
On Dec 29, 2008, at 1:38 PM, Justin Walker wrote:

> 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

Justin Walker

unread,
Dec 29, 2008, 5:09:53 PM12/29/08
to sage-s...@googlegroups.com
Hmm...I see what you mean...

sage: N=matrix(R,2,1,[x1+x2,x1*x2])
sage: N
 
[x1 + x2]
[  x1*x2]
sage: y=list(N)
sage: y
 [(x1 + x2), (x1*x2)]

Curiouser and curiouser...

I guess one man's "object" is another man's ...

Is there some reason that "rows" trump "columns"?   Is this an artifact of implementation, e.g., a matrix is a list of rows?

Should "list(obj)" do "obj.list()"?

I'm more curious than complaining; seems a bit counterintuitive to me.

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
-----------
Like the ski resort full of girls hunting for husbands
and husbands hunting for girls, the situation is not
as symmetrical as it might seem.
  - Alan MacKay
--

John Cremona

unread,
Dec 29, 2008, 5:23:41 PM12/29/08
to sage-s...@googlegroups.com
Maybe I missed the point here but after

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

where the only non-obvious thing to a mathematician is that the
row/col indices start at at 0.

The list() discussion seems separate to me.

John Cremona

2008/12/29 Santanu Sarkar <sarkar.sa...@gmail.com>:

Justin Walker

unread,
Dec 29, 2008, 5:43:49 PM12/29/08
to sage-s...@googlegroups.com

On Dec 29, 2008, at 5:23 PM, John Cremona wrote:

>
> 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.

William Stein

unread,
Dec 29, 2008, 7:15:34 PM12/29/08
to sage-s...@googlegroups.com
On Mon, Dec 29, 2008 at 2:43 PM, Justin Walker <jus...@mac.com> wrote:
>
>
> On Dec 29, 2008, at 5:23 PM, John Cremona wrote:
>
>>
>> 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.
>

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

Robert Bradshaw

unread,
Dec 29, 2008, 7:34:07 PM12/29/08
to sage-s...@googlegroups.com

+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


William Stein

unread,
Dec 29, 2008, 7:47:49 PM12/29/08
to sage-s...@googlegroups.com
On Mon, Dec 29, 2008 at 4:34 PM, Robert Bradshaw
<robe...@math.washington.edu> wrote:
>
> +1 to (deprecating then removing) removing X.list(), and replacing it
> with X.entries().

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

Robert Bradshaw

unread,
Dec 29, 2008, 7:59:36 PM12/29/08
to sage-s...@googlegroups.com
On Dec 29, 2008, at 4:47 PM, William Stein wrote:

>
> On Mon, Dec 29, 2008 at 4:34 PM, Robert Bradshaw
> <robe...@math.washington.edu> wrote:
>>
>> +1 to (deprecating then removing) removing X.list(), and replacing it
>> with X.entries().
>
> 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?

Done. http://trac.sagemath.org/sage_trac/ticket/4889

Jason Grout

unread,
Jan 21, 2009, 8:04:45 PM1/21/09
to sage-s...@googlegroups.com
Robert Bradshaw wrote:
> On Dec 29, 2008, at 4:47 PM, William Stein wrote:
>
>> On Mon, Dec 29, 2008 at 4:34 PM, Robert Bradshaw
>> <robe...@math.washington.edu> wrote:
>>> +1 to (deprecating then removing) removing X.list(), and replacing it
>>> with X.entries().
>> 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?
>
> Done. http://trac.sagemath.org/sage_trac/ticket/4889


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

Reply all
Reply to author
Forward
0 new messages