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.Wed, 02 Jun 2021 18:21:19 +0200Function which works with a vector define from outside not from insidehttps://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/(Sorry, I fell short of imagination in the title of the question)
One more time I am surprise by what I do not understand. Here is a working code
def one_dimension_less(P,c):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
with a good call :
A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=vector([1,0,0])
one_dimension_less(P,c)
Until there it's a simple projection on `c`. As, in the exemple, `c` could be `(1,0,0)`, `(0,1,0)` or `(0,0,1)` it comes to my mind that I can prepare the call to the function according to :
A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=zero_vector(3)
c[1]=1
show(c)
one_dimension_less(P,c)
So in changing the index of 1 in `c`, I can change the projection. Now It cames to my mind that in passing only the index in the function I can obtain directly what I expect without more preparation than the `A` matrix and the `c` vector. For that I change the function to
def one_dimension_less(P,param):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
c=zero_vector(m)
c[param]=1
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
Now the call
one_dimension_less(P,0)
return the error :
unsupported operand parent(s) for *: 'Full MatrixSpace of 1 by 3 dense matrices over Integer Ring' and 'Ambient free module of rank 6 over the principal ideal domain Integer Ring'.
I think to understand that this is a ring problem but I do not see why it works from outside and not from inside the function.
Wed, 02 Jun 2021 17:54:37 +0200https://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/Comment by slelievre for <p>(Sorry, I fell short of imagination in the title of the question)</p>
<p>One more time I am surprise by what I do not understand. Here is a working code</p>
<pre><code>def one_dimension_less(P,c):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
</code></pre>
<p>with a good call :</p>
<pre><code>A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=vector([1,0,0])
one_dimension_less(P,c)
</code></pre>
<p>Until there it's a simple projection on <code>c</code>. As, in the exemple, <code>c</code> could be <code>(1,0,0)</code>, <code>(0,1,0)</code> or <code>(0,0,1)</code> it comes to my mind that I can prepare the call to the function according to :</p>
<pre><code>A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=zero_vector(3)
c[1]=1
show(c)
one_dimension_less(P,c)
</code></pre>
<p>So in changing the index of 1 in <code>c</code>, I can change the projection. Now It cames to my mind that in passing only the index in the function I can obtain directly what I expect without more preparation than the <code>A</code> matrix and the <code>c</code> vector. For that I change the function to</p>
<pre><code>def one_dimension_less(P,param):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
c=zero_vector(m)
c[param]=1
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
</code></pre>
<p>Now the call</p>
<pre><code>one_dimension_less(P,0)
</code></pre>
<p>return the error :</p>
<p>unsupported operand parent(s) for *: 'Full MatrixSpace of 1 by 3 dense matrices over Integer Ring' and 'Ambient free module of rank 6 over the principal ideal domain Integer Ring'.</p>
<p>I think to understand that this is a ring problem but I do not see why it works from outside and not from inside the function.</p>
https://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/?comment=57416#post-id-57416The error message says you are trying to multiply a 1 x 3 matrix by a vector of size 6.
Leftover value of `P` from a different example?Wed, 02 Jun 2021 18:21:19 +0200https://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/?comment=57416#post-id-57416Answer by slelievre for <p>(Sorry, I fell short of imagination in the title of the question)</p>
<p>One more time I am surprise by what I do not understand. Here is a working code</p>
<pre><code>def one_dimension_less(P,c):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
</code></pre>
<p>with a good call :</p>
<pre><code>A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=vector([1,0,0])
one_dimension_less(P,c)
</code></pre>
<p>Until there it's a simple projection on <code>c</code>. As, in the exemple, <code>c</code> could be <code>(1,0,0)</code>, <code>(0,1,0)</code> or <code>(0,0,1)</code> it comes to my mind that I can prepare the call to the function according to :</p>
<pre><code>A=matrix([[1,2,3],[1,2,1],[2,2,1]])
b=vector([1,1,1])
c=zero_vector(3)
c[1]=1
show(c)
one_dimension_less(P,c)
</code></pre>
<p>So in changing the index of 1 in <code>c</code>, I can change the projection. Now It cames to my mind that in passing only the index in the function I can obtain directly what I expect without more preparation than the <code>A</code> matrix and the <code>c</code> vector. For that I change the function to</p>
<pre><code>def one_dimension_less(P,param):
A,b = P
m = A.nrows();
M = range(0,m)
n = A.ncols()
c=zero_vector(m)
c[param]=1
N = [i for i in M if A[i,:]*c < 0]
Z = [i for i in M if A[i,:]*c == 0]
P = [i for i in M if A[i,:]*c > 0]
p = Z + [(i,j) for i in N for j in P]
r = len(p)
D = Matrix(r,n); d = Matrix(r,1)
for i in range(0,r):
if not isinstance(p[i],tuple):
D[i,:] = A[p[i],:]
d[i] = b[p[i]]
else:
(s,t) = p[i]
D[i,:] = (A[t,:]*c)*A[s,:] - (A[s,:]*c)*A[t,:]
d[i] = (A[t,:]*c)*b[s] - (A[s,:]*c)*b[t]
return (D,d)
</code></pre>
<p>Now the call</p>
<pre><code>one_dimension_less(P,0)
</code></pre>
<p>return the error :</p>
<p>unsupported operand parent(s) for *: 'Full MatrixSpace of 1 by 3 dense matrices over Integer Ring' and 'Ambient free module of rank 6 over the principal ideal domain Integer Ring'.</p>
<p>I think to understand that this is a ring problem but I do not see why it works from outside and not from inside the function.</p>
https://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/?answer=57415#post-id-57415Did you use the value of `P` you had in mind?
Did you try this in a fresh Sage session?
Here is what I get.
Define the last version of your function:
def one_dimension_less(P, param):
A, b = P
m = A.nrows()
M = range(m)
n = A.ncols()
c = zero_vector(m)
c[param] = 1
N = [i for i in M if A[i, :]*c < 0]
Z = [i for i in M if A[i, :]*c == 0]
P = [i for i in M if A[i, :]*c > 0]
p = Z + [(i, j) for i in N for j in P]
r = len(p)
D = Matrix(r, n)
d = Matrix(r, 1)
for i in range(0, r):
if not isinstance(p[i], tuple):
D[i, :] = A[p[i], :]
d[i] = b[p[i]]
else:
s, t = p[i]
D[i, :] = (A[t, :]*c)*A[s, :] - (A[s, :]*c)*A[t, :]
d[i] = (A[t, :]*c)*b[s] - (A[s, :]*c)*b[t]
return (D, d)
Run the suggested example:
sage: A = matrix([[1, 2, 3], [1, 2, 1], [2, 2, 1]])
sage: b = vector([1, 1, 1])
sage: one_dimension_less(P, 0)
Traceback (most recent call last)
...
NameError: name 'P' is not defined
Or, guessing that `P` should be `(A, b)`:
sage: P = (A, b)
sage: one_dimension_less(P, 0)
([], [])
Wed, 02 Jun 2021 18:18:40 +0200https://ask.sagemath.org/question/57411/function-which-works-with-a-vector-define-from-outside-not-from-inside/?answer=57415#post-id-57415