Open rwst opened 10 years ago
First a technicality (which I don't think affects the point you're making): You're feeding sefms
a string that's not coming from maxima, so it's not entirely obvious that str(ex)
is valid input for sefms
. For instance:
sage: maxima_calculus(euler_gamma)
%gamma
sage: maxima_calculus(pi)
%pi
so sefms
is getting all kinds of symbols here that in maxima don't represent what they're meant to.
In fact, if I try to convert properly, I get an error for this expression:
sage: maxima_calculus(ex)
TypeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
The same problem arises when we try to avoid string manipulations as much as possible:
sage: t=maxima_calculus(str(ex))
sage: from sage.interfaces.maxima_lib import max_to_sr
sage: %time max_to_sr(t.ecl())
CPU times: user 1.6 s, sys: 3 ms, total: 1.6 s
Indeed, the fact that these expressions aren't valid prevents us from checking that the results are equal:
sage: bool(max_to_sr(t.ecl()) == sefms(str(ex)))
TypeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
I think the case would be better made with an example that's actually valid.
That said, I suspect the following happens: when we reconstruct an expression (from a string or otherwise), we call the symbolic functions in question, relying on them producing an appropriate symbolic expression. However, many symbolic functions try to do some simplifications before giving up and just returning a symbolic expression. As you're experiencing, some of these routines may be trying a little much for cases where, given the source of the expression, we expect very little simplification to occur.
Bypassing this, however, would be rather complicated. Of course, we could just transliterate the syntax trees (in essence, the other direction sr_to_max
does that--it does NOT call the maxima translations but just puts their symbols in an expression tree) but in reality we don't have the information for doing so. It would also mean that the current decentralized architecture where every symbolic function gets a chance to validate its arguments would need to be revisited.
The most reasonable solution might be to equip all symbolic function-producing call sequences (or at least the ones that interfaces produce) have an extra optional argument dont_do_anything_fancy=True
, to avoid any expensive argument manipulations. Perhaps this can be coordinated with hold=True
which already exists on some.
You would then have to insert that optional parameter at all relevant spots.
In short: it's not sefms
itself, it's the fact that sefms
is accessing sage's symbolic functions via their end-user interface, because we don't have another one.
It calls 120 times the function
MaximaLib._eval_line()
which takes 17ms in average = 2 seconds. 17 ms is an eternity. A lot of simplification attempts are going on in_eval_line
. For this expression I count ten calls totrigsimp
,trigexpand
,radcan
,fullratsimp
. Why would we want to call Maxima when converting *from Maxima, at all? And if it's necessary to build the expression, why simplify when it's supposed to be already simplified?Component: interfaces
Keywords: maxima, conversion, optimization, speed
Issue created by migration from https://trac.sagemath.org/ticket/16594