# 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
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.

edit retag close merge delete

The 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?

Sort by » oldest newest most voted

Did 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)
([], [])

more