{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$x(\\theta,\\varphi)=(R+r\\cos\\theta)\\cos\\varphi$$\n", "$$y(\\theta,\\varphi)=(R+r\\cos\\theta)\\sin\\varphi$$\n", "$$z(\\theta,\\varphi)=r\\sin\\theta$$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "n = 20\n", "R = 15\n", "r = 1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "theta = np.linspace(0, 2*np.pi, n)\n", "phi = np.linspace(0, 2*np.pi, n)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def torus(theta, phi, R, r):\n", " delta = (R + r*np.cos(theta))\n", " x = delta*np.cos(phi)\n", " y = delta*np.sin(phi)\n", " z = r*np.sin(theta)\n", " return x, y, z" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x, y, z = torus(theta, phi, R, r)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "thetas, phis = np.meshgrid(theta, phi)\n", "theta, phi = thetas.reshape(-1), phis.reshape(-1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "x, y, z = torus(theta, phi, R, r)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "X = np.vstack((x, y, z)).T" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def rotation_matrices(theta_x, theta_y, theta_z):\n", " Rx = [[1, 0, 0],\n", " [0, np.cos(theta_x), -np.sin(theta_x)],\n", " [0, np.sin(theta_x), np.cos(theta_x)]]\n", " Ry = [[np.cos(theta_y), 0, np.sin(theta_y)], \n", " [0, 1, 0], \n", " [-np.sin(theta_y), 0, np.cos(theta_y)]]\n", " Rz = [[np.cos(theta_z), -np.sin(theta_z), 0], \n", " [np.sin(theta_z), np.cos(theta_z), 0], \n", " [0, 0, 1]]\n", " return Rx, Ry, Rz" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "Rx, Ry, Rz = rotation_matrices(np.pi/6, np.pi/3, np.pi/6)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "X_rotated = ((X.dot(Rx)).dot(Ry)).dot(Rz)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "torus_data = pd.DataFrame(X_rotated, columns=['x', 'y', 'z'])\n", "torus_data['longitudinal'] = np.tile(np.arange(n), n)\n", "torus_data['cross-sectional'] = np.repeat(np.arange(n), n)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "