text_plain_output_1.png
from numpy.linalg import inv

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_train = pd.read_csv('../input/train.csv')
df_test = pd.read_csv('../input/test.csv')

def get_xyz_data(filename):
    pos_data = []
    lat_data = []
    with open(filename) as f:
        for line in f.readlines():
            x = line.split()
            if x[0] == 'atom':
                pos_data.append([np.array(x[1:4], dtype=np.float), x[4]])
            elif x[0] == 'lattice_vector':
                lat_data.append(np.array(x[1:4], dtype=np.float))
    return (pos_data, np.array(lat_data))

def length(v):
    return np.linalg.norm(v)
def unit_vector(vector):
    return vector / length(vector)
def angle_between(v1, v2):
    v1_u = unit_vector(v1)
    v2_u = unit_vector(v2)
    return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
def angle_deg_between(v1, v2):
    return np.degrees(angle_between(v1, v2))
def get_lattice_constants(lattice_vectors):
    lat_const_series = pd.Series()
    for i in range(3):
        lat_const_series['lattice_vector_' + str(i + 1) + '_ang'] = length(lattice_vectors[i])
    lat_const_series['lattice_angle_alpha_degree'] = angle_deg_between(lattice_vectors[1], lattice_vectors[2])
    lat_const_series['lattice_angle_beta_degree'] = angle_deg_between(lattice_vectors[2], lattice_vectors[0])
    lat_const_series['lattice_angle_gamma_degree'] = angle_deg_between(lattice_vectors[0], lattice_vectors[1])
    return lat_const_series

A = np.transpose(train_lat)
R = train_xyz[0][0]

from numpy.linalg import inv
B = inv(A)

r = np.matmul(B, R)

def get_distances(reduced_coords, amat):
    natom = len(reduced_coords)
    dists = np.array([np.inf] * natom ** 2).reshape(natom, natom)
    for i in range(natom):
        dists[i, i] = 0
        for j in range(i):
            rij = reduced_coords[i][0] - reduced_coords[j][0]
            for l in range(-1, 2):
                for m in range(-1, 2):
                    for n in range(-1, 2):
                        r = rij + np.array([l, m, n])
                        dists[i, j] = min(dists[i, j], length(np.matmul(amat, r)))
            dists[j, i] = dists[i, j]
    return dists

train_red = [[np.matmul(B, R), symbol] for R, symbol in train_xyz]
train_dist = get_distances(train_red, A)
train_dist