Ask Your Question

tmonteil's profile - activity

2020-07-02 04:31:53 -0500 commented answer How to organize a list according to an other list

you can always hide loops, for example by replacing them with recursive calls of functions.

2020-07-02 02:45:48 -0500 answered a question How to organize a list according to an other list

For every sublist of Yc and every element a in X, search a in the sublist and append the corresponding element b in Y (X and Y are aligned with zip):

Yc = []
for j in Xc:
    l = []
    for (a,b) in zip(X,Y):
        if a in j:
            l.append(b)
    Yc.append(l)

Note that this way, the elements in the sublists are not sorted in the same order (but i guess you do not mind given the description).

2020-07-01 16:04:48 -0500 commented answer Adding edges to a graph

.^_^ .

2020-07-01 10:49:15 -0500 answered a question Adding edges to a graph

Your loop for g in graphs(i): iterates over all graphs of size i, which is a big waste of time. The graphs() generator works by adding edges, so if a property P is stable by edge removal, then it can serve as a filter during the generation (not after) : if some graphs does not satifsy P, then its "descendance" in the generation tree is not explored.

Here, the property P is "being triangle-free and having chromatic number at most 3", which is stable by edge removal.

Hence graphs(n, property=lambda g : g.is_triangle_free() and g.chromatic_number() <= 3) will generate all graphs of size n with that property, see graphs? for more details.

So, here is a possible way to construct the maximal graphs you are looking for:

def maximal_graphs(n):
    mg = []
    for G in graphs(n, property=lambda g : g.is_triangle_free() and g.chromatic_number() <= 3):
        maximal=True
        for e in G.complement().edges():
            H = copy(G)
            H.add_edge(e)
            if H.is_triangle_free() and H.chromatic_number() <= 3:
                maximal=False
                break
        if maximal:
            mg.append(G)
    return mg

You can use that function to count the graphs you ar looking for:

sage: L = [len(maximal_graphs(n)) for n in range(3,11)] ; L
[1, 2, 3, 4, 6, 10, 16, 31]

You can then search in the OEIS, in case something is known about such sequence:

sage: oeis(L)
0: A216783: Number of maximal triangle-free graphs with n vertices.

sage: s = _[0]
sage: s.first_terms()
(1, 1, 1, 2, 3, 4, 6, 10, 16, 31, 61, 147, 392, 1274, 5036, 25617, 164796, 1337848, 13734745, 178587364, 2911304940, 58919069858, 1474647067521, 45599075629687)

Unfortunately, the next term differs:

sage: len(maximal_graphs(11))
60

So, for graphs with 11 vertices, being maximal triangle-free is not equivalent to being maximal triangle-free and 3-colorable.

You can see the following answers to get more details about generation of graphs satisfying an hereditary property:

2020-07-01 10:22:09 -0500 answered a question Simplifying expression by known terms

Since you symbolic expression is a polynomial, i would suggest to work in polynomial rings:

sage: R.<b0,a1,a2,h,v_0,v_1,v_2,u_0,u_1,u_2> = QQ[]
sage: R
Multivariate Polynomial Ring in b0, a1, a2, h, v_0, v_1, v_2, u_0, u_1, u_2 over Rational Field
sage: P = (16*h^2*v_0^2+(38*h*u_0-38*h*u_1)*v_0+25*u_1^2-50*u_0*u_1+25*u_0^2)/3 ; P
16/3*h^2*v_0^2 + 38/3*h*v_0*u_0 - 38/3*h*v_0*u_1 + 25/3*u_0^2 - 50/3*u_0*u_1 + 25/3*u_1^2

You can replace the substitution by working in a quotient ring:

sage: Q = R.quotient([-h*v_0+2*u_1-2*u_0-a1,-2*(h*v_0-u_1+u_0)-a2]) ; Q
Quotient of Multivariate Polynomial Ring in b0, a1, a2, h, v_0, v_1, v_2, u_0, u_1, u_2 over Rational Field by the ideal (-h*v_0 - a1 - 2*u_0 + 2*u_1, -2*h*v_0 - a2 - 2*u_0 + 2*u_1)

And see how your polynomial behaves there:

sage: Q(P)
4/3*a2bar^2 - a2bar*u_0bar + u_0bar^2 + a2bar*u_1bar - 2*u_0bar*u_1bar + u_1bar^2

You can lift the result back to the initial polynomial ring :

sage: Q(P).lift()
4/3*a2^2 - a2*u_0 + u_0^2 + a2*u_1 - 2*u_0*u_1 + u_1^2

And make it a symbolic expression if needed:

sage: SR(Q(P).lift())
4/3*a2^2 - a2*u_0 + u_0^2 + a2*u_1 - 2*u_0*u_1 + u_1^2
2020-06-30 16:42:57 -0500 answered a question nearby algebraic number / convert polynomial type to expression type

Since there are no integer roots, p.roots() does not gies anything since it is a polynomial over the integers:

sage: p.parent()
Univariate Polynomial Ring in x over Integer Ring

You can however ask for its roots as algebraic numbers:

sage: p.roots(QQbar)
[(-2.236067977499790?, 1), (2.236067977499790?, 1)]

or as symbolic expressions:

sage: p.roots(SR)
[(-sqrt(5), 1), (sqrt(5), 1)]

or even as floating-point real numbers:

sage: p.roots(RDF)
[(-2.23606797749979, 1), (2.23606797749979, 1)]
2020-06-23 05:45:24 -0500 commented answer Why does implicit multiplication not work in the Sage Cell Server?

I doubt there is an easy way without modifying sagecell (or running your own).

2020-06-22 17:19:50 -0500 answered a question t == -1/2*(2/45*I*sqrt(4613)*sqrt(3) - 82/5)^(1/3)*(I*sqrt(3) + 1) + 1/3*(10*I*sqrt(3) - 10)/(2/45*I*sqrt(4613)*sqrt(3) - 82/5)^(1/3)

You can use the show function to display the result in LaTeX :

https://sagecell.sagemath.org/?z=eJwr...

If you have multiple such formulas in a jupyter notebook, you can also add:

%display latex

in some cell, so that every formula will be turned into LaTeX once that cell is evaluated.

2020-06-22 17:16:34 -0500 answered a question y == 1/4*sqrt(5) + 1/4*I*sqrt(2*sqrt(5) + 10) - 1/4,

You can use the show fiunction to display the result in latex, see

https://sagecell.sagemath.org/?z=eJwr...

2020-06-22 12:58:33 -0500 answered a question assume gives false for a<oo

First, you should know that for symbolic computations, False does not mean i know this is False, but i was not able to prove it is True.

That said, for such simple comparison, Sage should indeed be able to decide, hence i consider this as a bug.

Thanks for reporting, this is now trac ticket 29938 and it is also listed on the symbolic wiki page

2020-06-17 19:08:40 -0500 answered a question Why does implicit multiplication not work in the Sage Cell Server?

I think this is not specific to SageCell. It is the same in jupyter. implicit_multiplication changes the behaviour of the preparser, but the whole cell is preparsed according to the previous state of the preparser.

If, in a single cell, you write:

implicit_multiplication(True)
2x

You will get an error. But if you put the two lines in different cells, then it will work.

To take another example, you can do:

preparser(False)
type(1)

You will get <class 'sage.rings.integer.Integer'> because the whole cell was preparsed before the preparser is turned off. If you put the two lines in two different cells, you will get <class 'int'>, because the second line will not be preparsed anymore.

About the Sage preparser, see https://doc.sagemath.org/html/en/refe...

The sagecell works like a single jupyter cell.

2020-06-17 13:39:16 -0500 answered a question A way to recuperate data computed to construct an histogram

The histogram is just an image:

sage: h = histogram(l, bins=20)
sage: type(h)
<class 'sage.plot.graphics.Graphics'>

hence you will not be able to get those data from it easily.

2020-06-17 04:55:02 -0500 commented question A problem with Pandas dataframe (Need help urgently)

Could you please make the file Chinonrougegpsga1.xlsx accessible somewhere ?

2020-06-15 05:04:15 -0500 answered a question importing hyperbolic geometry modules

This is not about an optional pip package, there is an issue with your Sage installation. How did you install it ? Could you please give us some informations so that someone can try to understand your problem:

  • which version of Sage did you use ?
  • which OS ?
  • did you install Sage from the distro ?
  • did you install Sage from the binaries, and which ones ?
  • did you compile Sage yourself ?
2020-06-14 15:05:28 -0500 answered a question Is the `hex(31)` conversion deprecated or not?

It is a known issue, but no consensus was found about fixing it, see https://groups.google.com/forum/#!msg...

2020-06-14 15:05:19 -0500 answered a question Is the `011` notation for octal 11 deprecated or not?

It is a known issue, but no consensus was found about fixing it, see https://groups.google.com/forum/#!msg...

2020-06-09 19:38:36 -0500 commented answer Monomial with power modulo n

@emmanuel-charpentier : i used RR to stay close to @only1sale question, I then explained that QQ could be used otherwise, i do not get your point.

2020-06-09 06:37:51 -0500 received badge  Nice Answer (source)
2020-06-09 05:36:28 -0500 answered a question Monomial with power modulo n

You can make a polynomial quotient ring:

sage: R.<x> = RR[]
sage: R
Univariate Polynomial Ring in x over Real Field with 53 bits of precision
sage: S = R.quotient(x^6-1)
sage: S
Univariate Quotient Polynomial Ring in xbar over Real Field with 53 bits of precision with modulus x^6 - 1.00000000000000
sage: S.inject_variables()
Defining xbar

Then, you can do

sage: xbar^9 + xbar^11
xbar^5 + xbar^3
sage: xbar^9 * xbar^11
xbar^2

Also, if your coefficients turn out to be rational, i would sugest to define sage: R.<x> = QQ[]

2020-06-08 15:52:58 -0500 answered a question factoring an expression

I am not sure about the context, but you can do:

sage: (f/x^2).expand() * x^2
(x + 3/x)*x^2
2020-06-08 15:18:07 -0500 commented question Roots of a function

What do you mean by root ? Do you mean branch of the cube root ?

2020-06-08 05:47:12 -0500 answered a question How do I write a code which gives me all prime ideals up to a specific norm?

You just have to do:

sage: K.ideals_of_bdd_norm(5)
{1: [Fractional ideal (1)],
 2: [Fractional ideal (a + 1)],
 3: [],
 4: [Fractional ideal (2)],
 5: [Fractional ideal (2*a + 1), Fractional ideal (-a - 2)]}

What you get is a Python dictionary, whose keys are the norms, and the values are Python lists of the ideals with given norm.

For example:

sage: K.ideals_of_bdd_norm(5)[3]
[]

is an empty list since there is no ideal with norm 3

sage: K.ideals_of_bdd_norm(5)[5]
[Fractional ideal (2*a + 1), Fractional ideal (-a - 2)]

is a list with 2 elements: you can access them as follows:

sage: K.ideals_of_bdd_norm(5)[5][0]
Fractional ideal (2*a + 1)
sage: K.ideals_of_bdd_norm(5)[5][1]
Fractional ideal (-a - 2)

Now, if you want all those ideals without caring about their norm, you can do:

sage: d = K.ideals_of_bdd_norm(5)
sage: [f for i in d for f in d[i]]
[Fractional ideal (1),
 Fractional ideal (a + 1),
 Fractional ideal (2),
 Fractional ideal (2*a + 1),
 Fractional ideal (-a - 2)]

or

sage: flatten(d.values())
[Fractional ideal (1),
 Fractional ideal (a + 1),
 Fractional ideal (2),
 Fractional ideal (2*a + 1),
 Fractional ideal (-a - 2)]
2020-06-07 17:22:49 -0500 answered a question How to define conditionaly two vector from one

You should replace:

[conds(zouzou[_],.5) if _ in range(0,10)]

with

[conds(zouzou[_],.5) for _ in range(0,10)]
2020-06-06 05:53:28 -0500 answered a question Replace all numbers < e-n by 0

The apply_map method allows to apply a given map to any entry of the matrix, here you can replace any small number by zero:

sage: M = Ω*Λ
sage: M.apply_map(lambda x : 0 if abs(x) <10^-15 else x)

You can also directly use:

sage: M.round()
2020-06-02 06:28:01 -0500 answered a question Ignoring binary string monoid

Let me just add a warning: if you want to make B an integer, as in your previous question, you can first try:

sage: ZZ(str(B))
262145

which looks pretty weird. This is due to the fact that the number:

sage: str(B)
'01000001'

starts with a 0, indicating that it should undertand the representation to be in octal, while you want it in binary. Fortunately, you can specify the base in the conversion:

sage: b = ZZ(str(B), base=2) ; b
65
sage: b.parent()
Integer Ring
2020-06-02 06:23:28 -0500 answered a question How to identify binary digits as integers?

You can make a conversion:

sage: b = ascii_to_bin('Hi')[4]
sage: b
1

Unfortunately, there is no direct conversion to ZZ:

sage: ZZ(b)
TypeError: unable to coerce <class 'sage.monoids.string_monoid_element.StringMonoidElement'> to an integer

but you can first make b a string an then an integer:

sage: a = ZZ(str(b)) ; a
1
sage: a.parent()
Integer Ring
2020-06-01 15:40:25 -0500 commented question Variable elimination in Sage

Could you please provide a concrete example of system you are considering, and which behaviour you expect ?

2020-06-01 08:51:48 -0500 commented question Removing some element of a list according to the zero of another

How is Pref defined ?

2020-06-01 08:46:03 -0500 commented question Get point coordinates of curve over number field

Could you please provide the construction of curve ?

2020-05-31 19:44:10 -0500 answered a question Convert between different ways of expressing permutations

I am not sure about your exact question, but you might start with:

sage: x = Permutation([2,1,4,3])
sage: y = Permutation([3,2,1,4])
sage: x.cycle_tuples()
[(1, 2), (3, 4)]
sage: y.cycle_tuples()
[(1, 3), (2,), (4,)]

Regarding Kazhdan-Lusztig polynomial for Weyl groups, you can have a look at :

2020-05-31 18:38:53 -0500 commented answer Pb with an integer random draw

ZZ is defined by default:

sage: ZZ
Integer Ring

Maybe you overwrrote it ?

2020-05-31 06:54:30 -0500 answered a question jupyter doc

You can have a look at the first worksheet of this tutorial https://tmpsage.metelu.net/cirm2019/

It starts with a Jupyter survival guide : edit mode vs command mode + some shortcuts.

You can also have a look at this tutorial : https://doc.sagemath.org/html/en/prep...

but it still has some sagenb-related stuff (to be removed soon).

You can reuse both (CC BY-SA 3.0 licensed)

2020-05-31 06:10:46 -0500 answered a question Clickable Image

Assume that in a code cell you did:

p = plot(sin)
p.save('myplot.png')

so that the file myplot.png is a picture of the sin function.

Now, to make a cickable image from that image to https://ask.sagemath.org/, you can do in a markdown cell:

[![](myplot.png)](https://ask.sagemath.org)

Now, within an interact, the question is less clear, if you want the click to be interpreted by the interact, you can have a look at : https://github.com/sagemath/sage-comb...

2020-05-30 14:18:59 -0500 edited answer Equating coefficients of multivariate polynomials

Whe you write :

sage: var('a0,a1') ; R.<x,y> = QQ[] ; 
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x ;

The product a1*x (and the other elementary operations) is done using coercion, that is Sage first searches the common parent between the symbolic ring and the bivariate rational polynomials in x,y. It turns out that it is the symbolic ring itself. Hence, what you get is and element of the symbolic ring:

sage: Pol.parent()
Symbolic Ring

In particular, you lose the polynomial structure in x,y.

The first approach is to let Sage consider a0 and a1 a possible coefficients for polynomials in x,y, by declaring the polynomial ring R to be defined over the symbolic ring SR:

sage: var('a0,a1') ; R.<x,y> = SR[]
(a0, a1)
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x 
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Symbolic Ring

Then you can extract the nonzero coefficients:

sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: solve(Pol.coefficients(),[a0,a1])
[]

There is of course no solution since a0 should be both equal to 0 and -5.

Another approach is to let the coefficients be elements of another polynomial ring S, that will be the ring over which R will be defined:

sage: S.<a0,a1> = QQ[] ; S
Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: R.<x,y> = S[] ; R
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x 
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field

Now, the common zeroes of the coeficients of Pol (which are polynomials, elements of S), is nothing but the variety of an ideal:

sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: S.ideal(Pol.coefficients())
Ideal (-a1, -a0 - 5, a1, a0) of Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: I = S.ideal(Pol.coefficients())
sage: I.variety()
[]

Which is empty as well.