{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multilinear tensors" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using TensorDec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We consider a multi-linear tensor of size 3 x 5 x 4, which is sum of r=4 tensor products of the random column vectors of the matrices `A0`, `B0`, `C0`with weights `w0`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×5×4 Array{Float64,3}:\n", "[:, :, 1] =\n", " 0.347299 0.151553 0.114054 0.155983 0.282425 \n", " 0.010721 0.00987083 0.00909818 0.00590759 0.0139975\n", " 0.389758 0.24162 0.203963 0.201205 0.300751 \n", "\n", "[:, :, 2] =\n", " 0.328047 0.150379 0.12208 0.142833 0.262686 \n", " 0.0530373 0.0496668 0.0546258 0.0193254 0.0734109\n", " 0.490219 0.341215 0.31404 0.244133 0.393444 \n", "\n", "[:, :, 3] =\n", " 0.358308 0.147021 0.115107 0.14935 0.293383 \n", " 0.0626374 0.0587502 0.0648279 0.0224873 0.0878258\n", " 0.385146 0.229846 0.210108 0.170616 0.342252 \n", "\n", "[:, :, 4] =\n", " 0.20043 0.0894733 0.0626167 0.0954052 0.17385 \n", " 0.0382902 0.0375343 0.0393505 0.0163238 0.0554182\n", " 0.203668 0.128217 0.106686 0.103265 0.198758 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r=4\n", "w0 = rand(r)\n", "A0 = rand(3,r)\n", "B0 = rand(5,r)\n", "C0 = rand(4,r)\n", "\n", "T0 = tensor(w0, A0, B0, C0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We compute its decomposition:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "w, A, B, C = decompose(T0);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We obtain a decomposition of rank 4 with weights:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.30720814696815224\n", " 0.5694221275090653 \n", " 0.9706126355492068 \n", " 0.0400306207408488 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The r=4 vectors of norm 1 of the first components of the decomposition are the columns of the matrix A: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×4 Array{Float64,2}:\n", " 0.0956115 0.19154 0.827422 0.603317\n", " 0.638545 0.00713372 0.00592765 0.124732\n", " 0.763622 0.981459 0.561549 0.787687" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The r=4 vectors of norm 1 of the second components are the columns of the matrix B: " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×4 Array{Float64,2}:\n", " -0.436806 -0.558735 -0.700754 0.118617 \n", " -0.419512 -0.489725 -0.259599 0.485964 \n", " -0.466462 -0.454027 -0.193181 0.0261587\n", " -0.154209 -0.334743 -0.285791 0.677514 \n", " -0.625974 -0.360266 -0.567941 0.538572 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The r=4 vectors of norm 1 of the third components are the columns of the matrix C:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:\n", " -0.0811061 -0.537059 -0.554383 0.535713 \n", " -0.575176 -0.788566 -0.484317 0.0169553\n", " -0.695558 -0.283155 -0.590019 0.0269297\n", " -0.422839 -0.0977454 -0.331624 0.8438 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It corresponds to the tensor $\\sum_{i=1}^{r} w_i \\, A[:,i] \\otimes B[:,i] \\otimes C[:,i]$ for $i \\in 1:r$:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×5×4 Array{Float64,3}:\n", "[:, :, 1] =\n", " 0.347299 0.151553 0.114054 0.155983 0.282425 \n", " 0.010721 0.00987083 0.00909818 0.00590759 0.0139975\n", " 0.389758 0.24162 0.203963 0.201205 0.300751 \n", "\n", "[:, :, 2] =\n", " 0.328047 0.150379 0.12208 0.142833 0.262686 \n", " 0.0530373 0.0496668 0.0546258 0.0193254 0.0734109\n", " 0.490219 0.341215 0.31404 0.244133 0.393444 \n", "\n", "[:, :, 3] =\n", " 0.358308 0.147021 0.115107 0.14935 0.293383 \n", " 0.0626374 0.0587502 0.0648279 0.0224873 0.0878258\n", " 0.385146 0.229846 0.210108 0.170616 0.342252 \n", "\n", "[:, :, 4] =\n", " 0.20043 0.0894733 0.0626167 0.0954052 0.17385 \n", " 0.0382902 0.0375343 0.0393505 0.0163238 0.0554182\n", " 0.203668 0.128217 0.106686 0.103265 0.198758 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = tensor(w, A, B, C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We compute the $L^2$ norm of the difference between $T$ and $T_0$:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5632252009555643e-15" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using LinearAlgebra\n", "norm(T-T0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.0", "language": "julia", "name": "julia-1.0" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.0" } }, "nbformat": 4, "nbformat_minor": 2 }