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.Sat, 26 Feb 2022 19:32:46 +0100How to define a matrix conditional to a list of stringshttps://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/Need a little help.
suppose I have the following list :
signs=['=','=','>=','>=','>=','<=','<=','<=','<=','<=']
with this list I need to create in Sagemath a matrix with 10 lines and 18 columns each equality/inequality sign correspond to a line with
'=' gives 1 on the corresponding line the other 17 enters being 0
'>=' gives - 1, 1 on the corresponding line the other 16 enters being 0
'<=' gives 1, 1 on the corresponding line the other 16 enters being 0
and of course, there is only one 1 (-1) in each column.
What I want is a function. Someone will tell me that I must learn Python (smile) but each of my tentatives fails.
Thank in advance for the helpSat, 26 Feb 2022 17:00:10 +0100https://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/Answer by John Palmieri for <p>Need a little help.
suppose I have the following list :</p>
<pre><code>signs=['=','=','>=','>=','>=','<=','<=','<=','<=','<=']
</code></pre>
<p>with this list I need to create in Sagemath a matrix with 10 lines and 18 columns each equality/inequality sign correspond to a line with</p>
<pre><code>'=' gives 1 on the corresponding line the other 17 enters being 0
'>=' gives - 1, 1 on the corresponding line the other 16 enters being 0
'<=' gives 1, 1 on the corresponding line the other 16 enters being 0
</code></pre>
<p>and of course, there is only one 1 (-1) in each column.</p>
<p>What I want is a function. Someone will tell me that I must learn Python (smile) but each of my tentatives fails.</p>
<p>Thank in advance for the help</p>
https://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/?answer=61295#post-id-61295How about this:
signs = ['=','=','>=','>=','>=','<=','<=','<=','<=','<=']
# lines: will be the rows of the matrix
lines = []
# column: where to put the next nonzero entry
column = 0
# total number of columns in matrix
size = sum(len(s) for s in signs)
for s in signs:
if s == '=':
new = column * [0] + [1] + (size - column - 1) * [0]
column += 1
elif s == '>=':
new = column * [0] + [-1, 1] + (size - column - 2) * [0]
column += 2
elif s == '<=':
new = column * [0] + [1, 1] + (size - column - 2) * [0]
column += 2
lines.append(new)
mat = matrix(lines)
Sat, 26 Feb 2022 17:41:33 +0100https://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/?answer=61295#post-id-61295Answer by Max Alekseyev for <p>Need a little help.
suppose I have the following list :</p>
<pre><code>signs=['=','=','>=','>=','>=','<=','<=','<=','<=','<=']
</code></pre>
<p>with this list I need to create in Sagemath a matrix with 10 lines and 18 columns each equality/inequality sign correspond to a line with</p>
<pre><code>'=' gives 1 on the corresponding line the other 17 enters being 0
'>=' gives - 1, 1 on the corresponding line the other 16 enters being 0
'<=' gives 1, 1 on the corresponding line the other 16 enters being 0
</code></pre>
<p>and of course, there is only one 1 (-1) in each column.</p>
<p>What I want is a function. Someone will tell me that I must learn Python (smile) but each of my tentatives fails.</p>
<p>Thank in advance for the help</p>
https://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/?answer=61297#post-id-61297In a compact form:
signs = ['=','=','>=','>=','>=','<=','<=','<=','<=','<=']
shift = [sum(len(signs[i]) for i in range(j)) for j in range(len(signs))]
Matrix(ZZ, 10, 18, lambda i,j: 0 if j<shift[i] or j>shift[i]+1 else -1 if j==shift[i] and signs[i]=='>=' else 0 if j==shift[i]+1 and signs[i]=='=' else 1)
Sat, 26 Feb 2022 19:32:46 +0100https://ask.sagemath.org/question/61294/how-to-define-a-matrix-conditional-to-a-list-of-strings/?answer=61297#post-id-61297