Opened 8 years ago

# Handle substitutions of partial sums and products

Reported by: Owned by: Vincent Delecroix major sage-6.7 symbolics Michael Orlitzky, Marc Mezzarobba N/A

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.

### comment:1 Changed 8 years ago by Vincent Delecroix

Description: modified (diff)

### comment:2 follow-up:  3 Changed 8 years ago by Nils Bruin

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 8 years ago by Vincent Delecroix

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 7 years ago by Marc Mezzarobba

Description: modified (diff)

### comment:5 Changed 5 years ago by Ralf Stephan

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

### comment:6 Changed 5 years ago by Vincent Delecroix

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