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, 12 Oct 2020 03:21:48 +0200Using 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/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/