Ask Your Question

Mafra's profile - activity

2018-08-13 19:13:01 +0100 received badge  Enlightened (source)
2018-08-13 19:13:01 +0100 received badge  Good Answer (source)
2018-08-09 14:08:34 +0100 received badge  Nice Answer (source)
2018-08-08 19:29:14 +0100 answered a question arrange an expression in powers of a variable

Alternatively, you can use collect():

sage: de.collect(ep)
ep^2*diff(f1(x), x, x) + ep*(diff(f0(x), x, x) + diff(f1(x), x)) + diff(f0(x), x)

To get the coefficient of ep^2 you can do:

sage: de.coefficients(ep)[2][0]
diff(f1(x), x, x)

To get all the coefficients at once:

sage: de.coefficients()
[[diff(f0(x), x), 0],
 [diff(f0(x), x, x) + diff(f1(x), x), 1],
 [diff(f1(x), x, x), 2]]

If you define

sage: epcoeff = [de.coefficients(ep)[i][0] for i in range(len(de.coefficients()))]

you can access the coefficient of ep^n using epcoeff[n], so that you can feed it to desolve etc.

2018-02-01 15:58:06 +0100 received badge  Notable Question (source)
2017-04-05 17:48:36 +0100 received badge  Civic Duty (source)
2017-04-05 15:57:51 +0100 commented question Defining a generic symbolic function with symmetric arguments

@mforets: Thank you for pointing that out! Now it works in general, see updated question. I can now also define an antisymmetric function to keep track of the parity of the arguments in order to mimic the Levi-Civita tensor. I.e, typing eps(2,1,3) returns - eps(1,2,3). Good!

2017-04-05 12:38:25 +0100 commented question Defining a generic symbolic function with symmetric arguments

@mforets: Thank you very much for suggesting a general Python audience, I will try that later. Regarding the recursion, you have to know when to stop sorting the arguments. I updated the question with one attempt that is not general wrt the number of arguments just as an idea to try.

2017-04-03 21:00:56 +0100 received badge  Popular Question (source)
2017-03-29 00:25:38 +0100 commented question use multiple precision by default ?

@tmonteil: I believe that he is doing something like QQ = RealField(1000) to have numbers with 1000 bits of precision and he does not want to keep on writing everything inside QQ(). It would be nice to have a 'default' precision command that one sets up in the beginning and forget about it

2017-03-29 00:25:38 +0100 received badge  Commentator
2017-03-29 00:21:57 +0100 commented answer Arbitrary precision with power function

Is there a way to choose a default precision such that one does not need to keep typing RealField(1000)?

2016-11-05 02:37:05 +0100 commented question Speeding up power_mod

@Wojowu Despite the different environments, ~45s to ~18s is a huge difference. I tested 99999^450.powermod(9999^5000,10^6000) on my machine and on SMC and both were around 4.7s. Out of curiosity, what is the precise computation that you tested?

2016-11-05 01:26:29 +0100 commented question Speeding up power_mod

@Wojowu I'd expect GMP's .powermod to have a similar performance as Julia since Julia uses GMP too. But you cannot really compare the methods unless you compare them running in the same computer under the same load. So using JuliaBox and SageMathCloud makes all the difference to the point that you cannot really make any definitive statement of Julia vs Sage.

2016-11-03 21:20:46 +0100 commented answer Defining a generic symbolic function with symmetric arguments

Right, s(1,1) makes sense but it is zero. So in general one can restrict the labels to be distinct. Furthermore, s(i,j) is commutative. In Mathematica terms I am looking for the equivalent of using something like SetAttributes[s, Orderless] and nothing more.

2016-11-03 09:42:36 +0100 received badge  Good Question (source)
2016-11-02 15:38:41 +0100 received badge  Enthusiast
2016-11-01 19:03:39 +0100 commented answer Defining a generic symbolic function with symmetric arguments

So ideally one would have the choice of defining a symmetric function via an argument to the function() method, for example function('f', nargs=2, sym=symmetric) would define a function 'f' that takes two arguments and is symmetric in the sense that f(3,1) = f(1,3)

2016-11-01 19:00:30 +0100 commented answer Defining a generic symbolic function with symmetric arguments

Thank you for your answer, but this is not really what I am looking for. The name of the function in the input must be the same as the output; ie I want to type A = s(3,2)*s(1,2) + 2*s(4,2)*s(1,3) and obtain A = s(2,3)*s(1,2) + 2*s(2,4)*s(1,3). If I am allowed to use a different name in the input I already know a solution (just replace def s(*m) by def ss(*m) in my original question and type A = ss(3,2)*ss(1,2) + ... instead).

2016-10-30 14:27:50 +0100 commented question Speeding up power_mod

@Wojowu I found out that you can use the gmp implementation within Sage. Did you try it? It should give us roughly the same performance as Julia.

2016-10-30 13:50:20 +0100 commented question Speeding up power_mod

Julia uses the gmp library (see line 440 of []), whereas Sage uses a hand-made power_mod() function in python (see []). So one way to speed up Sage's power_mod() function is to implement it in pynac. Have you compared to Python's built-in pow(a,b,c) function, where c is the modulus? see here []

2016-10-22 02:44:35 +0100 answered a question How to find a polynomial identity to evaluate sum of fifth powers?

You can also solve it a bit more directly without explicitly invoking the Groebner basis (this happens under the hood).

sage: R.<a,b,c> = PolynomialRing(QQ,order='lex')
sage: f1 = a+b+c - 3
sage: f2 = a^2+b^2+c^2 - 5
sage: f3 = a^3+b^3+c^3 - 7
sage: Rel = ideal(f1,f2,f3)
sage: Rel.reduce(a^5+b^5+c^5)

(I came up with this solution after reading the solution to Exercise 37 from the book "Calcul Mathematique avec Sage", given on page 423)

2016-10-06 07:04:02 +0100 received badge  Teacher (source)
2016-10-03 16:17:11 +0100 answered a question leading coefficient polynomial

For example,


you can extract its coefficients as follows:


to get

[[2, 0], [7, 3], [2, 4]]

So the last sublist contains the answer, in this case 2 is the coefficient of x^4.

To extract this automatically you can define a function that takes a polynomial as argument:

def get_highest_coeff(P):
    m = P.coefficients()
    ncoeffs = len(m)
    return m[ncoeffs - 1][0]

And use it like this:


to get 2 as the answer. Hope this helps you get going!

2016-09-30 13:17:21 +0100 commented question Is there a command like Mathematica's Dynamic[] in Sage?

There is a "profiling" section in the tutorial which could be helpful.

2016-09-25 19:20:05 +0100 received badge  Scholar (source)
2016-09-25 16:26:17 +0100 received badge  Nice Question (source)
2016-09-24 23:55:43 +0100 asked a question solve((x^3 - 4*x) > 0,x) does not enforce condition assume(x > 0)

I actually have two (related) questions.

First question, am I misunderstanding the following code?

solve((x^3 - 4*x) > 0,x)

The solution given by sagemath-7.3 is

[[x > -2, x < 0], [x > 2]]

but I would expect only the solution [x > 2] under the condition x > 0.

Is this a bug or am I doing something wrong?

Second question. How can I enforce the condition x > 0 myself and "solve" the resulting conditions of:

[[[D < -2],
  [D > 0, D < -1/9*sqrt(1081) + 116/9],
  [D > 1/9*sqrt(1081) + 116/9]],
 [[D > -4, D < -2],
  [D > 2, D < -1/3*sqrt(7009) + 116/3],
  [D > 1/3*sqrt(7009) + 116/3]],
 [[D > -4, D < 0], [D > 2, D < 11]],
 [[D < -6], [D > -2, D < 0], [D > 2, D < 19]],
 [[D < -4], [D > -2, D < 0], [D > 2]],
 [[D > -6, D < -4], [D > -2, D < 0], [D > 2]]]

How can I solve this type of question efficiently? I believe that discarding the negative solutions in the above system will imply that 2 < D < 1/9(116 - sqrt(1081)), for example. However, I'd like to do this for many other cases as well.

Any help will be appreciated!

2016-09-22 18:50:48 +0100 received badge  Nice Question (source)
2016-09-22 18:41:52 +0100 received badge  Editor (source)
2016-09-19 22:51:05 +0100 received badge  Supporter (source)
2016-09-19 21:43:23 +0100 received badge  Student (source)
2016-09-18 23:05:36 +0100 asked a question Defining a generic symbolic function with symmetric arguments

Updated question:

I would like to define a generic symbolic function called s whose arguments are totally symmetric.

So if I type s(3,2) the output must be s(2,3),

   sage: s(3,2) 

and similarly

   sage: s(3,4,1,2) 

and in general some arbitrary product

   sage: s(3,2)*s(1,2)^2*s(5,4,1)

Is it possible to have such a definition? It would be nice if there was an argument to the built-in function() to specify the symmetry of its arguments. In this case I would get what I want with something like

s = function('s', sym='symmetric')

Solution from Apr/2017:

Upon reading Sage's source code I noticed the 'eval_func' option when defining functions. So if I define (following a suggestion of mforets, thanks!):

def symmetric(self, *x):

    L = list(x)
    L0 = copy(L)

    if L0 <= L:
        return self(*L)

and define the (Mandelstam invariant) s as follows

 s = function("s", eval_func=symmetric)

everything works. For example

  sage: 2*s(2,1)*s(6,5,4,3)^2
  2*s(3, 4, 5, 6)^2*s(1, 2)

So I consider this question closed!