Возникли проблемы с получением времени для работы с NumPy - PullRequest
0 голосов
/ 15 октября 2019

Я просто хочу посмотреть, сколько времени займет выполнение этого кода. Здесь есть похожий вопрос:

модуль timeit в python не распознает модуль numpy

, и я понимаю, что они говорят, но я не понимаю, где этистроки кода должны быть размещены. Вот что у меня есть. Я знаю, что это немного долго, чтобы прокрутить, но вы можете видеть, где я поместил команды timeit в начале и в конце. Это не работает, и я предполагаю, что это потому, что я поместил эти строки кода для timeit неправильно. Код работает, если я удаляю материал timeit.

Спасибо

import timeit

u = timeit.Timer("np.arange(1000)", setup = 'import numpy as np')

#set up variables

m = 4.54
g = 9.81
GR = 8
r_pulley = .1
th1=np.pi/4   #based on motor 1 encoder counts. Number of degrees rotated from + x-axis of base frame 0
th2=np.pi/4   #based on motor 2 encoder counts. Number of degrees rotated from + x-axis of m1 frame 1
th3_motor = np.pi/4*12
th3_pulley = th3_motor/GR

#required forces in x,y,z at end effector
fx = 1
fy = 1
fz = m*g  #need to figure this out


l1=6
l2=5
l3=th3_pulley*r_pulley 

#Build Homogeneous Tranforms Matrices

H1_0 = np.array(([np.cos(th1),-np.sin(th1),0,0],[np.sin(th1),np.cos(th1),0,0],[0,0,1,l3],[0,0,0,1]))
H2_1 = np.array(([np.cos(th2),-np.sin(th2),0,l1],[np.sin(th2),np.cos(th2),0,0],[0,0,1,0],[0,0,0,1]))
H3_2 = np.array(([1,0,0,l2],[0,1,0,0],[0,0,1,0],[0,0,0,1]))
H2_0 = np.dot(H1_0,H2_1)
H3_0 = np.dot(H2_0,H3_2)
print(np.matrix(H3_0))

#These HTMs are using the way I derived them, not the "correct" way. 
#The answers are the same, but I think the processing time will be the same. 
#This is because either way the two matrices with all the sines and cosines...
#will be the same. Only difference is in one method the ones and zeroes...
#matrix is the first HTM, in the other method it is the last HTM. So its the...
#same number of matrices with the same information, just being dot-producted...
#in a different order. 

#Build Jacobian

#np.cross(x, y)

d10 = H1_0[0:3, 3]
d20 = H2_0[0:3, 3]
d30 = H3_0[0:3, 3]
print(d30)

subt1 = d30-d10
subt2 = d30-d20
#tsubt1 = subt1.transpose()
#tsubt2 = subt2.transpose()
#print(tsubt1)
zeroes = np.array(([0,0,1]))

print(subt1)
print(subt2)

cross1 = np.cross(zeroes, subt1)

cross2 = np.cross(zeroes, subt2)

cross1
cross2



#These cross products are correct but need to be tranposed into columns, right now they are a single row.

#tcross1=cross1.reshape(-1,1)
#tcross2=cross2.reshape(-1,1)

#dont actually need these transposes but I didnt want to forget the command. 

# build jacobian (J)


#J = np.zeros((6,2))
#J[0:3,0] = cross1
#J[0:3,1] = cross2
#J[3:6,0] = zeroes
#J[3:6,1] = zeroes
#J

#find torques
J_force = np.zeros((2,3))
J_force[0,:]=cross1
J_force[1,:]=cross2
J_force

#build force matrix
forces = np.array(([fx],[fy],[fz]))
forces

torques = np.dot(J_force,forces)
torques #top number is theta 1 (M1) and bottom number is theta 2 (M2)



#need to add z axis?

print(u.timeit())

1 Ответ

1 голос
/ 15 октября 2019
# u is a timer eval np.arange(1000)
u = timeit.Timer("np.arange(1000)", setup = 'import numpy as np')
# print how many seconds needed to run np.arange(1000) 1000000 times
# 1000000 is the default value, you can set by passing a int here.
print(u.timeit())

Итак, вот что вы хотите.

import timeit

def main():
    #set up variables

    m = 4.54
    g = 9.81
    GR = 8
    r_pulley = .1
    th1=np.pi/4   #based on motor 1 encoder counts. Number of degrees rotated from + x-axis of base frame 0
    th2=np.pi/4   #based on motor 2 encoder counts. Number of degrees rotated from + x-axis of m1 frame 1
    th3_motor = np.pi/4*12
    th3_pulley = th3_motor/GR

    #required forces in x,y,z at end effector
    fx = 1
    fy = 1
    fz = m*g  #need to figure this out


    l1=6
    l2=5
    l3=th3_pulley*r_pulley 

    #Build Homogeneous Tranforms Matrices

    H1_0 = np.array(([np.cos(th1),-np.sin(th1),0,0],[np.sin(th1),np.cos(th1),0,0],[0,0,1,l3],[0,0,0,1]))
    H2_1 = np.array(([np.cos(th2),-np.sin(th2),0,l1],[np.sin(th2),np.cos(th2),0,0],[0,0,1,0],[0,0,0,1]))
    H3_2 = np.array(([1,0,0,l2],[0,1,0,0],[0,0,1,0],[0,0,0,1]))
    H2_0 = np.dot(H1_0,H2_1)
    H3_0 = np.dot(H2_0,H3_2)
    print(np.matrix(H3_0))

    #These HTMs are using the way I derived them, not the "correct" way. 
    #The answers are the same, but I think the processing time will be the same. 
    #This is because either way the two matrices with all the sines and cosines...
    #will be the same. Only difference is in one method the ones and zeroes...
    #matrix is the first HTM, in the other method it is the last HTM. So its the...
    #same number of matrices with the same information, just being dot-producted...
    #in a different order. 

    #Build Jacobian

    #np.cross(x, y)

    d10 = H1_0[0:3, 3]
    d20 = H2_0[0:3, 3]
    d30 = H3_0[0:3, 3]
    print(d30)

    subt1 = d30-d10
    subt2 = d30-d20
    #tsubt1 = subt1.transpose()
    #tsubt2 = subt2.transpose()
    #print(tsubt1)
    zeroes = np.array(([0,0,1]))

    print(subt1)
    print(subt2)

    cross1 = np.cross(zeroes, subt1)

    cross2 = np.cross(zeroes, subt2)

    cross1
    cross2



    #These cross products are correct but need to be tranposed into columns, right now they are a single row.

    #tcross1=cross1.reshape(-1,1)
    #tcross2=cross2.reshape(-1,1)

    #dont actually need these transposes but I didnt want to forget the command. 

    # build jacobian (J)


    #J = np.zeros((6,2))
    #J[0:3,0] = cross1
    #J[0:3,1] = cross2
    #J[3:6,0] = zeroes
    #J[3:6,1] = zeroes
    #J

    #find torques
    J_force = np.zeros((2,3))
    J_force[0,:]=cross1
    J_force[1,:]=cross2
    J_force

    #build force matrix
    forces = np.array(([fx],[fy],[fz]))
    forces

    torques = np.dot(J_force,forces)
    torques #top number is theta 1 (M1) and bottom number is theta 2 (M2)

    #need to add z axis?

u = timeit.Timer(main)
print(u.timeit(5))
...