首页 > 解决方案 > 如何在 Python 中直接在代码中计算叉积(用于以后的 3D-Plot)?

问题描述

*# 我在脚本下面的解决方法不是很优雅,它是先手动计算叉积(数组格式),然后将数据手动输入到上面的代码(一种 6-tupel-vector 格式)到行cross_AxB 和 cross_BxA 以获得 3D 图。

我想有一种方法可以直接在代码中以可绘图格式计算叉积 np.cross,就像其他操作/函数(如(*、/、np.pi 等)一样)

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


#crossproduct for initial calculation (print below) & then manual input into below cross_AxB and cross_BxA
x = np.array([[0,0,0], [1,2,3]])
y = np.array([[0,0,0], [-3,5,7]])


vector_A = ([0,0,0,1,2,3])
vector_B = ([0,0,0,-3,5,7])
cross_AxB = ([0,   0,   0, -1, -16,  11])
cross_BxA = ([0,   0,   0,  1,  16, -11])

vectors = np.array([vector_A, vector_B, cross_AxB, cross_BxA])

X, Y, Z, U, V, W = zip(*vectors)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
ax.quiver(X, Y, Z, U, V, W)
ax.set_xlim([-10, 10])
ax.set_ylim([-10, 10])
ax.set_zlim([-10, 10])

plt.draw()
print(np.cross(x,y))
print(np.cross(y,x))

标签: pythonvector3d

解决方案


推荐阅读