Ask Your Question

tririver's profile - activity

2021-03-10 22:35:34 +0200 received badge  Popular Question (source)
2020-10-14 17:08:06 +0200 received badge  Notable Question (source)
2017-06-09 20:02:43 +0200 received badge  Popular Question (source)
2016-11-24 07:32:50 +0200 received badge  Popular Question (source)
2016-10-01 16:20:59 +0200 received badge  Guru (source)
2016-10-01 16:20:59 +0200 received badge  Great Answer (source)
2016-02-02 17:37:58 +0200 received badge  Famous Question (source)
2015-01-13 22:08:36 +0200 received badge  Taxonomist
2014-06-29 03:15:05 +0200 marked best answer How to save a list to a file, with sage objects in it?

Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save

f = function('a',var('t'))
s = [f] # together with other things in the list

How to save s to a file?

I tried two ways:

(1) Standard python module pickle cannot save sage objects, thus fails

(2) I converted s to a python object:

s = Sequence([f])

Now I can use'file_name'). However, there is a bug that I cannot load using load('file_name')

RuntimeError: unknown function 'a' in archive

It is a known bug: .

Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?

In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?


2014-06-29 03:15:05 +0200 marked best answer How to robustly simplify an object (including a number)


When writing a package, I met the following problem:

Say, I want the variable 'a' to be an expression, which is passed to the package by user. Then I want to call a.simplify() to simplify the expression.

However, as an input, a could naturally be the following cases:

# case 1
a = var('t')
# case 2
a = 1
# case 3
a = 0.1

a.simplify() will work on 1, but not work on 2 or 3, which crashes the whole package.

I could do something as

    b = a.simplify()
    b = a

But this really seems like a workaround instead of something formal. Is there a better way to handle this case? Thanks!

2014-04-30 20:45:13 +0200 received badge  Notable Question (source)
2013-07-23 22:59:46 +0200 received badge  Nice Question (source)
2013-04-01 14:39:06 +0200 received badge  Popular Question (source)
2012-03-07 21:55:43 +0200 received badge  Great Answer (source)
2012-03-07 21:55:43 +0200 received badge  Guru (source)
2012-01-09 07:56:29 +0200 commented answer Does or will Sage support the Visual Python module?

Alternatively, I like to do things the other way round. Write a Python (2.x) package and use "from sage import *" to use sage functions. Then it should work with everything.

2012-01-07 03:57:32 +0200 received badge  Nice Answer (source)
2012-01-06 12:44:22 +0200 answered a question Min and functions

You can at least do it in another way:

sage: def myMin(x,y):
....:     return min(x,y)
sage: print myMin(3,2)

But concerning your original post, it really looks like a bug. I am not expert enough. If it is indeed a bug, we should report it.

2012-01-05 05:13:22 +0200 received badge  Necromancer (source)
2012-01-04 15:26:21 +0200 received badge  Nice Answer (source)
2012-01-04 08:01:50 +0200 answered a question How to do mul(function, i=1..n); like in Maple?

I agree with @Daniel Krenn that this is a problem that range() is a function in python, not the level of sage. Thus range(var('n')) does not work.

I also met similar cases recently. Thus I believe this case is not rare and it would great if someone improve the situation to have something more consistent. How I solved the problem is use a workaround: sum over a list instead of sum directly. The code is as follows:

def H(n,k):
    return sum([sum([ S(2*i+1,b)*S(n-2*i-3,k-(2*i+2)-b) for b in range(k-(2*i+2)+1)]) for i in range(floor((n-5)/4)+1)])

It takes me a long time to test H(121,4). I can run H(21, 4) or other a few small numbers, but they always return 1. I am not sure is it the desired answer. But at least it returns something.

Could I also suggest something: when asking a question, it would be better to isolate the problem into as simple case as possible. Then it may be easier for other people to debug and you can get better and quicker answers. For example, with the definition of S, the following already gives an error:

sum( S(i,1),i,0,1)
2012-01-02 13:15:59 +0200 received badge  Commentator
2012-01-02 13:15:59 +0200 commented answer subs_expr with e^t

@kcrisman : Thanks for the suggestion. I will try to make a patch to the doc.

2012-01-02 11:57:40 +0200 received badge  Good Answer (source)
2012-01-02 11:26:17 +0200 received badge  Nice Answer (source)
2012-01-02 08:55:29 +0200 answered a question subs_expr with e^t

Use a wild card (a variable that matches everything) to do complicated subs_expr. I would suggest this technique to be written in sage tutorials. It is very useful to me.

sage: t = var('t')
sage: foo = e^t
sage: w0 = SR.wild(0)
sage: foo.subs_expr(e^w0==e.n()^w0)
2011-12-28 08:58:01 +0200 commented answer CLI System Requirements

Archlinux is another option. Because archlinux package is .tar.xz (with pkgbuild though), one can unzip it to anywhere. It has sage as well.

2011-12-28 06:00:03 +0200 received badge  Good Answer (source)
2011-12-28 06:00:03 +0200 received badge  Enlightened (source)
2011-12-22 13:04:25 +0200 commented question "Evaluate All" keyboard shortcut

It would be nice to have such a feature. Currently I hold shift and press as many enters as number of cells :-)

2011-12-22 08:04:16 +0200 commented answer Is it possible to edit the content of an expression by hand?

@DSM: Thank you very much! I think as long as it seems an open question, I cannot expect a better answer before Sage is improved. Thus I mark this answer as the correct solution. I think both (1) and (2) are fine enough work arounds for my purpose. Also, I am very glad to find I have learnt how to use the star operator in Python from your post:-)

2011-12-22 08:00:27 +0200 marked best answer Is it possible to edit the content of an expression by hand?

Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.

(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:

class D(object):
    def __getitem__(self, *args):
        args = flatten(args)
        def dfun(f):
            ops = f.operands()
            dv = [f] + [ops[i] for i in args]
            return diff(*dv)
        return dfun

D = D()

After which the D operator kind of works:

sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)

(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:

def dpr(obj):
    print repr(obj._maxima_()).replace("'","")

sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)

(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:

sage: f = function("f", var("t"))
sage: diff(f,t)
sage: diff(f,t).operator()
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
sage: diff(f,t).operator()

I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines should work, even though it doesn't seem to.

If I actually needed to do something like this I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish ... (more)

2011-12-22 07:59:48 +0200 commented question Is it possible to edit the content of an expression by hand?

@kcrisman: Thanks for your comment. I met a few inconvenient cases where a general function is treated very differently from a variable. Seems I am a stranger here who always use a general function to produce troubles :-)

2011-12-21 23:14:08 +0200 received badge  Autobiographer
2011-12-21 22:55:20 +0200 answered a question How do I understand the result of symbolic integrals

Incomplete Gamma function and the Ei function are indeed related. See the following pages for example.

In this sense, the result of the integration is correct.

There is indeed a subtly of brunch cut. Maxima / Sage defines the brunch cut of the function differently from Mathematica. I tested the Maxima result "-1/x + Ei(x) - gamma(-1, -x)" is indeed continuous on the complex plane (i.e. the result has no problem). But the same function in Mathematica is not continuous.

2011-12-21 22:35:00 +0200 commented question Is it possible to edit the content of an expression by hand?

BTW, I asked a similar question two days ago without answer. I feel my question maybe not clear. Thus here I ask again. I deleted the original question in order not to pollute the forum. Thank you!

2011-12-21 22:33:54 +0200 asked a question Is it possible to edit the content of an expression by hand?


Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that.

To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example,

sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1

I want to delete the "+1" in the output and save the "D[0](f)(t)^2 + 2*D[0](f)(t)" in another variable. Is it possible to do that? Directly write

g = D[0](f)(t)^2 + 2*D[0](f)(t)

doesn't make sense. Sage says 'D' is not defined.

2011-12-21 13:36:39 +0200 received badge  Supporter (source)
2011-12-21 08:55:37 +0200 received badge  Nice Answer (source)