[sage-devel] Re: multivariate polynomial coefficients

2007-10-13 Thread Joel B. Mohler

Thanks for the discussion about this topic.  I send this mail to re-iterate 
and summarize.  It seems there are two things that you might want:
1)  Get the coefficient of a specific monomial in the multivariate polynomial 
ring.
2)  Get the coefficient of the polynomial in a tower of (two) polynomial 
extensions

I suggest that MPolynomial.coefficient method perform function #1 above.  It 
currently does some strange things over the base rings I've tested.  It does 
vary on base ring due to implementation differences.

I suggest that there be a new method which performs function #2 and I suggest 
it be called polynomial_coefficient, but I really don't like that name.  I 
almost prefer coefficient_polynomial since it will sort beside coefficient 
in documentation and help people realize that if they are not happy 
with coefficient that they have an alternative.  Of course, each method's 
documentation should probably mention the other.

I'm wondering if we could have a vote on preferred syntax.  I'm not going to 
describe the parameters because if they are not clear enough from context, it 
probably isn't a good parameter choice :) :

sage: P.v,w,x,y,z=ZZ[]
sage: f=(1-v)*(1-2*w)*(1-3*x); f
-6*v*w*x + 2*v*w + 3*v*x + 6*w*x - v - 2*w - 3*x + 1
##
#  Alternative Number 1
##
sage: f.polynomial_coefficient({w:0,v:1})
3*x - 1
##
#  Alternative Number 2
##
sage: f.polynomial_coefficient([x,y,z],v)
3*x - 1
##
#  Alternative Number 3
#  Overloading of coefficient to handle both functionalities
##
sage: f.coefficient(v,base=ZZ['x,y,z'])
3*x - 1
sage: f.coefficient(v)  # here's an example of functionality #1 above
-1

Those are listed in the order of my preference for the interface.  It feels to 
me that Alternative Number 3 would not be much fun to implement (and 
involve coercions that some view as suspect).  I basically 
implemented Alternative Number 1 as a function in my own program and it 
feels like a straightforward implementation just iterating through the 
polydict.  I'd love to have some other voters.

NOTE:  It is NOT sufficient to simply provide a monomial expression to 
the polynomial_coefficient method because it doesn't provide for the case 
when you explicitly want an exponent to be 0 -- this point seems to me to 
have caused a great deal of confusion in the current implementation.

--
Joel

--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread didier deshommes

2007/10/12, Joel B. Mohler [EMAIL PROTECTED]:
 sage: P.x,y,z=ZZ[]
 sage: f=x*y^2*z^3+y^2*z
 sage: f.coefficient(y,2,z,3)  # I want the coefficient of y^2*z^3
 # Bang

 That doesn't seem very nice to me.

Good point: Dictionary it is then. (Incidentally, there downs seem a
non-obvious way to do this in Maple).

didier


 --
 Joel

 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Soroosh Yazdani

Another *possible* way of sage behaving is
sage: P.x,y=ZZ[]
sage: f=x*y^2+x*y+y+x+1
sage: f.coefficient(y^2)
0
sage: f.coefficient(y^1)
1
sage: f.coefficient(y^0)
1

Now, I'm not sure if this is better or not, but I thought maybe I should
point it out. The biggest problem with this is that if you do want to extract 
the whole polynomial coefficient, you will have some issues. I'm guessing
the proposal for dictionary will resolve this nicely.

Soroosh

On Fri, Oct 12, 2007 at 10:36:29AM -0400, Joel B. Mohler wrote:
 This e-mail is too long.  Here's the bottom line:  I suggest that the 
 coefficient method on a multivariate polynomial ring take a dictionary 
 indicating the variables and degrees that you want to restrict your attention 
 to.
 
 It seems that the multivariate polynomial coefficient function is a bit 
 inflexible (and inconsistent).  I'm looking for some insight about how to 
 think about the following things.
 
 sage: P.x,y=ZZ[]
 sage: f=x*y^2+x*y+y+x+1
 sage: f.coefficient(y^2)
 x
 sage: f.coefficient(y^1)
 x + 1
 sage: f.coefficient(y^0)
 1
 
 I realize that y^0 == 1 so that the last line is returning the constant 
 coefficient (and the implication that y is special to me the user is totally 
 unseen by the coefficient method).  But, the logic seems a bit inconsistent.  
 I'd suggest that this next line work:
 
 sage: f.coefficient({y:0})
 x + 1
 
 Does anyone have any objection?  It actually seems like a dictionary of 
 variables whose exponents you want to control would make a much better 
 interface to this.  In any case, we can maintain both functionalities by 
 checking the type of the parameter.
 
 And another:
 
 sage: P.x,y=QQ[]  # DIFFERENT BASE RING THAN LAST TIME
 sage: f=x*y^2+x*y+y+x+1
 sage: f.coefficient(y^0)  # DIFFERENT OUTPUT
 x*y^2 + x*y + x + y + 1
 
 What in the world is going on in that last line?  (Hmm, I think that it is 
 getting a 1 so it is thinking that no variables are restricted -- that's 
 actually consistent, but seems mighty strange.)
 
 Does anyone else agree?  Or, have a better suggestion?
 
 --
 Joel
 
 

--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Joel B. Mohler

On Friday 12 October 2007 11:41, didier deshommes wrote:
 But:
  coeff(f,y,0);

                                               x + 1

 returns the right answer

 Actually I like Maple's notation better here over the dictionary
 notation you proposed: it is as intuitive and I have to type less
 curly braces to get the same result :)

Ok, that's a good reason, but what if I was working in something like this?:

sage: P.x,y,z=ZZ[]
sage: f=x*y^2*z^3+y^2*z
sage: f.coefficient(y,2,z,3)  # I want the coefficient of y^2*z^3
# Bang

That doesn't seem very nice to me.

--
Joel

--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread cwitty

On Oct 12, 7:36 am, Joel B. Mohler [EMAIL PROTECTED] wrote:
 This e-mail is too long.  Here's the bottom line:  I suggest that the
 coefficient method on a multivariate polynomial ring take a dictionary
 indicating the variables and degrees that you want to restrict your attention
 to.

Sounds like a great idea.

 Does anyone have any objection?  It actually seems like a dictionary of
 variables whose exponents you want to control would make a much better
 interface to this.  In any case, we can maintain both functionalities by
 checking the type of the parameter.

I would actually slightly prefer to remove the previous interface.  It
seems very hard to use; worse, it seems like it would be easy to
accidentally write buggy code using the old interface.

(I actually have code that finds f.coefficient({y:0}) by starting with
f and subtracting out all larger coefficients of y.)

Carl


--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Joel B. Mohler

On Friday 12 October 2007 13:36, Mike Hansen wrote:
 If you're doing a dictionary anyway, doesn't it make more sense to use
 **kwargs?  For example,

 sage: P.x,y=ZZ[]
 sage: f=x*y^2+x*y+y+x+1
 sage: f.coefficient(y=2)
 x
 sage: f.coefficient(y=1)
 x + 1
 sage: f.coefficient(x=1, y=2)
 1

 It takes a little bit to get used to the semantics of it, but the
 syntax is much more natural / Pythonic.

Hmm, possibly.  kwargs feels scary to me with variables since the 'x' in the 
kwargs parameter list is a totally different 'x' than the one in P.gen(0).  
They just happened to be named the same in some different scope.  I think 
that allowing both methods might be ok.

Another more concrete reason is that I want to do something like this:

P=PolynomialRing(ZZ,'x',n)
d={}
d[P.gen(1)] = 1
d[P.gen(2)] = 2
poly.coefficient(d)

That is, I want to fill out my dictionary very dynamically.  I suppose there 
is probably a way to do this with kwargs, but it feels like a reach into some 
dark corner syntactically.

--
Joel

--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Mike Hansen

If you're doing a dictionary anyway, doesn't it make more sense to use
**kwargs?  For example,

sage: P.x,y=ZZ[]
sage: f=x*y^2+x*y+y+x+1
sage: f.coefficient(y=2)
x
sage: f.coefficient(y=1)
x + 1
sage: f.coefficient(x=1, y=2)
1

It takes a little bit to get used to the semantics of it, but the
syntax is much more natural / Pythonic.

--Mike

On 10/12/07, didier deshommes [EMAIL PROTECTED] wrote:

 2007/10/12, Joel B. Mohler [EMAIL PROTECTED]:
  sage: P.x,y,z=ZZ[]
  sage: f=x*y^2*z^3+y^2*z
  sage: f.coefficient(y,2,z,3)  # I want the coefficient of y^2*z^3
  # Bang
 
  That doesn't seem very nice to me.

 Good point: Dictionary it is then. (Incidentally, there downs seem a
 non-obvious way to do this in Maple).

 didier

 
  --
  Joel
 
  
 

 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread boothby




On Fri, 12 Oct 2007, Joel B. Mohler wrote:


 On Friday 12 October 2007 13:36, Mike Hansen wrote:
 If you're doing a dictionary anyway, doesn't it make more sense to use
 **kwargs?  For example,

 sage: P.x,y=ZZ[]
 sage: f=x*y^2+x*y+y+x+1
 sage: f.coefficient(y=2)
 x
 sage: f.coefficient(y=1)
 x + 1
 sage: f.coefficient(x=1, y=2)
 1

 It takes a little bit to get used to the semantics of it, but the
 syntax is much more natural / Pythonic.

 Hmm, possibly.  kwargs feels scary to me with variables since the 'x' in the
 kwargs parameter list is a totally different 'x' than the one in P.gen(0).
 They just happened to be named the same in some different scope.  I think
 that allowing both methods might be ok.

... I don't understand what you mean by this.  When you construct a polynomial 
ring, each variable gets a name, and that name is (AFAIK) immutable.

If an argument doesn't correspond to an entry in R.variable_names(), then an 
exception should be raised.




 Another more concrete reason is that I want to do something like this:

 P=PolynomialRing(ZZ,'x',n)
 d={}
 d[P.gen(1)] = 1
 d[P.gen(2)] = 2
 poly.coefficient(d)

 That is, I want to fill out my dictionary very dynamically.  I suppose there
 is probably a way to do this with kwargs, but it feels like a reach into some
 dark corner syntactically.

 --
 Joel

 




--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Nils Bruin

Both the poly.coefficients({x:1,y:2}) and poly.coefficients(x=1,y=2)
seem confusing to me (the latter one downright scary. Exponents and
variable names have no business being on opposite sides of an equality
sign). In mathematical terms, what you want to do is view the
polynomial ring k[x,y,z] as the ring k[z][x,y] and ask for the
coefficient of the monomial x*y^2.
Wouldn't it be clearer to have a call like

poly.polynomial_coefficient({z},x*y^2) ?

i.e., give the sum of the terms in poly that are of the form
z^e*x*y^2 ? In this setting,

(x^2*y^2+z*x*y^2+x*y).polynomial_coefficient({z,y},x*y^2) == y+z

which may be useful. polynomial_coefficient seems an ugly name to me,
but this interface is a lot easier to explain and much more explicit
about what the routine does.


--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread didier deshommes

2007/10/12, Joel B. Mohler [EMAIL PROTECTED]:

 This e-mail is too long.  Here's the bottom line:  I suggest that the
 coefficient method on a multivariate polynomial ring take a dictionary
 indicating the variables and degrees that you want to restrict your attention
 to.

 It seems that the multivariate polynomial coefficient function is a bit
 inflexible (and inconsistent).  I'm looking for some insight about how to
 think about the following things.

 sage: P.x,y=ZZ[]
 sage: f=x*y^2+x*y+y+x+1
 sage: f.coefficient(y^2)
 x
 sage: f.coefficient(y^1)
 x + 1
 sage: f.coefficient(y^0)
 1

 I realize that y^0 == 1 so that the last line is returning the constant
 coefficient (and the implication that y is special to me the user is totally
 unseen by the coefficient method).  But, the logic seems a bit inconsistent.
 I'd suggest that this next line work:

 sage: f.coefficient({y:0})
 x + 1

+1.

Interestingly enough, Maple has the same limitation when you pass the
exponent to it directly:
 coeff(f,y^0);
Error, invalid input: coeff received 1, which is not valid for its 2nd
argument, x

But:
 coeff(f,y,0);
  x + 1

returns the right answer

Actually I like Maple's notation better here over the dictionary
notation you proposed: it is as intuitive and I have to type less
curly braces to get the same result :)

didier

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sage-devel group.
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-devel?hl=en
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread cwitty

On Oct 12, 12:27 pm, [EMAIL PROTECTED] wrote:
 On Fri, 12 Oct 2007, Joel B. Mohler wrote:
  Hmm, possibly.  kwargs feels scary to me with variables since the 'x' in the
  kwargs parameter list is a totally different 'x' than the one in P.gen(0).
  They just happened to be named the same in some different scope.  I think
  that allowing both methods might be ok.

 ... I don't understand what you mean by this.  When you construct a 
 polynomial ring, each variable gets a name, and that name is (AFAIK) 
 immutable.

I think I understand.  It just feels cleaner if variables are
variables, and strings are strings, and the two only meet during
parsing and printing (and not necessarily even during parsing).

For me, this is partly an aesthetic issue, but there are practical
consequences as well.  If some ways of dealing with polynomials
require variable names as strings, then there's no way of dealing with
this:

sage: QQ['x']['x']['x', 'x']
Multivariate Polynomial Ring in x, x over Univariate Polynomial Ring
in x over Univariate Polynomial Ring in x over Rational Field
sage: QQ['x']['x']['x', 'x'].random_element()
((-4*x^2 + x + 2)*x^2 + (2*x^2 + 8*x - 1/2)*x + 1/6*x^2 + 2*x + 1/4)*x
+ ((1/7*x^2 - 2*x - 1)*x^2 + (x^2 + x + 1/10)*x + 1/6*x^2 + 2*x + 1)*x
+ (1/2*x^2 + x + 1)*x^2 + (-2*x^2 + 9*x - 1)*x + 1/2*x^2 + x + 3/2

That's an absurd example, of course, but there are more important
issues as well.  Suppose you have an algorithm that takes a polynomial
in an arbitrary multivariate ring, and your algorithm needs to lift
the polynomial to a ring with one more variable, do some computations
there, and then drop back to the original ring.  What to name the new
variable?  If names are unimportant, and the identity of the variable
object is all that matters, then it's easy...just always name the new
variable z.  However, if names are vital, then it's important to
make sure the name doesn't clash with any names used in the current
polynomial ring (or its base rings, recursively).

However, given all that, I think separating variables and their names
is a lost cause in SAGE.  Polynomials must have distinct, sensible
variable names, or else they cannot be shared with Pari, or with any
subprocess-based interface.  There's already code all through SAGE
that maps back and forth between variables and their names with
abandon, so adding one more place won't hurt much.

There is one more issue; I wonder if cutting down on the number of
places we need to map from strings to variables would make things
faster?

Carl


--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Joel B. Mohler

On Friday 12 October 2007 15:52, Nils Bruin wrote:
 Both the poly.coefficients({x:1,y:2}) and poly.coefficients(x=1,y=2)
 seem confusing to me (the latter one downright scary. Exponents and
 variable names have no business being on opposite sides of an equality
 sign). 

Yes, I agree, mathematically it appears risky.

 In mathematical terms, what you want to do is view the 
 polynomial ring k[x,y,z] as the ring k[z][x,y] and ask for the
 coefficient of the monomial x*y^2.
 Wouldn't it be clearer to have a call like
 poly.polynomial_coefficient({z},x*y^2) ?

I think this confuses me.  What is the {z} indicating?

Suppose I had a poly ring with 19 variables and one of them was named y.  
How would I get the coefficient for y^0 in your syntax?  (That is, the 
constant term in k[y][...].)  This is the sticking point in what is currently 
implemented.

--
Joel

--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---



[sage-devel] Re: multivariate polynomial coefficients

2007-10-12 Thread Nils Bruin



On Oct 12, 1:25 pm, Joel B. Mohler [EMAIL PROTECTED] wrote:
[...]
 Suppose I had a poly ring with 19 variables and one of them was named y.
 How would I get the coefficient for y^0 in your syntax?  (That is, the
 constant term in k[y][...].)  This is the sticking point in what is currently
 implemented.

No, that would be the constant term in k[...][y]. The syntax would
then be:

poly.coeff({ a for a in poly.parent().generators() if a != y }, 1)

(i.e., all variables except y are allowed)

except that python doesn't like set comprehension (why not? is there
an unordered list type with quick membership test?), so we might want
to make it a list instead:

poly.coeff([ a for a in poly.parent().generators() if a != y ], 1)

If that is the most common usage scenario you have in mind, then this
notation might be a tad wasteful. Would surgery on poly.dict() solve
your problems?

Incidentally, poly.coefficient does have documentation. The behaviour
you describe is a bug relative to that description. The alternative
behaviour that you describe, nor mine, is in line with the definition
given there either.



--~--~-~--~~~---~--~~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~--~~~~--~~--~--~---