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.Wed, 26 Jun 2019 12:21:03 +0200Compute the square root of a complex matrixhttps://ask.sagemath.org/question/47010/compute-the-square-root-of-a-complex-matrix/Hello,
I can't find how to compute the square root of a complex matrix... I tried `m.sqrt()`, `sqrt(m)` (display only symbolic sqrt), `sqrt(m).n()`... None of them work. What is the regular way to compute the root of a complex matrix? (you can assume the matrix is self-adjoint if needed).
Thanks!
def test():
phi = matrix(CC, [[1/sqrt(2)],[i]])
m = phi * phi.C.T
print("Matrix: {}".format(m))
s = sqrt(m).n() # Does not work: AttributeError: 'ComplexField_class_with_category' object has no attribute 'complex_field'
#s = m^(1/2) # Does not work either, NotImplementedError: non-integral exponents not supported
# s = m.sqrt() # Does not work either, no attribute sqrt
print("Sqrt: {}".format(s))
print("s*s: {}".format(s*s))
if s*s == m:
print("Ok")
else:
print("Nope :(")
EDIT: For now I'm using the numpy backend with manual diagonalisation, but I'd like to know if there is a better way to proceed...
phi = matrix(CDF, [[1/sqrt(2)],[i]])
D, P = m.eigenmatrix_right()
s = P*diagonal_matrix([sqrt(x) for x in D.diagonal()])*P^-1tobiasBoraWed, 26 Jun 2019 12:21:03 +0200https://ask.sagemath.org/question/47010/Strange matrix producthttps://ask.sagemath.org/question/45547/strange-matrix-product/When I enter
A = matrix([[1, 2], [-1, 0], [1, 1]])
B = matrix([[0, 4], [1, -1], [1, 2]])
A*B
I get an error message, as expected.
But when I enter
A = matrix(QQ, [[1, 2], [-1, 0], [1, 1]])
B = matrix(QQ, [[0, 4], [1, -1], [1, 2]])
A*B
I get the output
[ 1 0]
[ 1 -2]
[ 1 3]
However, if I try
A = matrix(QQ, [[1, 2], [-1, 0], [1, 1]])
B = matrix(QQ, [[0, 4], [1, -1], [1, 2], [2, 2]])
A*B
I get an error message.
In all of the examples I try, Sage will give some output for a matrix product of two matrices of the same size as long as the field QQ is specified for at least one of the matrices. I’ve searched online and haven’t found anything to help me understand this. This will be problematic for my students who might draw the conclusion that the standard matrix product is defined when it really isn't. Can anyone help me understand this?SteveSMon, 25 Feb 2019 14:50:00 +0100https://ask.sagemath.org/question/45547/Cannot solve system of simultaneous equationshttps://ask.sagemath.org/question/42173/cannot-solve-system-of-simultaneous-equations/I am trying to solve a particular problem, but my test solution for setting up the code isn't working. I have tried two ways - one, which just outputs the same equations, and the other, I'm unsure on how to solve this large list of equations which are unrealistic to type out.
First attempt
var('a b c d e f g h i')
X=matrix(3,3,[[0,1,0],[0,0,1],[1,0,0]])
P=matrix(3,3,[[a,b,c],[d,e,f],[g,h,i]])
Pdagger=P.transpose()
Xdagger=X.transpose()
L=P*X*Pdagger
This outputs $$\begin{pmatrix} ab+ac+bc & cd+ae+bf & cg+ah+bi \\\\ bd+ce+af & de+df+ef & fg+dh+ei \\\\ bg+ch+ai & eg+fh+di & gh+gi+hi \end{pmatrix}$$ I then make $L=X$, then type these equations element wise into the simultaneous equation solver;
equations=solve([a*b+a*c+b*c==0,c*d+a*e+b*f==1,c*g+a*h+b*i==0,b*d+c*e+a*f==0,d*e+d*f+e*f==0,f*g+d*h+e*i==1,b*g+c*h+a*i==1,e*g+f*h+d*i==0,g*h+g*i+h*i==0],a,b,c,d,e,f,g,h,i)
The output:
a*e + b*f == 1, d*e + d*f + e*f == 0, b*g + c*h + a*i == 1, c*g + a*h + b*i == 0, e*g + f*h + d*i == 0, f*g + d*h + e*i == 1, g*h + g*i + h*i == 0]
Second attempt;
Q=X*Pdagger
B=Q.solve_left(X)
eqn=[]
for i in range(0,3):
for j in range(0,3):
eqn.append(B[i][j])
This gives me a list of the equations, I then try the solver again;
solve([eqn[0]==0,...eqn[8]==0],a,b,c,d,e,f,g,h,i)
This just gives me an error:
(a, b, c, d, e, f, g, h, i)
Error in lines 13-13
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "/ext/sage/sage-8.1/local/lib/python2.7/site-packages/sage/symbolic/relation.py", line 976, in solve
raise TypeError("%s is not a valid variable." % repr(i))
TypeError: 2 is not a valid variable.
The hope is that I will be able to extend this to larger matrices, so autonomy is welcomed greatly. But I will happily take any advice given I'm at a completely lose end. It is worth noting that the solution to this problem; $$PXP^{\dagger}=X$ is the identity matrix. My understanding is that the first method cannot find a closed form? I have even tried specifying that a,b,c,d,e,f,g,h,i are all 0 or 1 and this has not helped. I believe my second method is more on the right track, however I cannot afford to manually type out the 9 non-linear equations that pop out from the matrix solver.PatrickLewisThu, 26 Apr 2018 01:01:09 +0200https://ask.sagemath.org/question/42173/Creating a symbolic matrix in a loop and then substituting values in for the variables?https://ask.sagemath.org/question/41655/creating-a-symbolic-matrix-in-a-loop-and-then-substituting-values-in-for-the-variables/I need to create a symbolic matrix in terms of the variables xi with one row of n elements for a given integer n, so for example if n=5 i would need to compute the matrix `[x0, x1, x2, x3, x4]`
As a follow on from this i then need to substitute in random values from a finite field say `GF(q)` for the first `v` number of variables in the matrix for a given integer `v < n` . So for example if `v = 3` then i would need to substitute in random values from `GF(q)` for the first `x0, x1, x2 ` variables of the matrix to get something like `[GF(q).random_element(), GF(q).random_element(), GF(q).random_element(), x3, x4 ]` of course i need to be able to do this for any integers n and v i choose. Any help about how to do this in sage would be great, thanks.
EDIT: I need to substitute in random values from a finite field say `GF(q)` for the first `v` number of variables for a given integer `v < n` into each of the multivariate equations in a list. So for example, if my list of multivariate equations is given by `eqn` where `eqn` looks like the following: `[[10*x0^2 + 11*x0*x1 - 12*x1^2 - 14*x0*x2 + 8*x1*x2 + 6*x2^2 - 15*x0*x3 - 13*x1*x3 - 7*x2*x3 + 9*x3^2 - 13*x0*x4 + 11*x1*x4 + 6*x2*x4 - 15*x3*x4 + 11*x4^2 - 13*x0*x5 + 12*x1*x5 + 6*x2*x5 - 8*x4*x5 + x5^2], [4*x0^2 + 8*x0*x1 - 3*x1^2 - 8*x0*x2 - 3*x1*x2 - 10*x2^2 + 5*x0*x3 - 14*x1*x3 - 5*x2*x3 - 12*x3^2 - 10*x0*x4 + 8*x1*x4 - 10*x2*x4 - 4*x3*x4 + 6*x4^2 - 11*x0*x5 - 3*x1*x5 - 9*x2*x5 + 9*x3*x5 + 4*x4*x5 - 11*x5^2]]` when `o = 2` then i need to substitute random values from GF(q) into the first v variables, to obtain a list of multivariate equations in only the remaining variables that have not been substituted.DalvirTue, 20 Mar 2018 15:44:10 +0100https://ask.sagemath.org/question/41655/Cant find matrix kernelhttps://ask.sagemath.org/question/41456/cant-find-matrix-kernel/ I have a symbolic matrix at the start and I use the determinant to find roots, ok. But when I input some solution of the determinant equation in the matrix again, sage is not able to find the kernel (null space).
Here's my atempt:
A=matrix([[x^2,-1.75*x,0],[-1.75*x,x^2,2.07*i*x^2+4.65*i],[0,2.07*i*x^2+-4.65*i,x^2]])
sol=solve(A.det(),x,solution_dict=True)
A.subs(x=sol[0][x]).right_kernel_matrix()
My output is simple:
[]ianncunhaFri, 09 Mar 2018 21:33:07 +0100https://ask.sagemath.org/question/41456/Calculation Kernel of a matrixhttps://ask.sagemath.org/question/39575/calculation-kernel-of-a-matrix/I'm trying to write a program in which one part is related to calculation of kernel of a matrix. Its output and expected output are different. For example,
A = [[1, 0, 1], [1, 0, 0], [0, 1, 1], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, 0, -1], [0, -1, 1], [0, -1, 0], [-1, 0, 1]]
A is the matrix whose kernel is wanted.
When I calculate its kernel with some programs they give output different, my program is as well. But, when I try to calculate its kernel some others, like SAGE, give output,
1 0 0 0 0 0 0 -2 2 1
0 1 0 0 0 0 0 -1 1 1
0 0 1 0 0 0 0 -1 2 0
0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 -1 1 0
0 0 0 0 0 1 0 1 -1 -1
0 0 0 0 0 0 1 1 -1 0
The above one is what I expect as output. What is the point that I may overlook?
Here is my procedure to calculate the kernel in my program,
A.transposeInPlace();
FullPivLU<MatrixXf> lu(A);
MatrixXf A_null_space = lu.kernel();
A_null_space.transposeInPlace();
But in that way, I get different then expected one, but SAGE gives the above matrix that actually I expect.
0.5 0 -1 1 0 0 0 0 0 0.5
-0.5 0 -0 0 1 0 0 0 0 -0.5
0.5 0 -0 0 0 1 0 0 0 -0.5
0.5 0 -0 0 0 0 1 0 0 0.5
-1 0 1 0 0 0 0 1 0 -1
-0.5 0 1 0 0 0 0 0 1 -0.5
-0.5 1 -0 0 0 0 0 0 0 0.5
**I'm really but really confused because both matrix seem right! How come?**
Sage's output proof,
https://i.stack.imgur.com/F3ryq.png
My program's output proof,
https://i.stack.imgur.com/7Mw8y.pngstudentboyWed, 15 Nov 2017 15:38:29 +0100https://ask.sagemath.org/question/39575/Several matrix multiplications over binary fieldshttps://ask.sagemath.org/question/38251/several-matrix-multiplications-over-binary-fields/Hi! I need to compute the product of several matrices with entries in a binary field. Since products do not depend on each other, computations can be easily distributed among the processors. However results of sequencial code and in processes are almost the same!
Here some code of a simple test that represents what I want to achieve:
from sage.all import *
import multiprocessing as mp
from multiprocessing.pool import ThreadPool, cpu_count
import time
def matrix_mult(A, B):
start = time.time()
A*B
return time.time() - start
def matrix_mult_proc(A, B, n):
avg = 0.0
for i in range(n):
start = time.time()
A*B
avg += (time.time() - start)
# print('time: {}'.format(time.time() - start))
print('Average per call proc {}: {}'.format(os.getpid(), avg / n))
@parallel
def matrix_mult_parallel(A, B):
start = time.time()
A*B
return time.time() - start
# print('time: {}'.format(time.time() - start))
iters = 40
n = 100
Fq = GF(2**16, 'X')
MS = MatrixSpace(Fq, n)
A = MS.random_element()
start = time.time()
tms = map(lambda i: matrix_mult(A, A), range(iters))
print('\n*** sequencial:\nAverage per call: {}\nTotal: {}'.format(sum(tms) / iters,time.time() - start))
nthreads = cpu_count()
pool = ThreadPool(nthreads)
start = time.time()
tms = pool.map(lambda i: matrix_mult(A, A), range(iters))
print('\n*** multithread {} threads:\nAverage per call: {}\nTotal: {}'.format(nthreads, sum(tms) / iters,time.time() - start))
nprocs = cpu_count()
procs = []
start = time.time()
print('\n*** multiproc {} procs:'.format(nprocs))
for i in range(nprocs):
p = mp.Process(target=matrix_mult_proc, args=(A, A, iters//nprocs))#, out_q))
procs.append(p)
p.start()
for p in procs:
p.join()
print('Total: {}'.format(time.time() - start))
args = [(A, A)]*iters
start = time.time()
tms = map(lambda e: e[1], list(matrix_mult_parallel(args)))
print('\n*** sage_parallel\nAverage per call: {}\nTotal {}'.format(sum(tms) / iters, time.time() - start))
Results are as follow:
For sequencial
Average time of a matrix multiplication: 0.279646992683
Total time for 40 multiplications: 11.1862668991
For Threadpool with 4 threads maximum
Average time of a matrix multiplication: 0.280531394482
Total time for 40 multiplications: 11.2248089314
For 4 processes in a 4 core computer (2 physical):
Average time of a matrix multiplication: 1.13726825714
Total time for 40 multiplications: 11.7641329765
With sage's @parallel decorator:
Average time of a matrix multiplication: 1.1256641984
Total time for 40 multiplications: 11.7641329765
I don't understand why multiplications seem to take a proportional amount of time to the number of processes. Same behavior on a 8 core machine.
Hope someone can explain. Thanks in advance.egonzalezFri, 14 Jul 2017 04:17:23 +0200https://ask.sagemath.org/question/38251/Hermite form, entries reduced over pivotshttps://ask.sagemath.org/question/37964/hermite-form-entries-reduced-over-pivots/ Hello!
I have a question concerning the function hermite_form. This function unfortunately does not reduce the entries above the pivots of the hermite normal form if I use the function on a matrix with polynomial entries. Is there another function or option which enables me to do that?
Following short example hopefully illustrates what I mean:
R.<x> = QQ[];
M = matrix(2,2, [x,x, 0,x]);
M.hermite_form()
This will give the exact same matrix M and not the matrix [x,0, 0,x] which I would expect or hope to get.
Especially if somebody wants to apply another algorithm to such a matrix in hermite form it can be difficult to work with non-reduced matrices.
Kind regards
Philippphilipp7Fri, 16 Jun 2017 17:24:41 +0200https://ask.sagemath.org/question/37964/How could I copy a matrix to another matrix?https://ask.sagemath.org/question/37310/how-could-i-copy-a-matrix-to-another-matrix/I have a matrix and I wanna copy this matrix to another matrix(let's call them M1 and M2),
but when I use: M2=M1 and modify M1, M2 changes too, which means M1 and M2 are actually one matrix.
So how should I copy the matrix, just the value of entries.Zhengguan LiMon, 17 Apr 2017 04:57:28 +0200https://ask.sagemath.org/question/37310/Element-wise product of matriceshttps://ask.sagemath.org/question/37078/element-wise-product-of-matrices/ I am trying to calculate Hadamard product of two matrices.
<pre><code>
def elementwise(M, N):
assert(M.parent() == N.parent())
nc, nr = M.ncols(), M.nrows()
A = copy(M.parent().zero_element())
for r in xrange(nr):
for c in xrange(nc):
A[r,c] = M[r,c] * N[r,c]
return A
ring = PolynomialRing(QQ, 1, 'x')
T = Matrix(ring, [
[1,-x,-x,0],
[-x,1,0,-x],
[0,-x,1,0],
[-x,0,0,1]])
R = Matrix(QQ, [
[1,1,0,1],
[1,1,1,0],
[1,0,0,1],
[0,1,1,0]])
B = ~T
elementwise(B,R)</pre></code>
I get a following mistake
<pre><code>Error in lines 21-21
Traceback (most recent call last):
File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "", line 2, in elementwise
AssertionError</pre></code>
Although elementwise(B,R) does not work, the program calculates elementwise(B,B) and elementwise(R,R) without any mistakes. Probably that's because matrices B and R have different types (B is a dense matrix over general ring and R is a rational dense matrix). What should I do?dmitalSat, 25 Mar 2017 13:48:47 +0100https://ask.sagemath.org/question/37078/echelon_form not giving reduced echelon formhttps://ask.sagemath.org/question/35819/echelon_form-not-giving-reduced-echelon-form/According to the document(emphasis mine):
> echelon_form(algorithm='default', cutoff=0, **kwds)
>
> Return the echelon form of self.
>
> **OUTPUT**:
>
> The *reduced row echelon form* of self, as an immutable matrix.
Here is what I was doing:
sage: A = Matrix([[1,0,3,1,2],[-1,3,0,-1,1],[2,1,7,2,5],[4,2,14,0,6]])
sage: A.echelon_form()
[1 0 3 1 2]
[0 1 1 0 1]
[0 0 0 4 4]
[0 0 0 0 0]
While the result is in row echelon form, it's not in **reduced** row echelon form. What I expect is something like this:
[1 0 3 0 1]
[0 1 1 0 1]
[0 0 0 1 1]
[0 0 0 0 0]
Also, if this is merely a documentation bug, how can I get the reduced row echelon form of a matrix?sunqingyaoWed, 30 Nov 2016 15:24:42 +0100https://ask.sagemath.org/question/35819/Bug in matrix multiplication and inversion?https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/I'm using sage 7.1 and this code here returns `false`.
M = matrix ([[sqrt(1-1/2),1/2],[1,-sqrt(1-1/2)]])
N = matrix ([[sqrt(1-1/3),1],[1/3,-sqrt(1-1/3)]])
(M*N).inverse() == N.inverse()*M.inverse()
What is going on?
ThomasMon, 18 Apr 2016 16:06:07 +0200https://ask.sagemath.org/question/33133/pseudo-inverse of a Zmod(2) matrixhttps://ask.sagemath.org/question/32675/pseudo-inverse-of-a-zmod2-matrix/ I have a 25 x 25 Zmod(2) matrix that is singular, and I would like to find the pseudo-inverse. I haven't found anything yet that will work. Suggestions?doug719Sat, 27 Feb 2016 00:05:36 +0100https://ask.sagemath.org/question/32675/elementary matrices for elementary row operationshttps://ask.sagemath.org/question/31194/elementary-matrices-for-elementary-row-operations/Hello,
I'm using Sage to get random_matrix for Students.
random_matrix(QQ,4,4, algorithm="echelonizable", rank=4, upper_bound=15)
Is there an option to get all elementary matrices for all elementary matrix-operations by using for example matrix.inverse()?
Thanks for the help!SebastianMon, 30 Nov 2015 11:48:03 +0100https://ask.sagemath.org/question/31194/Can I use the block_matrix command to create arbitrary sizes of block matrix arrays?https://ask.sagemath.org/question/26780/can-i-use-the-block_matrix-command-to-create-arbitrary-sizes-of-block-matrix-arrays/ Like say I want to create a $4k \times 4k$ size matrix by putting together certain $4\times 4$ matrices into a $k \times k$ array.
- Can the "block_matrix" command help me create such a $4k \times 4k$ array of 0s called $A$ ? (such later I can say something like $A[a,b] = X$ where $0 \leq a,b \leq k$ and $X$ is some $4\times 4$ matrix and that will assign the matrix $X$ at the $(a,b)$ array position) phoenixSat, 09 May 2015 20:40:56 +0200https://ask.sagemath.org/question/26780/How to build a matrix thought of as an array of smaller matrices?https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/ Say I am given a data set which looks like $[ (0,2,A), (0,3,B), (1,2,C), (1,4,D) ]$ where $A,B,C,D$ are matrices all of the same dimension say $k$. (the data set will always have unique pairs of integers - as in if (1,2,*) tuple occurs then (2,1,*) tuple will not occur)
Now I want to create a 4x4 matrix say X of dimension $4k$ thought of as a 4x4 array of k-dimensional matrices.
The arrays in $X$ are to be defined as $X(0,2) = A, X(2,0) = A^{-1}, X(0,3) = B, X(3,0) = B^{-1}, X(1,2) = C, X(2,1) = C^{-1}, X(1,4) = D, X(4,1) = D^{-1}$ and all other array positions in $X$ are to be filled in with $0$ matrices of dimension $k$.
- How can one create such a X on SAGE?
X is a matrix of matrices and I am not sure how one can define this on SAGE. Like saying "X(0,3) = B" is not going to make any obvious sense to SAGE. I necessarily need X to be a matrix so that i can later say calculate its characteristic polynomial.
[I showed this above example with just $4$ tuples. I want to eventually do it with much larger data sets]
phoenixFri, 08 May 2015 19:38:32 +0200https://ask.sagemath.org/question/26764/Matrix dot multiplication slowness and BLAS versionshttps://ask.sagemath.org/question/26507/matrix-dot-multiplication-slowness-and-blas-versions/Hello everyone!
Is there a way to increase a performance of matrix multiplication in Sage? Right now I am relying on numpy's dot function like this:
import numpy as np
N = 768
P = 1024
A = np.random.random((P, N))
A.T.dot(A)
Timing dot product in Sage now gives me a time about second and a half:
>>> setup = """
...
... import numpy as np
...
... N = 768
... P = 1024
...
... A = np.random.random((P, N))
... """
>>> timeit.repeat('A.T.dot(A)', setup=setup, number=10, repeat=3)
[18.736198902130127, 18.66787099838257, 17.36500310897827]
Yet the same multiplication in Matlab takes less than 100 ms. I heard that numpy internally relying on BLAS and it can be replaced with OpenBLAS /ATLAS/IntelMKL or something like that for the better performance.
So I am looking for some kind of manual or info about that is going on with the performance in regard with underlying numpy's components and when one should consider replacing one with another and is there a simple way to do that?
EugeneSun, 12 Apr 2015 15:52:31 +0200https://ask.sagemath.org/question/26507/Why cant I multiply two vectors together on sage cell?https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/Hello all. Sage noob here. My friend recently told me about sage and convinced me to give it a whirl. (My background is MATLAB).
I made an account, and went to sage cell for a quick run. I typed this in:
v = vector([3, 4]);
vC = v.column();
vC*v
I would like to simply get the outer product of those two vectors. However it errors out.
I would like to think that I am boneheading something here, because this is a legitimate operation. I have multiple tutorial/documentation tabs open but nothing really elucidating why/how this doesnt work.
Would appreciate any insight.
GravitusMon, 17 Feb 2014 12:11:18 +0100https://ask.sagemath.org/question/11049/How to do symbolic vector/matrix manipulations in Sage?https://ask.sagemath.org/question/11050/how-to-do-symbolic-vectormatrix-manipulations-in-sage/
Hello,
So I have a 2x1 vector $\mathbf{x} \in R^2$, and a function on it called $f(\mathbf{x}) = log(\mathbf{1}^T \mathbf{x})$ which of course, returns a scalar.
I would like to compute the Hessian of this function in Sage, symbolically. That is, I would like it so show me what the 2x2 Hessian matrix would look like, but in terms of the elements of $x$, or even better, in terms of the vector $\mathbf{x}$.
Is there such a way? Thanks. GravitusMon, 17 Feb 2014 12:35:27 +0100https://ask.sagemath.org/question/11050/Determinant of block matriceshttps://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
kautschukSat, 23 Nov 2013 02:45:41 +0100https://ask.sagemath.org/question/10766/element-wise operationshttps://ask.sagemath.org/question/10707/element-wise-operations/In MATLAB element-wise operations:
[link text](http://www.glue.umd.edu/afs/glue.umd.edu/system/info/olh/Numerical/Matlab_Matrix_Manipulation_Software/Matrix_Vector_Operations/elementwise)
".*", ".^", "./" and so on.
In SAGE I have only found ".elementwise_product()". Are there any other element-wise operations on matrices, vectors, lists?
Thanks in advance!gundamlhSun, 10 Nov 2013 12:47:44 +0100https://ask.sagemath.org/question/10707/Division of polynomial matriceshttps://ask.sagemath.org/question/10439/division-of-polynomial-matrices/Is it possible to perform euclidean division between two polynomial matrices in sage?
e.g.if
$A= \begin{bmatrix}
x^2 +1 & x \newline
0&x-1
\end{bmatrix} , B=\begin{bmatrix}
x & 2 \newline
1 &x-1
\end{bmatrix}$
are given find the matrices $Q,R$ so $A=QB +R$
*in this example the answer is $Q= \begin{bmatrix}
x & -1 \newline
0& 1
\end{bmatrix} , R=\begin{bmatrix}
2 & -1 \newline
-1 & 0
\end{bmatrix}$*koukourikosThu, 15 Aug 2013 15:03:25 +0200https://ask.sagemath.org/question/10439/Adjoint of symbolic matrix fails due to type conversionhttps://ask.sagemath.org/question/10366/adjoint-of-symbolic-matrix-fails-due-to-type-conversion/Trying the following code
var('x')
Matrix([[sqrt(x),x],[1,0]]).adjoint()
I get an error message
TypeError: no conversion of this rational to integer
This seems very strange to me. Computing the adjoint of any matrix should be possible, without requiring the elements to be rational, integral or anything else special. Over the symbolic ring in particular, it should always be possible to write the minors as expressions, perhaps without any evaluation or simplification in cases where doing more might cause trouble.
Surprisingly, computing the inverse works without issues. This is despite the fact that inversion requires division, and thus entails the danger of division by zero, where computing the adjoint does not. So one workaround might be multiplying the inverse by the determinant. But this will fail for some matrices where computing the adjoint should work. And it might also leave behind divisions within the elements, which might be difficult to get rid of without changing other radical changes to the form of the involved expressions. I just had a more complicated example where `simplify` left the division and `simplify_full` split square roots in a rather unreadable fashion.
Is there a valid explanation for the above behavior, or is this a bug?
Is there a better way to work around this problem?MvGFri, 19 Jul 2013 18:00:37 +0200https://ask.sagemath.org/question/10366/Convert a sage.interfaces.matlab.MatlabElement objecthttps://ask.sagemath.org/question/10003/convert-a-sageinterfacesmatlabmatlabelement-object/Hi,
I tried the following command to call Matlab to generate a sparse matrix and multiply it with a vector
sage: matlab('rand(3)')*matrix([1,2,2]).T
0.0344 0.7655 0.4898
0.4387 0.7952 0.4456
0.3816 0.1869 0.6463
This is not correct. It seems the multiplication is not done. I'm curious if there is anyway to do the multiplication of a sage.interfaces.matlab.MatlabElement object and a built-in matrix object? Or is there anyway to convert a sage.interfaces.matlab.MatlabElement object to one that can do multiplications with the built-in matrix types? Thanks ZhengMon, 08 Apr 2013 14:46:59 +0200https://ask.sagemath.org/question/10003/backslash operator on RDF matriceshttps://ask.sagemath.org/question/9787/backslash-operator-on-rdf-matrices/I've discovered that the backslash operator produces a weird type error when used on matrices with Real Double Field entries:
----------------------------------------------------------------------
| Sage Version 5.6, Release Date: 2013-01-21 |
| Type "notebook()" for the browser-based notebook interface. |
| Type "help()" for help. |
----------------------------------------------------------------------
sage: A = matrix(QQ, 3, [1,2,4,2,3,1,0,1,2])
sage: B = matrix(QQ, 3, 2, [1,7,5,2,1,3])
sage: A\B
[ -1 1]
[13/5 -3/5]
[-4/5 9/5]
sage: A = matrix(RDF, 3, [1,2,4,2,3,1,0,1,2])
sage: B = matrix(RDF, 3, 2, [1,7,5,2,1,3])
sage: A\B
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/gtmarks/<ipython console> in <module>()
/usr/local/sage-5.6/local/lib/python2.7/site-packages/sage/misc/preparser.pyc in __mul__(self, right)
1398 (0.0, 0.5, 1.0, 1.5, 2.0)
1399 """
-> 1400 return self.left._backslash_(right)
1401
1402
/usr/local/sage-5.6/local/lib/python2.7/site-packages/sage/matrix/matrix2.so in sage.matrix.matrix2.Matrix._backslash_ (sage/matrix/matrix2.c:3658)()
/usr/local/sage-5.6/local/lib/python2.7/site-packages/sage/matrix/matrix_double_dense.so in sage.matrix.matrix_double_dense.Matrix_double_dense.solve_right (sage/matrix/matrix_double_dense.c:11636)()
TypeError: vector of constants over Real Double Field incompatible with matrix over Real Double Field
I don't understand the final "imcompatibility" message: both the vector and the matrix are RDF type, so how could they be incompatible? This is a small version of a problem I encountered in the context of an attempt to demonstrate to a linear algebra class the computation time of solving systems of linear equations via LU factorization versus via matrix inversion by naive methods; to make the case for LU, I have to avoid various optimized algorithms built into SAGE. (Perhaps I should be using a call to LAPACK or something for this demonstration?)Greg MarksFri, 08 Feb 2013 21:53:18 +0100https://ask.sagemath.org/question/9787/creating a matrix from blockshttps://ask.sagemath.org/question/9588/creating-a-matrix-from-blocks/I am new to SAGE. I need to create a matrix by using matrices of different sizes. For example:
Abar= [(A + B), C]
[ D , E]
where A and B are 2x2's, C is a 2x1, D is a 1x2, and E is a 1x1. I do not need Abar to be a partitioned matrix. Thanks much.DionysusThu, 29 Nov 2012 19:30:58 +0100https://ask.sagemath.org/question/9588/