On Mon, Jul 31, 2023 at 03:28:23PM +0000, '68th' via FriCAS - computer algebra system wrote:
> I've got an expression as a result of some commands. How can I turn it to a function?
It is not clear why do you want a function, and it is quite
likely that what you want can be done more conveninet using
expressions.
If you really need a function you can start from following
expample:
expr := x + exp(y)
ei := convert(expr)@InputForm
interpret(function(ei, [x, y], fe))
This defines function named 'fe', with two arguments 'x' and 'y'
and which have code given by expression 'expr'. You can
use this function like:
fe(1, 2)
Compiling function fe with type (PositiveInteger, PositiveInteger)
-> Expression(Integer)
2
(5) %e + 1
Type: Expression(Integer)
Note: The code I gave defines "untyped" function, for each
combination of arguments types FriCAS on first use creates
typed variant: the "Compiling function fe with type ..." message
means that FriCAS created specialized version of the function
for specific argument types.
Some more explanation: FriCAS has special type called InputForm.
It represents parsed form of user input. But it is also possible
to create InputForm using computation and then pass it to FriCAS
for processing like user input. In particular expression can
be converted to InputForm. The line
ei := convert(expr)@InputForm
converts expression to InputForm. This when passed back to
FriCAS in appropriate type context would recreate original
expression. But we can also transform InputForm, in particular
function called 'function' transforms InputForm corresponding
to an expression into InputForm corresponding to a named function.
'interpret' just processes InputForm as if it came from user
input.
--
Waldek Hebisch