Ask Your Question
1

sage worksheet indexing issues

asked 2024-09-23 09:20:46 +0100

vjelm gravatar image

I'm trying to use a sage worksheet for a much different problem, but the issue I'm having can be expressed concisely as follows:

Let's say I have some list A. I want to make several lists (call them MDN(j)) where each list has entries '0,' the 4th to jth elements of A, '99'.

In the end, when I ask it to print MDN(j), it just prints '0,' the 4th to max(j)th elements of A, '99.' That is, if I have j in [4,..,9] and ask it to print MDN(6), it will print MDN(9).

Why can't I access indexed MDN's? Is there a better way to do this?

A=[1,2,3,4,5,6,7,8,9,10,11,12,13]
print('A=',A)

M=[0]
print('M=',M)

N=[99]
print('N=',N)

for j in [4,..,9]:
    D=[A[i] for i in [1,2,..,j]]
    print('D=',D)
    MDN(j)=M+D+N
    print('M+D+N=',MDN(j))

#why is this printing MDN(9)?
print(MDN(6))

output: https://i.sstatic.net/tr6S2vsy.png

edit retag flag offensive close merge delete

Comments

John Palmieri gravatar imageJohn Palmieri ( 2024-09-23 19:48:37 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-09-23 19:28:27 +0100

Emmanuel Charpentier gravatar image

updated 2024-09-23 19:53:24 +0100

In your loop, the line :

    MDN(j)=M+D+N

doesn't do what you seem to think it does.

It (re-)defines a symbolic function (a. k. a. "callable symbolic expression") with one argument j and returning the current value of M+D+N (and ignores its argument j).

Your call

print(MDN(6))

is executed immediately after the last execution of your loop body, where D is [A[i] for i in (1,2,..,9]. Therefore, any call to the MDN function will return this value.

You seem to want to create a MDM list of lists, with at least 10 elements, and update its 5th to 10th elements to the values you generate. In order to do this, you must remember a few basic facts about Python lists and Sage's symbolic functions :

  • Lists are indexed by square brackets ([), not parentheses ( ().
  • Their indices start at 0, not 1.
  • Python does not accept indexing lists by list in general ; however, Python has slice indexing L[a:b] where a and b are integers (or variables with integer values), denoting the sublist of L sarting with its ath element and up to its b-1th element (in other word, a is the first included element and b is the first *excluded` element).
  • In Sage (not Python in general), the notation f(x, y...)=... (re-)defines a symbolic function (a. k. a. "callable symbolic expression") f with arguments x, y, etc... ; f(u, v) is the value of this symbolic expression applied to the arguments x=u and y=v.

There are a lot of good Python tutorials to choose from ; Sage has an excellent (if slightly outdated) tutorial book, also available as a printed book.

HTH,

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2024-09-23 09:15:13 +0100

Seen: 181 times

Last updated: Sep 23