Cross product of two vectors in Python

后端 未结 5 748
盖世英雄少女心
盖世英雄少女心 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:01

    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
    

提交回复
热议问题