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.Sat, 05 Jan 2019 08:23:28 -0600Working with sums/products of listshttp://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 08:23:28 -0600http://ask.sagemath.org/question/44906/Working with sums/products of listshttp://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 08:20:32 -0600http://ask.sagemath.org/question/44905/Print symbolic variables like a_1 in A[1] stylehttp://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? DanialBaghFri, 05 Oct 2018 22:30:25 -0500http://ask.sagemath.org/question/43838/subs a list with another listhttp://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.
ThanksMarioMMon, 03 Sep 2018 23:33:34 -0500http://ask.sagemath.org/question/43571/graphs.planar_graphshttp://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()standardtrickynessWed, 23 May 2018 17:10:38 -0500http://ask.sagemath.org/question/42414/Evaluating a list of multivariate equations at random values.http://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 16:51:34 -0500http://ask.sagemath.org/question/41662/Listing fractionshttp://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)]ajbThu, 01 Feb 2018 22:03:13 -0600http://ask.sagemath.org/question/40928/unpacking list in a listhttp://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 15:16:35 -0600http://ask.sagemath.org/question/40575/How to obtain all finite connected distributive lattices with SAGEhttp://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 12:59:36 -0500http://ask.sagemath.org/question/38878/How do I define a list of products of matrices from a given list?http://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 16:45:15 -0500http://ask.sagemath.org/question/30124/creating list n many timeshttp://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 08:45:30 -0500http://ask.sagemath.org/question/29935/List indexed by (signed) integers?http://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 15:23:40 -0500http://ask.sagemath.org/question/28726/Testing for list membershiphttp://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??schmuWed, 03 Jun 2015 17:49:37 -0500http://ask.sagemath.org/question/27014/How to plot the output of A.eigenvectors_right()?http://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. DVDSun, 07 Sep 2014 23:06:34 -0500http://ask.sagemath.org/question/24042/How reverse a list with Sage?http://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.petropolisThu, 01 Aug 2013 22:42:57 -0500http://ask.sagemath.org/question/10402/Outputing lists without brackets in SageTeXhttp://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 01:37:27 -0500http://ask.sagemath.org/question/10292/Partition list satisfying conditionshttp://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 12:35:39 -0500http://ask.sagemath.org/question/9944/why cannot recalculate?http://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 02:21:07 -0500http://ask.sagemath.org/question/8823/Swap coordinates of list elementshttp://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 03:41:42 -0600http://ask.sagemath.org/question/8702/join lists?http://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 10:21:13 -0600http://ask.sagemath.org/question/8696/