Ask Your Question

Nicolas Essis-Breton's profile - activity

2019-10-20 13:58:14 +0100 received badge  Popular Question (source)
2016-09-25 19:51:47 +0100 received badge  Famous Question (source)
2013-10-03 19:44:34 +0100 received badge  Notable Question (source)
2013-03-26 09:04:01 +0100 received badge  Popular Question (source)
2012-10-23 07:35:57 +0100 commented answer Filtering an expression: keeping only term with even power

@achrzesz Very flexible approach. Thank you.

2012-10-23 07:34:50 +0100 marked best answer Filtering an expression: keeping only term with even power

Is that what you need?

var('x y');
p=(5*x^2*y^2+x*y^2+x^3*y+x^4*y^4).polynomial(QQ)          
sum([v[0]*v[1] for v in p if forall(v[1].exponents()[0],lambda x:is_even(x))[0]])
#x^4*y^4 + 5*x^2*y^2
2012-10-22 20:57:26 +0100 marked best answer Filtering an expression: keeping only term with even power

Here is an answer using the list command and some list comprehensions.

var('x y')
R.<x,y>=PolynomialRing(RR)
q=x^2*y^2+x+x^2*y+y^2
terms=list(q)
ans=[t[0]*t[1] for t in terms if (t[1].degree(x) % 2)==0 and (t[1].degree(y) % 2==0)]
sum(ans)

This will also keep the coefficients.

2012-10-22 20:42:54 +0100 commented answer Filtering an expression: keeping only term with even power

@archrzesz Exactly what I want, thank you. Is there a way to keep the coefficient of each monomial? For example, return $5 x^4 y^4$.

2012-10-22 19:26:25 +0100 asked a question Filtering an expression: keeping only term with even power

In the expression $$ x^2 y^2+x y^2+x^3y + 5 x^4 y^4 $$ I would like to keep only the term where the variables have even power $$ x^2y^2+5 x^4 y^4 $$

Is there a way to do it?

I look into this post.
But once I get the operands, I don't see how to analyze the variable exponent.

2012-01-29 00:55:07 +0100 received badge  Nice Question (source)
2011-12-21 20:18:47 +0100 commented answer Programming with Sage: defining module

@Shashank Thanks Shashank.

2011-12-21 20:17:38 +0100 marked best answer Programming with Sage: defining module

If result.sage is calling add function from addition.sage, you need to load that file in result.sage.

You can also load the file without giving the path, if you type the following two lines in the beginning of the notebook

import sys
sys.path.append('path-to-module')
2011-12-21 15:54:04 +0100 received badge  Editor (source)
2011-12-21 15:52:47 +0100 asked a question Programming with Sage: defining module

Let's say I want to write two modules 'addition.sage' and 'result.sage'.
I would like to import 'addition.sage' in 'result.sage'. How can I do this?
I've try the following, but it doesn't seem to work.

'addition.sage' file:
from sage.all import *
add(x,y)=x+y

'result.sage' file:
from addition import add
print add(1,2)

my sage session:
sage: load('myPath/result.sage')

This gives me the error 'ImportError: No module named addition'.
Although, my PYTHONPATH contains the directory where my two modules are located.

2011-12-20 10:37:42 +0100 commented answer Defining a function of the form $f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$

@DSM Thanks DSM, your answer is enlightening

2011-12-20 10:35:59 +0100 received badge  Supporter (source)
2011-12-20 10:35:56 +0100 marked best answer Defining a function of the form $f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$

You're getting the TypeError because you're double-assigning x: in your second line, you make it a list of variables, but in the third you redefine it as a symbolic variable. The f(x)=sum(x[i],i,0,n) line is translated as

sage: preparse("f(x) = sum(x[i], i, 0, n")
'__tmp__=var("x"); f = symbolic_expression(sum(x[i], i, Integer(0), n).function(x)'

Unfortunately we can't simply rename x to xx: the "f(x,y) = x+y" syntax requires explicit specification of the variables, and you can't pass it a list. So for a fixed n (which is the only case I really know how to handle offhand) I would probably do this instead:

sage: n = 2
sage: xx = [var('x_%d' % i) for i in range(n)]
sage: 
sage: f = sum(xx).function(*xx)
sage: f
(x_0, x_1) |--> x_0 + x_1

[Here I took advantage of the * operator in *xx, which turns f(*[a,b,c]) into f(a,b,c).]

Oh, I've just noticed that I used [0..n-1] as the indices and I think you're using [0..n], but that's easy to change.

2011-12-20 10:35:56 +0100 received badge  Scholar (source)
2011-12-20 10:12:47 +0100 received badge  Student (source)
2011-12-20 09:52:14 +0100 asked a question Defining a function of the form $f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$

How can I define a function of the form $$f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$$ I tried the following
sage: n = 2
sage: x = [ var('x_%d' % i) for i in range(n) ]
sage: f(x)=sum(x[i],i,0,n)
The last line gives me the error
"TypeError: 'sage.symbolic.expression.Expression' object does not support indexing".