ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Sep 2019 04:24:50 -0500Determinants of Matrices with Symmetric Functionshttp://ask.sagemath.org/question/47774/determinants-of-matrices-with-symmetric-functions/I'm interested in calculating the determinant of the matrix $ A = (a_{i+j})$ for $0\leq i,j\leq n$ in which $a_{k} = \frac{h_k}{e_k}$ where $h_k, e_k$ are the homogeneous (elementary, resp.) symmetric functions of degree $k$. I would like to express the numerator of $\det(A)$ in terms of monomial symmetric functions.
I have written some code that I think does the job, but it seems an incredibly hacky way of doing it in my opinion. Since I am quite new to sage I was wondering whether someone might be able to take a look at what I've done and suggest a more sage-like way to tackle this problem? My code is below.
I define a set of functions:
# set up two matrices containing symbolic functions hh(i+j), ee(i+j), which are later replaced with symmetric functions h and e
def heMatrices(N):
hh = function('hh')
ee = function('ee')
H = matrix(SR,N,N,lambda i,j:hh(i+j))
E = matrix(SR,N,N,lambda i,j:ee(i+j))
return H,E
# Pointwise divides two matrices (a bit like Hadamard division??)
def elementwiseDivision( M, N ):
assert( M.parent() == N.parent() )
nc, nr = M.ncols(), M.nrows()
A = copy( M.parent().zero() )
for r in range(nr):
for c in range(nc):
A[r,c] = M[r,c] / N[r,c]
return A
# extracts numerator of determinant
def extractNumerator(M):
return det(M).numerator()
# extracts denominator of determinant
def extractDenominator(M):
return det(M).denominator()
# This is dodgy - treates expression as a python string and replaces e(x) with e[x], h(x) with h[x]
def convertToSymmetricString(expr):
st = str(expr)
st = st.replace('(','[')
st = st.replace(')',']')
st = st.replace('ee','e')
st = st.replace('hh','h')
return st
# Puts together the above functions.
def evaluateDeterminant(N):
h,e = heMatrices(N)
A = elementwiseDivision(h,e)
srExpression = extractNumerator(A)
sageExpression = convertToSymmetricString(srExpression)
return sageExpression
Then I open sage in the command line and type the following:
sage: Sym = SymmetricFunctions(QQ)
sage: Sym.inject_shorthands(verbose=False)
sage: ee = evaluateDeterminant(2)
sage: ans = sage_eval(ee, locals=vars())
sage: ans = m(ans)
which returns
2*m[2, 1, 1] + 2*m[2, 2] + 2*m[3, 1] + m[4]
which I believe is what I'm after.WubbaLubbaDubDubFri, 06 Sep 2019 04:24:50 -0500http://ask.sagemath.org/question/47774/Formal determinant of symbolic matrixhttp://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/ I have some sparse symbolic matrices, and want to compute their formal determinant (without cancellation of terms). In other words, if I have the matrix
x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
I would like the result of
M.determinant()
to be x*y - x*y, rather than just 0. The variables in each monomial are allowed to commute with each other, but on the other hand I would like all monomials containing a 0 to vanish (i.e if in the example above M = Matrix(SR, [[x,0],[x,y]])), then the determinant should be just x*y, rather than x*y - 0*x).
Is there a way to achieve this (without using the expansion of the determinant as permutations, since the dimension of the matrices gets quite big!)? Thanks in advance!
danieleCWed, 22 May 2019 09:32:20 -0500http://ask.sagemath.org/question/46624/Hurwitz determinantshttp://ask.sagemath.org/question/9433/hurwitz-determinants/Dear all,
let p(z)=z^n+a_1*z^{n-1}+...+a_n be a polynomial where n is a positive integer and a_1,a_2,..., a_n are real numbers. Then the so-called **Hurwitz determinants** of order k=1,2,...,n of p are defined as det(a_{2i-j}) , 1 \leq i,j \leq k where a_0=1 and a_l=0 for l<0 or l>k.
Is there a routine implemented in Sage to compute these determinants (numerically)?
Thanks a lot in advance.HacksteinTue, 16 Oct 2012 02:36:20 -0500http://ask.sagemath.org/question/9433/Given a value and conditions, find a matrix with that value as its determinanthttp://ask.sagemath.org/question/45527/given-a-value-and-conditions-find-a-matrix-with-that-value-as-its-determinant/ I'm not sure if the title is named too obscurely, so I'll give an example of what I'm talking.
Say we have an integer matrix:
M= 0 a
b 0 (sorry for weird formatting; cannot seem to write matrices properly on here - any help with that as well? :P )
I want to find all values $(a,b) \in (-2,-1,0,1,2)$ such that $det(M)=2$.
I've tried the following but I'm not sure how to finish it off / if this is the right way to go:
import itertools
for (a,b) in itertools.product([-2,-1,0,1,2], repeat=2):
M = Matrix(ZZ, [[0,a],[b,0]])
if M.determinant()==2:
min_params.append((a,b))
elif
min_params = [(a,b)]
print 'Sets of parameters with this determinant:', min_params
As I say, I feel like the start would be a good way to go, but I'm stuck when reaching `elif`, and I'm not sure if this is the right way to tackle this anyway!
Any help would be great!LukeC93Thu, 21 Feb 2019 06:01:41 -0600http://ask.sagemath.org/question/45527/Minimal determinant of a matrix with varied entrieshttp://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/I would like Sage to tell me to minimal value of the determinant of a matrix when a vary some entries over a set range.
For example, say with the 4x4 matrix A (sorry for weird layout, I can't get the Latex code to work properly?):
0 1 i j
1 0 1 k
0 1 0 1
0 0 1 0
I'd like to know, for $\{i,j,k} \in \{0, \pm1\}$, what the smallest non-trivial determinant is, and for which combination(s) of $i,j,k$ this is for.
I'm still pretty new to Sage, so I'm a bit unsure of how to do this effectively, if it is at all possible to do this?
Side note: I know I could of course write
sage A.determinant()
and this would give me the value of the determinant in terms of $i,j,k$. And in this example that would likely be much easier.
The idea is that I'd like to do this with larger matrices (that aren't as nice as this), more variables, etc. It would obviously be nicer to have have Sage simply compute the smallest determinant and give the corresponding values for the variables than me have to plug it in.LukeC93Fri, 08 Feb 2019 04:05:15 -0600http://ask.sagemath.org/question/45367/How to find all the sub-square matrices of an 4x4 matrix and all their determinantshttp://ask.sagemath.org/question/41136/how-to-find-all-the-sub-square-matrices-of-an-4x4-matrix-and-all-their-determinants/ 1. I need to find all the sub-square matrices of a $4 \times 4$ matrix.
2. Then find the determinant of all $70$ of them.
I was directed to Sage but I know little about it. A simple program would be much appreciated.
For example, the matrix below:
$$2\quad 3\quad 1\quad 1$$
$$1\quad 2\quad 3\quad 1$$
$$1\quad 1\quad 2\quad 3$$
$$3\quad 1\quad 1\quad 2$$
Thanks to anyone who can help. Redbook1Thu, 15 Feb 2018 13:24:16 -0600http://ask.sagemath.org/question/41136/random matrix with determinant +- 1http://ask.sagemath.org/question/40912/random-matrix-with-determinant-1/ I want to generate a random 4x4 matrix with integer entries and determinant either 1 or -1. I know that you can use
`random_matrix(ZZ,4,4, algorithm = 'unimodular')`
to generate matrices with determinant 1 (so in the special linear group). However, I'm actually more interested in the matrices with determinant -1.
Is there a 'Sage' way to do this? Or are there other functions/routines out there I should look at?
Thanks!
Daniel LWed, 31 Jan 2018 22:15:26 -0600http://ask.sagemath.org/question/40912/generation of certain matriceshttp://ask.sagemath.org/question/37860/generation-of-certain-matrices/I'd like to create a list of roughly 100-1000 $2 \times 2$ matrices $[A_1,A_2,...,A_N]$ that have the following properties:
1. $\det A_j = 1$
2. The entries of each $A_j$ are in an imaginary quadratic integer ring, such as $\mathbb{Z}[i]$, or $\mathbb{Z}[\sqrt{-2}]$
For example, the matrix
$$\begin{bmatrix}
1&2i \\\\
0&1
\end{bmatrix}$$
fits the above specifications when the ring is $\mathbb{Z}[i]$.
I know that I probably want to run some kind of loop over the entries of the matrix, but I'm not sure how to do this. Perhaps I want to initially treat the matrices as lists of length 4, and then run an iterative loop over the lists. Then, when the above specifications are met, that list is stored somewhere else. I think I'd also like to put a bound on the "size" of the matrix entries, but that should be easy to do afterwards.
Thanks! Daniel LThu, 08 Jun 2017 12:26:31 -0500http://ask.sagemath.org/question/37860/Compute the determinant of a symbolic 5x5 matrixhttp://ask.sagemath.org/question/26489/compute-the-determinant-of-a-symbolic-5x5-matrix/why does the following script fail to compute the determinant of a 5x5 matrix (same problem appears with larger similar matrices 6x6 and 7x7 matrix too) ?
version();
var ('a,b,c,d,e,f,g,h');
A4 = matrix(SR,4,4,[a,b,c,d,a,a,b,c,a,a,a,b,h,a,a,a]);
A5 = matrix(SR,5,5,[a,b,c,d,e,a,a,b,c,d,a,a,a,b,c,a,a,a,a,b,h,a,a,a,a]);
A4; A4.determinant().expand().factor();
A5; A5.determinant();
A5.determinant().expand().factor();
a spurious "_e" appears in the determinant
'Sage Version 6.5, Release Date: 2015-02-17'
(a, b, c, d, e, f, g, h)
[a b c d]
[a a b c]
[a a a b]
[h a a a]
a^4 - 3*a^3*b + 3*a^2*b^2 - 2*a^2*b*c + a^2*c^2 + a^3*d - a^2*b*d - b^3*h + 2*a*b*c*h - a*c^2*h - a^2*d*h + a*b*d*h
[a b c d e]
[a a b c d]
[a a a b c]
[a a a a b]
[h a a a a]
_e*a^4 + a^5 - 2*_e*a^3*b - 4*a^4*b + _e*a^2*b^2 + 6*a^3*b^2 - 4*a^2*b^3 + 3*a^2*b^2*c - a^3*c^2 - 2*a^2*b*c^2 + a^2*c^3 - 2*a^3*b*d + 2*a^2*b^2*d + 2*a^3*c*d - 2*a^2*b*c*d - _e*a^3*h + 2*_e*a^2*b*h - _e*a*b^2*h + b^4*h - 3*a*b^2*c*h + a^2*c^2*h + 2*a*b*c^2*h - a*c^3*h + 2*a^2*b*d*h - 2*a*b^2*d*h - 2*a^2*c*d*h + 2*a*b*c*d*h
Error in lines 13-19
Traceback (most recent call last):
File "/projects/cbc78de9-848d-4653-bf96-aa8a68749e86/.sagemathcloud/sage_server.py", line 879, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "sage/symbolic/expression.pyx", line 9266, in sage.symbolic.expression.Expression.factor (build/cythonized/sage/symbolic/expression.cpp:45045)
f = self.polynomial(QQ)
File "sage/symbolic/expression.pyx", line 5716, in sage.symbolic.expression.Expression.polynomial (build/cythonized/sage/symbolic/expression.cpp:31627)
return polynomial(self, base_ring=base_ring, ring=ring)
File "/usr/local/sage/sage-6.5/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1163, in polynomial
converter = PolynomialConverter(ex, base_ring=base_ring, ring=ring)
File "/usr/local/sage/sage-6.5/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 999, in __init__
self.ring = PolynomialRing(self.base_ring, names=vars)
File "/usr/local/sage/sage-6.5/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_ring_constructor.py", line 477, in PolynomialRing
R = _multi_variate(base_ring, names, n, sparse, order, implementation)
File "/usr/local/sage/sage-6.5/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_ring_constructor.py", line 564, in _multi_variate
names = normalize_names(n, names)
File "sage/structure/parent_gens.pyx", line 207, in sage.structure.parent_gens.normalize_names (build/cythonized/sage/structure/parent_gens.c:2797)
names = _certify_names(names)
File "sage/structure/parent_gens.pyx", line 148, in sage.structure.parent_gens._certify_names (build/cythonized/sage/structure/parent_gens.c:2291)
raise ValueError, "first letter of variable name must be a letter"
ValueError: first letter of variable name must be a letter
PierreFri, 10 Apr 2015 16:38:04 -0500http://ask.sagemath.org/question/26489/Large symbolic determinanthttp://ask.sagemath.org/question/8021/large-symbolic-determinant/I have a dense matrix with symbolic variable entries. I am interested in the equation that describes when the matrix is singular. So I have been having Sage compute the determinant of this matrix and set it equal to zero, and return this expression (a polynomial in several variables).
However when the dimensions get larger (around 9 variables) Sage is taking an incredibly long amount of time to do it. (My program has to perform a number of other tasks as well, the determinant seems to be taking up all the run-time.)
Is there any way I can speed up this kind of computation?David FerroneThu, 24 Mar 2011 17:06:25 -0500http://ask.sagemath.org/question/8021/How to stop Sage from finding erroneous complex roots?http://ask.sagemath.org/question/25587/how-to-stop-sage-from-finding-erroneous-complex-roots/ Consider the matrix,
A = matrix ( [0,1,w^a,1],[1,0,1,w^(k-b)],[w^(k-a),1,0,w^(k-c)],[1,w^b,w^c,0])
where w is the m^th of the k^th roots of unity, `w = exp((2*pi*I*m )/k`
for some k a positive integer, and 1 <= m <= (k-1)
and 1<= a,b,c <= (k-1)
Then the characteristic polynomial of the above matrix is,
`p(x) = x^4 - 6*x^2 -x *(w^(a-c) + w^(c-a) + w^b + w^(-b) + w^(b-c) + w^(c-b) + w^a + w^(-a)) + (3 -w^c - w^(-c) - w^(a+b-c) - w^(-a-b+c) - w^(a-b) - w^(-a+b) )`
- Is there a way to get sage to be able to calculate the above characteristic polynomial?
Now I try getting roots of the above by doing,
g(x)=real_part(p(x)).simplify()
g.solve(x)
- Now Sage seems to be generically detecting complex eigenvalues as roots of g!
(I tried on say k=6, m=a=b=c=1)
This can't happen since its a characteristic polynomial of a Hermitian matrix!
How to get across this trouble?
PhoenixTue, 20 Jan 2015 23:06:19 -0600http://ask.sagemath.org/question/25587/Defining differential operator that acts like curlhttp://ask.sagemath.org/question/25367/defining-differential-operator-that-acts-like-curl/ Hello,
As a newbie in using SAGE (experience with Python and Numpy), I was wondering how to define a differential operator that acts like curl (or $\nabla \times \vec F$). The curl for a vector field $\vec F=(F_x,F_y, F_z)$is defined as a determinant $\mathrm{det} (\nabla,\vec F) $. I want to define such operators that act on $F_x, F_y, F_z$ **without calculating the determinant a priori**. In other words if I was to multiply $\frac {\partial }{\partial x}$ with $F_y$, I would expect to get $\frac{\partial F_y}{\partial x}$
EDIT:
class DiffOpp(SageObject):
def __init__(self, dep_var):
self.dep_var = dep_var
def __mul__(self, f):
return diff(f, self.dep_var)Marios PapachristouTue, 30 Dec 2014 07:45:21 -0600http://ask.sagemath.org/question/25367/Determinant of block matriceshttp://ask.sagemath.org/question/10766/determinant-of-block-matrices/I need to calculate the determinant of a symbolic 4x4 matrix M, where each cell itself is a matrix itself.
Is that possible?
I did a beginner's check, and it did not seem to work:
mq=matrix(SR, 2, 2, 'a b c d'.split(' '))
MQ=matrix(SR, 2, 2, [mq, mq, mq, mq])
MQ.det()
Traceback (click to the left of this block for traceback)
...
TypeError: mutable matrices are unhashable
kautschukFri, 22 Nov 2013 19:45:41 -0600http://ask.sagemath.org/question/10766/