ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 27 Dec 2020 15:32:26 +0100Transform a list of commands in a programhttps://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/ 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.
Sun, 27 Dec 2020 02:23:09 +0100https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/Answer by slelievre for <p>I have that list of commands </p>
<pre><code>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()
</code></pre>
<p>The list os commands works if you choose m and n properly, but that is not practical. </p>
https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?answer=54941#post-id-54941Here 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
Sun, 27 Dec 2020 03:18:21 +0100https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?answer=54941#post-id-54941Comment by phcosta for <p>Here is what I would do</p>
<ul>
<li>format your code with one instruction per line</li>
<li>indent everything by four spaces</li>
<li>add an unindented <code>def something(m, n):</code> in front</li>
<li>decide what to return at the end</li>
</ul>
<p>For example:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?comment=54944#post-id-54944Thank you very much.Sun, 27 Dec 2020 15:09:35 +0100https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?comment=54944#post-id-54944Comment by slelievre for <p>Here is what I would do</p>
<ul>
<li>format your code with one instruction per line</li>
<li>indent everything by four spaces</li>
<li>add an unindented <code>def something(m, n):</code> in front</li>
<li>decide what to return at the end</li>
</ul>
<p>For example:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?comment=54948#post-id-54948What 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 examplesSun, 27 Dec 2020 15:32:26 +0100https://ask.sagemath.org/question/54940/transform-a-list-of-commands-in-a-program/?comment=54948#post-id-54948