I'd do something like this:
sage: var('x y')
(x, y)
sage: d, = solve([x+y==6, x-y==4], x, y, solution_dict=True)
sage: d
{y: 1, x: 5}
sage: f = 2*x+y
sage: f.subs(d)
11
--Mike
I think this is a very worthwhile thing to implement, at least if the
LHS are all single variables.
- Robert
> That was the way I used to do it when I discovered the solution_dict
> param.
>
> So, I started using the dictionaries to do substitution even in
> complex expressions... The result was to wait so much time!
>
> On the contrary, I found that the subs(expr, x=<value>) was SOOOO much
> faster (are the dictionaries so slow to deal with?).
>
> So, I implemented a subList function, where I can pass an expression,
> and a list of substitution, in the very same way that the solve()
> function returns the results.
>
> So, now I put all my subsets of numerical values in list like this:
> set = [x == 10, y == 20, z == 30, ...]
> and do the substitution like this:
> result = subList(expr, set)
>
> Within the function, I just iterate the subs(expr, x = <value>) on the
> number of list elements.
>
> This seems to me much faster than using dictionaries, especially for
> complex expressions.
If this is the case, this is clearly a bug.
- Robert
Maybe you could publish a notebook worksheet, or attach a script,
showing a complete worked example to demonstrate the slowness?
Carl
The description doesn't help me. I'm willing to take a brief look if
I have a simple reproduction case, but your description doesn't give
me much hope of reproducing the problem. (It might be enough to give
a hint to somebody familiar with the code, but I'm not.)
You could try:
test = loads(dumps(bad_expr))
where bad_expr is the slow one. If test acts the same as bad_expr (is
similarly slow), then that means that you can just do:
save(bad_expr, 'slow_substitution.sobj')
and post slow_substitution.sobj.
> Could you also show me where should I look at for better understanding
> this? Is it a good idea to verify the types of the expressions used?
> Is it a problem interfacing to any symbolic package different from the
> ordinary maxima one? I don't exactly recall if I took advantage of
> others.
I see that you're substituting in a symbolic value. Does it change
anything if you change Vgr_pk to a numeric value instead?
Looking at types is good.
Probably where I would start is by running the slow command, pressing
Control-C when it's about halfway through, and looking at the
backtrace to see if I could figure out what was going on. The problem
would almost certainly be related to something on the backtrace, so
that's a reasonable place to start. My first guess would be that
you'll discover that the slowness is because it's interacting with
maxima somehow; if that's the case, then you need to figure out why.
Using other symbolic packages might or might not be a problem. (It's
not supposed to be a problem, but we're assuming there's a bug
somewhere. Interfaces between separate packages are always a fruitful
place for bugs.)
Carl
I was actually calling G_igr_d "bad_expr", but it doesn't matter.
If you don't have any publicly available web space, you could just
email them to me privately and I'll stick them in public space on
sage.math (and take a quick look myself).
Carl
The files are now available at
http://sage.math.washington.edu/home/cwitty/slow-substitution/
I won't be able to look at them until this evening (maybe now+8 hours).
Carl
OK, I spent some time looking at this, and this may indeed be the
problem. I have a workaround for your case, but I may need more
information to get somebody to actually fix the problem.
Can you try this test:
var('Cb')
G_igr_d._operands[1]._operands[1] is Cb
and report what the last line prints?
If it prints False, then you're seeing what I'm seeing: somehow you
have two different synbolic variable objects, both named Cb.
Ordinarily you would only ever have one such object. With two
separate objects, Python can't immediately answer the question whether
Cb_1==Cb_2, so it asks Maxima (which says yes, they're equal).
In that case, the workaround is to do:
G_works = SR(repr(G_igr_d))
This is an ugly hack that prints out G_igr_d, and then parses it back
in; doing so makes it use the standard variable objects.
If this is the problem, can you try to remember how you actually got
G_igr_d? It seems like the problem actually might be the integration
with some other symbolic system, and it would be very helpful to know
which one.
Carl
There is no problem with loads/dumps -- it is something specific to
the SymbolicVariable class. In any case, I put a patch up at
http://trac.sagemath.org/sage_trac/ticket/5466 with a more appropriate
description.
--Mike