Opened 5 years ago

Last modified 2 years ago

#18396 new enhancement

Handle substitutions of partial sums and products

Reported by: vdelecroix Owned by:
Priority: major Milestone: sage-6.7
Component: symbolics Keywords:
Cc: mjo, mmezzarobba Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by vdelecroix)

Sage is not able to identify partial sum in a substitution

sage: var('x,y')
sage: f = x + x^2 + x^4
sage: f.subs(x^2 == y)             # one term is fine
x^4 + x + y
sage: f.subs(x + x^2 == y)         # partial sum does not work
x^4 + x^2 + x
sage: f.subs(x + x^2 + x^4 == y)   # whole sum is fine
y

Similarly with products

sage: f = x * cos(x) * sin(x)
sage: f.subs( cos(x) * sin(x) == y)
x*cos(x)*sin(x)

As mentioned in the doc, this is the same behavior as in Maple but differ from Mathematica. We should be clearer on the semantic of substitute and potentially implement partial sum and product substitutions.

See also #10049 and ​http://ask.sagemath.org/question/25972/substitute-xy-by-u/ (with related ticket #17879)

Change History (6)

comment:1 Changed 5 years ago by vdelecroix

  • Description modified (diff)

comment:2 follow-up: Changed 5 years ago by nbruin

I'm not so sure we have to do more than document it. Obviously you cannot expect substitutions to happen on any "equal" subexpression, since that concept isn't well-defined.

The thing is: x+x^2 isn't a syntactical subunit of x + x^2 + x^4 for the internal representation, which is roughly ('+',x,('^',x,2)) versus ('+',x,('^',x,2),('^',x,4))

You'll have to decide how much tricks are worthwhile to implement before you just add the relation y-(x^2+x) and ask for elimination of x via groebner bases.

comment:3 in reply to: ↑ 2 Changed 5 years ago by vdelecroix

Replying to nbruin:

I'm not so sure we have to do more than document it. Obviously you cannot expect substitutions to happen on any "equal" subexpression, since that concept isn't well-defined.

I do not want to substitute "equal" subexpression but only identical ones. And doing so, I want to consider 'a+c' as a unit of 'a+b+c+d' and 'a*c' as a unit in 'a*b*c*d'. This is perhaps not desirable though.

The thing is: x+x^2 isn't a syntactical subunit of x + x^2 + x^4 for the internal representation, which is roughly ('+',x,('^',x,2)) versus ('+',x,('^',x,2),('^',x,4))

I know, and this is precisely the purpose of the ticket.

You'll have to decide how much tricks are worthwhile to implement before you just add the relation y-(x^2+x) and ask for elimination of x via groebner bases.

Note that x + y - (u + v) does not exist. But I agree that there is an ambiguous +/- issue (as far as the ticket description is concerned).

comment:4 Changed 4 years ago by mmezzarobba

  • Description modified (diff)

comment:5 Changed 2 years ago by rws

However, even the whole matching does not work consistently. See https://github.com/pynac/pynac/issues/269

comment:6 Changed 2 years ago by vdelecroix

  • Description modified (diff)
Note: See TracTickets for help on using tickets.