functions

10 views
Skip to first unread message

68th

unread,
Jul 31, 2023, 11:28:37 AM7/31/23
to '68th' via FriCAS - computer algebra system
I've got an expression as a result of some commands. How can I turn it to a function?

Waldek Hebisch

unread,
Aug 1, 2023, 5:48:56 PM8/1/23
to '68th' via FriCAS - computer algebra system
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

68th

unread,
Aug 3, 2023, 11:47:49 AM8/3/23
to fricas...@googlegroups.com
------- Original Message -------
On Tuesday, August 1st, 2023 at 9:48 PM, Waldek Hebisch <de...@fricas.math.uni.wroc.pl> wrote:

> ... it is quite
> likely that what you want can be done more conveninet using
> expressions.

How could expressions be easier than doing 'fe(1, 2)' once you have a function and a number of values as its different pairs of arguments?

> ei := convert(expr)@InputForm
> interpret(function(ei, [x, y], fe))

Thank you very much!

Waldek Hebisch

unread,
Aug 4, 2023, 9:18:07 AM8/4/23
to '68th' via FriCAS - computer algebra system
On Thu, Aug 03, 2023 at 03:47:29PM +0000, '68th' via FriCAS - computer algebra system wrote:
> ------- Original Message -------
> On Tuesday, August 1st, 2023 at 9:48 PM, Waldek Hebisch <de...@fricas.math.uni.wroc.pl> wrote:
>
> > ... it is quite
> > likely that what you want can be done more conveninet using
> > expressions.
>
> How could expressions be easier than doing 'fe(1, 2)' once you have a function and a number of values as its different pairs of arguments?

If you need to evaluate at many points, then functions are easier.
However, if you need manipulations/transformations, then possibilities for functions are quite limited, and expressions allow much more.

--
Waldek Hebisch

68th

unread,
Aug 6, 2023, 6:12:57 AM8/6/23
to fricas...@googlegroups.com
------- Original Message -------
On Friday, August 4th, 2023 at 1:18 PM, Waldek Hebisch <de...@fricas.math.uni.wroc.pl> wrote:

> If you need to evaluate at many points, then functions are easier.
> However, if you need manipulations/transformations, then possibilities > for functions are quite limited, and expressions allow much more.

Yes, that's what I thought.
Reply all
Reply to author
Forward
0 new messages