{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vector Fields\n",
"\n",
"The standard way to represent $N$-dimensional vector fields in *tntorch* is via a list of $N$ tensors, each of which has $N$ dimensions. Functions that accept or return vector fields do so in that form. For example, `gradient()`:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3D TT tensor:\n",
"\n",
" 64 64 64\n",
" | | |\n",
" (0) (1) (2)\n",
" / \\ / \\ / \\\n",
"1 10 10 1\n",
", 3D TT tensor:\n",
"\n",
" 64 64 64\n",
" | | |\n",
" (0) (1) (2)\n",
" / \\ / \\ / \\\n",
"1 10 10 1\n",
", 3D TT tensor:\n",
"\n",
" 64 64 64\n",
" | | |\n",
" (0) (1) (2)\n",
" / \\ / \\ / \\\n",
"1 10 10 1\n",
"]\n"
]
}
],
"source": [
"import torch\n",
"torch.set_default_dtype(torch.float64)\n",
"import tntorch as tn\n",
"\n",
"t = tn.rand([64]*3, ranks_tt=10)\n",
"grad = tn.gradient(t)\n",
"print(grad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check that the curl of any gradient is 0:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(1.0344e-06)\n",
"tensor(1.4569e-06)\n",
"tensor(2.5995e-06)\n"
]
}
],
"source": [
"curl = tn.curl(tn.gradient(t)) # List of 3 3D tensors\n",
"print(tn.norm(curl[0]))\n",
"print(tn.norm(curl[1]))\n",
"print(tn.norm(curl[2]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's also check that the divergence of any curl is zero (we'll use a random, non-gradient vector field here):"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(4.8832e-08)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vf = [tn.rand([64]*3, ranks_tt=1) for n in range(3)]\n",
"tn.norm(tn.divergence(tn.curl(vf)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and that the Laplacian of a scalar field `t` equals the divergence of `t`'s gradient:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(0.)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tn.norm(tn.laplacian(t) - tn.divergence(tn.gradient(t)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}