Ask Your Question

Bétréma's profile - activity

2021-03-09 21:34:19 +0200 received badge  Notable Question (source)
2021-03-09 21:34:19 +0200 received badge  Popular Question (source)
2018-04-12 16:06:53 +0200 received badge  Good Answer (source)
2016-11-05 11:37:59 +0200 received badge  Necromancer (source)
2016-11-02 21:51:56 +0200 received badge  Good Answer (source)
2016-08-06 16:57:59 +0200 received badge  Nice Answer (source)
2014-01-13 07:29:36 +0200 received badge  Critic (source)
2013-09-03 10:45:28 +0200 commented question error in Posets examples (order ideal function)

In such a situation, look at *your* documentation, to see if this example exists in *your* version of Sage; it does'nt for Sage 5.8 (my version), hence it's a recent feature.

2013-06-13 09:12:51 +0200 commented answer ergodic markov chain - steady state

"Iterating a lot" is the obvious way for picking a "random" element "in" the steady state, problems arise with the specification of "a lot" :-) And in real life, matrices which describe Markov chains are rather large.

2013-06-12 11:39:54 +0200 commented question ergodic markov chain - steady state

Question looks too general, ask Google about "coupling from the past".

2013-04-08 09:12:05 +0200 commented question Distinct (nonisomorphic) trees

http://www.sagemath.org/doc/reference/graphs/sage/graphs/graph_generators.html#sage.graphs.graph_generators.GraphGenerators.trees is a bit hidden

2013-03-27 04:57:40 +0200 answered a question How do I evaluate sum() containing factorial()?

The answer to the 'howto' question is: don't use symbolic calculus when it's not necessary (see preceding answers). Here is a tentative answer to the 'why' question. Yes there are special rules for evaluation of symbolic sums:

sage: var ('i k n t')
sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)
sum(k^i*t^i*factorial(-i + n + 1), i, 0, n)
sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)(n=3)
sum(k^i*t^i*factorial(-i + 4), i, 0, 3)

This is indeed surprising, but think of:

sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)(n=100)
sum(k^i*t^i*factorial(-i + 101), i, 0, 100)

Do you prefer this symbolic expression, or its full expansion? Hence there is no perfect strategy for evaluation of symbolic expressions, and there are also theoretical obstacles because canonical forms can't exist for symbolic expressions, even for very narrow classes.

2013-03-26 05:25:50 +0200 received badge  Nice Answer (source)
2013-03-24 08:41:21 +0200 answered a question List (or set) of matrices

To complete ppurka's answer, here is your code with corrections:

def ListM(p): 
    UT=[]
    for i1 in range(p): 
       for i2 in range(p): 
          for i3 in range(p):
             for i4 in range(p):
                UT=UT+[matrix([[i1,i2],[i3,i4]])]
    return UT

Note that output of ListM(3) looks ugly, but this is a formatting "bug" when you display such a list, output is correct.

Python has very nice syntax for "list comprehensions", you may write:

def rows(p):
    return [[x,y] for x in range(p) for y in range(p)]
def ListM(p): 
    return [matrix([r1,r2]) for r1 in rows(p) for r2 in rows(p)]

Of course in this case using MatrixSpace(GF(p),2,2) is definitely the right solution. Good luck with Sage !

2013-03-23 12:03:00 +0200 answered a question How do I evaluate sum() containing factorial()?

"Why do stuff in symbolics", I agree with ppurka, and I suggest:

def sumfact (k, n, t):
    return sum (factorial (n-i) * k^i * t^i for i in range (n+1))

which uses the built-in Python function sum.

[edit] After precisions added by 'stan', I'm puzzled, Sage gives me the (not very surprising) expected answers:

sage: var ('i k t')
sage: for n in range (5):
...       print sum (factorial(n-i)*k^i*t^i, i, 0, n)
1
k*t + 1
k^2*t^2 + k*t + 2
k^3*t^3 + k^2*t^2 + 2*k*t + 6
k^4*t^4 + k^3*t^3 + 2*k^2*t^2 + 6*k*t + 24
2013-03-22 13:07:41 +0200 received badge  Commentator
2013-03-22 13:07:41 +0200 commented question sage_eval: invalid syntax

@kcrisman 'y' is an input, not an output.

2013-03-22 09:04:54 +0200 commented question sage_eval: invalid syntax

What's wrong with a simple assignment y=3, why do you need sage-eval ?

2013-03-22 08:44:08 +0200 answered a question How to get a Boolean from the type of an object?

For matrices, I don't know, this is a complex topic in Sage. For tuples of pairs, this is pure Python, and I suggest:

def is_a_pair(a):
    return isinstance(a,tuple) and len(a)==2
def is_a_tuple_of_pairs(A):
    if not isinstance(A,tuple):
        return False
    return all (is_a_pair(a) for a in A)

Note that if you accept lists in addition to tuples, you can use:

isinstance(a,(list,tuple))

inside these functions and so on.

2013-03-11 09:42:35 +0200 received badge  Enthusiast
2013-02-28 09:21:01 +0200 answered a question c++ cython in the notebook

On my installation, in a notebook cell:

%cython
cimport sage.rings.number_field.number_field_element_quadratic

yields

[...]code_sage4_spyx_0.c:644:14: error: field ‘__pyx___numerator’ has incomplete type
[...]code_sage4_spyx_0.c:645:13: error: field ‘__pyx___denominator’ has incomplete type
[...]code_sage4_spyx_0.c:1090:13: error: field ‘x’ has incomplete type
[...]code_sage4_spyx_0.c:1356:14: error: field ‘x’ has incomplete type
error: command 'gcc' failed with exit status 1

but adding the magic commentbelow (see http://wiki.sagemath.org/faq) makes gcc quite happy:

%cython
#clang c++
cimport sage.rings.number_field.number_field_element_quadratic
2013-02-27 09:57:34 +0200 answered a question How to unpack .spkg files

The answer is rough: it's impossible to install Sage on a Windows system ! It's a feature, not a bug :-) See e.g. http://en.wikipedia.org/wiki/Sage_(ma...

2013-02-25 13:57:18 +0200 answered a question How are list of matrices printed by sage?

Yes, in interactive Python sessions, output uses displayhook instead of repr:

sage: A=matrix([[1,2],[3,4]])
sage: u=[A,A]
sage: repr(u)
'[[1 2]\n[3 4], [1 2]\n[3 4]]'
sage: import sys
sage: sys.displayhook(u)
[
[1 2]  [1 2]
[3 4], [3 4]
]

Sage defines it's own module sage.misc.displayhook. Imho that's a dead end, because math formatting is very complex, and the right tool is LaTeX, very well integrated in Sage and Sage Notebook. I suggest you write a method _latex_(self) for your objects, then lists of these objects will be correctly formatted by LaTeX. The string

'[' + repr(A) + ',' + repr(B) + ']'

is not a valid representation for [A,B] if there are newlines inside repr(A) and repr(B), while

'\left[' + latex(A) + ',' + latex(B) + '\right]'

is always a valid LaTeX description of [A,B].

2013-02-04 09:06:22 +0200 commented question Excercises, randomly generated and automatically checked

What is an "excercise" ? :-)

2013-01-09 14:24:11 +0200 commented answer Recursive backtracking function: how to clear variables on new function call?

Look at the first path returned by get_paths in my example: [[2, 1], [2, 1, 1], [2, 2, 1], [3, 2, 1], [3, 3, 1]], it's coded by the last skew tableau returned by sst.list(): [[None, None, 3], [None, 2, 4], [1]], because we add the first cell in a third new row, the second one is appended to the second row, the third one to the first row, and the fourth one to the second row (again). Anybody familiar with the subject may explain you the correspondence, it's easier on a sheet of paper or on a blackboard :-)

2013-01-06 09:24:55 +0200 answered a question Need to improve a function in sage which checks trueness of expressions in an example.

Your question is a bit daunting, but let us make some experiments (Sage predefines x to be a global indeterminate):

sage: p=x^2+2*x-3
sage: p.parent()
Symbolic Ring

Who is this guy? His nickname is SR:

sage: SR?
Symbolic Ring, parent object for all symbolic expressions.

One of the main goals of symbolic calculus is to solve equations:

sage: solve(p==0,x), solve(p==1,x)
([x == -3, x == 1], [x == -sqrt(5) - 1, x == sqrt(5) - 1])

Assume is badly documented, but looks useful in this context:

sage: assume(x>0)
sage: solve(p==0,x), solve(p==1,x)
([x == 1], [x == sqrt(5) - 1])
sage: assume(x, 'rational')
sage: assumptions()
[x > 0, x is rational]
sage: solve(p==0,x), solve(p==1,x)
([x == 1], [])

So far so good, and back to your question: is Sage a proof checker, which could help to check derivation of new propositions from assumptions? Answer is definitely negative:

sage: forget(); assume(p==0); assumptions()
[x^2 + 2*x - 3 == 0]
sage: prop = (x==1) or (x==-3); prop
x == -3

This very strange result is explained in a preceeding post.

sage: bool (prop)
False

Imho, no hope for improving your function, Sage is not the right tool. If you are interested in proof checkers, take a look at Coq, but this is another story.

2013-01-05 16:52:12 +0200 answered a question Recursive backtracking function: how to clear variables on new function call?

I think the following code gives the good output (but is far from optimal, many computations are repeated again and again, as in the naive recursive computation of the Fibonacci sequence), here parameters are assumed to be partitions:

def get_paths(top, bot):
    if top == bot:
        return [[top]]
    path_list = []
    for p in top.down_list():
        if p.contains(bot):
            path_list += get_paths(p, bot)
    for u in path_list:
        u.append(top)
    return path_list

For example:

sage: gp = get_paths (Partition([3,3,1]),Partition([2,1])); len(gp)
8
sage: len (get_paths (Partition([3,3,2]),Partition([1])))
42

Actually we are computing functions already defined in Sage:

sage: sst = StandardSkewTableaux([[3, 3, 1], [2, 1]])
sage: sst.cardinality()
8
sage: StandardTableaux([3,3,2]).cardinality()
42

Use sst.list() to get the "same" list as the one computed by get_paths(). You may eventually look at the source code of these functions, in the modules sage.combinat.tableau and sage.combinat.skew_tableau .

Amitiés.

2013-01-05 14:35:58 +0200 answered a question Recursive backtracking function: how to clear variables on new function call?

This is a very partial answer, but in a Python crash course (by William Stein I presume, now I can't find the link) I learned this example:

def f(a, L=[]):
    L.append(a)
    return L

with the following astonishing successive outputs:

sage: f(2)
[2]
sage: f(5)
[2, 5]
sage: f(4)
[2, 5, 4]

(this is pure Python, Sage is not guilty).

2013-01-05 13:39:14 +0200 commented question Need to improve a function in sage which checks trueness of expressions in an example.

You are probably a high school student who is struggling with quadratic equations and so on, I can tell you that Sage is definitely useless for learning how to handle such computations and for acquiring the necessary skills in this domain. Sorry and good luck with a more traditional approach !

2013-01-04 13:44:04 +0200 commented question Need to improve a function in sage which checks trueness of expressions in an example.

You "really need comments and suggestions" about very cumbersome expressions, and in a precedent post it was "very necessary" for you "to understand this behavior of sage", please what does it mean ?

2013-01-03 06:22:00 +0200 answered a question Express domain membership

I don't know if you actually need symbolic expressions, otherwise tests are very straightforward:

sage: x=12
sage: x in ZZ, x in QQ, x in RR
(True, True, True)

Note that tests using pure Python isinstance give different output, no surprise:

sage: isinstance(x, Integer), isinstance(x, ZZ)
(True, False)
2012-12-30 16:33:14 +0200 received badge  Nice Answer (source)