简体   繁体   English

Python中两个向量的叉积

[英]Cross product of two vectors in Python

How can I calculate the cross product of two vectors without the use of programming libraries?如何在不使用编程库的情况下计算两个向量的叉积?

Eg given vectors a = (1, 2, 3) and b = (4, 5, 6)例如,给定向量a = (1, 2, 3)b = (4, 5, 6)

are you asking about the formula for the cross product? 你在问交叉产品的配方吗? Or how to do indexing and lists in python? 或者如何在python中做索引和列表?

The basic idea is that you access the elements of a and b as a[0], a[1], a[2], etc. (for x, y, z) and that you create a new list with [element_0, element_1, ...]. 基本思想是你将a和b的元素作为[0],a [1],a [2]等(对于x,y,z)访问,并使用[element_0创建一个新列表, element_1,...]。 We can also wrap it in a function. 我们也可以将它包装在一个函数中。

On the vector side, the cross product is the antisymmetric product of the elements, which also has a nice geometrical interpretation. 在矢量方面,叉积是元素的反对称乘积,它也具有很好的几何解释。

Anyway, it would be better to give you hints and let you figure it out, but that's not really the SO way, so... 无论如何,最好给你提示并让你搞清楚,但那不是真正的SO方式,所以...

def cross(a, b):
    c = [a[1]*b[2] - a[2]*b[1],
         a[2]*b[0] - a[0]*b[2],
         a[0]*b[1] - a[1]*b[0]]

    return c
import numpy as np
a = np.array([1,0,0])  
b = np.array([0,1,0])  
print np.cross(a,b)

If you want to implement the cross product yourself you may see http://en.wikipedia.org/wiki/Vector_cross_product or a math/physics book. 如果您想自己实现跨产品,可以查看http://en.wikipedia.org/wiki/Vector_cross_product或数学/物理书籍。 Shortly (a1, a2, a3) X (b1, b2, b3) = (a2*b3-a3*b2, a3*b1-a1*b3, a1*b2-a2*b1) 不久(a1,a2,a3)X(b1,b2,b3)=(a2 * b3-a3 * b2,a3 * b1-a1 * b3,a1 * b2-a2 * b1)

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

for multiple dimensions, this might work; 对于多个维度,这可能有用;

    def crossProd(a,b):
      dimension = len(a)
      c = []
      for i in range(dimension):
        c.append(0)
        for j in range(dimension):
          if j <> i:
            for k in range(dimension):
              if k <> i:
                if k > j:
                  c[i] += a[j]*b[k]
                elif k < j:
                  c[i] -= a[j]*b[k]
      return c

I defined a successror funtion z,This is to help write the formulas of the cross product In a slightly consise way.here is the code我定义了一个后继函数z,这是为了帮助以稍微简洁的方式编写叉积的公式。这是代码

    from numpy import zeros
    def z(a):
      if a == 0 or a == 1:
       return a+1
      elif a == 2:
       return 0
    n = 3
    i = 0
    v = zeros(n, float)
    v1 = zeros(n, float)
    v2 = zeros(n, float)
    v1[0] = float(input("enter x component of v1 "))
    v1[1] = float(input("enter y component of v1 "))
    v1[2] = float(input("enter z component of v1 "))
    v2[0] = float(input("enter x component of v2 "))
    v2[1] = float(input("enter y component of v2 "))
    v2[2] = float(input("enter z component of v2 "))


    def cp(x, y):
     global i
     while i < n:
      v[i] = x[z(i)]*y[z(z(i))]-x[z(z(i))]*y[z(i)]
      i = i + 1
     return v


    ans = cp(v1, v2)
    print(ans)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM