Cross product of two vectors in Python

后端 未结 5 768
盖世英雄少女心
盖世英雄少女心 2021-02-18 13:25

How can I calculate the cross product of two vectors without the use of programming libraries?

E.g given vectors a = (1, 2, 3) and b = (4, 5, 6)

5条回答
  •  我寻月下人不归
    2021-02-18 14:09

    I did it like this:

    def cross_product(u,v):  
        dim = len(u)
        s = []
        for i in range(dim):
            if i == 0:
                j,k = 1,2
                s.append(u[j]*v[k] - u[k]*v[j])
            elif i == 1:
                j,k = 2,0
                s.append(u[j]*v[k] - u[k]*v[j])
            else:
                j,k = 0,1
                s.append(u[j]*v[k] - u[k]*v[j])
        return s
    

提交回复
热议问题