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.Mon, 05 Aug 2019 08:51:11 -0500How to draw a graph whose vertices are elements of permuation grouphttp://ask.sagemath.org/question/47395/how-to-draw-a-graph-whose-vertices-are-elements-of-permuation-group/How to write the following program in SageMath:
Consider the Permutation Group $S_3$.
The elements of $S_3$ are $e,(12),(13),(23),(123),(132)$.
I want to draw a graph $G$ whose members are the elements of $S_3$ and two vertices $x,y$ are adjacent if and only if $xy\neq yx$.
I am stuck in doing the following things:
1. How to call the elements of $S_3$ through a loop?
2. How to draw the graph $G$ ?
I can check whether they commute or not but I am stuck in the two things.
Is there any way to write the code in SageMath?
As an example if I input $S_3$ I want to get the following graph
$G=Graph({1:[2,3,4,5],2:[1,3,4,5],3:[1,2,4,5],4:[1,2,3],5:[1,2,3]})$
Any help will be highly appreciated.CaptchaMon, 05 Aug 2019 08:51:11 -0500http://ask.sagemath.org/question/47395/Is there any code to calculate the subgroup generated by the permutationshttp://ask.sagemath.org/question/47371/is-there-any-code-to-calculate-the-subgroup-generated-by-the-permutations/ Consider the Symmetric Group $S_4$
I have three permutations namely $(3,4), (1,2), (1,3)(2,4)$.
How to find the subgroup generated by these permutations ?
Is there any code to calculate in Sagemath the subgroup generated by the permutations given above?
As an example the code should work like this :
If I input $(12)$ the code should give $\{(12),e\}$
Can someone help please?CaptchaSun, 04 Aug 2019 01:16:11 -0500http://ask.sagemath.org/question/47371/Towered extension fields through chosen polynomialhttp://ask.sagemath.org/question/46862/towered-extension-fields-through-chosen-polynomial/(Edited, since `<u>` compiles in HTML as an underlyning tag, dan.)
K.<u> = GF(q)
KT.<u> = K.extension(2)
KTT.<u> = K.extension(6)
In this case `KTT` is a degree 12 extension. My question is:
Can I obtain `KTT` by `KTT.<u> = K.extension(x^6 - a)`, where `x^6 -a` is some irreducible polynomial over `KT` ???
Documentation gives nothing about this towering of fields.pchSat, 08 Jun 2019 07:23:35 -0500http://ask.sagemath.org/question/46862/'CyclicCode_with_category' object has no attribute 'gen_mat'http://ask.sagemath.org/question/41150/cycliccode_with_category-object-has-no-attribute-gen_mat/I am trying to create the generator matrix for Quadratic Residue code and the code I am using is:
def gen_matrices(n):
C = codes.QuadraticResidueCode(n, GF(2))
D = C.dual_code()
G = C.gen_mat()
H = D.gen_mat()
J = G.transpose() * (G * G.transpose())^-1
K = H.transpose() * (H * H.transpose())^-1
if rank(block_matrix([[G],[H]]))!=G.ncols()\
or G * H.transpose ()!=0:
raise("Logic error: The code is not LCD")
return [ G, H, J, K ]
gen_matrices(17)
Here I am first generating the QR code, where G is the generator matrix for the code C and H is the generator matrix for the cyclic code D. So after the encoding is done we now do XOR operation of the two codes - z = xG (XOR) yH. Now I want to do decoding and here J and K are the matrices which are used for doing decoding. Here J is given by J = G.Transpose*(G*G.Transpose)^-1.
Here I am running a code that is being used in a paper.
The paper is (https://hal.archives-ouvertes.fr/hal-01240242/document).
When I am running the code I am getting the following error:
AttributeError Traceback (most recent call last)
<ipython-input-2-68b96fb2983c> in <module>()
----> 1 gen_matrices(Integer(17))
<ipython-input-1-f09fbcf52b0e> in gen_matrices(n)
2 C = codes.QuadraticResidueCode(n, GF(Integer(2)))
3 D = C.dual_code()
----> 4 G = C.gen_mat()
5 H = D.gen_mat()
6 J = G.transpose() * (G * G.transpose())**-Integer(1)
/opt/sagemath-8.1/src/sage/structure/category_object.pyx in sage.structure.categ ory_object.CategoryObject.__getattr__ (build/cythonized/sage/structure/category_ object.c:8014)()
854 AttributeError: 'PrimeNumbers_with_category' object has no a ttribute 'sadfasdf'
855 """
--> 856 return self.getattr_from_category(name)
857
858 cdef getattr_from_category(self, name):
/opt/sagemath-8.1/src/sage/structure/category_object.pyx in sage.structure.categ ory_object.CategoryObject.getattr_from_category (build/cythonized/sage/structure /category_object.c:8177)()
869 cls = self._category.parent_class
870
--> 871 attr = getattr_from_other_class(self, cls, name)
872 self.__cached_methods[name] = attr
873 return attr
/opt/sagemath-8.1/src/sage/cpython/getattr.pyx in sage.cpython.getattr.getattr_f rom_other_class (build/cythonized/sage/cpython/getattr.c:1837)()
247 dummy_error_message.cls = type(self)
248 dummy_error_message.name = name
--> 249 raise dummy_attribute_error
250 cdef PyObject* attr = _PyType_Lookup(<type>cls, name)
251 if attr is NULL:
AttributeError: 'CyclicCode_with_category' object has no attribute 'gen_mat'
Can anyone please suggest what I need to do?SubhajitFri, 16 Feb 2018 00:41:37 -0600http://ask.sagemath.org/question/41150/Why does the code not work?http://ask.sagemath.org/question/40318/why-does-the-code-not-work/Hi,
I wanted to use a program from an older question from myself again and found the following strange thing:
The following code works fine:
m=3
le_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.comparability_graph().edges(labels=None)]
cover_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.hasse_diagram().edges(labels=None)]
format_pt = lambda k: "'x{0}'".format(k+1)
format_relations = lambda relations: [[format_pt(a),format_pt(b)] for a,b in relations]
format_pts = lambda P: [format_pt(a) for a in P]
format_poset = lambda P: [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]
what_you_want = lambda n: [format_poset(P) for P in [posets.BooleanLattice(m)]]
print(what_you_want(m))
The following code does not work:
m=3
le_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.comparability_graph().edges(labels=None)]
cover_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.hasse_diagram().edges(labels=None)]
format_pt = lambda k: "'x{0}'".format(k+1)
format_relations = lambda relations: [[format_pt(a),format_pt(b)] for a,b in relations]
format_pts = lambda P: [format_pt(a) for a in P]
format_poset = lambda P: [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]
what_you_want = lambda n: [format_poset(P) for P in posets.YoungsLattice(m)]
print(what_you_want(m))
The only difference between two codes is that in the second code I have
what_you_want = lambda n: [format_poset(P) for P in posets.YoungsLattice(m)]
instead of
what_you_want = lambda n: [format_poset(P) for P in [posets.BooleanLattice(m)]].
Entering the second code gives an error.
I do not really understand why the first works and the second does not, so any help is welcome how to fix the error.sagequstionsTue, 26 Dec 2017 17:05:26 -0600http://ask.sagemath.org/question/40318/Export to C codehttp://ask.sagemath.org/question/7922/export-to-c-code/Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.
hennesMon, 07 Feb 2011 01:26:21 -0600http://ask.sagemath.org/question/7922/%paste leads to dyld: Symbol not found: _sqlite3_intarray_bindhttp://ask.sagemath.org/question/34468/paste-leads-to-dyld-symbol-not-found-_sqlite3_intarray_bind/ I'm trying to paste Python code into Sage-5.12-OSX-64bit-10.8 and I'm getting the following error:
sage: %paste
dyld: Symbol not found: _sqlite3_intarray_bind
Referenced from: /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData
Expected in: /Applications/Sage-5.12-OSX-64bit-10.8.app/Contents/Resources/sage/local/lib/libsqlite3.dylib
in /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData
## -- End pasted text --
What's wrong?
%cpaste works.mavaviljSun, 14 Aug 2016 04:29:45 -0500http://ask.sagemath.org/question/34468/Simply trying to understand these lines of codehttp://ask.sagemath.org/question/33505/simply-trying-to-understand-these-lines-of-code/ Very new to sage, and trying to decipher all that's going on in the following code:
ndirect = 4;
p = [];
for i in range (0,25):
p.append (Primes ().unrank (i));
def calculate144311 (n):
return recurseDirectly (n,0,[0] * n) - 1;
def recurseDirectly (n,np,residues):
result = 0;
if (np < ndirect):
residues [np] = 0;
lim = p [np] - 1;
while residues [np] < lim:
if residues [np] != 1:
result = max (result,recurseDirectly (n,np + 1, residues));
residues [np] = residues [np] + 1;
else:
remains = [];
for r in range (1,2000):
for i in range (0,ndirect):
toAdd = true;
res = (r + residues [i]) % p [i];
if res == 1 or res == p [i] - 1:
toAdd = false;
break;
if (toAdd):
remains.append (r);
result = max (result,recurse (n,0,ndirect,remains,[false] * n,[0] * n,residues));
return result;
def recurse (n,d,nused,remains,used,primes,residues):
result = 0;
repeat = true;
while (repeat):
result = max (result,remains [d]);
repeat = false;
for i in range (ndirect,nused):
r = (residues [i] + remains [d]) % primes [i];
if r == 1 or r == primes [i] - 1:
d += 1;
repeat = true;
break;
for i in range (ndirect,n):
if not used [i]:
pi = p [i];
for r in [-1,1]:
res = pi - ((r + remains [d] - 1) % pi + 1);
if res != 1 and res != pi - 1:
residues [nused] = res;
primes [nused] = pi;
used [i] = true;
result = max (result,recurse (n,d + 1,nused + 1,remains,used,primes,residues));
used [i] = false;
return result;
for i in range (ndirect,17):
print (i,calculate144311(i));
Here's my current grasp of what's going on. My hope is that someone will guide me through my misunderstandings and help fully get how to code similarly in sage:
The final 2 lines of code are what sage is running; everything before given the information that it'll use to run it. It will create a list of outputs for "calculate 144311" using the inputs in the range of ndirect (defined in line 1 as it is used throughout the code) up to whatever number I choose as a stopping point (in this case, 17).
The code first looks at "def calculate 144311" (def meaning "define"?) and is told to run "recurseDirectly with three inputs, "n, 0, and 0 times n? (need some help here). It then subracts 1 from... all three?
Since recurseDirectly is defined as having inputs of "n, np, and residues" this begin with values given by "calculate 144311". First, the code sets the result to 0. We then begin an if then with "np (which started at 0) < ndirect" being the qualifier. If True, it runs the "if". False runs the "else".
The if begins by setting the residues of np to 0 (how does this relate to "residues" above?), sets lim to the product of p and np minus 1 (it appears from above the p begins as an empty set and is then populated with the first "i" primes up to a max of 25?).
As part of the "if", while runs a loop as long as residues of np are less than lim. This while contains its own if then. if residues doesn't equal 1, then if sets to the result to the max of... I don't get what it's doing here. Need help. It then adds 1 to the residues of np.
Now we move to the else:
"remains" starts as an empty set. The "for" command loops the following as long as r and i are within the range (do these count up by one in this case?). toAdd starts true. res... well, I don't know what's happening here. Another if: res =1 or the product of p and i minus 1, toAdd then becomes false. Don't know what break does. Then, if toAdd is True, remains has r added as an element of its set. Result is then set to the max of the following... which I don't get. However, this triggers "recurse". And a result is returned... meaning?
Next, recurse.
This has 7 inputs gained from above. Result again begins at 0. Repeat begins true. While it's true, there's a loop. Result becomes the bigger of itself or the remains of d. repeat becomes false. For loops code while i is between ndirect and nused (counting up by 1?). r becomes... something.
I'm going to stop there as this is already too long.
Would love to be able to code something like this myself, but I need some guidance understanding what's going on.
Thanks!toucanSat, 21 May 2016 23:48:03 -0500http://ask.sagemath.org/question/33505/How do I use a sage graph in LaTEX?http://ask.sagemath.org/question/25896/how-do-i-use-a-sage-graph-in-latex/I created a project in Sage that graphed multiple curves at once. Is there a way I can just import this graph or the code into LaTEX?ecain17Sun, 22 Feb 2015 21:42:20 -0600http://ask.sagemath.org/question/25896/How does one get Sage to make random choices?http://ask.sagemath.org/question/25783/how-does-one-get-sage-to-make-random-choices/ Like say in the middle of a code I compute a function value $f(x)$ and I knwo that $0 \leq f(x) \leq 1$. Now I want to write an if-then-else which will do one option with probability $f(x)$ and the other otion with probability $1-f(x)$. How does one make Sage do this?
Can somoen kindly show a template example to simulate this? phoenixMon, 09 Feb 2015 15:54:11 -0600http://ask.sagemath.org/question/25783/Adding Code To My SageMath Notebookhttp://ask.sagemath.org/question/11052/adding-code-to-my-sagemath-notebook/I have written a class, LRC, using the Sage Notebook which works just fine. Now I would like to put it into my sage environment without including it on each worksheet.
Before I go on I should describe my environment a bit. I am running a windows 7 machine with Virtual Box. I imported a Sage Notebook virtual machine and run it. The virtual box shares some of the drive space with the windows machine, in particular a spot called D:\unixshare
So I extracted the code from the cell to a file and placed it at D:\unixshare\forLRC\LRC.py which at least in theory available to the virtual box machine.
In a new worksheet I put this in a cell.
import sys
sys.path.append('/media/sf_D_DRIVE//unixshare/forLRC') # move your python files here
import LRC
lrc = LRC()
.-.-.-.-.-.-.-.-.-.-
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_4.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW1wb3J0IHN5cwpzeXMucGF0aC5hcHBlbmQoJy9tZWRpYS9zZl9EX0RSSVZFLy91bml4c2hhcmUvZm9yTFJDJykgIyBtb3ZlIHlvdXIgcHl0aG9uIGZpbGVzIGhlcmUKCmltcG9ydCBMUkMKCmxyYyAgICA9IExSQygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpMF8IdA/___code___.py", line 5, in <module>
import LRC
File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 22, in <module>
class LRC( object ):
File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 31, in LRC
var( "myOmega" ) # sometimes used as the angular frequency
NameError: name 'var' is not defined
I did get a new file: D:\unixshare\forLRC\LRC.pyc
So I have a problem that does not let me go any farther. I am not a particularly experienced python programmer, but have programmed in various languages for some time. Some help? Thanks.
russ_henselMon, 17 Feb 2014 14:11:05 -0600http://ask.sagemath.org/question/11052/Elliptic Curve Rational Field Height CODEhttp://ask.sagemath.org/question/9923/elliptic-curve-rational-field-height-code/Hi, I would like to know what does the function R() in lines 5060,5061 etc do. This is the code that I'm looking at:
http://hg.sagemath.org/sage-main/file/d06cf4b2215d/sage/schemes/elliptic_curves/ell_rational_field.py#l1
Thank you!BlackadderSun, 17 Mar 2013 08:14:07 -0500http://ask.sagemath.org/question/9923/Algorithm implementation in Sagehttp://ask.sagemath.org/question/9910/algorithm-implementation-in-sage/Input: Two undirected simple graphs G1 and G2, each having n vertices.
Output: `True` if G1 ? = G2; `False` otherwise.
for i ? 1,2 do
Ai ? adjacency matrix of Gi
pi ? permutation equivalence class of Ai
A0 i ? lexicographically maximal element of pi
if A0 1 = A0 2 then
return True
return False
anyone have any idea how to implement this in Sage ?MohabThu, 14 Mar 2013 00:22:04 -0500http://ask.sagemath.org/question/9910/How are list of matrices printed by sage?http://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/If I construct a list of matrices and let sage print it, the matrices are (correctly) aligned on the top:
sage: A=matrix([[1,2],[3,4]])
sage: [A,A]
[
[1 2] [1 2]
[3 4], [3 4]
]
Howewer, this is not the output of the __repr__ function of a list: compare:
sage: print [A,A].__repr__()
[[1 2]
[3 4], [1 2]
[3 4]]
I would like to construct an object whose output is on more than one lines (like a matrix), and I would like a list of these objects to be printed aligned on the top (like with the matrices in the first example above). How can I do that?
antoSun, 24 Feb 2013 05:03:22 -0600http://ask.sagemath.org/question/9837/Normalization (integral closure)http://ask.sagemath.org/question/7876/normalization-integral-closure/I tried doing some calculations that involve integral closures, but I seem to run into problems:
Say I define a ring as a quotient ring: for example the quotient ring $\mathbb{C}[x,y]/(y^3-x^2)$. Then if I use the integral_closure option, he rejects me. As far as I can tell this is because he treats quotient rings as Commutative Rings rather than, say, integral domains, and so it doesn't have the option of integral closures.
I'm sure there's a way to do such things. What is it? Is the idea that we have to apply some functor so that that ring would be treated as an object in a different category?OliverSat, 15 Jan 2011 14:17:17 -0600http://ask.sagemath.org/question/7876/LaTeX table from an HTML tablehttp://ask.sagemath.org/question/9514/latex-table-from-an-html-table/Dear all:
I've made an html.table in my SAGE session, and I'd like to "export" it as LaTeX code.
Is it possible?
Thank you.DoxThu, 08 Nov 2012 06:28:33 -0600http://ask.sagemath.org/question/9514/Navigating sage source codehttp://ask.sagemath.org/question/9429/navigating-sage-source-code/I'm thoroughly confused by the source code. I've downloaded and unpacked all the source code in spkg/standard and spkg/base, but what I get looks totally different from what I see when browsing the code online (http://hg.sagemath.org/sage-main/file/d06cf4b2215d/sage). Can someone please help me make sense of the source code? I just want to be able to navigate through the code that gets executed when some sage commands are invoked.
Thank you.rmp251Mon, 15 Oct 2012 07:20:54 -0500http://ask.sagemath.org/question/9429/looping newlinehttp://ask.sagemath.org/question/9343/looping-newline/I am running Sage 5.1 under Windows 7 using the Virtual Box image.
When I enter a gp program with a newline it goes into a loop.
This works:
%gp
lucas(p) ={my(u,q); u=4; q=1<<p - 1;for(k=3,p, u = (sqr(u)-2) % q);u == 0;}
lucas(5)
But this does not:
%gp
lucas(p) ={
my(u,q);
u=4;
q=1<<p - 1;
for(k=3,p, u = (sqr(u)-2) % q);
u == 0;
}
lucas(5)
Can you tell me what I need to do so that I can enter a program on multiple lines?
BrianSat, 22 Sep 2012 13:57:59 -0500http://ask.sagemath.org/question/9343/Submitting a module to Sage trac with supporting test code and documentationhttp://ask.sagemath.org/question/9097/submitting-a-module-to-sage-trac-with-supporting-test-code-and-documentation/Hi all:
I recently wrote
1. a new Python module for Sage
2. a Python module that tests (1) using the unittest module
3. a PDF document describing the mathematical background for the algorithms in (1)
I want to submit all three of these to the Sage trac server for reviewers to refer to. Is that a good idea? If so, what's the best way to do that? I know how to submit (1) as a patch, but what about (2) and (3)?
Thanks.araichevTue, 19 Jun 2012 18:38:05 -0500http://ask.sagemath.org/question/9097/Recursion oddityhttp://ask.sagemath.org/question/8468/recursion-oddity/I've been having a problem with a recursive factorization algorithm I've been writing in Sage code. The problem is reproduced on my machine using the code below:
def gcd_recur(num):
if(num <= 1):
return
g = gcd(15,num)
print "GCD of 15 and {0}: {1}".format(num, g)
gcd_recur(num/2)
gcd_recur(50)
The result I'm getting is:
GCD of 15 and 50: 5
GCD of 15 and 25: 1
GCD of 15 and 25/2: 1
GCD of 15 and 25/4: 1
GCD of 15 and 25/8: 1
GCD of 15 and 25/16: 1
However, I can run the code below outside of the algorithm and receive the correct result:
gcd(15, 25)
5
Are there some variable scoping issues that I'm not seeing here? Thanks.
BirdmanSat, 12 Nov 2011 15:04:37 -0600http://ask.sagemath.org/question/8468/Generation of optimized Fortran code?http://ask.sagemath.org/question/8134/generation-of-optimized-fortran-code/Hey All,
Is it possible to use Sage to generate Fortran code from its expressions which is optimized?
I'm looking for an OSS alternative to the CodeGeneration package of Maple.
I came across a discussion about it from 2008 where it was said that it is a planned feature but I didn't manage to find anything newer about that.
Many thanks,
OmriOmriWed, 08 Jun 2011 03:07:25 -0500http://ask.sagemath.org/question/8134/Is it possible to run over the isomorphism classes of groups?http://ask.sagemath.org/question/7914/is-it-possible-to-run-over-the-isomorphism-classes-of-groups/I want to check if a certain statement is correct about groups, but it is very hard to check it by hand (and even harder to prove). Is it possible to run over isomorphism classes of groups (for any specific cardinality), so as to check that statement?OliverSun, 30 Jan 2011 14:00:44 -0600http://ask.sagemath.org/question/7914/highlighting the code in notebookhttp://ask.sagemath.org/question/7871/highlighting-the-code-in-notebook/I wonder if there is any way to highlight the code (things like "def" "for") in the notebook.
I have found some informations about the css fileused by notebook but maybe someone could describe me the way if there is one.
Thanks.stanislavSat, 15 Jan 2011 04:12:42 -0600http://ask.sagemath.org/question/7871/Is it possible to use Matlab Code to control SAGE?http://ask.sagemath.org/question/7741/is-it-possible-to-use-matlab-code-to-control-sage/Hi,
i'm joining a course at University that requires me to use MatLab and although there is a Campus License available, I'd prefer to use an open source program. However, I'm required to turn in the exercise as compileable MatLab code. Browsing the web for alternatives, I found Sage and now I wonder if I can use "proper" MatLab code in Sage.
Thanks for the time you take to read and answer my question!
NeuneckNeuneckMon, 25 Oct 2010 06:34:59 -0500http://ask.sagemath.org/question/7741/