sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.32k stars 453 forks source link

solve() breaks substitute_function() #17504

Open a46d73b2-c70f-469a-b7c4-f2ca845e72d5 opened 9 years ago

a46d73b2-c70f-469a-b7c4-f2ca845e72d5 commented 9 years ago

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.

CC: @sagetrac-jakobkroeker

Component: symbolics

Issue created by migration from https://trac.sagemath.org/ticket/17504

nbruin commented 9 years ago
comment:2

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
a46d73b2-c70f-469a-b7c4-f2ca845e72d5 commented 9 years ago
comment:3

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?

nbruin commented 9 years ago
comment:4

Replying to @sagetrac-wonder:

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)
a46d73b2-c70f-469a-b7c4-f2ca845e72d5 commented 9 years ago
comment:5

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...