LoginSignup
1
3

More than 5 years have passed since last update.

3D座標の重心を計算する > Python operator:面倒 | Numpy array:簡単

Last updated at Posted at 2018-05-11
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

処理

  • 3D座標の重心を求めたい
    • 座標要素ごとの加算が必要

情報

operatorパッケージのaddをmap()で処理している例がある。

しかしながら、これは面倒である(後述のコード参照)

PythonとNumpyでの処理

test_3D_center_180512.py
from operator import add
import numpy as np


def add_python(v1, v2, v3):
    print('===add_python===')
    center1 = (v1 + v2 + v3)
    print(center1)

    wrk = map(add, v1, v2)
    center2 = map(add, wrk, v3)
    print(*center2)


def add_numpy(na1, na2, na3):
    print('===add_numpy===')
    wrk = na1 + na2 + na3
    print(wrk)


def calc_center(na1, na2, na3):
    print('===calc_center===')
    center = (na1 + na2 + na3) / 3.0
    print(center)

TV1 = [-42, -8, 60]  # TV: Triangle vertex
TV2 = [43, 41, 60]
TV3 = [21, -54, 60]
add_python(TV1, TV2, TV3)

na1 = np.array(TV1)  # na: numpy array
na2 = np.array(TV2)
na3 = np.array(TV3)
add_numpy(na1, na2, na3)

calc_center(na1, na2, na3)

run
$ python3 test_3D_center_180512.py 
===add_python===
[-42, -8, 60, 43, 41, 60, 21, -54, 60]
22 -21 180
===add_numpy===
[ 22 -21 180]
===calc_center===
[ 7.33333333 -7.         60.        ]

Numpy arrayで処理するほうが遥かに簡単ですね。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3