加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
processor.py 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
alyssaq 提交于 2017-05-23 22:29 . autopep8
import numpy as np
def read_matrix(path, astype=np.float64):
""" Reads a file containing a matrix where each line represents a point
and each point is tab or space separated. * are replaced with -1.
:param path: path to the file
:parama astype: type to cast the numbers. Default: np.float64
:returns: array of array of numbers
"""
with open(path, 'r') as f:
arr = []
for line in f:
arr.append([(token if token != '*' else -1)
for token in line.strip().split()])
return np.asarray(arr).astype(astype)
def cart2hom(arr):
""" Convert catesian to homogenous points by appending a row of 1s
:param arr: array of shape (num_dimension x num_points)
:returns: array of shape ((num_dimension+1) x num_points)
"""
if arr.ndim == 1:
return np.hstack([arr, 1])
return np.asarray(np.vstack([arr, np.ones(arr.shape[1])]))
def hom2cart(arr):
""" Convert homogenous to catesian by dividing each row by the last row
:param arr: array of shape (num_dimension x num_points)
:returns: array of shape ((num_dimension-1) x num_points) iff d > 1
"""
# arr has shape: dimensions x num_points
num_rows = len(arr)
if num_rows == 1 or arr.ndim == 1:
return arr
return np.asarray(arr[:num_rows - 1] / arr[num_rows - 1])
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化