0

I have been trying to get this program to work using Numpy in Python. Here is my code.

import numpy as np

def function(inNum):
    return inNum - 1


i = 1
n = 50000000
AllOutputs = np.array([])
while(i < n):
    SingularOutput = np.array([])
    num = i

    while(num > 1):
        print(num)
        num = function(num)
        np.append(SingularOutput,num)
    np.append(AllOutputs,SingularOutput)

    i = i + 1
    print(i)

The output in the console gives decreasing numbers, here is a snippet.

269
268
267
266
265
264
263
262
261
260
259
258
257
256
255
254

It seems as if is being assigned the values returned from function(inNum), but as it can be seen i is not ever assigned any value other than i = i + 1, thus I do not know why this is happening. Lastly, if I modify the num = i assignment to nums = 1 (or I assume anything else) I get the expected output of

1
2
3
4
5
...(etc)

What am I doing wrong?

6
  • 1
    are you really looking for np.arange? Commented Jan 31, 2022 at 19:02
  • 3
    as an aside, don't use numpy.append in a loop Commented Jan 31, 2022 at 19:04
  • 2
    You have two sequences being printed. On each iteration of the outer loop, you print a descending sequence from num to 2, followed by the value of i. The i values do increase with each iteration, but most of the output comes from the decreasing sequence. Commented Jan 31, 2022 at 19:07
  • 1
    When you set num = 1, you prevent the inner loop from executing at all, so all you see is the increasing values of i produced by the outer loop. Commented Jan 31, 2022 at 19:11
  • 1
    The statement np.append(SingularOutput,num) doesn't modify SingularOutput. It creates a new array--which you never use. See numpy.org/doc/stable/reference/generated/numpy.append.html Commented Jan 31, 2022 at 19:12

1 Answer 1

1

Don't try to imitate list methods with numpy:

AllOutputs = []
while(i < n):
    SingularOutput = []
    num = i

    while(num > 1):
        print(num)
        num = function(num)
        SingularOutput.append(num)
    AllOutputs.append(SingularOutput)

print(AllOutputs)
arr = np.array(AllOutputs)
print(arr)
i = i + 1
print(i)

np.empty([]) and np.append are not clones of [] and list.append. Please read the docs with care!

Running the code I see that you are making sublists that differ in length. There's no point to using numpy for this.

In [39]: i=1; n=6
    ...: AllOutputs = []
    ...: while(i < n):
    ...:     SingularOutput = []
    ...:     num = i
    ...:     while(num > 1):
    ...:         print('num',num)
    ...:         num = fun(num)
    ...:         SingularOutput.append(num)
    ...:     AllOutputs.append(SingularOutput)
    ...:     i = i+1
    ...:     print('i',i)
    ...: 
i 2
num 2
i 3
num 3
num 2
i 4
num 4
num 3
num 2
i 5
num 5
num 4
num 3
num 2
i 6
In [40]: AllOutputs
Out[40]: [[], [1], [2, 1], [3, 2, 1], [4, 3, 2, 1]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.