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.Tue, 22 Jun 2021 08:34:49 +0200way to subs var by matrix in polynomialhttps://ask.sagemath.org/question/57660/way-to-subs-var-by-matrix-in-polynomial/HI
What is the best way to substitute to the variables of a polynomial x, y with 2 matrices X, Y
R.<x,y> = QQ[]
p = x^2 + 5*y + 2*x*y + 3*x + y^2
show("p : ",p," \t degre : \t",p.degree(x))
for ex in range(1,p.degree(x)+1) :
coefx=p.coefficient(x^ex)
print(" ex : ",ex," \t coefx : ",coefx)
for ey in range(1,p.degree(x)+1) :
coefy=p.coefficient(y^ey)
print(" ey : ",ey," \t coefy : ",coefy)
#for ey in range(1,p.degree(y)+1) :
# coefy=p.coefficient(y^ex)
# for ex in range(0,coefy.degree(y)+1) :
# coefx=p.coefficient(x^ex)
# print(" ex : ",ex," \t coefx : ",coefx," \t ey : ",ey," \t coefy : ",coefy)
var('x_00,x_01,x_10,x_11',domain='integer')
var('y_00,y_01,y_10,y_11',domain='integer')
X=matrix(SR,[[x_00,x_01],[x_10,x_11]]); Y=matrix(SR,[[y_00,y_01],[y_10,y_11]])
show(X,Y)
#p.subs([x==X,y==Y])ortolljTue, 22 Jun 2021 08:34:49 +0200https://ask.sagemath.org/question/57660/Finding a diagonal matrix for a given matrixhttps://ask.sagemath.org/question/57499/finding-a-diagonal-matrix-for-a-given-matrix/ Let $A$ be a real $n \times n$ matrix.
Then there exist orthogonal matrices $T_1$ and $T_2$ such that $A=T_1 D T_2$ where D is a diagonal matrix with positive entries. D is unique up permutation of the diagonal entries. This decomposition is sometimes called Cartan decomposition.
>Question: Is it possible to obtain the matrix $D$ for a given matrix $A$ in Sage?klaaaWed, 09 Jun 2021 14:00:12 +0200https://ask.sagemath.org/question/57499/Finding a permutation matrix associated to a non-singular matrixhttps://ask.sagemath.org/question/57433/finding-a-permutation-matrix-associated-to-a-non-singular-matrix/ Let $A$ be a non-singular complex matrix.
Then there exist a decomposition A=LPU where L is lower triangular, U is upper triangular and P is a uniquely determined permutation matrix just depending on A.
Similarly, there exists a decomposition A=L' P' L' , where L' is a lower triangular matrix and P' is a uniquely determined permutation just depending on A.
(see for example https://link.springer.com/chapter/10.1007%2F978-3-319-70953-6_1 theorem 1.1.10)
>Question: Is there a quick way to determine the permutation matrices P and P' for a given non-singular matrix A (we can assume that A has integer entries if that helps)?klaaaThu, 03 Jun 2021 18:42:03 +0200https://ask.sagemath.org/question/57433/Issue with inversion of complex symbolic array initialized with numpyhttps://ask.sagemath.org/question/56850/issue-with-inversion-of-complex-symbolic-array-initialized-with-numpy/Hello!
I am trying to invert a symbolic matrix that is initialized with a combination of numpy arrays and I get an error. Below i present a simple code that gives the error.
import numpy as np
a = np.zeros((5,5) , dtype = 'complex')
np.fill_diagonal(a,1)
b = var('x')*a
c = matrix(b)
c.inverse()
Gives the error:
> ECL says: THROW: The catch
> MACSYMA-QUIT is undefined.
What I have noticed up until now is that the issue stems from the data type. When I try casting the numpy array to float before turning it into a matrix, it works. However, my actual code makes use of complex coefficients. I believe it might have to do with how the imaginary part is represented in SageMath in comparison to numpy. ( `j` vs `I` )
When I manually create the symbolic array with `I` for the imaginary part, the `.inverse()` has no issue.yorgos_sotWed, 28 Apr 2021 13:31:54 +0200https://ask.sagemath.org/question/56850/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/How to convert linear system to matrix formhttps://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g.
> 3 x + 2 y = 16
>
> 7 x + y = 19fahadktkMon, 05 Apr 2021 06:09:26 +0200https://ask.sagemath.org/question/56499/Splitting a matrixhttps://ask.sagemath.org/question/56377/splitting-a-matrix/Dear SAGE community,
I am looking for a way that can help me split this matrix, A
A = matrix([[x], [x - 2*y]]), which is a linear combination of variables x, and y
into a Coefficient matrix, C = matrix([[1, 0], [1, -2]]) times the vector containing independent variables b = matrix([[x], [y]]).
Basically, A = C*b.
If there's a way to obtain the matrix C, would be really helpful.
-TIAApoorvThu, 25 Mar 2021 20:18:08 +0100https://ask.sagemath.org/question/56377/How one can find a matrix with given condition?https://ask.sagemath.org/question/55877/how-one-can-find-a-matrix-with-given-condition/I found the following recreational mathematics problem too hard for me. Can anyone give me hints how to find a solution?
Consider a $4\times 4$ matrix with integer coefficients. Let its elements be $a_{i,j}$ for $1\leq i,j\leq 4$. Now form four sets $A_1$, $A_2$, $A_3$, $A_4$. Say that $A_1$ is the set of elements $a_{i,j}$. Also, $A_2$ is the set of elements $a_{i,j}+a_{i+1,j}+a_{i,j+1}+a_{i+1,j+1}$ i.e. the set of sums of elements of $2\times 2$-subsquares of the matrix. Similarly, denote by $A_3$ the set of sums of $3\times 3$-submatrices and $A_4$ the set containing the sum of all elements of the given $4\times 4$-matrix.
Now, I heard a rumor that one can give an example of 16 integers $a_{i,j}$ such that all integers from 1 to 25 belong to $A_1 \cup A_2 \cup A_3 \cup A_4$. How can one find such an example? It is easy to see that at least some of the elements $a_{i,j}$ must satisfy $1\leq a_{i,j}\leq 25$ but there are still so many elements that finding such a matrix by brute force seems impossible. I was wondering if genetic algorithms or simulated annealing work for such a problem but I don't have enough experience to implement that.
The best I know is that getting all integers from 1 to 24 is possible, for instance using the matrix:
-42 22 23 7
13 11 -32 14
-23 16 15 8
19 9 -22 1Jaakko SeppäläWed, 24 Feb 2021 15:17:19 +0100https://ask.sagemath.org/question/55877/Matrix multiplication with a vector in EuclideanSpacehttps://ask.sagemath.org/question/55490/matrix-multiplication-with-a-vector-in-euclideanspace/ Hello
Oversimplified version of my question is "How can I multiply a vector with a matrix?"
E.<x,y,z>=EuclideanSpace(start_index=0)
vf=E.vector_field([x*y,x^2,z**2]) # the components are for testing purposes
M=Matrix(RR,3,3);M[:]=1 # this is just a test
# Neither this
M*vf
# nor this works
M*vf.at(E((3,2,1)))
The only work around I could think of is
v=vf.at(E((3,2,1)))
q=vector([v[0],v[1],v[2]])
M*q
or directly on the vector field
v=vector([v[0].expr(), v[1].expr(), v[2].expr()])
M*q
With this approach I have to put the transformed components back in the vector field defined within EuclideanSpace.
I would like to use EuclideanSpace to work with vectors.
Is there an easy way to handle this? Basically I am looking for an easy way to transform a vector field defined in EuclideanSpace?
Thanks in advance for your help.
curios_mindFri, 29 Jan 2021 01:25:10 +0100https://ask.sagemath.org/question/55490/totally unimodality testhttps://ask.sagemath.org/question/54739/totally-unimodality-test/ I have seen that a `totally unimodality test exists` under `Python`. Has anybody implemented it as a function ? (It's a heavy work)CyrilleFri, 18 Dec 2020 07:52:15 +0100https://ask.sagemath.org/question/54739/use type signature in functions for matrix and vectorhttps://ask.sagemath.org/question/54478/use-type-signature-in-functions-for-matrix-and-vector/ Hi,
I'm trying to create a function that takes a vector and a matrix. I want to explicitly used typed variables but I'm not sure what to type for the types matrix and vector.
Example with : `fun(i:int): return 0`
So what is the equivalent for this with `func(M, V)`? I tried `func(M: matrix, V :vector)` and `func(M: sage.matrix, V :sage.vector)` but both gave : "name 'matrix' is not defined"
Thanks in advancemochaTue, 01 Dec 2020 01:34:28 +0100https://ask.sagemath.org/question/54478/Partial tracehttps://ask.sagemath.org/question/53133/partial-trace/I'm a total noob in Sage and I've never used Python before, so my question might be stupid. I'm working on a system of 2 qubits (in a generic state) interacting with the environment (in a state \ket(00)). I have an evolved state of the global system (the system of interest + ambient) and I need to trace out the ambient. I know how to do it analytically but I have no idea how to do it in Sage. I would appreciate any help.CarolliceFri, 21 Aug 2020 18:26:11 +0200https://ask.sagemath.org/question/53133/NotImplementedErrorhttps://ask.sagemath.org/question/52972/notimplementederror/Hi
UBUNTU 18.04, SageMath 9.1, Jupyter Notebook
its ok when I set matrixForm=False, but I got **NotImplementedError** error when I choose matrixForm=True.
Why ?
var('r0_0,r0_1,r1_0,r1_1,p_0,p_1,v_0,v_1,x,y')
def projectvOnV(v,V) :
# projection matrix
x=V[0];y=V[1]
projectM=matrix(SR,[(x^2/(x^2 + y^2), x*y/(x^2 + y^2)),
(x*y/(x^2 + y^2), y^2/(x^2 + y^2))])
return projectM*v
matrixForm=True
if matrixForm :
Vr0 = matrix(SR,[r0_0,r0_1]).transpose()
Vr1 = matrix(SR,[r1_0,r1_1]).transpose()
Vp = matrix(SR,[p_0,p_1]).transpose()
else :
Vr0 = vector(SR,[r0_0,r0_1])
Vr1 = vector(SR,[r1_0,r1_1])
Vp = vector(SR,[p_0,p_1])
Vp=Vr0-Vr1
Vp_0=projectvOnV(Vr0,Vp)# vector Vr0 projeted on Vp
show("Vp : ",Vp,Vp.parent())
show("Vp_0 : ",Vp_0,Vp_0.parent())
Vin=Vr0-Vp_0
displayedObject=[Vin,Vr0,Vr1,Vp,Vp_0]
if matrixForm :
for e in displayedObject :
e=vector(e)
@interact
def _( thetaNumVr0=slider(0.01,2*pi,0.05,default=3.5),
rhoNumVr0=slider(0.01,4,0.05,default=2),
thetaNumVr1=slider(0.01,2*pi,0.05,default=5),
rhoNumVr1=slider(0.01,4,0.05,default=1)):
numL=[r0_0==rhoNumVr0*cos(thetaNumVr0),r0_1==rhoNumVr0*sin(thetaNumVr0),
r1_0==rhoNumVr1*cos(thetaNumVr1),r1_1==rhoNumVr1*sin(thetaNumVr1)]
plt=plot(arrow([0,0],Vr0.subs(numL),color="red",width=3,linestyle='dashed'))
plt+=text("Vr0",(Vr0*4/5+vector([0.1,0.1])).subs(numL),color="red",fontsize=10)
plt+=plot(arrow([0,0],Vr1.subs(numL),color="blue",width=3,linestyle='dashed'))
plt+=text("Vr1",(Vr1*4/5+vector([0,0.1])).subs(numL),color="blue",fontsize=10)
plt+=plot(arrow((Vr1).subs(numL).list(),(Vr1+Vp).subs(numL),color="grey",width=3,linestyle='dashed'))
plt+=text("Vp",((Vr1+Vp).subs(numL))/2,color="grey",fontsize=10)
plt+=plot(arrow([0,0],(Vp_0).subs(numL),color="pink",width=3,linestyle='dashed'))
plt+=text("Vp_0",(Vp_0*4/5+vector([0.1,0.1])).subs(numL),color="pink",fontsize=10)
plt+=plot(arrow([0,0],(Vin).subs(numL),color="black",width=3,linestyle='solid'))
plt+=text("Vin",(Vin*4/5+vector([0.1,0.1])).subs(numL),color="black",fontsize=10)
show(plt,aspect_ratio=1)ortolljWed, 12 Aug 2020 22:04:15 +0200https://ask.sagemath.org/question/52972/parallel matrix rankhttps://ask.sagemath.org/question/52889/parallel-matrix-rank/ I have* a (relatively) large non-square symbolic matrix $M$ which I would like to calculate the rank of. The entries of the matrix are polynomials with coefficients in a (small) finite field, and the matrix is relatively sparse, although currently it's being treated as a non-sparse matrix.
The rank could simply be calculated as `M.rank()`, but I'm not sure whether or not the matrix rank algorithm implemented works in parallel or not. Could someone clarify whether or not parallel calculation of matrix rank is implemented in Sage or not? If it is implemented but isn't used by default, how do I make use of the parallel version?
Thanks!
(*) It's more accurate to say that I'm *going to have* such a matrix; my initial version of the code was written in Mathematica, and I'm currently porting the code to Sage after too many headaches with Mathematica's handling of finite fields, which could definitely be improved upon. So unfortunately I'm not yet at the stage of running `M.rank()` and checking whether or not it runs in parallel.BakerbakuraThu, 06 Aug 2020 13:08:00 +0200https://ask.sagemath.org/question/52889/Test for a valid entry in a matrixhttps://ask.sagemath.org/question/52874/test-for-a-valid-entry-in-a-matrix/I have a question of methodology. The following code work nicely:
def ratio_for_pivot(mat, var_ent):
return [mat[i][mat.ncols()-1]/mat[i][var_ent] for i in range(mat.nrows()-1)]
It takes a matrix `mat` and a column number `var_ent` of this matrix and does the division of terms in the same row and return a vector. But as you can see, `mat[i][var_ent]` coud be 0. So I need a test. Should I construct a test function or is it possible to make directly a test (?) inside the list construction. For instance, this works as expected :
def pos_rat(x,y) :
if y>0:
return x/y
else :
return "NAN"
def ratio_for_pivot(mat, var_ent):
return [pos_rat(mat[i][mat.ncols()-1],mat[i][var_ent]) for i in range(mat.nrows()-1)]
But what I want to know is that is there a more compact way to obtain the same result.
As an exemple : `M=[[1, 2, 3, 4],[2, 3, 0, 1],[5, 4, 1, 3]]`
PS : Sorry for the ugly title I have no inspiration.CyrilleWed, 05 Aug 2020 17:20:18 +0200https://ask.sagemath.org/question/52874/Compute determinant of matrix of Schur functions using Kronecker producthttps://ask.sagemath.org/question/52805/compute-determinant-of-matrix-of-schur-functions-using-kronecker-product/Given a matrix $m=(a_{i,j})$, where each $a_{i,j}$ is a Schur function,
by default, `m.det()` will give the determinant using the original multiplication.
But I want it to use the Kronecker product for Schur functions.
I did not find any options to set the Kronecker product as default
multiplication operator in the ring of Schur functions or in the matrix class.
To give an example, let us define such a matrix:
sage: Sym = SymmetricFunctions(QQ)
sage: s = Sym.schur()
sage: f = s[2]
sage: m = matrix([[f, 0], [0, f]])
sage: m
[s[2] 0]
[ 0 s[2]]
Then compare:
sage: m.det()
s[2, 2] + s[3, 1] + s[4]
sage: f * f
s[2, 2] + s[3, 1] + s[4]
sage: f.kronecker_product(f)
s[2]XieSun, 02 Aug 2020 07:43:12 +0200https://ask.sagemath.org/question/52805/List Object not Callablehttps://ask.sagemath.org/question/52552/list-object-not-callable/ [link text](https://imgur.com/a/oGlsdV8)
Hello, see the link given above. Apparently, I need 60 points to upload images. So the idea is I am trying to construct a matrix by appending lists to each row. I am not understanding the error I am receiving.
CoefficientwList is just a list of numbers. From the second picture, you can see that Separation4 maps a list to a list of lists. By typing Separation4(CoefficientwList)[i1], I am extracting the i1th list in the list of lists. I then apply the vector function to make this list a vector and then from the 3rd picture, you see I add it to a row of the matrix M. Is there anything wrong here?
If more info is needed, please tell me but I am not seeing what the issue is. whatupmattSat, 18 Jul 2020 00:49:29 +0200https://ask.sagemath.org/question/52552/Zero Matrix has an Inverse over Finite Field?https://ask.sagemath.org/question/52487/zero-matrix-has-an-inverse-over-finite-field/The following code
M = Matrix([0], ring=GF(4))
M
Prints "[0]" as expected. The inverse of M clearly does not exist. Even so, running
M.inverse()
does not throw an error, and instead prints "[1]". What's going on here?
I am running Sage 8.1. Thanks for the help.brantmvThu, 16 Jul 2020 04:15:30 +0200https://ask.sagemath.org/question/52487/Checking what the span is for a vectorhttps://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/Let's assume I have a vector called v1 and I have a matrix called Matrix.
Let us assume that the vector is in the span of the rows of Matrix. How would I know what the linear combination is? Here is what I do know. Let's assume this is a m by n matrix. I do know there is the span function. So
I can do something like make a list of vectors out of the Matrix. Say something like make an empty list ListofVectors=[].
i=0
while i< m:
ListofVectors.append(M[i])
i+=1
Now doing
v1 in span(ListofVectors)
will give me true assuming v1 is in the span. However, is there a function that tells me what the coefficients are for each term. For example, the vector v1=[3,2,1] for the ListofVectors being [1,1,1], [1,0,0], and [0,1,0] should give me coefficients 1,2,1 respectively as 1*[1,1,1] + 2*[1,0,0] + 1*[0,1,0] gives [3,2,1].
whatupmattThu, 16 Jul 2020 00:20:50 +0200https://ask.sagemath.org/question/52485/Finding basis of image of Matrixhttps://ask.sagemath.org/question/51662/finding-basis-of-image-of-matrix/ Is there a command that explicitly gives me the vectors for the image given a matrix M? If not, lets suppose I have the matrix in row echelon, N. So I create an empty list. Basically, I want to say while i< # of rows, if row i of row echelon form matrix N has a 1, put M[i] in list. But I don't know how to say if a row has a 1( a pivot).
P.s., yes I need the rows. My matrix is like transposed.whatupmattMon, 01 Jun 2020 00:52:22 +0200https://ask.sagemath.org/question/51662/From csv to matrixhttps://ask.sagemath.org/question/51594/from-csv-to-matrix/I have this file copied of an other question
pcb138,pcb180,pcb52,pcb118,pcb
1.46,0.738,0.532,0.72,19.9959
0.64,0.664,0.03,0.236,6.0996
3.29,1.15,0.134,1.54,24.9655
3.94,1.33,0.466,1.94,37.4436
3.18,2.14,0.243,1.47,30.183
2.43,1.3,0.137,1.31,20.8036
3.94,3.49,0.208,0.876,41.3818
3.38,1.04,0.477,2.46,29.478
2.21,0.966,0.457,1.14,24.2387
2.49,1.59,0.298,1.18,26.3198
0.86,0.395,0.02,0.406,8.591
3.38,1.85,0.539,1.5,36.4229
7.39,4.42,0.707,3.55,66.4108
2.49,1.59,0.298,1.18,26.3198
0.86,0.395,0.02,0.406,8.591
3.38,1.85,0.539,1.5,36.4229
7.39,4.42,0.707,3.55,66.4108
It is saved in a file name pcb.dat. With the following code
import csv
file=r'C:\Users\Cyrille\pcb.dat'
reader=csv.reader(open(file))
L=[]
for row in reader:
L.append(row)
show(L)
I can import it in SM. But it's a list composed of strings. And I want a matrix beginning at the second line with Real or Rational numbers. How can I obtain this little and simple (but absolutely necessary) operation ? Therer is a lot of questions on that subject but I cannot see in the answer the one I am searching.CyrilleWed, 27 May 2020 23:57:49 +0200https://ask.sagemath.org/question/51594/Generate >2D Matrixhttps://ask.sagemath.org/question/49762/generate-2d-matrix/I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?
Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.mattbMon, 03 Feb 2020 14:27:19 +0100https://ask.sagemath.org/question/49762/matrix base ring changes by a complex multiplicationhttps://ask.sagemath.org/question/49557/matrix-base-ring-changes-by-a-complex-multiplication/The complex multiplication of matrix changes the base ring.
A = matrix(CDF,[[1,2],[3,4]]); print(A)
B = 2*A
C = I*A
print(type(A))
print(type(B))
print(type(C))
The result is
[1.0 2.0]
[3.0 4.0]
<class 'sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense'>
<class 'sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense'>
<class 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
So the multiplication by an imaginary number breaks the CDF property.
This is very inconvenient for numerical calculations, because the symbolic computation is very slow.cxrjddWed, 15 Jan 2020 10:59:29 +0100https://ask.sagemath.org/question/49557/Exponential function does not work for diagonal matriceshttps://ask.sagemath.org/question/49190/exponential-function-does-not-work-for-diagonal-matrices/ D = diagonal_matrix([0])
exp(D)dejongWed, 25 Dec 2019 21:27:47 +0100https://ask.sagemath.org/question/49190/Add commas after matrix-elements in matrix()https://ask.sagemath.org/question/49184/add-commas-after-matrix-elements-in-matrix/I want to replace the brackets, but to add commas after each matrix-element, except the last ones in the rows.
I get only the first part.
Is there an elementary way to add commas to the sage-matrix? <br>
*Because `matrix()` has the great advantage that the elements are **aligned** in all cases.*
m = matrix([[2222,333,44444], [1,0,0]])
mymatrix = m.str().replace('[', '').replace(']', '') # replace [, ]
#.... # add commas
print mymatrix
Out Actual:
2222 333 44444
1 0 0
Out Target:
2222, 333, 44444
1, 0, 0geroyxWed, 25 Dec 2019 20:06:27 +0100https://ask.sagemath.org/question/49184/Keeping zeros after matrix multiplicationhttps://ask.sagemath.org/question/48925/keeping-zeros-after-matrix-multiplication/ Hello, Sage Community!
Suppose I do the following:
var('x y z')
u = vector([0, 1, 1])
v = vector([x, y, z])
u * v
The result is obviously `y+z`. I would like to keep the zeros after the multiplication, in order to have `0x+y+z` as my result. Is it possible?
Thanks in advance for your answers!dsejasSun, 01 Dec 2019 17:46:45 +0100https://ask.sagemath.org/question/48925/Absence of column vectorshttps://ask.sagemath.org/question/48860/absence-of-column-vectors/ I'm curious as to why column vecotrs seem to be non-existent in sage. To give you some context, I work with the following system:
R3 = IntegerModRing(3)
c_7_4 = [
[1, 0, -2, 0, 0, 0, 1],
[1, 1, 0, 0, -2, 0, 0],
[0, 1, 1, 0, 0, -2, 0],
[0, 0, 1, 1, 0, 0, -2],
[0, -2, 0, 1, 1, 0, 0],
[-2, 0, 0, 0, 1, 1, 0],
[0, 0, 0, -2, 0, 1, 1]
]
C3 = Matrix(R3, c_7_4)
B3 = C3.right_kernel().basis()
Clearly, the right kernel of C3 is a column vector, but if you run this code, you would find that
print(B3[0]) # returns a row vector
print(B3[0] * C3) # returns an answer
print(C3 * B3[0]) # returns an answer
Given that a column matrix should reasonably be written
[[a],[b]]
Why is this not the case? Specifically, is there a coding limitation to what the programmers can do which forces them to implement it in this way, or is there some mathematical usefulness to this which is beyond my understanding?
Thanks!
KraigSat, 23 Nov 2019 18:44:38 +0100https://ask.sagemath.org/question/48860/[Sage vs Python] Matrix indexing and list standardshttps://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/Hello, Sage Community!
Since I learned Sage, I have used the notation `A[i][j]` (which obviously came from my previous experience with Python lists) to access the (i+1, j+1)-th element of a matrix. However, I discovered a few weeks ago that I can also use the syntax `A[i,j]`. I am wondering which of these two ways of matrix indexing is preferred in Sage?
My tests using `timeit` indicate that the second syntax is faster than the first one by a factor of 500. So I suppose it to be simply natural that the fastest syntax is preferred. However, why to deviate from the list syntax? After all, lists and lists of lists are (in some sense) arrays, too.
On the other hand, talking about lists, there is the syntax `[1..10]`, which creates the list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Why is this the case when Python would ignore the 10? For example, `range(1, 10)` is equivalent to
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Even `srange(1, 10)` creates the same list, but with Sage `Integer`s, instead of Python's `int`s. Is there any good reason for having `[1..10]`be equivalent to `srange(1, 11)`?
Thanks in advance for your answers!dsejasMon, 18 Nov 2019 23:18:18 +0100https://ask.sagemath.org/question/48802/Why SageMath cannot inverte this matrix?https://ask.sagemath.org/question/48771/why-sagemath-cannot-inverte-this-matrix/Here is a list of three self-adjoint commuting matrices I would like to simultaneously diagonalize:
SageMath version 8.9, Release Date: 2019-09-29
sage: kk=QQ.algebraic_closure()
sage: L=[[[0,1,0,0,0,0,0,0],[1,4,0,1,1,1,1,0],[0,0,1,1,1,1,1,2],[0,1,1,1,1,1,1,2],[0,1,1,1,1,1,1,2],[0,1,1,1,1,1,1,2],[0,1,1,1,1,1,1,2],[0,0,2,2,2,2,2,3]],[[0,0,1,0,0,0,0,0],[0,0,1,1,1,1,1,2],[1,1,1,1,1,1,1,2],[0,1,1,2,1,1,1,2],[0,1,1,1,2,1,1,2],[0,1,1,1,1,2,1,2],[0,1,1,1,1,1,2,2],[0,2,2,2,2,2,2,3]], [[0,0,0,1,0,0,0,0],[0,1,1,1,1,1,1,2],[0,1,1,2,1,1,1,2],[1,1,2,0,0,2,3,2],[0,1,1,0,2,2,2,2],[0,1,1,2,2,1,1,2],[0,1,1,3,2,1,0,2],[0,2,2,2,2,2,2,4]]]
sage: M=[matrix(kk,l) for l in L]
sage: M
[
[0 1 0 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0]
[1 4 0 1 1 1 1 0] [0 0 1 1 1 1 1 2] [0 1 1 1 1 1 1 2]
[0 0 1 1 1 1 1 2] [1 1 1 1 1 1 1 2] [0 1 1 2 1 1 1 2]
[0 1 1 1 1 1 1 2] [0 1 1 2 1 1 1 2] [1 1 2 0 0 2 3 2]
[0 1 1 1 1 1 1 2] [0 1 1 1 2 1 1 2] [0 1 1 0 2 2 2 2]
[0 1 1 1 1 1 1 2] [0 1 1 1 1 2 1 2] [0 1 1 2 2 1 1 2]
[0 1 1 1 1 1 1 2] [0 1 1 1 1 1 2 2] [0 1 1 3 2 1 0 2]
[0 0 2 2 2 2 2 3], [0 2 2 2 2 2 2 3], [0 2 2 2 2 2 2 4]
]
I applied the following process to find the appropriate change of basis matrix p:
sage: p=identity_matrix(kk,8)
sage: q=~p
sage: for m in M:
....: a=q*m*p
....: da, pa = a.jordan_form(transformation=True)
....: p=p*pa
....: q=~p
But the process does not finish (quickly enough, as expected), if I break it (Ctrl+C) after 5 minutes, I get:
sage: p
[1 1 1 1 1 1 1.000000000000000? 1]
[0 0 0 -0.2360679774997897? 4.236067977499789? -1 0 9]
[1 1 1 -1 -1 0 1.000000000000000? 10]
[-3.959901309262505? -0.4551399712853820? -0.3198201782016355? 0 0 1 1.734861458749522? 11]
[-1.747468453907949? 0.1971262975999527? 1.126757059617217? 0 0 1 -2.576414903309221? 11]
[1.441290910412462? 0.5668886275946296? -2.523095590631670? 0 0 1 -0.4850839473754211? 11]
[3.266078852757992? -1.308874953909201? 0.7161587092160885? 0 0 1 0.3266373919351204? 11]
[0 0 0 0.618033988749895? -1.618033988749895? -2 0 18]
[I removed spaces between entries for p being more readable]
and in fact the problem is that SageMath cannot inverte p, the following does not finish (after several hours):
sage: q=~p
whereas the matrix p is invertible:
sage: det(p)
97930.0?
**Questions**: What is the problem? How to solve it?
______________
By applying the answer of @rburing I get the following:
sage: sigma=K.embeddings(QQbar)[0]
sage: P=p.apply_map(sigma)
sage: P
[ 1 1 1 1 1 1 1 1]
[ 9 -1 0 0 0 0 0.?e7 0.?e7]
[ 10 0 1 1 1 1 -1 -1]
[ 11 1 0.?e7 0.?e7 0.?e6 0.?e8 0 0]
[ 11 1 0.?e8 0.?e7 0.?e7 0.?e7 0 0]
[ 11 1 0.?e8 0.?e7 0.?e8 0.?e7 0 0]
[ 11 1 0.?e8 0.?e7 0.?e7 0.?e8 0 0]
[ 18 -2 0 0 0 0 0.?e7 0.?e7]
sage: Q=q.apply_map(sigma)
sage: Q*M[0]*P
[ 9 0 0 0 0 0 0 0]
[ 0 -1 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0.?e7 0]
[ 0 0 0 0 0 0 0 0.?e7]
whereas
sage: M[0].eigenvalues()
[9, -1, 0, 0, 0, 0, -0.2360679774997897?, 4.236067977499789?]
How to make this method working better?Sébastien PalcouxSat, 16 Nov 2019 11:16:03 +0100https://ask.sagemath.org/question/48771/Bug when testing if a matrix is in SRhttps://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/On sagemath 8.9, I find the behavior of ``SR`` with matrix a bit strange.
sage: M = matrix([[0, -1], [1, 0]])
sage: print(SR(M)+M)
[[ 0 -1]
[ 1 0] -1]
[ 1 [ 0 -1]
[ 1 0]]
What is the use of coercing a matrix to the symbolic ring if it cannot be used as a matrix afterward?
These two lines of code also both raise an attribute error:
sage: SR(M) == M
sage: M in SR
AttributeError: 'ComplexIntervalField_class_with_category' object has no attribute 'complex_field'
By itself, it is not very relevant, but some element constructors (e.g. ``lie_algebra``) contain a line
if x in self.base_ring():
do something
which means ``SR`` cannot be used as a base ring.
Is this a known bug?
A workaround would be to add a single line in ``SR.__contains__`` to return false when ``x`` is matrix (or maybe there are case when a matrix is considered in ``SR``, I don't know), but this would not solve the underlying issues.Florentin JaffredoSat, 02 Nov 2019 17:09:26 +0100https://ask.sagemath.org/question/48591/