mindspore-quaternion


Namemindspore-quaternion JSON
Version 0.7.1 PyPI version JSON
download
home_pagehttps://gitee.com/dechin/mindspore-quaternion
SummaryQuaternion data structure based on MindSpore Framework
upload_time2023-11-03 09:51:57
maintainer
docs_urlNone
authorDechin CHEN
requires_python
licenseMIT Licence
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # mindspore-quaternion--基于MindSpore深度学习框架的四元数数据结构
![](https://img.shields.io/pypi/l/mindspore-quaternion)

四元数在自动化等领域有非常广泛的应用,其必要性在于解决了使用欧拉角旋转的过程中有可能出现的欧拉角死锁问题。
关于更多的四元数的原理和计算方法,可以阅读本readme末尾的参考博客。

## 安装与更新
本软件支持pip一键安装与更新(这里推荐使用pypi原始的资源,国内的一些镜像源同步的并不是很及时):
```bash
$ python3 -m pip install mindspore-quaternion --upgrade -i https://pypi.Python.org/simple/
```
也可以使用源码进行安装:
```bash
$ git clone https://gitee.com/dechin/mindspore-quaternion.git
$ cd mindspore-quaternion
$ python3 setup.py install
```

## 定义四元数
详细的代码可以参考[该链接](https://gitee.com/dechin/mindspore-quaternion/tree/master/examples)下的内容,这里我们只作为简单介绍。首先我们需要import一些必要的软件包:
```python
from quaternion import Quaternion
import numpy as np
import mindspore as ms
from mindspore import Tensor
```
接下来我们可以看一下各个不同shape的四元数定义。首先是定义一个常数的四元数,其实也就是`[s, 0, 0, 0]`这样的一个四元数:
```python
element = Tensor([0.], ms.float32)
element_quaternion = Quaternion(element)
print ('The type of element is: {}'.format(type(element_quaternion)))
print ('The value of element is: {}'.format(element_quaternion.to_tensor()))
```
上述代码的执行结果为:
```bash
The type of element is: <class 'quaternion.Quaternion'>
The value of element is: [[0. 0. 0. 0.]]
```
还可以定义三维矢量所对应的四元数,是如同`[0, vx, vy, vz]`这样的形式:
```python
vector = Tensor(np.arange(3), ms.float32)
vector_quaternion = Quaternion(vector)
print('The type of vector is: {}'.format(type(vector_quaternion)))
print('The value of vector is: {}'.format(vector_quaternion.to_tensor()))
```
上述代码的执行结果为:
```bash
The type of vector is: <class 'quaternion.Quaternion'>
The value of vector is: [[0. 0. 1. 2.]]
```
当然,如果一开始我们就定义好了一个完整的四维的四元数`[s, vx, vy, vz]`,那么也是可以用来直接构造一个四元数对象的:
```python
quater = Tensor(np.arange(4), ms.float32)
quater_quaternion = Quaternion(quater)
print('The type of quater is: {}'.format(type(quater_quaternion)))
print('The value of quater is: {}'.format(quater_quaternion.to_tensor()))
```
上述代码的执行结果为:
```bash
The type of quater is: <class 'quaternion.Quaternion'>
The value of quater is: [[0. 1. 2. 3.]]
```
这里必须要提一个最重要的point,那就是当我们使用一个深度学习框架去实现四元数这样的数据结构的时候,我们当然更多的是考虑到GPU加速在多batch的四元数计算
下的性能优势。所以这里我们也可以去定义一个更高维度的,多batch的四元数:
```python
batch_quater = Tensor(np.arange(16).reshape((4, 4)), ms.float32)
batch_quater_quaternion = Quaternion(batch_quater)
print('The type of batch_quater is: {}'.format(type(batch_quater_quaternion)))
print('The value of batch_quater is: {}'.format(batch_quater_quaternion.to_tensor()))
```
上述代码的执行结果为:
```bash
The type of batch_quater is: <class 'quaternion.Quaternion'>
The value of batch_quater is: [[ 0.  1.  2.  3.]
                               [ 4.  5.  6.  7.]
                               [ 8.  9. 10. 11.]
                               [12. 13. 14. 15.]]
```

## 四元数运算
在mindspore-quaternion中我们支持了一些四元数的基本运算,这里我们仅展示一些最常用的四元数基本运算:
```python
from quaternion import Quaternion
import mindspore as ms
from mindspore import Tensor
q1 = Tensor([1, 2, 3, 4], ms.float32)
q1_quaternion = Quaternion(q1)
q2 = Tensor([3, 1, 0, 5], ms.float32)
q2_quaternion = Quaternion(q2)
print ('The sum of q1 and q2 is: {}'.format(q1_quaternion + q2_quaternion))
print ('The product of q1 and q2 is: {}'.format(q1_quaternion * q2_quaternion))
```
上述代码的运行结果如下:
```bash
The sum of q1 and q2 is: [[4. 3. 3. 9.]]
The product of q1 and q2 is: [[-19.  22.   3.  14.]]
```
除了普通的四元数运算之外,mindspore-quaternion还可用于计算四元数与向量的哈密顿积:
```python
vector = Tensor([1, 2, 3], ms.float32)
hamiltonian = Tensor([0.5, 0.5, 0.5, 0.5], ms.float32)
hamiltonian_quaternion = Quaternion(hamiltonian)
print ('The Hamiltonian product of hamiltonian | vector > is: {}'.format(hamiltonian_quaternion | vector))
```
上述代码的运行结果如下:
```bash
The Hamiltonian product of hamiltonian | vector > is: [[3. 1. 2.]]
```
同时我们也可以使用逆变换:
```python
vector_transform = hamiltonian_quaternion.conjugate() | (hamiltonian_quaternion | vector)
print('The Hamiltonian product of hamiltonian^(-1) | hamiltonian | vector > is: {}'.format(vector_transform))
```
得到的结果如下所示:
```bash
The Hamiltonian product of hamiltonian^(-1) | hamiltonian | vector > is: [[1. 2. 3.]]
```
可用发现,我们经过一重四元数的变换和一重对应的逆变换之后,又得到了跟原来一样的向量。除此之外,我们最经常使用四元数的场景,
是对空间向量之间进行一个变换,比如我们可以获取两个向量之间的旋转四元数。首先我们需要定义一些初始向量和目标向量:
```python
src_vector = Tensor([1, 0, 0], ms.float32)
src_vector_quaternion = Quaternion(src_vector)
dst_vector1 = Tensor([0, 0, 1], ms.float32)
dst_vector1_quaternion = Quaternion(dst_vector1)
dst_vector2 = Tensor([np.sqrt(3) / 3, np.sqrt(3) / 3, np.sqrt(3) / 3], ms.float32)
dst_vector2_quaternion = Quaternion(dst_vector2)
```
然后可以研究其中从初始向量到目标向量的变换四元数,再使用变换四元数作用到初始向量上:
```python
transform_quaternion = src_vector_quaternion >> dst_vector1_quaternion
print ('The quaternion transform from vector to target_vector is: {}'.format(transform_quaternion))
print ('The transform result is: {}'.format(transform_quaternion | src_vector))
transform_quaternion = src_vector_quaternion >> dst_vector2_quaternion
print('The quaternion transform from vector to target_vector is: {}'.format(transform_quaternion))
print('The transform result is: {}'.format(transform_quaternion | src_vector))
```
运行的结果如下所示:
```bash
The quaternion transform from vector to target_vector is: [[ 0.70710677  0.         -0.7071068   0.        ]]
The transform result is: [[0.         0.         0.99999994]]
The quaternion transform from vector to target_vector is: [[ 0.88807386  0.         -0.32505763  0.32505763]]
The transform result is: [[0.57735026 0.5773504  0.5773504 ]]
```
以上就是一些常用的四元数操作,更多操作可以参考`examples/`路径下的示例。

## 函数式运算
为了更好的支持MindSpore框架中对计算图的编译,这里如果我们使用MindSpore框架去构建一个Quaternion的对象,会引发Jit-FallBack
的报错,因此需要支持一些函数式的操作——保留Tensor的数据结构,仅引入一些四元数的操作。下述代码是一些比较简单的案例:
```python
from quaternion.functions import quaternion_multiply, quaternion_inverse, hamiltonian_product, quaternion_diff
import mindspore as ms
from mindspore import Tensor
import numpy as np

hamiltonian = Tensor([0.5, 0.5, 0.5, 0.5], ms.float32)
q1 = Tensor([1, 2, 3, 4], ms.float32)
q2 = Tensor([3, 1, 0, 5], ms.float32)
vector = Tensor([1, 2, 3], ms.float32)
src_vector = Tensor([1, 0, 0], ms.float32)
dst_vector1 = Tensor([0, 0, 1], ms.float32)
dst_vector2 = Tensor([np.sqrt(3) / 3, np.sqrt(3) / 3, np.sqrt(3) / 3], ms.float32)

assert np.allclose(quaternion_inverse(hamiltonian).asnumpy(), np.array([[0.5, -0.5, -0.5, -0.5]], np.float32))
assert np.allclose(quaternion_multiply(q1, q2).asnumpy(), np.array([[-19., 22., 3., 14.]], np.float32))
assert np.allclose(quaternion_multiply(q2, q1).asnumpy(), np.array([[-19., -8., 15., 20.]], np.float32))
assert np.allclose(hamiltonian_product(hamiltonian, vector).asnumpy(), np.array([[0., 3., 1., 2.]], np.float32))
assert np.allclose(quaternion_diff(src_vector, dst_vector1).asnumpy(),
                       np.array([[0.70710677,  0.,         -0.7071068,   0.]], np.float32))
assert np.allclose(quaternion_diff(src_vector, dst_vector2).asnumpy(),
                       np.array([[0.88807386,  0.,         -0.32505763,  0.32505763]], np.float32))
```
如果运行正确,上述代码可以正常执行通过,否则会报错。

## Numpy函数式运算
为了方便本地环境中没有MindSpore的用户使用,mindspore-quaternion也提供了numpy版本的四元数运算支持,使用方法与上述
函数式运算中的内容基本一致,相关接口为:
```python
from quaternion.np_functions import inverse, multiply, hamiltonian, transform
```
并且,基于numpy版本的四元数运算,用户可以对蛋白质的pdb文件进行旋转平移变换的操作,例如:
```python
import numpy as np
from quaternion.rotate_pdb import rotate

if __name__ == '__main__':
    pdb_in = 'test_in.pdb'
    pdb_out = 'test_out.pdb'
    # 新原点
    o1 = np.array([42.882, 32.025, 38.245], np.float32)
    # 新X轴
    x1 = np.array([38.326, 24.174, 48.484], np.float32)
    # 新XY平面上的一个点,但不能在OX方向上
    m1 = np.array([53.187, 51.701, 33.301], np.float32)
    rotate(pdb_in, pdb_out, o1, x1, m1)
```
这样,就可以把经过旋转和平移变换之后的坐标保存到指定的新的pdb文件中。

## 代码贡献
欢迎外部协作者提PR过来,有如下几点PR要求:
1. 先提Issue再提交PR,说清楚特性;
2. 贡献代码部分的flake8需要清空。

## 参考博客
1. https://www.cnblogs.com/dechinphy/p/quaternion-calc.html

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitee.com/dechin/mindspore-quaternion",
    "name": "mindspore-quaternion",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Dechin CHEN",
    "author_email": "dechin.phy@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/81/74/8ba0baa9cac5f02ea27744edb0f74406cd2c60689cb782e277de5246a1b8/mindspore-quaternion-0.7.1.tar.gz",
    "platform": "any",
    "description": "# mindspore-quaternion--\u57fa\u4e8eMindSpore\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u7684\u56db\u5143\u6570\u6570\u636e\u7ed3\u6784\n![](https://img.shields.io/pypi/l/mindspore-quaternion)\n\n\u56db\u5143\u6570\u5728\u81ea\u52a8\u5316\u7b49\u9886\u57df\u6709\u975e\u5e38\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5176\u5fc5\u8981\u6027\u5728\u4e8e\u89e3\u51b3\u4e86\u4f7f\u7528\u6b27\u62c9\u89d2\u65cb\u8f6c\u7684\u8fc7\u7a0b\u4e2d\u6709\u53ef\u80fd\u51fa\u73b0\u7684\u6b27\u62c9\u89d2\u6b7b\u9501\u95ee\u9898\u3002\n\u5173\u4e8e\u66f4\u591a\u7684\u56db\u5143\u6570\u7684\u539f\u7406\u548c\u8ba1\u7b97\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9605\u8bfb\u672creadme\u672b\u5c3e\u7684\u53c2\u8003\u535a\u5ba2\u3002\n\n## \u5b89\u88c5\u4e0e\u66f4\u65b0\n\u672c\u8f6f\u4ef6\u652f\u6301pip\u4e00\u952e\u5b89\u88c5\u4e0e\u66f4\u65b0\uff08\u8fd9\u91cc\u63a8\u8350\u4f7f\u7528pypi\u539f\u59cb\u7684\u8d44\u6e90\uff0c\u56fd\u5185\u7684\u4e00\u4e9b\u955c\u50cf\u6e90\u540c\u6b65\u7684\u5e76\u4e0d\u662f\u5f88\u53ca\u65f6\uff09\uff1a\n```bash\n$ python3 -m pip install mindspore-quaternion --upgrade -i https://pypi.Python.org/simple/\n```\n\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6e90\u7801\u8fdb\u884c\u5b89\u88c5\uff1a\n```bash\n$ git clone https://gitee.com/dechin/mindspore-quaternion.git\n$ cd mindspore-quaternion\n$ python3 setup.py install\n```\n\n## \u5b9a\u4e49\u56db\u5143\u6570\n\u8be6\u7ec6\u7684\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003[\u8be5\u94fe\u63a5](https://gitee.com/dechin/mindspore-quaternion/tree/master/examples)\u4e0b\u7684\u5185\u5bb9\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u4f5c\u4e3a\u7b80\u5355\u4ecb\u7ecd\u3002\u9996\u5148\u6211\u4eec\u9700\u8981import\u4e00\u4e9b\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\uff1a\n```python\nfrom quaternion import Quaternion\nimport numpy as np\nimport mindspore as ms\nfrom mindspore import Tensor\n```\n\u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b\u5404\u4e2a\u4e0d\u540cshape\u7684\u56db\u5143\u6570\u5b9a\u4e49\u3002\u9996\u5148\u662f\u5b9a\u4e49\u4e00\u4e2a\u5e38\u6570\u7684\u56db\u5143\u6570\uff0c\u5176\u5b9e\u4e5f\u5c31\u662f`[s, 0, 0, 0]`\u8fd9\u6837\u7684\u4e00\u4e2a\u56db\u5143\u6570\uff1a\n```python\nelement = Tensor([0.], ms.float32)\nelement_quaternion = Quaternion(element)\nprint ('The type of element is: {}'.format(type(element_quaternion)))\nprint ('The value of element is: {}'.format(element_quaternion.to_tensor()))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e3a\uff1a\n```bash\nThe type of element is: <class 'quaternion.Quaternion'>\nThe value of element is: [[0. 0. 0. 0.]]\n```\n\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e09\u7ef4\u77e2\u91cf\u6240\u5bf9\u5e94\u7684\u56db\u5143\u6570\uff0c\u662f\u5982\u540c`[0, vx, vy, vz]`\u8fd9\u6837\u7684\u5f62\u5f0f\uff1a\n```python\nvector = Tensor(np.arange(3), ms.float32)\nvector_quaternion = Quaternion(vector)\nprint('The type of vector is: {}'.format(type(vector_quaternion)))\nprint('The value of vector is: {}'.format(vector_quaternion.to_tensor()))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e3a\uff1a\n```bash\nThe type of vector is: <class 'quaternion.Quaternion'>\nThe value of vector is: [[0. 0. 1. 2.]]\n```\n\u5f53\u7136\uff0c\u5982\u679c\u4e00\u5f00\u59cb\u6211\u4eec\u5c31\u5b9a\u4e49\u597d\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u56db\u7ef4\u7684\u56db\u5143\u6570`[s, vx, vy, vz]`\uff0c\u90a3\u4e48\u4e5f\u662f\u53ef\u4ee5\u7528\u6765\u76f4\u63a5\u6784\u9020\u4e00\u4e2a\u56db\u5143\u6570\u5bf9\u8c61\u7684\uff1a\n```python\nquater = Tensor(np.arange(4), ms.float32)\nquater_quaternion = Quaternion(quater)\nprint('The type of quater is: {}'.format(type(quater_quaternion)))\nprint('The value of quater is: {}'.format(quater_quaternion.to_tensor()))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e3a\uff1a\n```bash\nThe type of quater is: <class 'quaternion.Quaternion'>\nThe value of quater is: [[0. 1. 2. 3.]]\n```\n\u8fd9\u91cc\u5fc5\u987b\u8981\u63d0\u4e00\u4e2a\u6700\u91cd\u8981\u7684point\uff0c\u90a3\u5c31\u662f\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u53bb\u5b9e\u73b0\u56db\u5143\u6570\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5f53\u7136\u66f4\u591a\u7684\u662f\u8003\u8651\u5230GPU\u52a0\u901f\u5728\u591abatch\u7684\u56db\u5143\u6570\u8ba1\u7b97\n\u4e0b\u7684\u6027\u80fd\u4f18\u52bf\u3002\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u4e5f\u53ef\u4ee5\u53bb\u5b9a\u4e49\u4e00\u4e2a\u66f4\u9ad8\u7ef4\u5ea6\u7684\uff0c\u591abatch\u7684\u56db\u5143\u6570\uff1a\n```python\nbatch_quater = Tensor(np.arange(16).reshape((4, 4)), ms.float32)\nbatch_quater_quaternion = Quaternion(batch_quater)\nprint('The type of batch_quater is: {}'.format(type(batch_quater_quaternion)))\nprint('The value of batch_quater is: {}'.format(batch_quater_quaternion.to_tensor()))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e3a\uff1a\n```bash\nThe type of batch_quater is: <class 'quaternion.Quaternion'>\nThe value of batch_quater is: [[ 0.  1.  2.  3.]\n                               [ 4.  5.  6.  7.]\n                               [ 8.  9. 10. 11.]\n                               [12. 13. 14. 15.]]\n```\n\n## \u56db\u5143\u6570\u8fd0\u7b97\n\u5728mindspore-quaternion\u4e2d\u6211\u4eec\u652f\u6301\u4e86\u4e00\u4e9b\u56db\u5143\u6570\u7684\u57fa\u672c\u8fd0\u7b97\uff0c\u8fd9\u91cc\u6211\u4eec\u4ec5\u5c55\u793a\u4e00\u4e9b\u6700\u5e38\u7528\u7684\u56db\u5143\u6570\u57fa\u672c\u8fd0\u7b97\uff1a\n```python\nfrom quaternion import Quaternion\nimport mindspore as ms\nfrom mindspore import Tensor\nq1 = Tensor([1, 2, 3, 4], ms.float32)\nq1_quaternion = Quaternion(q1)\nq2 = Tensor([3, 1, 0, 5], ms.float32)\nq2_quaternion = Quaternion(q2)\nprint ('The sum of q1 and q2 is: {}'.format(q1_quaternion + q2_quaternion))\nprint ('The product of q1 and q2 is: {}'.format(q1_quaternion * q2_quaternion))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n```bash\nThe sum of q1 and q2 is: [[4. 3. 3. 9.]]\nThe product of q1 and q2 is: [[-19.  22.   3.  14.]]\n```\n\u9664\u4e86\u666e\u901a\u7684\u56db\u5143\u6570\u8fd0\u7b97\u4e4b\u5916\uff0cmindspore-quaternion\u8fd8\u53ef\u7528\u4e8e\u8ba1\u7b97\u56db\u5143\u6570\u4e0e\u5411\u91cf\u7684\u54c8\u5bc6\u987f\u79ef\uff1a\n```python\nvector = Tensor([1, 2, 3], ms.float32)\nhamiltonian = Tensor([0.5, 0.5, 0.5, 0.5], ms.float32)\nhamiltonian_quaternion = Quaternion(hamiltonian)\nprint ('The Hamiltonian product of hamiltonian | vector > is: {}'.format(hamiltonian_quaternion | vector))\n```\n\u4e0a\u8ff0\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n```bash\nThe Hamiltonian product of hamiltonian | vector > is: [[3. 1. 2.]]\n```\n\u540c\u65f6\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9006\u53d8\u6362\uff1a\n```python\nvector_transform = hamiltonian_quaternion.conjugate() | (hamiltonian_quaternion | vector)\nprint('The Hamiltonian product of hamiltonian^(-1) | hamiltonian | vector > is: {}'.format(vector_transform))\n```\n\u5f97\u5230\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a\n```bash\nThe Hamiltonian product of hamiltonian^(-1) | hamiltonian | vector > is: [[1. 2. 3.]]\n```\n\u53ef\u7528\u53d1\u73b0\uff0c\u6211\u4eec\u7ecf\u8fc7\u4e00\u91cd\u56db\u5143\u6570\u7684\u53d8\u6362\u548c\u4e00\u91cd\u5bf9\u5e94\u7684\u9006\u53d8\u6362\u4e4b\u540e\uff0c\u53c8\u5f97\u5230\u4e86\u8ddf\u539f\u6765\u4e00\u6837\u7684\u5411\u91cf\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u6700\u7ecf\u5e38\u4f7f\u7528\u56db\u5143\u6570\u7684\u573a\u666f\uff0c\n\u662f\u5bf9\u7a7a\u95f4\u5411\u91cf\u4e4b\u95f4\u8fdb\u884c\u4e00\u4e2a\u53d8\u6362\uff0c\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u7684\u65cb\u8f6c\u56db\u5143\u6570\u3002\u9996\u5148\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e9b\u521d\u59cb\u5411\u91cf\u548c\u76ee\u6807\u5411\u91cf\uff1a\n```python\nsrc_vector = Tensor([1, 0, 0], ms.float32)\nsrc_vector_quaternion = Quaternion(src_vector)\ndst_vector1 = Tensor([0, 0, 1], ms.float32)\ndst_vector1_quaternion = Quaternion(dst_vector1)\ndst_vector2 = Tensor([np.sqrt(3) / 3, np.sqrt(3) / 3, np.sqrt(3) / 3], ms.float32)\ndst_vector2_quaternion = Quaternion(dst_vector2)\n```\n\u7136\u540e\u53ef\u4ee5\u7814\u7a76\u5176\u4e2d\u4ece\u521d\u59cb\u5411\u91cf\u5230\u76ee\u6807\u5411\u91cf\u7684\u53d8\u6362\u56db\u5143\u6570\uff0c\u518d\u4f7f\u7528\u53d8\u6362\u56db\u5143\u6570\u4f5c\u7528\u5230\u521d\u59cb\u5411\u91cf\u4e0a\uff1a\n```python\ntransform_quaternion = src_vector_quaternion >> dst_vector1_quaternion\nprint ('The quaternion transform from vector to target_vector is: {}'.format(transform_quaternion))\nprint ('The transform result is: {}'.format(transform_quaternion | src_vector))\ntransform_quaternion = src_vector_quaternion >> dst_vector2_quaternion\nprint('The quaternion transform from vector to target_vector is: {}'.format(transform_quaternion))\nprint('The transform result is: {}'.format(transform_quaternion | src_vector))\n```\n\u8fd0\u884c\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a\n```bash\nThe quaternion transform from vector to target_vector is: [[ 0.70710677  0.         -0.7071068   0.        ]]\nThe transform result is: [[0.         0.         0.99999994]]\nThe quaternion transform from vector to target_vector is: [[ 0.88807386  0.         -0.32505763  0.32505763]]\nThe transform result is: [[0.57735026 0.5773504  0.5773504 ]]\n```\n\u4ee5\u4e0a\u5c31\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u56db\u5143\u6570\u64cd\u4f5c\uff0c\u66f4\u591a\u64cd\u4f5c\u53ef\u4ee5\u53c2\u8003`examples/`\u8def\u5f84\u4e0b\u7684\u793a\u4f8b\u3002\n\n## \u51fd\u6570\u5f0f\u8fd0\u7b97\n\u4e3a\u4e86\u66f4\u597d\u7684\u652f\u6301MindSpore\u6846\u67b6\u4e2d\u5bf9\u8ba1\u7b97\u56fe\u7684\u7f16\u8bd1\uff0c\u8fd9\u91cc\u5982\u679c\u6211\u4eec\u4f7f\u7528MindSpore\u6846\u67b6\u53bb\u6784\u5efa\u4e00\u4e2aQuaternion\u7684\u5bf9\u8c61\uff0c\u4f1a\u5f15\u53d1Jit-FallBack\n\u7684\u62a5\u9519\uff0c\u56e0\u6b64\u9700\u8981\u652f\u6301\u4e00\u4e9b\u51fd\u6570\u5f0f\u7684\u64cd\u4f5c\u2014\u2014\u4fdd\u7559Tensor\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ec5\u5f15\u5165\u4e00\u4e9b\u56db\u5143\u6570\u7684\u64cd\u4f5c\u3002\u4e0b\u8ff0\u4ee3\u7801\u662f\u4e00\u4e9b\u6bd4\u8f83\u7b80\u5355\u7684\u6848\u4f8b\uff1a\n```python\nfrom quaternion.functions import quaternion_multiply, quaternion_inverse, hamiltonian_product, quaternion_diff\nimport mindspore as ms\nfrom mindspore import Tensor\nimport numpy as np\n\nhamiltonian = Tensor([0.5, 0.5, 0.5, 0.5], ms.float32)\nq1 = Tensor([1, 2, 3, 4], ms.float32)\nq2 = Tensor([3, 1, 0, 5], ms.float32)\nvector = Tensor([1, 2, 3], ms.float32)\nsrc_vector = Tensor([1, 0, 0], ms.float32)\ndst_vector1 = Tensor([0, 0, 1], ms.float32)\ndst_vector2 = Tensor([np.sqrt(3) / 3, np.sqrt(3) / 3, np.sqrt(3) / 3], ms.float32)\n\nassert np.allclose(quaternion_inverse(hamiltonian).asnumpy(), np.array([[0.5, -0.5, -0.5, -0.5]], np.float32))\nassert np.allclose(quaternion_multiply(q1, q2).asnumpy(), np.array([[-19., 22., 3., 14.]], np.float32))\nassert np.allclose(quaternion_multiply(q2, q1).asnumpy(), np.array([[-19., -8., 15., 20.]], np.float32))\nassert np.allclose(hamiltonian_product(hamiltonian, vector).asnumpy(), np.array([[0., 3., 1., 2.]], np.float32))\nassert np.allclose(quaternion_diff(src_vector, dst_vector1).asnumpy(),\n                       np.array([[0.70710677,  0.,         -0.7071068,   0.]], np.float32))\nassert np.allclose(quaternion_diff(src_vector, dst_vector2).asnumpy(),\n                       np.array([[0.88807386,  0.,         -0.32505763,  0.32505763]], np.float32))\n```\n\u5982\u679c\u8fd0\u884c\u6b63\u786e\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u901a\u8fc7\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\n\n## Numpy\u51fd\u6570\u5f0f\u8fd0\u7b97\n\u4e3a\u4e86\u65b9\u4fbf\u672c\u5730\u73af\u5883\u4e2d\u6ca1\u6709MindSpore\u7684\u7528\u6237\u4f7f\u7528\uff0cmindspore-quaternion\u4e5f\u63d0\u4f9b\u4e86numpy\u7248\u672c\u7684\u56db\u5143\u6570\u8fd0\u7b97\u652f\u6301\uff0c\u4f7f\u7528\u65b9\u6cd5\u4e0e\u4e0a\u8ff0\n\u51fd\u6570\u5f0f\u8fd0\u7b97\u4e2d\u7684\u5185\u5bb9\u57fa\u672c\u4e00\u81f4\uff0c\u76f8\u5173\u63a5\u53e3\u4e3a\uff1a\n```python\nfrom quaternion.np_functions import inverse, multiply, hamiltonian, transform\n```\n\u5e76\u4e14\uff0c\u57fa\u4e8enumpy\u7248\u672c\u7684\u56db\u5143\u6570\u8fd0\u7b97\uff0c\u7528\u6237\u53ef\u4ee5\u5bf9\u86cb\u767d\u8d28\u7684pdb\u6587\u4ef6\u8fdb\u884c\u65cb\u8f6c\u5e73\u79fb\u53d8\u6362\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a\n```python\nimport numpy as np\nfrom quaternion.rotate_pdb import rotate\n\nif __name__ == '__main__':\n    pdb_in = 'test_in.pdb'\n    pdb_out = 'test_out.pdb'\n    # \u65b0\u539f\u70b9\n    o1 = np.array([42.882, 32.025, 38.245], np.float32)\n    # \u65b0X\u8f74\n    x1 = np.array([38.326, 24.174, 48.484], np.float32)\n    # \u65b0XY\u5e73\u9762\u4e0a\u7684\u4e00\u4e2a\u70b9\uff0c\u4f46\u4e0d\u80fd\u5728OX\u65b9\u5411\u4e0a\n    m1 = np.array([53.187, 51.701, 33.301], np.float32)\n    rotate(pdb_in, pdb_out, o1, x1, m1)\n```\n\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u628a\u7ecf\u8fc7\u65cb\u8f6c\u548c\u5e73\u79fb\u53d8\u6362\u4e4b\u540e\u7684\u5750\u6807\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u65b0\u7684pdb\u6587\u4ef6\u4e2d\u3002\n\n## \u4ee3\u7801\u8d21\u732e\n\u6b22\u8fce\u5916\u90e8\u534f\u4f5c\u8005\u63d0PR\u8fc7\u6765\uff0c\u6709\u5982\u4e0b\u51e0\u70b9PR\u8981\u6c42\uff1a\n1. \u5148\u63d0Issue\u518d\u63d0\u4ea4PR\uff0c\u8bf4\u6e05\u695a\u7279\u6027\uff1b\n2. \u8d21\u732e\u4ee3\u7801\u90e8\u5206\u7684flake8\u9700\u8981\u6e05\u7a7a\u3002\n\n## \u53c2\u8003\u535a\u5ba2\n1. https://www.cnblogs.com/dechinphy/p/quaternion-calc.html\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "Quaternion data structure based on MindSpore Framework",
    "version": "0.7.1",
    "project_urls": {
        "Homepage": "https://gitee.com/dechin/mindspore-quaternion"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0d9bf60bc6771253a8aa7bfa51e2f9c9f1ce3b7f91b51efa0783d3040b05ffd1",
                "md5": "4cb26d0f52903ce9ba1631178260ca1d",
                "sha256": "e1bc572941df885dca7fd420c42a57e3e68f3b2da6fd05ba0f9c27a0deffcbf2"
            },
            "downloads": -1,
            "filename": "mindspore_quaternion-0.7.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4cb26d0f52903ce9ba1631178260ca1d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13506,
            "upload_time": "2023-11-03T09:52:21",
            "upload_time_iso_8601": "2023-11-03T09:52:21.656352Z",
            "url": "https://files.pythonhosted.org/packages/0d/9b/f60bc6771253a8aa7bfa51e2f9c9f1ce3b7f91b51efa0783d3040b05ffd1/mindspore_quaternion-0.7.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "81748ba0baa9cac5f02ea27744edb0f74406cd2c60689cb782e277de5246a1b8",
                "md5": "ebaaa83efb60477118f6ccd8918e69ec",
                "sha256": "b276e40f766d91c6588dd84449dfa3242e388e93200a33a4230f8b4676325ae2"
            },
            "downloads": -1,
            "filename": "mindspore-quaternion-0.7.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ebaaa83efb60477118f6ccd8918e69ec",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10701,
            "upload_time": "2023-11-03T09:51:57",
            "upload_time_iso_8601": "2023-11-03T09:51:57.215631Z",
            "url": "https://files.pythonhosted.org/packages/81/74/8ba0baa9cac5f02ea27744edb0f74406cd2c60689cb782e277de5246a1b8/mindspore-quaternion-0.7.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-03 09:51:57",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mindspore-quaternion"
}
        
Elapsed time: 0.27297s