【问题标题】:Python combination of all positive and negative coordinate values所有正负坐标值的Python组合
【发布时间】:2017-04-07 16:07:51
【问题描述】:

我有一个代码可以从这个输入文件计算 3D 坐标的距离公式

  input_file="mock_data.csv"
  cmd=pd.read_csv(input_file)
  subset = cmd[['carbon','x_coord', 'y_coord','z_coord']]
  coordinate_values = [tuple(x) for x in subset.values]

  atoms = coordinate_values
  atomPairs = itertools.combinations(atoms, 2)
  for pair in atomPairs:
    x1 = pair[0][1]
    y1 = pair[0][2]
    z1 = pair[0][3]
    x2 = pair[1][1]
    y2 = pair[1][2]
    z2 = pair[1][3]

    """Define the values for the distance between the atoms"""
    def calculate_distance(x1,y1,x2,y2,z1,z2):
       dist=math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2-z1)**2)
       return dist
    d=calculate_distance(x1,y1,x2,y2,z1,z2)

我目前已经编写了代码来计算每个“碳”之间的距离。我的问题是我的坐标都是绝对值 - 每个坐标都可以是正数或负数。我想计算所有可能坐标的每个碳之间的距离,即每个 3D 坐标的所有正负组合。

简单示例:“碳”1 的坐标为 (1.08, 0.49, 0.523),但也可以是 (-1.08, -0.49, -0.523), (-1.08, 0.49, 0.523), (-1.08, -0.49 , 0.523), (-1.08, 0.49, -0.523), (1.08, -0.49, 0.523), (1.08, -0.49, -0.523), (1.08, 0.49, -0.523),总共有八种可能性每个坐标系。

我需要一个代码来遍历所有这些可能的坐标值来计算我已经编码的距离。

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 on topichow to ask 在这里申请。 StackOverflow 不是设计、编码、研究或教程服务。
  • 创建一些包含所有所需坐标的列表,并使用itertools.product 获取笛卡尔积。

标签: python coordinates combinations


【解决方案1】:

这是一个相当简单的例子。

import numpy as np
from itertools import product
from scipy.spatial.distance import cdist

base = np.array([-1,1]) #accounts for signed coord

x = 1*base              #change coord values
y = 2*base
z = 3*base

coords = list(product(x,y,z))    #cartesian product

distances = cdist(coords,coords) #better implementation of distance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 2020-03-11
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多