What is Fricas equivalent to Maple op command (extract operands from expression)

37 views
Skip to first unread message

Nasser M. Abbasi

unread,
Dec 23, 2023, 3:42:13 PM12/23/23
to FriCAS - computer algebra system
Maple has very useful command op(expression)  which basically extracts operands of the expression to an expression sequence. For example, gives two lists and if I want to join the two lists into one list, I could do

L1:=["A","B","C"];
L2:=["E","F","G"];
L3:=[ op(L1) , op(L2) ];

              L3 := ["A", "B", "C", "E", "F", "G"]
How would one do the same thing in Fricas? concat() does not work. Also concat only works on string while the above Maple code works on any type of list.

Does Fricas have something similar to Maple's op?

Thanks
--Nasser

Grégory Vanuxem

unread,
Dec 23, 2023, 3:49:11 PM12/23/23
to fricas...@googlegroups.com
Hello,

This is not what you are looking for but you can use ´append’ eventually if necessary with ´map’´. For sure there more command for that.

And sorry for thé bad quote character.


--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/e868f60b-4626-4246-9cc3-e1f8b89a7d34n%40googlegroups.com.

Nasser M. Abbasi

unread,
Dec 23, 2023, 3:53:04 PM12/23/23
to FriCAS - computer algebra system
What I meant to say, the lists in Fricas have to be same type, else it will not work with concat. Here is an example

20) -> L1:=[1,2,3]      
(23) -> L2:=["D","E","F"]
(24) -> concat(L1,L2)    
    gives error.

But in Maple using op()

L1:=[1,2,3];

L2:=["E","F","G"];
L3:=[ op(L1) , op(L2) ];

          L3 := [1, 2, 3, "E", "F", "G"]

op() also work on any expression, 

op(sin(x)+cos(x)+exp(y))
gives
                        sin(x), cos(x), exp(y)

Does Fricas have op()?  whe I did ?op it said no. How does one then extract all the operands of expression in Fricas?

--Nasser

Grégory Vanuxem

unread,
Dec 23, 2023, 4:30:15 PM12/23/23
to fricas...@googlegroups.com
Le sam. 23 déc. 2023 à 21:53, 'Nasser M. Abbasi' via FriCAS - computer
algebra system <fricas...@googlegroups.com> a écrit :
>
> What I meant to say, the lists in Fricas have to be same type, else it will not work with concat. Here is an example
>
> 20) -> L1:=[1,2,3]
> (23) -> L2:=["D","E","F"]
> (24) -> concat(L1,L2)
> gives error.
>
> But in Maple using op()
>
> L1:=[1,2,3];
> L2:=["E","F","G"];
> L3:=[ op(L1) , op(L2) ];
>
> L3 := [1, 2, 3, "E", "F", "G"]

Here concat(L1,L2)@List(Any)

> op() also work on any expression,
>
> op(sin(x)+cos(x)+exp(y))
> gives
> sin(x), cos(x), exp(y)

Here I would do:
kernels(sin(x)+cos(x)-exp(y))
=>
y
(4) [sin(x), %e , cos(x)]
Type: List(Kernel(Expression(Integer)))


I stop here, I am not at all an Expression specialist, sorry.

But I'm pretty sure there is no such operator unless you "destruct"
your expression, in InputForm or something like that, that is to a near Lisp
level.

Regards,

- Greg


> Does Fricas have op()? whe I did ?op it said no. How does one then extract all the operands of expression in Fricas?
>
> --Nasser
>
> On Saturday, December 23, 2023 at 2:42:13 PM UTC-6 Nasser M. Abbasi wrote:
>>
>> Maple has very useful command op(expression) which basically extracts operands of the expression to an expression sequence. For example, gives two lists and if I want to join the two lists into one list, I could do
>>
>> L1:=["A","B","C"];
>> L2:=["E","F","G"];
>> L3:=[ op(L1) , op(L2) ];
>>
>> L3 := ["A", "B", "C", "E", "F", "G"]
>> How would one do the same thing in Fricas? concat() does not work. Also concat only works on string while the above Maple code works on any type of list.
>>
>> Does Fricas have something similar to Maple's op?
>>
>> Thanks
>> --Nasser
>
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/2ee3f825-3af7-4fba-bba5-91ae7afa40f2n%40googlegroups.com.

Ralf Hemmecke

unread,
Dec 23, 2023, 4:33:40 PM12/23/23
to fricas...@googlegroups.com
On 12/23/23 21:53, 'Nasser M. Abbasi' via FriCAS - computer algebra
system wrote:
> What I meant to say, the lists in Fricas have to be same type, else it will
> not work with concat. Here is an example
>
> 20) -> L1:=[1,2,3]
> (23) -> L2:=["D","E","F"]
> (24) -> concat(L1,L2)
> gives error.

Yes. And it should give an error. Elements of a list must be of the same
type.

However...

You can create a Union type.

%%% (1) -> l1:=[1,2,3]

(1) [1, 2, 3]
Type: List(PositiveInteger)
%%% (2) -> l2:=["D","E","F"]

(2) ["D", "E", "F"]
Type: List(String)
%%% (3) -> U ==> Union(Integer, String)
Type: Void
%%% (4) -> LU ==> List U
Type: Void
%%% (5) -> l1::LU

(5) [1, 2, 3]
Type: List(Union(Integer,String))
%%% (6) -> l2::LU

(6) ["D", "E", "F"]
Type: List(Union(Integer,String))
%%% (7) -> concat(l1::LU, l2::LU)

(7) [1, 2, 3, "D", "E", "F"]
Type: List(Union(Integer,String))

FriCAS also has a type "Any". So you could do

%%% (8) -> concat(l1::List(Any), l2::List(Any))

(8) [1, 2, 3, "D", "E", "F"]
Type: List(Any)


'Any', however, is a weird thing. It can somehow be considered as the
all-type, but on the other hand, an element of it is actually a pair,
consisting of the value and the type for that value. So it is NOT the
same as forgetting about types.

Anyway, I am not sure that you really want to use Any in your program.

> But in Maple using op()
>
> L1:=[1,2,3];
> L2:=["E","F","G"];
> L3:=[ op(L1) , op(L2) ];
>
> L3 := [1, 2, 3, "E", "F", "G"]
>
> op() also work on any expression,
>
> op(sin(x)+cos(x)+exp(y))
> gives
> sin(x), cos(x), exp(y)
>
> Does Fricas have op()? whe I did ?op it said no. How does one then extract
> all the operands of expression in Fricas?

The sad answer is "no". FriCAS does not have op() that is like in Maple,
but that is due to the fact that FriCAS works fundamentally differently.

First, you should know, what the type your "expression" actually is.
Say

)type on

and the type of your expression will be shown (similar to above).
If it is 'Expression(Integer)' then look at

https://fricas.github.io/api/Expression.html

and check which function could apply. In particular for
Expression(Integer) you should understand that it actually is a rational
function an many "variables". These variables are called "kernels"
(FriCAS type 'Kernel').

You can list all the kernels of your expression by calling

kernels(expr)

Let us take

%%% (1) -> expr := sin(x)^2 + tan(x)*x^3

3 2
(1) x tan(x) + sin(x)
Type: Expression(Integer)
%%% (2) -> kernels expr

(2) [tan(x), sin(x), x]
Type: List(Kernel(Expression(Integer)))

%%% (3) -> isPlus expr

3 2
(3) [x tan(x), sin(x) ]
Type: Union(List(Expression(Integer)),...)

I hope, from here you can get a bit further.

Ralf

Waldek Hebisch

unread,
Dec 23, 2023, 4:38:53 PM12/23/23
to 'Nasser M. Abbasi' via FriCAS - computer algebra system
On Sat, Dec 23, 2023 at 12:42:12PM -0800, 'Nasser M. Abbasi' via FriCAS - computer algebra system wrote:
> Maple has very useful command op(expression)
> <https://www.maplesoft.com/support/help/maple/view.aspx?path=op> which
> basically extracts operands of the expression to an expression sequence.
> For example, gives two lists and if I want to join the two lists into one
> list, I could do
>
> L1:=["A","B","C"];
> L2:=["E","F","G"];
> L3:=[ op(L1) , op(L2) ];
>
> L3 := ["A", "B", "C", "E", "F", "G"]
> How would one do the same thing in Fricas? concat() does not work.

I another mail you say that you want list of different types
of entries. That is not possible in FriCAS: if you want
things in as single list you need common type, like:

(12) -> L1:=[1,2,3]

(12) [1, 2, 3]
Type: List(PositiveInteger)
(13) -> L2:=["D","E","F"]

(13) ["D", "E", "F"]
Type: List(String)
(14) -> concat(L1::List(Any), L2)

(14) [1, 2, 3, "D", "E", "F"]
Type: List(Any)

Note: you can embed any FriCAS type into Any, but once you did this
you loose many nice properties. In particular FriCAS chooses
operations based on types, but there are only few operations
applicable to Any. Normally you would like much more specific
type.

> Also
> concat only works on string while the above Maple code works on any type of
> list.

In FriCAS 'concat' is overloaded. Above it was 'concat' for
UnaryRecursiveAggregate, which means lists and a bit more.
There is also 'concat' for 'LinearAggregate', in particular vectors
and strings.

> Does Fricas have something similar to Maple's op?

You did not say what you want to do. If goal is transforming
expressions, then FriCAS has tools to do this, but they are
different. Note that FriCAS expressions are different than
Maple expressions.

If you want "untyped" computations, then you are really fighting
against FriCAS: one of core principles in FriCAS is that operations
should be "well typed", that is operate on specific types and
produce result of specific type. FriCAS types are pretty general
so this usually is not a trouble. There are possibilites do
do "untyped" things, say using Any or SExpession, but you
should first try to fit things into normal FriCAS types.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages