Ask Your Question

Philipp Schneider's profile - activity

2024-02-25 07:47:13 +0100 received badge ● Notable Question (source)
2024-01-17 13:46:24 +0100 received badge ● Notable Question (source)
2024-01-17 13:46:24 +0100 received badge ● Popular Question (source)
2021-04-12 18:37:29 +0100 received badge ● Popular Question (source)
2021-01-21 23:45:07 +0100 received badge ● Good Question (source)
2021-01-21 23:44:03 +0100 received badge ● Famous Question (source)
2019-03-01 18:18:17 +0100 received badge ● Self-Learner (source)
2019-02-26 20:48:42 +0100 answered a question coercion between polynomial rings with block term orders

a quick and dirty workaround is calling

S(str(f2))

instead of

S(f2)
2019-02-26 12:15:45 +0100 asked a question coercion between polynomial rings with block term orders

Hello,

I have the following problem: I define a polynomial ring

R.<x,y> = PolynomialRing(QQ,2,order='deglex')

then I define a new polynomial ring, with an addition auxiliary variable and a polynomial in this ring

S = PolynomialRing(R.base(),['w1']+[str(v) for v in R.gens() ],order=TermOrder('lex',1)+R.term_order())
S.inject_variables(verbose=False)
f = w1*x^3 + w1*y + x^2 + y^2

then I define another polynomial ring, with another addition auxiliary variable. Coercing to this new ring works fine

T = PolynomialRing(S.base(),['w2']+[str(v) for v in S.gens() ],order=TermOrder('lex',1)+S.term_order())
f2 = T(f)

However the way backwards fails

S(f2)

(...) /home/phil/Applications/SageMath/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_ring.pyc in _coerce_map_from_(self, P) 791 from sage.rings.polynomial.multi_polynomial_ring import is_MPolynomialRing 792 if is_MPolynomialRing(P) and self.variable_name() in P.variable_names(): --> 793 P_ = P.remove_var(self.variable_name()) 794 return self.base_ring()!=P_ and self.base_ring().has_coerce_map_from(P_) 795

/home/phil/Applications/SageMath/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ring_base.pyx in sage.rings.polynomial.multi_polynomial_ring_base.MPolynomialRing_base.remove_var (build/cythonized/sage/rings/polynomial/multi_polynomial_ring_base.c:5831)() 299 return PolynomialRing(self.base_ring(), vars, order=self.term_order()) 300 except ValueError: --> 301 raise ValueError("impossible to use the original term order (most likely because it was a block order). Please specify the term order for the subring") 302 else: 303 return PolynomialRing(self.base_ring(), vars, order=order)

ValueError: impossible to use the original term order (most likely because it was a block order). Please specify the term order for the subring

Any ideas what I could do to prevent this problem? How would I specify the term order (as it was suggested)?

Kind regards, Philipp

2015-04-15 15:28:58 +0100 received badge ● Self-Learner (source)
2015-04-15 15:28:58 +0100 received badge ● Nice Answer (source)
2015-04-15 15:28:10 +0100 received badge ● Self-Learner (source)
2015-02-07 22:50:06 +0100 received badge ● Notable Question (source)
2014-04-24 00:05:50 +0100 received badge ● Great Question (source)
2013-09-13 13:16:17 +0100 received badge ● Famous Question (source)
2013-08-27 18:11:57 +0100 received badge ● Great Question (source)
2013-06-22 17:43:49 +0100 received badge ● Nice Question (source)
2013-06-22 17:43:49 +0100 received badge ● Good Question (source)
2013-02-27 02:46:48 +0100 received badge ● Famous Question (source)
2012-03-17 10:20:53 +0100 received badge ● Popular Question (source)
2012-01-05 05:43:24 +0100 received badge ● Nice Question (source)
2011-11-07 14:53:20 +0100 received badge ● Notable Question (source)
2011-09-25 18:12:03 +0100 received badge ● Good Question (source)
2011-09-25 18:12:03 +0100 received badge ● Nice Question (source)
2011-09-09 06:21:25 +0100 received badge ● Notable Question (source)
2011-04-29 09:58:21 +0100 received badge ● Popular Question (source)
2010-12-10 07:27:29 +0100 received badge ● Popular Question (source)
2010-11-22 02:23:07 +0100 received badge ● Teacher (source)
2010-10-14 23:11:12 +0100 received badge ● Student (source)
2010-10-14 23:11:10 +0100 received badge ● Supporter
2010-10-14 23:11:10 +0100 received badge ● Editor (source)
2010-10-14 23:11:10 +0100 received badge ● Scholar (source)
2010-10-13 20:12:39 +0100 commented answer What is your favorite way to debug sage code?

Thanks. This is not a pretty solution, but it works. Also, instead of avoiding code that needs to be preparsed I can run sage -preparse foo.sage and then debug the resulting foo.py...

2010-10-13 17:42:44 +0100 asked a question What is your favorite way to debug sage code?

How do you debug sage code?

My favorite way do debug Python code is to use pdb in GUD emacs-mode.

I split the window into two buffers; in one I interact with pdb, in the other I see the code I'm debugging with the current line highlighted. When I step into a function which is defined in a different file, this window automatically shows the content of the new file.

Is it possible to use pdb and GUD with sage? Or is it possible to achieve this effect in a different way?

2010-10-01 18:14:58 +0100 asked a question show polyhedron in an interact

Hi, I am trying to write an interact which lets the user enter a list of vertices and then shows the corresponding polyhedron.

My first attempt is the following. The problem is that Sage does not show the polyhedron.

@interact
def _(t1=text_control("Polyhedron"), vertices="[[1,0],[0,1],[0,0]]"):
    p=Polyhedron(vertices=eval(vertices))
    show(p)

Does anybody know how to fix this?

Thanks.

2010-08-27 19:04:28 +0100 marked best answer why is symbolic comparison so slow?

The previous answers (with the exception of Nils Bruin's) are incorrect in their assessment of the problem -- the slowdown is indeed coming from trying to decide whether "x == tan(x)" is True or False. In particular, the slowdown comes a lot from our overhead in talking to Maxima. Sage performs the following things when trying to test whether an equation is zero or not.

  1. Check to see if both sides are constants. If they are, compare the constants.

  2. Ask GiNaC / Pynac to see if the expressions are structurally equal (i.e., they have are the same expression trees.) If they are, then we can immediately say things are equal.

  3. Check to make sure that there are no assumptions on any of the variables. If there are, then we have to immediately send things to Maxima.

  4. If there are no assumptions, then we can try evaluating at a number of points to try to verify that they are provably different using interval arithmetic. This is done in the test_relation method of Expression objects.

  5. If that doesn't work, we send it to Maxima. Since the Maxima output gives False if they are not equal as well as if it can't prove they are equal, we can only rely on a True answer from Maxima. Thus, a number of simplifications are done to try to "prove" equality. This is all done in sage.symbolic.relation.test_relation_maxima

  6. If all of the above things don't work, then we have to return False.

There are a number of areas for improvement:

  1. Most importantly there are quite a few cases in which the test_relation method fails. For example, there some code to check to see if using CIF for the values isn't working and if so switch over to RIF. However, that code is broken. Fixing that would allow something like

    sage: eq = x == tan(x)
    sage: eq.test_relation()
    False
    

    instead of giving NotImplemented like it does now.

  2. We can add a syntactically_equal method which just exposes, GiNaC's relational_to_bool test. If we just do that and modify mydiff appropriately, then we get

    sage: k = var('k')
    sage: s = sum(k*x^k,k,0,10)
    sage: %time mydiff(s, x)
    CPU times: user 0.02 s, sys: 0.00 s, total: 0.02 s
    Wall time: 0.02 s
    100*x^9 + 81*x^8 + 64*x^7 + 49*x^6 + 36*x^5 + 25*x^4 + 16*x^3 + 9*x^2 + 4*x + 1
    

I will make tickets for these and add the links to them here.

2010-08-27 16:05:17 +0100 commented answer why is symbolic comparison so slow?

thank you!

2010-08-27 13:38:40 +0100 answered a question why is symbolic comparison so slow?

the problem is neither Python, nor recursion, but Sage expressions. If I replace those with lists and strings the computation time drops to 0:

def mydiff(s,x='x'):
    if not x in str(s):
        return 0
    if s == x:
        return 1
    elif s == ['log',x]:
        return ['pow',x,-1]
    elif s == ['sin',x]:
        return ['cos',x]
    elif s == ['cos',x]:
        return ['mul',-1,['sin',x]]
    elif s == ['tan',x]:
        return ['add',1,['pow',['tan',x],2]]
    elif s == ['arcsin',x]:
        return ['pow',['add',1,['mul',-1,['pow',x,2]]],-1/2]
    elif s == ['arctan',x]:
        return ['pow',['add',1,['pow',x,2]],-1]
    op = s[0]
    ops = s[1:]
    if op == 'pow' and ops[0]==x and ops[1] in Rationals():
        return ['mul',ops[1],['pow',ops[0],(ops[1]-1)]]
    elif op == 'add':
        return ['sum',map(mydiff,ops)]
    elif op == 'mul':
        if len(ops) == 2:
            if ops[0] in Rationals():
                return ['mul',ops[0],mydiff(ops[1])]
            else:
                f = ops[0]; g = ops[1]
                return ['add',['mul',g,mydiff(f)],['mul',f,mydiff(g)]]

Now:

sage: var('k')
sage: s = ['add']+[['mul',k,['pow','x',k]] for k in [1..10]]
sage: %time mydiff(s)
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
['sum', [['mul', 1, ['mul', 1, ['pow', 'x', 0]]], ['mul', 2, ['mul', 2, ['pow', 'x', 1]]], ['mul', 3, ['mul', 3, ['pow', 'x', 2]]], ['mul', 4, ['mul', 4, ['pow', 'x', 3]]], ['mul', 5, ['mul', 5, ['pow', 'x', 4]]], ['mul', 6, ['mul', 6, ['pow', 'x', 5]]], ['mul', 7, ['mul', 7, ['pow', 'x', 6]]], ['mul', 8, ['mul', 8, ['pow', 'x', 7]]], ['mul', 9, ['mul', 9, ['pow', 'x', 8]]], ['mul', 10, ['mul', 10, ['pow', 'x', 9]]]]]
2010-08-27 10:27:25 +0100 answered a question why is symbolic comparison so slow?

@mhampton i don't wont to write the next best differentiator in the world. I'm just trying to give an example on how to program with symbolic expressions in sage.

@harald sorry, i should change the line

return mydiff(ops[0]) + mydiff(reduce(op,ops[1:]))

into

return sum(map(mydiff,ops))

then no ripping apart and reconstruction occurs. (I was trying to mimic pattern matching.)

Still, this doesn't help much. Does anybody know how Sage tests equality of symbolic expressions? Does it try to simplify those each time before it compares them?

2010-08-26 21:00:40 +0100 edited question why is symbolic comparison so slow?

I tried to write a little differentiatior:

from operator import add, mul, pow
def mydiff(s,x=x):
    if x not in SR(s).variables():
        return 0
    elif s == x:
        return 1
    elif s == log(x):
        return 1/x
    elif s == sin(x):
        return cos(x)
    elif s == cos(x):
        return -sin(x)
    elif s == tan(x):
        return 1+tan(x)^2
    elif s == arcsin(x):
        return 1/sqrt(1-x^2)
    elif s == arctan(x):
        return 1/(1+x^2)
    op = s.operator()
    ops = s.operands()
    if op == pow and ops[0]==x and ops[1]._is_numeric():
        return ops[1]*ops[0]^(ops[1]-1)
    elif op == add:
        return mydiff(ops[0]) + mydiff(reduce(op,ops[1:]))
    elif op == mul:
        f = ops[0]; g = reduce(mul,ops[1:])
        return g*mydiff(f) + f*mydiff(g)
    elif len(ops)==1:
        return mydiff(ops[0],x)*mydiff(op(x),x).subs(x=ops[0]))

one my one year old macbook i need to wait more than 17 second to get the following answer

%time mydiff(sum(k*x^k,k,0,10))
CPU times: user 12.39 s, sys: 0.78 s, total: 13.17 s
Wall time: 17.49 s
100*x^9 + 81*x^8 + 64*x^7 + 49*x^6 + 36*x^5 + 25*x^4 + 16*x^3 + 9*x^2 + 4*x + 1

why does it _that_ long?