Ask Your Question

Sébastien's profile - activity

2019-01-09 13:01:43 -0600 received badge  Notable Question (source)
2019-01-04 05:20:48 -0600 commented answer Quaternion * integer

you must do something like:

if isinstance(other, Integer):
    return something
elif isinstance(other, quater):
    return something else
    raise TypeError('multiplication with other(={}) not defined'.format(other))
2019-01-03 10:31:45 -0600 commented answer Documentation erratum

simplify_full exists so that it appears under tab completion on E.simplify. full_simplify exists maybe because sometimes people use this name (in maple?, in mathematica? for instance)

2019-01-03 10:26:41 -0600 answered a question Quaternion * integer

by defining a method def __mul__(self, other): in your class called quater. Something like this:

def __mul__(self, other):
    if isinstance(other, Integer):
        return something
    elif isinstance(other, quater):
        return something else
        raise TypeError('multiplication with other(={}) not defined'.format(other))
2019-01-03 05:20:42 -0600 commented answer Sage Floyd algorithm in Cython

Are you welling to add algorithm='Floyd-Warshall-scipy', algorithm='Floyd-Warshall-numpy' to the method shortest_path_all_pairs in Sage and even improve the Sage pure python implementation? That would be awesome.

2018-12-03 03:40:58 -0600 commented answer Error in finding the closest vector in a sublattice of Z^n

"one of the main things I want to do is simply orthogonally project a vector onto a subspace spanned by the columns of a matrix, which is functionality Sage also seems not to have built-in anywhere I can find it."

Orthogonal projection can be made with multiplication of matrices, and I think that you can multiply matrices in Sage. In the wikipedia page Projection (linear algebra), you will find a formula of the form:

sage: P_A = A*(A.transpose()*A).inverse()*A.transpose()

which gives you a matrix which does the orthogonal projection with respect to the column of the matrix A.

2018-12-01 01:55:47 -0600 marked best answer Can make doc use less memory?

Running make doc works fine during the night on my computer because I am not using it. But running make doc is really annoying during the day when let's say I want to use firefox at the same time: the computer gets swapping and becomes unusable.

2018-10-25 09:17:20 -0600 commented answer Is there anything in Sage which solves #SAT ?

For an small typical example I have,

sage: p
Boolean Program (maximization, 275 variables, 65 constraints)
sage: p.polyhedron(backend=backend) # takes forever
2018-10-24 10:45:27 -0600 received badge  Nice Question (source)
2018-10-22 10:40:06 -0600 commented answer Is there anything in Sage which solves #SAT ?

Thank you for your idea of using integral_points_count. I will compare the speed with dancing links which was the only way I knew to count solutions.

2018-10-19 04:29:29 -0600 asked a question Is there anything in Sage which solves #SAT ?

For example using sharpsat or relsat or Dsharp?

2018-10-18 08:04:43 -0600 commented question Change binary Operation in Monoid/Semigroup Rings

Maybe you need to create a new category for the algebra you need. Maybe you will find information on how to do this in the four documentation pages in the section Parents/Elements, Categories and algebraic structures of the thematic tutorials.

2018-09-09 13:56:39 -0600 answered a question Is there a sage command to compute complex numbers?

I get:

sage: z = 1 + I
sage: sqrt(1/z)
sqrt(-1/2*I + 1/2)

If you want the real and imaginary parts, you may do:

sage: s = sqrt(1/z)
sage: s.real().simplify()
1/4*2^(3/4)*sqrt(sqrt(2) + 2)
sage: s.imag().simplify()
-1/4*2^(3/4)*sqrt(-sqrt(2) + 2)
2018-09-03 03:30:23 -0600 received badge  Nice Answer (source)
2018-09-03 02:15:55 -0600 answered a question Setting relations between variables

You may create a dictionary of variables:

import itertools
S = {}
for i,j,k,l in itertools.product([1..4], repeat=4):
    S[(i,j,k,l)] = var('S_{}{}{}{}'.format(i,j,k,l))

which then allows you to create a loop to set your relations.

Alternatively, you may create only the variable S_ijkl in the loop above and add a line for the others:

for i,j,k,l in itertools.product([1..4], repeat=4):
    if j>i or l>k:
    S[(i,j,k,l)] = var('S_{}{}{}{}'.format(i,j,k,l))
    S[(j,i,k,l)] = S[(i,j,l,k)] = S[(j,i,l,k)]
2018-08-29 09:10:55 -0600 commented answer Wrong result from integral(log(1+1/(x**2+1)), x, -oo, oo)

So where should we fix this?

2018-08-28 07:49:45 -0600 received badge  Nice Answer (source)
2018-08-28 04:03:40 -0600 asked a question Why .45 // .28 does not work?

Taking modulo works with Python float and Sage float:

sage: .45r % .28r
sage: .45 % .28

The quotient works with Python float:

sage: .45r // .28r

But why should it not work with Sage float as well?

sage: .45 // .28
Traceback (most recent call last):
TypeError: unsupported operand parent(s) for //: 'Real Field with 53 bits of precision' and 'Real Field with 53 bits of precision'
2018-08-27 15:48:31 -0600 answered a question Wrong result from integral(log(1+1/(x**2+1)), x, -oo, oo)

By default, Sage uses Maxima which indeed gets it wrong.

sage: integrate(log(1 + 1/(x**2 + 1)), x, -oo, oo, algorithm='maxima')
2*pi - 2*sqrt(2)*pi

Nonetheless, Sympy and Giac both get it right:

sage: integrate(log(1 + 1/(x**2 + 1)), x, -oo, oo, algorithm='sympy')
-2*pi + 2*sqrt(2)*pi
sage: integrate(log(1 + 1/(x**2 + 1)), x, -oo, oo, algorithm='giac')
2*pi*(sqrt(2) - 1)
2018-08-26 13:18:10 -0600 received badge  Nice Answer (source)
2018-08-22 08:20:07 -0600 answered a question su2 matrix exponentiation

Using sympy, it seems to work:

sage: a,b,c,d = [a._sympy_() for a in G.list()]
sage: from sympy import Matrix as SympyMatrix
sage: G_sympy = SympyMatrix([[a,b], [c,d]])
sage: G_sympy_exp = G_sympy.exp()
sage: G_sympy_exp.simplify()
sage: G_sympy_exp
[(-(cos(theta) - 1)*exp(2*I*t) + cos(theta) + 1)*exp(-I*t)/2,                I*(I*sin(phi) - cos(phi))*sin(t)*sin(t
[        (-exp(2*I*t) + 1)*exp(I*phi)*exp(-I*t)*sin(theta)/2, ((cos(theta) + 1)*exp(2*I*t) - cos(theta) + 1)*exp(-I

Then using copy-paste, I am able to construct the matrix G_exp as a Sage object:

sage: G_sympy_exp.tolist()
[[(-(cos(theta) - 1)*exp(2*I*t) + cos(theta) + 1)*exp(-I*t)/2,
  I*(I*sin(phi) - cos(phi))*sin(t)*sin(theta)],
 [(-exp(2*I*t) + 1)*exp(I*phi)*exp(-I*t)*sin(theta)/2,
  ((cos(theta) + 1)*exp(2*I*t) - cos(theta) + 1)*exp(-I*t)/2]]
sage: G_exp = matrix([[(-(cos(theta) - 1)*exp(2*I*t) + cos(theta) + 1)*exp(-I*t)/2,
....:   I*(I*sin(phi) - cos(phi))*sin(t)*sin(theta)],
....:  [(-exp(2*I*t) + 1)*exp(I*phi)*exp(-I*t)*sin(theta)/2,
....:   ((cos(theta) + 1)*exp(2*I*t) - cos(theta) + 1)*exp(-I*t)/2]]
....: )
sage: G_exp
[-1/2*((cos(theta) - 1)*e^(2*I*t) - cos(theta) - 1)*e^(-I*t)                  (-I*cos(phi) - sin(phi))*sin(t)*sin(theta)]
[            -1/2*(e^(2*I*t) - 1)*e^(I*phi - I*t)*sin(theta)  1/2*((cos(theta) + 1)*e^(2*I*t) - cos(theta) + 1)*e^(-I*t)]

Note: I was expecting that G._sympy_() would return the equivalent matrix in Sympy but this method does not exist. It would be nice to add it. Also, there must be an easier way to convert the sympy G_sympy_exp back to a sage matrix instead of the copy-paste I am doing above.

2018-08-17 08:59:28 -0600 answered a question How can I compute the orbits of an automorphism group?

Let's say that you are considering the automorphism group of a graph. Then, you do:

sage: edges = [(0,1), (1,2), (2,3), (3,4), (4,5), (5,3)]
sage: G = Graph(edges, format='list_of_edges')
sage: group = G.automorphism_group()
sage: group
Permutation Group with generators [(4,5)]
sage: group.orbits()
[[0], [1], [2], [3], [4, 5]]

You may also get both the automorphism group and the orbits from the graph:

sage: group, orbits = G.automorphism_group(orbits=True)
sage: group
Permutation Group with generators [(4,5)]
sage: orbits
[[0], [1], [2], [3], [4, 5]]
2018-08-07 02:13:42 -0600 answered a question No module named coin_backend

You need to run make after the installation of cbc or at least rebuild sagelib. The documentation on MILP solvers says to install cbc like this:

sage -i cbc sagelib
2018-08-05 17:43:26 -0600 received badge  Nice Answer (source)
2018-08-04 08:30:18 -0600 answered a question Words avoiding patterns

You may use RecursivelyEnumeratedSet in Sage to do these kind of thing:

sage: def non_rep_words(alphabet):
....:     def children(w):
....:         return [w+(a,) for a in alphabet if not w or w[-1] != a]
....:     seeds = [tuple()]
....:     R = RecursivelyEnumeratedSet(seeds, children, structure='forest')
....:     return R
sage: Rab = non_rep_words(['a','b'])
sage: Rab
An enumerated set with a forest structure
sage: it = Rab.breadth_first_search_iterator()
sage: [next(it) for _ in range(6)]
[(), ('a',), ('b',), ('a', 'b'), ('b', 'a'), ('a', 'b', 'a')]
sage: list(Rab.elements_of_depth_iterator(3))
[('a', 'b', 'a'), ('b', 'a', 'b')]
sage: list(Rab.elements_of_depth_iterator(4))
[('a', 'b', 'a', 'b'), ('b', 'a', 'b', 'a')]
sage: list(Rab.elements_of_depth_iterator(5))
[('a', 'b', 'a', 'b', 'a'), ('b', 'a', 'b', 'a', 'b')]
2018-07-22 06:47:36 -0600 commented answer Finding the frequency of each number in a string

Instead of using defaultdict(int), one may use Counter also from collections:

>>> from collections import Counter
>>> c = Counter()
>>> c['a'] += 1
>>> c[2] += 1
>>> c[2] += 1
>>> c
Counter({2: 2, 'a': 1})

or even better:

>>> Counter([2,2,'a'])
Counter({2: 2, 'a': 1})
2018-07-19 15:43:25 -0600 received badge  Critic (source)
2018-07-18 14:53:29 -0600 received badge  Nice Answer (source)
2018-07-12 00:48:26 -0600 received badge  Nice Answer (source)
2018-07-11 02:49:07 -0600 answered a question Why is the Subwords class separated from FiniteWord?

The module was created in 2007 during the translation by Mike Hansen of 30k lines of code from Mupad-Combinat to sage. Around the same time, in 2008, the module sage/combinat/words was created and nothing has ever been done to merge the two.

Notice that the code in is based on itertools.combinations and no wiser algorithm is coded to enumerate distinct subwords.

sage: s = Subwords('aaaaa', 3)
sage: s.__iter__??
       iterator = itertools.combinations(self._w, self._k)
sage: list(s)
['aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa']

If you know the existence of a better algorithm, you are welcome to add it to the file which contains already 3 methods related to subwords:

sage: w = Word('abc')
sage: w.*subword*?
2018-07-09 16:45:27 -0600 commented question Using sagemath on windows 10 : impossible to attach file

Did you try absolute paths and write \ instead of /?

sage: attach('/home/Javier/C:\Users\Javier\Desktop\current\height1/height1.sage')
sage: attach('/home/Javier/C:\Users\Javier\Desktop\current\height1\height1.sage')
2018-06-26 08:29:21 -0600 received badge  Popular Question (source)
2018-06-25 15:00:09 -0600 received badge  Nice Answer (source)
2018-06-12 14:19:56 -0600 received badge  Good Answer (source)
2018-06-12 13:44:14 -0600 received badge  Nice Answer (source)
2018-06-12 11:14:17 -0600 answered a question Running several commands from command line

One way is to concatenate the commands with a ; in between:

$ sage -c "m=matrix(2,2,range(4));print(m^10)"
[ 44726  79647]
[159294 283667]
2018-06-07 13:57:53 -0600 commented question pdflatex not available from Jupyter in
2018-06-06 21:05:09 -0600 received badge  Nice Answer (source)