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, 17 Dec 2021 22:02:35 +0100Defining functions from a finite subset of $\mathbb{N}$ to a finite subset of $\mathbb{N}$https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. `f` is in my collection if and only if `f` is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination.
I tried with the following code which isn't helpful when the number of vertices is little large.
def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.
Any suggestion or help is highly appreciated.
Thank you for your help in advance.Thu, 09 Dec 2021 19:49:56 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/Comment by Irfan for <p>I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. <code>f</code> is in my collection if and only if <code>f</code> is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination. </p>
<p>I tried with the following code which isn't helpful when the number of vertices is little large.</p>
<pre><code>def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
</code></pre>
<p>Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.</p>
<p>Any suggestion or help is highly appreciated.</p>
<p>Thank you for your help in advance.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60208#post-id-60208I am sorry. I meant that we have some '+' on the finite set with the property that when we can add two elements then the resulting addition must be in that set. The addition is not over the full set. E.g. we have a graph with 10 vertices with a coloring ( may not be proper) by 0 and 1. Suppose we can add 'a' and 'b' where both 'a' and 'b' are not colored with '1'. Then it must have the property color(a)+color(b)=color(a+b)Sat, 11 Dec 2021 12:50:20 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60208#post-id-60208Comment by John Palmieri for <p>I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. <code>f</code> is in my collection if and only if <code>f</code> is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination. </p>
<p>I tried with the following code which isn't helpful when the number of vertices is little large.</p>
<pre><code>def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
</code></pre>
<p>Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.</p>
<p>Any suggestion or help is highly appreciated.</p>
<p>Thank you for your help in advance.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60203#post-id-60203If N is the set of non-negative integers, the only nonempty finite subset of N which is closed under addition is {0}. So what do you mean by "addition"?Fri, 10 Dec 2021 23:42:12 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60203#post-id-60203Comment by John Palmieri for <p>I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. <code>f</code> is in my collection if and only if <code>f</code> is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination. </p>
<p>I tried with the following code which isn't helpful when the number of vertices is little large.</p>
<pre><code>def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
</code></pre>
<p>Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.</p>
<p>Any suggestion or help is highly appreciated.</p>
<p>Thank you for your help in advance.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60204#post-id-60204The Python itertools package is good for iterating over subsets, combinations, powersets, things like that: https://docs.python.org/3/library/itertools.html.Fri, 10 Dec 2021 23:43:24 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60204#post-id-60204Comment by slelievre for <p>I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. <code>f</code> is in my collection if and only if <code>f</code> is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination. </p>
<p>I tried with the following code which isn't helpful when the number of vertices is little large.</p>
<pre><code>def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
</code></pre>
<p>Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.</p>
<p>Any suggestion or help is highly appreciated.</p>
<p>Thank you for your help in advance.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60201#post-id-60201Welcome to Ask Sage! Thank you for your question.Fri, 10 Dec 2021 19:34:48 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60201#post-id-60201Answer by John Palmieri for <p>I want to define all functions from a finite subset (which is closed under addition) of $\mathbb{N}$ to another finite subset of $\mathbb{N}$ (target set is {0,1} in my case) with some special properties e.g. <code>f</code> is in my collection if and only if <code>f</code> is additive. I came across this type of problem as a part of my research. (e.g. the set of ways we can color a finite graph with finite set of colors where is the set of vertices we have a binary operation). Of course theoretically we can do it by considering power sets (considering all elements which are mapped to 1) which is not helpful while programming is Sage. Later I have to run my program over this collection to reach the destination. </p>
<p>I tried with the following code which isn't helpful when the number of vertices is little large.</p>
<pre><code>def Lab_Funs(M):
P = list(powerset(M))
l = len(P)
F = list(var('F_%d' % i) for i in range(l))
for i in range(l):
Q = P[i]
F[i] = []
for s in range(len(M)):
if M[s] in Q:
F[i] = F[i] + [(M[s], 1)]
else:
F[i] = F[i] + [(M[s], 0)]
return [F[i] for i in range(l)]
def Req_Lab_Funs(M, y):
RLF = []
for f in Lab_Funs(M):
k = 1
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0], f[p][0])[1] == 1:
N = plus(f[s][0], f[p][0])[0]
r = M.index(N)
if f[s][1] + f[p][1] != f[r][1]:
k = 0
if k > 0:
L = [f[r][0] for r in range(len(M))]
t = L.index(y)
if f[t][1] == 1:
RLF = RLF + [f]
return RLF
</code></pre>
<p>Lab_Funs: Labeling functions
Req_Lab_Funs: Required Labeling functions.</p>
<p>Any suggestion or help is highly appreciated.</p>
<p>Thank you for your help in advance.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?answer=60212#post-id-60212A few comments: (1) don't do `list(powerset(M))`. Instead, just do `for Q in powerset(M)`: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. `for Q in powerset(M)` should be faster and more memory efficient. (2) Similarly, you may be able to have `Lab_Funs` return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at `itertools` to see what it provides.
Edit:
Minor simplification: change
IT=iter(powerset(M))
LIS=[];
for g in IT:
to
LIS=[];
for g in powerset(M):
More significant change: replace `Lab_Funs` by an iterator: don't compute the whole thing at once:
class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
Then do `for f in Lab_Funs_iter(M):` Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what `plus` is supposed to do.Sun, 12 Dec 2021 00:57:15 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?answer=60212#post-id-60212Comment by John Palmieri for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60334#post-id-60334If you don't provide code for `plus` here, there is no way for us to test the code and see what problems you're having. You also don't describe the problems; your recent comment says that it works for small numbers and implies that it doesn't for large ones, but I don't know what the problems are. Various versions of `Lab_Funs` work fine when `M=[1..10]`.Fri, 17 Dec 2021 22:02:35 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60334#post-id-60334Comment by Irfan for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60333#post-id-60333Please see the comments before the answers. It's an operation on the vertices ( not all).Fri, 17 Dec 2021 19:19:14 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60333#post-id-60333Comment by John Palmieri for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60305#post-id-60305What is `plus`?Thu, 16 Dec 2021 03:53:03 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60305#post-id-60305Comment by Irfan for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60297#post-id-60297def Req_Lab_Funs(M,y):
RLF=[];
for f in Lab_Funs(M):
k=1;
for s in range(len(M)):
for p in range(len(M)):
if plus(f[s][0],f[p][0])[1]==1:
N=plus(f[s][0],f[p][0])[0];
r=M.index(N);
if f[s][1]+f[p][1]!=f[r][1]:
k=0;
if k>0:
L=[f[r][0] for r in range(len(M))];
t=L.index(y);
if f[t][1]==1:
RLF=chain(RLF,[f])
print(type(RLF))
return(RLF)
I am trying to find
Req_Lab_Funs(S,[1..10])
For
M=[1..10]Wed, 15 Dec 2021 18:36:06 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60297#post-id-60297Comment by Irfan for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60294#post-id-60294Sorry John. It's working for comparatively small numbers. I am writing my edited code. Can you point out please where is the problem?
from itertools import chain
def Lab_Funs(M):
IT=iter(powerset(M))
LIS=[];
for g in IT:
F=[];
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
LIS=chain(LIS,[F])
print(type(LIS))
return(LIS)
And the next part is....Wed, 15 Dec 2021 18:27:46 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60294#post-id-60294Comment by slelievre for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60258#post-id-60258I turned John's comment into an answer. You can accept it (accept button at top left of answer) to mark your question as solved.Tue, 14 Dec 2021 16:55:38 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60258#post-id-60258Comment by Irfan for <p>A few comments: (1) don't do <code>list(powerset(M))</code>. Instead, just do <code>for Q in powerset(M)</code>: if you're going to iterate over something, there is no need to turn it into a list first, and that may take time and may also require a lot of memory. <code>for Q in powerset(M)</code> should be faster and more memory efficient. (2) Similarly, you may be able to have <code>Lab_Funs</code> return an iterator, not a list: since you are going to iterate over the entries, there is no need to compute the whole list all at once. (3) Also, please look at <code>itertools</code> to see what it provides.</p>
<p>Edit:</p>
<p>Minor simplification: change</p>
<pre><code>IT=iter(powerset(M))
LIS=[];
for g in IT:
</code></pre>
<p>to</p>
<pre><code>LIS=[];
for g in powerset(M):
</code></pre>
<p>More significant change: replace <code>Lab_Funs</code> by an iterator: don't compute the whole thing at once:</p>
<pre><code>class Lab_Funs_iter:
def __init__(self, M):
self.M = M
def __iter__(self):
self.powerset = powerset(M)
return self
def __next__(self):
g = self.powerset.__next__()
M = self.M
F = []
for s in range(len(M)):
if M[s] in g:
F=F+[(M[s],1)];
else:
F=F+[(M[s],0)]
return F
</code></pre>
<p>Then do <code>for f in Lab_Funs_iter(M):</code> Do an internet search for "python iterator" to find more about Python iterators. I don't know what problems you're having and I can't test your code because I don't know what <code>plus</code> is supposed to do.</p>
https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60234#post-id-60234Thank you for your comment. It works.Mon, 13 Dec 2021 19:13:12 +0100https://ask.sagemath.org/question/60181/defining-functions-from-a-finite-subset-of-mathbbn-to-a-finite-subset-of-mathbbn/?comment=60234#post-id-60234