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, 25 Oct 2022 10:36:35 +0200Why does this code have trubles with a specific input?https://ask.sagemath.org/question/64597/why-does-this-code-have-trubles-with-a-specific-input/I wrote this code and I found out it gives problem with specific inputs
def counter(w): #turns a list of classes into a list of cardinality of the classes
n=len(w)
s=[]
for i in range(n):
a=len(w[i])
s.append(a)
return s
str=[1,3,1,2,1,2,1,3,1]
def stringer(s,j): #computes mu(i)
n=len(s)
m=0
d=0
m=m+(s[j])
d=d+s[j]^2
for i in range(j+1):
for k in range(j,n):
if i!=k:
ss=0
for q in range(i,k+1):
ss=ss+s[q]
m=m+2*s[i]*s[k]/ss
d=d+2*s[i]*s[k]
w=m/d
return w
def mumaker(s): #makes the mu line
n=len(s)
m=[]
m = numpy.empty(n, dtype=object)
for j in range(n):
m[j]=stringer(s,j)
return(m)
def fuser(s,w,m): #aggregates elements with the same mu in s,w and m
g=[]
h=[]
n=len(m)
for j in range(n):
l=s[j]
r=w[j]
for i in range(j+1,n):
if m[i]==m[j]:
l=l+s[i]
r=r+w[i]
s[i]=0
m[i]=2
w[i]=0
g.append(l)
h.append(r)
return(g,h,m)
def orderer(s,w,m): #orderes the strings based on the mu
k=0
q=0
t=0
r=len(s)
n=[]
for j in range(r):
n=[]
for i in range(j,r):
n.append(m[i])
t=min(n)
for i in range(j,r):
if m[i]==t:
k=s[j]
q=w[j]
s[j]=s[i]
w[j]=w[i]
s[i]=k
w[i]=q
k=m[j]
m[j]=m[i]
m[i]=k
return(s,w)
def reducer(v): #removes the 0
w=[]
n=len(v)
for i in range(n):
if v[i]!=0:
w.append(v[i])
v=w
return(v)
def final(w):
str=counter(w)
n=mumaker(str)
[g,h,m]=fuser(str,w,n)
[p,q]=orderer(g,h,m)
k=reducer(p)
u=reducer(q)
return(k,u)
def cycler(w):
for i in range(10):
i=i+1
u=w
[k,w]=final(u)
print('New mu line cardinalities=',k, 'New mu line=', u)
if u==final(u)[1]:
print('Fuzzy grade=',i)
break
cycler(v)
This works fine with almost every input, for example v=[[0],[1],[2],[3],[4,5]].
However if I try inputs like v=[[0],[1],[2],[3],[4],[5]] or v=[[0,1],[2,3],[4,5]] (i.e. inputs in which every element has the same lenght) the following error comes up:
TypeError Traceback (most recent call last)
Input In [1], in <cell line: 134>()
130 print('Fuzzy grade=',i)
131 break
--> 134 cycler(v)
Input In [1], in cycler(w)
125 i=i+Integer(1)
126 u=w
--> 127 [k,w]=final(u)
128 print('New mu line cardinalities=',k, 'New mu line=', u)
129 if u==final(u)[Integer(1)]:
Input In [1], in final(w)
112 def final(w):
--> 113 str=counter(w)
114 n=mumaker(str)
115 [g,h,m]=fuser(str,w,n)
Input In [1], in counter(w)
7 s=[]
8 for i in range(n):
----> 9 a=len(w[i])
10 s.append(a)
11 return s
TypeError: object of type 'sage.rings.integer.Integer' has no len()
How can I solve this?Manga18Tue, 25 Oct 2022 10:36:35 +0200https://ask.sagemath.org/question/64597/How to iterate over symbolic coefficientshttps://ask.sagemath.org/question/48391/how-to-iterate-over-symbolic-coefficients/I am a relative newbie to Sage but not to algebraic manipulation in general.
I have a family of expressions with non-linear coefficients which I am not sure how best to represent. In the end it has to be an (infinite) ring of polynomials representing a Volterra space, but I have the same terms inside non-linear functions (which I need to evaluate to a known value) and as the polynomial variables.
Here is an example, where simple symbolic manipulation doesn't work:
f_m = a*x^b
ex = expand(f_m.subs({b : b_m + (b_s-b_m)^3})).derivative(b_m).full_simplify()
ex.subs({b_m + (b_s-b_m)^3 : b_m }).show()
Producing:
-(3*a*b_m^2 - 6*a*b_m*b_s + 3*a*b_s^2 - a)*x^(-b_m^3 + 3*b_m^2*b_s - 3*b_m*b_s^2 + b_s^3 + b_m)*log(x)
When what I want is:
-(3*a*b_m^2 - 6*a*b_m*b_s + 3*a*b_s^2 - a)*x^(-b_m)*log(x)
How can I iterate over non-linear elements (exponent and the arguments to any non-monomial terms) carrying out direct substitutions, while leaving monomials alone. I want the result to remain as a polynomial ring on b_s
----------
Clarification:
The intent is to use the code for a generic non-linear construct. It's intended to handle any general non-linear expression in L2, but these are manipulated to belong to a class of Volterra polynomials with non-linear coefficients. like the log(x) and x^bm on the above example. Those are the coefficients of the monomials.
Idealy whatever process I follow should be able to identify the non-monomial coefficients in the expression and remove the monomial terms from them by doing the substitution b_s = b_m. Basically:
x.subs(b_s = b_m) for x not a monomial term in expression.
Which would make the resulting expression strictly a polynomial ring on b_s with non-linear coefficients on x and b_m.Edgar BrownFri, 18 Oct 2019 10:29:04 +0200https://ask.sagemath.org/question/48391/Priniting Horizontally??https://ask.sagemath.org/question/36978/priniting-horizontally/Hello, guys! hope you are having a good day. First of all, I'm very new to sage and this forum, so would you please be generous to my question,, please?..
So, I was trying to do some recursive/iterative calculations as follows:
def calculator(L1,L2):
c=0
m=len(L1)
n=len(L2)
print '('
if m==0:
c=c+0
print '+'
print c
print ')'
elif m==1:
l1=L1[0]
l2=L2[0]
if l1<l2:
c=c+l1
print '+'
print c
print ')'
else:
c=c+0
print '+'
print c
print ')'
else:
for i in range(m):
l1=L1[i]
l2=L2[i]
if l1<l2:
c=c+l2
print '+'
print c
print ')'
else:
L3=[L1[j] for j in range(i)]+[L1[j] for j in range(i+1,m)]
L4=[L2[j] for j in range(i)]+[L2[j] for j in range(i+1,n)]
c=c+(-1)^m*calculator(L3,L4)
print '+'
print c
print ')'
return c
And, if we calculate, for example,
L1=[5,7,3]; L2=[7,5,2]; calculator(L1,L2)
then we get: (I've intentionally wrote vertically in the below, because that is exactly the sage gives us as a result)
(
+
7
)
(
+
7
)
(
+
5
)
+
12
)
+
-5
)
(
+
7
)
(
+
5
)
+
12
)
+
-17
)
-17
BUT!, if we actually, calculate by hand, then we get:
calculator([5,7,3],[7,5,2])
=7-calculator([5,3],[7,2])-calculator([5,7],[7,5]
=7-(7+calculator([5],[7]))-(7+calculator([5],[7])
=7-(7+(5))-(7+(5))=-17
And I actually want 7-(7+(5))-(7+(5))=-17 as a result... Can any body help me how to solve this kind of problem,, please...
And, moreover, if it is possible to get the result like
"calculator([5,7,3],[7,5,2])
=7-calculator([5,3],[7,2])-calculator([5,7],[7,5]
=7-(7+calculator([5],[7]))-(7+calculator([5],[7])
=7-(7+(5))-(7+(5))=-17"
then, it would be much greater...!
Oh one last thing to note is that even though we get the vertical result as above, if we write down horizontally that result, we get
(
+
7
)
(
+
7
)
(
+
5
)
+
12
)
+
-5
)
(
+
7
)
(
+
5
)
+
12
)
+
-17
)
-17
which shows us that there are some redundancy in the result and some parentheses are showing weirdly...
Can any body,, please help me how to solve this kind of problem... Thank you for help! and I hope you have a great day!!! I wish you the best luck for your future! Thank you!sssageeeeSat, 18 Mar 2017 19:14:33 +0100https://ask.sagemath.org/question/36978/OrderedSetPartitions (variant)https://ask.sagemath.org/question/34001/orderedsetpartitions-variant/I would like to have the list of partitions of [1,2,...N] of finite size k
but with using the NULL set
for example, if looking for the partitions of [1,2,3,4,5] of size 3,
i would like `[ [],[],[1,2,3,4,5] ]`
and `[ [],[1,2,3],[4,5] ]` to be in this list
is it possible to do natively in Sage, or should i write a custom function ?
> OrderedSetPartitions(5,3).list()
returns
[[{1, 2, 3}, {4}, {5}],
[{1, 2, 3}, {5}, {4}],
[{1, 2, 4}, {3}, {5}],
[{1, 2, 5}, {3}, {4}],...
EDIT: here is a (non optimized at all) attempt to solve naively this problem
def OrderedSetPartitions_0(A,k):
cols={i for i in range(k)}
# returns the list of k-OrderedSetPartitions of A, allowing for the empty set
s=Subsets(cols).list()
res=[]
count=0
P=[OrderedSetPartitions(A,i) for i in range(k+1)]
for sub in s:
print("sub=")
print(sub)
tmp=[ {} for i in range(k)]
c=sub.cardinality()
for part in P[c]:
print("part=")
print(part)
for i in range(c):
tmp[sub[i]]=part[i]
print("tmp=")
print(tmp)
res=res.append([tmp])
# res=res.append([tmp]) # tried this too
print("res=")
print(res)
count=count+1
return(res)
# print(count)
A=range(3)
k=2
A
P=[OrderedSetPartitions(A,i) for i in range(k+1)]
# note that P[2].list is a list of list !
P[2].list()
> [[{0, 1}, {2}],
> [{0, 2}, {1}],
> [{1, 2}, {0}],
> [{0}, {1, 2}],
> [{1}, {0, 2}],
> [{2}, {0, 1}]]
myset=OrderedSetPartitions_0(A,k)
I get this error message, and I admit I don't get it at all, because it looks fine when coding,
but somehow res seems to be "None" instead of []
> sub=
> {}
> sub=
> {0}
> part=
> [{0, 1, 2}]
> tmp=
> [{0, 1, 2}, {}]
> res=
> None
> sub=
> {1}
> part=
> [{0, 1, 2}]
> tmp=
> [{}, {0, 1, 2}]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "_sage_input_21.py", line 10, in <module>
> exec compile(u'open("___code___.py","w").write("#
> -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bXlzZXQ9T3JkZXJlZFNldFBhcnRpdGlvbnNfMChBLGsp"),globals())+"\\n");
> execfile(os.path.abspath("___code___.py"))
> File "", line 1, in <module>
>
> File "/private/var/folders/gm/z065gk616xg6g0xgn4c7_bvc0000gn/T/tmpryfYOj/___code___.py", line 2, in <module>
> exec compile(u'myset=OrderedSetPartitions_0(A,k)
> File "", line 1, in <module>
>
> File "/private/var/folders/gm/z065gk616xg6g0xgn4c7_bvc0000gn/T/tmpSH_9LF/___code___.py", line 27, in OrderedSetPartitions_0
> res=res.append([tmp])
> AttributeError: 'NoneType' object has no attribute 'append'
The problem is about res
if i omit the line res=res.append(tmp), i will get the enumeration ok i think
EDIT:
i changed `res=res.append(tmp)` by `res.append(tmp)`
i also changed the line initializing tmp into `tmp=[ set() for i in range(k)]`
now as a result of `print(tmp)` i get the correct enumeration
[{0, 1, 2}, set([]), set([])]
[set([]), {0, 1, 2}, set([])]
[set([]), set([]), {0, 1, 2}]
[{0, 1}, {2}, set([])]
[{0, 2}, {1}, set([])]
[{1, 2}, {0}, set([])]
[{0}, {1, 2}, set([])]
[{1}, {0, 2}, set([])]
[{2}, {0, 1}, set([])]
[{0, 1}, set([]), {2}]
[{0, 2}, set([]), {1}]
[{1, 2}, set([]), {0}]
[{0}, set([]), {1, 2}]
[{1}, set([]), {0, 2}]
[{2}, set([]), {0, 1}]
[set([]), {0, 1}, {2}]
[set([]), {0, 2}, {1}]
[set([]), {1, 2}, {0}]
[set([]), {0}, {1, 2}]
[set([]), {1}, {0, 2}]
[set([]), {2}, {0, 1}]
[{0}, {1}, {2}]
[{0}, {2}, {1}]
[{1}, {0}, {2}]
[{2}, {0}, {1}]
[{1}, {2}, {0}]
[{2}, {1}, {0}]
However, for res, i get the strange result
[[{0, 1, 2}, set(), set()],
[set(), {0, 1, 2}, set()],
[set(), set(), {0, 1, 2}],
[{2}, {0, 1}, set()],
[{2}, {0, 1}, set()],
[{2}, {0, 1}, set()],
[{2}, {0, 1}, set()],
[{2}, {0, 1}, set()],
[{2}, {0, 1}, set()],
[{2}, set(), {0, 1}],
[{2}, set(), {0, 1}],
[{2}, set(), {0, 1}],
[{2}, set(), {0, 1}],
[{2}, set(), {0, 1}],
[{2}, set(), {0, 1}],
[set(), {2}, {0, 1}],
[set(), {2}, {0, 1}],
[set(), {2}, {0, 1}],
[set(), {2}, {0, 1}],
[set(), {2}, {0, 1}],
[set(), {2}, {0, 1}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}]]
only the first 3 lines are correct. There seems to be some side-effects. This is very enigmatic to me because between
`print(tmp)` and `res.append(tmp)` there are no instructions at all !!!!faguiSun, 03 Jul 2016 15:18:42 +0200https://ask.sagemath.org/question/34001/