Ask Your Question

To give a name to an append list

asked 2020-10-05 09:48:16 +0200

Cyrille gravatar image

updated 2020-10-05 10:44:36 +0200

slelievre gravatar image

If I code this

A = matrix([[1/2, 1/4, 0], [1, 3, -1], [1, 1, 0]])
b = vector([5, 18,10])
c = vector([102, 303, -100])
t = matrix(A.augment(b))
c = c.list()
show(t, c)

SageMath returns what is expected. But if I want to change the name of c.append(0)? It returns None, so I cannot reuse the augmented vector.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-10-05 10:37:53 +0200

slelievre gravatar image

updated 2020-10-06 00:40:56 +0200

The method append modifies the list in place.

It does not return anything (or equivalently, it returns None).

It has the side-effect of modifying the list.

Doing d = c.append(0) assigns the result of c.append(0) (i.e., None) to d.

To get a new list consisting of the former value of c with one extra element 0, use:

sage: d = c + [0]

Recommended: go through a few of SageMath's thematic tutorials, especially those in the section "Introduction to Python".

In a comment after my initial answer, there is a follow up question about Python equivalents of some Mathematica functions for operating on lists: RotateLeft, RotateRight, PadLeft, PadRight (the links point to the Wolfram language reference manual entries for those functions).

Implementing their equivalent in Sage is a good exercise after going through the tutorial.

One could make two versions, one that rotates or pads inplace, one that returns the result.

Here is one way to rotate right one step, inplace:

sage: a = [0, 3, 9, 5, 6, 8, 4]
sage: a[:0] = [a.pop()]
sage: a
[4, 0, 3, 9, 5, 6, 8]

Here is a rotated_right function:

def rotated_right(a, n=1):
    if len(a) < 2 or n % len(a) == 0:
        return a[:]
    n = n % len(a)
    return a[-n:] + a[:-n]

A pad_right function and a padded_right function:

def pad_right(a, n=1)
    if len(a) < n:
        a.extend([0] * (n - len(a)))

def padded_right(a, n=1)
    if len(a) < n:
        return a[:] + [0] * (n - len(a))
    return a[:]

Further recommendations:

edit flag offensive delete link more


Ok my question was stupid, but I wonder if there is some implementation of mathematica RotateLeftRotateRightPadLeft, PadRight.... which are very usefull functions.

Cyrille gravatar imageCyrille ( 2020-10-05 15:42:41 +0200 )edit

Writing such functions would be an excellent exercise for learning Python's basic list management... I second slelievre's recommendation, and remind you of a previous recommendation about this book...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-10-05 18:02:50 +0200 )edit

I second @Emmanuel Charpentier's recommendation to read "Calcul mathématique avec Sage". I learned a lot from it.

slelievre gravatar imageslelievre ( 2020-10-06 00:43:17 +0200 )edit

You are both particularly right. But, notice there is always a but, I have a huge lot of courses and I postpone always to learn Python seriously, because of a lack of time.

Cyrille gravatar imageCyrille ( 2020-10-06 10:34:56 +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


Asked: 2020-10-05 09:48:16 +0200

Seen: 117 times

Last updated: Oct 06 '20