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, 18 Oct 2019 10:29:04 +0200How 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/