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.Mon, 07 Feb 2022 18:10:42 +0100How to construct a matrix space ?https://ask.sagemath.org/question/60999/how-to-construct-a-matrix-space/I know we can create a matrix space using "MatrixSpace" but I am looking to create a specific space. I tried :
M = MatrixSpace(ZZ,4)
B = M.basis()
sub = [B[0,0],B[1,3],B[2,2],B[3,1]]
SubM = M.submodule(sub)
SubM.basis()
The program show me : `Finite family {0: B[0], 1: B[1], 2: B[2], 3: B[3]}`
And when I take a random element of SubM( with `SubM.random_element()`), it tells me it isn't in M.
I thought the problem came from my generator so I tried with :
`sub = [matrix(4,4,{(0,0):1}),matrix(4,4,{(1,3):1}),matrix(4,4,{(2,2):1}),matrix(4,4,{(3,1):1})] `
But I have the same result.
Do you know what I am doing wrong ?
I also need to do a XOR between matrix space, do you think it is possible ? How ?
Thanks you for your time :D
IHaveNoIdeaMon, 07 Feb 2022 18:10:42 +0100https://ask.sagemath.org/question/60999/Changing MatrixSpace Basis?https://ask.sagemath.org/question/46107/changing-matrixspace-basis/
V = (MatrixSpace(SR, 2, 2))
W = V.subspace_with_basis(
[
matrix([[1,1],[1,1]]),
matrix([[1,0],[pi,1]]),
matrix([[0,7],[8,9]]),
matrix([[1,0],[0,-1]])
]
)
var('a', 'b','c', 'd')
W.coordinate_vector(matrix([[a+b,a+b],[c+d,c+d]]))
This doesn't worko6pFri, 12 Apr 2019 06:44:28 +0200https://ask.sagemath.org/question/46107/how to get the transformation matrix for a transformation over 4x4 matrices?https://ask.sagemath.org/question/44194/how-to-get-the-transformation-matrix-for-a-transformation-over-4x4-matrices/I have the following transformation:
M = MatrixSpace(QQ,4,4)
def f(m):
return matrix([
[m[0][0], m[0][1], m[0][2], m[0][3]],
[m[1][3], m[1][0], m[1][1], m[1][2]],
[m[2][2], m[2][3], m[2][0], m[2][1]],
[m[3][1], m[3][2], m[3][3], m[3][0]]
])
print linear_transformation(M, M, f)
This is not working - but I can't figure out what linear_transformation is expecting.
How can I get this to work? Is this even possible?rlswMon, 05 Nov 2018 21:00:31 +0100https://ask.sagemath.org/question/44194/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/Find the kernel of a matrix $A$ and make it a matrix.https://ask.sagemath.org/question/33533/find-the-kernel-of-a-matrix-a-and-make-it-a-matrix/ I am trying to write a function that computes the monic genrator of an ideal $I\in k[x_1,\dots,x_n]$, i.e., the generator of $I\cap k[x_i]$ for each $i$. For this I need to use linear algebra for the set
$$\{1, x_i, x_i^2,\dots\}$$
I write each one of them in terms of the basis for the quotient ring $k[x_1,\dots,x_n]/I$, and see if they are linearly dependent.
Since I add in one more power a time, when I find a linearly dependent set, it should have nullity $1$. So if I can get the one element in basis of the kernel, I am done. But the $A.kernel()$ command in Sage gives me this:
N=M.kernel();N
Vector space of degree 2 and dimension 1 over Rational Field
Basis matrix:
[0 1]
Is there a way to assign it as a vector using the kernel command? Or do I have to write my own function to implement it? Thank you for your help!KittyLTue, 24 May 2016 13:47:57 +0200https://ask.sagemath.org/question/33533/Direct sum of matrix spaceshttps://ask.sagemath.org/question/11169/direct-sum-of-matrix-spaces/Is it possible to construct a direct sum of matrix spaces?
For example:
M1 = MatrixSpace(QQ, 1)
M2 = MatrixSpace(QQ, 2)
M = DirectSum(M1, M1, M2) # doesnt work
ThanksvuurSun, 23 Mar 2014 09:09:32 +0100https://ask.sagemath.org/question/11169/