Ask Your Question

# Transform a list of commands in a program Anonymous

I have that list of commands

X = [SR.var("x%d"%i) for i in [1..n]]; b = vector(random_matrix(QQ,1,m)); A = matrix(QQ,m,n);
A[0, -n:] = ones_matrix(1,n);
for i in [1..m-2]:
A[i, i:i+2] = ones_matrix(1,2); eq = [A[i]*vector(X) == b[i] for i in range(m)] ;  a = matrix([[e.lhs().coefficient(v) for v in X] for eqin eq]) ; C= A.augment(b); D = C.right_kernel()


The list os commands works if you choose m and n properly, but that is not practical.

edit retag close merge delete

## 1 Answer

Sort by » oldest newest most voted

Here is what I would do

• format your code with one instruction per line
• indent everything by four spaces
• add an unindented def something(m, n): in front
• decide what to return at the end

For example:

def something(m, n):
x = lambda i: SR.var(f"x_{i}", latex_name=f"x_{{{i}}}")
X = vector([x(i) for i in [1 .. n]])
b = VectorSpace(QQ, m).random_element()
A = matrix(QQ, m, n)
A[0, :] = ones_matrix(1, n)
for i in [1 .. m - 2]:
A[i, i:i + 2] = ones_matrix(1, 2)
eq = [A[i]*X == b[i] for i in range(m)]
a = matrix([[e.lhs().coefficient(v) for v in X] for e in eq])
C = A.augment(b)
D = C.right_kernel()
return X, b, A, eq, a, C, D

more

## Comments

What remains to do:

• find a more descriptive name for the function
• add a documentation string which
• says what the function does
• documents the input and output
• provides a few examples

## Your Answer

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

Add Answer

## Stats

Asked: 2020-12-27 02:23:09 +0200

Seen: 42 times

Last updated: Dec 27 '20