Ask Your Question
0

Concatenation of list of variables

asked 2023-04-26 12:09:27 +0200

Cyrille gravatar image

updated 2023-04-26 12:10:21 +0200

Suppose I have three variables x,y,z. For pratical reasons, I want to define :

varx = var('x y')
varl = var('z')

A problem arrise immediately

type(varx)

returns <class 'tuple'>

when

type(varl)

returns <class 'sage.symbolic.expression.Expression'>. As they are not of the same (i can understand why but it seems a little weird) type, it's not possible to concatenate them. What is strange is that

[varl]

is a list, but

list(varl)

is not because varl is not enumerable. Finally,

list(varx)+[varl]

do what I was expecting (concatenation), but it was after a long time of essais and error. Could some one explain the logic behind this ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-04-26 15:25:56 +0200

Max Alekseyev gravatar image

updated 2023-04-26 15:28:21 +0200

varx = var('x y') defines a tuple of two variables, which can be accessed as varx[0] and varx[1], which have names x and y respectively.

list(varl) is undefined as its requests conversion of a variable varl into a list. It's not the same as creating a list composed of a single variable, which would be [varl] as you noticed. And in general, list(obj) is not the same as [obj] where obj is any object.

list(varx) converts the tuple varx into a list (still composed of two variables), which enables further concatenations with other lists, like list(varx)+[varl].

edit flag offensive delete link more

Comments

But a one-tuple is a tuple. This was the origin of my question.

Cyrille gravatar imageCyrille ( 2023-04-26 19:11:11 +0200 )edit

You don't have one-tuples in your question. If you mean obj and (obj,) - they are different objects for any obj.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-04-26 20:03:27 +0200 )edit

var behaves inconsistently, indeed.

Complain fiercely (or file an issue...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-27 01:33:13 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-04-26 12:09:27 +0200

Seen: 62 times

Last updated: Apr 26 '23