vectormath:Python的矢量数学工具
项目描述
vectormath
基于NumPy构建的Python矢量数学工具
为什么
vectormath
包通过利用NumPy提供了一套快速、简单的矢量数学工具库。这使得可以创建显式的几何构造(例如,Vector3
和Plane
),而不必重新定义底层数组数学。
范围
vectormath
包包括Vector3
/Vector2
和Vector3Array
/Vector2Array
。
目标
速度:所有低级操作都依赖于NumPy数组。这些数组密集、有类型,部分在C语言中实现。特别是
VectorArray
类通过一次对所有向量执行向量运算,而不是循环,从而利用了这种速度。简单性:高级操作明确且直接。这个库应该可以被程序员、数学家和地质学家使用。
替代方案
连接
properties将
vectormath
用作底层框架,以实现向量属性。
安装
要安装存储库,请确保您已安装pip并运行
pip install vectormath
对于开发版本
git clone https://github.com/seequent/vectormath.git
cd vectormath
pip install -e .
示例
此示例简要演示了Vector3
和Vector3Array
的一些显著特性。
import numpy as np
import vectormath as vmath
# Single Vectors
v = vmath.Vector3(5, 0, 0)
v.normalize()
print(v) # >> [1, 0, 0]
print(v.x) # >> 1.0
# VectorArrays are much faster than a for loop over Vectors
v_array = vmath.Vector3Array([[4, 0, 0], [0, 2, 0], [0, 0, 3]])
print(v_array.x) # >> [4, 0, 0]
print(v_array.length) # >> [4, 2, 3]
print(v_array.normalize()) # >> [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
# Vectors can be accessed individually or in slices
print(type(v_array[1:])) # >> vectormath.Vector3Array
print(type(v_array[2])) # >> vectormath.Vector3
# All these classes are just numpy arrays
print(isinstance(v, np.ndarray)) # >> True
print(type(v_array[1:, 1:])) # >> numpy.ndarray
当前版本:v0.2.2