Ask Your Question

Luca's profile - activity

2021-04-03 08:26:10 +0200 received badge  Good Answer (source)
2019-05-07 01:05:32 +0200 received badge  Necromancer (source)
2017-09-18 02:08:58 +0200 received badge  Nice Answer (source)
2017-03-25 17:50:35 +0200 received badge  Nice Answer (source)
2015-05-20 20:49:41 +0200 commented question Elements in the lattice $A_n$

Sorry, misread your question. Here's some facts

  • The positive entries in your vector form a composition of k of length a ≤ n.
  • The absolute values of the negative entries form a composition of k of length b ≤ n + 1 - a.
  • The rest are zero entries.

So, a sketch of algorithm to generate one such vector would be :

  • Form all compositions of k of length at most n.
  • Choose a and b so that a+b ≤ n+1.
  • Choose the positions for the a positive coefficients, fill with a composition of length a.
  • Chose the positions for the b negative cofficients, fill with a composition of length b.
  • Fill the rest with zeros.

To iterate over all such vectors, you can use Sage's Compositions and Permutations.

2015-05-19 14:23:21 +0200 commented question Elements in the lattice $A_n$

I think you are looking for the integer partitions of k of length n+1. Have a look at the iterator Partitions(k,length=n+1).

2015-03-31 17:23:16 +0200 answered a question notebook versus terminal session

The notebook is all about the web. Everything you can do at the terminal, you can do in the notebook. And _vice versa_. Some things, are just more nicely shared via a notebook. See (not Sage, but close enough).

Other good reasons to use notebooks :

Personally, I always use the terminal for personal or one shot computations, and Sage inside an IPython notebook for collaborating and sharing.

2015-02-28 16:33:00 +0200 commented question Using from Python (breaking the monolith)

I fully agree with you, and I am certainly not the only one. Unfortunately, Sage is a monolith, and there is little you can do right now. Making Sage more modular is one of the goals of this submitted EU project, that will hopefully begin next september.

2014-12-28 15:26:38 +0200 answered a question Variables in Sage

You must have forgotten the multiplication sign *. Quite obviously, $(c^2+d^2)$ was substituted for $a$ in the expression, thus you got $(c^2+d^2)^2 + b^2$. If you are using a recent version of Sage, you must have gotten a warning when evaluating the expression, saying that the correct form for expression substitution is

(a^2 + b^2)(a=(c^2 + d^2))

In Sage 6.4.1

sage: var('a,b')
(a, b)
sage: (a+b)(a+b)
/home/dfl/sage/local/lib/python2.7/site-packages/IPython/core/ DeprecationWarning:
Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future
release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See for details.
  exec(code_obj, self.user_global_ns, self.user_ns)
a + 2*b
sage: (a+b)*(a+b)
(a + b)^2
2014-11-30 21:50:26 +0200 commented answer Why does Sage return negative number when evaluating 181.0%360

Exactly for the reason I said: so that the output is consistent with the output of a/b, the rounding mode, and the mathematical definition of Euclidean division (which in truth does not make much sense for floats).

In Python rounding is done to the lowest integer, so it makes sense to return 181, indeed

>>> 360 * int(181.0 / 360) + (181.0 % 360)

In Mathematica, rounding is done to the closest integer, and indeed

360*round(181.0 / 360) + (181.0 mod 360)

answers 541 on WolframAlpha. Oups! Guess Mathematica does not really care for mathematical consistency.

2014-11-24 18:00:29 +0200 commented answer Lower prime divisor

Didn't know about trial_division. It is probably better than my answer, as it does the same thing, but with dedicated code.

2014-11-24 17:59:13 +0200 answered a question Lower prime divisor

It depends on the numbers you are factoring. Factorization algorithms for large numbers do not find factors in increasing order, thus you need to compute all factors in order to know which is smallest.

If your number is small, or has very small factors, this might be faster :

sage: n = 123456789
sage: bound = 100
sage: next(p for p in primes(bound) if n % p == 0)

play around with bound to find the value that works for you. Be aware that if no prime up to bound divides n, the last instruction will raise an exception, in that case you can try a higher bound, or use prime_divisors.

2014-11-23 19:43:29 +0200 answered a question Why does Sage return negative number when evaluating 181.0%360

So that this code works

sage: a = 181.0
sage: b = 360
sage: n = (a / b).round()
sage: b * n + (a % b) == a

From the sage docstring (which you can read by typing a.__mod__?)

Return the value of "left - n*right", rounded according to the rounding mode of the parent, where "n" is the integer quotient of "x" divided by "y". The integer "n" is rounded toward the nearest integer (ties rounded to even).

2014-09-19 16:25:39 +0200 answered a question Converting polynomials between rings

Because of the order you've defined the objects, L(t=t) will be an element of R2. Slightly more robustly, in your example you can do any of the following.

sage: L(t=R2.gen(4)).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
sage: L(t=R2.4).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
2014-08-30 00:47:38 +0200 received badge  Nice Answer (source)
2014-07-29 20:49:17 +0200 received badge  Nice Answer (source)
2014-07-22 14:55:31 +0200 received badge  Nice Answer (source)
2014-07-20 12:39:29 +0200 answered a question "divides" in ring of integers

With your input,

( r(gens[1]) / r(x) ).is_integral()

returns True. So, no, I see no problem here. However

sage: f = CyclotomicField(3)
sage: r.<z> = f.ring_of_integers()
sage: r(2).divides(z)
sage: (z/2).is_integral()

which is fishy.

2014-07-19 02:40:34 +0200 commented answer How Do I Extract Terms Containing Certain Coefficients From A Polynomial?

Careful: there's an indentation level missing after the second for

2014-07-19 02:37:15 +0200 commented answer How Do I Extract Terms Containing Certain Coefficients From A Polynomial?

To convert back to polynomial, just sum() over the list.

2014-07-19 00:48:16 +0200 answered a question How Do I Extract Terms Containing Certain Coefficients From A Polynomial?

I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing p is your polynomial expression, here's a fancy solution

[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]

There's certainly many other, but I am afraid none is going to be very simple. A word of explanation:

  • The first for loops over each monomial.
  • For each monomial m, m({x:0}) evaluates the monomial at the point x=0, where x ranges over i_L, d, etc.
  • If is_zero() returns True, the variable is obviously in the monomial.
  • In a summation, True and False get converted to 0 and 1, thus sum(...) == 1 guarantees that the monomial contains exactly one of the variables i_l, d, etc.

Thanks for this refreshing riddle :)

2014-07-18 16:37:45 +0200 commented answer How th work with enumerable and infinite set
2014-07-18 16:25:06 +0200 answered a question How th work with enumerable and infinite set

You just found a bug in Sage. Sage does not have enough knowledge to know that the set of rationals that are not prime numbers is infinite. If there was no bug, you'd have an error saying something like "Sage cannot compute the cardinality of B".

However, if you type B.cardinality??, you'll notice that what the function does is simply return len(list(self)), i.e. it constructs the list of ALL rationals that are not prime numbers, and returns its length. You get no answer because it's entered an infinite loop.

2014-06-14 08:24:01 +0200 received badge  Nice Answer (source)
2014-06-13 08:34:34 +0200 answered a question Find algebraic solutions to system of polynomial equations

Yes, you can use Gröbner bases. Here is an example

sage: A.<x,y,z> = QQ[]
sage: I = A.ideal(z*x^2-y, y^2-x*y, x^3+1)
sage: I.variety()
[{y: -1, z: -1, x: -1}, {y: 0, z: 0, x: -1}]

Tis is not implemented with coefficients in RR and will raise an error. However You can still ask for a Gröbner basis

sage: A.<x,y,z> = RR[]
sage: I = A.ideal(z*x^2-y, y^2-x*y, x^3+1)
sage: I.groebner_basis()
[x^3 + 1.00000000000000, x*y + z, y^2 + z, x*z - y*z, z^2 + y]
2014-04-30 12:12:04 +0200 commented question derivative of MPolynomial_polydict

I'm sorry. This doesn't help either. If you want useful help, you must make an useful effort to isolate the potential bug. It is by putting less code, not more, that you will achieve this: I cannot guess what parameters to `lambda_siep` are going to trigger the bug.

2014-04-29 10:37:24 +0200 commented question derivative of MPolynomial_polydict

I cannot reproduce your problem. Please give a complete working example. How much is `n`? Who's `Y`? There's a missing braket in your definition of `R`.

2014-04-17 14:43:01 +0200 answered a question Silverman Appendix G

Don't think so. Maybe you'll have more interesting answers on the sage-nt list!forum/sage-nt

2014-02-17 13:36:12 +0200 answered a question Cutting unnecessary zeroes in float numbers

You could just check that

type(x) == float

Or, instead of round, you could use python's string formatter.


will format x using 5 digits of precision (including before and after the decimal separator), removing trailing zeroes and the decimal dot for integers. Not exactly equivalent to your code, but maybe it will be good enough for your use.

If you have a locale configured for French/Italian/whatever notation


will automatically put a comma instead of a dot for you.

2014-02-10 15:48:53 +0200 answered a question List members of each subgroup of integers modulo n

multiplicative_subgroups returns only lists of generators, rather than proper group objects. This is, in my opinion, unfortunate. You can work it around with a bit of code. This function returns the list of elements spanned by a list G of generators

def list_elts(G):
    exps = [xrange(g.multiplicative_order()) for g in G]
    return [prod(g^e for g, e in zip(G, exp)) for exp in CartesianProduct(*exps)]

So, you may call

sage: R = Zmod(20)
sage: map(list_elts, R.multiplicative_subgroups())
[[1, 17, 9, 13, 11, 7, 19, 3],
 [1, 11, 9, 19],
 [1, 3, 9, 7],
 [1, 11],
 [1, 19],
 [1, 17, 9, 13],
 [1, 9],
2014-02-06 15:34:33 +0200 commented question Best practice: python vs sage-python

This is not really a question. However, I don't see any reason to uninstall your system's copy of python. Just my 2 pence.

2014-01-25 14:57:14 +0200 answered a question Difficulties evaluating an expression string

sage_eval does not have access by default to the variables declared in your session. Use the locals keword:

sage: eqs2 = [sage_eval(repr(xx), locals=globals()) for xx in eqs]

Depending on what you want to do, it may be more appropriate to use locals() instead of globals(). See

2014-01-18 06:35:58 +0200 commented answer Multivariate polynomials again: specifying variables

I would have said that [c.polynomial(y) for c in p.polynomial(x)] is not too much typing to achieve want you want, but I just tried it and it looks buggy. I'll investigate further.

2014-01-16 20:35:56 +0200 answered a question Multivariate polynomials again: specifying variables

Symbolics have a method .poly:

sage: var('x,y')
(x, y)
sage: p = x*y + x^2*y^2
sage: p.poly(x)
x^2*y^2 + x*y
sage: p.poly(x).coefficients()
[[y, 1], [y^2, 2]]

Multivariate polynomial rings have a method .polnomial()

sage: A.<x,y> = QQ[]
sage: p = x*y + x^2*y^2
sage: p.polynomial(x)
y^2*x^2 + y*x
sage: p.polynomial(x).list()
[0, y, y^2]

Both methods collect only with respect to one variable, so you may need to iterate.

2014-01-12 16:03:19 +0200 answered a question Representing finite field elements in terms of subfield elements

This is not supported yet. Your best option, for the moment, is use linear algebra:

sage: M = matrix([vector(w4^i*w2^j) for i in range(2) for j in range(2)])
sage: M
[1 0 0 0]
[0 1 1 0]
[0 1 0 0]
[0 0 1 1]
sage: vector(a) * M^-1
(1, 0, 1, 1)

which, of course, says $a$ equals $1 + w_4(1+w_2)$.

2014-01-06 08:26:30 +0200 received badge  Enthusiast
2014-01-04 10:01:52 +0200 answered a question A problem on Groebner Fans with more than 10 variables

Surprisingly, as the error message says, some methods do not apparently work when some variable names are prefixes to others. Here's a nasty workaround

sage: R = PolynomialRing(QQ, [chr(i+ord('a')) for i in range(11)])
sage: R
Multivariate Polynomial Ring in a, b, c, d, e, f, g, h, i, j, k over Rational Field
sage: R.ideal(R.gen(0)).groebner_fan()
Groebner fan of the ideal:
Ideal (a) of Multivariate Polynomial Ring in a, b, c, d, e, f, g, h, i, j, k over Rational Field

... you'll have to get creative when you go past 26 variables, though!

2013-12-27 04:19:21 +0200 answered a question morphism between permutation group and matrix group

Sage did not remove Gap's functionality, it simply did not interface to it. There seems to be no implementation of Homsets specific to groups, and in particular no implementation of morphisms specified via the images of the generators, like there is for rings, for example. I agree that the error message is not very informative.

I see no easy workaround: either you define your own python functions, or you work directly with gap. This is probably worth a ticket on

2013-12-17 23:36:33 +0200 received badge  Nice Answer (source)
2013-12-17 17:15:50 +0200 answered a question Does Sage "Show Its Work"?

Sage is open source. A student, or anyone else, can learn how sage computes the answers by reading the source code.

This can even be done interactively, by using the ?? magic. Say you want to learn how the Sylvester matrix of two polynomials is computed. After creating a polynomial object

sage: R.<x> = QQ[]

(x in this case) you can read the source code of sylvester_matrix() like this

sage: x.sylvester_matrix??

Admittedly, this will rarely give you a crystal clear explanation of the algorithm, as most of the time the work is done elsewhere (for example in some C library).

2013-12-17 06:33:16 +0200 answered a question where candownload v5.13 ? mirror all is v5.12

I suppose you use the Windows version. Please wait a couple of days, as it takes some more work to create the virtual machine for Windows.