簡體   English   中英

所有正負坐標值的Python組合

[英]Python combination of all positive and negative coordinate values

我有一個代碼,可以從此輸入文件計算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),每個坐標系總共有八種可能性。

我需要一個代碼來檢查所有這些可能的坐標值,以計算出我已經編碼的距離。

這是一個非常簡單的示例。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM