-1

I am trying to write a numpy function that iterates with itself to update the values of its function. If for example Random_numb was equal to [50, 74, 5, 69, 50]. So the calculations would go like, 10* 50 = 500 for the first calculation, with the equation Starting_val = Starting_val * Random_numb. The Starting_Val would equal to 500 so for the second calculation it would go as 500 * 74 = 37000. Updating the Startin_Val to 37000 from 500. Iterating through the Random_numb as it does the calculations, using element 1: 50 for calculation 1 and element 2 74 for calculation 2 and so on. The calculations would go on until the end of the Random_numb array.

import numpy as np
Starting_val = 10
Random_numb = random.randint(100, size=(5))
Starting_val = Starting_val * Random_numb 

2 Answers 2

1

IIUC you're looking for prod:

import numpy as np

Starting_val = 10
Random_numb = np.array([50, 74, 5, 69, 50])

Random_numb.prod(initial=Starting_val)
#638250000

If you're interested in the multiplied values of the array it'll be cumprod:

Starting_val * Random_numb.cumprod()
# array([      500,     37000,    185000,  12765000, 638250000])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank is there a way I could perform more advanced arithmetic's with multipication division and addition like Starting_val = Starting_val + Starting_val * Random_numb/10
what do you need in the end: just one value (like in prod above) or the whole array (like in cumprod)? It would be best you provide the desired output for the example.
I have posted a new question about it I would appreciate it If you could take a look and perhaps it is more clear. stackoverflow.com/questions/66091981/…
1

Here you are just multiplying the Starting_val with your array of random numbers. You are not updating Starting_val each time. Try out the below code

for i in range(len(Random_numb)):
    Random_numb[i] *= Starting_val
    Starting_val = Random_numb[i]

Hope this solves your query!

1 Comment

I am trying not to use for loops and use numpy instead as it is much faster, but thank you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.