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.Fri, 24 Nov 2023 07:00:37 +0100Solving for a matrix.https://ask.sagemath.org/question/74346/solving-for-a-matrix/I have an m x n matrix A and an m x n matrix of variables X. I want to solve for all possible X such that AX^T = XA^T and A^TX = X^TA. Is there any nice way to do this?
I've tried using various commands like "Solve" but "solve_right" but it doesn't seem to be working. Here is my code so far.
A = matrix([[0,1],[1,0],[1,1]])
xrownum = A.nrows()
xcolnum = A.ncols()
X = matrix([[var("x_{}_{}".format(u,v), latex_name="a_{{{},{}}}".format(u,v)) for v in range(xcolnum)] for u in range(xrownum)]);
for i in range(xrownum):
for j in range(xcolnum):
if A[i,j] != 0:
X[i,j] = 0
XAT = X*A.transpose()
AXT = A*X.transpose()
ATX = A.transpose()*X
XTA = X.transpose()*A
Of course, X = 0 will always work, but I am interested in knowing if it is the ONLY matrix which works.Thu, 16 Nov 2023 19:07:11 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/Comment by Max Alekseyev for <p>I have an m x n matrix A and an m x n matrix of variables X. I want to solve for all possible X such that AX^T = XA^T and A^TX = X^TA. Is there any nice way to do this?</p>
<p>I've tried using various commands like "Solve" but "solve_right" but it doesn't seem to be working. Here is my code so far.</p>
<pre><code>A = matrix([[0,1],[1,0],[1,1]])
xrownum = A.nrows()
xcolnum = A.ncols()
X = matrix([[var("x_{}_{}".format(u,v), latex_name="a_{{{},{}}}".format(u,v)) for v in range(xcolnum)] for u in range(xrownum)]);
for i in range(xrownum):
for j in range(xcolnum):
if A[i,j] != 0:
X[i,j] = 0
XAT = X*A.transpose()
AXT = A*X.transpose()
ATX = A.transpose()*X
XTA = X.transpose()*A
</code></pre>
<p>Of course, X = 0 will always work, but I am interested in knowing if it is the ONLY matrix which works.</p>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74349#post-id-74349$A^TX = X^TA$ gives $n^2$ linear equations. Just solve the system of these equations with linear algebra. Checking whether $X=0$ is the only solution corresponds to checking whether the system has a trivial kernel.Thu, 16 Nov 2023 20:33:25 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74349#post-id-74349Comment by wizard7703 for <p>I have an m x n matrix A and an m x n matrix of variables X. I want to solve for all possible X such that AX^T = XA^T and A^TX = X^TA. Is there any nice way to do this?</p>
<p>I've tried using various commands like "Solve" but "solve_right" but it doesn't seem to be working. Here is my code so far.</p>
<pre><code>A = matrix([[0,1],[1,0],[1,1]])
xrownum = A.nrows()
xcolnum = A.ncols()
X = matrix([[var("x_{}_{}".format(u,v), latex_name="a_{{{},{}}}".format(u,v)) for v in range(xcolnum)] for u in range(xrownum)]);
for i in range(xrownum):
for j in range(xcolnum):
if A[i,j] != 0:
X[i,j] = 0
XAT = X*A.transpose()
AXT = A*X.transpose()
ATX = A.transpose()*X
XTA = X.transpose()*A
</code></pre>
<p>Of course, X = 0 will always work, but I am interested in knowing if it is the ONLY matrix which works.</p>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74350#post-id-74350Right, I am just unsure how to implement this in Sage. I am very new to the language and I would like to write a program which can do this computation for an arbitrary A.Thu, 16 Nov 2023 20:35:17 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74350#post-id-74350Answer by Max Alekseyev for <p>I have an m x n matrix A and an m x n matrix of variables X. I want to solve for all possible X such that AX^T = XA^T and A^TX = X^TA. Is there any nice way to do this?</p>
<p>I've tried using various commands like "Solve" but "solve_right" but it doesn't seem to be working. Here is my code so far.</p>
<pre><code>A = matrix([[0,1],[1,0],[1,1]])
xrownum = A.nrows()
xcolnum = A.ncols()
X = matrix([[var("x_{}_{}".format(u,v), latex_name="a_{{{},{}}}".format(u,v)) for v in range(xcolnum)] for u in range(xrownum)]);
for i in range(xrownum):
for j in range(xcolnum):
if A[i,j] != 0:
X[i,j] = 0
XAT = X*A.transpose()
AXT = A*X.transpose()
ATX = A.transpose()*X
XTA = X.transpose()*A
</code></pre>
<p>Of course, X = 0 will always work, but I am interested in knowing if it is the ONLY matrix which works.</p>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?answer=74352#post-id-74352Here is how we get the basis of the solutions to $\\{ A^TX = X^TA, AX^T=XA^T \\}$:
D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)Thu, 16 Nov 2023 20:57:52 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?answer=74352#post-id-74352Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74449#post-id-74449There still seems to be problems from my testing. There are a few examples which I know will have nontrivial (X != 0) solutions, for instance A = matrix([[1,0],[0,-1]]). Running the code above with matrix returns a TypeError.Fri, 24 Nov 2023 07:00:37 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74449#post-id-74449Comment by Max Alekseyev for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74447#post-id-74447It's not a blank matrix, it's the empty list of solutions, meaning that there are none for your matrix `A = matrix([[1,1,1],[1,1,1],[1,1,-1]])`.Thu, 23 Nov 2023 14:47:16 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74447#post-id-74447Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74434#post-id-74434Perhaps I am testing the code incorrectly, but no matter what I set A to, I seem to be getting a blank matrix. Here is my code I am testing with.
A = matrix([[1,1,1],[1,1,1],[1,1,-1]])
xrownum = A.nrows()
xcolnum = A.ncols()
X = matrix([[var("x_{}_{}".format(u,v), latex_name="a_{{{},{}}}".format(u,v)) for v in range(xcolnum)] for u in range(xrownum)]);
for i in range(xrownum):
for j in range(xcolnum):
if A[i,j] != 0:
X[i,j] = 0
D1 = A.T*X-X.T*A
D2 = A*X.T-X*A.T
V = [x for x in X.list() if x]
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)Tue, 21 Nov 2023 21:58:41 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74434#post-id-74434Comment by Max Alekseyev for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74369#post-id-74369The old code assumed that each element of $X$ is a variable. Now it takes into account the possibility of zero elements in $X$, and convert the kernel basis into matrices of the same size as $X$. The optional `assert` statement test the results validity.Sat, 18 Nov 2023 00:59:39 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74369#post-id-74369Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74367#post-id-74367Flattened as in the null vector in the ith coefficient corresponds to the row floor(i/#number of columns) and the column i%number of columns? I am testing some matrices this way and they don't seem to be working.Fri, 17 Nov 2023 23:57:09 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74367#post-id-74367Comment by Max Alekseyev for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74366#post-id-74366Any linear combination of those vectors gives a solutiom $X$ (in flattened form).Fri, 17 Nov 2023 23:38:50 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74366#post-id-74366Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74365#post-id-74365actually, I am having a hard time converting from the null space to a solution for X. This code is returning a list of vectors which are the kernel of the linear system of equations, but I'm unsure how to use this to find a particular X such that the above equations are satisfied.Fri, 17 Nov 2023 23:25:59 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74365#post-id-74365Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74358#post-id-74358Ah great. Thank you so much!Fri, 17 Nov 2023 03:04:07 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74358#post-id-74358Comment by Max Alekseyev for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74357#post-id-74357It's done same way - see updated answer.Fri, 17 Nov 2023 03:03:29 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74357#post-id-74357Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74356#post-id-74356I want to find an X such that both AX^T = XA^T and A^TX = X^TA. These are not quite the same equations, but I want to account for all n^2 + m^2 many linear equations from this.Fri, 17 Nov 2023 02:26:29 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74356#post-id-74356Comment by Max Alekseyev for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74355#post-id-74355I'm not sure what you are asking for. Do you want a parametric solution or something else?Fri, 17 Nov 2023 02:14:59 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74355#post-id-74355Comment by wizard7703 for <p>Here is how we get the basis of the solutions to $\{ A^TX = X^TA, AX^T=XA^T \}$:</p>
<pre><code>D1 = A.T*X - X.T*A
D2 = A*X.T - X*A.T
V = [x for x in X.list() if x] # variables
M = Matrix([[d.coefficient(x) for x in V] for d in D1.list()+D2.list()])
B = [X.subs(zip(V,b)) for b in M.right_kernel().basis()]
assert all( A.T*x == x.T*A and A*x.T == x*A.T for x in B )
print(B)
</code></pre>
https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74354#post-id-74354Is there a way to solve this system of equations and the system of equation AX^T-XA^T = 0 in the same variables at the same time? Thanks for the help it seems like your code is solving the first part correctlyFri, 17 Nov 2023 01:30:46 +0100https://ask.sagemath.org/question/74346/solving-for-a-matrix/?comment=74354#post-id-74354