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.Fri, 12 Aug 2022 13:52:50 +0200Looping around self defined indiceshttps://ask.sagemath.org/question/63646/looping-around-self-defined-indices/How can we effectively shorten the following piece of code:
g=graphs.CirculantGraph(8,[1,2])
v=g.vertices()
v1=set(v)-set([0,4])
v2=set(v)-set([1,5])
v3=set(v)-set([2,6])
v4=set(v)-set([3,7])
v5=set(v)-set([0,5])
v6=set(v)-set([1,4])
v7=set(v)-set([2,7])
v8=set(v)-set([3,6])
g1=g.subgraph(v1)
g2=g.subgraph(v2)
g3=g.subgraph(v3)
g4=g.subgraph(v4)
g5=g.subgraph(v5)
g6=g.subgraph(v6)
g7=g.subgraph(v7)
g8=g.subgraph(v8)
Like, I wish to loop on the indices `g(i)`. Instead of writing repeatedly `g1=subgraph(v1)` and so on, is there a way to loop arounfd the self defined index `g(i)`? Thanks beforehand.vidyarthiFri, 12 Aug 2022 13:52:50 +0200https://ask.sagemath.org/question/63646/Graph plotting not working properlyhttps://ask.sagemath.org/question/62646/graph-plotting-not-working-properly/Consider the following code:
def Sier(g):
h = Graph()
for i in range(1, g.order() + 1):
perm = {u: (i, u) for u in g}
gg = g.relabel(perm=perm, inplace=False)
h.add_edges(gg.edges())
return h
f = Sier(graphs.CompleteGraph(4))
v = f.vertices()
n1 = [((a, b), (b, a)) for a, b in v]
n2 = [(a, a), (a, a)) for a, b in v]
x = set(n1) - set(n2)
z = f.add_edges(x)
z.plot()
The plot function does not give the graph I desire.
The `size()` function gives the size correctly.
But the diagram is incorrect. Why is this?
Any workarounds to make it precise?vidyarthiFri, 27 May 2022 17:03:29 +0200https://ask.sagemath.org/question/62646/Looping with function having an object as a parameterhttps://ask.sagemath.org/question/62434/looping-with-function-having-an-object-as-a-parameter/ Consider the following code:
def Sier(H):
for i in list(range(1,H.order())):
H.disjoint_union(H)
return H
f=Sier(graphs.CompleteGraph(5))
f.order()
I get the output as 5.
But, what I actually wanted was:
def Sier(n,k):
l=list(range(1,k))
g=graphs.CompleteGraph(n)
for i in l:
g=g.disjoint_union(graphs.CompleteGraph(n))
return g
k=Sier(5,5)
k.order()
which gives me the correct output as 25. I ask as to what is the problem with the first code, like how to get $k$ disjoint copies of a graph $H$ with order $k$?vidyarthiFri, 13 May 2022 20:16:10 +0200https://ask.sagemath.org/question/62434/Looping in graphshttps://ask.sagemath.org/question/62418/looping-in-graphs/ When I run the code:
l=[1,2,3,4,5]
s=graphs.CompleteGraph(1)
for i in l:
t=s.disjoint_union(graphs.CompleteGraph(i+1))
t.order()
I get the output as $7$, which not as the expected $21$. How do I rectify this? How to loop over graphs? Any hints? Thanks beforehand.vidyarthiFri, 13 May 2022 08:42:20 +0200https://ask.sagemath.org/question/62418/other way of creating a list of perfect squareshttps://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/ Hi there! I am currently explaining a certain exercise to my buddy and part of it consists in creating a list of perfect squares up to root of 64 (included) therefore the list has to look like this [0, 1, 4, 9, 16, 25, 36, 49, 64]
My first idea was this
squares=[i*i for i in range(sqrt(64)+1)]
which works but I am now trying to do it in a way so that is not compressed such us
for i in range(sqrt(64)+1)
squares=[i*i]
print(squares)
But I keep getting this error
for i in range(sqrt(Integer(64))+Integer(1))
^
SyntaxError: invalid syntax
Any ideas of what I am doing wrong?
jhonvi2Thu, 07 Oct 2021 12:34:38 +0200https://ask.sagemath.org/question/59269/While loop with recording intermediate stepshttps://ask.sagemath.org/question/55273/while-loop-with-recording-intermediate-steps/Sorry for a certainly too long code but the problem is in the `while` the loop
def All_pref(cand=["A","B","C","D"],code=1) :
ncand=len(cand)
Scand=sorted(Set(cand))
all_pref=Arrangements(Scand,ncand).list()
all_pref1=[str(Word(x)) for x in all_pref]
if code==1 :
return ncand
if code==2 :
return all_pref1
cand=["A","B","C","D"]
Ap=All_pref(cand,2)
ne=[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5]
alf=[list(build_alphabet(x)) for x in Ap]
show(alf)
resultats={}
alfc={}
iteration=0
while len(alf[0])>0:
show(alf)
resultats[iteration]={}
premier_par_pref =[[ne[i] if alf[i].index(x)==0 else 0 for i in range(len(alf))] for x in cand]
nb_fois_premier_par_pref = [add(x) for x in premier_par_pref]
show(nb_fois_premier_par_pref)
min_nb_fois_premier_par_pref = min(nb_fois_premier_par_pref)
index_min_nb_fois_premier_par_pref = nb_fois_premier_par_pref.index(min_nb_fois_premier_par_pref)
cand_el=cand[index_min_nb_fois_premier_par_pref]
show(cand_el)
resultats[iteration]["elimine"]=cand_el
alfc[iteration]=alf.copy()
show(alfc[iteration])
resultats[iteration]["liste"]=alfc[iteration]
[x.remove(cand_el) for x in alf]
cand.remove(cand_el)
iteration+=1
show(resultats)
if you look at the final result it is empty. Where do I fail ?CyrilleThu, 14 Jan 2021 18:44:22 +0100https://ask.sagemath.org/question/55273/infinite if inside a loophttps://ask.sagemath.org/question/54852/infinite-if-inside-a-loop/I want to define a function that checks if a member of a set is another then print "well".
Let me explain it it the following example:
Assume that `Z = Compositions(17, min_part=4).list()`, `L` is some set and `f` is some function.
How I can write a function such that if `f(a)`, where `a` is a member of `Z`, was in `L`, then that is "well"?
My attempt:
for i in in srange(len(Z)):
for j in srange(i):
if f(Z[i][j]) in L:
I don't know how to finish it because I just know finite `if, else`, not more.Blue skyTue, 22 Dec 2020 22:22:45 +0100https://ask.sagemath.org/question/54852/error while assigning variableshttps://ask.sagemath.org/question/49155/error-while-assigning-variables/ Hi. I have the following coding of iteration where I want to find the values of u[1,1], u[1,2], u[1,3] and u[1,4].
u,t,x = var('u t x')
u[1]=x;u[1]
u[2]=x^2-1;u[2]
u[3]=2*x^2+3;u[3]
u[4]=x^3-5;u[4]
g[1]=-x^3-x;g[1]
g[2]=(1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1;g[2]
g[3]=(1/2)*x^6-(31/6)*x^3+2*x^2+3;g[3]
g[4]=x^3-5;g[4]
K2[1,1]=1;K2[1,1]
K2[1,2]=0;K2[1,2]
K2[1,3]=1;K2[1,3]
K2[1,4]=1;K2[1,4]
K2[2,1]=x-1;K2[2,1]
K2[2,2]=t;K2[2,2]
K2[2,3]=0;K2[2,3]
K2[2,4]=-x;K2[2,4]
K2[3,1]=x-t;K2[3,1]
K2[3,2]=0;K2[3,2]
K2[3,3]=0;K2[3,3]
K2[3,4]=-3*t^2;K2[3,4]
K2[4,1]=2*x-3;K2[4,1]
K2[4,2]=0;K2[4,2]
K2[4,3]=0;K2[4,3]
K2[4,4]=0;K2[4,4]
for i in range(1,4):
u[i,1]=-h*sum(integrate(K2[i,j]*g[j]),j,1,4);u[i,1]
for i in range(1,4):
u[i,2]=(1+h)*u[i,1]-h*sum(integrate(K2[i,j]*u[j,1]),j,1,4);u[i,2]
But it gave me an error saying that: 'sage.symbolic.expression.Expression' object does not support item assignment Can someone explain to me what I might be doing wrong here.ShaTue, 24 Dec 2019 06:37:57 +0100https://ask.sagemath.org/question/49155/Error while assigning iteration on variable S[i,m-1]https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/Hi. I have the following code: I did some minor changes in the code by adding the variable `S[i,m-1]`, apart from the `S[*,*]` mentioned in the coding, the values of `S`'s are zero. The actual formula to calculate the first iteration is : `u_{i,1}(x)=h*[g_i(x)-S_{i,0}(x)-\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt]`, while for second iteration onwards uses: `u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt`. Here is the full code (which was helped by @dsejas).
Update: 2nd january: after 3 hours of discussion and re-deriving all iterations with my sv, we figured out that the iterations for this modified HAM only differs for `m=1` and `m=2` (the difference is that there is an inclusion of `S[*,*]` variable). From `m=3,...,10` the iterations are exactly the same as HAM (`S[*,*]` are all equal to zero) that we successfully did before. So I altered the code by leaving `m=1` and `m=2` separately and only doing the iterations for m=`3 till 10` (which I obtained from the code in the previous post). However upon calculating `CC`, the error is suppose to be some number x `10^(-15)`. Can you help me skim through the coding to see if I did a mistake anywhere.
from __future__ import print_function
NUMBER_OF_ITERATIONS = 10
h, t, x = var('h t x')
g = vector(SR, 5)
print('g(t): ***************************************************')
g[1] = -t^3-t; print(g[1])
g[2] = (1/4)*t^5-(1/4)*t^4-(1/2)*t^3-3*t^2-1; print(g[2])
g[3] = (1/2)*t^6-(31/6)*t^3+2*t^2+3; print(g[3])
g[4] = t^3-5; print(g[4])
print()
gg = vector(SR, 5)
print('gg(t): ***************************************************')
gg[1] = -x^3-x; print(gg[1])
gg[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(gg[2])
gg[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(gg[3])
gg[4] = x^3-5; print(gg[4])
print()
S = matrix(SR, 5, 5)
print('S(x): ***************************************************')
S[1,0] = -x^3; print(S[1,0])
S[1,1] = -x; print(S[1,1])
S[2,0] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3; print(S[2,0])
S[2,1] = -3*x^2-1; print(S[2,1])
S[3,0] = (1/2)*x^6-(31/6)*x^3; print(S[3,0])
S[3,1] = 2*x^2+3; print(S[3,1])
S[4,0] = x^3; print(S[1,0])
S[4,1] = -5; print(S[1,1])
K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 0; print(K2[1,1])
K2[1,2] = 1; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3*t; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])
print()
u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
u[1,1] = h*(gg[1]-S[1,0]-h*integrate(K2.row(1)*g.subs(x==t),t, 0, x)); print(u[1,1].full_simplify())
u[2,1] = h*(gg[2]-S[2,0]-h*integrate(K2.row(2)*g.subs(x==t),t, 0, x)); print(u[2,1].full_simplify())
u[3,1] = h*(gg[3]-S[3,0]-h*integrate(K2.row(3)*g.subs(x==t),t, 0, x)); print(u[3,1].full_simplify())
u[4,1] = h*(gg[4]-S[4,0]-h*integrate(K2.row(4)*g.subs(x==t),t, 0, x)); print(u[4,1].full_simplify())
print()
u[1,2] = (1+h)*u[1,1]-h*S[1,1]-h*integrate(K2.row(1)*u.column(1).subs(x==t),t, 0, x); print(u[1,2].full_simplify())
u[2,2] = (1+h)*u[2,1]-h*S[2,1]-h*integrate(K2.row(2)*u.column(2).subs(x==t),t, 0, x); print(u[2,2].full_simplify())
u[3,2] = (1+h)*u[3,1]-h*S[3,1]-h*integrate(K2.row(3)*u.column(3).subs(x==t),t, 0, x); print(u[3,2].full_simplify())
u[4,2] = (1+h)*u[4,1]-h*S[4,1]-h*integrate(K2.row(4)*u.column(4).subs(x==t),t, 0, x); print(u[4,2].full_simplify())
print()
for m in range(3, NUMBER_OF_ITERATIONS+1):
print('-------------------- For m = ' + str(m) + ': --------------------')
for i in range(1, 5):
u[i,m] = (1+h)*u[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
print()
AA=gg[1]+u[1,1]+u[1,2]+u[1,3]+u[1,4]+u[1,5]+u[1,6]+u[1,7]+u[1,8]+u[1,9]+u[1,10];AA
BB=AA.subs(h=-1,x=0.1);BB
CC=abs(BB-0.1);CCShaWed, 01 Jan 2020 14:32:37 +0100https://ask.sagemath.org/question/49300/Using piecewise-defined functions for recursive integer sequencehttps://ask.sagemath.org/question/49260/using-piecewise-defined-functions-for-recursive-integer-sequence/I have
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_4_56555555.png)
with `div --> //` ("integer part" of division).
I wonder how to use piecewise-defined functions for calculating `n(i)` and `Q(i)`. <br>
This does not work: <br>
r = 1
m = 7
n(i) = piecewise([([0,0], r//m), ((0,oo), 10*(n(i-1)-Q(i-1)*m))])
Q(i) = piecewise([([0,0], 0), ((1,oo), n(i)//m)])
print n(0)
print n(1)
print n(5)
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_45_9999999999999.jpg)
geroyxMon, 30 Dec 2019 16:46:55 +0100https://ask.sagemath.org/question/49260/Checking that all values in an array satisfy an inequalityhttps://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advancemerluzaFri, 01 Feb 2019 09:19:09 +0100https://ask.sagemath.org/question/45259/Checking that all values in an array satisfy an inequalityhttps://ask.sagemath.org/question/45258/checking-that-all-values-in-an-array-satisfy-an-inequality/I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advancemerluzaFri, 01 Feb 2019 09:18:03 +0100https://ask.sagemath.org/question/45258/How to avoid "flags"?https://ask.sagemath.org/question/44438/how-to-avoid-flags/ I read that utilizing flags is deprecated in modern programming. Can someone explain me how to avoid it when I want to go out of multiple "for" loops?logomathMon, 26 Nov 2018 10:58:46 +0100https://ask.sagemath.org/question/44438/Is it possible to speed up loop iteration in Sage?https://ask.sagemath.org/question/41375/is-it-possible-to-speed-up-loop-iteration-in-sage/I have a sage code that looks like this:
uni = {}
end = (l[idx]^(e[idx] - 1)) * (l[idx] + 1) # where end in my case is about 2013265922 but can also be much larger
for count in range(0, end):
i = randint(1, 303325737249669131)
if i in uni:
uni[i] += 1
else:
uni[i] = 1
So basically, I want to create very large number of random integers in the given range, check whether the number was already in the dictionary, if yes increment its count, if not initialize it to 1. But, this takes such a long time that it doesn't finish in a reasonable time. Is there any way to speed up this kind of loops in Sage (or Python)?ninhoSat, 03 Mar 2018 12:10:30 +0100https://ask.sagemath.org/question/41375/recursively solve and substitutehttps://ask.sagemath.org/question/40753/recursively-solve-and-substitute/I'm working on a pretty complicated problem (theoretically, not necessarily computationally). I'm not sure how to distill my question down to a simple example. But basically I have a system of equations over the ring of polynomials (or power series) over the symbolic ring. I want to be able to recursively solve for one symbolic function in terms of another, substitute the solution, and repeat. The ultimate goal is to extract relevant data all as a function of some positive integer k. It seems like I should be able to build a loop to do this, but I'm new to sage and programming in general. I haven't been able to find anything quite like what I'm trying to do online. So I'm asking for suggestions.
Here are some more details:
I have several polynomial expressions
j,k,z = var('j,k,z')
a,f=function('a,f')
p= lambda k : z^(-2*k) + sum(a(j)*z^(-j) for j in range(2*k-1))
F= lambda k : sum(f(j)*z^j, j, 2, 2*k)
which I plug into a formal matrix valued function and perform a series of algebraic manipulations. In the end I get a matrix whose entries are polynomials in the a(j)'s and f(j)'s. I want to recursively solve for the f(j)'s in terms of the a(j)'s so as to diagonalize this matrix expression. I can do this "by hand" but I want a function that will just spit out the end result. For example I know that f(2) = 1/4*a(2*k-2) for any k.
I've tried, for example:
def A1(k) :
for j in range(2,2*k) :
A0(k)=A0(k).substitute(f0(k,j)[0])
return A0(k)
Here A0(k) is a previously defined matrix expression and f0(k,j) is a function that solves for f(j). This code actually runs but then trying to call A1(2), etc., leads to errors.
Another attempt was to define
A1= lambda k,j : coef(A0(k),-k-2+j).substitute(f0(k,j)[0])
and then try to define a function of k in terms of the A1(k,j)'s using the sum() command. (In the code above coef(A,j) is a function that returns the coefficient of the z^j term in a polynomial over a ring of matrices).
The problem with this last attempt is that A1(k,j) for j bigger than 2 returns terms that include f(2), f(3), etc. I need something that solves for f(2), substitutes this value for f(2) into all future computations; then solves for f(3), substitutes this values for f(3) into all future computations; and so on until f(2*k).
I apologize if this question is too broad somehow. In looking over some of the literature on loops, solve(), substitute(), etc., I haven't found anything quite like this. Any suggestions? And thank you.charleslebarronTue, 23 Jan 2018 19:33:04 +0100https://ask.sagemath.org/question/40753/Loop is not finishing executionhttps://ask.sagemath.org/question/39530/loop-is-not-finishing-execution/I have the following Sage code:
p1 = 2
p2 = 5
e1 = ceil(100*log(2,p1))
e2 = ceil(5060*log(2,p2))
f = 1
p = p1^e1*p2^e2-1
while p not in Primes():
f = f + 1
p = p1^e1*p2^e2*f-1
p
I'm running this code in Sage Notebook, in a cell, but the value of `p` is never printed. I guess it is not finishing the execution. If I run the same code in Magma, it finishes after few seconds with the result of `p`. Any ideas what the problem might be?whateverTue, 14 Nov 2017 09:39:45 +0100https://ask.sagemath.org/question/39530/generation of certain matriceshttps://ask.sagemath.org/question/37860/generation-of-certain-matrices/I'd like to create a list of roughly 100-1000 $2 \times 2$ matrices $[A_1,A_2,...,A_N]$ that have the following properties:
1. $\det A_j = 1$
2. The entries of each $A_j$ are in an imaginary quadratic integer ring, such as $\mathbb{Z}[i]$, or $\mathbb{Z}[\sqrt{-2}]$
For example, the matrix
$$\begin{bmatrix}
1&2i \\\\
0&1
\end{bmatrix}$$
fits the above specifications when the ring is $\mathbb{Z}[i]$.
I know that I probably want to run some kind of loop over the entries of the matrix, but I'm not sure how to do this. Perhaps I want to initially treat the matrices as lists of length 4, and then run an iterative loop over the lists. Then, when the above specifications are met, that list is stored somewhere else. I think I'd also like to put a bound on the "size" of the matrix entries, but that should be easy to do afterwards.
Thanks! Daniel LThu, 08 Jun 2017 19:26:31 +0200https://ask.sagemath.org/question/37860/Apply Functions Iterativelyhttps://ask.sagemath.org/question/35989/apply-functions-iteratively/I have a few functions `R1,R2,R3,R4` which act on vectors. If `X = matrix(3,1,(x0,y0,r))` then I have `R2` defined as
`def R2(X):
return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))`
For instance, if `v = matrix(3,1,(1,1,1))`, then applying R2 to v gives the matrix $(-1,1,1)$.
I'd like to apply about 10 "levels" of iteration of these functions and have the output as a list of vectors.
So, the list should be
$R1(v), R2(v),R3(v),R4(v),R1^2(v),R2(R1(v)),R3(R1(v)),R4(R1(v)),R1^3(v),...,R1^{10}(v)\,...,R4^{10}(v)$
(as vectors)
where $R_j^n = R_j \circ \cdots \circ R_j$ $n$ times.
How can I do this?Daniel LMon, 12 Dec 2016 20:29:49 +0100https://ask.sagemath.org/question/35989/apply functions iteratively (modified re-post)https://ask.sagemath.org/question/36159/apply-functions-iteratively-modified-re-post/*(This is a modified re-post of [Apply Functions Iteratively](https://ask.sagemath.org/question/35989/apply-functions-iteratively/) since my original question wasn't answered)*
I have a few functions `R1,R2,R3,R4` which act on vectors. If `X = matrix(3,1,(x0,y0,r))` then I have
`def R1(X):
return matrix(3,1,(X[0,0]/(X[0,0]^2+X[1,0]^2-X[2,0]^2),X[1,0]/(X[0,0]^2+X[1,0]^2-X[2,0]^2),X[2,0]/abs(X[0,0]^2+X[1,0]^2-X[2,0]^2)))`
`def R2(X):
return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))`
`def R3(X):
return matrix(3,1,(-X[0,0]+2,X[1,0],X[2,0]))`
`def R4(X):
return matrix(3,1,(X[0,0],-X[1,0]+2,X[2,0]))`
For instance, if `v = matrix(3,1,(1,1,1))`, then applying R2 to v gives the matrix $(-1,1,1)$.
I'd like to apply about 10 "levels" of iteration of these functions and have the output as a list of vectors.
So, the list should be
$R1(v), R2(v),R3(v),R4(v),R1^2(v),R2(R1(v)),R3(R1(v)),R4(R1(v)),R1^3(v),...,R1^{10}(v)\,...,R4^{10}(v)$
(as vectors)
where $R_j^n = R_j \circ \cdots \circ R_j$ $n$ times.
I know how to apply a single function iteratively, but I don't know how to combine/loop the four together.
Thanks.Daniel LWed, 04 Jan 2017 02:14:39 +0100https://ask.sagemath.org/question/36159/if loop not workinghttps://ask.sagemath.org/question/34320/if-loop-not-working/ I have to use an if loop in my program. I am checking the irreducibility of a collection of polynomials. If f(x) is a reducible polynomial, then I want to find its factor. This is my code
v=f.is_irreducible()
if (v==0) f.factor()
But I am getting a syntax error. Can someone help me ?
nebuckandazzerWed, 03 Aug 2016 17:32:40 +0200https://ask.sagemath.org/question/34320/Integer result for fractionhttps://ask.sagemath.org/question/32347/integer-result-for-fraction/ Hi!
Can somebody explain me this strange behavior of Sage :
for p in range(1,10):
for q in range(1,10):
print p/q
Then `p/q` return the integer division.
However:
for p in range(1,10):
print p/7
give rational results.
A solution is to use `Rational(p)/Rational(q)` instead of `p/q`. But it is not an explanation.
Thanks,
Arnaud
Arnaud1418Wed, 27 Jan 2016 20:12:43 +0100https://ask.sagemath.org/question/32347/How can I get Sage to go over all possible maps between two sets?https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/ What I want to do is this : Say I take a graph $K_{n,n}$ and choose an ordering for each edge arbitrarily - say denote each edge as $(i,j)$ where $i$ is in the left partition and $j$ is in the right partition. I have a set of matrices $A = { A_1, A_2,...,A_k \}$. I want to iterate over all possible ways in which one could have assigned an A matrix to an edge of this graph.
- How does one do that? (..apart from writing a massive sequence of nested loops!..)
phoenixSun, 03 May 2015 00:25:28 +0200https://ask.sagemath.org/question/26705/Trying to print and plot two parameters of multivariate functionshttps://ask.sagemath.org/question/26678/trying-to-print-and-plot-two-parameters-of-multivariate-functions/ I am trying to numerically find the values of two parameters that will make three multivariate functions true. I am trying to use for loops and if statements to do this. I need this for a university project I am working on. I am completely new to Python, and the syntax of the Sage notebook I have created does not seem to be working. Below is what I typed. Also, here is the [link](https://cloud.sagemath.com/projects/f9c3371c-3682-4811-b236-f62976d8fa24/files/Shape%20Factor%20versus%20Efficiency.sagews) to the notebook I created.
var('V_m,phi_0,f_0,Lambda,sigma_q,T_i,t,T_2,I_0,sigma_B,T_amb,eta')
V_m=3.06*10^-4
T_i=1300
Lambda=4.5
phi_0=0.5
f_0=srange(0.1,100,step=0.1,universe=None)
T_2=srange(500,2000,step=0.1,universe=None)
sigma_q=srange(0.5*10^12,3.5*10^12,step=10^6,universe=None)
I_0=700000
T_amb=300
pi_n=pi.numerical_approx()
sigma_B=5.67*10^-8
for i in f_0:
for j in T_2:
for k in sigma_q:
R_0=n(((V_m/((1-phi_0)*float(pi)*i))^(1/3))*sqrt(phi_0))
H_0=n(((V_m/((1-phi_0)*float(pi)*i))^(1/3)))
L_0=n((((V_m*(i)^2)/((1-phi_0)*float(pi))))^(1/3))
T_R0L0=n(T_i+((k)/(2*Lambda))*(1/2)*(((R_0)^2)-((H_0)^2))+((H_0)^2)*ln((H_0)/(R_0)))
T_MAXR0=n((((I_0)/(float(pi)*((H_0)^2)*sigma_B))+((T_amb)^4)-((k)/(sigma_B))*((((H_0)^2)-((R_0)^2))
/R_0))^(1/4))
T_Mean=n((T_MAXR0+T_R0L0)/2)
T_R00=n(((1/2)*(((I_0)/(float(pi)*((H_0)^2)*sigma_B))+((T_amb)^4)+((T_Mean)^4)-((k)/(sigma_B))*
((((H_0)^2)-((R_0)^2))/R_0)))^(1/4))
A1=n((-1/2*I_0*(i - sqrt(i^2 + 4*phi_0))*i))
A2=n(V_m * k + 1/30*(15*j^4*i^6 + 15*T_amb^4*i^6 - 6*(i^6 - 20*(3*i^2*phi_0^2
- 4*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00^4 - 6*(i^6 + 40*(3*i^2*phi_0^2 -
8*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00^3*T_R0L0 - 6*(i^6 -120*(i^2*phi_0^2 -
4*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00^2*T_R0L0^2 - 6*(i^6 + 40*(3*i^2*phi_0^2 -
8*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00*T_R0L0^3 - 6*(i^6 - 20*(3*i^2*phi_0^2 -
4*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R0L0^4 - 3*(5*j^4*i^5 + 5*T_amb^4*i^5 - 2*(i^5 +
3*i^3*phi_0 - 44*i*phi_0^2)*T_R00^4 - 2*(i^5 - 2*i^3*phi_0 + 176*i*phi_0^2)*T_R00^3*T_R0L0 -
2*(i^5 - 2*i^3*phi_0 - 264*i*phi_0^2)*T_R00^2*T_R0L0^2 - 2*(i^5 - 2*i^3*phi_0 +
176*i*phi_0^2)*T_R00*T_R0L0^3 - 2*(i^5 + 3*i^3*phi_0 - 44*i*phi_0^2)*T_R0L0^4)*\
sqrt(i^2 + 4*phi_0) + 32*(144*T_R00^2*T_R0L0^2*i*phi_0^2 - (5*i^3*phi_0 - 24*i*phi_0^2)\
*T_R00^4 + (5*i^3*phi_0 - 96*i*phi_0^2)*T_R00^3*T_R0L0 + (5*i^3*phi_0 - 96*i*phi_0^2)\
*T_R00*T_R0L0^3 - (5*i^3*phi_0 - 24*i*phi_0^2)*T_R0L0^4)*sqrt(phi_0))*sigma_B*\
(float(pi)*V_m^2/(i^2*(phi_0 - 1)^2))^(1/3)/i^4)
A3=(-1/2*(i^2 - sqrt(i^2 + 4*phi_0)*i + 2*phi_0)*((j^4 - T_amb^4)*sigma_B - I_0*(i^2*(phi_0 -
1)^2/(float(pi)*V_m^2))^(1/3)) + 1/30*(15*j^4*i^6 - 5*(i^6 + 48*phi_0^3*arcsinh(1/2*i/sqrt(phi_0)))\
*T_R00^4 - 4*(i^6 - 240*phi_0^3*arcsinh(1/2*i/sqrt(phi_0)))*T_R00^3*T_R0L0 - 3*(i^6 -
120*(i^2*phi_0^2 - 4*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00^2*T_R0L0^2 - 2*(i^6 +
120*(3*i^2*phi_0^2 - 4*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R00*T_R0L0^3 - (i^6 -
120*(3*i^2*phi_0^2 - 2*phi_0^3)*arcsinh(1/2*i/sqrt(phi_0)))*T_R0L0^4 - (15*j^4*i^5
- 5*(i^5 - 2*i^3*phi_0 + 12*i*phi_0^2)*T_R00^4 - 4*(i^5 - 2*i^3*phi_0 +
6*i*phi_0^2)*T_R00^3*T_R0L0 - 3*(i^5 - 2*i^3*phi_0 - 264*i*phi_0^2)*T_R00^2*T_R0L0^2 \
- 2*(i^5 - 2*i^3*phi_0 + 456*i*phi_0^2)*T_R00*T_R0L0^3 - (i^5 + 28*i^3*phi_0 -
324*i*phi_0^2)*T_R0L0^4)*sqrt(i^2 + 4*phi_0) - 32*(24*T_R00^3*T_R0L0*i*phi_0^2 -
72*T_R00^2*T_R0L0^2*i*phi_0^2 - (5*i^3*phi_0 - 72*i*phi_0^2)*T_R00*T_R0L0^3 + \
(5*i^3*phi_0 - 24*i*phi_0^2)*T_R0L0^4)*sqrt(phi_0))*sigma_B/i^4)
eta=(k)*(V_m)/(I_0)
if abs(A1-A2)<0.5 and abs(A3)<5:
print(i,eta)
I tried using matplotlib to plot this, but nothing happened. Right now I woyld settle for Sage simply printing me the values of i and eta which make the if statement true. If I got these values in a text file, I could use a spreadsheet to generate a plot. Can anyone help?C. ThompsonWed, 29 Apr 2015 02:18:25 +0200https://ask.sagemath.org/question/26678/How to create a loop with two variables?https://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/I have defined the following functions:
def I(x):
return 1
def Moebius(x):
return sum(moebius(i) * I(x*i^(-1)) for i in range(1,x+1))
The output is of course Mertens function. How do I turn `Moebius(x)` into an operator, so that I can define `Moebius^2(x)` as the Moebius transform of the output?
Kevin SmithSat, 03 Jan 2015 12:55:13 +0100https://ask.sagemath.org/question/25383/Generation of all 3-index tensors with {-1,0,1} as entries.https://ask.sagemath.org/question/24417/generation-of-all-3-index-tensors-with-101-as-entries/ I need to generate all tensors $A_{ijk}$ (i.e. array with 3 indices), where $i=0, \ldots, 6$, $j=0, \ldots 6$, $k=0, \ldots 5$, such that only integer numbers $-1$, $0$ and $1$ could be used as entries. There are $6*6*5*3$ such possibilities. What would be the shortest system of embedded loop for this in SAGE? I just started using SAGE and don't have any previous experience with Python. The only solution I was able to think of include hundreds of embedded loops. IraTue, 07 Oct 2014 13:57:17 +0200https://ask.sagemath.org/question/24417/for-while loop problemhttps://ask.sagemath.org/question/10201/for-while-loop-problem/Hi experts!
Im a newbie Python and sage user.
Im writing a script for draw N random lines in a rectangle of b*h center in axes.
The actual code is:
print('Distribuye N rectas aleatorios de longitud 1 en un rectángulo de b x h centrado en el origen de un par de ejes cartesianos\n')
N=float(raw_input('Número de rectas aleatóreos (N)?:\n'))
b=float(raw_input('Base del rectángulo (b>1)?:\n'))
h=float(raw_input('Altura del cuadrado (h>1)?:\n'))
uniforme_x=RealDistribution('uniform',[-b/2,b/2])
uniforme_y=RealDistribution('uniform',[-h/2,h/2])
lista_x=[uniforme_x.get_random_element() for j in srange(N)]
lista_y=[uniforme_y.get_random_element() for j in srange(N)]
lista_xy = zip(lista_x , lista_y)
lineas=[]
d2=1*1*2
for j in lista_xy:
x1 = j[0]
y1 = j[1]
while d2>1*1:
x2 = uniforme_x.get_random_element()
y2 = uniforme_x.get_random_element()
d2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
lineas.append([j,(x2,y2)])
grafico=sum(lineas)
grafico.show()
The associated output is a graph of N lines. Each line comes from a different item from the list zip BUT end at the same point (x2, y2). Furthermore the lines have length (d2) greater than 1.
I cant find the error. I know this is a basic question but i hope you can help me.
Waiting for your answers.
Best regards.
mresimulatorThu, 06 Jun 2013 13:11:30 +0200https://ask.sagemath.org/question/10201/How to design a loophttps://ask.sagemath.org/question/10097/how-to-design-a-loop/If I have designed a pseudo random number generator(PRNG), but I just cant output the random number from the first round ( from the input seed),
how could I design a loop that could help me run the number of round I need in the PRNG?? AyeTue, 07 May 2013 18:19:59 +0200https://ask.sagemath.org/question/10097/Adding a loop to graphhttps://ask.sagemath.org/question/9413/adding-a-loop-to-graph/I have a for loop that adds edges to a graph `g`, but I also want the graph to add loops. How can I do this, for a general graph whose structure I do not know (that is, I want to add the loop to a graph, rather than make a new graph)?
I've tried using the `g.add_edge(1,1)` function, but that doesn't work, i.e. no loop is added. I also tried using `g.add_edge` three times, then `g.merge_vertices` to contract the cycle to a loop, but also to no avail. Sage justs simplifies the graph.
Any approach to this would be appreciated, direct or indirect. jlvThu, 11 Oct 2012 10:30:53 +0200https://ask.sagemath.org/question/9413/Aborting a Loophttps://ask.sagemath.org/question/8752/aborting-a-loop/Say I have a function which runs a while loop which always evaluates as true and thus keeps running, is there a way to abort this without just closing the terminal all together? Thanks.ZaubertrankFri, 02 Mar 2012 21:24:53 +0100https://ask.sagemath.org/question/8752/Porting a finite-differences-matrix from Matlab to Numpyhttps://ask.sagemath.org/question/7982/porting-a-finite-differences-matrix-from-matlab-to-numpy/Hi there,
I'm an engineering student plagued by a prof who adores Matlab. For reasons I'm sure you understand, I'd like to avoid Matlab and learn Numpy/Scipy instead. Now I'm given this code, which I'm supposed to adapt to a problem given in class (irrelevant).
If you're somewhat familiar with Matlab and interested in helping me port a weird piece of code, then this question is for you -- read on :)
Short explanation: We're creating a meshgrid of nodes, and assign voltages (matrix Vstart) to some of them. We'd like to find the potentials of the nodes in between. You'd assume we want the voltage drop to follow ?²=0, and indeed I get a fine, smooth potential drop if I loop through the in-between-nodes 50 times and repeatedly assign to them the average value of the nodes around them (that was my approach -- code not shown here).
For starters, here's the Matlab initialization code (my comments):
for m = 1:length(N)
d = a/N(m); #distance between mesh nodes
N1 = N(m)+1; #side length of the central conductor
N2 = b/a*N(m)+1; #side length of meshgrid
Vstart = zeros(N2,N2);
Vstart(1,:) = Vb; #set the outer nodes to Vb
Vstart(:,1) = Vb;
Vstart(N2,:) = Vb;
Vstart(:,N2) = Vb;
lim1=(N2-N1)/2+1;
lim2=(N2+N1)/2;
Vstart(lim1:lim2,lim1:lim2) = Va; #set the inner nodes to Va
Now we're told to use a different approach than the one described above, and we're given a piece of Matlab code that fills a matrix A with a bunch of negative-quarters, and another matrix A with a long line of 0's, Vb's and Va's. It then solves a system and puts the results into V. I'm not quite sure how the matrix works here, although the many 1/4's make me think this is just my previous approach in disguise.
function[A,C] = mACfd(Vstart,N2)
C = zeros(N2^ 2,1);
for i = 1:N2
for j = 1:N2
k = (i-1)*N2+j;
A(k,k) = 1;
if (Vstart(i,j) == 0)
A(k,k-N2) = -1/4;
A(k,k+N2) = -1/4;
A(k,k+1) = -1/4;
A(k,k-1) = -1/4;
else C(k) = Vstart(i,j);
end;
end;
end;
[A,C] = mACfd(Vstart,N2);
V = inv(A)*C;
for i = 1:N2
V2D(i,:) = V((i-1)*N2+1:i*N2);
end;
I don't understand how matrices A and C are filled, what their dimensions are, what they're for, and how I can efficiently reproduce the algorithm in Python. Particularly the weird indexing irks me.
If I didn't make myself clear, please ask in the comments.
Thanks!sebastian_kThu, 03 Mar 2011 21:23:00 +0100https://ask.sagemath.org/question/7982/