How can I get time efficent by creating an array with for loop?

MaxHuber1234

I have a for loop with a range of 2000 in this for loop I have to create an array called Array out of two other arrays, let's call them ArrayOfPositionSatellite with a size of (3,38) and the other array called ArrayOfPositionMassPoint with a size of (38, 3, 4412). The size of Array is (38,3,4412) and the size of PositonOfSatellite and PointsOfMassPoint is (3, ). My attempt to overwrite the ArrayOfMassPoint with to for-loops :

ArrayOfPositionSatellite=  ArrayOfPositionSatellite.T  
Array = ArrayOfPositionMassPoint
for  i in range(38):
     for k  in range(4412):
            PositionOfSatellite =  ArrayOfPositionSatellite[:,i]
            PositionOfMassPoint=  ArrayOfPositionMassPoint[i,:,k]
            ElementOfA = -Gravitationalconstant* (PositionOfSatellite  - PositionOfMassPoint)/(np.linalg.norm( PositionOfSatellite - PositionOfMassPoint)**3)
            Array[i,:,k] = ElementOfArray

Problem

My problem is that it takes around 3 hours to run the code and this is too long. Is there some way to make it more time-efficient?

If something is unclear please leave a comment and I will add more details.

armamut

You can vectorize your calculations. Like:

import numpy as np
ArrayOfPositionSatellite = np.random.randn(3, 38)
ArrayOfPositionMassPoint = np.random.randn(38, 3, 4412)
Gravitationalconstant = 6.67430e-11

# This is the difference vector
v = ArrayOfPositionMassPoint - ArrayOfPositionSatellite.T[:,:,None]
# This is norm of the difference vector
norm = np.linalg.norm(v, axis=1) ** 3
# This is normalized vector
norm_v = v / norm[:, None, :]
# This is the result
array = norm_v * -Gravitationalconstant
array.shape
>>> (38, 3, 4412)

This takes around ~40ms on my machine, instead of 3 hours.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I get an array from an If statement?

분류에서Dev

How can I pause a loop until I get a users input?

분류에서Dev

How can I get the tkinter entry from a loop

분류에서Dev

How can i get my query execution time in logging

분류에서Dev

How can I get the contents of a file one line at a time?

분류에서Dev

How can I get nth child in array of items retrieved by class?

분류에서Dev

In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

분류에서Dev

Is there anyway I can get the time of cached file?

분류에서Dev

How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

분류에서Dev

How can I organize this array

분류에서Dev

How i can iterate list to get 10 elements each time in java

분류에서Dev

How can I preform a GET request when user input equals current time?

분류에서Dev

How can i get from the List<string> of files each time another file?

분류에서Dev

How can I get a RejectedExecutionException

분류에서Dev

How can I get a solution for this?

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

How can I iterate through a function with for loop?

분류에서Dev

How can I resolve this loop in c#?

분류에서Dev

How can I get $_GET values to a variable

분류에서Dev

How can I flatten this object stream without creating duplicate objects?

분류에서Dev

How can I write a recursive function to get an array representing the set of every permutation of values?

분류에서Dev

How do i stop a while loop after a certain time in C

분류에서Dev

how can i loop for store items i want in ExtJS XTemplate?

분류에서Dev

how can i run 3 while loops simultaneously in unix and update the output of each loop to database as soon we get the output

분류에서Dev

How can I convert date time from time zone A to time zone B in Joda time?

분류에서Dev

MediaElement.js: How can I get percentage of time played (ej. 90%) in order count a complete visioned video

분류에서Dev

for loop and array to get image

분류에서Dev

How can I pass the array object?

분류에서Dev

How can i collect all items of an array

Related 관련 기사

  1. 1

    How can I get an array from an If statement?

  2. 2

    How can I pause a loop until I get a users input?

  3. 3

    How can I get the tkinter entry from a loop

  4. 4

    How can i get my query execution time in logging

  5. 5

    How can I get the contents of a file one line at a time?

  6. 6

    How can I get nth child in array of items retrieved by class?

  7. 7

    In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

  8. 8

    Is there anyway I can get the time of cached file?

  9. 9

    How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

  10. 10

    How can I organize this array

  11. 11

    How i can iterate list to get 10 elements each time in java

  12. 12

    How can I preform a GET request when user input equals current time?

  13. 13

    How can i get from the List<string> of files each time another file?

  14. 14

    How can I get a RejectedExecutionException

  15. 15

    How can I get a solution for this?

  16. 16

    How can I break a symbolic link loop?

  17. 17

    How can I iterate through a function with for loop?

  18. 18

    How can I resolve this loop in c#?

  19. 19

    How can I get $_GET values to a variable

  20. 20

    How can I flatten this object stream without creating duplicate objects?

  21. 21

    How can I write a recursive function to get an array representing the set of every permutation of values?

  22. 22

    How do i stop a while loop after a certain time in C

  23. 23

    how can i loop for store items i want in ExtJS XTemplate?

  24. 24

    how can i run 3 while loops simultaneously in unix and update the output of each loop to database as soon we get the output

  25. 25

    How can I convert date time from time zone A to time zone B in Joda time?

  26. 26

    MediaElement.js: How can I get percentage of time played (ej. 90%) in order count a complete visioned video

  27. 27

    for loop and array to get image

  28. 28

    How can I pass the array object?

  29. 29

    How can i collect all items of an array

뜨겁다태그

보관