# Copyright (C) 2017 Henrique Pereira Coutada Miranda
# All rights reserved.
#
# This file is part of phononwebsite
#
""" Functions to manipulate the atoms and lattice."""
import numpy as np


def red_car(red, lat):
    """
    Convert reduced coordinates to Cartesian.
    """
    return np.array(
        [coord[0] * lat[0] + coord[1] * lat[1] + coord[2] * lat[2] for coord in red]
    )


def car_red(car, lat):
    """
    Convert Cartesian coordinates to reduced.
    """
    return np.array([np.linalg.solve(np.array(lat).T, coord) for coord in car])


def rec_lat(lat):
    """
    Calculate the reciprocal lattice vectors.
    """
    a1, a2, a3 = np.array(lat)
    v = np.dot(a1, np.cross(a2, a3))
    b1 = np.cross(a2, a3) / v
    b2 = np.cross(a3, a1) / v
    b3 = np.cross(a1, a2) / v
    return np.array([b1, b2, b3])
