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, 08 Dec 2022 06:13:33 +0100How to speed up this codehttps://ask.sagemath.org/question/65274/how-to-speed-up-this-code/Given a square matrix M, to find (say) five sets of, (say) 4 matrices A1,A2,A3,A4 such that
(i) each Ai is a unique 0,1 or 0,-1 matrix
(ii) each Ai is not - all zeroes or all ones or all minus ones matrix(that is, matrices with all entries same are not allowed)
(iii) M= A1+A2+A3+A4
(iv) Each matrix product AiAj (i,j=1,2,3,4)is a linear combination of A1,A2,A3,A4 with integer coefficients.
This is the same problem which was discussed in :
https://ask.sagemath.org/question/64286/cant-find-error-in-my-code/
But there we wanted “all possible combinations” satisfying the required conditions. There we basically discussed the process for first getting all combinations satisfying (iii) and then checked condition(iv). But for 3rd order and above no output even after one week.
So thought that rather than first collecting all combinations satisfying(iii) it would be better to check (iv) alongside individually and after say, five outputs , the program ends, as even this much would also help me for my work .
I have made the following code:
#Given Matrix
M = matrix(2,2, [1, 2, 2, -1])
show("M=",M)
n = int(input("Enter the order of the matrix M : "))
T = Tuples((0,1),(n^2))
#Removing all-zero and all-one tuples as not needed for our case"
R=T.list()[1:-1]
#Forming n by n matrices from list in R. Contains all {0,1 }matrices.
P = []
for v in R:
P.append(matrix(ZZ,n,n,v))
#Forming all n by n , {0,-1} matrices.
N=[]
for a in P:
b=-a
N.append(b)
test_sum = int(input("In how many matrices you want to break M :"))
num_nonneg = int(input("How many matrices with non-negative entries you want :"))
num_nonpos = (test_sum)-(num_nonneg)
nonneg_comb = Combinations(P, num_nonneg)
nonpos_comb = Combinations(N, num_nonpos )
#Defining a function which converts matrix to vector
def mat_to_vector(m):
return vector( sum(map(list,m.rows()), []), immutable=True )
Counter=0
X = ZZ^(n^2)
for a in nonneg_comb:
for b in nonpos_comb:
flag=0
V = X.span(mat_to_vector((a+b)[i]) for i in range(len((a+b))))
for i in range(test_sum):
for j in range(test_sum):
if( (sum(a+b)!=M) or (mat_to_vector((a+b)[i]*(a+b)[j]) not in V)):
flag=1
if(flag==0):
Counter+=1
show("(",Counter,").", a+b)
if Counter==5:
break;
But this is also time taking for higher orders. So, wanted to know how it can be speeded up or how the code given by Max Alekseyev in the above link can accordingly be edited.sgmthThu, 08 Dec 2022 06:13:33 +0100https://ask.sagemath.org/question/65274/Forming unique collections of matriceshttps://ask.sagemath.org/question/64630/forming-unique-collections-of-matrices/I am facing following two similar kind of problems:
Problem(1) Suppose I have the following code:
A=matrix(2,2,[1,1,1,1])
B=matrix(2,2,[2,2,2,2])
C=matrix(2,2,[3,3,3,3])
D=matrix(2,2,[4,4,4,4])
L1=[A,B,C]
L2=[C,B,A]
L3=[A,C,D]
show(L1,L2,L3)
Then in the output I want L1 and L3 only, as L1 and L2 contain same matrices.
Problem(2): Now suppose we have the following code:
P=matrix(2,2,[5,5,5,5])
Q=matrix(2,2,[6,6,6,6])
R=matrix(2,2,[7,7,7,7])
S=matrix(2,2,[8,8,8,8])
T1=[P,P,Q]
T2=[Q,P,P]
T3=[P,R,S]
show(T1,T2,T3)
Here also, in the output I want T1 and T3 only, as T1 and T2 contain same matrices. Note that I want T1 as it is, that is, P should be there twice.mak3521Wed, 26 Oct 2022 18:51:02 +0200https://ask.sagemath.org/question/64630/How to speed up the code?https://ask.sagemath.org/question/64373/how-to-speed-up-the-code/Given a matrix M, to find all 1,-1 matrices which satisfy the condition given in the code.
For n=3, I am getting the outputs easily. For n=6, it ran for around 12 hours and got "flint exception error".
I understand that len(T) is huge so there might be some memory issues. Is there anyway to get over it?
> n=3
T=Tuples((1,-1),n^2)
A=[matrix(n,n,t) for t in T]
for a in A:
if 3*a^2==M:
show(a)
mak3521Mon, 10 Oct 2022 16:59:47 +0200https://ask.sagemath.org/question/64373/Can't find error in my codehttps://ask.sagemath.org/question/64286/cant-find-error-in-my-code/Please refer to my question at
https://ask.sagemath.org/question/63878/making-code-faster-by-splitting-and-use-of-supercomputer/
According to the suggestion given in the end by Max Alekseyev I have made a complete code. Here I am showing only the initial part , as problem lies in it . I don't know what I am missing or what mistake I have made.
> M = matrix(3,3, [1, 1, 1, -1, 1, 1, -1, -1, 1])
List_S=[M^0,M^1,M^2]
n = 3
V = GF(2)^(n^2)
VL1=V.list()[1:]
# Removing the all-zero list because not needed for our purpose
VL2=VL1[:-1]
#Removing the all-ones list because not needed for our purpose
P=[]
for v in VL2:
P.append(matrix(ZZ,n,n,v))
#Forming n by n matrices from list in VL2. Contains all 0,1 matrices.
N=[]
for p in P:
k=-p
N.append(k)
#Forming n by n matrices from P by taking their negative. Contains all 0,-1 matrices
U = []
for a in P:
U.append(a)
for b in N:
U.append(b)
#Taking union of P and N
show("U=",len(U))
def mat_to_vector(m):
temp = []
for i in range(m.ncols()):
for j in range(m.nrows()):
temp.append(m[i][j])
return(vector(temp))
#Defining a function which converts matrix to vector
CAND=[]
for i in range(len(U)):
List_U=[U[i]]
List_SU=List_S+List_U
W=U[:]
W.remove(W[i])
T=tuple(List_SU)
X = ZZ^(n^2)
Y = X.span(mat_to_vector(T[i]) for i in range(len(T)))
for w in W:
if mat_to_vector(w) in Y:
CAND.append(mat_to_vector(w))
VTM=[matrix(ZZ,n,n,u) for u in CAND]
for b in VTM:
b.set_immutable()
B = set(VTM)
show("B=",len(B))
According to the suggestion, len(B) should come out to be less than len(U).
But it is coming out to be same as len(U). Kindly find the error.sgmthMon, 03 Oct 2022 16:34:28 +0200https://ask.sagemath.org/question/64286/Forming Combinations with conditionshttps://ask.sagemath.org/question/64104/forming-combinations-with-conditions/Suppose B is a finite collection of distinct square matrices of nth order.
And , A a subcollection of B.
I want unique combinations of four distinct elements -three from A and one from B.
I tried this
X=Combinations(A,3)
Y=Combinations(B,1)
for i in range(len(X)):
for j in range(3):
for k in range(len(Y)):
if ((X[i])[j])!=(Y[k]):
show(X[i]+Y[k])
I know problem is with my "if" command.
My question is how to change the “if” command so that none of the matrices in X[i] equals Y[k], so that we get distinct matrices in a combination.
Further, how to get unique combinations.
Thanks.
sgmthTue, 20 Sep 2022 06:47:18 +0200https://ask.sagemath.org/question/64104/Matrix with negative indiceshttps://ask.sagemath.org/question/63397/matrix-with-negative-indices/Is there a way or hack to operate with matrices with negative indices? For example a 3x3 matrix with row indices {-1,0,1} and column indices {-1, 0, 1}. So one can do m[-1,-1] for first row and column entry, while still retain matrix operations (ring ops, determinant, etc.). I'm dealing with random walks where the symetry needs that.jesuslopMon, 25 Jul 2022 15:15:11 +0200https://ask.sagemath.org/question/63397/Matrix constructor runs out of memoryhttps://ask.sagemath.org/question/60673/matrix-constructor-runs-out-of-memory/I am trying to construct a matrix with entries in $\mathbb{Q}(q)$, from a list of Sage vectors. I do this using
`A = matrix(QQ['q'].fraction_field(), relations)
`
where the vectors in the list `relations` are Sage vectors over `QQ['q'].fraction_field()`.
The list `relations` is generated by a subroutine involving some randomness, and in many cases the above works fine but sometimes my script uses too much memory and is killed by the operating system. Tracing the memory allocation it is clear that the problem occurs at the given line where I construct the matrix from the list of vectors.
The sorts of matrices for which the script fails are tend to have about 100 columns and 50-100 rows. They are sparse (each row has <= 4 nonzero entries) and the nonzero entries are of the form `-2/q^x + 2` where `x` is in the range `10000 - 100000`.
It seems clear that when some combination of the order of magnitude of the exponents `x` and the matrix size is reached, then the matrix constructor uses too much memory. I wondered if there is a solution anyone can suggest, or even just explain a bit more about why this constructor seems to max out on memory?
(P.S. I plan to re-implement things using sparse matrices, and maybe this will help, though I'd still like to understand better why the dense version fails.)PatrickKinnearThu, 13 Jan 2022 19:01:24 +0100https://ask.sagemath.org/question/60673/Partitioning a (1,-1)square matrix into (0,-1,1)summand matriceshttps://ask.sagemath.org/question/60560/partitioning-a-1-1square-matrix-into-0-11summand-matrices/ Given a square matrix M with entries from {1,-1} how to find all possible sets of matrices A1,A2,A3.....(with entries from {0,-1,1}), such that M=A1+A2+A3....sgmthThu, 06 Jan 2022 16:25:20 +0100https://ask.sagemath.org/question/60560/Matrix with polynomial entries in Sage.https://ask.sagemath.org/question/59687/matrix-with-polynomial-entries-in-sage/ I am trying to define a function to produce matrices with polynomial entries. I defined the following
def BraidMatrices(i,n,z):
r=identity_matrix(n,n)
r[i,i]=0
r[i,i+1]=1
r[i+1,i]=1
r[i+1,i+1]=z
return r
z=VariableGenerator('z')
n=3
t1=BraidMatrices(1,n,z[1])
t1
But it has some errors:
1099 n = self.pyobject()
1100 except TypeError:
-> 1101 raise TypeError("unable to convert %r to an integer" % self)
1102 if isinstance(n, sage.rings.integer.Integer):
1103 return n
TypeError: unable to convert z1 to an integer
How to define a matrix with polynomial entries in Sage? Thank you very much.lijr07Thu, 11 Nov 2021 17:09:18 +0100https://ask.sagemath.org/question/59687/Assuming matrix is positive semi-definitehttps://ask.sagemath.org/question/58070/assuming-matrix-is-positive-semi-definite/ I'm trying to find moments of of transformations of the multivariate normal distribution.
Let $x,y,z\sim N(\mu, \Sigma)$, I want to find out (for example) $E[e^x], E[e^{2x}], E[e^xy]$.
I have defined the following:
x, y, z = var('x y z')
X = vector([x,y,z])
mx, my, mz = var('mx my mz')
mu = vector([mx, my, mz])
sx, sy, sz = var('sx sy sz')
sxy, sxz, syz = var('sxy sxz syz')
assume(sx > 0)
assume(sy > 0)
assume(sz > 0)
S = matrix(SR, [[sx^2, sxy, sxz], [sxy, sy^2, syz], [sxz, syz, sz^2]])
f = (1 / sqrt(S.det() * (2*pi)^3))*exp(-0.5*((X - mu) * S.inverse() * (X - mu)))
And now I'm trying some integration, for example:
(x*f).integrate(z, -Infinity, Infinity).integrate(y, -Infinity, Infinity).integrate(x, -Infinity, Infinity)
I'd expect this to yield `mx` but of course life ain't easy so I get errors related to the covariance matrix:
ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(sx^2*(sy^2*sz^2-syz^2)
>0)', see `assume?` for more details)
Is sx^2*(sy^2*sz^2-syz^2)
-sxy^2*sz^2
+2*sxy*sxz*syz
-sxz^2*sy^2 positive or negative?
Even for 1D integration (i.e only over $z$, `(x*f).integrate(z, -Infinity, Infinity)`) I get errors:
ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(sx^2*(sy^2*sz^2-syz^2)
>0)', see `assume?` for more details)
Is sx^2*(sy^2*sz^2-syz^2)
-sxy^2*sz^2
+2*sxy*sxz*syz
-sxz^2*sy^2 positive or negative?
I have tried assuming everything he asks but I still get the same errors. Is there any nice way of telling `SAGE` to assume the matrix `S` is positive-semidefinite?
PS I have managed to work with two 2D case, by using correlation and the "full" version of the formula (rather than the vectorized):
x = var('x')
y = var('y')
mux = var('mux')
muy = var('muy')
sx = var('sx')
sy = var('sy')
r = var('r')
assume(r<=1)
assume(r>=-1)
k = 1/(2*pi*sx*sy*sqrt(1-r^2))
fxy = k*exp(-(1/(2*(1-r^2)))*(((x-mux)/sx)^2 - 2*r*((x-mux)/sx)*((y-muy)/sy) + ((y-muy)/sy)^2))
If I run `integrate(integrate(x*fxy, y, -Infinity, Infinity), x, -Infinity, Infinity).simplify()`, the result is eventually the correct one (`mux`).guyaspWed, 21 Jul 2021 09:58:41 +0200https://ask.sagemath.org/question/58070/Define matrix indexed by partitionshttps://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/ I want to define the following matrix in sage: $J$ is of size Partitions(n), and $J_{\lambda, \mu} = 1$ if $\lambda'=\mu$ and $0$ else.
Can someone please help? mathstudentTue, 29 Jun 2021 21:00:56 +0200https://ask.sagemath.org/question/57814/Finding centraliser algebras of a finite set of matriceshttps://ask.sagemath.org/question/57363/finding-centraliser-algebras-of-a-finite-set-of-matrices/Let $S$ be a finite set of $n \times n$-matrices over a field $K$ (lets say finite or the real or complex field).
Is it possible to obtain the $K$-algebra (or at least its vector space dimension) of $n \times n$-matrices X in Sage with $XY=YX$ for all $Y \in S$?
(I can only think of a way for doing this for finite field with very small $n$ by looking at all elements,but maybe there is a better technique in Sage)klaaaSun, 30 May 2021 13:48:31 +0200https://ask.sagemath.org/question/57363/matrix is found singular over CC, nonsingular over CDFhttps://ask.sagemath.org/question/56365/matrix-is-found-singular-over-cc-nonsingular-over-cdf/I am trying to take the inverse of complex valued matrix where every entry is either +/-1,+/-i,or 0. The matrices are quite large and I am getting substantial rounding errors when I construct the matrix using "matrix(CDF,array)" and then take the inverse, but I still get an output.
When I construct the matrix using the same array but using "matrix(CC,array)" and then take the inverse I get this error:
ZeroDivisionError: input matrix must be nonsingular
I am confused because the array when I use "matrix(CC,array)" is exactly the same as the array when I use CDF, however "matrix(CDF,array)" is nonsingular and the function inverse works.
Why is this happening and is there another way I can avoid large rounding errors when taking the inverse?
I hope this is enough detail. I'm not really sure how to provide an example of this error occurring since the smallest example of the array I am
working with is still quite large (52x52) and I haven't been able to get this error to occur with smaller simpler arrays.
This is the encoded matrix over the Gaussian Integers - I had been using QQbar() to run it since it worked to get the inverse.
`b = [286102294921876, 178813934326175001, 69849193096160908206250, 27284841053187847167978515625, 10658141036401502788066894531250000, 179100036621093756, 111937522888183596880, 69960951805114746112500, 43725594878196716328140625, 27343048714101314544736328125, 17080310499295592308075000000000, 10675194062059745192527954101562500, 1716613769531255, 112652778625488296880, 44004991650581359960953125, 17189449863508343696746875000000, 63948846218409016728401336669921875, 895500183105468780, 559687614440917984400, 349804759025573730562500, 218627974390983581640703125, 136715243570506572723681640625, 85401552496477961540375000000000, 53375970310298725962639770507812500, 8583068847656275, 563263893127441484400, 85947249317541718483734375000000, 319744231092045083642006683349609375, 4477500915527343900, 2798438072204589922000, 1749023795127868652812500, 1093139871954917907959375000, 683212419971823692323242187500, 427007762482389807701875000000000, 266879851551493629813198852539062500, 42915344238281375, 2816319465637207422000, 1100124791264533997803125000, 429736246587708592418671875000000, 1598721155460225418210033416748046875, 22387504577636719500, 13992190361022949610000, 8745118975639343264062500, 5465699359774589539796875000, 3416062099859118461616210937500, 2135038812411949038509375000000000, 1334399257757468149065994262695312500, 35762786865235000, 13969838619232179688125, 5456968210637569433595703125, 2131628207280300557632452392578125, 1332267629550187848508377075195312500]`
The issue I'm now having is that I expected the inverse this matrix to look much different - as well as the determinant to be much larger.beanplantzThu, 25 Mar 2021 02:03:28 +0100https://ask.sagemath.org/question/56365/Determinant Function on Qphttps://ask.sagemath.org/question/57057/determinant-function-on-qp/ See attachment[C:\fakepath\Screenshot (104).png](/upfiles/16206765974636286.png)
I basically have 2 matrices with entries in Qp. I cant exactly do Matrix2.charpoly() as this is det (xI-Matrix2) and I want determinant of (I-xMatrix2). So I decided to just type det(I-x*Matrix2) but it gives me error. Is there a way to fix this?
If you want me to be honest, I get an error just from I-x*Matrix2. So it might not even be the determinant function. whatupmattMon, 10 May 2021 22:01:39 +0200https://ask.sagemath.org/question/57057/Obtaining directed graphs associated to matriceshttps://ask.sagemath.org/question/56433/obtaining-directed-graphs-associated-to-matrices/Let $M$ be an $n \times n$-matrix with entries only 0 or 1 and all diagonal entries equal to 1. (usually $M$ is upper triangular)
Let $R$ be the same matrix as $M$ but with all diagonal entries set to zero.
Let $U=(u_{i,j})$ be the matrix with 1 as an entry if $R^2$ (the matrix product of $R$ with itself) has a non-zero entry in the same position and let $U$ have 0 in this entry if $R^2$ has 0 as an entry in this position.
Let $C=(c_{i,j})$ be the matrix $R-U$.
Let $G_M$ be the directed graph with $n$ vertices and there is an arrow from $i$ to $j$ if and only if $c_{i,j}$ is 1.
For example when $M$ is the matrix with rows $[1,1,1],[0,1,1],[0,0,1]$ then the graph $G_M$ has 3 vertices with an arrow from 1 to 2 and an arrow from 2 to 3.
My question is whether there is a quick method to obtain all such 0-1 matrices with Sage for a given $n$ and the associated graph $G_M$ displayed as a picture (and as a graph in sage).klaaaTue, 30 Mar 2021 17:51:14 +0200https://ask.sagemath.org/question/56433/A matrix containing differential operators acting on a matrix containing functionshttps://ask.sagemath.org/question/56311/a-matrix-containing-differential-operators-acting-on-a-matrix-containing-functions/Suppose I have a 2x2 operator matrix, D. Some of its elements are of the form (d/dx), and (d2/dx2).
Now when this matrix is multiplied with another matrix, f, whose elements are functions of x, I will get the final matrix whose corresponding elements are differentiated.
for example: **D = matrix([[d/dx, d3/dx3], [d2/dx2, d2/dx2]])** is an operator matrix which operates on a function matrix, **f(x) = matrix([[x, x^2], [x^3, x]])** as D(f(x)) = D*f(x), (simple matrix multiplication).
Writing D = matrix([[diff( , x), diff( , x, 3)], [diff( , x, 2), diff( , x, 2)]]) does not work as diff() function needs an input function.
So how can I write the D() operator matrix?
PS: I could use f.apply_map(lambda e: diff(e, x)), but then it applies d/dx to all elements in f(x). Whereas, I have 'diff()' operators of different orders in the D matrix.ApoorvMon, 22 Mar 2021 13:29:51 +0100https://ask.sagemath.org/question/56311/Finding positive matriceshttps://ask.sagemath.org/question/56173/finding-positive-matrices/ Hi. I am new in Sage, so please accept my apologies if my question is basic.
Let $L=[A1,A2,A3,...,A100]$ be a list of non-negative matrices. I want to find a list that contains all positive matrices. Thanks in advance.Blue skyMon, 15 Mar 2021 20:17:13 +0100https://ask.sagemath.org/question/56173/tensor_product of matrices over cyclotomic fieldhttps://ask.sagemath.org/question/54762/tensor_product-of-matrices-over-cyclotomic-field/Beginner question: I have created some matrices over a cyclotomic field and attempted to compute some of their tensor products:
N = 3
k.<w> = CyclotomicField(N)
Z = diagonal_matrix(k,N,[w^j for j in range(0,N)])
X = matrix(k,N,N,[1 if (i%N - floor(i/N)) % N == N-1 else 0 for i in range(0,N*N)])
I = identity_matrix(k,N)
I.tensor_product(X)
X.tensor_product(I)
Z.tensor_product(I)
Z.tensor_product(X)
#I.tensor_product(Z)
#X.tensor_product(Z)
If I uncomment either of the last two lines I get
> AttributeError: 'sage.matrix.matrix_generic_sparse.Matrix_generic_sparse' object has no attribute '_rational_matrix'
It seems my code is working when both matrices in the tensor product are rational or when self is non-rational, but not when self is rational and the argument is non-rational. Should I be somehow instructing sagemath to regard self as a matrix over the cyclotomic field?Will OrrickSat, 19 Dec 2020 20:38:38 +0100https://ask.sagemath.org/question/54762/counting number of matrices in finite fieldhttps://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/I am new to sage and I want to count the number of matrices per rank in a given finite field.
Example: M=matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])
in Z/3ZsmokzetaWed, 02 Dec 2020 16:21:39 +0100https://ask.sagemath.org/question/54520/Determinant of large sparse symbolic matriceshttps://ask.sagemath.org/question/54004/determinant-of-large-sparse-symbolic-matrices/ I am working on a symbolic circuit simulation program for electronic circuits (SLiCAP). I have a matlab (MuPAD) and a python version available.
The key task of such a program is the calculation of the determinant of sparse matrices with symbolic entries. The MuPAD (MATLAB symbolic toolbox) version calculates the determinant of a sparse matrix (dim = 52x52) with one symbolic variable (the Laplace variable) in about one minute (minor expansion, algorithm unknown). The Python version uses maxima and the newdet method (Gentleman-Johnson algorithm). This method is limited to dim=50x50, but I had to reduce the size to (30x30) because of memory paging errors reported by Lisp.
Now I would like to try SageMath with the "df" algorithm for this purpose, but its running more then 30 minutes ...
I know the Gentleman-Johnson method is included in PyNAC but it doesn't seem to be included in the sage wrapper.
My questions:
1. Can SageMath be forced to use the Gentleman-Johnson algorithm included in PyNAC?
2. If not can a wrapper be build to do this?
3. If so, can someone help we with this?
Thanks in advance for repying!
written so that it can be used?
SLiCAPThu, 22 Oct 2020 15:55:53 +0200https://ask.sagemath.org/question/54004/Using matrices in Cythonhttps://ask.sagemath.org/question/53856/using-matrices-in-cython/I want to work with matrices in Cython.
But I cannot even define a matrix. Here is the code in my Sage notebook:
sage:%%cython
sage:A=Matrix([[1,0],[0,1]])
Here is the error messsage:
RuntimeError: Error compiling Cython file:
------------------------------------------------------------
...
from sage import all
A=Matrix([[1,0],[0,1]]) ^
------------------------------------------------------------
_Users_macbook__sage_temp_bogon_52702_tmp_jzby0ki0_pyx_0.pyx:2:2: undeclared name not builtin: Matrix
In the end, my goal is to make use of this library in Cython:
matrix/matrix_integer_dense.pyx
Thanks for your help1571Mon, 12 Oct 2020 03:21:48 +0200https://ask.sagemath.org/question/53856/Right choice for a dictionary of matrices with non constant coefficients.https://ask.sagemath.org/question/52312/right-choice-for-a-dictionary-of-matrices-with-non-constant-coefficients/Hi,
K is any field.
I need to store matrices in a dictionary.
Some of them have constant coefficients in K (let M1 be such a matrix),
and some have non constant coefficients, say these coeffcients are functions from t to K (let M2 be such a matrix).
I need to get M2 from the dictionary, and then be able to manipulate it as if it was a function of t.
(T) In particular, I need to be able to manipulate the matrices obtained from M2 by setting t to be equal to some specific values.
Since my functions are polynomials for now, I have tried doing something like this:
```
var('t')
sample = {}
sample[('M1')] = matrix(QQ, 2, [7, 5, 2, 4])
sample[('M2')] = matrix(QQ['t'], 2, [t^2, 2, 3, 1 - t])
```
But then I don't know how to proceed to perform the task (T).
And I have the feeling that there are much better options.
What should I do?
Thanks.BostonFri, 03 Jul 2020 14:32:53 +0200https://ask.sagemath.org/question/52312/what is the equivalent function of mathematica's FullForm in Sage?https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/ i'm trying to find the internal representation of matrices in sage. In Mathematica we can simply use FullForm, and this will show that matrices are internally lists in Mathematica. But i could not find any function that does the same in Sage!salWed, 27 May 2020 22:11:13 +0200https://ask.sagemath.org/question/51590/SageTeX: formatting matriceshttps://ask.sagemath.org/question/51321/sagetex-formatting-matrices/Hello all,
I would like to have sagetex producing matrices with the columns centered. If was writing this by hand, I would do
`\begin{bmatrix*}[c] ... \end{bmatrix*}`
From my XXX.sout file, I see the result `\begin{array}{rrr} ... \end{array}`. Is there a way to control this? Either playing at the sage level directly or by controlling sagetex?
Thanks!
JCFri, 08 May 2020 03:41:24 +0200https://ask.sagemath.org/question/51321/Putting a vector into part of a row of a matrixhttps://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/So from the link given here- https://ask.sagemath.org/question/8366/editing-entries-of-matrix/
From the answer given, I see that to replace a row (say row 1) on a matrix by a list say List 1, I just do
K[1, :] = vector(List1)
For example,
K = Matrix(QQ, 6, 8)
A priori, this is just the 0 matrix. Now writing
K[1, :] = vector([1, 1, 1, 1, 1, 1, 1, 1])
will replace my first row of my 6 x 8 matrix with the entries all 1's.
However, what if I want to replace say row 1 but only the last 6 entries
so I will have `[0, 0, 1, 1, 1, 1, 1, 1]`. Of course, I can just type out
K[1, :] = vector([0, 0, 1, 1, 1, 1, 1, 1])
but this is not viable for larger matrices.
An example would be say my `K = Matrix(QQ, 40, 80)` and I have a list given by `L1 = [1, 1, 1, 1]`.
Suppose I want to replace the row 1 with 40th-43rd entry being `L1`.
Method 1- Type out 76 zeros and do `K[1, :] = vector(L1 with 76 zeros)`. This is not really ideal.
Method 2- Replace entry by entry. This is also not ideal if my `L1` is say of length 20.
Is there a way to just say something like `K[1, 40-43] = vector([L1])`?whatupmattSat, 18 Apr 2020 18:04:49 +0200https://ask.sagemath.org/question/50841/Maps of monomials to monomialshttps://ask.sagemath.org/question/50703/maps-of-monomials-to-monomials/ Let us assume I have a function F that takes say degree 1 monomials to degree 2 monomials and degree 3 monomials in variable x,y (example: multiplication by $x+y^{2}$). I have a basis for degree 1 monomials given by x,y; a basis for degree 2 given by $x^{2},xy,y^{2}$, and a basis for degree 3 monomials given by $x^{3},x^{2}y,xy^{2},y^{3}$. So if I want to express F as a matrix, I can say
g1= F(x), g2=F(y).
Then the first row of my matrix will be coefficient of $x^{2}$ in g1, coefficient of $xy$ in g1, coefficient of $y^{2}$ in g1, coefficient of $x^{3}$ in g1, ...., coefficient of $y^{3}$ in g1. The second row will just be the same with g1 replaced with g2.
The issue is if I want degree 10 monomials, the writing out each basis element is too much, especially if there were more variables. I was wondering if there was a simpler way in Sage to write this in coding language without writing out each basis element. Suppose I want degree k monomials to degree m and n monomials. Basically, I feel I need some command like the following. I feel there should be a package for this maybe?
1. Define F. Let B be a k x (m+n) matrix. Take the space of all degree k monomials (maybe ordered lexigraphically?). Apply F to that space. For each element F is applied to, in the image, extract degree m part. Write it out as a vector in the space of degree m monomials. Same for degree n part. Put vector into part of matrix B.
whatupmattSun, 12 Apr 2020 16:55:35 +0200https://ask.sagemath.org/question/50703/How can I perform matrix operations in a transcendental extension of Q?https://ask.sagemath.org/question/50223/how-can-i-perform-matrix-operations-in-a-transcendental-extension-of-q/ I have three variables $p_w, p_i, p_f$. I want to construct a matrix whose entries are members are rational polynomials in these variables and perform computations with this matrix (ultimately diagonalize it and obtain a general formula for its $n$-th power). But I want to do this computation symbolically, treating the three variables as transcendental elements adjoined to $\mathbb Q$. How an I do this?JackMThu, 12 Mar 2020 14:24:18 +0100https://ask.sagemath.org/question/50223/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/Trigonometric simplifications and matriceshttps://ask.sagemath.org/question/48674/trigonometric-simplifications-and-matrices/I want to simplify trigonometric identities in a matrix. For example, say I want to show that the composition of two rotation matrices is a rotation, I can do with sage something like
> var("theta1,theta2")
Rtheta1=column_matrix([[cos(theta1),sin(theta1)],[-sin(theta1),cos(theta1)]])
Rtheta2=column_matrix([[cos(theta2),sin(theta2)],[-sin(theta2),cos(theta2)]])
produit=Rtheta1*Rtheta2
show(produit.simplify_trig())
show(produit.apply_map(lambda x: x.trig_reduce()))
Note that `simplify_trig` or `trig_reduce` don't work on matrices and that you need to use `apply_map` to use it entry by entry, as detailed in Mike Hansen's answer in [this question](https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/) .
However when I get to 3 matrices, sage can't simplify with the above procedure:
> var("theta1,theta2,theta3")
Rtheta1=column_matrix([[cos(theta1),sin(theta1)],[-sin(theta1),cos(theta1)]])
Rtheta2=column_matrix([[cos(theta2),sin(theta2)],[-sin(theta2),cos(theta2)]])
Rtheta3=column_matrix([[cos(theta3),sin(theta3)],[-sin(theta3),cos(theta3)]])
produit=Rtheta1*Rtheta2*Rtheta3
show(produit.apply_map(lambda x: x.trig_reduce()))
For example, the 1-1 entry in this matrix is returned as `cos(theta1 + theta2)*cos(theta3) - sin(theta1 + theta2)*sin(theta3)` .
The weird thing is that using `(cos(theta1 + theta2)*cos(theta3) - sin(theta1 + theta2)*sin(theta3)).trig_reduce()` produces the correct simplification `cos(theta1 + theta2 + theta3)` .
What's happening here? Any other way to force the simplification?Jean-SébastienFri, 08 Nov 2019 15:53:07 +0100https://ask.sagemath.org/question/48674/removing the repeated rows of a matrixhttps://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/ My sage program outputs a matrix with integer entries in which some rows are repeated. But I want to remove the multiple entries of rows in the output. Kindly somebody explains to me how to do this.
Thank you.GA316Wed, 07 Aug 2019 13:54:59 +0200https://ask.sagemath.org/question/47418/