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.Thu, 07 Oct 2021 10:53:53 +0200Diagonalize matrix numerically over $\mathbb{C}$https://ask.sagemath.org/question/59263/diagonalize-matrix-numerically-over-mathbbc/Suppose I have a matrix m:
$$
m = \left(\begin{array}{rr}
2 & -3 \\\\
1 & 0
\end{array}\right).
$$
It is diagonalizable and has complex eigenvalues.
I now want to diagonalize it, but get an error:
In [20]: m = matrix([[2, -3], [1, 0]]); m.diagonalization()
...
ValueError: matrix entries must be from a field
When I specify the field `m = matrix(CDF, [[2, -3], [1, 0]])`, I get `ValueError: base field must be exact, but Complex Double Field is not`. Specifying `ComplexLazyField()` instead of `CDF` raises `NotImplementedError`.
So, apparently, Sage is trying to diagonalize the matrix symbolically, i.e. exactly. But what if I don't care about exactness and just want a straightforward numerical answer?
This is how I would do it with `sympy`:
In [22]: import sympy as sp
...: m = sp.Matrix([[2, -3], [1, 0]])
...: m.diagonalize()
Out[22]:
(Matrix([
[1 - sqrt(2)*I, 1 + sqrt(2)*I],
[ 1, 1]]),
Matrix([
[1 - sqrt(2)*I, 0],
[ 0, 1 + sqrt(2)*I]]))
Notice that the output is actually exact. I know I can run this same Python code in Sage, but I assume there's a more native way to do it.
To sum up, **how do I get Sage to diagonalize a matrix over $\mathbb{C}$? How do I change the code if I only need the numerical answer?**SageUserNickThu, 07 Oct 2021 10:53:53 +0200https://ask.sagemath.org/question/59263/optimize matrix elements to obtain certain eigenvalueshttps://ask.sagemath.org/question/56693/optimize-matrix-elements-to-obtain-certain-eigenvalues/Greetings to all,
someone could tell me if possible (and what I should learn, where I should start looking) to solve the following problem:
I have built a Matrix: (its a dynamical matrix build from force constants matrices), the elements of the matrix are symbolic expressions that depends of certain variables (in particular unknown force constants). I know the eigenvalues of this matrix at certain "points of 2 coordinates" (coordinates are variables of the expression that defines the elements of the array).
So, solving a linear system of equations (the equations are determinated by the "expression that sage return for the eigenvalues at these points" == "and the experimental eigenvalue that i measured") i get a numeric value for the variables (that's what my problem is all about).
But the problem is that the values of my own correspond to experimental values, and so I have achieved good constants for 4 points, but I need to adjust them to a set of values of my own, to a curve (the curve -its not only only are a set of curves- is the relation dispersion for bidimensional materials, graphene, BN, etc.) . So i need adjust the values of constants for optimize the adjust to the curve
We can consider a 2x2 matrix, D, and its elements depend of the constants alpha1, alpha2, alpha3 and alpha4, and the point where it's evaluated (q_x, q_y). Well i know that at point (0,0) the eigenvalues are 0 and 830, so i obtain the equation for the eigenvalues:
-6*(alpha1+alpha2)= 630; 0=0
and other equations at other points (at simetric points of first Brilluin Zone). Sagemath solves the linear system of equations an i can determine the constans. Then I rewrite the D matrix with the constants determined (alpha1=1.4, alpha2=2.5, and so ...), and obtain the eigenvalues of the D matrix at other points (pairs of q_x, q_y): the matrix still depends on the points [q_x, q_y]. So, what i want is, starting with the initial value of the constants that i obtained from first points, adjust them so that the matrix's eigenvalues best fit the experimental eigenvalues that i have.
On page 13 of [Noebook-Casimir.pdf](https://github.com/CasimirVictoria/TFG-Semiconductores_2D/blob/master/Notebook-Casimir.pdf) you can see what i mean.CasimirVictoriaSat, 17 Apr 2021 19:22:37 +0200https://ask.sagemath.org/question/56693/find_root of function involving maximal eigenvaluehttps://ask.sagemath.org/question/54418/find_root-of-function-involving-maximal-eigenvalue/ I have defined a function returning the leading eigenvalue of a matrix L as follows (the first function is to handle what I think are numerical errors, where what should be a real eigenvalue is seen as x + 0.?e-80*I, for instance):
def is_essentially_real(x):
if x.imag() == 0:
return(True)
else:
return(False)
def get_leading_eigenvalue(L):
evals = L.eigenvalues()
moduli = [e.n() for e in evals if is_essentially_real(e)]
moduli = [e for e in moduli if e >= 0]
r = max(moduli)
return(r)
I want to solve equations involving this function using find_root. As a toy example, I have the following:
def mat(b):
M = matrix(RR, 2, 2, [1, b, 1, 2])
return(M)
def f(b):
M = mat(b)
r = get_leading_eigenvalue(M)
return(r)
b = var(b)
find_root(f(b) - 3, 1,3 ) #produces error
I know that f(2) = 3, so if I understand correctly, my last line of code should return the number 2. Instead, I get the following message:
> TypeError: Cannot evaluate symbolic expression to a numeric value.
My best guess is that the method matrix.eigenvalues() does not play well with the method find_roots(), but I am not sure how to get around this. I have tried changing the matrix field to both of QQ and SR, to no avail. Any feedback is appreciated. Also, this is my first question on the site, so if I've committed any sins, please let me know.
ericrozonSat, 28 Nov 2020 05:06:26 +0100https://ask.sagemath.org/question/54418/Eigenvalues and eigenspaces of orthogonal (or rotation) matriceshttps://ask.sagemath.org/question/49496/eigenvalues-and-eigenspaces-of-orthogonal-or-rotation-matrices/Given an orthogonal transformation of finite order, e.g.
Matrix([[0,0,0,-1],[1,0,0,-1],[0,1,0,-1],[0,0,1,-1]])
Its eigenvalues are going to be of the form
exp(I*pi/5),exp(2*I*pi/5),...,exp(2*I*pi*m),...
corresponding to a splitting of the matrix into rotation (and reflection) matrices. I'd like to extract these fractions `m` (mod ZZ) and study the corresponding (real rotation and reflection) eigenspaces.
My impression is that Sage isn't suitable for doing this directly, but that I should use e.g. the Maxima or Mathematica interface? Any suggestions for the most suitable method?Bob67846Fri, 10 Jan 2020 23:05:24 +0100https://ask.sagemath.org/question/49496/Is there any way to find the eigenvalues of a matrix in terms of a variable?https://ask.sagemath.org/question/49488/is-there-any-way-to-find-the-eigenvalues-of-a-matrix-in-terms-of-a-variable/ I have the following matrix
M=
2n-1 & n-1 & n
1 & 2n-3 & 0
1& 0 & 1
Here $n$ is a variable
I want to find its eigenvalues.
Is there any way to find the eigenvalues of this matrix in terms of $n$ in sagemath.
I even cant input a matrix in terms of a variable.
Can someone please help me out?CaptchaFri, 10 Jan 2020 11:36:02 +0100https://ask.sagemath.org/question/49488/Pairs of graphs with same spectral radius but with different diameter and different number of edgeshttps://ask.sagemath.org/question/41990/pairs-of-graphs-with-same-spectral-radius-but-with-different-diameter-and-different-number-of-edges/ I know that graphs.cospectral_graphs() gives all pairs of graphs with same eigenvalues. But I need graphs with same spectral radius i.e. largest eigenvalue (the other eigenvalues may be different) and differing in diameter and number of edges.
What to do for this? Thank you in advance.Deepak SarmaThu, 12 Apr 2018 07:47:21 +0200https://ask.sagemath.org/question/41990/Eigenvalues over symbolic ringhttps://ask.sagemath.org/question/45567/eigenvalues-over-symbolic-ring/I am trying to compute the eigenvalues of a 16x16 matrix whose eigenvalues are multivariate polynomials in 4 variables of degree at most 3 and integer coefficients. When I try to do so using the Symbolic Ring SR my machine quickly runs out of memory (~15GB available I believe).
TypeError: ECL says: Memory limit reached. Please jump to an outer pointer, quit program and enlarge the memory limits before executing the program again.
I am using sage 8.1 (and I could try to upgrade if that is the issue here). The example I am struggling with is included at the end of this question (I did not write this by hand, but I don't think that it is relevant how the matrix was generated. One should be able to copy-and-paste this into Sage)
I tried to change the ring to the fraction field of a Multivariate Polynomial Ring over Rational Field, and this allows to quickly compute the characteristic polynomial and its roots, without killing my machine. Unfortunately though some of the eigenvalues are not rational functions, so they do not show up. This is what I did
R = QQ['a,b,c,d']
N = matrix(R, 16, 16, M)
N.change_ring(R.fraction_field()).characteristic_polynomial().roots()
Mathematica on the same machine is able to compute the eigenvalues symbolically (quite quickly actually), and I can solve a similar problem in Sage if I only use 2 variables. Am I missing something, or is simply the algorithm used by Sage not efficient enough for 16x16 matrices in 4 variables?
----------
a,b,c,d = var('a','b','c','d', domain='positive')
M = matrix(16,16,[
(3*c^2*d + 6*c + 1, 0, 0, (a - b)*c*d + a - b, 0, (a - b)*c*d + a - b, (a - b)*c*d + a - b, 0, 0, (a - b)*c*d + a - b, (a - b)*c*d + a - b, 0, (a - b)*c*d + a - b, 0, 0, 3*(a - b)^2*d),
(0, -3*c^2*d + 1, (a + b)*c*d + a + b, 0, (a + b)*c*d + a + b, 0, 0, -(a - b)*c*d + a - b, (a + b)*c*d + a + b, 0, 0, -(a - b)*c*d + a - b, 0, -(a - b)*c*d + a - b, 3*(a + b)*(a - b)*d, 0),
(0, (a + b)*c*d + a + b, -3*c^2*d + 1, 0, (a + b)*c*d + a + b, 0, 0, -(a - b)*c*d + a - b, (a + b)*c*d + a + b, 0, 0, -(a - b)*c*d + a - b, 0, 3*(a + b)*(a - b)*d, -(a - b)*c*d + a - b, 0),
((a - b)*c*d + a - b, 0, 0, 3*c^2*d - 2*c + 1, 0, -(a + b)*c*d + a + b, -(a + b)*c*d + a + b, 0, 0, -(a + b)*c*d + a + b, -(a + b)*c*d + a + b, 0, (2*(a + b)^2 + (a - b)^2)*d, 0, 0, (a - b)*c*d + a - b),
(0, (a + b)*c*d + a + b, (a + b)*c*d + a + b, 0, -3*c^2*d + 1, 0, 0, -(a - b)*c*d + a - b, (a + b)*c*d + a + b, 0, 0, 3*(a + b)*(a - b)*d, 0, -(a - b)*c*d + a - b, -(a - b)*c*d + a - b, 0),
((a - b)*c*d + a - b, 0, 0, -(a + b)*c*d + a + b, 0, 3*c^2*d - 2*c + 1, -(a + b)*c*d + a + b, 0, 0, -(a + b)*c*d + a + b, (2*(a + b)^2 + (a - b)^2)*d, 0, -(a + b)*c*d + a + b, 0, 0, (a - b)*c*d + a - b),
((a - b)*c*d + a - b, 0, 0, -(a + b)*c*d + a + b, 0, -(a + b)*c*d + a + b, 3*c^2*d - 2*c + 1, 0, 0, (2*(a + b)^2 + (a - b)^2)*d, -(a + b)*c*d + a + b, 0, -(a + b)*c*d + a + b, 0, 0, (a - b)*c*d + a - b),
(0, -(a - b)*c*d + a - b, -(a - b)*c*d + a - b, 0, -(a - b)*c*d + a - b, 0, 0, -3*c^2*d + 1, 3*(a + b)*(a - b)*d, 0, 0, (a + b)*c*d + a + b, 0, (a + b)*c*d + a + b, (a + b)*c*d + a + b, 0),
(0, (a + b)*c*d + a + b, (a + b)*c*d + a + b, 0, (a + b)*c*d + a + b, 0, 0, 3*(a + b)*(a - b)*d, -3*c^2*d + 1, 0, 0, -(a - b)*c*d + a - b, 0, -(a - b)*c*d + a - b, -(a - b)*c*d + a - b, 0),
((a - b)*c*d + a - b, 0, 0, -(a + b)*c*d + a + b, 0, -(a + b)*c*d + a + b, (2*(a + b)^2 + (a - b)^2)*d, 0, 0, 3*c^2*d - 2*c + 1, -(a + b)*c*d + a + b, 0, -(a + b)*c*d + a + b, 0, 0, (a - b)*c*d + a - b),
((a - b)*c*d + a - b, 0, 0, -(a + b)*c*d + a + b, 0, (2*(a + b)^2 + (a - b)^2)*d, -(a + b)*c*d + a + b, 0, 0, -(a + b)*c*d + a + b, 3*c^2*d - 2*c + 1, 0, -(a + b)*c*d + a + b, 0, 0, (a - b)*c*d + a - b),
(0, -(a - b)*c*d + a - b, -(a - b)*c*d + a - b, 0, 3*(a + b)*(a - b)*d, 0, 0, (a + b)*c*d + a + b, -(a - b)*c*d + a - b, 0, 0, -3*c^2*d + 1, 0, (a + b)*c*d + a + b, (a + b)*c*d + a + b, 0),
((a - b)*c*d + a - b, 0, 0, (2*(a + b)^2 + (a - b)^2)*d, 0, -(a + b)*c*d + a + b, -(a + b)*c*d + a + b, 0, 0, -(a + b)*c*d + a + b, -(a + b)*c*d + a + b, 0, 3*c^2*d - 2*c + 1, 0, 0, (a - b)*c*d + a - b),
(0, -(a - b)*c*d + a - b, 3*(a + b)*(a - b)*d, 0, -(a - b)*c*d + a - b, 0, 0, (a + b)*c*d + a + b, -(a - b)*c*d + a - b, 0, 0, (a + b)*c*d + a + b, 0, -3*c^2*d + 1, (a + b)*c*d + a + b, 0),
(0, 3*(a + b)*(a - b)*d, -(a - b)*c*d + a - b, 0, -(a - b)*c*d + a - b, 0, 0, (a + b)*c*d + a + b, -(a - b)*c*d + a - b, 0, 0, (a + b)*c*d + a + b, 0, (a + b)*c*d + a + b, -3*c^2*d + 1, 0),
(3*(a - b)^2*d, 0, 0, (a - b)*c*d + a - b, 0, (a - b)*c*d + a - b, (a - b)*c*d + a - b, 0, 0, (a - b)*c*d + a - b, (a - b)*c*d + a - b, 0, (a - b)*c*d + a - b, 0, 0, 3*c^2*d + 6*c + 1)]
)angeloluciaThu, 28 Feb 2019 01:50:15 +0100https://ask.sagemath.org/question/45567/Fast numerical computation of eigenvalueshttps://ask.sagemath.org/question/44531/fast-numerical-computation-of-eigenvalues/I am trying to calculate the eigenvalues of a large (say, n=1000 to 10000) sparse Hermitian square matrix using SAGE
> numpy.linalg.eigvalsh(MyMatrix)
takes very long. I noticed it utilizes only a single core of my CPU.
How would one go about speeding the calculation? Specifically, I'm looking for a solution using parallel computation, or maybe something which is "more compiled".
Thank you.SageMathematicianSat, 01 Dec 2018 19:10:10 +0100https://ask.sagemath.org/question/44531/Eigenvalues of matrix with entries in polynomial ringhttps://ask.sagemath.org/question/8797/eigenvalues-of-matrix-with-entries-in-polynomial-ring/Hi!
I just wrote some code on the sage-combinat queue which computes a matrix with entries in a polynomial ring R = PolynomialRing(QQ, 'x', n)
sage: P = Poset(([1,2,3,4], [[1,3],[1,4],[2,3]]), linear_extension = True)
sage: L = P.linear_extensions()
sage: M = L.markov_chain_transition_matrix(labeling = 'source')
sage: M
[-x0 - x1 - x2 x3 x0 + x3 0 0]
[ x1 + x2 -x0 - x1 - x3 0 x1 0]
[ 0 x1 -x0 - x3 0 x1]
[ 0 x0 0 -x0 - x1 - x2 x0 + x3]
[ x0 0 0 x0 + x2 -x0 - x1 - x3]
sage: M.eigenvalues()
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
/Applications/sage-5.0.beta7/devel/sage-combinat/sage/combinat/posets/<ipython console> in <module>()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/matrix/matrix2.so in sage.matrix.matrix2.Matrix.eigenvalues (sage/matrix/matrix2.c:26415)()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/matrix/matrix2.so in sage.matrix.matrix2.Matrix.fcp (sage/matrix/matrix2.c:11089)()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_element.so in sage.rings.polynomial.polynomial_element.Polynomial.factor (sage/rings/polynomial/polynomial_element.c:22655)()
NotImplementedError:
Is it possible to compute this some other way or is this just not yet implemented (which would surprise me!).
Thanks,
Anne
-----
Edit (originally posted as an answer by the original poster of the question)
Here is an easier example with the question:
sage: R = PolynomialRing(QQ, 'x', 2)
sage: x = R.gens()
sage: M = matrix([[x[0],x[1]],[x[1],x[0]]])
sage: M.eigenvalues()
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
/Applications/sage-5.0.beta7/devel/sage-combinat/sage/combinat/posets/<ipython console> in <module>()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/matrix /matrix2.so in sage.matrix.matrix2.Matrix.eigenvalues (sage/matrix/matrix2.c:26415)()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/matrix/matrix2.so in sage.matrix.matrix2.Matrix.fcp (sage/matrix/matrix2.c:11089)()
/Applications/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_element.so in sage.rings.polynomial.polynomial_element.Polynomial.factor (sage/rings/polynomial/polynomial_element.c:22655)()
NotImplementedError:Anne SchillingFri, 16 Mar 2012 20:05:50 +0100https://ask.sagemath.org/question/8797/Eigenvalues of hecke operatorshttps://ask.sagemath.org/question/40588/eigenvalues-of-hecke-operators/For a project I am trying to build a program that computes a basis for cusp forms of weight 2 over $\Gamma_0(N)$. At one point, I want to represent eigenvalues of multiple Hecke operators. For example, (for $N=23$) I have found the following matrices for the Hecke operators:
$
T_2=\begin{pmatrix} -2 & -1/2 ;
2 & 1 \end{pmatrix}, T_3=\begin{pmatrix} 3 & 1 ;
-4 & -3 \end{pmatrix} \quad T_5=\begin{pmatrix} -2 & -1/2 ;
2 & 1 \end{pmatrix} \quad
$
so we have the following characteristic polynomials $f_2=x^2+x-1$, $f_3=x^2-5$, $f_5=x^2 +2x-4$. Suppose we denote the solution of $f_2$ (eigenvalues of $T_2$) by $a$ and it's conjugate. Then we can denote the solution of $f_3$ by $2a+1$ and of $f_5$ by $2a$.
I am trying to figure out how I can 'make' Sage represent these eigenvalues in this way. Say I have found these matrices above, I can create k.< a >=NumberField(x^2+x-1), then $a$ represents the root of the polynomial. So the question for me is, how can I make Sage give me the eigenvalues of $T_3$ and $T_5$ in terms of $a$?
Furthermore, I am wondering how to get the jordan normal form of $T_2$, or eigenmatrix in terms of $a$. If I ask for jordan_form, I get an error, and if I ask for eigenmatrix_right, I only get a numerical expression.
Remark: A little context, the goal for this project is to represent newforms in a similar way as Sage does, when you ask for example Newforms(23,2,names='a'). We need the eigenvalues of the hecke operators since they correspond with the coefficients of the Fourier series.
----------TheBeiramSat, 13 Jan 2018 11:32:29 +0100https://ask.sagemath.org/question/40588/command to get eigenvalueshttps://ask.sagemath.org/question/39621/command-to-get-eigenvalues/ If a matrix is given for heigher order ..i want to know how to get eigen values by sage.RajniFri, 17 Nov 2017 05:23:02 +0100https://ask.sagemath.org/question/39621/comparing sets of roots of charpolyhttps://ask.sagemath.org/question/35729/comparing-sets-of-roots-of-charpoly/ I am missing something about how to compare list of roots.
During a small algorithm I need to know wether a matrix has or not complex eigenvalues. I did the following
A=matrix(QQ,[[1,2,1],[6,-1,0],[-1,-2,-1]])
a=(B.charpoly()).roots(ring= QQ, multiplicities=False)
b=(B.charpoly()).roots(ring= QQbar, multiplicities=False)
then a is the list [-4,0,3] and b is the list [3,0,-4].
I don't get the following :
set(a)==set(b)
return false while
set([-4,0,3])==set([3,0,-4])
return true.
Any help, either on the first pb (knowing that a QQ matrix has complex eigenvalues) or on the second would be greatly appreciated.
Cheers.Laurent BThu, 24 Nov 2016 21:15:42 +0100https://ask.sagemath.org/question/35729/Eigenvalues/vectors of jacobian matrix as complex numbershttps://ask.sagemath.org/question/33049/eigenvaluesvectors-of-jacobian-matrix-as-complex-numbers/ Hello,
I'm trying to find the eigenvalues and eigenvectors of a jacobian matrix expressed as complex numbers (complex double field?), not an expression (like 1/2 * sqrt(...)...).
sorry for the silly question...
--------------------------------------------------------------------------
EDIT: Some more info; I'm studying a dynamical system at an equilibrium point. I would like to get an expression for the eigenvectors and values assosiated with the center eigenspace of a "hopf bifurcation".
Here is the code in question:
var('alpha1, alpha2, beta1, beta2, mu1, mu2, phi1, phi2, c1, c2, k1, k2, t ') ;
# suggested parameter values:
alpha1 = 0.7; alpha2 = 0.3
beta1 = 0.003; beta2 = 0.0015
mu1 = 0.6; mu2 = 0.4
phi1 = 2.3; phi2 = 4
c1 = 400000; c2 = 100
k1 = 0.0033100000000000;
k2 = 0.000988067680992286;
# The vector field and its jacobian matrix:
f(P, M, L, G,k2) = [phi1*M*(1-M/c1) - (alpha1 + beta1)* P - k1*P*G, alpha1*P - mu1*M, phi2*G*(1-G/c2) - (alpha2 + beta2)* L + k2*P*G, alpha2*L - mu2*G];
J = jacobian(f, (P, M, L, G));
# Equilibrium solution:
M4 =((alpha1*alpha2 + alpha1*beta2)*c1*c2*k1*mu1*mu2 + (alpha1^2*alpha2*c1*phi1 - (alpha1*alpha2*c1*c2*k1 + (alpha1^2*alpha2 + alpha1*alpha2*beta1)*c1)*mu1)*phi2)/(alpha2*c1*c2*k1*k2*mu1^2 + alpha1^2*alpha2*phi1*phi2);
P4 = ((alpha2 + beta2)*c1*c2*k1*mu1^2*mu2 + (alpha1*alpha2*c1*mu1*phi1 - (alpha2*c1*c2*k1 + (alpha1*alpha2 + alpha2*beta1)*c1)*mu1^2)*phi2)/(alpha2*c1*c2*k1*k2*mu1^2 + alpha1^2*alpha2*phi1*phi2);
L4 = -((alpha1*alpha2 + alpha2*beta1)*c1*c2*k2*mu1^2*mu2 - alpha1^2*alpha2*c2*mu2*phi1*phi2 - (alpha1*alpha2*c1*c2*k2*mu1*mu2 - (alpha1^2*alpha2 +alpha1^2*beta2)*c2*mu2^2)*phi1)/(alpha2^2*c1*c2*k1*k2*mu1^2 + alpha1^2*alpha2^2*phi1*phi2);
G4 = -((alpha1*alpha2 + alpha2*beta1)*c1*c2*k2*mu1^2 - alpha1^2*alpha2*c2*phi1*phi2 -(alpha1*alpha2*c1*c2*k2*mu1 - (alpha1^2*alpha2 + alpha1^2*beta2)*c2*mu2)*phi1)/(alpha2*c1*c2*k1*k2*mu1^2 + alpha1^2*alpha2*phi1*phi2);
# Now I evaluate it at the fixed point:
J4 = J(P=P4, G=G4, M=M4, L=L4)
J4 = J4(k2=k2)
Now if I do something like;
J4.eigenvalues()
I get an lengthy radical expression.
And when computing the eigenvectors:
D, P = J4.eigenmatrix_left()
I get an error...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_129.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("RCwgUCA9IEo0LmVpZ2VubWF0cml4X2xlZnQoKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpYDeAbA/___code___.py", line 2, in <module>
exec compile(u'D, P = J4.eigenmatrix_left()
File "", line 1, in <module>
File "sage/matrix/matrix2.pyx", line 5617, in sage.matrix.matrix2.Matrix.eigenmatrix_left (build/cythonized/sage/matrix/matrix2.c:39965)
IndexError: list index out of rangeVillageIdiotSun, 10 Apr 2016 15:12:11 +0200https://ask.sagemath.org/question/33049/eigenvalues vs roots of characteristic polynomialhttps://ask.sagemath.org/question/31656/eigenvalues-vs-roots-of-characteristic-polynomial/Hi,
Sorry if this is a stupid question but I am struggling to make Sage compute the exact eigenvalues of a matrix.
I am also confused that when I ask for the roots of the characteristic polynomial Sage answers with complex roots...
I am new to Sage so please forgive me :)
Here is my worksheet:
sage: A = matrix([[1, 1, 2], [1, -2, 0], [2, 0, 1]])
sage: A
[ 1 1 2]
[ 1 -2 0]
[ 2 0 1]
sage: A.eigenvalues()
[-2.439311671683875?, -0.6611203141265045?, 3.100431985810380?]
sage: x = var('x')
sage: H = A.charpoly()
sage: H
x^3 - 8*x - 5
sage: H.roots()
[]
sage: (x^3-8*x-5).roots()
[(-1/2*(1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3)*(I*sqrt(3) + 1) + 1/3*(4*I*sqrt(3) - 4)/(1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3),
1),
(-1/2*(1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3)*(-I*sqrt(3) + 1) + 1/3*(-4*I*sqrt(3) - 4)/(1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3),
1),
((1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3) + 8/3/(1/18*I*sqrt(1373)*sqrt(3) + 5/2)^(1/3),
1)]
Laurent BThu, 17 Dec 2015 14:58:59 +0100https://ask.sagemath.org/question/31656/Problems in solving eigenvalue equations with differential operatorshttps://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/Let's say we have a linear differential operator $\hat A = \sum_k a_k \frac {d^k} {dx^k}$ and we want to solve the eigenvalue equation $\hat A f(x) = a f(x)$ which is an ODE that we put in sage and outputs a solution $g(x)$. We now want to substitute $f(x)$ with $g(x)$ and simplify our expression in order to extract the eigenvalues of $\hat A$. The problem here is that I am not able to substitute the derivatives with my solution neither via `eqn.substitute_expression(f(x) == g(x))` nor via `eqn.substitute_function(f(x),g(x))` because `D[0]f(x)`, ... `D[0,0,...,0]f(x)` remain unchanged.
papachristoumariosThu, 03 Sep 2015 23:50:54 +0200https://ask.sagemath.org/question/29398/Condensing variables of a matrixhttps://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/I'm working with matrices such as
M = matrix([
[a, b, 0],
[c, 0, d],
[0, e, 0]])
where a, b, c, d, and e are variables. Then I loop through some numbers for each of a, b, c, d, and e, looking at the eigenvalues of the resulting matrix. $$ char(M) = x^3 - ax^2 - (bc + de)x + ade $$
So, I'm doing more work than necessary, and ideally, I'd reduce M down to
M = matrix([
[a, b, 0],
[1, 0, d],
[0, 1, 0]])
Is there a simple way of using Sage to do so?IcemanTue, 07 Jul 2015 19:16:55 +0200https://ask.sagemath.org/question/27271/Orthonormal basis consisting of eigenvectors of a matrixhttps://ask.sagemath.org/question/26525/orthonormal-basis-consisting-of-eigenvectors-of-a-matrix/ How to find an orthonormal basis consisting of eigenvectors of a matrix. We assume that the matrix is over reals.Uday BhaskarTue, 14 Apr 2015 11:21:01 +0200https://ask.sagemath.org/question/26525/eigenvalues of matrices in AAhttps://ask.sagemath.org/question/26357/eigenvalues-of-matrices-in-aa/ M=matrix(AA,[[0,-1],[1,0]])
M.eigenvectors_right()
raises an error "eigenvectors are not implemented for matrices with eigenvalues that are not in the fraction field of the base ring or in QQbar" which is just wrong because the eigenvalues of M are certainly in QQbar.
Can someone please explain how to avoid this problem?
Thanks,
Jaume
JaumeFri, 27 Mar 2015 10:41:04 +0100https://ask.sagemath.org/question/26357/Calculation of symbolic eigenvalueshttps://ask.sagemath.org/question/25240/calculation-of-symbolic-eigenvalues/ Hello,
I am very new to Sage. In my first trial, I sought for eigenvalues of a symbolic matrix. However, Sage returns the following error:
ArithmeticError: could not determine eigenvalues exactly using symbolic matrices; try using a different type of matrix via self.change_ring(), if possible
I have seen an example of eigenvalues of a symbolic matrix being calculated by Sage on another page. So I wonder what the problem is in this case. Below is my code:
var('s b')
A = matrix([[0,0,b,b,b,b,b,b,b,b],[s,0,0,0,0,0,0,0,0,0],[0,s,0,0,0,0,0,0,0,0],[0,0,s,0,0,0,0,0,0,0],[0,0,0,s,0,0,0,0,0,0],[0,0,0,0,s,0,0,0,0,0],[0,0,0,0,0,s,0,0,0,0],[0,0,0,0,0,0,s,0,0,0],[0,0,0,0,0,0,0,s,0,0],[0,0,0,0,0,0,0,0,s,0]]); A
A_evals = A.eigenvalues()
Many thanks for your support.kavir1698Fri, 12 Dec 2014 18:17:24 +0100https://ask.sagemath.org/question/25240/Solve the following system of ODE's and plot its solutionhttps://ask.sagemath.org/question/24852/solve-the-following-system-of-odes-and-plot-its-solution/Solve the following system of ordinary differential equations
d^2x/dt^2 + 5x + 2y = 0
d^2y/dt^2 + 2x + 8y = 0
with initial conditions x(0)=1, y(0)=2, x'(0)=-4, y'(0)=2
and plot its solution as a curve in the {x,y} plane.fomel_sergeyThu, 13 Nov 2014 02:21:22 +0100https://ask.sagemath.org/question/24852/Black box numerical optimization of a convex functionhttps://ask.sagemath.org/question/23831/black-box-numerical-optimization-of-a-convex-function/I would like to numerically optimize a convex function without using any of its internals, that is, a python function. Is there an implementation of this in sage? I tried `sage.numerical.optimize.find_local_minimum` but it fails internally with a
TypeError: unable to find a common ring for all elements
My function to optimize is in fact the second largest eigenvalue of a matrix that is constructed from one parameter. Here it is in all gory detail:
def binaryStrings(k):
for i in range(2**k):
s = str(bin(i))[2:]
yield "0"^(k-len(s)) + s;
def hammingDistanceInt (a,b):
return bin(a^^b).count("1")
def cubeAdjacency (k):
# Note: We would like to use QQ instead of CDF, but this will
# result in inexact computations failing due to rounding later
# (Yes it does!)
# https://groups.google.com/forum/#!topic/sage-support/3IIbu0OBJX4
return matrix(CDF, 2^k, 2^k, lambda i, j: 1 if hammingDistanceInt(i,j)==1 else 0);
def hemmeckeFiberAdjacency (k, p1):
p2 = (1 - 4*k*p1)/2;
M = p1 * block_diagonal_matrix (cubeAdjacency(k),cubeAdjacency(k));
M[0,2^k] = p2
M[2^k,0] = p2
rowsums = M * vector ([1]*2^(k+1))
M += diagonal_matrix(vector([1]*2^(k+1))-rowsums);
return M
def slem(k,p):
e = map(abs, hemmeckeFiberAdjacency(k,p).eigenvalues())
e.sort();
return e[-2];
def slem4(p): return slem(4,p)
plot(slem4, [0,1/16]) # Works fine
sage.numerical.optimize.find_local_minimum(slem4, [0,1/16]) # Type errorThomasTue, 19 Aug 2014 12:43:57 +0200https://ask.sagemath.org/question/23831/QZ decomposition for generalized eigenvalueshttps://ask.sagemath.org/question/10751/qz-decomposition-for-generalized-eigenvalues/[QZ decomposition @Wiki](http://en.wikipedia.org/wiki/Schur_decomposition#Generalized_Schur_decomposition)
[QZ factorization @MATLAB](http://www.mathworks.de/de/help/matlab/ref/qz.html)
Can we use SAGE to solve a generalized eigenvalue problem?
Thanks in advance!gundamlhThu, 21 Nov 2013 05:26:24 +0100https://ask.sagemath.org/question/10751/Why does jordan_form not work over inexact rings?https://ask.sagemath.org/question/10488/why-does-jordan_form-not-work-over-inexact-rings/Hi,
Given a matrix M with entries (variables) in SR, I needed to compute the transformation matrix to a jordan form of M.
I ended up copying the code in "jordan_form" and "_jordan_form_vector_in_difference" from "matrix2.pyx",
deleting the "if (base_ring is None and not self.base_ring().is_exact()) ..."
and replacing "evals = A.charpoly().roots()" by
eigenvects=A.eigenvectors_right()
evals=[(eigenvect[0],eigenvect[2]) for eigenvect in eigenvects]
So far it seems to work. Can this go wrong? Or could one just change the original code in matrix2.pyx, and allow inexact rings?
Thanks for your help!LolinaThu, 29 Aug 2013 06:43:36 +0200https://ask.sagemath.org/question/10488/Plotting eigenvalues as a function of a variablehttps://ask.sagemath.org/question/9675/plotting-eigenvalues-as-a-function-of-a-variable/I am attempting to plot the the eigenvalues for the matrix:
H=matrix([[1+x, 0, x],[0, 2, 2*x],[x, 2*x, 1+x]])
which were calculated using the following method:
a=H.eigenvalues()
in the following way:
h=fast_callable(real_part(a[2]),vars=[x],domain=CC)
plot(h,(0,0.5))
but unfortunately this does not work for a[0] and a[1],
f=fast_callable(real_part(a[0]),vars=[x],domain=CC)
g=fast_callable(real_part(a[1]),vars=[x],domain=CC)
plot(f,(0,0.5))+plot(g,(0,0.5))
Any suggestions would be greatly appreciated.
wrighteapTue, 01 Jan 2013 15:30:29 +0100https://ask.sagemath.org/question/9675/Sorted list of symbolic eigenvalues (and corresponding eigenvectors)https://ask.sagemath.org/question/9599/sorted-list-of-symbolic-eigenvalues-and-corresponding-eigenvectors/Is there a way to obtain a sorted list of eigenvalues when they are computed symbolically (in SR). Particularly knowing that in specific points, they can switch order.
So far, what I have is (assuming $R^3$):
def sorted_eval(m, x, y, z, order=0):
_ev = m.eigenvalues()
_ev = numpy.array(ev.subs(x=x, y=y, z=z))
_ev.sort()
return _ev[order]
But then if I want to perform a contour plot:
x, y, z = var('x y z')
p = vector([x,y,z])
f = p * p
h = f.hessian()
contour(lambda x, y: sorted_eval(h, x, y, 0, 0), (x, -1.5, 1.5), (y, -1.5, 1.5))
It takes a long time
Thanks
D
DemMon, 03 Dec 2012 14:08:01 +0100https://ask.sagemath.org/question/9599/eigenvalues of a derivative vs derivative of eigenvalueshttps://ask.sagemath.org/question/9543/eigenvalues-of-a-derivative-vs-derivative-of-eigenvalues/Hi! I have this little problem. If anyone would be so kind to share his knowledge and shed some lite on it, I'd be very grateful. Big thanks in advance (and sorry for my english)!
I have a matrix M=M(x) depending on a variable x. I want sage to compute trace of a product of a derivative of M, M' and some function of it, f(M), at a fixed value of x=x_0; that is:
(tr[M'*f(M)])|_(x=x_0).
It just so happens that tr[M'*f(M)] = sum( ev_i' *f(ev_i) ), where {ev_i(x)} are eigenvalues of M. Lucky me. Diagonalisation of M commutes with differentiating or taking the function of it, one could say.
But my M and its derivative are somewhat complicated, yet simplify greatly after substituting x=x_0. So I would very much prefer first to compute M', substitute x_0 M0:=M|_(x=x_0) and M'0:=M'|_(x=x_0), and only after that ask sage for eigenvalues:
ev1=M0.eigenvalues()
ev2=M'0.eigenvalues()
So here's the question: do i have any reason for hoping that:
(tr[M'*f(M)])|_(x=x_0) = sum( ev2[i] *f(ev1[i]) for i in range(dim of M) )?
(That is, wether the order of eigenvalues changes if I exchange diagonalisation with differentiation?)ozikSat, 17 Nov 2012 19:18:33 +0100https://ask.sagemath.org/question/9543/