📄 d_ktensor_doc.html
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Kruskal tensors</title> <meta name="generator" content="MATLAB 7.2"> <meta name="date" content="2007-01-10"> <meta name="m-file" content="D_ktensor_doc"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. On Gecko-based browsers, the shrink-to-fit doesn't work. */ p,h1,h2,div.content div { /* for MATLAB's browser */ width: 600px; /* for Mozilla, but the "width" tag overrides it anyway */ max-width: 600px; /* for IE */ width:expression(document.body.clientWidth > 620 ? "600px": "auto" );}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>Kruskal tensors</h1> <introduction> <p>Kruskal format is a decomposition of a tensor X as the sum of the outer products a the columns of matrices. For example, we might write </p> <p><img vspace="5" hspace="5" src="D_ktensor_doc_eq45850.png"> </p> <p>where a subscript denotes column index and a circle denotes outer product. In other words, the tensor X is built frm the columns of the matrices A,B, and C. It's often helpful to explicitly specify a weight for each outer product, which we do here: </p> <p><img vspace="5" hspace="5" src="D_ktensor_doc_eq88653.png"> </p> <p>The <tt>ktensor</tt> class stores the components of the tensor X and can perform many operations, e.g., <tt>ttm</tt>, without explicitly forming the tensor X. </p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Kruskal tensor format via ktensor</a></li> <li><a href="#3">Specifying weights in a ktensor</a></li> <li><a href="#4">Creating a one-dimensional ktensor</a></li> <li><a href="#5">Constituent parts of a ktensor</a></li> <li><a href="#7">Creating a ktensor from its constituent parts</a></li> <li><a href="#8">Creating an empty ktensor</a></li> <li><a href="#9">Use full or tensor to convert a ktensor to a tensor</a></li> <li><a href="#11">Use double to convert a ktensor to a multidimensional array</a></li> <li><a href="#12">Use tendiag or sptendiag to convert a ktensor to a ttensor.</a></li> <li><a href="#16">Use ndims and size for the dimensions of a ktensor</a></li> <li><a href="#19">Subscripted reference for a ktensor</a></li> <li><a href="#23">Subscripted assignment for a ktensor</a></li> <li><a href="#26">Use end for the last array index.</a></li> <li><a href="#29">Adding and subtracting ktensors</a></li> <li><a href="#33">Basic operations with a ktensor</a></li> <li><a href="#36">Use permute to reorder the modes of a ktensor</a></li> <li><a href="#37">Use arrange to normalize the factors of a ktensor</a></li> <li><a href="#39">Use fixsigns for sign indeterminacies in a ktensor</a></li> <li><a href="#41">Use ktensor to store the 'skinny' SVD of a matrix</a></li> <li><a href="#44">Displaying a ktensor</a></li> <li><a href="#45">Displaying data</a></li> </ul> </div> <h2>Kruskal tensor format via ktensor<a name="1"></a></h2> <p>Kruskal format stores a tensor as a sum of rank-1 outer products. For example, consider a tensor of the following form.</p> <p><img vspace="5" hspace="5" src="D_ktensor_doc_eq69229.png"> </p> <p>This can be stored in Kruskal form as follows.</p><pre class="codeinput">rand(<span class="string">'state'</span>,0);A = rand(4,2); <span class="comment">%<-- First column is a_1, second is a_2.</span>B = rand(3,2); <span class="comment">%<-- Likewise for B.</span>C = rand(2,2); <span class="comment">%<-- Likewise for C.</span>X = ktensor({A,B,C}) <span class="comment">%<-- Create the ktensor.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 x 2 X.lambda = [ 1 1 ] X.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 X.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 X.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><p>For Kruskal format, there can be any number of matrices, but every matrix must have the same number of columns. The number of rows can vary. </p><pre class="codeinput">Y = ktensor({rand(4,1),rand(2,1),rand(3,1)}) <span class="comment">%<-- Another ktensor.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 x 2 x 3 Y.lambda = [ 1 ] Y.U{1} = 0.4103 0.8936 0.0579 0.3529 Y.U{2} = 0.8132 0.0099 Y.U{3} = 0.1389 0.2028 0.1987</pre><h2>Specifying weights in a ktensor<a name="3"></a></h2> <p>Weights for each rank-1 tensor can be specified by passing in a column vector. For example,</p> <p><img vspace="5" hspace="5" src="D_ktensor_doc_eq203872.png"> </p><pre class="codeinput">lambda = [5.0; 0.25]; <span class="comment">%<-- Weights for each factor.</span>X = ktensor(lambda,{A,B,C}) <span class="comment">%<-- Create the ktensor.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 x 2 X.lambda = [ 5 0.25 ] X.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 X.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 X.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><h2>Creating a one-dimensional ktensor<a name="4"></a></h2><pre class="codeinput">Y = ktensor({rand(4,5)}) <span class="comment">%<-- A one-dimensional ktensor.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 Y.lambda = [ 1 1 1 1 1 ] Y.U{1} = 0.6038 0.7468 0.4186 0.6721 0.3795 0.2722 0.4451 0.8462 0.8381 0.8318 0.1988 0.9318 0.5252 0.0196 0.5028 0.0153 0.4660 0.2026 0.6813 0.7095</pre><h2>Constituent parts of a ktensor<a name="5"></a></h2><pre class="codeinput">X.lambda <span class="comment">%<-- Weights or multipliers.</span></pre><pre class="codeoutput">ans = 5.0000 0.2500</pre><pre class="codeinput">X.U <span class="comment">%<-- Cell array of matrices.</span></pre><pre class="codeoutput">ans = [4x2 double] [3x2 double] [2x2 double]</pre><h2>Creating a ktensor from its constituent parts<a name="7"></a></h2><pre class="codeinput">Y = ktensor(X.lambda,X.U) <span class="comment">%<-- Recreate X.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 x 3 x 2 Y.lambda = [ 5 0.25 ] Y.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 Y.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 Y.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><h2>Creating an empty ktensor<a name="8"></a></h2><pre class="codeinput">Z = ktensor <span class="comment">%<-- Empty ktensor.</span></pre><pre class="codeoutput">Z is a ktensor of size [empty tensor] Z.lambda = [ ]</pre><h2>Use full or tensor to convert a ktensor to a tensor<a name="9"></a></h2><pre class="codeinput">full(X) <span class="comment">%<-- Converts to a tensor.</span></pre><pre class="codeoutput">ans is a tensor of size 4 x 3 x 2 ans(:,:,1) = 0.8529 0.5645 0.6692 0.3085 0.2549 0.2569 0.5239 0.3362 0.4080 0.3552 0.1945 0.2668 ans(:,:,2) = 1.7450 1.0454 1.3370 0.5235 0.3695 0.4175 1.0940 0.6439 0.8348 0.8131 0.4423 0.6098</pre><pre class="codeinput">tensor(X) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">ans is a tensor of size 4 x 3 x 2 ans(:,:,1) = 0.8529 0.5645 0.6692 0.3085 0.2549 0.2569 0.5239 0.3362 0.4080 0.3552 0.1945 0.2668 ans(:,:,2) = 1.7450 1.0454 1.3370 0.5235 0.3695 0.4175 1.0940 0.6439 0.8348 0.8131 0.4423 0.6098</pre><h2>Use double to convert a ktensor to a multidimensional array<a name="11"></a></h2><pre class="codeinput">double(X) <span class="comment">%<-- Converts to an array.</span></pre><pre class="codeoutput">ans(:,:,1) = 0.8529 0.5645 0.6692 0.3085 0.2549 0.2569 0.5239 0.3362 0.4080 0.3552 0.1945 0.2668ans(:,:,2) = 1.7450 1.0454 1.3370 0.5235 0.3695 0.4175 1.0940 0.6439 0.8348 0.8131 0.4423 0.6098</pre><h2>Use tendiag or sptendiag to convert a ktensor to a ttensor.<a name="12"></a></h2> <p>A ktensor can be regarded as a ttensor with a diagonal core.</p><pre class="codeinput">R = length(X.lambda); <span class="comment">%<-- Number of factors in X.</span>core = tendiag(X.lambda, repmat(R,1,ndims(X))); <span class="comment">%<-- Create a diagonal core.</span>Y = ttensor(core, X.u) <span class="comment">%<-- Assemble the ttensor.</span></pre><pre class="codeoutput">Y is a ttensor of size 4 x 3 x 2 Y.core is a tensor of size 2 x 2 x 2 Y.core(:,:,1) = 5 0 0 0 Y.core(:,:,2) = 0 0 0 0.2500 Y.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 Y.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 Y.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><pre class="codeinput">norm(full(X)-full(Y)) <span class="comment">%<-- They are the same.</span></pre><pre class="codeoutput">ans = 3.8057e-016</pre><pre class="codeinput">core = sptendiag(X.lambda, repmat(R,1,ndims(X))); <span class="comment">%<-- Sparse diagonal core.</span>Y = ttensor(core, X.u) <span class="comment">%<-- Assemble the ttensor</span></pre><pre class="codeoutput">Y is a ttensor of size 4 x 3 x 2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -