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.Tue, 13 Jul 2021 18:14:40 +0200How to multiply a list by an integer?https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/This is so basic I'm embarrassed to even ask the question, but I've tried all the possibilities I can think of, and searched two SM guides (SM Tutorial and SM for Undergrads), and I can't find out how to do this. I want to multiply a list of rational numbers (fractions) by an integer. I've tried with lists and sets (although sets wouldn't really work, because the list of fractions contains repetitions), and I've tried with the single integer as simply a number and as a list. But I always get an error message, saying "unsupported operand type(s)" or something similar. Here is an example of what I'm trying to do:
a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4,]
b = 840
print (a ^ b)
Help for a clueless newbie would be greatly appreciated. (Also, if anyone can recommend a tutorial or guide that will explain how to do these sorts of things in SM, that would be even better!)Jerry CaveneyTue, 13 Jul 2021 18:14:40 +0200https://ask.sagemath.org/question/57992/How to create lists of n-tuples efficiently?https://ask.sagemath.org/question/56756/how-to-create-lists-of-n-tuples-efficiently/The ultimate problem I'm trying to solve, is that given two integers *p* and *q*, I would like to create a list (say "*List1*"), whose entries are (all of the) lists of length *2q* which:
1) For all *L* in *List1*, the first *q* entries are between 0 and *pq-1* (inclusive), and the next *q* entries are between *pq* and *2pq-1* (inclusive)
2) For all *L* in *List1*, the entries are strictly increasing, that is for all *1<=i<=2pq*, *L[i] < L[i+1]*
I have a way of doing this which is very "brute force", where I first construct the list of lists of length 1, then the list of lists of length 2, etc.
I'm imagining (hoping) there exists some way for me to just iterate over all *2q* -tuples (without repetition) of numbers from 1 to *2pq* Something along the lines of
List1 = []
for x in 2q-tuples of [0, 1, 2, ..., 2pq-1]:
if x satisfies 1) and 2):
List1.append(x)
I suppose I would want to convert x to a list before appending to List1, or many there's some way to iterate over lists instead of tuples. That's not so important, I just want an efficient way to construct these lists.sum8tionWed, 21 Apr 2021 19:12:26 +0200https://ask.sagemath.org/question/56756/Obtaining incidence algebras for GAP via Sagehttps://ask.sagemath.org/question/56230/obtaining-incidence-algebras-for-gap-via-sage/Gap (via its package QPA) can obtain the incidence algebras of a given
connected poset `P` as a quiver algebra `KQ/I` (note that any
incidence algebra is isomorphic to the quiver algebra where `Q`
is the Hasse quiver and the relations I are generated by all
commutativity relations `w1-w2` where the paths `w1` and `w2`
start and end at the same points).
However, sadly GAP is very slow with this and obtaining the quiver algebra
for a poset with 40 or more points can take days or even weeks.
I wonder whether there is a way to use Sage to obtain
two lists of quiver and relations from a given poset
and then use those lists to input them in GAP to directly obtain
the quiver algebra in GAP (so that GAP has to do no computations
for the relations, which seems to be the main problem
although I'm not really sure why it takes so long).
Solving this problem would be very important to deal with
large posets in GAP (and one really needs the incidence algebra
as a quiver algebra in QPA to do homological algebra with it
as Sage has no such functions).
Here is an example, namely the strong Bruhat order of the
symmetric group $S_3$ with quiver and relations.
First here it is in Sage:
Y = posets.SymmetricGroupBruhatOrderPoset(3)
display(Y)
Now quiver and relations for GAP should look as follows:
Quiver(
["x123", "x132", "x213", "x231", "x312", "x321"],
[["x123", "x132", "x123_x132"],
["x123", "x213", "x123_x213"],
["x132", "x231", "x132_x231"],
["x132", "x312", "x132_x312"],
["x213", "x231", "x213_x231"],
["x213", "x312", "x213_x312"],
["x231", "x321", "x231_x321"],
["x312", "x321", "x312_x321"]])
[ x123_x132*x132_x231-x123_x213*x213_x231,
x123_x132*x132_x312-x123_x213*x213_x312,
x132_x231*x231_x321-x132_x312*x312_x321,
x213_x231*x231_x321-x213_x312*x312_x321 ]
Here the first entry is the quiver specified by the points
["x123", "x132", "x213", "x231", "x312", "x321"]
and by the arrows
[["x123", "x132", "x123_x132"],
["x123", "x213", "x123_x213"],
["x132", "x231", "x132_x231"],
["x132", "x312", "x132_x312"],
["x213", "x231", "x213_x231"],
["x213", "x312", "x213_x312"],
["x231", "x321", "x231_x321"],
["x312", "x321", "x312_x321"]]
that specify the Hasse diagram of the poset. Here we see that
a point is called for example `x132` , so we put an `x` before each
name of the point (here the point is named `132` in Sage).
An arrow is named for example `x132_x312` and the list entry
`["x132","x312","x132_x312"]` specifies that the arrow `x132_x312`
starts at the point `x132` and ends at the point `x312`.
The relations are then of the form for example `x123_x132*x132_x231-x123_x213*x213_x231`.
Note that we do not need really all relations of the form `w1-w2` in general
as some of those relations might be implied by some other.
So to make things faster it might be a good idea to find "minimal" relations,
but I am not quite sure how to do that.
As a test, it would be interesting if one can make things fast enough
to obtain for example the strong Bruhat order of $S_5$ (which has 120 points)
as a quiver algebra in GAP within 2 or 3 hours.
At the moment this takes more than a month with GAP.klaaaThu, 18 Mar 2021 18:43:09 +0100https://ask.sagemath.org/question/56230/Multiple intersection between listshttps://ask.sagemath.org/question/56058/multiple-intersection-between-lists/This is a toy example. Suppose I have a lot of lists (whose the number is not fixed as in the example and I want to determine the intersection of the list like
L=[["B", "C", "D", "E"],["A", "C", "D"],["A", "B","D", "E"], ["A", "B","D", "C"] ]
show(L)
interL1=[x for x in L[0] if x in L[1]]
show(interL1)
interL2=[x for x in L[0] if (x in L[1] and x in L[2])]
show(interL2)
interL3=[x for x in L[0] if (x in L[1] and x in L[2] and x in L[3])]
show(interL2)
But, as the number of list (here 4), I would like to know if there is a possibility to define an operator `and` like `and(L[i],i,0, 3)`CyrilleMon, 08 Mar 2021 18:17:54 +0100https://ask.sagemath.org/question/56058/Finding an involution with Sagehttps://ask.sagemath.org/question/55622/finding-an-involution-with-sage/I have four lists L1,LL1 and L2, LL2 and a bijection G from LL1 to LL2. LL1 is a sublist of L1 and LL2 a sublist of L2.
For example they look like this:
L1=[ [ [ 2, 2, 2, 1 ], 4 ], [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ], [ [ 3, 3, 2, 1 ], 3 ], [ [ 4, 3, 2, 1 ], 2 ] ]
LL1=[ [ [ 2, 2, 2, 1 ], 4 ], [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ], [ [ 4, 3, 2, 1 ], 2 ] ]
and
L2=[ [ [ 2, 2, 2, 1 ], 2 ], [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ], [ [ 3, 3, 2, 1 ], 3 ], [ [ 4, 3, 2, 1 ], 4 ] ]
LL2=[ [ [ 2, 2, 2, 1 ], 2 ], [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ], [ [ 4, 3, 2, 1 ], 4 ] ]
The bijection G looks like this:
[ [ [ [ 2, 2, 2, 1 ], 4 ], [ [ 4, 3, 2, 1 ], 4 ] ], [ [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ] ], [ [ [ 2, 3, 2, 1 ], 3 ], [ [ 3, 2, 2, 1 ], 3 ] ],
[ [ [ 4, 3, 2, 1 ], 2 ], [ [ 2, 2, 2, 1 ], 2 ] ] ]
Thus an element in such a list is a tuple of the form [ [ 2, 2, 2, 1 ], 4 ] where [2,2,2,1] correponds to some algebraic object and the second entry is number. L1 and L2 will always have the same number of elements and LL1 and LL2 will have the same number of elements. The bijection G is given also as a list, where an entry such as
[ [ [ 2, 2, 2, 1 ], 4 ], [ [ 4, 3, 2, 1 ], 4 ] ]
means of course that G maps [ [ 2, 2, 2, 1 ], 4 ] to [ [ 4, 3, 2, 1 ], 4 ] .
Im searching for bijections F between L1 and L2 that satisfy the following three conditions:
1. F is an involution, that is $F^2=id$.
2. F preserves the number.
3. F restricted to LL1 maps to LL2 via the given involution G.
In the above example F is uniquely given as follows:
[ [ [ [ 2, 2, 2, 1 ], 4 ], [ [ 4, 3, 2, 1 ], 4 ] ], [ [ [ 3, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 1 ], 3 ] ], [ [ [ 2, 3, 2, 1 ], 3 ], [ [ 3, 2, 2, 1 ], 3 ] ],
[ [ [ 4, 3, 2, 1 ], 2 ], [ [ 2, 2, 2, 1 ], 2 ] ],[ [ [ 3, 3, 2, 1 ], 3 ], [ [ 3, 3, 2, 1 ], 3 ] ] ]
Here is another example where the lists are larger:
L1=[ [ [ 2, 2, 2, 2, 1 ], 5 ], [ [ 3, 2, 2, 2, 1 ], 4 ], [ [ 2, 3, 2, 2, 1 ], 3 ], [ [ 3, 3, 2, 2, 1 ], 4 ], [ [ 4, 3, 2, 2, 1 ], 3 ],
[ [ 2, 2, 3, 2, 1 ], 4 ], [ [ 3, 2, 3, 2, 1 ], 3 ], [ [ 2, 3, 3, 2, 1 ], 4 ], [ [ 3, 3, 3, 2, 1 ], 4 ], [ [ 4, 3, 3, 2, 1 ], 3 ], [ [ 2, 4, 3, 2, 1 ], 3 ], [
[ 3, 4, 3, 2, 1 ], 3 ],
[ [ 4, 4, 3, 2, 1 ], 3 ], [ [ 5, 4, 3, 2, 1 ], 2 ] ]
LL1=[ [ [ 2, 2, 2, 2, 1 ], 5 ], [ [ 3, 2, 2, 2, 1 ], 4 ], [ [ 2, 3, 2, 2, 1 ], 3 ], [ [ 4, 3, 2, 2, 1 ], 3 ], [ [ 2, 2, 3, 2, 1 ], 4 ],
[ [ 3, 2, 3, 2, 1 ], 3 ], [ [ 2, 4, 3, 2, 1 ], 3 ], [ [ 5, 4, 3, 2, 1 ], 2 ] ]
L2=[ [ [ 2, 2, 2, 2, 1 ], 2 ], [ [ 3, 2, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 2, 1 ], 3 ], [ [ 3, 3, 2, 2, 1 ], 3 ], [ [ 4, 3, 2, 2, 1 ], 4 ],
[ [ 2, 2, 3, 2, 1 ], 3 ], [ [ 3, 2, 3, 2, 1 ], 3 ], [ [ 2, 3, 3, 2, 1 ], 3 ], [ [ 3, 3, 3, 2, 1 ], 3 ], [ [ 4, 3, 3, 2, 1 ], 4 ], [ [ 2, 4, 3, 2, 1 ], 4 ], [
[ 3, 4, 3, 2, 1 ], 4 ],
[ [ 4, 4, 3, 2, 1 ], 4 ], [ [ 5, 4, 3, 2, 1 ], 5 ] ]
LL2=[ [ [ 2, 2, 2, 2, 1 ], 2 ], [ [ 3, 2, 2, 2, 1 ], 3 ], [ [ 2, 3, 2, 2, 1 ], 3 ], [ [ 4, 3, 2, 2, 1 ], 4 ], [ [ 2, 2, 3, 2, 1 ], 3 ],
[ [ 3, 2, 3, 2, 1 ], 3 ], [ [ 2, 4, 3, 2, 1 ], 4 ], [ [ 5, 4, 3, 2, 1 ], 5 ] ]
G=[ [ [ [ 2, 2, 2, 2, 1 ], 5 ], [ [ 5, 4, 3, 2, 1 ], 5 ] ], [ [ [ 3, 2, 2, 2, 1 ], 4 ], [ [ 2, 4, 3, 2, 1 ], 4 ] ], [ [ [ 2, 3, 2, 2, 1 ], 3 ], [ [ 3, 2, 3,
2, 1 ], 3 ] ],
[ [ [ 4, 3, 2, 2, 1 ], 3 ], [ [ 2, 2, 3, 2, 1 ], 3 ] ], [ [ [ 2, 2, 3, 2, 1 ], 4 ], [ [ 4, 3, 2, 2, 1 ], 4 ] ], [ [ [ 3, 2, 3, 2, 1 ], 3 ], [ [ 2, 3, 2, 2, 1
], 3 ] ],
[ [ [ 2, 4, 3, 2, 1 ], 3 ], [ [ 3, 2, 2, 2, 1 ], 3 ] ], [ [ [ 5, 4, 3, 2, 1 ], 2 ], [ [ 2, 2, 2, 2, 1 ], 2 ] ] ]
There might be many F satisfying the three conditions.
Thus my question: Is there an easy method to find all such bijections F satisfying the three conditions when L1,LL1,L2,LL2 and G are given?klaaaMon, 08 Feb 2021 11:13:39 +0100https://ask.sagemath.org/question/55622/A strange behavior in passing from list to sethttps://ask.sagemath.org/question/54797/a-strange-behavior-in-passing-from-list-to-set/I find it a little puzzling that while the following code:
cand = ["A","B","C","D"]
show(cand)
len(cand)
displays the natural order `A, B, C, D`, this little variation:
Scand = Set(cand) # La majuscule est importante
show(Scand)
changes the order to `B, D, A, C`.
This is disturbing since when one later asks for `Arrangements`,
the order is not the natural one, which is highly perturbing
given the high number of cases.CyrilleMon, 21 Dec 2020 16:53:01 +0100https://ask.sagemath.org/question/54797/How to change/set variables?https://ask.sagemath.org/question/46945/how-to-changeset-variables/I created a list of formal variables that I want to work with under addition, multiplication etc. in the following way:
g = list(var('g_%d' % i) for i in rang(4))
Specifically I'm working with differential forms
M = Manifold(4, 'M', field='complex')
U = M.open_subset('U')
c_xyXY<x, y, X, Y>=U.chart()
eU = c_xyXY.frame()
d = [[M.diff_form(2, name='d_{}{}'.format(j, i)) for i in range(2)] for j in range(2)]
d[0][0] [eU, 2, 0] = g[0]
d[0][1] [eU, 3, 0] = g[1]
d[1][0] [eU, 2, 1] = g[2]
d[1][1] [eU, 3, 1] = g[3]
I then want to set the variables to something specific like
g[0] = x^2
but when I try to display this I get
d[0][0].display(eU)
d_00 = -g_0dx/\dX
What I want is
d_00 = -x^2dx/\dX
EDIT: The question as originally asked has been sufficiently answered by eric_g below. However I'm still having difficulty with a more complicated version of the same set-up, which I present here:
https://ask.sagemath.org/question/46993/update-how-to-changeset-variables/sum8tionWed, 19 Jun 2019 19:57:49 +0200https://ask.sagemath.org/question/46945/Working with sums/products of listshttps://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/I need to work symbolically with expressions such as this
$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$
where $x$ would be a random sample of size $n$.
I don't know of any way to express the indexing of the sample $x$ by each element...
The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function
var('a','b','x','n','i')
assume(x>0,a>0,b>0,i>0,n>0)
X = function('X',nargs=1)
L = product(a*b*X(i)^(a-1)*(1-X(i)^a)^(b-1), i, 1, n)
But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:
$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$
I don't know how to deal with this expression, and it seems to me like it should be straight forward.
In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.galioSat, 05 Jan 2019 15:23:28 +0100https://ask.sagemath.org/question/44906/Working with sums/products of listshttps://ask.sagemath.org/question/44905/working-with-sumsproducts-of-lists/I need to work symbolically with expressions such as this
$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$
where $x$ would be a random sample of size $n$.
I don't know of any way to express the indexing of the sample $x$ by each element...
The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function
var('a','b','x','n','i')
assume(x>0,a>0,b>0,i>0,n>0)
X = function('X',nargs=1)
L = product(a*b*X(i)^(a-1)*(1-X(i)^a)^(b-1), i, 1, n)
But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:
$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$
I don't know how to deal with this expression, and it seems to me like it should be straight forward.galioSat, 05 Jan 2019 15:20:32 +0100https://ask.sagemath.org/question/44905/Print symbolic variables like a_1 in A[1] stylehttps://ask.sagemath.org/question/43838/print-symbolic-variables-like-a_1-in-a1-style/Because of Sagemath's problem with symbolic arrays, I have defined my vectors like this:
A =[0 for j in range(4)]
for j in range(4):
A[j]=var('a_'+str(j))
I have some symbolic variables stored in another variable. Lets say I have a subroutine that works with `A[i]` and in the end, `L` becomes something such as: `L=a_1+a_2*a_3`. When I print `L`, I want to have it printed in the original vector format. So when I type `L` or `print(L)` in a cell and press enter, my desired output is `A[1]+A[2]*A[3]` and not `a_1+a_2*a_3`. I want this type of output because I am transferring SageMath outputs to C where I employ indexed arrays. How can I achieve this? DanialBaghSat, 06 Oct 2018 05:30:25 +0200https://ask.sagemath.org/question/43838/subs a list with another listhttps://ask.sagemath.org/question/43571/subs-a-list-with-another-list/ I define
x = [var('x_%d' % k) for k in range(p)]
y = [var('y_%d' % k) for k in range(p)]
z =x + y
PRz = PolynomialRing(QQ,z)
with $p$ arbitrary, then I need new variables, to comare polynomials, define as:
t = [z[k] + 1/z[k] for k in range(p)]
Is there a way to substitute the list of variables $z$ with the list of varialbes $t$? The only way that I could do it was as
polynomial.subs(y_0 = t[0]/2, y_1 = t[1]/2, y_2 = t[2]/2, y_3 = t[3]/2)
But I want it for a general number of variables.
ThanksMarioMTue, 04 Sep 2018 06:33:34 +0200https://ask.sagemath.org/question/43571/graphs.planar_graphshttps://ask.sagemath.org/question/42414/graphsplanar_graphs/So I'm a bit confused by the documentation of graphs.planar_graphs I thought it would just generate a list of planar graphs so I don't know why the following code doesn't work (and also why I need to do list(l) )
l = graphs.planar_graphs(4, dual=False)
gen=list(l) #don't know why this is necessary
k=len(gen)
ii=1
G=gen[ii]
p=G.plot()
p.show()standardtrickynessThu, 24 May 2018 00:10:38 +0200https://ask.sagemath.org/question/42414/Evaluating a list of multivariate equations at random values.https://ask.sagemath.org/question/41662/evaluating-a-list-of-multivariate-equations-at-random-values/I have a list of multivariate equations, there are `o` multivariate equations in the list where `o` is an integer and `v = 2o`, the variables that make up the equations are the generators of `P = PolynomialRing(K,'x',n)` where `n = v + o` so for example if `o = 2` then `v = 4` and `n = 6` so the variables that make up the multivariate equations in the list would be `P.gens() = (x0, x1, x2, x3, x4, x5)`. Now i need to substitute in random values from `GF(q)` for the first `v` variables in each of the multivariate equations in the list so in the example above where `v = 4` random values from `GF(q)`would be substituted in for the variables `x0, x1, x2, x3`in each of the multivariate equations in the list, leaving equations in only two variables `x4 and x5`. I have tried to implement this but i don't really know where to start as i know i am doing something wrong but what i have is the following. If the list of multivariate equations is called `eqn` for example.
for i in range(v):
xi = var('x'+str(i))
eqnsub0 = eqn[i].subs({x[i]:GF(q).random_element()})
I already know this is very wrong as the indexing will already go out of bounds since `v > o` but i don't really know where to start with this. Any help would be great, thanks. DalvirTue, 20 Mar 2018 22:51:34 +0100https://ask.sagemath.org/question/41662/Listing fractionshttps://ask.sagemath.org/question/40928/listing-fractions/Hi, I would like to request help in listing rational numbers. For motivation, the command
> [x for x in IntegerRange(1,10)]
outputs the integers from 1 through 9. Is there a similar command for listing out rational numbers a/b with a and b in the range (x1,y1) and (x2,y2), respectively.
For instance, if a is in range (1,3) and b is in (4,6), I want to return
> [1/4, 1/5, 1/2, 2/5]
On a related note, how can one impose a gcd(x,y)==1 in the following list?
> [(x,y) for x in IntegerRange(1,10) for
> y in IntegerRange(1,10)]ajbFri, 02 Feb 2018 05:03:13 +0100https://ask.sagemath.org/question/40928/unpacking list in a listhttps://ask.sagemath.org/question/40575/unpacking-list-in-a-list/ Hi, I'm trying to convert a sympy code to sage.
I have found that the following does not work:
sector1 = [1,2,3]
sector2 = [4,5,6]
newlist = [*sector1, sector2]
even though the unpacking works for functions (ex F(*sector1)).
How do I do that?LAVThu, 11 Jan 2018 22:16:35 +0100https://ask.sagemath.org/question/40575/How to obtain all finite connected distributive lattices with SAGEhttps://ask.sagemath.org/question/38878/how-to-obtain-all-finite-connected-distributive-lattices-with-sage/In https://ask.sagemath.org/question/38865/obtaining-all-posets-in-a-certain-form-with-sage/ I asked for a programm that translates all finite posets into a given form.
Now I want to do the same with all finite connected distributive lattices instead of all connected posets.
First I wanted it with all lattices, I changed the code
posets = [ p for p in Posets(n) if p.is_connected() ]
into posets = [ p for p in Posets(n) if p.is_connected() and p.is_lattice]
This worked for me. But if I try to obtain now in addition all distributive lattices via the command is_distributive() (found here: http://doc.sagemath.org/html/en/reference/combinat/sage/combinat/posets/lattices.html#) I get an error when I try
into posets = [ p for p in Posets(n) if p.is_connected() and p.is_lattice() and p.is_distributive()]. How to fix that?
And is there a way to obtain all connected finite posets with n points having a global maximum and a global minimum?
Another question: I want to use the SAGE output of this to define a list in GAP. The problem is that I cannot directly copy paste since GAP allows me to define a list only in case the list occupies only one line.
(for example
[ [["x1", "x2", "x3"], [["x1", "x2"], ["x1", "x3"]] ],
[["x1", "x2", "x3"], [["x1", "x2"], ["x2", "x3"]]],
[["x1", "x2", "x3"], [["x1", "x3"], ["x2", "x3"]]]]
is not ok for GAP but [ [["x1", "x2", "x3"], [["x1", "x2"], ["x1", "x3"]] ], [["x1", "x2", "x3"], [["x1", "x2"], ["x2", "x3"]]], [["x1", "x2", "x3"], [["x1", "x3"], ["x2", "x3"]]]] is ok when it is in one line).
Is there a way to obtain the SAGE output in one line or is there a textmanager that can do such a thing? (this is a problem in case the SAGE output is very long)
edit: For my second problem I found the website https://www.textfixer.com/tools/remove-line-breaks.php to remove line breaks in text. I wonder if there is a basic way using texteditors without the internet.sagequstionsWed, 20 Sep 2017 19:59:36 +0200https://ask.sagemath.org/question/38878/How do I define a list of products of matrices from a given list?https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/I'm trying to define a list of products of matrices, of a set length, from another list of matrices. This how I've tried to do it (using simplified data and length 2).
a = matrix(ZZ,[[1,0],[0,1]])
b = matrix(ZZ,[[0,1],[1,0]])
c = matrix(ZZ,[[1,1],[0,1]])
M=[a,b,c]
S=[X*Y for X, Y in M]
S
The output I get is
[0, 0, 1]
Obviously this is not the list of desired products, moreover it is not a list of matrices, and the list has 3 items rather than the 6 possible things that should occur.
Beyond this simple example,
I'd also like to be able to do this with longer products and more complicated initial lists.
This probably has a simple answer (I am a beginner). My next question though would be, how could I define a function that inputs some $l\in\mathbb{N}$
and outputs the list of products of that length?j0equ1nnMon, 19 Oct 2015 23:45:15 +0200https://ask.sagemath.org/question/30124/creating list n many timeshttps://ask.sagemath.org/question/29935/creating-list-n-many-times/ How to define n many empty lists where n is the number of vertices of the given arbitrary graph.
Thanks in advance.GA316Mon, 12 Oct 2015 15:45:30 +0200https://ask.sagemath.org/question/29935/List indexed by (signed) integers?https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/I've been doing some work in SAGE with a linear operator on a polynomial algebra T: F[x] --> F[x] (here F is some field, x is a variable, and T is the operator), storing the data of T as a list, so that T[n] keeps track of T(x^n). Here n is a nonnegative integer, of course. This has been worked pretty well for me.
Now I would like to do similar computations in a space where x is invertible. That is, I now have a linear operator T: F[x,x^{-1}] --> F[x, x^{-1}]. I would love to store the data of T as a signed-integer-indexed list, so that T[n] again keeps track of T(x^n), but now n can be any (positive or negative) integer.
Is there a way to implement this easily in Python/SAGE? Right now I am dragging T around as a pair of lists, T1[n] := T(x^n) and T2[n] := T(x^{-n}). This is annoying and inelegant and inefficient (I constantly have to take three cases -- n positive, negative, or zero), but it does work. But is there something better out there?
I am not a good programmer in any sense, and I won't have time to work very hard on this. But I hope that I am missing something clean and simple. Thanks in advance for any help. sibilantThu, 30 Jul 2015 22:23:40 +0200https://ask.sagemath.org/question/28726/Testing for list membershiphttps://ask.sagemath.org/question/27014/testing-for-list-membership/ I'd like to adopt Sage in my teaching this fall, so I've started to
play around with some basic manipulations that I would expect my
students to be able to do. I am an absolute beginner in Sage and Python.
Suppose that I want all the partitions of 6 that contain the number "2".
My first attempt failed:
L=Partitions(6).list(); L
[[6], [5, 1], [4, 2], [4, 1, 1], [3, 3], [3, 2, 1], [3, 1, 1, 1], [2, 2, 2],
[2, 2, 1, 1], [2, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
[j for j in L if j.count(2)>0]
>"Error in lines 1-1\nTraceback (most recent call last):\n
File \"/projects/6313dd6a-b1df-46ed-885a-a1bdf71f3fc3/.sagemathcloud/sage_server.py\", line 879,
in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1,
in <module>\n File \"sage/structure/element.pyx\", line 431,
in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4644)\n
return getattr_from_other_class(self, P._abstract_element_class, name)\n File \"sage/structure/misc.pyx\",
line 253, in sage.structure.misc.getattr_from_other_class (build/cythonized/sage/structure/misc.c:1582)\n
raise dummy_attribute_error\nAttributeError: 'Partitions_n_with_category.element_class' object has
no attribute 'count'\n"
However, if I create a new list E by copying and pasting the above, it works fine.
E=[[6], [5, 1], [4, 2], [4, 1, 1], [3, 3], [3, 2, 1], [3, 1, 1, 1], [2, 2, 2],
[2, 2, 1, 1], [2, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
[j for j in E if j.count(2)>0]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]
Is there a better way to do this, without copying and pasting??schmuThu, 04 Jun 2015 00:49:37 +0200https://ask.sagemath.org/question/27014/How to plot the output of A.eigenvectors_right()?https://ask.sagemath.org/question/24042/how-to-plot-the-output-of-aeigenvectors_right/ Trying to calculate and plot eigenvectors of a matrix A.
The call A.eigenvectors_right() works, but the output is a mixed list. How to extract the
eigenvectors from it to plot them?
The attempt: s=Lambda.eigenvectors_right();v=s[1];v[1];v1=v[1];plot(v1) gives
an error message...
Thank you. DVDMon, 08 Sep 2014 06:06:34 +0200https://ask.sagemath.org/question/24042/How reverse a list with Sage?https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/See on http://www.sagemath.org/doc/thematic_tutorials/tutorial-programming-python.html
From the section "Modifying lists: reverse, sort, ...":
L = [4,2,5,1,3]
L.reverse()
sage: [3, 1, 5, 2, 4]
However I see -- nothing.
print L.reverse()
gives 'None'. I am using Sage 5.8.petropolisFri, 02 Aug 2013 05:42:57 +0200https://ask.sagemath.org/question/10402/Outputing lists without brackets in SageTeXhttps://ask.sagemath.org/question/10292/outputing-lists-without-brackets-in-sagetex/Hi,
how can I output a list in SageTeX without it being enclosed in brackets?
Consider for example:
\begin{sagesilent}
A = [ 1, 2, 4 ]
\end{sagesilent}
and in the text
... $A = \{ \sage{A} \}$
produces $A = \\{ \left[1, 2, 4\right] \\}$ instead of $\\{ 1,2,4 \\}$. Or a more realistic example:
\begin{sagesilent}
R.<x,y> = PolynomialRing(QQ, 'x,y', order='lex')
I = R.ideal(x^2+1, y*x-y)
\end{sagesilent}
and know somewhere in the text I want to put
The Groebner basis of $I$ is $\{ \sage{I.groebner_basis()} \}$
but the result is: $\\{ \left[x^{2} + 1, y\right] \\}$ rather then expected $\\{ x^{2} + 1, y \\}$pkoprowskiThu, 27 Jun 2013 08:37:27 +0200https://ask.sagemath.org/question/10292/Partition list satisfying conditionshttps://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/Hi, I am looking for a function which will partition a list. For a simple example, suppose I have the list `[1,2,3,4,5,6]`. Suppose I want all the even integers in one list and the odd integers in another. Is there a function that can do this, and perhaps yeild something like `[[1,3,5],[2,4,6]]`? In this case, the number of partitions is 2, so it is fairly easy, but in my problem the number is not known. In mathematica, the function I want is called GatherBy.sjcavazos2Sun, 24 Mar 2013 18:35:39 +0100https://ask.sagemath.org/question/9944/why cannot recalculate?https://ask.sagemath.org/question/8823/why-cannot-recalculate/These code work fine
html("<h1> From Dictionary to Matrix (1)</h1>")
@interact
def newtarph( D=input_box(default={0:{1:3}, 1: {0: 2, 4: 2, 5: 2}, 2: {0: 3, 1: 4}}, label='Dictionary')):
E=[]
for u in D.keys():
for i in range(len(D.values()[u])):
E.append([u, D.values()[u].keys()[i], D.values()[u].values()[i]])
print matrix(E)
These other code d'ont work:
html("<h1> From Dictionary to Matrix (1)</h1>")
@interact
def newtarph( D=input_box(default={1: {0: 2, 4: 2, 5: 2}, 2: {0: 3, 1: 4}}, label='Dictionary')):
E=[]
for u in D.keys():
for i in range(len(D.values()[u])):
E.append([u, D.values()[u].keys()[i], D.values()[u].values()[i]])
print matrix(E)
canisvetusSat, 24 Mar 2012 08:21:07 +0100https://ask.sagemath.org/question/8823/Swap coordinates of list elementshttps://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/Suppose I have list like
L = [(2,3), (4,5), (6,7)]
What's the easiest way to transform it to this list:
L'=[(3,2),(5,4),(7,5)]
i.e. swap the coordinates in each entry.sagefanWed, 08 Feb 2012 10:41:42 +0100https://ask.sagemath.org/question/8702/join lists?https://ask.sagemath.org/question/8696/join-lists/Hello! Is there a way to join two lists? For example,
v=[1,2,3];
w=[4,5,6];
how can I obtain the list z=[1,2,3,4,5,6]?
Thank you very much,
FrancescoFrancescoFri, 03 Feb 2012 17:21:13 +0100https://ask.sagemath.org/question/8696/