{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Arithmetics\n",
"\n",
"## Basic Arithmetics\n",
"\n",
"The most basic tensor operations (addition `+`, subtraction `-`, and product `*` with either a scalar or with another tensor) can be accomplished via direct manipulation of tensor cores (see e.g. the [original tensor train paper](https://epubs.siam.org/doi/abs/10.1137/090752286?journalCode=sjoce3))."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4D TT tensor:\n",
"\n",
" 32 32 32 32\n",
" | | | |\n",
" (0) (1) (2) (3)\n",
" / \\ / \\ / \\ / \\\n",
"1 1 1 1 1\n",
"\n"
]
}
],
"source": [
"import tntorch as tn\n",
"import torch\n",
"torch.set_default_dtype(torch.float64)\n",
"import numpy as np\n",
"\n",
"t1 = tn.ones([32]*4)\n",
"t2 = tn.ones([32]*4)\n",
"\n",
"t = tn.round((t1+t2)*(t2-2))\n",
"print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also *assign* values to parts of a tensor:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[6., 6., 2., 2., 2.],\n",
" [6., 6., 2., 2., 2.],\n",
" [6., 6., 2., 2., 2.],\n",
" [3., 3., 1., 1., 1.],\n",
" [3., 3., 1., 1., 1.]])\n"
]
}
],
"source": [
"t = tn.ones(5, 5)\n",
"t[:3, :] = 2\n",
"t[:, :2] *= 3\n",
"print(t.torch())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced Operations\n",
"\n",
"Thanks to [cross-approximation](cross.ipynb), *tntorch* supports many other more advanced operations on tensors, including element-wise division `/`, `exp()`, `log()`, `sin()`, etc."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4D TT-Tucker tensor:\n",
"\n",
" 32 32 32 32\n",
" | | | |\n",
" 7 13 13 7\n",
" (0) (1) (2) (3)\n",
" / \\ / \\ / \\ / \\\n",
"1 7 7 7 1\n",
"\n"
]
}
],
"source": [
"domain = [torch.linspace(0, np.pi, 32)]*4\n",
"x, y, z, w = tn.meshgrid(domain)\n",
"\n",
"t = tn.round(1 / (1+x+y+z+w))\n",
"print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now try the trigonometric identity $\\sin^2(x) + \\cos^2(x) = 1$:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4D TT tensor:\n",
"\n",
" 32 32 32 32\n",
" | | | |\n",
" (0) (1) (2) (3)\n",
" / \\ / \\ / \\ / \\\n",
"1 13 17 13 1\n",
"\n"
]
}
],
"source": [
"t = tn.round(tn.sin(t)**2 + tn.cos(t)**2)\n",
"print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The tensor `t` should be $1$ everywhere. Indeed:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(1.0000)\n",
"tensor(1.8159e-15)\n"
]
}
],
"source": [
"print(tn.mean(t))\n",
"print(tn.var(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
}