Substitution in symbolic expressions

已查看 766 次
跳至第一个未读帖子

Jean-Pierre Flori

未读,
2010年9月29日 10:38:592010/9/29
收件人 sage-devel
Sage has the following behavior inherited from GiNaC (http://
www.ginac.de/tutorial/Pattern-matching-and-advanced-substitutions.html)
:

----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: x,y,z = var('x,y,z')
sage: P = x+y
sage: P.subs({x+y:z})
z
sage: P = x+y+z
sage: P.subs({x+y:z})
x + y + z
sage: w0 = SR.wild(0)
sage: P.subs({x+y+w0:z+w0})
2*z
sage: P = x+y
sage: P.subs({x+y+w0:z+w0})
z
sage:

Of course the same thing is happening with mul objects.
I think this is somewhat misleading and should at least be explained
in the documentation.
The above url is already give in the documentation of the match
function but not in the one of subs.
Maybe this explains the warning in the documentation of the subs_expr
function.
However that warning refers to Maxima whereas :

sage: get_systems('P.subs_expr({x+y+w0:z+w0})')
['ginac']

The weird example can also be solved using a wildcard :

sage: t = var('t')
sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t
sage: f
(x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x)
sage: f.subs_expr(x^2 + y^2 == t)
(x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x)
sage: f.subs_expr(x^2 + y^2 + w0 == t + w0)
(x, y, t) |--> 2*t + sin(y) + cos(x)

I don't know if such a trick should be implemented in Sage, pynac, or
even in GiNaC.
At least, it should be documented.

Best regards,

rjf

未读,
2010年9月29日 14:49:172010/9/29
收件人 sage-devel
Look at what ratsubst will do in Maxima.

If you think you have a well-defined operation in mind, what does it
do with
substituting 1 for s^2+c^2 in the expression s^4+3*s^2*c^2+ c^4?

RJF

Jean-Pierre Flori

未读,
2010年9月29日 15:27:412010/9/29
收件人 sage-devel


On 29 sep, 20:49, rjf <fate...@gmail.com> wrote:
> Look at what ratsubst will do in Maxima.
>
> If you think you have a well-defined operation in mind, what does it
> do with
> substituting 1  for s^2+c^2  in the expression s^4+3*s^2*c^2+ c^4?
I don't think I had a well mathematically defined operation in mind.
Of course I should have read Maxima doc before but was confused by the
warning because I knew that the subs function uses Pynac and not
Maxima.
My point is that for me the meaning of "formal pattern" and
"mathematical meaning" didn't make me think of :
"b must be an atom or a complete subexpression of c" as is said in
maxima doc.
I thought : ok, it should replace it as "sed" would do with a string,
but it doesn't and it is weird, but the documentation says so...
Especially when the Sage doc then says:
"The following seems really weird, but it is what Maple does" and
"Actually Mathematica does something that makes more sense"
But knowing the internals of pynac, or just reading the doc above, it
is not weird at all, so I thought that the Sage documentation might
tell something else that "it is weird" but it is that way.
And with that in mind, I wouldn't expect it to do anything with your
example.
I would call collect(s^2) before.
But that is just my humble opinion.
>
> RJF
>
> On Sep 29, 7:38 am, Jean-Pierre Flori <jpfl...@gmail.com> wrote:
>
> > Sage has the following behavior inherited from GiNaC (http://www.ginac.de/tutorial/Pattern-matching-and-advanced-substituti...)

Burcin Erocal

未读,
2010年9月29日 15:28:502010/9/29
收件人 sage-...@googlegroups.com

The function substitute_expression() is one of the relics remaining
from the old symbolics code. IMHO, we should merge it with the
substitute() function and deprecate it.

> However that warning refers to Maxima whereas :
>
> sage: get_systems('P.subs_expr({x+y+w0:z+w0})')
> ['ginac']
>
> The weird example can also be solved using a wildcard :
>
> sage: t = var('t')
> sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t
> sage: f
> (x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x)
> sage: f.subs_expr(x^2 + y^2 == t)
> (x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x)
> sage: f.subs_expr(x^2 + y^2 + w0 == t + w0)
> (x, y, t) |--> 2*t + sin(y) + cos(x)
>
> I don't know if such a trick should be implemented in Sage, pynac, or
> even in GiNaC.

You can definitely suggest it to the ginac developers. I'm curious to
see what they think.

Note that this might not be so straight forward, especially if the
expression we're supposed to replace contains wildcards in the first
place. We can add some logic to our interface to ginac and add the
wildcard to the expression if none exists. Though I'm afraid this might
increase the confusion if someone is trying to debug substitutions in a
complex expression.

One other point against doing this by default is that substituting with
wildcards is slower. Even if we decide to do this, there should be an
option not to.


Cheers,
Burcin

Jean-Pierre Flori

未读,
2010年9月29日 15:39:392010/9/29
收件人 sage-devel
I completely agree with that and am aware that it would be slower.
That is why I don't think it should be done by default, but something
in the doc should give someinsight on what is going on and what can be
done.

>
> Cheers,
> Burcin

rjf

未读,
2010年9月30日 10:25:182010/9/30
收件人 sage-devel
The semantics of substitution as done in Maxima and probably Ginac (no
wildcards)
is quite clear IF you understand the representation of expressions as
trees.
Not strings.

That means that some people will NOT understand substitution.

A fairly safe bet is only to substitute for atoms.

Jean-Pierre Flori

未读,
2010年9月30日 10:34:312010/9/30
收件人 sage-devel


On 30 sep, 16:25, rjf <fate...@gmail.com> wrote:
> The semantics of substitution as done in Maxima and probably Ginac (no
> wildcards)
> is quite clear IF you understand the representation of expressions as
> trees.
> Not strings.
I think we both agree that there is nothing weird about not replacing x
+y by z in x+y+z.
So we should also agree on the fact that the following sentence in the
examples of the documentation :
"The following seems really weird, but it is what Maple does:"
is WRONG, or at least MISLEADING.
Which is the main point of my first post.

Burcin Erocal

未读,
2010年10月1日 12:51:512010/10/1
收件人 sage-...@googlegroups.com
Hi,

On Wed, 29 Sep 2010 12:39:39 -0700 (PDT)
Jean-Pierre Flori <jpf...@gmail.com> wrote:

> I completely agree with that and am aware that it would be slower.
> That is why I don't think it should be done by default, but something
> in the doc should give someinsight on what is going on and what can be
> done.

I opened two tickets based on this thread:

* #10049 improve documentation of substitute for symbolic expressions
http://trac.sagemath.org/sage_trac/ticket/10049

* #10048 deprecate substitute_expression()
http://trac.sagemath.org/sage_trac/ticket/10048

Cheers,
Burcin

回复全部
回复作者
转发
0 个新帖子