Ask Your Question

tririver's profile - activity

2020-10-14 10:08:06 -0600 received badge  Notable Question (source)
2017-06-09 13:02:43 -0600 received badge  Popular Question (source)
2016-11-24 00:32:50 -0600 received badge  Popular Question (source)
2016-10-01 09:20:59 -0600 received badge  Guru (source)
2016-10-01 09:20:59 -0600 received badge  Great Answer (source)
2016-02-02 10:37:58 -0600 received badge  Famous Question (source)
2015-01-13 15:08:36 -0600 received badge  Taxonomist
2014-06-28 20:15:05 -0600 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-28 20:15:05 -0600 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 13:45:13 -0600 received badge  Notable Question (source)
2013-07-23 15:59:46 -0600 received badge  Nice Question (source)
2013-04-01 07:39:06 -0600 received badge  Popular Question (source)
2012-03-07 14:55:43 -0600 received badge  Guru (source)
2012-03-07 14:55:43 -0600 received badge  Great Answer (source)
2012-01-09 00:56:29 -0600 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-06 20:57:32 -0600 received badge  Nice Answer (source)
2012-01-06 05:44:22 -0600 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-04 22:13:22 -0600 received badge  Necromancer (source)
2012-01-04 08:26:21 -0600 received badge  Nice Answer (source)
2012-01-04 01:01:50 -0600 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 06:15:59 -0600 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 06:15:59 -0600 received badge  Commentator
2012-01-02 04:57:40 -0600 received badge  Good Answer (source)
2012-01-02 04:26:17 -0600 received badge  Nice Answer (source)
2012-01-02 01:55:29 -0600 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 01:58:01 -0600 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-27 23:00:03 -0600 received badge  Enlightened (source)
2011-12-27 23:00:03 -0600 received badge  Good Answer (source)
2011-12-22 06:04:25 -0600 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 01:04:16 -0600 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 01:00:27 -0600 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 00:59:48 -0600 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 16:14:08 -0600 received badge  Autobiographer
2011-12-21 15:55:20 -0600 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 15:35:00 -0600 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 15:33:54 -0600 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 06:36:39 -0600 received badge  Supporter (source)
2011-12-21 01:55:37 -0600 received badge  Nice Answer (source)
2011-12-21 00:21:34 -0600 edited answer Reliability of Sage vs commercial software

Here is my personal point of view:

(1) Sage is a rapidly growing project. The first version was released in 2005. Thus one could expect there's a lot of space to improve. Indeed the community is very active.

(2) In terms of powerful and speed, I would think Mathematica is still better so far. I mainly use Sage or Mathematica to do symbolic calculation. I found Mathematica can do more symbolic integrations than Sage could. The ODE solver is also more convenient. Also, Sage calls Maxima to do simplifications, which is more slowly than Mathematica.

(3) In terms of stability, I have met several bugs in Sage and I would not mark it as very stable. But the good thing is the bugs breaks the calculation explicitly (by saying something is wrong), instead of give you a wrong result. Also for every bug I met, I could eventually find a workaround, with the help of very kind community.

(4) Despite (2) and (3), I am still moving my previous Mathematica work to Sage. Why? Most importantly, it is because Sage is based on Python, and Mathematica has its own language. The Python-Sage resolution has much better support on class, name-space, etc. Thus when we have a lot of codes, it is much easier to reuse them in Python than in Mathematica (including the .m package).

(5) It's also a matter of Windows / Linux. Sage is more native on Linux, while Mathematica behaves better on Windows. (I have never used Mac thus I don't have comment on that.) The Linux support (especially notebook interface) of Mathematica is very slow, and occasionally unstable.

(6) There are also a number of other differences. For example, in Sage one has to define a variable before use (although don't need to declare). Mathematica doesn't need that. The former is safer but the latter is convenient. Also, one can view source code of a function in sage by typing, e.g. expand?? . This improves my programming technique because then I easily learn how more professional people write code.

Considering the above differences, currently, if you want to use a CAS to do a number of small calculations (say, calculate a integration in your work and insert the result back to your hand-writing notes), I would recommend Mathematica (despite the price issue...). On the other hand, if you write a lot of code and want to reuse them, Sage is a sage option.

Finally, I am looking forward to seeing sage become better and better. The active development group give us end users a lot of hope.