Creating a Program to Decompose a Number and Run a Function on that Decomposition
I've been puzzling over how to get a certain function working in Python. The function, takes positive integers to other positive integers as follows: Phi_m(n2) = Phi_m(m*n + r) = m*x[n1] + r*(x[n1 + 1] - x[n1]) The above terms are all integer valued and are defined as follows: n2 = the (n2)th slot of the output string m = a fixed positive integer n = some multiple of m such that n*m is less than or equal to n2 r = a remainder term to fill in the amount missing from n*m in decomposing n2 x[n1] = the element in the [n1]th slot of the input string x[n1 + 1] = the element in the [n1 + 1]th slot of the input string In general we start with a string of numbers, say 0, 1, 1, 2, 3, 3 and end up with a string of (k+1)m-1 terms, where k is the number of terms you started with. To use the function we first fix an m, say m = 2. Now we decompose n2 in terms of m, where n2 is representing a 'slot' of our output sequence. Say n2=5. Then we are asking 'what is in the fifth 'slot' of our output string'. In this case our total output string will be of length (5+1)2+1. Notice that we do not count 0 - it is always present and is for our purposes the 0th term, hence we have 5 initial terms. To answer our question of what goes in the slot we take 5=2*2+1 as our decomposition. Now that we have a decomposition we can apply our function: F(x(5)) = F(x(2*2+1)) 2x[2] + 1(x[3] - x[2]). The thing is, for Python to do this it has to know how to decompose each number. So it knows 2 is fixed, and knows 2*3 is too much and so chooses 2*2. Then it has to know this is too little and add remainder 1. Only once it's done this can it actually grab n = 5. That is, it can run the function. It seems clear that once it knows how to do this it can just run through every n in our range, but I'm really not sure how to program the meat of this function. Now to answer some questions: Is x a function? A list? A number? x[n] is essentially a list. What do you mean when you say "values of an input string"? What's the signature of Phi_m? The function acting on this list takes in a single element of the list, gives us a decomposition of the number somehow, and then applies the 'formula' you see above. In this sense it is more of a two step algorithm. To give another example, say we want to apply psi_2 to 0,1,2,2. Then we have an output of length (3+1)2-1=7. F(7)=F(2*3+1) = 2x[3] + 1(x[4] - x[3]). As we can see, we are missing x[4] (remember 0 doesn't count as a term). So we actually need to stop our calculation one shy of the 7 terms we 'should' have. Hence, although we actually want 7 terms the program really only needs to give 6 terms, the other term can be hand calculated, or the user can append one extra term to the input string 0,1,2,2 and run the program again. Please let me know if this is unclear. I will certainly continue revising until it makes sense to those reading. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 5:12:08 AM UTC-4, Joshua Landau wrote: > On 18 July 2013 00:58, CTSB01 wrote: > > > Please let me know if this is unclear. I will certainly continue revising > > until it makes sense to those reading. > > > > Can you summarize what your question is? Leave aside the details of > > the function, just explain what thing in particular you aren't able > > to do. Hi Joshua, I actually managed to find a certain block like this: def phi_m(x, m): ... rtn = [] ... for n2 in range(0, len(x) * m - 2: ... n = n2 / m ... r = n2 - n * m ... rtn.append(m * x[n] + r * (x[n + 1] - x[n])) ... print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn ... rtn However, I am getting the error "expected an indented block" on line two. Any idea why? -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 6:12:52 PM UTC-4, Gary Herron wrote: > On 07/18/2013 02:57 PM, CTSB01 wrote: > > > On Thursday, July 18, 2013 5:12:08 AM UTC-4, Joshua Landau wrote: > > >> On 18 July 2013 00:58, CTSB01 wrote: > > >> > > >>> Please let me know if this is unclear. I will certainly continue > >>> revising until it makes sense to those reading. > > >> > > >> > > >> Can you summarize what your question is? Leave aside the details of > > >> > > >> the function, just explain what thing in particular you aren't able > > >> > > >> to do. > > > Hi Joshua, > > > > > > I actually managed to find a certain block like this: > > > > > > def phi_m(x, m): > > > ... rtn = [] > > > ... for n2 in range(0, len(x) * m - 2: > > That 'for' line has miss-matched parentheses. > > > ... n = n2 / m > > > ... r = n2 - n * m > > > ... rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > > ... print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn > > > ... rtn > > > > > > However, I am getting the error "expected an indented block" on line two. > > Any idea why? > > > > > > -- > > Dr. Gary Herron > > Department of Computer Science > > DigiPen Institute of Technology > > (425) 895-4418 Hi Gary, I fixed that issue, but I still end up with the same error. Specifically: File "", line 2 ... rtn = [] ^ IndentationError: expected an indented block -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 6:49:03 PM UTC-4, Ian wrote: > On Jul 18, 2013 4:23 PM, "CTSB01" wrote: > > > > > > File "", line 2 > > > ... rtn = [] > > > ^ > > The "..." is the continuation prompt from the interactive interpreter, not > part of the code. Don't paste it into Python. Thanks Ian. That worked regarding that issue. Now I have an 'invalid syntax' issue unfortunately. >> def phi_m(x,m): rtn = [] for n2 in range(0, len(x)*m - 2): n = n2 / m r = n2 - n * m rtn.append(m * x[n] + r * (x[n + 1] - x[n])) print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn rtn on the line print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn Is it something obvious? -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 7:45:49 PM UTC-4, Ian wrote: > On Thu, Jul 18, 2013 at 5:42 PM, Ian Kelly wrote: > > > On Thu, Jul 18, 2013 at 5:04 PM, CTSB01 wrote: > > >> Thanks Ian. That worked regarding that issue. Now I have an 'invalid > >> syntax' issue unfortunately. > > >> > > >>>> def phi_m(x,m): > > >> rtn = [] > > >> for n2 in range(0, len(x)*m - 2): > > >> n = n2 / m > > >> r = n2 - n * m > > >> rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > >> print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn > > >> rtn > > >> > > >> on the line print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn Is > >> it something obvious? > > > > > > Are you using Python 2 or 3? "print" has changed from a statement to > > > a function, so the above syntax would be invalid in Python 3. > > > > Note also that in Python 3 you should change the line "n = n2 / m" to > > "n = n2 // m" because the syntax for integer division has also > > changed. > > > > And regardless of your Python version, the last line should probably > > be "return rtn", not just "rtn". Thanks! I'm using 3.3.2. As I'm new to this, do you think it's a better idea to jump to 3.3.2 or stick with 2.7? I, for all intents and purposes, know neither. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
> It's only obvious if you're using Python 3.x. You have forgotten the > > parentheses in the call to the print() function. > > > On the other hand, if this is Python 2.x, I have no idea. Next time, > > please paste the actual error, not paraphrased. The error message > > includes a traceback. and a pointer to where in the line the error was > > detected. If it's pointing at the end of the second token, you must be > > running Python 3.x > And since you're using that annoying googlegroups, see this: > > http://wiki.python.org/moin/GoogleGroupsPython > > > -- > > DaveA Hi Dave, There aren't any emails in the Cc slot so I imagine that part is fine, I will definitely edit the extra quotes though I made the mistake of thinking it was just google being google. For reference , I'm running Python 3.x. I'll try out putting the quotes around it. The full code is: >>> def phi_m(x,m): rtn = [] for n2 in range(0, len(x)*m - 2): n = n2 / m r = n2 - n * m rtn.append(m * x[n] + r * (x[n + 1] - x[n])) print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn rtn SyntaxError: invalid syntax where the second apostrophe in 'n2 =' is marked in orange. Thanks to everyone who's helped out so far, hopefully with some experience I'll be able sort out any syntax issues that come my way. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
Thanks for the alternative links, I'll use gmane.org as an access point next time. > > Don't paraphrase. Just copy/paste it into your email message. And I'm > > assuming you know to run things from the terminal window, and not from > > IDLE or something else that messes up the error messages. Your comment > > about 'orange' doesn't sound promising. > > > > As Ian pointed out, you have no return value in this function. You > > calculate something called 'rtn', but never use it. The last line > > accomplishes nothing, since rtn is neither assigned nor returned, nor > > passed nor... You probably wanted: > > > >return rtn > Does something like def phi_m(x, m): rtn = [] for n2 in range(0, len(x) * m - 2): n = n2 / m r = n2 - n * m rtn.append(m * x[n] + r * (x[n + 1] - x[n])) print ('n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn) return rtn look right? It doesn't seem to have any errors. However, I do receive the following error when trying to implement an x after having defined phi: >>> x = [0, 1, 1, 2, 3] >>> phi_m(x, 2) Traceback (most recent call last): File "", line 1, in phi_m(x, 2) File "", line 6, in phi_m rtn.append(m * x[n] + r * (x[n + 1] - x[n])) TypeError: list indices must be integers, not float -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 10:48:23 PM UTC-4, Fábio Santos wrote: > On 19 Jul 2013 03:24, "CTSB01" wrote: > > > > > > Thanks for the alternative links, I'll use gmane.org as an access point > > next time. > > > > > > > > > > > Don't paraphrase. Just copy/paste it into your email message. And I'm > > > > > > > > assuming you know to run things from the terminal window, and not from > > > > > > > > IDLE or something else that messes up the error messages. Your comment > > > > > > > > about 'orange' doesn't sound promising. > > > > > > > > > > > > > > > > As Ian pointed out, you have no return value in this function. You > > > > > > > > calculate something called 'rtn', but never use it. The last line > > > > > > > > accomplishes nothing, since rtn is neither assigned nor returned, nor > > > > > > > > passed nor... You probably wanted: > > > > > > > > > > > > > > > > return rtn > > > > > > > > > > Does something like > > > > > > def phi_m(x, m): > > > rtn = [] > > > for n2 in range(0, len(x) * m - 2): > > > n = n2 / m > > > r = n2 - n * m > > > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > > print ('n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn) > > > return rtn > > > > > > look right? > > > > > > It doesn't seem to have any errors. However, I do receive the following > > error when trying to implement an x after having defined phi: > > > > > > >>> x = [0, 1, 1, 2, 3] > > > >>> phi_m(x, 2) > > > Traceback (most recent call last): > > > File "", line 1, in > > > phi_m(x, 2) > > > File "", line 6, in phi_m > > > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > > TypeError: list indices must be integers, not float > > When you think about it, it makes sense. If you have a list, say, > > [2, 5, 1] > > You can say, I want the first item (0) or the third item(2) but never, the > one-and-a-halfeth (0.5) item. Python only accepts integer values when > accessing list items. > > To access list items, convert your index into an integer value. Thanks Fabio. Is there a statement that lets me specify that I only need it to take the integer values? -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
On Thursday, July 18, 2013 10:43:11 PM UTC-4, Dave Angel wrote: > On 07/18/2013 10:16 PM, CTSB01 wrote: > > Does something like > > > > > > def phi_m(x, m): > > >rtn = [] > > >for n2 in range(0, len(x) * m - 2): > > > n = n2 / m > > > r = n2 - n * m > > > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > > print ('n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn) > > >return rtn > > look right? > > No, as Ian has pointed out, you need to use the // operator in Python 3 > > if you want to get integer results. So it'd be n = n2 // m > > However, even better is to use the divmod() function, which is intended > > for the purpose: > > > > n, r = divmod(n2, m) > > > It doesn't seem to have any errors. However, I do receive the following > > error when trying to implement an x after having defined phi: > > >>>> x = [0, 1, 1, 2, 3] > > >>>> phi_m(x, 2) > > > Traceback (most recent call last): > > >File "", line 1, in > > > phi_m(x, 2) > > >File "", line 6, in phi_m > > > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > > > TypeError: list indices must be integers, not float > > > That will be fixed if you correct the code as I described, so you'll get > > integers. > > There is a Brezenham algorith that might accomplish this whole function > > more accurately, or more efficiently. But I'd have to re-derive it, as > > it's been about 30 years since I used it. And chances are that the > > efficiencies it brought to machine code won't matter much here. > > -- > > DaveA Thanks Dave, I'll take a look at that. -- http://mail.python.org/mailman/listinfo/python-list
Creating a Simple User Interface for a Function
I have the following code that runs perfectly: def psi_j(x, j): rtn = [] for n2 in range(0, len(x) * j - 2): n = n2 / j r = n2 - n * j rtn.append(j * x[n] + r * (x[n + 1] - x[n])) print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn return rtn This code takes a string x = [0,1,1,1,2] for example (it must always begin with 0) and a parameter j, say 2, and outputs a string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example). It does this in two steps: First it decomposes some number m into a multiple of j and a remainder. Then it runs this decomposition through a function on the rtn.append line. Notice that this has cj - 1 terms where c is the number of terms in the input string and j is the parameter. Normally, we would like it to be able to calculate cj terms. This is an issue with the function that I am more than happy to put aside for the moment. My key interest is to be able to make this program usable for someone who has no knowledge of programming. In particular, I need some kind of user interface that prompts the user to input a string (ideally just by putting in numbers in the form 011123334 for example) and a parameter, and then displays the output sequence. This is essentially what the program already does but the idea is to make it usable for even the most technologically disinclined. Ideally it would do this without needing to run Python at all. If anyone is able to make this happen in Python I would be eternally grateful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a Simple User Interface for a Function
On Thursday, July 25, 2013 3:19:27 PM UTC-4, Dave Angel wrote: > On 07/25/2013 12:03 PM, CTSB01 wrote: > > > I have the following code that runs perfectly: > > > > def psi_j(x, j): > > >rtn = [] > > >for n2 in range(0, len(x) * j - 2): > > > n = n2 / j > > > r = n2 - n * j > > > rtn.append(j * x[n] + r * (x[n + 1] - x[n])) > > > print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn > > >return rtn > > No it doesn't run perfectly. It'll get a syntax error on the print > > function call. That's assuming you're still using Python 3.3. You > > really need to start by specifying your environment, without making us > > look back through previous threads from you. > > > This code takes a string x = [0,1,1,1,2] for example > > That's not a string. A string would be like > > xx = psi_j("0abcd1234") > > Perhaps you mean list? And is it a list of integers, or of arbitrary > > numbers? Are there any constraints on the sizes or signs of those numbers? > > > (it must always begin with 0) and a parameter j, say 2, and outputs a > > string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example). > > > It does this in two steps: First it decomposes some number m into a > > multiple of j and a remainder. > > Only if you replace the / with //. Or just use the function divmod(): > >n, r = divmod(n2, m) > > > Then it runs this decomposition through a function on the rtn.append line. > > > Notice that this has cj - 1 terms where c is the number of terms in the > > input string and j is the parameter. Normally, we would like it to be able > > to calculate cj terms. > > > This is an issue with the function that I am more than happy to put aside > > for the moment. > > > My key interest is to be able to make this program > > So far you have a function, not a program. If you put it in a text file > > and run it from python, it'll do nothing but display a syntax error > > message. And when you fix that, it'll just run without doing anything. > > usable for someone who has no knowledge of programming. In > > particular, I need some kind of user interface that prompts > > > the user to input a string (ideally just by putting in numbers in the form > > 011123334 for example) and a parameter, > > > and then displays the output sequence. This is essentially what the > > program already does but the idea is to make it usable > > > for even the most technologically disinclined. Ideally it would do this > > without needing to run Python at all. > > Then why are you asking on the Python forum? Or perhaps you mean > > without him knowing he's running Python? In that case, use a shebang > > line at the beginning, which will tell Linux to automatically invoke the > > specified program (or programming language in this case). > > > If anyone is able to make this happen in Python I would be eternally > > grateful. > > If we assume you're running Python 3.3 on Linux, and the user is willing > > to us the terminal, then how about parsing the string from the command > > line he types? You can access it as011123334 a string from sys.argv, > > and convert it to separate numbers. Of course as it stands now, you > > cannot tell whether the user wanted > >0,1,1,1,2,3,3,3,4 > > or > >0, 111, 23, 3, 3, 4 > > or something else. > > DaveA Sorry Dave, to answer each part of your response: 1) I decided to use Python 2.7, and I will be sure to specify this in all future threads. 2) It is a list of positive integers. In fact, it is always going to be a list of positive increasing integers. 3) You're right. What I meant was that if after running that bit of code I enter >>> x = [0,1,2,3,4,5] >>> psi_j(x,2) I will get output that matches my requirements. 4) Yes, sorry that's what I meant (if I understood correctly). I was told elsewhere that I might want to try using tkinter. Essentially I'm trying to create a user interface that allows the user to just type in a string 01112345 for example, and choose a parameter (say j=2) and then click a button to run the function. I'd like to be able to run send a .exe file that the user can just open up and use with no further setup. So on top of the user interface I would also it looks like need to determine how to make Python change a string 01112345 into a list so that it does that automatically when the user clicks 'run'. Would a shebang still be the right way to go? Thanks again Dave, apologies for the ambiguity. -- http://mail.python.org/mailman/listinfo/python-list