Ask Your Question
1

Transform a list of commands in a program

asked 2020-12-27 02:23:09 +0100

anonymous user

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)[0]); 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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-27 03:18:21 +0100

slelievre gravatar image

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
edit flag offensive delete link more

Comments

Thank you very much.

phcosta gravatar imagephcosta ( 2020-12-27 15:09:35 +0100 )edit

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
slelievre gravatar imageslelievre ( 2020-12-27 15:32:26 +0100 )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: 2020-12-27 02:23:09 +0100

Seen: 538 times

Last updated: Dec 27 '20