Opened 8 years ago

# solve() breaks substitute_function()

Reported by: Owned by: Lee Worden major sage-6.4 symbolics Jakob Kroeker N/A

### Description

Seems like this should work differently:

```\$ sage
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.4.1, Release Date: 2014-11-23                       │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: z = sage.symbolic.function_factory.function('z',nargs=1)
sage: z_eq = ( z(x) == x/(1-x) )
sage: z_eq.substitute_function( z, (x^2).function(x) )
x^2 == -x/(x - 1)
sage: x_expr = solve( z_eq, x )[0].rhs()
sage: x_expr
z(x)/(z(x) + 1)
sage: x_expr.substitute_function( z, (x^2).function(x) )
z(x)/(z(x) + 1)
```

I believe it should replace `z(x)` with `x^2` in the last result.

### comment:2 Changed 8 years ago by Nils Bruin

I think the problem here is that we can't really support/recognize the "nargs" argument, but we do have:

```sage: sage.symbolic.function_factory.function('z') == sage.symbolic.function_factory.function('z',nargs=1)
False
```

When we see the symbol appear on the maxima side, we have no idea whether it's a variadic function or one with a fixed number of arguments. Apparently, the interface presently chooses to go with variadic, since that is most generic. Indeed, if you change the first line to

```sage: z= sage.symbolic.function_factory.function('z')
```

there is no problem.

Further problems arise with bare functions, because that concept doesn't exist in the maxima interface and/or maxima itself:

```sage: f= sage.symbolic.function_factory.function('f',nargs=2)
sage: maxima_calculus(f)._sage_()(x)
x
sage: maxima_calculus(f)._sage_()(f=x)
x
sage: maxima_calculus(f)._sage_()(x,x)
ValueError: the number of arguments must be less than or equal to 1
```

### comment:3 follow-up:  4 Changed 8 years ago by Lee Worden

Wouldn't it be best to record what symbols sage is sending to maxima, and translate symbols in maxima's output back to the same things they corresponded to in the input?

### comment:4 in reply to:  3 Changed 8 years ago by Nils Bruin

Wouldn't it be best to record what symbols sage is sending to maxima, and translate symbols in maxima's output back to the same things they corresponded to in the input?

We do that to some extent, but lack of scoping makes this unpredictable:

```sage: f=function('f')
sage: F1=maxima_calculus(f(x))
sage: f=function('f',nargs=2)
sage: F2=maxima_calculus(f(x,x))
```

Since we're already mangling names for variables, one possibility would be to mangle any attributes into the names of functions, in which we'd get something along the lines:

```sage: f=function('f',nargs=2)
sage: maxima_calculus(f(x,x)) #we could do it like this:
'_SAGE_FUNCTION_NARGS_2_f(_SAGE_VAR_x,_SAGE_VAR_x)
```

### comment:5 Changed 8 years ago by Lee Worden

I see, yeah.

one possibility would be to mangle any attributes into the names of functions

Maybe streamlined by only using mangled names for non-variadic functions. Or only when a function comes up whose simple name is already in use...