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, 14 Oct 2021 20:52:10 +0200How to find orthogonal subspace of a subspacehttps://ask.sagemath.org/question/59357/how-to-find-orthogonal-subspace-of-a-subspace/I have written a code:
> n=4
V = VectorSpace(GF(2),n)
C=[[0,0,1,0], [1,1,1,0]]
U = V.subspace(C)
How to find orthogonal subspace of U? SanuThu, 14 Oct 2021 20:52:10 +0200https://ask.sagemath.org/question/59357/Express a vector field in terms of a framehttps://ask.sagemath.org/question/58073/express-a-vector-field-in-terms-of-a-frame/I have a set of 4 vector fields that form a frame, I have defined them as
N = Manifold(4, 'N')
Y.<x,y,k,t> = N.chart()
F = Y.frame()
X_1 = cos(k)*F[0] + sin(k)*F[1] + (-y/2*cos(k) + x/2*sin(k))*F[3]
X_2 = -sin(k)*F[0] + cos(k)*F[1] + (x/2*cos(k) + y/2*sin(k))*F[3]
X_3 = F[3]
b = var('b', domain='real')
assume(2 > b, b >= 0)
X_4 = F[2] - b*F[3]
Now I want to calculate their Lie bracket and express it in terms
of these vectors fields, for example the following gives `[X_2, X_4]`,
and I would like that written in terms of the `{X_1, X_2, X_3, X_4}`,
in this case the output should be `X_1`.
vw = X_2.bracket(X_4)
vw
vw.display()
In general, I would like to know, given a vector field `V`
in the original frame, how it can be expressed
in terms of `{X_1, X_2, X_3, X_4}`. Many thanks.raulWed, 21 Jul 2021 16:08:38 +0200https://ask.sagemath.org/question/58073/Find binary solution over non square matrixhttps://ask.sagemath.org/question/57894/find-binary-solution-over-non-square-matrix/ I have system of m homogeneous linear equations of n variables over GF(2) where m>n.
We know all zero is a solution. How to find any other solution? I know
there are other solutions as rank of the corresponding matrix < n.
SanuTue, 06 Jul 2021 20:53:51 +0200https://ask.sagemath.org/question/57894/Consider the set of all symmetric matrices of a given size $n$ with entries lying in $\{0,1\}$ such that all diagonal entries are zeros in the matrices.https://ask.sagemath.org/question/57844/consider-the-set-of-all-symmetric-matrices-of-a-given-size-n-with-entries-lying-in-01-such-that-all-diagonal-entries-are-zeros-in-the-matrices/Consider the set of all symmetric matrices of a given size $n$ with entries lying in {0,1} such that all diagonal entries are zeros in the matrices. Now how to write an algorithm that finds at least one matrix (from the set we have considered) which is similar to its inverse matrix via a permutation matrix.
For computation part, we can choose $n$ according to our convenience.
Please help regarding this. Thank yourewiThu, 01 Jul 2021 20:02:46 +0200https://ask.sagemath.org/question/57844/Is it possible for the spectrum() method to use all CPU cores?https://ask.sagemath.org/question/57824/is-it-possible-for-the-spectrum-method-to-use-all-cpu-cores/I need to compute the Laplacian spectrum of a ton of graphs and was wondering if it's possible to use all CPU cores instead of only one.lisandraspWed, 30 Jun 2021 18:41:11 +0200https://ask.sagemath.org/question/57824/How to construct a class of matrices satisfying a given matrix equation.https://ask.sagemath.org/question/56490/how-to-construct-a-class-of-matrices-satisfying-a-given-matrix-equation/![image description](/upfiles/16175450459866355.png)
Here we know that $A=I_n$ satisfies the given matrix equation. But can we find other non trivial matrix ($\neq I_n$). In other words, can we construct a class of matrices satisfying the given matrix equation.
Please help regarding this.rewiSun, 04 Apr 2021 16:06:55 +0200https://ask.sagemath.org/question/56490/Solve matrix equation in sympyhttps://ask.sagemath.org/question/55263/solve-matrix-equation-in-sympy/How can I solve Q*v=0 for v in matrix terms (Q is a matrix, v is a vector). v should be a zero vector. The matrix Q is supposed to be invertible and full rank.
from sympy.solvers import solve
from sympy import symbols
from sympy import MatrixSymbol
n=symbols('n')
v = MatrixSymbol('v', n, 1)
Q = MatrixSymbol('Q', n, n)
eq=Q*v
solve(eq,v)
Please help, thanks.Tintin1Wed, 13 Jan 2021 21:38:17 +0100https://ask.sagemath.org/question/55263/Coordinates in a free submodulehttps://ask.sagemath.org/question/54715/coordinates-in-a-free-submodule/I am working with free ℤ-modules that are presented a submodules of ℤ^n, for example:
A = Matrix([[1, 1, 1]])
V = A.right_kernel()
When creating this ℤ-module Sage computes a basis
(in this case `[(1, 0, -1), (0, 1, -1)])`.
I can also create elements of `V` as follows :
v = V([2, -1, -1])
but then I could not find a way to get the coordinates of `v`
in the basis of `V`.
Is there a function somewhere in Sage that does this?jraimbauWed, 16 Dec 2020 18:06:25 +0100https://ask.sagemath.org/question/54715/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/Solver returns empty solution for linear system (but it has a solution)https://ask.sagemath.org/question/54398/solver-returns-empty-solution-for-linear-system-but-it-has-a-solution/I have three n by n matrices : `m_want` , `m_l` and`m_r`
In the below examples, clearly `m_want = m_l * m_r`, however if I replace any of the value of either matrix by a variable and try to solve for this variable :
solution = solve([system[i][j]==want[i][j] for j in range(n) for i in range(n)], variables)
then the solution is empty. Does anyone knows why it behaves like this?
m_l = Matrix([[1, 1, 0, 0, 0, 0, 0, 0],
[0.7071067811865475, -0.7071067811865475, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0.4142135623730962, -2.4142135623730976, 0, 0, 0, 0],
[0, 0, 0, 0, 1.0, 1.0, 0, -0.7071067811865483],
[0, 0, 0, 0, 1.414213562373096, -0.7071067811865457, -1, 0],
[0, 0, 0, 0, 0, -0.7071067811865472, 1.0, 1.0],
[0, 0, 0, 0, -1, 0, 1.4142135623730945, -0.7071067811865468]]
)
m_r = matrix([
[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0],
[0.9238795325112865, 0.0, -0.9238795325112865, 0.0, 0.9238795325112865, 0.0, -0.9238795325112865, 0.0],
[0.0, -0.38268343236509034, 0.0, 0.38268343236509034, 0.0, -0.38268343236509034, 0.0, 0.38268343236509034],
[0.9807852804032295, 0, 0.19509032201612828, 0, -0.9807852804032295, 0, -0.19509032201612828, 0],
[0, -0.27589937928294467, 0, 1.38703984532215, 0, 0.27589937928294467, 0, -1.38703984532215],
[0.8314696123025446, 0, -0.5555702330196017, 0, -0.8314696123025446, 0, 0.5555702330196017, 0],
[0, -1.175875602419359, 0, 0.7856949583871042, 0, 1.175875602419359, 0, -0.7856949583871042]]
)
want = matrix([
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[0.7071067811865475, -0.7071067811865475, 0.7071067811865475, -0.7071067811865475, 0.7071067811865475, -0.7071067811865475, 0.7071067811865475, -0.7071067811865475],
[0.9238795325112865, -0.38268343236509034, -0.9238795325112865, 0.38268343236509034, 0.9238795325112865, -0.38268343236509034, -0.9238795325112865, 0.38268343236509034],
[0.38268343236508906, 0.9238795325112872, -0.38268343236508906, -0.9238795325112872, 0.38268343236508906, 0.9238795325112872, -0.38268343236508906, -0.9238795325112872],
[0.9807852804032303, 0.5555702330196018, 0.1950903220161283, 0.8314696123025453, -0.9807852804032303, -0.5555702330196018, -0.1950903220161283, -0.8314696123025453],
[0.5555702330196017, 0.19509032201612886, 0.8314696123025453, -0.9807852804032305, -0.5555702330196017, -0.19509032201612886, -0.8314696123025453, 0.9807852804032305],
[0.8314696123025446, -0.9807852804032307, -0.5555702330196028, -0.19509032201612753, -0.8314696123025446, 0.9807852804032307, 0.5555702330196028, 0.19509032201612753],
[0.19509032201612703, 0.8314696123025446, -0.9807852804032307, -0.5555702330196031, -0.19509032201612703, -0.8314696123025446, 0.9807852804032307, 0.5555702330196031]])
Pro7echThu, 26 Nov 2020 18:27:09 +0100https://ask.sagemath.org/question/54398/Sage solver : approximate solutionhttps://ask.sagemath.org/question/54399/sage-solver-approximate-solution/ I have the simple following equation :
res = solve([1.00000000000000*x8 == 0.923879532511287,0.414213562373096*x8 == 0.382683432365089],x8,solution_dict=True)
However, the solver will return an empty solution because the value do not quite exactly match (there is a 1e-15 difference that is due to numerical errors of the 53 bits mantissa precision). Is there a way to ask the solver to get an approximate solution instead?Pro7echThu, 26 Nov 2020 18:50:37 +0100https://ask.sagemath.org/question/54399/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/Echelon form in cythonhttps://ask.sagemath.org/question/53685/echelon-form-in-cython/Is there an already function implemented in Cython which computes the echelon form of an integer matrix?
Thanks for the help.1571Thu, 01 Oct 2020 12:36:01 +0200https://ask.sagemath.org/question/53685/solving a matrix equation in the integers.https://ask.sagemath.org/question/53262/solving-a-matrix-equation-in-the-integers/ I'm trying to use sage to solve an expression of the form $xS = y$ for a fixed integer matrix $S$ and a fixed integer vector $y$. I need my solution vector $x$ to also have integer coefficients.
How I'm currently doing this is by using the mixed integer linear programming tool and my code looks something like this.
p = MixedIntegerLinearProgram(maximization = True, solver = "GLPK")
x = p.new_variable(integer = True, nonnegative = True)
p.add_constraint( x*S == y) #here the vector can be anything
p.add_constraint( x[25] <= 2020 )
p.set_objective( x[25] )
p.solve()
p.get_values(x)
The issue I have here is that the objective and the constraint are arbitrary and were just picked so that the space that the MixedIntegerLinearProgram function is trying to optimize over has a "feasable solution" (sage's words not mine). Is there a better way to find a solution to $xS = y$ in the integers (i.e a different function or package supported by sage) and if not, is there a way to have my constarint be to make the norm of the vector as small as possible (so that I'm not just artificially picking an entry to optimize my search over)?
JRHalesWed, 02 Sep 2020 18:10:13 +0200https://ask.sagemath.org/question/53262/Differentiating function with fluctuating number of variableshttps://ask.sagemath.org/question/52075/differentiating-function-with-fluctuating-number-of-variables/(Edit: I've changed the question somewhat - upon editing the code the problem seems to lie elsewhere.)
Let's say I have a vector space V of dimension n (which is variable) and a matrix M (also depending on n and other input), and I want to understand the derivative of the function v -> ||M*v|| at some vector v in V, and then evaluate it at tangent vectors.
As far as I can tell, the easiest way to do this is to use a symbolic vector v, then calculate ||M*v||, then take diff(), and then I can plug in a tangent vector.
So I would write something like
v = list(var('v_%d' % i) for i in range(1,n+1))
def f(*arg):
L = []
for var in arg:
L.append(var)
return (M*vector(L)).norm()
(which is clearly bad and going nowhere) but attempting something like this, diff(f) throws an error:
unable to convert <function f at 0x7f2b046c5b90> to a symbolic expression
Trying
f(*v) = (M*vector(v)).norm()
doesn't work either.bksadieFri, 19 Jun 2020 02:08:35 +0200https://ask.sagemath.org/question/52075/What does "hidden_function keyword" mean?https://ask.sagemath.org/question/52119/what-does-hidden_function-keyword-mean/Hello, everyone. I am very new to Sage, so I am still getting used to it and learning the terminology. Recently, I was trying to test Sage out and I entered `vector(matrix([[]]))` into the Sage terminal prompt, but it gave me an error message I did not understand:
sage: vector(matrix([[]]))
sage: vector(matrix([[]]))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-9573ec2e6342> in <module>()
----> 1 vector(matrix([[]]))
sage/modules/free_module_element.pyx in sage.modules.free_module_element.vector (build/cythonized/sage/modules/free_module_element.c:4462)()
/usr/lib/python2.7/dist-packages/sage/categories/finite_dimensional_modules_with_basis.pyc in _vector_(self, order)
385 # We slightly break encapsulation for speed reasons
386 return dense_free_module.element_class(dense_free_module,
--> 387 self.dense_coefficient_list(order),
388 coerce=True, copy=False)
389
/usr/lib/python2.7/dist-packages/sage/categories/finite_dimensional_modules_with_basis.pyc in dense_coefficient_list(self, order)
364 if order is None:
365 try:
--> 366 order = sorted(self.parent().basis().keys())
367 except AttributeError: # Not a family, assume it is list-like
368 order = range(self.parent().dimension())
/usr/lib/python2.7/dist-packages/sage/matrix/matrix_space.pyc in basis(self)
1187 return Family(keys, v.__getitem__,
1188 hidden_keys=list(range(self.dimension())),
-> 1189 hidden_function=old_index)
1190
1191 def dimension(self):
/usr/lib/python2.7/dist-packages/sage/sets/family.pyc in Family(indices, function, hidden_keys, hidden_function, lazy, name)
379 if hidden_keys == []:
380 if hidden_function is not None:
--> 381 raise ValueError("hidden_function keyword only makes sense "
382 "together with hidden_keys keyword !")
383 if function is None:
ValueError: hidden_function keyword only makes sense together with hidden_keys keyword !
When I originally ran this line of code, I was expecting it to just output the empty vector, $()$. However, instead, it gave me this cryptic error message about `hidden_function` and `hidden_keys`. Can someone please explain to me what this error message means? I can't find anything about this error message on Google. Thanks!Noble MushtakSat, 20 Jun 2020 03:14:27 +0200https://ask.sagemath.org/question/52119/Expressing a list of vectors in terms of basis elementshttps://ask.sagemath.org/question/52063/expressing-a-list-of-vectors-in-terms-of-basis-elements/Let's assume I have a list for my basis [a,b,c,d,e]=BasisList. We can think of a,b,c,d,e as standard basis vectors with a being (1,0,0,0,0) for example.
I also have another list of many vectors each with 5 entries. For example, ExampleList=[(1,2,3,4,5), (2,3,4,1,8),...]. Is there a fast way to express each vector in terms of my basis instead of mulitplying term by term and summing them up. In the example above, I want to end up with the a list
EndList=[a+2b+3c+4d+5e, 2a+3b+4c+d+8e,...]
I can of course make a loop and do this summing+multiplying but it gets very long and sometimes redundant if my vector has zeros. I was thinking if there is a way to tell Sage to let a be (1,0,0,0,0), b be (0,1,0,0,0), etc and just do 1 for loop with command like "for element in ExampleList, express with basis given."whatupmattThu, 18 Jun 2020 05:21:19 +0200https://ask.sagemath.org/question/52063/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/Hypermatrices?https://ask.sagemath.org/question/51583/hypermatrices/ Hi all,
I want to store and manipulate arrays of $n\times n\times n$ numbers
$$x_{ijk}\quad\text{where}\quad 0\leq i, j, k\leq n-1$$
with Sage.
Here is what I have been doing so far:
`import numpy as np`
`n = 7`
`X = np.zeros((n, n, n))`
`X[2][4][1] = 17`
etc...
Remark: So $X$ is a so called 3-hypermatrix.
**Question:** Are NumPy arrays my best option when handling hypermatrices with Sage?
**Precision**: I will need to manipulate lists of such hypermatrices, and even hypermatrices whose elements will be hypermatrices, e.g.
$$X_{ijk}^{(abcd)}$$
and loop over such lists, e.g. for a ..., for b ..., for c ..., do ... with the 3-hypermatrix $X^{(abcd)}$.BostonWed, 27 May 2020 15:54:24 +0200https://ask.sagemath.org/question/51583/Basis for such a quotient vector spacehttps://ask.sagemath.org/question/51550/basis-for-such-a-quotient-vector-space/Hi all,
Here is my code:
`A = matrix(QQ, 4, 3, [1, 2, 3, 5, 10, 15, 0, 0, 0, -2, -4, -6])`
`B = matrix(QQ, 5, 4, [0, 2, 7, 5, 0, 0, 0, 0, 0, -2, -7, -5, 0, 4, 14, 10, 0, 6, 21, 15])`
`W = A.transpose().image()`
`V = B.right_kernel()`
`V/W`
This way W is the range of the linear map
$$A:\mathbb{Q}^3\longrightarrow\mathbb{Q}^4,$$
V is the nullspace of
$$B:\mathbb{Q}^4\longrightarrow\mathbb{Q}^5,$$
Since BA=0, W is a subspace of V.
and the command V/W returns in particular the dimension of the quotient vector space V/W which is equal to 2.
What I want is a basis of V/W, i.e. 2 vectors of $\mathbb{Q}^4$ which are in V,
in the form of a 2x4 matrix
the same way V.basis_matrix() returns a basis for V.
How can I do this?
Thanks!BostonMon, 25 May 2020 19:26:58 +0200https://ask.sagemath.org/question/51550/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/Compute dimension of vector subspacehttps://ask.sagemath.org/question/50626/compute-dimension-of-vector-subspace/I have a reasonably looking number field K of degree 20 (by reasonably looking I mean that the defining polynomial has coefficients < 1000). I have a stream that produces me reasonably looking vectors in K^30, that I call v1, v2, .... For each n I want to compute the dimension of V_n := span (v1, ..., vn).
If I go through (K^30).subspace([...]) then Sage runs the echelon_form and produces me matrices with coefficients of size 10^(100000).
Here is an example showing the issue
sage: x = polygen(QQ, 'x')
sage: K = NumberField(x^20 - 3, 'a')
sage: V = K**20
sage: U = V.subspace([])
....: for _ in range(20):
....: U += V.subspace([V.random_element()])
....: s = max(max(abs(coeff.numerator()) + abs(coeff.denominator()) for coeff in z.polynomial()) for z in U.matrix().list() if z)
....: print("coeff size: %d" % len(str(s)))
....: print("space dim: %d" % U.dimension())
....:
coeff size: 131
space dim: 1
coeff size: 301
space dim: 2
coeff size: 487
space dim: 3
coeff size: 640
space dim: 4
coeff size: 877
space dim: 5
coeff size: 1141
space dim: 6
coeff size: 1501
space dim: 7
coeff size: 1959
space dim: 8
Alternatively, you can consider the non streamed version
sage: K = NumberField(x^20 - 3, 'a')
sage: M = MatrixSpace(K, 20)
sage: m = M.random_element()
sage: m.rank()
This is not practical at all and basically stops around dim 15. Is there any alternative?
Note that for problems of the same size over rational numbers, Sage does succeed
sage: M = MatrixSpace(QQ, 2000)
sage: m = M.random_element()
sage: m[1400] = m[0] - 30 * m[18] + 25 * m[1500]
sage: %time m.rank()
CPU times: user 22.6 s, sys: 149 ms, total: 22.8 s
Wall time: 22.8 s
1999
(if the matrix is full rank the problem is way easier)
See also: [this thread on sage-devel](https://groups.google.com/forum/#!topic/sage-devel/vUJMpEqfXz0).vdelecroixTue, 07 Apr 2020 17:47:33 +0200https://ask.sagemath.org/question/50626/Error while creating augmented matrix: TypeError: unable to convert a to an integerhttps://ask.sagemath.org/question/49882/error-while-creating-augmented-matrix-typeerror-unable-to-convert-a-to-an-integer/ I am trying to create augmented matrix containing variables `a` and `b`, using the following code:
sage:
sage: u = Matrix([
....: [1, -1],
....: [2, 1]
....: ])
sage:
sage: var('a, b')
(a, b)
sage:
sage: w = vector([ a, b])
sage:
sage: u.augment(w, subdivide=True)
The last line, `u.augment(w, subdivide=True)`, is giving me `TypeError: unable to convert a to an integer`.ggWed, 12 Feb 2020 07:33:26 +0100https://ask.sagemath.org/question/49882/A linear algebra solve found zero solutions, weirdlyhttps://ask.sagemath.org/question/49698/a-linear-algebra-solve-found-zero-solutions-weirdly/Let `E`, `P`, `QA` be points of a 3D space, let `a*QAx+b*QAy+c*QAz==d` ensures that `QA` is in a certain plane. Let |E P| and |E QA| be the same line. Why did my attempt to find QA found zero solutions?
#!/usr/bin/env python
# coding: utf-8
# In[1]:
var('a b c d')
# In[2]:
var('Ex Ey Ez')
# In[3]:
E=vector([Ex,Ey,Ez])
# In[4]:
var('Px Py Pz')
# In[5]:
P=vector([Px,Py,Pz])
# In[6]:
var('k QAx QAy QAz')
# In[7]:
QA=vector([QAx,QAy,QAz])
# In[14]:
solve([E-P==k*(E-QA), a*QAx+b*QAy+c*QAz==d], [k, QAx, QAy, QAz])portonTue, 28 Jan 2020 08:42:04 +0100https://ask.sagemath.org/question/49698/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/Incorrect Eigenvalues/Eigenvectorshttps://ask.sagemath.org/question/47313/incorrect-eigenvalueseigenvectors/ I'm trying to compute eigenvalues and eigenvectors for certain matrices. Most of the time sage is giving me what seems to be the correct output, but sometimes it's quite wrong and I'm not sure why this is happening. Here's a concrete example with a circulant matrix.
sage: M=matrix.circulant([1,-1/2*I,0,0,0,0,0,0,0,0,1/2*I])
sage: M.eigenvectors_right()
[(1, [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)], 1)]
That is, it only returns a single eigenvector when I know there's in fact 11. A different issue appears with matrix.circulant([1,-1/2*I,0,0,0,0,1/2*I]), where now sage produces all the eigenvalues but produces no eigenvectors for most of these values.
zorkkoiTue, 30 Jul 2019 17:20:19 +0200https://ask.sagemath.org/question/47313/Linear Algebra tutorial with sage commandhttps://ask.sagemath.org/question/32295/linear-algebra-tutorial-with-sage-command/ Does anybody know a good tutorial for Linear Algebra with sage commands. I am looking for "only" Linear Algebra maybe numerical Linear Algebra with sage. Right know I am working with matlab. But i dont like it. Is there also a way to send sage code to sage math clound via terminal.
I am using sublime text editor and there is https://github.com/wuub/SublimeREPL
It run the code right inside it. Maybe sagemath ("without" the cloud) :)thethaWed, 20 Jan 2016 10:46:44 +0100https://ask.sagemath.org/question/32295/Extremal Rays of a Conehttps://ask.sagemath.org/question/47275/extremal-rays-of-a-cone/Can I use Sage to compute the subspace of a vector space that lies in non-negative real Euclidean space?
For example,
If I compute the nullspace of the matrix,
A =
[ [ 1 0 0 -1 0]
[-1 1 0 0 1]
[ 0 -1 1 0 0]
[ 0 0 -1 1 -1] ]
I get,
[1,1,1,1,0],[0,−1,−1,0,1]
as basis. Now I would like to restrict the solution space to only vectors with non-negative entries. I believe the two basis vectors I am looking for are,
[1,1,1,1,0],[1,0,0,1,1].
My program is written in Python, but I am not sure there is anything written for this sort of problem. Using the simplex method of linear programming (objective function set to zero) only spits out a single solution vector which seems to be the sum of the two I want. I ask here because I see some classes for cones and methods that gives the extremal rays, but it seems I already have to know the extremal rays to use that class.ElGalloNegroSun, 28 Jul 2019 00:36:16 +0200https://ask.sagemath.org/question/47275/On finding to find a minimum number of switch presses to shut down all lampshttps://ask.sagemath.org/question/44656/on-finding-to-find-a-minimum-number-of-switch-presses-to-shut-down-all-lamps/This is a follow up to https://ask.sagemath.org/question/39335/how-to-find-a-minimum-number-of-switch-presses-to-shut-down-all-lamps/
There is a problem on the
https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=muslam :
The following is an example case.
I have $n=120$ lamps in a circle, and enumerated by $L_1,\ldots,L_{120}$. Some of them are switched on and some of them are switched off. I also have been given a positive integer $m=7.$ One every turn I choose one lamp $L_i$ and then the lamps $L_{i-m},\ldots,L_{i+m}$ will change their state, I mean if lamp $L_j$ was turned off then now it is turned on and vice versa. Indexes are modulo $n$ so the lamps $L_{118}, L_{119}, L_1,L_2$ are consecutive.
What is the minimum number of turns to shut off all lamps and which switches one must press, if the initial states of the lamps are (from $L_1$ to $L_{120}$)
1010110110000100000101011001011111010111
1010011101001100000010001010011010110000
0000100110010100010010110111000000010110
where $1$ means that the corresponding lamp is on at the beginning and $0$ means that the corresponding lamp is off at the beginning? All cases are listed as below:
label, n.o. lamps, how many lamps original lamp states
a switch affects
per direction
================================================================================
B 6 1 101101
--------------------------------------------------------------------------------
C 10 2 1011010110
--------------------------------------------------------------------------------
D 20 1 11111011101010111111
--------------------------------------------------------------------------------
E 30 7 011100001010011011100001010011
--------------------------------------------------------------------------------
F 39 6 110100111111101000011000100110111100010
--------------------------------------------------------------------------------
G 53 9 0101100101111100100011100111101001001010
0010000010110
--------------------------------------------------------------------------------
H 120 7 1010110110000100000101011001011111010111
1010011101001100000010001010011010110000
0000100110010100010010110111000000010110
--------------------------------------------------------------------------------
I 220 27 1110111111101000100110011001100110100000
0010100011000111101100111111000001010000
1010110110011100100010011011010111100011
0101101000010000100110111101001001011010
1101001001110110001100011010111101001100
11010111110101010100
--------------------------------------------------------------------------------
J 500 87 1010001101101001110001101001000101010100
0001111111001101011000000011001111111011
1001110011010111111011010100010011011001
1001101110011011100001000111110101011111
1100111100001100110011101110101100001111
1100010010011010001111000000101110101101
1010100001100011111000111001000101101000
1011111111101111000000011111010001000000
1110011110111101010010011000000100010100
0011101011010011010110011110111000010010
0111100100011010010110001000011100101001
1110111010001001011001111011111011010110
10101101111011101110
--------------------------------------------------------------------------------
K 1002 83 0010100100100101000000110101111111101011
1101000101111110001110000110110110010101
1110110011011101100110111001110110010011
1101111010110011110101100001101010100011
1110001100011111110100011110100111111100
0011001011100110101100001101000001110010
0110100000100100100000011010000010111100
1110001110011110101001100111101101010000
0101010000011010011110101001001001000000
0011000100011011011001111010001101111000
0100001011010011001010111001111100110001
0011111110101101001100111101110000000000
1101100100000011000010010100010101001000
1100001000101001100110010100001000001101
1101000100001010011000101001101000100010
0011010001011101010100011101001101101100
0111110100110011001111000000001001001001
1001111001011111000010110000110010101000
1011001100111101000101000110000111010100
0010011011010111001101011001111000001011
1110101010101101111011111110100001100110
1000101100110011010000110000011011110011
0010000010000000111101101000001111101111
0100111110010101100011101001111101010000
1111100010011001110111111000101000000101
01
--------------------------------------------------------------------------------
L 2107 108 0111110100011000011111101110010101100011
1001111011101001001110111110001100011001
1001010100101011101101001000010111111111
1001101010111011110100100101000101100011
1110100010010010101110100000111100101000
0111101011111100010010110000100110100100
0100110101110010110011110010101101100111
1110010011000110110111010110010100101110
0111111101110000111001111100100010010001
1010110011000101100111111001011110101110
0111010110111110110101000101100100011000
1011000011011110001111100110100010100101
1101111100110011001110010010001010101111
1000001001000110011110010011011101110100
1011111100110010011000010110010110101010
0110101000011011110001010000010001000110
1001110101001001110110111111010011010111
1111011001000110111001000011101101110001
0000011111101000010101011111011011000011
1111000000011100010011011001011000110101
1101011111100001100010110010110011000000
0001001111100101110100100011011010011100
0000001111010101000111011000110110100001
1010110011100110111010111110110000010000
1000101001111001000110000101010000010111
1011100001000110001100010000001011101110
1001111110100010010000011000100101010101
1001001001110110101000001001001100001011
0011011100011111100111001110101101110001
0111010000010011110110011011000011101001
1111011010010000101111000010000001100110
1001011101001000010101001001011111111011
1000111000100001101100101110100011111100
1011001111101111110110101111101111011111
1001111100110101110101111110010010101101
1111111111000100100111100011101110110100
0100011011001010110100101101000000110010
0010010001001110110100011111100011111101
0100110111101101010101010100110110011011
0001111111000100000111011010101011000010
0011011110110110110100011001101111001000
1000000011110011100111100000001010010011
1000011101111100000101010101010010100101
1010001011010100011011001110110010100000
1000111101111000010111111101010110110111
0110001111100011001110000100100101001111
0000111111100010011001010000010110111000
1000110110001000001100110000001011000010
1000101101110000101100100010101111100011
1000010010111101000010000110011010000001
0010001100001000001100110111110100100111
1001100110001000100101011111001011001111
110001011111001101010101001
================================================================================
From the first link, I was able to solve smaller cases. Like
room = 'B'
input = '101101'
m = 1
F = GF(2)
L_init = vector(F,input)
d = len(L_init)
M = matrix(F,d,d)
for i in range(d):
for j in range(-m,m+1 ):
M[i,(i+j) % d] = 1
I = M.solve_right(L_init)
K = M.right_kernel() ; K.dimension()
lb = [(I+k).nonzero_positions() for k in K]
la = [len((I+k).nonzero_positions()) for k in K]
m = min(la)
# print(m)
notprinted = True
res = ""
for i in range(len(lb)):
if m == la[i] and notprinted:
for j in range(len(lb[i])):
res += str(lb[i][j]+1) + " "
#print(lb[i])
notprinted = False
print(room + ' ' + res)
Gives solution
B 1 3
and
room = 'H'
input = '1010110110000100000101011001011111010111'
input += '1010011101001100000010001010011010110000'
input += '0000100110010100010010110111000000010110'
m = 7
F = GF(2)
L_init = vector(F,input)
d = len(L_init)
M = matrix(F,d,d)
for i in range(d):
for j in range(-m,m+1 ):
M[i,(i+j) % d] = 1
I = M.solve_right(L_init)
K = M.right_kernel() ; K.dimension()
lb = [(I+k).nonzero_positions() for k in K]
la = [len((I+k).nonzero_positions()) for k in K]
m = min(la)
# print(m)
notprinted = True
res = ""
for i in range(len(lb)):
if m == la[i] and notprinted:
for j in range(len(lb[i])):
res += str(lb[i][j]+1) + " "
#print(lb[i])
notprinted = False
print(room + ' ' + res)
gives
H 1 3 11 12 14 16 17 18 21 22 26 28 30 32 35 41 42 44 47 49 51 53 58 59 60 64 66 69 72 77 80 82 87 93 99 100 101 105 106 109 110 112 113 114 115 120
But the similar approach seems to be too slow on the cases I, J, K and L. So my question is how to shut down all lamps in those cases with minimal number of switch presses?
I tried to run dan_fulea's code from the link but I was unable to read which switches should I press to shut down all lamps. I also saw the post https://math.stackexchange.com/questions/2569003/how-to-find-an-algorithm-to-shut-down-all-lamps-with-minimum-number-of-moves where was given an algorithm to solve the problem. But it looks too hard for me to implement it on Sagemath.
This is my best approach to solve the case I but the algorithm seem to be too slow to finish computation:
room = []
input = []
m = []
room.append('I')
input.append('1110111111101000100110011001100110100000001010001100011110110011111100000101000010101101100111001000100110110101111000110101101000010000100110111101001001011010110100100111011000110001101011110100110011010111110101010100')
m.append(27)
F = GF(2)
L_init = vector(F,input[0])
d = len(L_init)
M = matrix(F,d,d)
for i in range(d):
for j in range(-m[0],m[0]+1 ):
M[i,(i+j) % d] = 1
I = M.solve_right(L_init)
K = M.right_kernel()
best = None
for k in K:
A = (I+k).nonzero_positions()
B= []
if len((I+k).nonzero_positions()) < best or best == None:
S = room[0] + ' '
best = len((I+k).nonzero_positions())
for i in range(len(A)):
S +=str(int(A[i])+1) + ' '
print(S)
print("Optimal:")
print(S)mathhobbyistThu, 13 Dec 2018 07:47:32 +0100https://ask.sagemath.org/question/44656/