Ask Your Question

Björn's profile - activity

2021-03-28 00:18:40 +0200 received badge  Nice Question (source)
2021-03-28 00:17:50 +0200 received badge  Notable Question (source)
2020-12-28 23:48:31 +0200 received badge  Popular Question (source)
2020-06-18 20:52:19 +0200 received badge  Notable Question (source)
2020-04-16 12:19:56 +0200 received badge  Nice Question (source)
2020-04-16 12:19:43 +0200 received badge  Self-Learner (source)
2020-01-17 14:13:56 +0200 received badge  Notable Question (source)
2019-12-24 19:19:21 +0200 received badge  Good Question (source)
2019-02-20 16:10:01 +0200 received badge  Popular Question (source)
2019-01-08 06:50:42 +0200 received badge  Notable Question (source)
2018-06-28 21:39:25 +0200 received badge  Famous Question (source)
2018-03-21 08:39:50 +0200 commented answer Code highlighting and execution in sagetex

It is a partial workaround, but does not solve the original question, really. The sage: is a bit in the way, and this doesn't seem to support block code like function definitions or loops. Sorry for not noticing your answer earlier, I haven't logged in in a while.

2018-02-16 00:32:07 +0200 received badge  Popular Question (source)
2018-02-16 00:32:07 +0200 received badge  Notable Question (source)
2018-01-29 16:36:14 +0200 received badge  Popular Question (source)
2017-12-16 12:58:19 +0200 received badge  Popular Question (source)
2017-09-06 03:47:27 +0200 received badge  Nice Question (source)
2017-09-05 14:44:22 +0200 asked a question Code highlighting and execution in sagetex

Is there a version of \begin{sageblock} my multiline code \end{sageblock} that has code highlighting and line numbering like the sagecommandline environment and code execution?

Of course typesetting with lstlistings and execution with sagesilent could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.

2017-04-20 07:48:07 +0200 received badge  Nice Answer (source)
2017-04-19 18:52:52 +0200 commented answer Why does simplify break print_latex_func?

That's just perfect, thanks! latex(sexp2), latex(f) instead of sexp2 ; f really demonstrates the achievement of your recursive coercion. Very nice, I especially like that the code does not require a priori knowledge of the function to be substituted. This really should be built into sage's simplify() at the stage when it processes what comes back from maxima.

2017-04-19 18:52:52 +0200 received badge  Commentator
2017-04-19 18:46:17 +0200 commented answer Why does simplify break print_latex_func?

I guess will have to live with that ;) Thanks again for giving me the starting point, there wasn't much left to do after that.

2017-04-19 16:53:27 +0200 received badge  Self-Learner (source)
2017-04-19 16:20:07 +0200 commented answer Why does simplify break print_latex_func?

Not only that, to get the operator name, one somehow needs to find it by location in the sage expression, at least I am not aware of a better way to find a reference to the "maxima" a(). Maybe one could loop over all operators in the expression and somehow filter by a feature that identifies them as maxima operators... but that's messy.

2017-04-19 15:19:32 +0200 received badge  Organizer (source)
2017-04-19 15:15:37 +0200 commented answer Why does simplify break print_latex_func?

Thanks to kcrisman I figured the remaining bits out myself. Complete code as follows:

# [...]
print latex(sexp)
sexp2=simplify(sexp) # expand doesn't hurt it, so omit that
print latex(sexp2)
# observe they are different due to sexp2 being processed 
# via Maxima, as explained by kcrisman
# maxima replaces a() by a new a() that doesn't have the 
# print function:

print sexp2.operands()[0].operator().__class__ # maxima a()
print a.__class__ # the a() we have defined

# to coerce back, we need to replace the former by the latter:
print latex(sexp2.substitute_function(sexp2.operands()[0].operator(),a))

(And that does the intended.)

2017-04-19 14:38:07 +0200 commented answer Why does simplify break print_latex_func?

Aha, I see... At the end of the day I don't really mind Maxima, as long as I can coerce back, which I cannot seem to figure out how to achieve. Obvious (to me) candidates would seem to be latex(sexp2.subs({a(n):a(n)})) or even latex(sexp2.subs({a: a})). But they do not work as expected or throw errors when I use keywords in combination with arguments to a().

print sexp2.operands()[0].operator().__class__ # maxima a()
print a.__class__ # my a()

Reveals the functions a() are indeed different, the former function_factory.NewSymbolicFunction, the latter expression.Expression. Can't I just somehow retrospectively set the print_latex_func on the function_factory.NewSymbolicFunction?

2017-04-19 13:53:34 +0200 answered a question How to simplify fractions?

Use bool() like so

,var n
2017-04-19 13:50:49 +0200 commented question Why does simplify break print_latex_func?

I know it is simplify() and not expand() that causes the problem. But why and how?

2017-04-19 04:17:36 +0200 asked a question Why does simplify break print_latex_func?

Here's a minimal example of what I am talking about. Both print statements should return the same output in this scenario.

,var n x
def my_latex_print(self, *args): 
    return "a_{%s}" %(', '.join(map(latex, args)))

print latex(sexp)
print latex(sexp)

However, the outputs differ:

a_{n + 1} + a_{n}
a\left(n + 1\right) + a\left(n\right)

Of course simplify-expand may lead to a different expression, but that's not what I am concerned about. I am concerned about the representation of a(n) instead of a_n in the output.

My main question is: How can I recover a_n out of sexp at the end of my code?

2017-04-10 15:11:36 +0200 received badge  Teacher (source)
2017-03-27 17:32:39 +0200 received badge  Popular Question (source)
2017-03-25 01:08:34 +0200 commented question formatting fractions involving square roots

Thanks, that is at least something. While playing with this, I noticed that power? and power?? documentation does not mention the hold keyword at all.

Searching for hold=True I just found, which makes this question a duplicate.

2017-03-21 02:49:08 +0200 answered a question derivative of non-commuting symbolic product

I realise I may just define a new operation "non-commutative product" (ncp for short) that takes two arguments and respects the product rule, as well as some basic simplifications.

var('t s')

def e_func(self, *args, **kwds):
    if args[0]==1:
        return args[1]
    elif args[1]==1:
        return args[0]
    elif args[0]*args[1]==0:
        return 0

ncp = function('ncp', nargs=2,
               tderivative_func=lambda self, *args, **kwds:\
               print_func=lambda self,*args: format(args[0])+' . '+format(args[1]) # denote ncp by .

print ncp(t^2,3*s^3).derivative(t)
print ncp(t,3*s^3).derivative(t)
print ncp(t^2,3*s^3/t).derivative(t)

Which produces

t^2 . 3*s^3
2*t . 3*s^3
t^2 . -3*s^3/t^2 + 2*t . 3*s^3/t

I would very much prefer a more "built-in" solution over this hack. Also, without further modification this approach does not work for products of three or more factors. For that reason I'll leave the acceptance of an answer open for a more concise solution.

2017-03-20 15:57:40 +0200 asked a question derivative of non-commuting symbolic product

Consider the product rule $\frac d{dt}[A(t)B(t)]=\dot{A}(t)B(t)+A(t)\dot{B}(t)$ with $A$ and $B$ not commuting, e.g, matrix valued. I'd like to replicate this in sage, however, I don't see how I can specify that $A$ and $B$ do not commute. So far I have


which yields B(t)*diff(A(t), t) + A(t)*diff(B(t), t). But here sage has assumed that the operators and their derivatives do commute. Not what I want.

I did look into sage.symbolic.function_factory.function and the like, but could not find anything about products. Am I overlooking something or is this currently not possible?

2017-03-20 14:03:19 +0200 received badge  Editor (source)
2017-03-20 14:03:19 +0200 edited question formatting fractions involving square roots

How can I tell sage that I want the result to be shown as $\frac{1}{\sqrt{3}}$ rather than $\frac13\sqrt3$ when I type show(1/sqrt(3))?