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, 25 Jul 2018 08:39:42 +0200Optimizing a function of a given matrixhttps://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/ Let us consider the $4\times 4$ symmetric matrix $$ A_x=\left(\begin{array}{rrrr}
0 & 1 & 1 & 1 \\
1 & 0 & 2^x & 2^x \\
1 & 2^x & 0 & 2^x \\
1 & 2^x & 2^x & 0
\end{array}\right) $$
Here I need to find $\min \{ x>0: det(A_x)=0 \, or \, ||A_x^{-1}||=0 \} ,$ where by $||M||$ we mean the sum of all entries of the matrix $M.$ I'm looking for a general sage program where my input will be a matrix with entries as functions of an inderminant (like the matrix $A_x$ above) which will give me the unique $x$ corresponding to my matrix. If no such real value exists, it should result as $\infty$ Can anyone help me? Thank you in advance.Mon, 23 Jul 2018 16:50:52 +0200https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/Answer by slelievre for <p>Let us consider the $4\times 4$ symmetric matrix $$ A_x=\left(\begin{array}{rrrr}
0 & 1 & 1 & 1 \
1 & 0 & 2^x & 2^x \
1 & 2^x & 0 & 2^x \
1 & 2^x & 2^x & 0
\end{array}\right) $$</p>
<p>Here I need to find $\min { x>0: det(A_x)=0 \, or \, ||A_x^{-1}||=0 } ,$ where by $||M||$ we mean the sum of all entries of the matrix $M.$ I'm looking for a general sage program where my input will be a matrix with entries as functions of an inderminant (like the matrix $A_x$ above) which will give me the unique $x$ corresponding to my matrix. If no such real value exists, it should result as $\infty$ Can anyone help me? Thank you in advance.</p>
https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?answer=43126#post-id-43126One could use Sage to explore the problem.
Define the matrix $A$:
sage: A = matrix(SR, 4, [0, 1, 1, 1, 1, 0, 2^x, 2^x, 1, 2^x, 0, 2^x, 1, 2^x, 2^x, 0])
sage: A
[ 0 1 1 1]
[ 1 0 2^x 2^x]
[ 1 2^x 0 2^x]
[ 1 2^x 2^x 0]
Compute its determinant:
sage: det(A)
-3*2^(2*x)
The sum of its entries:
sage: sum(A.list())
6*2^x + 6
Its inverse:
sage: ~A
[-2/3*2^x 1/3 1/3 1/3]
[ 1/3 -2/3/2^x 1/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x -2/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x 1/3/2^x -2/3/2^x]
The sum of the entries of its inverse:
sage: sum((~A).list())
-2/3*2^x + 2
Now solve $\det(A) = 0$ and $\lVert A^{-1} \rVert = 0$,
extract positive solutions, take the minimum, and compute
a numerical approximation.
sage: S = solve(det(A) == 0, x); S
[]
sage: T = solve(sum((~A).list()) == 0, x); T
[x == (log(6) - log(2))/log(2)]
sage: min([e.rhs() for e in S + T if e.rhs() > 0]).n()
1.58496250072116
This can all be put in a function which, given a matrix `A`
as above, returns this minimum positive solution.Tue, 24 Jul 2018 03:08:15 +0200https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?answer=43126#post-id-43126Comment by slelievre for <p>One could use Sage to explore the problem.</p>
<p>Define the matrix $A$:</p>
<pre><code>sage: A = matrix(SR, 4, [0, 1, 1, 1, 1, 0, 2^x, 2^x, 1, 2^x, 0, 2^x, 1, 2^x, 2^x, 0])
sage: A
[ 0 1 1 1]
[ 1 0 2^x 2^x]
[ 1 2^x 0 2^x]
[ 1 2^x 2^x 0]
</code></pre>
<p>Compute its determinant:</p>
<pre><code>sage: det(A)
-3*2^(2*x)
</code></pre>
<p>The sum of its entries:</p>
<pre><code>sage: sum(A.list())
6*2^x + 6
</code></pre>
<p>Its inverse:</p>
<pre><code>sage: ~A
[-2/3*2^x 1/3 1/3 1/3]
[ 1/3 -2/3/2^x 1/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x -2/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x 1/3/2^x -2/3/2^x]
</code></pre>
<p>The sum of the entries of its inverse:</p>
<pre><code>sage: sum((~A).list())
-2/3*2^x + 2
</code></pre>
<p>Now solve $\det(A) = 0$ and $\lVert A^{-1} \rVert = 0$,
extract positive solutions, take the minimum, and compute
a numerical approximation.</p>
<pre><code>sage: S = solve(det(A) == 0, x); S
[]
sage: T = solve(sum((~A).list()) == 0, x); T
[x == (log(6) - log(2))/log(2)]
sage: min([e.rhs() for e in S + T if e.rhs() > 0]).n()
1.58496250072116
</code></pre>
<p>This can all be put in a function which, given a matrix <code>A</code>
as above, returns this minimum positive solution.</p>
https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43140#post-id-43140Edited question to add last step.Wed, 25 Jul 2018 06:28:46 +0200https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43140#post-id-43140Comment by Deepak Sarma for <p>One could use Sage to explore the problem.</p>
<p>Define the matrix $A$:</p>
<pre><code>sage: A = matrix(SR, 4, [0, 1, 1, 1, 1, 0, 2^x, 2^x, 1, 2^x, 0, 2^x, 1, 2^x, 2^x, 0])
sage: A
[ 0 1 1 1]
[ 1 0 2^x 2^x]
[ 1 2^x 0 2^x]
[ 1 2^x 2^x 0]
</code></pre>
<p>Compute its determinant:</p>
<pre><code>sage: det(A)
-3*2^(2*x)
</code></pre>
<p>The sum of its entries:</p>
<pre><code>sage: sum(A.list())
6*2^x + 6
</code></pre>
<p>Its inverse:</p>
<pre><code>sage: ~A
[-2/3*2^x 1/3 1/3 1/3]
[ 1/3 -2/3/2^x 1/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x -2/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x 1/3/2^x -2/3/2^x]
</code></pre>
<p>The sum of the entries of its inverse:</p>
<pre><code>sage: sum((~A).list())
-2/3*2^x + 2
</code></pre>
<p>Now solve $\det(A) = 0$ and $\lVert A^{-1} \rVert = 0$,
extract positive solutions, take the minimum, and compute
a numerical approximation.</p>
<pre><code>sage: S = solve(det(A) == 0, x); S
[]
sage: T = solve(sum((~A).list()) == 0, x); T
[x == (log(6) - log(2))/log(2)]
sage: min([e.rhs() for e in S + T if e.rhs() > 0]).n()
1.58496250072116
</code></pre>
<p>This can all be put in a function which, given a matrix <code>A</code>
as above, returns this minimum positive solution.</p>
https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43139#post-id-43139Actually, I was stuck at this step. I tried the code "solve" to solve the two systems, but I don't get numerical value from this. Again I tried "find_root" code with the interval but one of the systems (First one) does not have a solution, and so it gives a long list of errors. Therefore I cannot evaluate the minimum of all solutions (min() doesn't work). For this particular matrix, I can find it by direct calculation. But what I need a general method to get my required (numerical value) whenever I input a matrix as a function of $x$. I need something like this
X=det(A); Y=sum((~A).list()); S=solve([X,x>0],x); T=solve([Y,x>0],x);
min(S+T)
This doesn't give me numerical value. Note the last step, I want sage to calculate the minimum value of the two solution sets S and T.Wed, 25 Jul 2018 05:49:07 +0200https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43139#post-id-43139Comment by Deepak Sarma for <p>One could use Sage to explore the problem.</p>
<p>Define the matrix $A$:</p>
<pre><code>sage: A = matrix(SR, 4, [0, 1, 1, 1, 1, 0, 2^x, 2^x, 1, 2^x, 0, 2^x, 1, 2^x, 2^x, 0])
sage: A
[ 0 1 1 1]
[ 1 0 2^x 2^x]
[ 1 2^x 0 2^x]
[ 1 2^x 2^x 0]
</code></pre>
<p>Compute its determinant:</p>
<pre><code>sage: det(A)
-3*2^(2*x)
</code></pre>
<p>The sum of its entries:</p>
<pre><code>sage: sum(A.list())
6*2^x + 6
</code></pre>
<p>Its inverse:</p>
<pre><code>sage: ~A
[-2/3*2^x 1/3 1/3 1/3]
[ 1/3 -2/3/2^x 1/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x -2/3/2^x 1/3/2^x]
[ 1/3 1/3/2^x 1/3/2^x -2/3/2^x]
</code></pre>
<p>The sum of the entries of its inverse:</p>
<pre><code>sage: sum((~A).list())
-2/3*2^x + 2
</code></pre>
<p>Now solve $\det(A) = 0$ and $\lVert A^{-1} \rVert = 0$,
extract positive solutions, take the minimum, and compute
a numerical approximation.</p>
<pre><code>sage: S = solve(det(A) == 0, x); S
[]
sage: T = solve(sum((~A).list()) == 0, x); T
[x == (log(6) - log(2))/log(2)]
sage: min([e.rhs() for e in S + T if e.rhs() > 0]).n()
1.58496250072116
</code></pre>
<p>This can all be put in a function which, given a matrix <code>A</code>
as above, returns this minimum positive solution.</p>
https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43147#post-id-43147Thank you for your solution. It works with that particular matrix. But it doesn't work for all matrices.
For example consider $$A=\left(\begin{array}{rrrrr}
0 & 2^{x} & 1 & 1 & 1 \\
2^{x} & 0 & 1 & 1 & 1 \\
1 & 1 & 0 & 2^{x} & 2^{x} \\
1 & 1 & 2^{x} & 0 & 2^{x} \\
1 & 1 & 2^{x} & 2^{x} & 0
\end{array}\right)$$
or
$$A=\left(\begin{array}{rrrr}
0 & 1 & 2^{x} & 1 \\
1 & 0 & 1 & 2^{x} \\
2^{x} & 1 & 0 & 1 \\
1 & 2^{x} & 1 & 0
\end{array}\right)$$
Here I get an error "cannot evaluate symbolic expression numerically"
Note: For all my matrices, it is known that the solution is at most equal to 2. So if something like min(find_root(X,0,2)+find_root(Y,0,2)) could be used, that is also enough for me.Wed, 25 Jul 2018 08:39:42 +0200https://ask.sagemath.org/question/43124/optimizing-a-function-of-a-given-matrix/?comment=43147#post-id-43147