├── Demo.m ├── README.md ├── images ├── 1.bmp └── 2.bmp └── lib ├── BSF_test.m ├── FPR_TPR.m ├── ProTlogSum.m ├── ROC_Cruve.m ├── SCRG_test.m ├── ToolBox ├── mySVD.m ├── tensor_toolbox │ ├── @ktensor │ │ ├── Contents.m │ │ ├── arrange.m │ │ ├── datadisp.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── double.m │ │ ├── end.m │ │ ├── extract.m │ │ ├── fixsigns.m │ │ ├── full.m │ │ ├── innerprod.m │ │ ├── isequal.m │ │ ├── ktensor.m │ │ ├── minus.m │ │ ├── mtimes.m │ │ ├── mttkrp.m │ │ ├── ncomponents.m │ │ ├── ndims.m │ │ ├── norm.m │ │ ├── normalize.m │ │ ├── nvecs.m │ │ ├── permute.m │ │ ├── plus.m │ │ ├── redistribute.m │ │ ├── score.m │ │ ├── size.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── times.m │ │ ├── tocell.m │ │ ├── ttm.m │ │ ├── ttv.m │ │ ├── uminus.m │ │ └── uplus.m │ ├── @sptenmat │ │ ├── Contents.m │ │ ├── aatx.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── double.m │ │ ├── end.m │ │ ├── full.m │ │ ├── nnz.m │ │ ├── norm.m │ │ ├── size.m │ │ ├── sptenmat.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── tsize.m │ │ ├── uminus.m │ │ └── uplus.m │ ├── @sptensor │ │ ├── Contents.m │ │ ├── and.m │ │ ├── collapse.m │ │ ├── contract.m │ │ ├── ctranspose.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── divide.m │ │ ├── double.m │ │ ├── elemfun.m │ │ ├── end.m │ │ ├── eq.m │ │ ├── find.m │ │ ├── full.m │ │ ├── ge.m │ │ ├── gt.m │ │ ├── innerprod.m │ │ ├── isequal.m │ │ ├── ldivide.m │ │ ├── le.m │ │ ├── lt.m │ │ ├── minus.m │ │ ├── mldivide.m │ │ ├── mrdivide.m │ │ ├── mtimes.m │ │ ├── mttkrp.m │ │ ├── ndims.m │ │ ├── ne.m │ │ ├── nnz.m │ │ ├── norm.m │ │ ├── not.m │ │ ├── nvecs.m │ │ ├── ones.m │ │ ├── or.m │ │ ├── permute.m │ │ ├── plus.m │ │ ├── private │ │ │ ├── allsubs.m │ │ │ ├── extract.m │ │ │ ├── irenumber.m │ │ │ ├── renumber.m │ │ │ └── subdims.m │ │ ├── rdivide.m │ │ ├── reshape.m │ │ ├── scale.m │ │ ├── size.m │ │ ├── spmatrix.m │ │ ├── sptensor.m │ │ ├── squeeze.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── times.m │ │ ├── transpose.m │ │ ├── ttm.m │ │ ├── ttt.m │ │ ├── ttv.m │ │ ├── uminus.m │ │ ├── uplus.m │ │ └── xor.m │ ├── @tenmat │ │ ├── Contents.m │ │ ├── ctranspose.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── double.m │ │ ├── end.m │ │ ├── minus.m │ │ ├── mtimes.m │ │ ├── norm.m │ │ ├── plus.m │ │ ├── size.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── tenmat.m │ │ ├── tsize.m │ │ ├── uminus.m │ │ └── uplus.m │ ├── @tensor │ │ ├── Contents.m │ │ ├── and.m │ │ ├── collapse.m │ │ ├── contract.m │ │ ├── ctranspose.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── double.m │ │ ├── end.m │ │ ├── eq.m │ │ ├── find.m │ │ ├── full.m │ │ ├── ge.m │ │ ├── gt.m │ │ ├── innerprod.m │ │ ├── isequal.m │ │ ├── issymmetric.m │ │ ├── ldivide.m │ │ ├── le.m │ │ ├── lt.m │ │ ├── minus.m │ │ ├── mldivide.m │ │ ├── mrdivide.m │ │ ├── mtimes.m │ │ ├── mttkrp.m │ │ ├── ndims.m │ │ ├── ne.m │ │ ├── nnz.m │ │ ├── norm.m │ │ ├── not.m │ │ ├── nvecs.m │ │ ├── or.m │ │ ├── permute.m │ │ ├── plus.m │ │ ├── power.m │ │ ├── rdivide.m │ │ ├── reshape.m │ │ ├── scale.m │ │ ├── size.m │ │ ├── squeeze.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── symmetrize.m │ │ ├── tenfun.m │ │ ├── tensor.m │ │ ├── times.m │ │ ├── transpose.m │ │ ├── ttm.m │ │ ├── ttsv.m │ │ ├── ttt.m │ │ ├── ttv.m │ │ ├── uminus.m │ │ ├── uplus.m │ │ └── xor.m │ ├── @ttensor │ │ ├── Contents.m │ │ ├── disp.m │ │ ├── display.m │ │ ├── double.m │ │ ├── end.m │ │ ├── full.m │ │ ├── innerprod.m │ │ ├── isequal.m │ │ ├── mtimes.m │ │ ├── mttkrp.m │ │ ├── ndims.m │ │ ├── norm.m │ │ ├── nvecs.m │ │ ├── permute.m │ │ ├── size.m │ │ ├── subsasgn.m │ │ ├── subsref.m │ │ ├── ttensor.m │ │ ├── ttm.m │ │ ├── ttv.m │ │ ├── uminus.m │ │ └── uplus.m │ ├── COPYRIGHT.txt │ ├── Contents.m │ ├── INSTALL.txt │ ├── LICENSE.txt │ ├── RELEASE_NOTES.txt │ ├── cp_als.m │ ├── cp_apr.m │ ├── cp_nmu.m │ ├── cp_opt.m │ ├── cp_wopt.m │ ├── create_guess.m │ ├── create_problem.m │ ├── doc │ │ ├── A1_tensor_doc.m │ │ ├── A2_sptensor_doc.m │ │ ├── B1_tenmat_doc.m │ │ ├── B2_sptenmat_doc.m │ │ ├── C_ttensor_doc.m │ │ ├── D_ktensor_doc.m │ │ ├── M1_multiply_doc.m │ │ ├── M2_identities_doc_future.m │ │ ├── N_nvecs_doc.m │ │ ├── Q_collapse_scale_doc.m │ │ ├── S_test_problems_doc.m │ │ ├── T1_algorithms_doc.m │ │ ├── T2_opt_algorithms_doc.m │ │ ├── T3_wopt_algorithms_doc.m │ │ ├── T4_cpapr_doc.m │ │ ├── V_SSHOPM_doc.m │ │ ├── html │ │ │ ├── A1_tensor_doc.html │ │ │ ├── A2_sptensor_doc.html │ │ │ ├── B1_tenmat_doc.html │ │ │ ├── B2_sptenmat_doc.html │ │ │ ├── C_ttensor_doc.html │ │ │ ├── C_ttensor_doc_eq15566.png │ │ │ ├── D_ktensor_doc.html │ │ │ ├── D_ktensor_doc_eq09466.png │ │ │ ├── D_ktensor_doc_eq51104.png │ │ │ ├── D_ktensor_doc_eq64665.png │ │ │ ├── D_ktensor_doc_eq81501.png │ │ │ ├── M1_multiply_doc.html │ │ │ ├── N_nvecs_doc.html │ │ │ ├── Q_collapse_scale_doc.html │ │ │ ├── S_test_problems_doc.html │ │ │ ├── T1_algorithms_doc.html │ │ │ ├── T2_opt_algorithms_doc.html │ │ │ ├── T3_wopt_algorithms_doc.html │ │ │ ├── T4_cpapr_doc.html │ │ │ ├── V_SSHOPM_doc.html │ │ │ ├── V_SSHOPM_doc.png │ │ │ ├── V_SSHOPM_doc_01.png │ │ │ └── V_SSHOPM_doc_02.png │ │ └── images │ │ │ ├── Workspace.png │ │ │ ├── banner-background.jpg │ │ │ └── logo.gif │ ├── export_data.m │ ├── generate_LowRank_tensor.m │ ├── helpindex.xml │ ├── helptoc.xml │ ├── import_data.m │ ├── info.xml │ ├── khatrirao.m │ ├── met │ │ ├── Contents.m │ │ ├── ttm_me.m │ │ ├── ttm_me_mem.m │ │ ├── ttm_me_partition.m │ │ ├── tucker_me.m │ │ └── tucker_me_test.m │ ├── my_logTFNN.m │ ├── parafac_als.m │ ├── sptendiag.m │ ├── sptenrand.m │ ├── sshopm.m │ ├── sshopmc.m │ ├── tendiag.m │ ├── teneye.m │ ├── tenones.m │ ├── tenrand.m │ ├── tensor_toolbox_product_page.html │ ├── tenzeros.m │ ├── tnorm.m │ ├── tt_RandOrthMat.m │ ├── tt_assignment_type.m │ ├── tt_ccong.m │ ├── tt_combinator.m │ ├── tt_cp_fg.m │ ├── tt_cp_fun.m │ ├── tt_cp_vec_to_fac.m │ ├── tt_cp_wfg.m │ ├── tt_cp_wfg_sparse.m │ ├── tt_cp_wfg_sparse_setup.m │ ├── tt_cp_wfun.m │ ├── tt_create_missing_data_pattern.m │ ├── tt_dimscheck.m │ ├── tt_fac_to_vec.m │ ├── tt_ind2sub.m │ ├── tt_intvec2str.m │ ├── tt_loglikelihood.m │ ├── tt_matrix2cellstr.m │ ├── tt_size2str.m │ ├── tt_sizecheck.m │ ├── tt_sub2ind.m │ ├── tt_subscheck.m │ ├── tt_subsubsref.m │ ├── tt_valscheck.m │ └── tucker_als.m ├── tprod.m ├── tran.m └── tsvd.m ├── Unfold.m ├── func_Calculate_ROC.m ├── gen_patch_ten.m ├── my_LogTFNN.m ├── my_normalized.m ├── prox_l1.m ├── prox_z.m ├── res_patch_ten_mean.m ├── structure_tensor_lambda.m └── trpca_pstnn.m /README.md: -------------------------------------------------------------------------------- 1 | # Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation 2 | Codes for our paper "Infrared Small Target Detection via Nonconvex Tensor Fibered Rank Approximation" in IEEE TRANSACTIONS ON GEOSCIENCE AND REMOTE SENSING. 3 | -------------------------------------------------------------------------------- /images/1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/images/1.bmp -------------------------------------------------------------------------------- /images/2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/images/2.bmp -------------------------------------------------------------------------------- /lib/BSF_test.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | close all; 4 | 5 | %% 计算目标邻域大小 width为长,height为宽 6 | I=imread('E:/研二上/张量/result/target_compare/PSTNN4.bmp'); 7 | [L,nm] = bwlabel(I,8); 8 | %根据标定数据找到连通域 9 | i=L(112,158); 10 | [r,c] = find(L == i); 11 | left = min(c); 12 | right = max(c); 13 | top = min(r); 14 | buttom = max(r); 15 | width = right - left; 16 | height = buttom - top; 17 | 18 | %% 计算原图灰度标准差 19 | I0=imread('E:/研二上/张量/数据集/data/Video2Frames11/0001.bmp'); 20 | I1=I0(top-25:buttom+25,left-25:right+25); 21 | sigma_b=(std2(I1))^2; 22 | 23 | %% 计算检测图像灰度标准差 24 | I2o=I(top-25:buttom+25,left-25:right+25); 25 | sigma_bo=(std2(I2o))^2; 26 | 27 | %% 计算BSF 28 | BSF=sigma_b/sigma_bo -------------------------------------------------------------------------------- /lib/FPR_TPR.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | close all; 4 | 5 | %% 统计不同阈值下的FPR 6 | I=imread("E:\研二上\张量\result\target_compare\LCM4.bmp"); 7 | [m,n]=size(I); 8 | s=m*n; 9 | FPR=[]; 10 | for i=0.05:0.05:1 11 | I1=im2bw(I,i); 12 | num1=sum(sum(I1==1)); 13 | fpr=num1/s; 14 | FPR=[FPR,fpr]; 15 | end 16 | 17 | %% 统计不同阈值下的TPR 18 | TPR=[]; 19 | for j=0.05:0.05:1 20 | I2=im2bw(I,j); 21 | if(I(112,158)==0) 22 | tpr=0; 23 | else 24 | tpr=1; 25 | end 26 | TPR=[TPR,tpr]; 27 | end 28 | 29 | result=[TPR;FPR]; 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /lib/ProTlogSum.m: -------------------------------------------------------------------------------- 1 | function [X] = ProTlogSum(Y, rho, tol) 2 | 3 | dim = ndims(Y); 4 | [n1, n2, n3] = size(Y); 5 | n12 = min(n1, n2); 6 | Yf = fft(Y, [], dim); 7 | Uf = zeros(n1, n12, n3); 8 | Vf = zeros(n2, n12, n3); 9 | Sf = zeros(n12,n12, n3); 10 | 11 | Yf(isnan(Yf)) = 0; 12 | Yf(isinf(Yf)) = 0; 13 | 14 | trank = 0; 15 | endValue = n3/2 + 1; 16 | for i = 1 : endValue 17 | [ temp, Sf(:, :, i),Uf(:,:,i),Vf(:,:,i)] = Pro2MlogSum(Yf(:,:,i), rho, tol); 18 | trank = max(temp, trank); 19 | end 20 | 21 | for j =n3:-1:endValue+1 22 | Uf(:,:,j) = conj(Uf(:,:,n3-j+2)); 23 | Vf(:,:,j) = conj(Vf(:,:,n3-j+2)); 24 | Sf(:,:,j) = Sf(:,:,n3-j+2); 25 | end 26 | 27 | Uf = Uf(:, 1:trank, :); 28 | Vf = Vf(:, 1:trank, :); 29 | Sf = Sf(1:trank, 1:trank, :); 30 | 31 | U = ifft(Uf, [], dim); 32 | S = ifft(Sf, [], dim); 33 | V = ifft(Vf, [], dim); 34 | 35 | X = tprod( tprod(U,S), tran(V) ); 36 | end 37 | 38 | 39 | 40 | function [n, SigmaNew, U, V] = Pro2MlogSum(Z, tau, tol) 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42 | % min: 1/2*||Z-X||^2 + tau * P_ls^*(X) 43 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | [U, Sigma, V] = svd(Z, 'econ'); 45 | Sigma = diag(Sigma); 46 | c1 = Sigma-tol; 47 | c2 = c1.^2-4*(tau-tol*Sigma); 48 | % tol = Sigma.^2/100000000; 49 | % c1 = Sigma-tol; 50 | % c2 = c1.^2-4*(tau-tol.*Sigma); 51 | ind = find (c2>0); 52 | n = length(ind); 53 | SigmaNew = zeros(length(Sigma),1) ; 54 | SigmaNew(1:n) = (c1(1:n)+sqrt(c2(1:n)))/2; 55 | SigmaNew = diag(SigmaNew); 56 | end -------------------------------------------------------------------------------- /lib/SCRG_test.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | close all; 4 | 5 | %% 计算目标邻域大小 width为长,height为宽 6 | I=imread('E:/研二上/张量/result/target_compare/PSTNN5.bmp'); 7 | [L,nm] = bwlabel(I,8); 8 | %根据标定数据找到连通域 9 | i=L(119,87); 10 | [r,c] = find(L == i); 11 | left = min(c); 12 | right = max(c); 13 | top = min(r); 14 | buttom = max(r); 15 | width = right - left; 16 | height = buttom - top; 17 | 18 | %% 计算原图SCR 19 | I0=imread('E:/研二上/张量/数据集/data/Video2Frames1/0001.jpg'); 20 | I1=I0(top:buttom,left:right); 21 | I2=I0(top-25:buttom+25,left-25:right+25); 22 | miu_t=mean(mean(I1)); 23 | miu_b=mean(mean(I2)); 24 | sigma_b=(std2(I2))^2; 25 | SCR_in=abs(miu_t-miu_b)/sigma_b; 26 | 27 | %% 计算检测图像SCR 28 | I1o=I(top:buttom,left:right); 29 | I2o=I(top-25:buttom+25,left-25:right+25); 30 | miu_to=mean(mean(I1o)); 31 | miu_bo=mean(mean(I2o)); 32 | sigma_bo=(std2(I2o))^2; 33 | SCR_out=abs(miu_to-miu_bo)/sigma_bo; 34 | 35 | %% 计算SCRG 36 | SCRG=SCR_out/SCR_in 37 | -------------------------------------------------------------------------------- /lib/ToolBox/mySVD.m: -------------------------------------------------------------------------------- 1 | function [S, V, D, Sigma2] = MySVD(A) 2 | [m, n] = size(A); 3 | if 2*m < n 4 | AAT = A*A'; 5 | [S, Sigma2, D] = svd(AAT); 6 | Sigma2 = diag(Sigma2); 7 | V = sqrt(Sigma2); 8 | tol = max(size(A)) * eps(max(V)); 9 | R = sum(V > tol); 10 | V = V(1:R); 11 | S = S(:,1:R); 12 | D = A'*S*diag(1./V); 13 | V = diag(V); 14 | return; 15 | end 16 | if m > 2*n 17 | [S, V, D, Sigma2] = mySVD(A'); 18 | mid = D; 19 | D = S; 20 | S = mid; 21 | return; 22 | end 23 | [S,V,D] = svd(A); 24 | Sigma2 = diag(V).^2; -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/disp.m: -------------------------------------------------------------------------------- 1 | function disp(t, name) 2 | %DISP Command window display for a ktensor. 3 | % 4 | % DISP(T) displays a Kruskal tensor with no name. 5 | % 6 | % DISP(T,NAME) display a Kruskal tensor with the given name. 7 | % 8 | % See also DISP, KTENSOR/DISPLAY, KTENSOR 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if ~exist('name','var') 23 | name = 'ans'; 24 | end 25 | 26 | fprintf('%s is a ktensor of size %s\n', name, tt_size2str(size(t))); 27 | fprintf('\t%s.lambda = %s\n',name, ['[ ' num2str(t.lambda') ' ]'] ); 28 | 29 | if (ndims(t) > 0) 30 | for j = 1 : ndims(t) 31 | fprintf('\t%s.U{%d} = \n', name, j); 32 | output = tt_matrix2cellstr(t.u{j}); 33 | fprintf('\t\t%s\n',output{:}); 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display for a ktensor. 3 | % 4 | % DISPLAY(T) displays a Kruskal tensor with its name. 5 | % 6 | % See also DISPLAY, KTENSOR/DISP, KTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | disp(t,inputname(1)); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/double.m: -------------------------------------------------------------------------------- 1 | function A = double(X) 2 | %DOUBLE Convert a ktensor to a double array. 3 | % 4 | % A = double(X) converts X to a standard multidimensional array. 5 | % 6 | % See also KTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | if isempty(X.lambda) % check for empty tensor 21 | A = []; 22 | return; 23 | end 24 | 25 | sz = [size(X) 1]; 26 | A = X.lambda' * khatrirao(X.u,'r')'; 27 | A = reshape(A,sz); 28 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for ktensor. 3 | % 4 | % The expression X(end,:,:) will call END(X,1,3) to determine 5 | % the value of the first index. 6 | % 7 | % See also KTENSOR, KTENSOR/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %TODO (after 2.0 release): Resolve ambiguity w.r.t X{end}and X(end,1,1) 22 | %for 1st-order tensors. 23 | 24 | if n > ndims(X) 25 | error('Subscript out of range.'); 26 | end 27 | 28 | if (n ~= 1) 29 | e = size(X,k); 30 | else 31 | e = ndims(X); 32 | end 33 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/extract.m: -------------------------------------------------------------------------------- 1 | function new_X = extract(X,idx) 2 | %EXTRACT Creates a new ktensor with only the specified components. 3 | % 4 | % Y = EXTRACT(X,S) selected the subset of components in X as defined by 5 | % S. It should be the case that S is a subset of [1,...,NCOMPONENTS(X)]. 6 | % 7 | % See also KTENSOR, NCOMPONENTS. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %% Set-up 22 | N = ndims(X); 23 | %% Extract 24 | new_lambda = X.lambda(idx); 25 | new_U = cell(N,1); 26 | for i = 1 : N 27 | new_U{i} = X.u{i}(:,idx); 28 | end 29 | new_X = ktensor(new_lambda, new_U); 30 | 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/full.m: -------------------------------------------------------------------------------- 1 | function t = full(t) 2 | %FULL Convert a ktensor to a (dense) tensor. 3 | % 4 | % T = FULL(C) converts a ktensor to a (dense) tensor. 5 | % 6 | % Examples 7 | % X = ktensor([3; 2], rand(4,2), rand(5,2), rand(3,2)); 8 | % Y = full(A) %<-- equivalent dense tensor 9 | % 10 | % See also KTENSOR, TENSOR. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | sz = size(t); 25 | data = t.lambda' * khatrirao(t.u,'r')'; 26 | t = tensor(data,sz); 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/innerprod.m: -------------------------------------------------------------------------------- 1 | function res = innerprod(X,Y) 2 | %INNERPROD Efficient inner product with a ktensor. 3 | % 4 | % R = INNERPROD(X,Y) efficiently computes the inner product between 5 | % two tensors X and Y. If Y is a ktensor, the inner product is 6 | % computed using inner products of the factor matrices, X{i}'*Y{i}. 7 | % Otherwise, the inner product is computed using ttv with all of 8 | % the columns of X's factor matrices, X{i}. 9 | % 10 | % See also KTENSOR, KTENSOR/TTV 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | if ~isequal(size(X),size(Y)) 25 | error('X and Y must be the same size.'); 26 | end 27 | 28 | % X is a ktensor 29 | switch class(Y) 30 | 31 | case {'ktensor'} 32 | M = X.lambda * Y.lambda'; 33 | for n = 1:ndims(X) 34 | M = M .* (X.u{n}' * Y.u{n}); 35 | end 36 | res = sum(M(:)); 37 | 38 | case {'tensor','sptensor','ttensor'} 39 | R = length(X.lambda); 40 | vecs = cell(1,ndims(X)); 41 | res = 0; 42 | for r = 1:R 43 | for n = 1:ndims(X) 44 | vecs{n} = X.u{n}(:,r); 45 | end 46 | res = res + X.lambda(r) * ttv(Y,vecs); 47 | end 48 | 49 | otherwise 50 | disp(['Inner product not available for class ' class(Y)]); 51 | end 52 | 53 | return; 54 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/isequal.m: -------------------------------------------------------------------------------- 1 | function [tf, tf_lambda, tf_U] = isequal(A,B) 2 | %ISEQUAL True if each component of two ktensor's is numerically equal. 3 | % 4 | %MATLAB Tensor Toolbox. 5 | %Copyright 2012, Sandia Corporation. 6 | 7 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 8 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 9 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 10 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 11 | % work by or on behalf of the U.S. Government. Export of this data may 12 | % require a license from the United States Government. 13 | % The full license terms can be found in the file LICENSE.txt 14 | 15 | tf = false; 16 | tf_lambda = false; 17 | tf_U = false; 18 | 19 | if ~isa(B,'ktensor') 20 | return; 21 | end 22 | 23 | tf_lambda = isequal(A.lambda, B.lambda); 24 | if ncomponents(A) == ncomponents(B) 25 | tf_U = cellfun(@isequal, A.u, B.u); 26 | end 27 | tf = tf_lambda & all(tf_U); 28 | 29 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/minus.m: -------------------------------------------------------------------------------- 1 | function C = minus(A,B) 2 | %MINUS Binary subtraction for ktensor. 3 | % 4 | % C = MINUS(A,B) computes C = A - B. A and B must both be ktensors 5 | % and have the same size, and the result is another ktensor of the 6 | % same size. 7 | % 8 | % C = MINUS(A,B) is called for the syntax 'A - B' when A or B is a 9 | % ktensor. 10 | % 11 | % See also KTENSOR, SIZE, ISEQUAL. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if (isa(A,'ktensor') && isa(B,'ktensor')) 26 | 27 | if ~isequal(size(A),size(B)) 28 | error('Tensor size mismatch.') 29 | end 30 | 31 | lambda = [A.lambda; -B.lambda]; 32 | M = ndims(A); 33 | u = cell(M,1); 34 | for m = 1 : M 35 | u{m} = [A.u{m} B.u{m}]; 36 | end 37 | C = ktensor(lambda,u); 38 | return; 39 | 40 | end 41 | 42 | error('Use minus(full(A),full(B)).'); 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/mtimes.m: -------------------------------------------------------------------------------- 1 | function C = mtimes(A,B) 2 | %MTIMES Implement A*B (scalar multiply) for ktensor. 3 | % 4 | % C = mtimes(A,B) computes A * B where A is a Kruskal tensor and B is 5 | % a scalar (or vice versa). The result C is the same size as A. 6 | % 7 | % See also KTENSOR. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | % Note: We can do scalar times a tensor, but anything more complex is 22 | % an error. 23 | 24 | if isa(B,'numeric') && isequal(size(B),[1 1]) 25 | C = ktensor(B * A.lambda, A.u); 26 | elseif isa(A,'numeric') && isequal(size(A),[1 1]) 27 | C = ktensor(A * B.lambda, B.u); 28 | else 29 | error('Use mtimes(full(A),full(B)).'); 30 | end 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/mttkrp.m: -------------------------------------------------------------------------------- 1 | function V = mttkrp(X,U,n) 2 | %MTTKRP Matricized tensor times Khatri-Rao product for ktensor. 3 | % 4 | % V = MTTKRP(X,U,n) efficiently calculates the matrix product of the 5 | % n-mode matricization of X with the Khatri-Rao product of all 6 | % entries in U, a cell array of matrices, except the nth. How to 7 | % most efficiently do this computation depends on the type of tensor 8 | % involved. 9 | % 10 | % See also KTENSOR, KTENSOR/TTV 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | N = ndims(X); 25 | 26 | if (n==1) 27 | R = size(U{2},2); 28 | else 29 | R = size(U{1},2); 30 | end 31 | 32 | % Compute matrix of weights 33 | W = repmat(X.lambda,1,R); 34 | for i = [1:n-1,n+1:N] 35 | W = W .* (X.u{i}' * U{i}); 36 | end 37 | 38 | % Find each column of answer by multiplying columns of X.u{n} with weights 39 | V = X.u{n} * W; 40 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/ncomponents.m: -------------------------------------------------------------------------------- 1 | function n = ncomponents(t) 2 | %NCOMPONENTS Number of components for a ktensor. 3 | % 4 | % NCOMPONENTS(T) returns the number of compontents in the ktensor T. 5 | % 6 | % See also KTENSOR 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | n = length(t.lambda); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/ndims.m: -------------------------------------------------------------------------------- 1 | function n = ndims(t) 2 | %NDIMS Number of dimensions for a ktensor. 3 | % 4 | % NDIMS(T) returns the number of dimensions of tensor T. 5 | % 6 | % See also KTENSOR 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | n = numel(t.u); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/norm.m: -------------------------------------------------------------------------------- 1 | function nrm = norm(A) 2 | %NORM Frobenius norm of a ktensor. 3 | % 4 | % NORM(T) returns the Frobenius norm of a ktensor. 5 | % 6 | % See also KTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | % Retrieve the factors of A 21 | U = A.u; 22 | 23 | % Compute the matrix of correlation coefficients 24 | coefMatrix = A.lambda * A.lambda'; 25 | for i = 1:ndims(A) 26 | coefMatrix = coefMatrix .* (U{i}'*U{i}); 27 | end 28 | 29 | nrm = sqrt(abs(sum(coefMatrix(:)))); 30 | 31 | return; 32 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/permute.m: -------------------------------------------------------------------------------- 1 | function b = permute(a,order) 2 | %PERMUTE Permute dimensions of a ktensor. 3 | % 4 | % B = PERMUTE(A,ORDER) rearranges the dimensions of A so that they 5 | % are in the order specified by the vector ORDER. The tensor 6 | % produced has the same values of A but the order of the subscripts 7 | % needed to access any particular element are rearranged as 8 | % specified by ORDER. 9 | % 10 | % See also KTENSOR. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | N = ndims(a); 25 | 26 | if ~isequal(1:N,sort(order)) 27 | error('Invalid permuation'); 28 | end 29 | 30 | b = ktensor(a.lambda, a.u(order)); 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/plus.m: -------------------------------------------------------------------------------- 1 | function C = plus(A,B) 2 | %PLUS Binary addition for ktensor. 3 | % 4 | % C = PLUS(A,B) adds two ktensors of the same size, and the 5 | % result is a ktensor of the same size. 6 | % 7 | % See also KTENSOR. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if (isa(A,'ktensor') && isa(B,'ktensor')) 22 | 23 | if ~isequal(size(A),size(B)) 24 | error('Tensor size mismatch.') 25 | end 26 | 27 | lambda = [A.lambda; B.lambda]; 28 | M = ndims(A); 29 | u = cell(M,1); 30 | for m = 1 : M 31 | u{m} = [A.u{m} B.u{m}]; 32 | end 33 | C = ktensor(lambda, u); 34 | return; 35 | end 36 | 37 | error('Use plus(full(A),full(B)).'); 38 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/redistribute.m: -------------------------------------------------------------------------------- 1 | function X = redistribute(X,mode) 2 | %REDISTRIBUTE Distribute lambda values to a specified mode. 3 | % 4 | % K = REDISTRIBUTE(K,N) absorbs the weights from the lambda vector 5 | % into mode N. Set the lambda vector to all ones. 6 | % 7 | % See also KTENSOR, NORMALIZE. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | for r = 1:length(X.lambda) 21 | X.u{mode}(:,r) = X.u{mode}(:,r) * X.lambda(r); 22 | X.lambda(r) = 1; 23 | end 24 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/size.m: -------------------------------------------------------------------------------- 1 | function m = size(t,idx) 2 | %SIZE Size of ktensor. 3 | % 4 | % D = SIZE(T) returns the size of the tensor. 5 | % 6 | % I = SIZE(T,DIM) returns the size of the dimension specified by 7 | % the scalar DIM. 8 | % 9 | % See also KTENSOR, KTENSOR/NDIMS. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | if isempty(t.lambda) 24 | m = []; 25 | end 26 | 27 | if exist('idx','var') 28 | m = size(t.u{idx}, 1); 29 | else 30 | for i = 1 : ndims(t) 31 | m(i) = size(t.u{i}, 1); 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/subsasgn.m: -------------------------------------------------------------------------------- 1 | function t = subsasgn(t,s,b) 2 | %SUBSASGN Subscripted assignement for ktensor. 3 | % 4 | % See also KTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | switch s(1).type 19 | case '.' 20 | switch s(1).subs 21 | case 'lambda' 22 | if length(s) == 1 23 | t = ktensor(b, t.u); 24 | else 25 | newlambda = subsasgn(t.lambda, s(2:end), b); 26 | t = ktensor(newlambda, t.u); 27 | end 28 | case {'u','U'} 29 | if length(s) == 1 30 | t = ktensor(t.lambda, b); 31 | else 32 | tmpu = subsasgn(t.u, s(2:end), b); 33 | t = ktensor(t.lambda, tmpu); 34 | end 35 | otherwise 36 | error(['No such field: ', s(1).subs]); 37 | end 38 | case '()' 39 | error('Cannot change individual entries in a ktensor.') 40 | case '{}' 41 | new_s(1).type = '.'; 42 | new_s(1).subs = 'u'; 43 | new_s(2:length(s)+1) = s; 44 | t = subsasgn(t, new_s, b); 45 | otherwise 46 | error('Invalid subsasgn.'); 47 | end 48 | 49 | 50 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/subsref.m: -------------------------------------------------------------------------------- 1 | function a = subsref(t,s) 2 | %SUBSREF Subscripted reference for a ktensor. 3 | % 4 | % Examples 5 | % X = ktensor([3; 2], rand(4,2), rand(5,2), rand(3,2)); 6 | % X.lambda returns the lambda array ([3;2]). 7 | % X.U returns a cell array of 3 matrices. 8 | % X.U{1} returns the matrix corresponding to the first mode. 9 | % X(2,3,1) calculates and returns that single element of A. 10 | % 11 | % See also KTENSOR. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | switch s(1).type 26 | case '.' 27 | switch s(1).subs 28 | case 'lambda' 29 | a = tt_subsubsref(t.lambda,s); 30 | case {'u','U'} 31 | a = tt_subsubsref(t.u,s); 32 | otherwise 33 | error(['No such field: ', s(1).subs]); 34 | end 35 | case '()' 36 | a = 0; 37 | for k = 1 : length(t.lambda) 38 | b = t.lambda(k); 39 | for i = 1 : length(s.subs) 40 | b = b * t.u{i}(s.subs{i},k); 41 | end 42 | a = a + b; 43 | end 44 | case '{}' 45 | a = subsref(t.u,s); 46 | otherwise 47 | error('Invalid subsref.'); 48 | end 49 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/times.m: -------------------------------------------------------------------------------- 1 | function C = times(A,B) 2 | %TIMES Element-wise multiplication for ktensor. 3 | % 4 | % TIMES(A,B) denotes element-by-element multiplication. 5 | % 6 | % C = TIMES(A,B) is called for the syntax 'A .* B' when A or B is a 7 | % tensor. 8 | % 9 | % See also KTENSOR, SPTENSOR/TIMES. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | if ~isequal(size(A),size(B)) 24 | error('Must be two tensors of the same size'); 25 | end 26 | 27 | switch class(B) 28 | case {'sptensor','tensor'} 29 | % Call back to sptensor version. 30 | C = times(B,A); 31 | return; 32 | otherwise 33 | error('Invalid second argument for ktensor/times'); 34 | end 35 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/tocell.m: -------------------------------------------------------------------------------- 1 | function U = tocell(X,N) 2 | %TOCELL Convert X to a cell array. 3 | % 4 | % TOCELL(X) converts X to a cell array, evenly distributing the 5 | % weight in lambda. 6 | % 7 | % TOCELL(X,N) absorbs the weights into the Nth factor matrix. 8 | % 9 | % See also KTENSOR, NORMALIZE. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | if exist('N','var') 24 | X = normalize(X,N); 25 | U = X.u; 26 | return; 27 | end 28 | 29 | if isequal(X.lambda,ones(size(X.lambda))) 30 | U = X.u; 31 | return; 32 | end 33 | 34 | 35 | lsplit = nthroot(X.lambda,ndims(X)); 36 | R = length(X.lambda); 37 | U = X.u; 38 | D = diag(lsplit); 39 | for n = 1:ndims(X) 40 | U{n} = U{n} * D; 41 | end 42 | 43 | 44 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus for ktensor. 3 | % 4 | % See also KTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | t.lambda = -t.lambda; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ktensor/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus for a ktensor. 3 | % 4 | % See also KTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | % This function does nothing! 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/Contents.m: -------------------------------------------------------------------------------- 1 | % @SPTENMAT 2 | % 3 | % Files 4 | % aatx - Implicitly compute A * A' * x for sptenmat. 5 | % disp - Command window display of a sptenmat. 6 | % display - Command window display of a sptenmat. 7 | % double - Convert a sptenmat to a sparse matrix. 8 | % end - Last index of indexing expression for sptenmat. 9 | % full - Convert a sptenmat to a (dense) tenmat. 10 | % nnz - Return number of nonzeros in a sptenmat. 11 | % norm - Frobenius norm of a sptenmat. 12 | % size - Return size of sptenmat. 13 | % sptenmat - Matricized sparse tensor stored as a sparse 2D array. 14 | % subsasgn - Subscripted assignment for sptenmat. 15 | % subsref - Subscripted reference for a sptenmat. 16 | % tsize - Tensor size of sptenmat. 17 | % uminus - Unary minus (-) for sptenmat. 18 | % uplus - Unary plus (+) for sptenmat. 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/aatx.m: -------------------------------------------------------------------------------- 1 | function z = aatx(a,x) 2 | %AATX Implicitly compute A * A' * x for sptenmat. 3 | % 4 | % Z = AATX(A,X) takes a sptenmat object A and computes A * A' * 5 | % X. This is done without converting A to a standard MATLAB sparse 6 | % matrix. 7 | % 8 | % This function is likely most useful as an argument to a routine 9 | % such as EIGS. 10 | % 11 | % See also SPTENMAT, SPTENSOR/EIGS. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | subs = a.subs; 26 | s1 = subs(:,1); 27 | s2 = subs(:,2); 28 | m = size(a,1); 29 | n = size(a,2); 30 | vals = a.vals; 31 | 32 | v1 = x(s1); 33 | v1 = vals .* v1; 34 | y = accumarray(s2, v1, [n 1]); 35 | 36 | v2 = y(s2); 37 | v2 = vals .* v2; 38 | z = accumarray(s1, v2, [m 1]); 39 | 40 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display of a sptenmat. 3 | % 4 | % DISPLAY(T) displays the tensor with its name. 5 | % 6 | % See also SPTENMAT/DISP. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | disp(t,inputname(1)); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/double.m: -------------------------------------------------------------------------------- 1 | function A = double(T) 2 | %DOUBLE Convert a sptenmat to a sparse matrix. 3 | % 4 | % A = double(T) converts T stored as a SPTENMAT to a sparse matrix. 5 | % 6 | % See also SPTENMAT. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | m = prod(T.tsize(T.rdims)); 21 | n = prod(T.tsize(T.cdims)); 22 | if isempty(T.subs) 23 | A = sparse(m,n); 24 | else 25 | A = sparse(T.subs(:,1), T.subs(:,2), T.vals, m, n); 26 | end 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for sptenmat. 3 | % 4 | % The expression X(end,:) will call END(X,1,2) to determine 5 | % the value of the first index. 6 | % 7 | % See also SPTENMAT, SPTENMAT/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if n > 2 22 | error('Subscript out of range.'); 23 | end 24 | e = size(X,k); 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/full.m: -------------------------------------------------------------------------------- 1 | function B = full(A) 2 | %FULL Convert a sptenmat to a (dense) tenmat. 3 | % 4 | % B = FULL(A) converts a sptenmat A to a (dense) tenmat B. 5 | % 6 | % See also SPTENMAT, TENMAT. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | % Extract the order and size of A 21 | siz = size(A); 22 | 23 | % Create a dense zero tensor B that is the same size as A 24 | B = tenmat(zeros([siz,1,1]), A.rdims, A.cdims, A.tsize); 25 | 26 | % Extract the linear indices of entries in A 27 | idx = tt_sub2ind(siz,A.subs); 28 | 29 | % Copy the values of A into B using linear indices 30 | B(idx) = A.vals; 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/nnz.m: -------------------------------------------------------------------------------- 1 | function n = nnz(a) 2 | %NNZ Return number of nonzeros in a sptenmat. 3 | % 4 | % nnz(A) returns the number of nonzeros in A. 5 | % 6 | % See also SPTENMAT. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | n = length(a.vals); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/norm.m: -------------------------------------------------------------------------------- 1 | function nrm = norm(T) 2 | %NORM Frobenius norm of a sptenmat. 3 | % 4 | % NORM(T) returns the Frobenius norm of a matricized sparse tensor. 5 | % 6 | % See also SPTENMAT, NORM. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | nrm = norm(T.vals); 21 | 22 | return; 23 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/size.m: -------------------------------------------------------------------------------- 1 | function siz = size(a,idx) 2 | %SIZE Return size of sptenmat. 3 | % 4 | % D = SIZE(T) returns the size of the tensor. 5 | % 6 | % I = size(T,DIM) returns the sizes of the dimensions specified by DIM. 7 | % 8 | % See also SPTENMAT. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if isempty(a.tsize) 23 | siz = []; 24 | return; 25 | end 26 | 27 | m = prod(a.tsize(a.rdims)); 28 | n = prod(a.tsize(a.cdims)); 29 | siz = [m n]; 30 | 31 | if exist('idx','var') 32 | siz = siz(idx); 33 | end 34 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/subsref.m: -------------------------------------------------------------------------------- 1 | function a = subsref(t,s) 2 | %SUBSREF Subscripted reference for a sptenmat. 3 | % 4 | % Examples 5 | % A.subs <-- returns the nonzero values as an array 6 | % A.vals <-- returns the corresponding 2D subscripts 7 | % A.tsize <-- returns the size original tensor 8 | % A.rdims <-- tensor dimensions that were mapped to rows 9 | % A.cdims <-- tensor dimensions that were mapped to columns 10 | % 11 | % See also SPTENMAT. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | switch s(1).type 26 | case '.' 27 | switch s(1).subs 28 | case 'vals' 29 | a = tt_subsubsref(t.vals,s); 30 | case 'tsize' 31 | a = t.tsize; 32 | case 'rdims' 33 | a = t.rdims; 34 | case 'cdims' 35 | a = t.cdims; 36 | case 'subs' 37 | a = tt_subsubsref(t.subs,s); 38 | otherwise 39 | error(['No such field: ', s.subs]); 40 | end 41 | otherwise 42 | error('Invalid subsref into tenmat.') 43 | end 44 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/tsize.m: -------------------------------------------------------------------------------- 1 | function sz = tsize(a,idx) 2 | %TSIZE Tensor size of sptenmat. 3 | % 4 | % D = TSIZE(X) returns the size of the tensor being stored as a 5 | % matrix. 6 | % 7 | % M = TSIZE(X,DIM) returns the length of the dimension(s) specified 8 | % by DIM. For example, SIZE(X,1) returns the size of the first 9 | % dimension of the tensor. 10 | % 11 | % See also SPTENMAT, SPTENMAT/SIZE. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isempty(a.tsize) 26 | sz = []; 27 | return; 28 | end 29 | 30 | if exist('idx', 'var') 31 | sz = a.tsize(idx); 32 | else 33 | sz = a.tsize; 34 | end 35 | 36 | return; 37 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus (-) for sptenmat. 3 | % 4 | % See also SPTENMAT. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | t.vals = -t.vals; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptenmat/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus (+) for sptenmat. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | % This function does nothing! 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/and.m: -------------------------------------------------------------------------------- 1 | function C = and(A,B) 2 | %AND Logical AND (&) for sptensors. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | %% Observations for sparse matrix case. 19 | % The result of a & 5 is sparse. 20 | % The result of a & 0 is sparse. 21 | % The result of a & full(a) is sparse. 22 | 23 | %% Case 1: One argument is a scalar 24 | if isscalar(B) 25 | if B == 0 26 | C = sptensor([],[],size(A)); 27 | else 28 | C = sptensor(A.subs,true,size(A)); 29 | end 30 | return; 31 | end 32 | 33 | % Call back with the arguments reversed. 34 | if isscalar(A) 35 | C = and(B,A); 36 | return; 37 | end 38 | 39 | %% Case 2: Both x and y are tensors of some sort 40 | % Check that the sizes match 41 | if ~isequal(size(A),size(B)) 42 | error('Must be tensors of the same size'); 43 | end 44 | 45 | if isa(A,'sptensor') && isa(B,'sptensor') 46 | C = sptensor([A.subs; B.subs], [A.vals; B.vals], size(A), ... 47 | @(x) length(x) == 2); 48 | return; 49 | end 50 | 51 | if isa(B,'tensor') 52 | BB = sptensor(A.subs,B(A.subs),size(A)); 53 | C = and(A,BB); 54 | return; 55 | end 56 | 57 | %% Otherwise 58 | error('The arguments must be two sptensors or an sptensor and a scalar.'); 59 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/ctranspose.m: -------------------------------------------------------------------------------- 1 | function ctranspose(x) 2 | %CTRANSPOSE is not defined for sparse tensors. 3 | % 4 | % See also SPTENSOR/PERMUTE. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | error('Transpose on sparse tensor is not defined'); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display of a sparse tensor. 3 | % 4 | % DISPLAY(T) displays the tensor with its name. 5 | % 6 | % See also SPTENSOR, SPTENSOR/DISP. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | disp(t,inputname(1)); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/divide.m: -------------------------------------------------------------------------------- 1 | function Y = divide(X,K,epsilon) 2 | %DIVIDE Divide an SPTENSOR by a nonnegative KTENSOR. 3 | % 4 | % Y = DIVIDE(X,K,EPSILON) divides the sparse tensor X by the 5 | % nonnegative ktensor K. Avoids divide-by-zero errors by dividing 6 | % by MIN(EPSILON,K-VALUE) at each nonzero of X. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | % Assumes K is a nonnegative ktensor 20 | 21 | Y = X; 22 | 23 | subs = Y.subs; 24 | vals = zeros(size(Y.vals)); 25 | R = numel(K.lambda); 26 | N = ndims(Y); 27 | for r = 1:R 28 | tvals = ones(size(vals)) * K.lambda(r); 29 | for n = 1:N 30 | v = K{n}(:,r); 31 | tvals = tvals .* v(subs(:,n)); 32 | end 33 | vals = vals + tvals; 34 | end 35 | Y.vals = Y.vals ./ max(epsilon, vals); 36 | 37 | return; 38 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/double.m: -------------------------------------------------------------------------------- 1 | function a = double(s) 2 | %DOUBLE Converts a sparse tensor to a dense multidimensional array. 3 | % 4 | % See also SPTENSOR, SPTENSOR/FULL. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | a = zeros([size(s) 1 1]); 19 | if nnz(s) > 0 20 | a(tt_sub2ind(size(s),s.subs)) = s.vals; 21 | end 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/elemfun.m: -------------------------------------------------------------------------------- 1 | function a = elemfun(a,fun) 2 | %ELEMFUN Manipulate the nonzero elements of a sparse tensor. 3 | % 4 | % X = ELEMFUN(X,@FUN) modifies the elements of X according to the 5 | % function @FUN which should take and array and output an equally 6 | % sized array. 7 | % 8 | % Examples 9 | % X = sptenrand([10,10,10],10); 10 | % X = elemfun(X,@sqrt) %<-- square root of every entry 11 | % X = elemfun(X, @(x) x+1) %<-- increase every entry by 1 12 | % X = elemfun(X, @(x) x ~= 0) %<-- change every nonzero to be 1 13 | % 14 | % See also SPTENSOR, SPFUN. 15 | % 16 | %MATLAB Tensor Toolbox. 17 | %Copyright 2012, Sandia Corporation. 18 | 19 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 20 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 21 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 22 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 23 | % work by or on behalf of the U.S. Government. Export of this data may 24 | % require a license from the United States Government. 25 | % The full license terms can be found in the file LICENSE.txt 26 | 27 | 28 | 29 | if ~isa(a,'sptensor') 30 | error('First argument must be a sparse tensor.'); 31 | end 32 | 33 | a.vals = fun(a.vals); 34 | idx = find(a.vals); 35 | if isempty(idx) 36 | a.vals = []; 37 | a.subs = []; 38 | else 39 | a.vals = a.vals(idx); 40 | a.subs = a.subs(idx,:); 41 | end 42 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for sparse tensor. 3 | % 4 | % The expression X(end,:,:) will call END(X,1,3) to determine 5 | % the value of the first index. 6 | % 7 | % See also SPTENSOR, SPTENSOR/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if n > ndims(X) 22 | error('Subscript out of range.'); 23 | end 24 | e = X.size(k); 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/find.m: -------------------------------------------------------------------------------- 1 | function [subs,vals] = find(t) 2 | %FIND Find subscripts of nonzero elements in a sparse tensor. 3 | % 4 | % [SUBS,VALS] = FIND(T) returns the subscripts and corresponding 5 | % values of the nonzero elements of T. 6 | % 7 | % Note that unlike the standard MATLAB find function for an array, 8 | % find does not return linear indices. Instead, it returns an M x N 9 | % array where M is the number of nonzero values and N = ndims(T). 10 | % Thus, I(k,:) specifies the subscript of value V(k). 11 | % 12 | % See also SPTENSOR, FIND. 13 | % 14 | %MATLAB Tensor Toolbox. 15 | %Copyright 2012, Sandia Corporation. 16 | 17 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 18 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 19 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 20 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 21 | % work by or on behalf of the U.S. Government. Export of this data may 22 | % require a license from the United States Government. 23 | % The full license terms can be found in the file LICENSE.txt 24 | 25 | 26 | subs = t.subs; 27 | vals = t.vals; 28 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/full.m: -------------------------------------------------------------------------------- 1 | function B = full(A) 2 | %FULL Convert a sparse tensor to a (dense) tensor. 3 | % 4 | % B = FULL(A) converts a sptensor A to a (dense) tensor B. 5 | % 6 | % See also SPTENSOR, TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | % Extract the order and size of A 21 | siz = size(A); 22 | 23 | % Create a dense zero tensor B that is the same size as A 24 | B = tensor(zeros([siz,1,1]),siz); 25 | 26 | if isempty(A.subs) 27 | return; 28 | end 29 | 30 | % Extract the linear indices of entries in A 31 | idx = tt_sub2ind(siz,A.subs); 32 | 33 | % Copy the values of A into B using linear indices 34 | B(idx) = A.vals; 35 | 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/isequal.m: -------------------------------------------------------------------------------- 1 | function z = isequal(x,y) 2 | %ISEQUAL for sptensors. 3 | % 4 | % ISEQUAL(A,B) compares the sparse tensors A and B for equality. 5 | % 6 | % See also SPTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | %% Observations for sparse matrix case. 21 | % The result of isequal(a,full(a)) is true! 22 | 23 | %% 24 | if ~isequal(x.size,y.size) 25 | z = false; 26 | elseif isa(x,'sptensor') && isa(y,'sptensor') 27 | z = (nnz(x-y) == 0); 28 | elseif isa(y,'tensor') 29 | z = isequal(full(x),y); 30 | else 31 | z = false; 32 | end 33 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/ldivide.m: -------------------------------------------------------------------------------- 1 | function C = ldivide(A,B) 2 | %LDIVIDE Array right division for sparse tensors. 3 | % 4 | % LDIVIDE(A,B) is called for the syntax 'A .\ B' when A or B is a sparse 5 | % tensor. A and B must have the same size, unless one is a scalar. 6 | % 7 | % See also SPTENSOR. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | C = rdivide(B,A); 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/mldivide.m: -------------------------------------------------------------------------------- 1 | function C = mldivide(A,B) 2 | %MLDIVIDE Slash left division for sparse tensors. 3 | % 4 | % MlDIVIDE(A,B) is called for the syntax 'A \ B' when A is a scalar and B 5 | % is a sparse tensor. 6 | % 7 | % Example 8 | % X = sptenrand([4 3 2],5); 9 | % 3 \ X 10 | % 11 | % See also SPTENSOR. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isscalar(A) 26 | newsubs = B.subs; 27 | newvals = B.vals / A; 28 | if A == 0 29 | nansubs = setdiff(allsubs(A),newsubs,'rows'); 30 | newsubs = [newsubs; nansubs]; 31 | newvals = [newvals; repmat(NaN,size(nansubs,1),1)]; 32 | end 33 | C = sptensor(newsubs,newvals,B.size); 34 | return; 35 | end 36 | 37 | error('MLDIVIDE only supports the scalar case for sparse tensors'); 38 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/mrdivide.m: -------------------------------------------------------------------------------- 1 | function C = mrdivide(A,B) 2 | %MRDIVIDE Slash right division for sparse tensors. 3 | % 4 | % MRDIVIDE(A,B) is called for the syntax 'A / B' when A is a sparse 5 | % tensor and B is a scalar. 6 | % 7 | % Example 8 | % X = sptenrand([4 3 2],5); 9 | % X / 3 10 | % 11 | % See also SPTENSOR. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isscalar(B) 26 | newsubs = A.subs; 27 | newvals = A.vals / B; 28 | if B == 0 29 | nansubs = setdiff(allsubs(A),newsubs,'rows'); 30 | newsubs = [newsubs; nansubs]; 31 | newvals = [newvals; repmat(NaN,size(nansubs,1),1)]; 32 | end 33 | C = sptensor(newsubs,newvals,A.size); 34 | return; 35 | end 36 | 37 | error('MRDIVIDE only supports the scalar case for sparse tensors'); 38 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/mtimes.m: -------------------------------------------------------------------------------- 1 | function C = mtimes(A,B) 2 | %MTIMES sptensor-scalar multiplication. 3 | % 4 | % C = MTIMES(A,B) is called for the syntax 'A * B' when A or B is a 5 | % sparse tensor and the other argument is a scalar. 6 | % 7 | % For tensor-matrix multiplication, use TTM. 8 | % For tensor-tensor multiplication, use TTT. 9 | % For tensor-tensor array multiplication, use TIMES or 'A .* B'. 10 | % 11 | % See also SPTENSOR, SPTENSOR/TTM, SPTENSOR/TTT, SPTENSOR/TIMES 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isscalar(B) 26 | C = sptensor(A.subs, A.vals * B, size(A)); 27 | return; 28 | end 29 | 30 | if isscalar(A) 31 | C = sptensor(B.subs, B.vals * A, size(B)); 32 | return; 33 | end 34 | 35 | error('MTIMES only supports the scalar case for sparse tensors'); 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/mttkrp.m: -------------------------------------------------------------------------------- 1 | function V = mttkrp(X,U,n) 2 | %MTTKRP Matricized tensor times Khatri-Rao product for sparse tensor. 3 | % 4 | % V = MTTKRP(X,U,n) efficiently calculates the matrix product of the 5 | % n-mode matricization of X with the Khatri-Rao product of all 6 | % entries in U, a cell array of matrices, except the nth. How to 7 | % most efficiently do this computation depends on the type of tensor 8 | % involved. 9 | % 10 | % See also SPTENSOR, TENSOR/MTTKRP, SPTENSOR/TTV 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | % In the sparse case, it is most efficient to do a series of TTV operations 25 | % rather than forming the Khatri-Rao product. 26 | 27 | N = ndims(X); 28 | 29 | if (n == 1) 30 | R = size(U{2},2); 31 | else 32 | R = size(U{1},2); 33 | end 34 | 35 | V = zeros(size(X,n),R); 36 | for r = 1:R 37 | % Set up cell array with appropriate vectors for ttv multiplication 38 | Z = cell(N,1); 39 | for i = [1:n-1,n+1:N] 40 | Z{i} = U{i}(:,r); 41 | end 42 | % Perform ttv multiplication 43 | V(:,r) = double(ttv(X, Z, -n)); 44 | end 45 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/ndims.m: -------------------------------------------------------------------------------- 1 | function n = ndims(t) 2 | %NDIMS Number of dimensions of a sparse tensor. 3 | % 4 | % NDIMS(T) returns the number of dimensions of sparse tensor T. 5 | % 6 | % Examples: 7 | % T = sptenrand([3 2 2],5); 8 | % ndims(T) %<-- should return 3 9 | % 10 | % See also SPTENSOR. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | n = size(t.size,2); 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/nnz.m: -------------------------------------------------------------------------------- 1 | function a = nnz(t) 2 | %NNZ Number of nonzeros in sparse tensor. 3 | % 4 | % NNZ(T) is the number of nonzero elements in T. 5 | % 6 | % See also SPTENSOR, SPTENSOR/FIND. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | if isempty(t.subs) 21 | a = 0; 22 | else 23 | a = size(t.subs,1); 24 | end 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/norm.m: -------------------------------------------------------------------------------- 1 | function nrm = norm(T) 2 | %NORM Frobenius norm of a sparse tensor. 3 | % 4 | % NORM(T) returns the Frobenius norm of a sparse tensor. 5 | % 6 | % See also SPTENSOR, NORM. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | nrm = norm(T.vals); 21 | 22 | return; 23 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/not.m: -------------------------------------------------------------------------------- 1 | function y = not(x) 2 | %NOT Logical NOT (~) for sptensors. 3 | % 4 | % ~X performs a logical not on the input tensor X. The result always 5 | % returned as a sparse tensor. 6 | % 7 | % See also SPTENSOR. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %% Observations for sparse matrix case. 22 | % The result of ~a is sparse. 23 | 24 | %% Then compute those indicies that are not in x 25 | subs = setdiff(allsubs(x),x.subs,'rows'); 26 | 27 | %% Assemble final result 28 | y = sptensor(subs,true,x.size); 29 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/ones.m: -------------------------------------------------------------------------------- 1 | function t = ones(t) 2 | %ONES Replace nonzero elements of sparse tensor with ones. 3 | % 4 | % S = ONES(T) generates a sparse tensor with the same sparsity 5 | % structure as T, but with ones in the nonzero position. 6 | % 7 | % See also SPTENSOR, SPONES. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | t.vals = ones(size(t.vals)); 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/or.m: -------------------------------------------------------------------------------- 1 | function C = or(A,B) 2 | %OR Logical OR (|) for sptensors. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | %% Observations for sparse matrix case. 19 | % The result of a | 5 is dense! 20 | % The result of a | 0 is dense! 21 | % The result of a | full(a) is dense! 22 | % The result of a | a is sparse. 23 | 24 | %% Case 1: One argument is a scalar 25 | if isscalar(B) || isa(B,'tensor') 26 | C = full(A) | B; 27 | return; 28 | end 29 | if isscalar(A) 30 | C = A | full(B); 31 | return; 32 | end 33 | 34 | %% Case 2: Both A and B are sparse tensors 35 | if ~isequal(size(A),size(B)) 36 | error('Must be tensors of the same size'); 37 | end 38 | 39 | if isa(A,'sptensor') && isa(B,'sptensor') 40 | C = sptensor([A.subs; B.subs], 1, size(A), @(x) length(x) >= 1); 41 | return; 42 | end 43 | 44 | %% Otherwise 45 | error('The arguments must be two sptensors or an sptensor and a scalar.'); 46 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/permute.m: -------------------------------------------------------------------------------- 1 | function t = permute(t,order) 2 | %PERMUTE Rearrange the dimensions of a sparse tensor. 3 | % 4 | % B = PERMUTE(A,ORDER) rearranges the dimensions of A so that they 5 | % are in the order specified by the vector ORDER. The result has the 6 | % same values of A, but the order of the subscripts needed to access 7 | % any particular element are rearranged as specified by ORDER. 8 | % 9 | % See also SPTENSOR, PERMUTE. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | % Error checking 24 | if (ndims(order) ~= 2) || (size(order,1) ~= 1) 25 | error('ORDER must be a row vector'); 26 | end 27 | 28 | % Check that the permuation is valid 29 | if ~isequal(sort(order),1:ndims(t)) 30 | error('Invalid permutation.'); 31 | end 32 | 33 | % Do the permutation 34 | if ~isempty(t.subs) 35 | t.subs = t.subs(:,order); 36 | end 37 | t.size = t.size(order); 38 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/private/allsubs.m: -------------------------------------------------------------------------------- 1 | function s = allsubs(x) 2 | %ALLSUBS Generate all possible subscripts for a sparse tensor X. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | %% Generate all possible indicies 19 | 20 | % Preallocate (discover any memory issues here!) 21 | s = zeros(prod(x.size),ndims(x)); 22 | 23 | % Generate appropriately sized ones vectors. 24 | o = cell(ndims(x),1); 25 | for n = 1:ndims(x) 26 | o{n} = ones(size(x,n),1); 27 | end 28 | 29 | % Generate each column of the subscripts in turn 30 | for n = 1:ndims(x) 31 | i = o; 32 | i{n} = (1:size(x,n))'; 33 | s(:,n) = khatrirao(i); 34 | end 35 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/private/extract.m: -------------------------------------------------------------------------------- 1 | function a = extract(t,srchsubs) 2 | %EXTRACT Extract value for a sptensor. 3 | % 4 | % EXTRACT(X,SUBS) returns a list of values. 5 | % 6 | % See also SPTENSOR/SUBSREF. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | 21 | % Check range of requested subscripts 22 | p = size(srchsubs,1); 23 | 24 | % Check that all subscripts are positive and less than the max 25 | invalid = (srchsubs < 0) | (srchsubs > ones(p,1)*t.size); 26 | badloc = find(sum(invalid,2)); 27 | if ~isempty(badloc) 28 | fprintf('The following subscripts are invalid: \n'); 29 | badsubs = srchsubs(badloc,:); 30 | badidx = tt_sub2ind(size(t),badsubs); 31 | for i = 1:numel(badloc) 32 | fprintf('\tsubscript = %s (linear index = %d)\n',... 33 | tt_intvec2str(badsubs(i,:)), badidx(i)); 34 | end 35 | error('Invalid subscripts'); 36 | end 37 | 38 | % Set the default answer to zero 39 | a = zeros(p,1); 40 | 41 | % Find which indices already exist and their locations 42 | [tf,loc] = ismember(srchsubs,t.subs,'rows'); 43 | 44 | % Fill in the non-zero elements in the answer 45 | nzsubs = find(tf); 46 | a(nzsubs,1) = t.vals(loc(nzsubs)); 47 | 48 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/private/irenumber.m: -------------------------------------------------------------------------------- 1 | function newsubs = irenumber(t, sz, range) 2 | %RENUMBER indices for sptensor subsasgn 3 | % 4 | % See also SPTENSOR/SUBSASGN 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | nz = nnz(t); 19 | if (nz == 0) 20 | newsubs = []; 21 | return; 22 | end 23 | 24 | newsubs = t.subs; 25 | for i = 1 : numel(range) 26 | r = range{i}; 27 | if ischar(r) && r == ':' 28 | continue; 29 | elseif numel(r) == 1 30 | newsubs = [newsubs(:,1:i-1), r*ones(nz,1), newsubs(:,i:end)]; 31 | else 32 | newsubs(:,i) = r(newsubs(:,i)); 33 | end 34 | end 35 | 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/reshape.m: -------------------------------------------------------------------------------- 1 | function a = reshape(a,new_size,old_modes) 2 | %RESHAPE Reshape sparse tensor. 3 | % 4 | % RESHAPE(X,SIZ) reshapes the sparse tensor to the given size. PROD(SIZ) 5 | % must be the same as PROD(SIZE(X)). 6 | % 7 | % RESHAPE(X,SIZ,MODES) reshapes only the specifies modes and appends the 8 | % new reshaped modes to the end of the indices. 9 | % 10 | % See also SPTENSOR, SPTENSOR/PERMUTE, RESHAPE. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | if ~exist('old_modes','var') 25 | old_modes = 1:ndims(a); 26 | keep_modes = []; 27 | else 28 | keep_modes = setdiff(1:ndims(a),old_modes); 29 | end 30 | old_size = a.size(old_modes); 31 | keep_size = a.size(keep_modes); 32 | 33 | 34 | if prod(new_size) ~= prod(old_size) 35 | error('prod(SIZ) must be the same size of prod(SIZE(X,MODES))'); 36 | end 37 | 38 | inds = tt_sub2ind(old_size,a.subs(:,old_modes)); 39 | new_subs = tt_ind2sub(new_size,inds); 40 | 41 | a.size = [keep_size new_size]; 42 | a.subs = [a.subs(:,keep_modes) new_subs]; 43 | 44 | 45 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/size.m: -------------------------------------------------------------------------------- 1 | function m = size(t,idx) 2 | %SIZE Sparse tensor dimensions. 3 | % 4 | % D = SIZE(T) returns the size of the tensor. 5 | % 6 | % I = size(T,DIM) returns the sizes of the dimensions specified by DIM. 7 | % 8 | % See also SPTENSOR, SPTENSOR/NDIMS. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if exist('idx','var') 23 | m = t.size(idx); 24 | else 25 | m = t.size; 26 | end 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/spmatrix.m: -------------------------------------------------------------------------------- 1 | function s = spmatrix(a) 2 | %SPMATRIX Converts a two-way sparse tensor to sparse matrix. 3 | % 4 | % SPMATRIX(X) converts a sparse tensor to a sparse matrix. The sparse 5 | % tensor must be two-dimensional. 6 | % 7 | % See also SPTENSOR, SPTENSOR/RESHAPE, SPTENMAT 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if ndims(a) ~= 2 22 | error('Sparse tensor must be two dimensional.'); 23 | end 24 | 25 | 26 | if isempty(a.subs) 27 | s = sparse(a.size(1), a.size(2)); 28 | else 29 | s = sparse(a.subs(:,1), a.subs(:,2), a.vals, a.size(1), a.size(2)); 30 | end 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/squeeze.m: -------------------------------------------------------------------------------- 1 | function Y = squeeze(X) 2 | %SQUEEZE Remove singleton dimensions from a sparse tensor. 3 | % 4 | % Y = SQUEEZE(X) returns a sparse tensor Y with the same elements as 5 | % X but with all the singleton dimensions removed. A singleton 6 | % is a dimension such that size(X,dim)==1. 7 | % 8 | % If X has *only* singleton dimensions, then Y is a scalar. 9 | % 10 | % Examples 11 | % squeeze( sptenrand([2,1,3],0.5) ) %<-- returns a 2-by-3 sptensor 12 | % squeeze( sptensor([1 1 1],1,[1 1 1]) ) %<-- returns a scalar 13 | % See also SPTENSOR. 14 | % 15 | %MATLAB Tensor Toolbox. 16 | %Copyright 2012, Sandia Corporation. 17 | 18 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 19 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 20 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 21 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 22 | % work by or on behalf of the U.S. Government. Export of this data may 23 | % require a license from the United States Government. 24 | % The full license terms can be found in the file LICENSE.txt 25 | 26 | 27 | if all(X.size > 1) 28 | % No singleton dimensions to squeeze 29 | Y = X; 30 | else 31 | idx = find(X.size > 1); 32 | if numel(idx) == 0 33 | % Scalar case - only singleton dimensions 34 | Y = X.vals; 35 | else 36 | siz = X.size(idx); 37 | if isempty(X.vals) 38 | Y = sptensor([],[],siz); 39 | else 40 | Y = sptensor(X.subs(:,idx), X.vals, siz); 41 | end 42 | end 43 | end 44 | 45 | return; 46 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/transpose.m: -------------------------------------------------------------------------------- 1 | function transpose(x) 2 | %TRANSPOSE is not defined on sparse tensors. 3 | % 4 | % See also SPTENSOR/PERMUTE. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | error('Transpose on sparse tensor is not defined'); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus (-) for sptensor. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | t.vals = -t.vals; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus (+) for sptensor. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | % This function does nothing! 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@sptensor/xor.m: -------------------------------------------------------------------------------- 1 | function C = xor(A,B) 2 | %XOR Logical XOR for sptensors. 3 | % 4 | % See also SPTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | %% Observations for sparse matrix case. 19 | % The result of xor(a,5) is dense! 20 | % The result of xor(a,0) is dense! 21 | % The result of xor(a,full(a)) is dense! 22 | % The result of xor(a,b) is sparse. 23 | 24 | %% Case 1: One argument is a scalar 25 | if isscalar(B) || isa(B,'tensor') 26 | C = xor(full(A),B); 27 | return; 28 | end 29 | if isscalar(A) 30 | C = xor(A,full(B)); 31 | return; 32 | end 33 | 34 | 35 | %% Case 2: Both x and y are tensors of some sort 36 | if ~isequal(size(A),size(B)) 37 | error('Must be tensors of the same size'); 38 | end 39 | 40 | if isa(A,'sptensor') && isa(B,'sptensor') 41 | C = sptensor([A.subs; B.subs], 1, size(A), @(x) length(x) == 1); 42 | return; 43 | end 44 | 45 | if isa(B,'tensor') 46 | Bsubs = find(B ~= 0); 47 | C = sptensor([A.subs; Bsubs], 1, size(A), @(x) length(x) == 1); 48 | return; 49 | end 50 | 51 | %% Otherwise 52 | error('The arguments must be two sptensors or an sptensor and a scalar.'); 53 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/Contents.m: -------------------------------------------------------------------------------- 1 | % @TENMAT 2 | % 3 | % Files 4 | % ctranspose - Complex conjugate transpose for tenmat. 5 | % disp - Command window display of a matricized tensor (tenmat). 6 | % display - Command window display of a tenmat. 7 | % double - Convert tenmat to double array. 8 | % end - Last index of indexing expression for tenmat. 9 | % minus - Binary subtraction (-) for tenmat. 10 | % mtimes - Multiplies two tenmat objects. 11 | % norm - Frobenius norm of a tenmat. 12 | % plus - Binary addition (+) for tenmat. 13 | % size - Size of tenmat. 14 | % subsasgn - Subscripted assignment for tenmat. 15 | % subsref - Subscripted reference for tenmat. 16 | % tenmat - Create a matricized tensor. 17 | % tsize - Tensor size of tenmat. 18 | % uminus - Unary minus (-) for tenmat. 19 | % uplus - Unary plus (+) for tenmat. 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/ctranspose.m: -------------------------------------------------------------------------------- 1 | function a = ctranspose(a) 2 | %CTRANSPOSE Complex conjugate transpose for tenmat. 3 | % 4 | % C = CTRANSPOSE(A) swaps the row and column indices of A. 5 | % 6 | % See also TENMAT. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | tmp = a.rindices; 21 | a.rindices = a.cindices; 22 | a.cindices = tmp; 23 | a.data = ctranspose(a.data); 24 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/disp.m: -------------------------------------------------------------------------------- 1 | function disp(t,name) 2 | %DISP Command window display of a matricized tensor (tenmat). 3 | % 4 | % DISP(T) displays a tensor as matrix with no name. 5 | % 6 | % DISP(T,NAME) display a tensor as matrix with the given name. 7 | % 8 | % See also TENMAT, TENMAT/DISPLAY. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if ~exist('name','var') 23 | name = 'ans'; 24 | end 25 | 26 | fprintf('%s is a matrix corresponding to a tensor of size %s\n',... 27 | name,tt_size2str(t.tsize)); 28 | fprintf('\t%s.rindices = %s (modes of tensor corresponding to rows)\n',... 29 | name,['[ ' num2str(t.rindices) ' ]']); 30 | fprintf('\t%s.cindices = %s (modes of tensor corresponding to columns)\n',... 31 | name,['[ ' num2str(t.cindices) ' ]']); 32 | 33 | if isempty(t.data) 34 | fprintf('\t%s.data = []\n',name); 35 | else 36 | fprintf('\t%s.data = \n',name); 37 | output = tt_matrix2cellstr(t.data); 38 | fprintf('\t\t%s\n',output{:}); 39 | end 40 | 41 | 42 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display of a tenmat. 3 | % 4 | % See also TENMAT. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | disp(t,inputname(1)); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/double.m: -------------------------------------------------------------------------------- 1 | function a = double(t) 2 | %DOUBLE Convert tenmat to double array. 3 | % 4 | % See also TENMAT. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | a = t.data; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for tenmat. 3 | % 4 | % The expression X(end,:) will call END(X,1,2) to determine 5 | % the value of the first index. 6 | % 7 | % See also TENMAT, TENMAT/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if n > ndims(X) 22 | error('Subscript out of range.'); 23 | end 24 | e = size(X.data,k); 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/minus.m: -------------------------------------------------------------------------------- 1 | function Z = minus(X,Y) 2 | %MINUS Binary subtraction (-) for tenmat. 3 | % 4 | % See also TENMAT, TENMAT/TENMATFUN. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | fun = @minus; 19 | 20 | % One argument is a scalar 21 | if ((prod(size(X)) == 1 || prod(size(Y)) == 1)) 22 | if (prod(size(Y)) == 1) && isa(X,'tenmat') 23 | Z = X; 24 | Z.data = fun(Z.data,Y); 25 | else 26 | Z = Y; 27 | Z.data = fun(X,Z.data); 28 | end 29 | return; 30 | end 31 | 32 | 33 | % Both arguments are tenmats 34 | Z = tenmat(Y); 35 | if ~(isequal(size(Y),size(Z))) 36 | error('Tenmat size mismatch.') 37 | end 38 | Z.data = fun(X.data,Z.data); 39 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/norm.m: -------------------------------------------------------------------------------- 1 | function n = norm(T) 2 | %NORM Frobenius norm of a tenmat. 3 | % 4 | % NORM(X) returns the Frobenius norm of a tenmat. 5 | % 6 | % See also TENMAT. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | v = reshape(T.data, numel(T.data), 1); 21 | n = norm(v); 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/plus.m: -------------------------------------------------------------------------------- 1 | function Z = plus(X,Y) 2 | %PLUS Binary addition (+) for tenmat. 3 | % 4 | % See also TENMAT, TENMAT/TENMATFUN. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | fun = @plus; 19 | 20 | % One argument is a scalar 21 | if ((prod(size(X)) == 1 || prod(size(Y)) == 1)) 22 | if (prod(size(Y)) == 1) && isa(X,'tenmat') 23 | Z = X; 24 | Z.data = fun(Z.data,Y); 25 | else 26 | Z = Y; 27 | Z.data = fun(X,Z.data); 28 | end 29 | return; 30 | end 31 | 32 | 33 | % Both arguments are tenmats 34 | Z = tenmat(Y); 35 | if ~(isequal(size(Y),size(Z))) 36 | error('Tenmat size mismatch.') 37 | end 38 | Z.data = fun(X.data,Z.data); 39 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/size.m: -------------------------------------------------------------------------------- 1 | function sz = size(a,idx) 2 | %SIZE Size of tenmat. 3 | % 4 | % D = SIZE(X) returns the two-element row vector D = [M N] 5 | % containing the number of rows and columns in the matrix. 6 | % 7 | % M = SIZE(X,DIM) returns the length of the dimension specified by 8 | % the scalar DIM. For example, SIZE(X,1) returns the number of 9 | % rows. 10 | % 11 | % See also TENMAT, TENMAT/TSIZE. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isempty(a.data) 26 | sz = []; 27 | elseif exist('idx', 'var') 28 | sz = size(a.data, idx); 29 | else 30 | sz = size(a.data); 31 | end 32 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/subsasgn.m: -------------------------------------------------------------------------------- 1 | function t = subsasgn(t,s,b) 2 | %SUBSASGN Subscripted assignment for tenmat. 3 | % 4 | % Examples 5 | % X = tenmat(rand(3,4,2),1); 6 | % X(1:2,1:2) = ones(2,2); <-- Calls SUBSASGN 7 | % 8 | % See also TENMAT, TENMAT/SUBSREF. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | switch s.type 23 | case '()' 24 | [m n] = size(t.data); 25 | t.data(s.subs{:}) = b; 26 | if ~isequal([m n],size(t.data)) 27 | error('Ambiguous change in size') 28 | end 29 | otherwise 30 | error('Invalid assignment for tenmat.') 31 | end 32 | 33 | 34 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/subsref.m: -------------------------------------------------------------------------------- 1 | function a = subsref(t,s) 2 | %SUBSREF Subscripted reference for tenmat. 3 | % 4 | % Examples 5 | % X(i,j) <-- returns the (i,j) entry in X 6 | % X.data <-- returns a 2D array of the data 7 | % X.tsize <-- returns the size original tensor 8 | % X.rdims <-- tensor dimensions that were mapped to rows 9 | % X.cdims <-- tensor dimensions that were mapped to columns 10 | % 11 | % See also TENMAT. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | switch s(1).type 26 | case '.' 27 | switch s(1).subs 28 | case 'data' 29 | a = tt_subsubsref(t.data,s); 30 | case 'tsize' 31 | a = tt_subsubsref(t.tsize,s); 32 | case {'rindices','rdims'} 33 | a = tt_subsubsref(t.rindices,s); 34 | case {'cindices','cdims'} 35 | a = tt_subsubsref(t.cindices,s); 36 | otherwise 37 | error(['No such field: ', s.subs]); 38 | end 39 | case '()' 40 | a = t.data(s.subs{:}); 41 | otherwise 42 | error('Invalid subsref into tenmat.') 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/tsize.m: -------------------------------------------------------------------------------- 1 | function sz = tsize(a,idx) 2 | %TSIZE Tensor size of tenmat. 3 | % 4 | % D = TSIZE(X) returns the size of the tensor being stored as a 5 | % matrix. 6 | % 7 | % M = TSIZE(X,DIM) returns the length of the dimension(s) specified 8 | % by DIM. For example, SIZE(X,1) returns the size of the first 9 | % dimension of the tensor. 10 | % 11 | % See also TENMAT, TENMAT/SIZE. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isempty(a.data) 26 | sz = []; 27 | elseif exist('idx', 'var') 28 | sz = a.tsize(idx); 29 | else 30 | sz = a.tsize; 31 | end 32 | 33 | return; 34 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus (-) for tenmat. 3 | % 4 | % See also TENMAT. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | t.data = -t.data; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tenmat/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus (+) for tenmat. 3 | % 4 | % See also TENMAT. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | % This function does nothing! 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/and.m: -------------------------------------------------------------------------------- 1 | function Z = and(X,Y) 2 | %AND Logical AND (&) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@and,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ctranspose.m: -------------------------------------------------------------------------------- 1 | function ctranspose(x) 2 | %CTRANSPOSE is not defined for tensors. 3 | % 4 | % See also TENSOR/PERMUTE. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | error('Transpose on tensor is not defined'); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/disp.m: -------------------------------------------------------------------------------- 1 | function disp(X,name) 2 | %DISP Command window display of a tensor. 3 | % 4 | % DISP(X) displays a tensor with no name. 5 | % 6 | % DISP(X,NAME) displays a tensor with the given name. 7 | % 8 | % See also TENSOR, TENSOR/DISPLAY. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if ~exist('name','var') 23 | name = 'ans'; 24 | end 25 | 26 | fprintf(1,'%s is a tensor of size %s\n',name,tt_size2str(X.size)); 27 | 28 | if isempty(X.data) 29 | fprintf(1,'\t%s = []\n',name); 30 | end 31 | 32 | s = shiftdim(num2cell(X.data,1:2),2); 33 | 34 | for i = 1:numel(s) 35 | fprintf('\t%s',name); 36 | if ndims(X) == 1 37 | fprintf('(:)'); 38 | elseif ndims(X) == 2 39 | fprintf('(:,:)'); 40 | elseif ndims(X) > 2 41 | fprintf('(:,:'); 42 | fprintf(',%d',tt_ind2sub(X.size(3:end),i)); 43 | fprintf(')'); 44 | end 45 | fprintf(' = \n'); 46 | output = tt_matrix2cellstr(s{i}); 47 | fprintf('\t%s\n',output{:}); 48 | end 49 | 50 | end 51 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display of a tensor. 3 | % 4 | % DISPLAY(X) displays a tensor with its name. 5 | % 6 | % See also TENSOR, TENSOR/DISP. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | disp(t,inputname(1)); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/double.m: -------------------------------------------------------------------------------- 1 | function A = double(X) 2 | %DOUBLE Convert tensor to double array. 3 | % 4 | % A = double(X) converts X to a standard multidimensional array. 5 | % 6 | % See also TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | A = double(X.data); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for tensor. 3 | % 4 | % The expression X(end,:,:) will call END(X,1,3) to determine 5 | % the value of the first index. 6 | % 7 | % See also TENSOR, TENSOR/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if n > ndims(X) 22 | error('Subscript out of range.'); 23 | end 24 | e = X.size(k); 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/eq.m: -------------------------------------------------------------------------------- 1 | function Z = eq(X,Y) 2 | %EQ Equal (==) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@eq,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/find.m: -------------------------------------------------------------------------------- 1 | function [subs,vals] = find(t) 2 | %FIND Find subscripts of nonzero elements in a tensor. 3 | % 4 | % S = FIND(X) returns the subscripts of the nonzero values in X. 5 | % 6 | % [S,V] = FIND(X) also returns a column vector of the values. 7 | % 8 | % Examples: 9 | % X = tensor(rand(3,4,2)); 10 | % subs = find(X > 0.5) %<-- find subscripts of values greater than 0.5 11 | % vals = X(subs) %<-- extract the actual values 12 | % 13 | % See also TENSOR/SUBSREF, TENSOR/SUBSASGN 14 | % 15 | %MATLAB Tensor Toolbox. 16 | %Copyright 2012, Sandia Corporation. 17 | 18 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 19 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 20 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 21 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 22 | % work by or on behalf of the U.S. Government. Export of this data may 23 | % require a license from the United States Government. 24 | % The full license terms can be found in the file LICENSE.txt 25 | 26 | 27 | % Find the *linear* indices of the nonzero elements 28 | idx = find(t.data); 29 | 30 | % Convert the linear indices to subscripts 31 | subs = tt_ind2sub(t.size,idx); 32 | 33 | % Extract the corresponding values and return as a column vector 34 | if nargout > 1 35 | if isempty(subs) 36 | vals = []; 37 | else 38 | vals = reshape(t.data(idx), length(idx), 1); 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/full.m: -------------------------------------------------------------------------------- 1 | function X = full(X) 2 | %FULL Convert to a (dense) tensor. 3 | % 4 | % FULL(X) returns X, i.e., does nothing. 5 | % 6 | % See also SPTENSOR/FULL, KTENSOR/FULL, TTENSOR/FULL. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | return; 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ge.m: -------------------------------------------------------------------------------- 1 | function Z = ge(X,Y) 2 | %GE Greater than or equal (>=) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@ge,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/gt.m: -------------------------------------------------------------------------------- 1 | function Z = gt(X,Y) 2 | %GT Greater than (>) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@gt,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/innerprod.m: -------------------------------------------------------------------------------- 1 | function res = innerprod(X,Y) 2 | %INNERPROD Efficient inner product with a tensor. 3 | % 4 | % R = INNERPROD(X,Y) efficiently computes the inner product between 5 | % two tensors X and Y. If Y is a tensor, then inner product is 6 | % computed directly. Otherwise, the inner product method for 7 | % that type of tensor is called. 8 | % 9 | % See also TENSOR, SPTENSOR/INNERPROD, KTENSOR/INNERPROD, TTENSOR/INNERPROD 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | % X is a tensor 24 | switch class(Y) 25 | 26 | case {'tensor'} 27 | % No need for same size check because it is implicit in the inner 28 | % product below. 29 | x = reshape(X.data, 1, numel(X.data)); 30 | y = reshape(Y.data, numel(Y.data), 1); 31 | res = x*y; 32 | 33 | case {'sptensor','ktensor','ttensor'} 34 | % Reverse arguments to call specialized code 35 | res = innerprod(Y,X); 36 | 37 | otherwise 38 | disp(['Inner product not available for class ' class(Y)]); 39 | 40 | end 41 | 42 | return; 43 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/isequal.m: -------------------------------------------------------------------------------- 1 | function z = isequal(x,y) 2 | %ISEQUAL for tensors. 3 | % 4 | % ISEQUAL(A,B) compares the tensors A and B for equality. 5 | % 6 | % See also TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | %% 21 | if ~isequal(x.size,y.size) 22 | z = false; 23 | elseif isa(x,'tensor') && isa(y,'tensor') 24 | z = isequal(x.data,y.data); 25 | elseif isa(y,'sptensor') 26 | z = isequal(x,full(y)); 27 | else 28 | z = false; 29 | end 30 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ldivide.m: -------------------------------------------------------------------------------- 1 | function Z = ldivide(X,Y) 2 | %LDIVIDE Left array divide for tensor. 3 | % 4 | % LDIVIDE(A,B) is called for the syntax 'A .\ B' when A or B is a tensor. 5 | % A and B must have the same size, unless one is a scalar. 6 | % 7 | % Examples 8 | % X = tenrand([4 3 2],5); 9 | % X .\ 3 10 | % X .\ X 11 | % 12 | % See also TENSOR, TENSOR/RDIVIDE. 13 | % 14 | %MATLAB Tensor Toolbox. 15 | %Copyright 2012, Sandia Corporation. 16 | 17 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 18 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 19 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 20 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 21 | % work by or on behalf of the U.S. Government. Export of this data may 22 | % require a license from the United States Government. 23 | % The full license terms can be found in the file LICENSE.txt 24 | 25 | 26 | Z = tenfun(@ldivide,X,Y); 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/le.m: -------------------------------------------------------------------------------- 1 | function Z = le(X,Y) 2 | %LE Less than or equal (<=) for tensor. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@le,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/lt.m: -------------------------------------------------------------------------------- 1 | function Z = lt(X,Y) 2 | %LT Less than (<) for tensor. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@lt,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/minus.m: -------------------------------------------------------------------------------- 1 | function Z = minus(X,Y) 2 | %MINUS Binary subtraction (-) for tensors. 3 | % 4 | % MINUS(A,B) is called for the syntax 'A - B' when A or B is a tensor. A 5 | % and B must have the same size, unless one is a scalar. A scalar can be 6 | % subtracted from a tensor of any size. 7 | % 8 | % See also TENSOR. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | Z = tenfun(@minus,X,Y); 23 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/mldivide.m: -------------------------------------------------------------------------------- 1 | function Z = mldivide(X,Y) 2 | %MLDIVIDE Slash left division for tensors. 3 | % 4 | % MLDIVIDE(A,B) is called for the syntax 'A \ B' when A is a scalar and B 5 | % is a tensor. 6 | % 7 | % Example 8 | % X = tenrand([4 3 2],5); 9 | % 3 \ X 10 | % 11 | % See also TENSOR, TENSOR/LDIVIDE. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isscalar(X) 26 | Z = tenfun(@ldivide,X,Y); 27 | return; 28 | end 29 | 30 | error('MLDIVIDE only supports the scalar case for tensors'); 31 | 32 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/mrdivide.m: -------------------------------------------------------------------------------- 1 | function Z = mrdivide(X,Y) 2 | %MRDIVIDE Slash right division for tensors. 3 | % 4 | % MRDIVIDE(A,B) is called for the syntax 'A / B' when A is a tensor and B 5 | % is a scalar. 6 | % 7 | % Example 8 | % X = tenrand([4 3 2],5); 9 | % X / 3 10 | % 11 | % See also TENSOR, TENSOR/RDIVIDE. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | if isscalar(Y) 26 | Z = tenfun(@rdivide,X,Y); 27 | return; 28 | end 29 | 30 | error('MRDIVIDE only supports the scalar case for tensors'); 31 | 32 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/mtimes.m: -------------------------------------------------------------------------------- 1 | function C = mtimes(A,B) 2 | %MTIMES tensor-scalar multiplication. 3 | % 4 | % C = MTIMES(A,B) is called for the syntax 'A * B' when A or B is a 5 | % tensor and the other argument is a scalar. 6 | % 7 | % For tensor-matrix multiplication, use TTM. 8 | % For tensor-tensor multiplication, use TTT. 9 | % For tensor-tensor array multiplication, use TIMES or 'A .* B'. 10 | % 11 | % Examples 12 | % X = tenrand([3,4,2]) 13 | % W = 5 * X 14 | % 15 | % See also TENSOR, TENSOR/TTM, TENSOR/TTT, TENSOR/TIMES 16 | % 17 | %MATLAB Tensor Toolbox. 18 | %Copyright 2012, Sandia Corporation. 19 | 20 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 21 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 22 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 23 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 24 | % work by or on behalf of the U.S. Government. Export of this data may 25 | % require a license from the United States Government. 26 | % The full license terms can be found in the file LICENSE.txt 27 | 28 | 29 | %% 30 | if isscalar(B) 31 | C = A; 32 | C.data = B * C.data; 33 | return; 34 | end 35 | 36 | if isscalar(A) 37 | C = B; 38 | C.data = A * C.data; 39 | return; 40 | end 41 | 42 | error('Mtimes only supports a tensor times a scalar'); 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/mttkrp.m: -------------------------------------------------------------------------------- 1 | function V = mttkrp(X,U,n) 2 | %MTTKRP Matricized tensor times Khatri-Rao product for tensor. 3 | % 4 | % V = MTTKRP(X,U,n) efficiently calculates the matrix product of the 5 | % n-mode matricization of X with the Khatri-Rao product of all 6 | % entries in U, a cell array of matrices, except the nth. How to 7 | % most efficiently do this computation depends on the type of tensor 8 | % involved. 9 | % 10 | % See also TENSOR, TENMAT, KHATRIRAO 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | N = ndims(X); 25 | if (N < 2) 26 | error('MTTKRP is invalid for tensors with fewer than 2 dimensions'); 27 | end 28 | 29 | if (length(U) ~= N) 30 | error('Cell array is the wrong length'); 31 | end 32 | 33 | if n == 1 34 | R = size(U{2},2); 35 | else 36 | R = size(U{1},2); 37 | end 38 | 39 | for i = 1:N 40 | if i == n, continue; end 41 | if (size(U{i},1) ~= size(X,i)) || (size(U{i},2) ~= R) 42 | error('Entry %d of cell array is wrong size', i); 43 | end 44 | end 45 | 46 | Xn = permute(X,[n 1:n-1,n+1:N]); 47 | Xn = reshape(Xn.data, size(X,n), prod(size(X))/size(X,n)); %#ok 48 | Z = khatrirao(U{[1:n-1,n+1:N]},'r'); 49 | V = Xn*Z; 50 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ndims.m: -------------------------------------------------------------------------------- 1 | function n = ndims(t) 2 | %NDIMS Return the number of dimensions of a tensor. 3 | % 4 | % NDIMS(X) returns the number of dimensions of tensor X. 5 | % 6 | % Examples 7 | % A = rand(4,3,1); ndims(A) %<-- Returns 2 8 | % X = tensor(A); ndims(X) %<-- Returns 2 9 | % X = tensor(A,[4 3 1]); ndims(X) %<-- Returns 3 10 | % 11 | % See also TENSOR 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | 25 | n = numel(t.size); 26 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ne.m: -------------------------------------------------------------------------------- 1 | function Z = ne(X,Y) 2 | %NE Not equal (~=) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@ne,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/nnz.m: -------------------------------------------------------------------------------- 1 | function n = nnz(x) 2 | %NNZ Number of nonzeros for tensors. 3 | % 4 | % See also TENSOR, SPTENSOR/NNZ. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | n = nnz(x.data); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/norm.m: -------------------------------------------------------------------------------- 1 | function n = norm(T) 2 | %NORM Frobenius norm of a tensor. 3 | % 4 | % NORM(X) returns the Frobenius norm of a tensor. 5 | % 6 | % See also TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | v = reshape(T.data, numel(T.data), 1); 21 | n = norm(v); 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/not.m: -------------------------------------------------------------------------------- 1 | function B = not(A) 2 | %NOT Logical NOT (~) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | B = tensor(not(A.data), size(A)); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/or.m: -------------------------------------------------------------------------------- 1 | function Z = or(X,Y) 2 | %OR Logical OR (|) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@or,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/permute.m: -------------------------------------------------------------------------------- 1 | function T = permute(T,order) 2 | %PERMUTE Permute tensor dimensions. 3 | % 4 | % B = PERMUTE(A,ORDER) rearranges the dimensions of A so that they 5 | % are in the order specified by the vector ORDER. The result has the 6 | % same values of A, but the order of the subscripts needed to access 7 | % any particular element are rearranged as specified by ORDER. 8 | % 9 | % See also TENSOR, TENSOR/SIZE, TENSOR/NDIMS, PERMUTE. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | if ndims(T) ~= numel(order) 24 | error('Invalid permutation order'); 25 | end 26 | 27 | % Check for special case of permuting an order-1 object (which has 28 | % no effect but confuses MATLAB's permute command which doesn't 29 | % think that there is such a thing as a 1D-array). 30 | if isequal(order,1) 31 | return; 32 | end 33 | 34 | % Check for special case of empty object (which has 35 | % no effect but confuses MATLAB's permute command which doesn't 36 | % think that there is such a thing as an empty array). 37 | if isempty(order) 38 | return; 39 | end 40 | 41 | % Note that permute does error checking on order, so we don't worry 42 | % about it. 43 | T.data = permute(T.data,order); 44 | T.size = T.size(order); 45 | 46 | return; 47 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/plus.m: -------------------------------------------------------------------------------- 1 | function Z = plus(X,Y) 2 | %PLUS Binary addition (+) for tensors. 3 | % 4 | % PLUS(A,B) is called for the syntax 'A + B' when A or B is a tensor. A 5 | % and B must have the same size, unless one is a scalar. A scalar can be 6 | % added to a tensor of any size. 7 | % 8 | % See also TENSOR. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | Z = tenfun(@plus,X,Y); 23 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/power.m: -------------------------------------------------------------------------------- 1 | function Z = power(X,Y) 2 | %POWER Elementwise power (.^) operator for a tensor. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@power,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/rdivide.m: -------------------------------------------------------------------------------- 1 | function Z = rdivide(X,Y) 2 | %RDIVIDE Right array divide for tensors. 3 | % 4 | % RDIVIDE(A,B) is called for the syntax 'A ./ B' when A or B is a tensor. 5 | % A and B must have the same size, unless one is a scalar. 6 | % 7 | % Examples 8 | % X = tenrand([4 3 2],5); 9 | % X ./ 3 10 | % X ./ X 11 | % 12 | % See also TENSOR. 13 | % 14 | %MATLAB Tensor Toolbox. 15 | %Copyright 2012, Sandia Corporation. 16 | 17 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 18 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 19 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 20 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 21 | % work by or on behalf of the U.S. Government. Export of this data may 22 | % require a license from the United States Government. 23 | % The full license terms can be found in the file LICENSE.txt 24 | 25 | 26 | Z = tenfun(@rdivide,X,Y); 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/reshape.m: -------------------------------------------------------------------------------- 1 | function t = reshape(t,siz) 2 | %RESHAPE Change tensor size. 3 | % RESHAPE(X,SIZ) returns the tensor whose elements 4 | % have been reshaped to the appropriate size. 5 | % 6 | % See also SQUEEZE. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | if prod(t.size) ~= prod(siz) 21 | error('Number of elements cannot change'); 22 | end 23 | 24 | t.data = reshape(t.data,siz); 25 | t.size = siz; 26 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/size.m: -------------------------------------------------------------------------------- 1 | function m = size(t,idx) 2 | %SIZE Tensor dimensions. 3 | % 4 | % D = SIZE(T) returns the sizes of each dimension of tensor X in a 5 | % vector D with ndims(X) elements. 6 | % 7 | % I = size(T,DIM) returns the size of the dimension specified by 8 | % the scalar DIM. 9 | % 10 | % Examples 11 | % A = rand(3,4,2,1); T = tensor(A,[3 4 2 1]); 12 | % size(A) %<-- returns a length-3 vector 13 | % size(T) %<-- returns a length-4 vector 14 | % size(A,2) %<-- returns 4 15 | % size(T,2) %<-- same 16 | % size(A,5) %<-- returns 1 17 | % size(T,5) %<-- ERROR! 18 | % 19 | % See also TENSOR, TENSOR/NDIMS, SIZE. 20 | % 21 | %MATLAB Tensor Toolbox. 22 | %Copyright 2012, Sandia Corporation. 23 | 24 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 25 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 26 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 27 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 28 | % work by or on behalf of the U.S. Government. Export of this data may 29 | % require a license from the United States Government. 30 | % The full license terms can be found in the file LICENSE.txt 31 | 32 | 33 | 34 | if exist('idx','var') 35 | m = t.size(idx); 36 | else 37 | m = t.size; 38 | end 39 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/squeeze.m: -------------------------------------------------------------------------------- 1 | function Y = squeeze(X) 2 | %SQUEEZE Remove singleton dimensions from a tensor. 3 | % 4 | % Y = SQUEEZE(X) returns a tensor Y with the same elements as 5 | % X but with all the singleton dimensions removed. A singleton 6 | % is a dimension such that size(X,dim)==1. 7 | % 8 | % If X has *only* singleton dimensions, then Y is a scalar. 9 | % 10 | % Examples 11 | % squeeze( tenrand([2,1,3]) ) %<-- returns a 2-by-3 tensor 12 | % squeeze( tenrand([1 1]) ) %<-- returns a scalar 13 | % 14 | % See also TENSOR. 15 | % 16 | %MATLAB Tensor Toolbox. 17 | %Copyright 2012, Sandia Corporation. 18 | 19 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 20 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 21 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 22 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 23 | % work by or on behalf of the U.S. Government. Export of this data may 24 | % require a license from the United States Government. 25 | % The full license terms can be found in the file LICENSE.txt 26 | 27 | 28 | if all(X.size > 1) 29 | % No singleton dimensions to squeeze 30 | Y = X; 31 | else 32 | idx = find(X.size > 1); 33 | if numel(idx) == 0 34 | % Scalar case - only singleton dimensions 35 | Y = X.data; 36 | else 37 | siz = X.size(idx); 38 | Y = tensor(squeeze(X.data),siz); 39 | end 40 | end 41 | 42 | return; 43 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/times.m: -------------------------------------------------------------------------------- 1 | function Z = times(X,Y) 2 | %TIMES Array multiplication for tensors. 3 | % 4 | % TIMES(A,B) is called for the syntax 'A .* B' when A or B is a 5 | % tensor. A and B must have the same size, unless one is a scalar. A 6 | % scalar can be multiplied by a tensor of any size. 7 | % 8 | % See also TENSOR. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if isa(Y,'ktensor') || isa(Y,'ttensor') || isa(Y,'sptensor') 23 | Y = full(Y); 24 | end 25 | 26 | Z = tenfun(@times,X,Y); 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/transpose.m: -------------------------------------------------------------------------------- 1 | function transpose(x) 2 | %TRANSPOSE is not defined on tensors. 3 | % 4 | % See also TENSOR/PERMUTE. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | error('Transpose on tensor is not defined'); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/ttsv.m: -------------------------------------------------------------------------------- 1 | function y = ttsv(A,x,n) 2 | %TTSV Tensor times same vector in multiple modes. 3 | % 4 | % Y = TTSV(A,X) multiples the tensor A by the vector X in all modes. 5 | % 6 | % Y = TTSV(A,X,-1) multiplies the tensor A by the vector X in all modes 7 | % but the first. Returns the answer as a normal MATLAB array (not a 8 | % tensor). 9 | % 10 | % Y = TTSV(A,X,-2) multiplies the tensor A by the vector X in all modes 11 | % but the first two. Returns the answer as a normal MATLAB matrix (not a 12 | % tensor). 13 | % 14 | % Y = TTSV(A,X,-N) multiplies the tensor A by the vector X is all modes 15 | % but the first N. 16 | % 17 | % See also TENSOR/TTV. 18 | % 19 | %MATLAB Tensor Toolbox. 20 | %Copyright 2012, Sandia Corporation. 21 | 22 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 23 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 24 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 25 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 26 | % work by or on behalf of the U.S. Government. Export of this data may 27 | % require a license from the United States Government. 28 | % The full license terms can be found in the file LICENSE.txt 29 | 30 | %% Process inputs (only two simple cases are supported) 31 | if ~exist('n','var') 32 | n = 0; 33 | elseif (n > 0) 34 | error('Invalid usage'); 35 | end 36 | 37 | %% Create X. 38 | P = ndims(A); 39 | [X{1:P}] = deal(x); 40 | 41 | %% Calculate 42 | if (n == 0) 43 | y = ttv(A,X); 44 | elseif (n == -1) || (n == -2) 45 | y = double(ttv(A,X,-(1:-n))); 46 | else 47 | y = ttv(A,X,-(1:-n)); 48 | end 49 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus (-) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | t.data = -t.data; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus (+) for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | % This function does nothing! 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@tensor/xor.m: -------------------------------------------------------------------------------- 1 | function Z = xor(X,Y) 2 | %XOR Logical EXCLUSIVE OR for tensors. 3 | % 4 | % See also TENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | Z = tenfun(@xor,X,Y); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/Contents.m: -------------------------------------------------------------------------------- 1 | % @TTENSOR 2 | % 3 | % Files 4 | % disp - Command window display of a ttensor. 5 | % display - Command window display of a ttensor. 6 | % double - Convert ttensor to double array. 7 | % end - Last index of indexing expression for ttensor. 8 | % full - Convert a ttensor to a (dense) tensor. 9 | % innerprod - Efficient inner product with a ttensor. 10 | % isequal - True if each component of two ttensor's is numerically equal. 11 | % mtimes - Implement scalar multiplication for a ttensor. 12 | % mttkrp - Matricized tensor times Khatri-Rao product for ttensor. 13 | % ndims - Return the number of dimensions for a ttensor. 14 | % norm - Norm of a ttensor. 15 | % nvecs - Compute the leading mode-n vectors for a ttensor. 16 | % permute - Permute dimensions for a ttensor. 17 | % size - Size of a ttensor. 18 | % subsasgn - Subscripted reference for a ttensor. 19 | % subsref - Subscripted reference for a ttensor. 20 | % ttensor - Tensor stored as a Tucker operator (decomposed). 21 | % ttm - Tensor times matrix for ttensor. 22 | % ttv - Tensor times vector for ttensor. 23 | % uminus - Unary minus for ttensor. 24 | % uplus - Unary plus for ttensor. 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/disp.m: -------------------------------------------------------------------------------- 1 | function disp(t,name) 2 | %DISP Command window display of a ttensor. 3 | % 4 | % DISP(T) displays a ttensor with no name. 5 | % 6 | % DISP(T,NAME) display a ttensor with the given name. 7 | % 8 | % See also TTENSOR. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2010, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 14 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 20 | % $Id: disp.m,v 1.14 2010/03/19 23:46:31 tgkolda Exp $ 21 | 22 | if ~exist('name','var') 23 | name = 'ans'; 24 | end 25 | 26 | fprintf(1,'%s is a ttensor of size %s\n', name, tt_size2str(size(t))); 27 | disp(t.core, sprintf('\t%s.core',name)); 28 | 29 | for j = 1 : ndims(t) 30 | fprintf('\t%s.U{%d} = \n', name, j); 31 | output = tt_matrix2cellstr(t.u{j}); 32 | fprintf('\t\t%s\n',output{:}); 33 | end 34 | 35 | 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/display.m: -------------------------------------------------------------------------------- 1 | function display(t) 2 | %DISPLAY Command window display of a ttensor. 3 | % 4 | % See also TTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2010, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 10 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 16 | % $Id: display.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 17 | 18 | disp(t,inputname(1)); 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/double.m: -------------------------------------------------------------------------------- 1 | function A = double(T) 2 | %DOUBLE Convert ttensor to double array. 3 | % 4 | % A = double(T) converts T to a standard multidimensional array. 5 | % 6 | % See also TTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2010, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 12 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 18 | % $Id: double.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 19 | 20 | A = double(full(T)); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/end.m: -------------------------------------------------------------------------------- 1 | function e = end(X,k,n) 2 | %END Last index of indexing expression for ttensor. 3 | % 4 | % The expression X(end,:,:) will call END(X,1,3) to determine 5 | % the value of the first index. 6 | % 7 | % See also TTENSOR, TTENSOR/SUBSREF, END. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2010, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 13 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 19 | % $Id: end.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 20 | 21 | % Note that this only works with {} because () is not supported by 22 | % subsref. 23 | e = ndims(X); 24 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/full.m: -------------------------------------------------------------------------------- 1 | function X = full(T) 2 | %FULL Convert a ttensor to a (dense) tensor. 3 | % 4 | % X = FULL(T) converts ttensor T to (dense) tensor X. 5 | % 6 | % See also TTENSOR, TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2010, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 12 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 18 | % $Id: full.m,v 1.11 2010/03/19 23:46:31 tgkolda Exp $ 19 | 20 | % Preallocate to ensure there is enough space 21 | X = tenzeros(size(T)); 22 | 23 | % Now do the calculation 24 | X = ttm(T.core,T.u); 25 | 26 | % Make sure that X is a dense tensor (small chance it could be a sparse 27 | % tensor). 28 | X = tensor(X); 29 | 30 | return; 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/isequal.m: -------------------------------------------------------------------------------- 1 | function [tf, tf_core, tf_U] = isequal(A,B) 2 | %ISEQUAL True if each component of two ttensor's is numerically equal. 3 | 4 | tf = false; 5 | tf_core = false; 6 | tf_U = false; 7 | 8 | if ~isa(B,'ttensor') 9 | return; 10 | end 11 | 12 | if ndims(A) ~= ndims(B) 13 | return; 14 | end 15 | 16 | tf_core = isequal(A.core, B.core); 17 | tf_U = cellfun(@isequal, A.u, B.u); 18 | tf = tf_core & all(tf_U); 19 | 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/mtimes.m: -------------------------------------------------------------------------------- 1 | function C = mtimes(A,B) 2 | %MTIMES Implement scalar multiplication for a ttensor. 3 | % 4 | % See also TTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2010, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 10 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 16 | % $Id: mtimes.m,v 1.8 2010/03/19 23:46:31 tgkolda Exp $ 17 | 18 | if ~isa(B,'ttensor') && numel(B) == 1 19 | C = ttensor(B * A.core, A.u); 20 | elseif ~isa(A,'ttensor') && numel(A) == 1 21 | C = ttensor(A * B.core, B.u); 22 | else 23 | error('Use mtimes(full(A),full(B)).'); 24 | end 25 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/mttkrp.m: -------------------------------------------------------------------------------- 1 | function V = mttkrp(X,U,n) 2 | %MTTKRP Matricized tensor times Khatri-Rao product for ttensor. 3 | % 4 | % V = MTTKRP(X,U,n) efficiently calculates the matrix product of the 5 | % n-mode matricization of X with the Khatri-Rao product of all 6 | % entries in U, a cell array of matrices, except the nth. How to 7 | % most efficiently do this computation depends on the type of tensor 8 | % involved. 9 | % 10 | % See also TTENSOR, TTENSOR/TTV 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2010, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 16 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 22 | % $Id: mttkrp.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 23 | 24 | N = ndims(X); 25 | 26 | if (n==1) 27 | R = size(U{2},2); 28 | else 29 | R = size(U{1},2); 30 | end 31 | 32 | % Compute cell array of weights to multiply into core 33 | W = cell(N,1); 34 | for i = [1:n-1,n+1:N] 35 | W{i} = (X.u{i}' * U{i}); 36 | end 37 | Y = mttkrp(X.core,W,n); 38 | 39 | % Find each column of answer by multiplying columns of X.u{n} with weights 40 | V = X.u{n} * Y; 41 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/ndims.m: -------------------------------------------------------------------------------- 1 | function n = ndims(t) 2 | %NDIMS Return the number of dimensions for a ttensor. 3 | % 4 | % NDIMS(T) returns the number of dimensions of tensor T. 5 | % 6 | % See also TTENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2010, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 12 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 18 | % $Id: ndims.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 19 | 20 | n = numel(t.u); 21 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/norm.m: -------------------------------------------------------------------------------- 1 | function nrm = norm(X) 2 | %NORM Norm of a ttensor. 3 | % 4 | % See also TTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2010, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 10 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 16 | % $Id: norm.m,v 1.9 2010/03/19 23:46:31 tgkolda Exp $ 17 | 18 | 19 | if prod(size(X)) > prod(size(X.core)) 20 | V = cell(ndims(X),1); 21 | for n = 1:ndims(X) 22 | V{n} = X.u{n}'*X.u{n}; 23 | end 24 | Y = ttm(X.core,V); 25 | tmp = innerprod(Y, X.core); 26 | nrm = sqrt(tmp); 27 | else 28 | nrm = norm(full(X)); 29 | end 30 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/permute.m: -------------------------------------------------------------------------------- 1 | function b = permute(a,order) 2 | %PERMUTE Permute dimensions for a ttensor. 3 | % 4 | % Y = PERMUTE(X,ORDER) rearranges the dimensions of X so that they 5 | % are in the order specified by the vector ORDER. The tensor 6 | % produced has the same values of X but the order of the subscripts 7 | % needed to access any particular element are rearranged as 8 | % specified by ORDER. 9 | % 10 | % See also TTENSOR. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2010, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 16 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 22 | % $Id: permute.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 23 | 24 | N = ndims(a); 25 | 26 | if ~isequal(1:N,sort(order)) 27 | error('Invalid permuation'); 28 | end 29 | 30 | newcore = permute(a.core,order); 31 | newu = a.u(order); 32 | b = ttensor(newcore,newu); 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/size.m: -------------------------------------------------------------------------------- 1 | function m = size(t,idx) 2 | %SIZE Size of a ttensor. 3 | % 4 | % D = SIZE(T) returns the size of the tensor. 5 | % 6 | % I = size(T,DIM) returns the size of the dimension specified by 7 | % the scalar DIM. 8 | % 9 | % See also TTENSOR, TTENSOR/NDIMS. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2010, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 15 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 21 | % $Id: size.m,v 1.8 2010/03/19 23:46:31 tgkolda Exp $ 22 | 23 | if ndims(t) == 0 24 | m = []; 25 | end 26 | 27 | if exist('idx','var') 28 | m = size(t.u{idx}, 1); 29 | else 30 | for i = 1 : ndims(t) 31 | m(i) = size(t.u{i}, 1); 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/subsref.m: -------------------------------------------------------------------------------- 1 | function a = subsref(t,s) 2 | %SUBSREF Subscripted reference for a ttensor. 3 | % 4 | % Examples 5 | % core = tensor(rand(2,2,2)); 6 | % X = TTENSOR(core, rand{4,2), rand(5,2),rand(3,2)); 7 | % X.core %<-- returns core array 8 | % X.U %<-- returns a cell array of three matrices 9 | % X.U{1} %<-- returns the matrix corresponding to the first mode. 10 | % 11 | % See also TTENSOR. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2010, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 17 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 23 | % $Id: subsref.m,v 1.10 2010/03/19 23:46:31 tgkolda Exp $ 24 | 25 | switch s(1).type 26 | case '.' 27 | switch s(1).subs 28 | case {'core','lambda'} 29 | a = tt_subsubsref(t.core,s); 30 | case {'U','u'} 31 | a = tt_subsubsref(t.u,s); 32 | otherwise 33 | error(['No such field: ', s.subs]); 34 | end 35 | case '()' 36 | error('Subsref with () not supported for ttensor.'); 37 | case '{}' 38 | new_s(1).type = '.'; 39 | new_s(1).subs = 'u'; 40 | new_s(2:length(s)+1) = s; 41 | a = subsref(t, new_s); 42 | otherwise 43 | error('Invalid subsref.'); 44 | end 45 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/uminus.m: -------------------------------------------------------------------------------- 1 | function t = uminus(t) 2 | %UMINUS Unary minus for ttensor. 3 | % 4 | % See also TTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2010, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 10 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 16 | % $Id: uminus.m,v 1.8 2010/03/19 23:46:31 tgkolda Exp $ 17 | 18 | t.core = -t.core; 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/@ttensor/uplus.m: -------------------------------------------------------------------------------- 1 | function t = uplus(t) 2 | %UPLUS Unary plus for ttensor. 3 | % 4 | % See also TTENSOR. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2010, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. 10 | % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2010) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in tensor_toolbox/LICENSE.txt 16 | % $Id: uplus.m,v 1.7 2010/03/19 23:46:31 tgkolda Exp $ 17 | 18 | % This function does nothing! 19 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/INSTALL.txt: -------------------------------------------------------------------------------- 1 | Please remember to cite the appropriate references when you have used 2 | Tensor Toolbox in your work. See "How to Cite" under 3 | http://www.sandia.gov/~tgkolda/TensorToolbox/ for more details. 4 | 5 | To install the Tensor Toolbox for MATLAB: 6 | 7 | 1. Unpack the compressed file. In a linux environment, for example, this can 8 | be done from the command line via: 9 | 10 | unzip tensor_toolbox_2.5.zip 11 | 12 | *or* 13 | 14 | gunzip -c tensor_toolbox_2.5.tar.gz | tar xvf - 15 | 16 | This should create a directory named *tensor_toolbox_2.5*. 17 | 18 | 2. Rename the root directory from *tensor_toolbox_2.5* 19 | to *tensor_toolbox*. 20 | 21 | 3. Start MATLAB. 22 | 23 | 4. Within MATLAB, cd to the *tensor_toolbox* directory and execute the 24 | following commands: 25 | 26 | addpath(pwd) %<-- Add the tensor toolbox to the MATLAB path 27 | cd ../met; addpath(pwd) %<-- [OPTIONAL] Also add the met directory 28 | savepath %<-- Save for future MATLAB sessions 29 | 30 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/cp_wopt.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/cp_wopt.m -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/N_nvecs_doc.m: -------------------------------------------------------------------------------- 1 | %% Generating the leading mode-n vectors 2 | % The leading mode-n vectors are those vectors that span the subspace of 3 | % the mode-n fibers. In other words, the left singular vectors of the 4 | % n-mode matricization of X. 5 | %% Using nvecs to calculate the leading mode-n vectors 6 | % The |nvecs| command efficient computes the leading n-mode vectors. 7 | rand('state',0); 8 | X = sptenrand([4,3,2],6) %<-- A sparse tensor 9 | %% 10 | nvecs(X,1,2) %<-- The 2 leading mode-1 vectors 11 | %% 12 | nvecs(X,1,3) % <-- The 3 leading mode-1 vectors 13 | %% 14 | nvecs(full(X),1,3) %<-- The same thing for a dense tensor 15 | %% 16 | X = ktensor({rand(3,2),rand(3,2),rand(2,2)}) %<-- A random ktensor 17 | %% 18 | nvecs(X,2,1) %<-- The 1 leading mode-2 vector 19 | %% 20 | nvecs(full(X),2,1) %<-- Same thing for a dense tensor 21 | %% 22 | X = ttensor(tenrand([2,2,2,2]),{rand(3,2),rand(3,2),rand(2,2),rand(2,2)}); %<-- A random ttensor 23 | %% 24 | nvecs(X,4,2) %<-- The 1 leading mode-2 vector 25 | %% 26 | nvecs(full(X),4,2) %<-- Same thing for a dense tensor 27 | %% Using nvecs for the HOSVD 28 | X = tenrand([4 3 2]) %<-- Generate data 29 | %% 30 | U1 = nvecs(X,1,4); %<-- Mode 1 31 | U2 = nvecs(X,2,3); %<-- Mode 2 32 | U3 = nvecs(X,3,2); %<-- Mode 3 33 | S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core 34 | Y = ttensor(S,{U1,U2,U3}) %<-- HOSVD of X 35 | %% 36 | norm(full(Y) - X) %<-- Reproduces the same result. 37 | 38 | %% 39 | U1 = nvecs(X,1,2); %<-- Mode 1 40 | U2 = nvecs(X,2,2); %<-- Mode 2 41 | U3 = nvecs(X,3,2); %<-- Mode 3 42 | S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core 43 | Y = ttensor(S,{U1,U2,U3}) %<-- Rank-(2,2,2) HOSVD approximation of X 44 | %% 45 | 100*(1-norm(full(Y)-X)/norm(X)) %<-- Percentage explained by approximation -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/T1_algorithms_doc.m: -------------------------------------------------------------------------------- 1 | %% ALS optimization for CP and Tucker tensor decompositions 2 | 3 | %% Alternating least squares for PARAFAC/CANDECOMP 4 | % The function |parafac_als| computes an estimate of the best rank-R 5 | % PARAFAC model of a tensor X using an alternating least-squares 6 | % algorithm. The input X can be a tensor, sptensor, ktensor, or 7 | % ttensor. The result P is a ktensor. 8 | rand('state',0); 9 | X = sptenrand([5 4 3], 10) 10 | %% 11 | P = parafac_als(X,2) 12 | %% 13 | P = parafac_als(X,2,struct('dimorder',[3 2 1])) 14 | %% 15 | P = parafac_als(X,2,struct('dimorder',[3 2 1],'init','nvecs')) 16 | %% 17 | U0 = {rand(5,2),rand(4,2),[]}; %<-- Initial guess for factors of P 18 | P = parafac_als(X,2,struct('dimorder',[3 2 1],'init',{U0})) 19 | %% Alternating least squares for Tucker model 20 | % The function |tucker_als| computes the best rank(R1,R2,..,Rn) 21 | % approximation of tensor X, according to the specified dimensions in 22 | % vector R. The input X can be a tensor, sptensor, ktensor, or 23 | % ttensor. The result returned in T is a ttensor. 24 | X = sptenrand([5 4 3], 10) 25 | %% 26 | T = tucker_als(X,2) %<-- best rank(2,2,2) approximation 27 | %% 28 | T = tucker_als(X,[2 2 1]) %<-- best rank(2,2,1) approximation 29 | %% 30 | T = tucker_als(X,2,struct('dimorder',[3 2 1])) 31 | %% 32 | T = tucker_als(X,2,struct('dimorder',[3 2 1],'init','eigs')) 33 | %% 34 | U0 = {rand(5,2),rand(4,2),[]}; %<-- Initial guess for factors of T 35 | T = tucker_als(X,2,struct('dimorder',[3 2 1],'init',{U0})) 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/T4_cpapr_doc.m: -------------------------------------------------------------------------------- 1 | %% Alternating Poisson Regression for fitting CP to sparse count data 2 | 3 | %% Set up a sample problem 4 | % We follow the general procedure outlined by E. C. Chi and T. G. Kolda, 5 | % On Tensors, Sparsity, and Nonnegative Factorizations, arXiv:1112.2414 6 | % [math.NA], December 2011 (http://arxiv.org/abs/1112.2414). 7 | 8 | % Pick the size and rank 9 | sz = [100 80 60]; 10 | R = 5; 11 | 12 | % Generate factor matrices with a few large entries in each column; this 13 | % will be the basis of our soln. 14 | A = cell(3,1); 15 | for n = 1:length(sz) 16 | A{n} = rand(sz(n), R); 17 | for r = 1:R 18 | p = randperm(sz(n)); 19 | nbig = round( (1/R)*sz(n) ); 20 | A{n}(p(1:nbig),r) = 100 * A{n}(p(1:nbig),r); 21 | end 22 | end 23 | lambda = rand(R,1); 24 | S = ktensor(lambda, A); 25 | S = normalize(S,'sort',1); 26 | 27 | % Create sparse test problem based on provided solution. 28 | nz = prod(sz) * .05; 29 | info = create_problem('Soln', S, 'Sparse_Generation', nz); 30 | 31 | % Extract data and solution 32 | X = info.Data; 33 | M_true = info.Soln; 34 | 35 | %% Call CP-APR 36 | 37 | % Compute a solution 38 | M = cp_apr(X, R, 'printitn', 10); 39 | 40 | % Score the solution 41 | factor_match_score = score(M, M_true, 'greedy', true) 42 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/V_SSHOPM_doc.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/V_SSHOPM_doc.m -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/C_ttensor_doc_eq15566.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/C_ttensor_doc_eq15566.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq09466.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq09466.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq51104.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq51104.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq64665.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq64665.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq81501.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/D_ktensor_doc_eq81501.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc_01.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/html/V_SSHOPM_doc_02.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/images/Workspace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/images/Workspace.png -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/images/banner-background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/images/banner-background.jpg -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/doc/images/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Electromagnetism-dog-technology/Infrared-Small-Target-Detection-via-Nonconvex-Tensor-Fibered-Rank-Approximation/0d518a57211a8c35fe6475d06b0cd0792234a886/lib/ToolBox/tensor_toolbox/doc/images/logo.gif -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/generate_LowRank_tensor.m: -------------------------------------------------------------------------------- 1 | function [Core,U,T]=generate_LowRank_tensor(Size,Rank) 2 | 3 | Dim=length(Size); 4 | 5 | Core=tensor(rand(Rank)); 6 | for i=1:Dim 7 | U{i}=orth(rand(Size(i),Rank(i))); 8 | end 9 | T=double(ttm(Core,U)); 10 | T=T-min(T(:)); 11 | T=T/max(T(:)); -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/helpindex.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Tensor Toolbox 6 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/info.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | R2006a 7 | Tensor 8 | toolbox 9 | $toolbox/matlab/icons/unknownicon.gif 10 | . 11 | 12 | 13 | 14 | 15 | doc tensor_toolbox/ 16 | $toolbox/matlab/icons/webicon.gif 17 | 18 | 19 | 20 | web http://www.sandia.gov/~tgkolda/TensorToolbox 21 | $toolbox/matlab/icons/webicon.gif 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/met/Contents.m: -------------------------------------------------------------------------------- 1 | % MET: Memory Efficient Tucker 2 | % 3 | % Files 4 | % ttm_me - Memory-efficient sptensor times matrix. 5 | % ttm_me_mem - Estimates intermediate memory comsumption for ttm_me. 6 | % ttm_me_partition - Finds best order for ttm_me. 7 | % tucker_me - Memory-efficient Tucker higher-order orthogonal iteration. 8 | % tucker_me_test - Very simple tests of tucker_me. 9 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/met/ttm_me_partition.m: -------------------------------------------------------------------------------- 1 | function [edims, sdims] = ttm_me_partition(U, esz, n) 2 | %TTM_ME_PARTITION Finds best order for ttm_me. 3 | % 4 | % [EDIMS, SDIMS] = TTM_ME_PARTITION(U, ESZ, N) finds the best order for 5 | % memory efficient ttm. ESZ specifies the number of dimensions that 6 | % are to be handled elementwise. U is the cell array of matrices. 7 | % The result returned in EDIMS and SDIMS are the modes that need to 8 | % be handled in element-wise and in slice-wise respectively. The 9 | % orders are in the descending order of the reduction ratio. 10 | % 11 | % See also TUCKER_ME, TTM_ME. 12 | % 13 | % Based on the paper: 14 | % T. G. Kolda and J. Sun. Scalable Tensor Decompositions for Multi-aspect 15 | % Data Mining. In: ICDM 2008: Proceedings of the 8th IEEE International 16 | % Conference on Data Mining, December 2008. 17 | 18 | % $Id: ttm_me_partition.m,v 1.2 2009/07/08 00:03:52 tgkolda Exp $ 19 | 20 | if nargin<3 21 | %default: n is not in 1:length(U) 22 | n = -1; 23 | end 24 | 25 | % compute reduction ratios 26 | dims = setdiff(1:length(U), n); 27 | r = []; 28 | for i=dims 29 | [nr, nc] = size(U{i}); 30 | r(end+1) = nr/nc; 31 | end 32 | 33 | %sort the reduction ratio and 34 | %put the ones w/ highest reduction ratio in edims 35 | [junk, ind] = sort(r,'descend'); 36 | edims = sort(dims(ind(1:min(length(ind),esz)))); 37 | sdims = sort(dims(ind(esz+1:end))); 38 | end -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/parafac_als.m: -------------------------------------------------------------------------------- 1 | function [P,Uinit] = parafac_als(X,R,opts) 2 | %PARAFAC_ALS Deprecated. Use CP_ALS instead. 3 | % 4 | % See also CP_ALS. 5 | % 6 | %MATLAB Tensor Toolbox. 7 | %Copyright 2012, Sandia Corporation. 8 | 9 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 10 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 11 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 12 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 13 | % work by or on behalf of the U.S. Government. Export of this data may 14 | % require a license from the United States Government. 15 | % The full license terms can be found in the file LICENSE.txt 16 | 17 | 18 | if (nargout == 2) & (nargin == 3) 19 | [P,Uinit] = cp_als(X,R,opts); 20 | elseif (nargout == 2) & (nargin == 2) 21 | [P,Uinit] = cp_als(X,R); 22 | elseif (nargout == 2) & (nargin == 1) 23 | [P,Uinit] = cp_als(X); 24 | elseif (nargout == 1) & (nargin == 3) 25 | P = cp_als(X,R,opts); 26 | elseif (nargout == 1) & (nargin == 2) 27 | P = cp_als(X,R); 28 | elseif (nargout == 1) & (nargin == 1) 29 | P = cp_als(X); 30 | end 31 | 32 | 33 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/sptendiag.m: -------------------------------------------------------------------------------- 1 | function X = sptendiag(v,sz) 2 | %SPTENDIAG Creates a sparse tensor with v on the diagonal. 3 | % 4 | % SPTENDIAG(V) creates a sparse tensor with N dimensions, each of 5 | % size N, where N is the number of elements of V. The elements of V 6 | % are placed on the superdiagonal. 7 | % 8 | % SPTENDIAG(V,SZ) is the same as above but creates a tensor of size 9 | % SZ. If SZ is not big enough, the tensor will be enlarged to 10 | % accommodate the elements of V on the superdiagonal. 11 | % 12 | % Examples 13 | % X = sptendiag([0.1 0.22 0.333]) %<-- creates a 3x3x3 sptensor 14 | % 15 | % See also SPTENSOR, TENDIAG. 16 | % 17 | %MATLAB Tensor Toolbox. 18 | %Copyright 2012, Sandia Corporation. 19 | 20 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 21 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 22 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 23 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 24 | % work by or on behalf of the U.S. Government. Export of this data may 25 | % require a license from the United States Government. 26 | % The full license terms can be found in the file LICENSE.txt 27 | 28 | 29 | % Make sure v is a column vector 30 | v = reshape(v,[numel(v) 1]); 31 | 32 | N = numel(v); 33 | if ~exist('sz','var') 34 | sz = repmat(N,1,N); 35 | end 36 | 37 | X = sptensor(sz); 38 | subs = repmat((1:N)', 1, length(sz)); 39 | X(subs) = v; 40 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tendiag.m: -------------------------------------------------------------------------------- 1 | function X = tendiag(v,sz) 2 | %TENDIAG Creates a tensor with v on the diagonal. 3 | % 4 | % TENDIAG(V) creates a tensor with N dimensions, each of size N, where N 5 | % is the number of elements of V. The elements of V are placed on the 6 | % superdiagonal. 7 | % 8 | % TENDIAG(V,SZ) is the same as above but creates a tensor of size SZ. If 9 | % SZ is not big enough, the tensor will be enlarged to accommodate the 10 | % elements of V on the superdiagonal. 11 | % 12 | % Examples 13 | % X = tendiag([0.1 0.22 0.333]) %<-- creates a 3x3x3 tensor 14 | % 15 | % See also TENSOR, SPTENDIAG. 16 | % 17 | %MATLAB Tensor Toolbox. 18 | %Copyright 2012, Sandia Corporation. 19 | 20 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 21 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 22 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 23 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 24 | % work by or on behalf of the U.S. Government. Export of this data may 25 | % require a license from the United States Government. 26 | % The full license terms can be found in the file LICENSE.txt 27 | 28 | 29 | % Make sure v is a column vector 30 | v = reshape(v,[numel(v) 1]); 31 | 32 | N = numel(v); 33 | if ~exist('sz','var') 34 | sz = repmat(N,1,N); 35 | end 36 | 37 | X = tenzeros(sz); 38 | subs = repmat((1:N)', 1, length(sz)); 39 | X(subs) = v; 40 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/teneye.m: -------------------------------------------------------------------------------- 1 | function A = teneye(M,N) 2 | %TENEYE Create identity tensor of specified size. 3 | % 4 | % We say E is the "identity tensor" if TTSV(E,X,-1) = X for all X such 5 | % that NORM(X) = 1. 6 | % 7 | % TENEYE(M,N) returns a (dense) identity tensor of order M and size N. 8 | % The identity tensor only exists when M is even and returns an error 9 | % otherwise. Due to the complexity of generating all possible indices, 10 | % this only works for relatively small M and N (i.e., N <= 7, M <= 6). 11 | % 12 | % Examples 13 | % E = teneye(4,2); %<-- Create 2 x 2 x 2 x 2 identity tensor 14 | % x = rand(2,1); x = x/norm(x); %<-- Generate random x with norm 1 15 | % norm(ttsv(E,x,-1)-x) %<-- Check that ttsv(E,x,-1) = x 16 | % 17 | % See also tensor, ttsv. 18 | % 19 | %MATLAB Tensor Toolbox. 20 | %Copyright 2012, Sandia Corporation. 21 | 22 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 23 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 24 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 25 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 26 | % work by or on behalf of the U.S. Government. Export of this data may 27 | % require a license from the United States Government. 28 | % The full license terms can be found in the file LICENSE.txt 29 | 30 | %% Check that it's an even tensor 31 | if mod(M,2) ~= 0 32 | error('m must be even'); 33 | end 34 | 35 | %% Generate all possible indices 36 | idx = tt_combinator(N,M,'c','r'); 37 | 38 | %% 39 | A = tenzeros(N*ones(1,M)); 40 | for i = 1:size(idx,1) 41 | p = perms(idx(i,:)); 42 | for j = 1:M/2 43 | s(:,j) = (p(:,2*j-1) == p(:,2*j)); 44 | end 45 | v = sum(sum(s,2)==M/2); 46 | A(p) = v / factorial(M); 47 | end 48 | 49 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tenones.m: -------------------------------------------------------------------------------- 1 | function X = tenones(varargin) 2 | %TENONES Ones tensor. 3 | % 4 | % X = TENONES(SZ) forms a tensor of size SZ with all ones. 5 | % 6 | % TENONES(SZ) is equivalent to TENSOR(ONES(SZ(1),SZ(2),...),SZ). 7 | % 8 | % See also TENSOR, ONES. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if nargin == 1 23 | sz = varargin{1}; 24 | else 25 | sz = cell2mat(varargin); 26 | end 27 | 28 | if isempty(sz) 29 | X = tensor(); 30 | return; 31 | end 32 | 33 | data = ones([sz 1 1]); 34 | X = tensor(data,sz); 35 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tenrand.m: -------------------------------------------------------------------------------- 1 | function X = tenrand(varargin) 2 | %TENRAND Uniformly distributed pseudo-random tensor. 3 | % 4 | % X = TENRAND(SZ) forms a tensor of size SZ with pseudo-random 5 | % values drawn from a uniform distribution on the unit interval. 6 | % 7 | % TENRAND(SZ) is equivalent to TENSOR(RAND(SZ(1),SZ(2),...),SZ). 8 | % 9 | % See also TENSOR, SPTENRAND, RAND. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | 23 | if nargin == 1 24 | sz = varargin{1}; 25 | else 26 | sz = cell2mat(varargin); 27 | end 28 | 29 | data = rand([sz 1 1]); 30 | X = tensor(data,sz); 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tenzeros.m: -------------------------------------------------------------------------------- 1 | function X = tenzeros(varargin) 2 | %TENZEROS Create zeros tensor. 3 | % 4 | % X = TENZEROS(SZ) forms a tensor of size SZ with all zeros. 5 | % 6 | % TENZEROS(SZ) is equivalent to TENSOR(ZEROS(SZ(1),SZ(2),...),SZ). 7 | % 8 | % See also TENSOR, ZEROS. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if nargin == 1 23 | sz = varargin{1}; 24 | else 25 | sz = cell2mat(varargin); 26 | end 27 | 28 | if isempty(sz) 29 | X = tensor; 30 | return; 31 | end 32 | 33 | if nargin == 2 34 | order = sz; 35 | dim = varargin{1}; 36 | sz = dim * ones(1,order); 37 | end 38 | 39 | data = zeros([sz 1 1]); 40 | X = tensor(data,sz); 41 | 42 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tnorm.m: -------------------------------------------------------------------------------- 1 | function n = tnorm(T) 2 | %NORM Frobenius norm of a tensor. 3 | % 4 | % NORM(X) returns the Frobenius norm of a tensor. 5 | % 6 | % See also TENSOR. 7 | % 8 | %MATLAB Tensor Toolbox. 9 | %Copyright 2012, Sandia Corporation. 10 | 11 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 12 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 13 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 14 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 15 | % work by or on behalf of the U.S. Government. Export of this data may 16 | % require a license from the United States Government. 17 | % The full license terms can be found in the file LICENSE.txt 18 | 19 | 20 | v = reshape(T.data, numel(T.data), 1); 21 | n = norm(v); 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_RandOrthMat.m: -------------------------------------------------------------------------------- 1 | function M=tt_RandOrthMat(n, tol) 2 | %TT_RANDORTHMAT Generates random n x n orthogonal real matrix. 3 | % 4 | % M = RANDORTHMAT(n) 5 | % generates a random n x n orthogonal real matrix. 6 | % 7 | % M = RANDORTHMAT(n,tol) 8 | % explicitly specifies a thresh value that measures linear dependence 9 | % of a newly formed column with the existing columns. Defaults to 1e-6. 10 | % 11 | % In this version the generated matrix distribution *is* uniform over the manifold 12 | % O(n) w.r.t. the induced R^(n^2) Lebesgue measure, at a slight computational 13 | % overhead (randn + normalization, as opposed to rand ). 14 | % 15 | % (c) Ofek Shilon , 2006. 16 | % 17 | %This code is *not* copyrighted by Sandia, but it is distributed with: 18 | %MATLAB Tensor Toolbox. 19 | %Copyright 2012, Sandia Corporation. 20 | 21 | 22 | 23 | if nargin==1 24 | tol=1e-6; 25 | end 26 | 27 | M = zeros(n); % prealloc 28 | 29 | % gram-schmidt on random column vectors 30 | 31 | vi = randn(n,1); 32 | % the n-dimensional normal distribution has spherical symmetry, which implies 33 | % that after normalization the drawn vectors would be uniformly distributed on the 34 | % n-dimensional unit sphere. 35 | 36 | M(:,1) = vi ./ norm(vi); 37 | 38 | for i=2:n 39 | nrm = 0; 40 | while nrm= 2) 22 | type = 'subtensor'; 23 | return; 24 | end 25 | 26 | type = 'subscripts'; 27 | 28 | 29 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_ccong.m: -------------------------------------------------------------------------------- 1 | function X = tt_ccong(m,n,gamma) 2 | %TT_CCONG Create a random matrix with a fixed congruence 3 | % 4 | % X = TT_CCONG(M,N,GAMMA) creates a matrix X of size M x N such 5 | % that each column of X has norm 1 and any two columns of X have an inner 6 | % product equal to GAMMA. 7 | % 8 | % Based on code from Evrim Acar and the paper G. Tomasi and R. Bro, A 9 | % comparison of algorithms for fitting the PARAFAC model, Computational 10 | % Statistics & Data Analysis, 50: 1700-1734, 2006. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | CG = gamma * ones(n,n) + (1-gamma) * eye(n); 24 | CGR = chol(CG); 25 | X = randn(m,n); 26 | [Q,~] = qr(X,0); 27 | X = Q * CGR; -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_cp_fun.m: -------------------------------------------------------------------------------- 1 | function [f,g] = tt_cp_fun(x,Z,Znormsqr) 2 | %TT_CP_FUN Calculate function and gradient for CP fit function. 3 | % 4 | % [F,G] = TT_CP_FUN(X,Z) where X is a vector containing the entries of the 5 | % components of the model and Z is the tensor to be fit. 6 | % 7 | % See also TT_CP_VEC_TO_FAC, TT_FAC_TO_VEC, TT_CP_FG, CP_OPT 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %% Convert x to a cell array of matrices 22 | A = tt_cp_vec_to_fac(x,Z); 23 | 24 | %% Call cp_fit and cp_gradient using cp_fg 25 | [f,G] = tt_cp_fg(Z,A,Znormsqr); 26 | 27 | %% Convert a cell array to a vector 28 | g = tt_fac_to_vec(G); 29 | 30 | 31 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_cp_vec_to_fac.m: -------------------------------------------------------------------------------- 1 | function A = cp_vec_to_fac(x,Z) 2 | %CP_VEC_TO_FAC Converts a vector to a cell array of factor matrices. 3 | % 4 | % A = CP_VEC_TO_FAC(X,Z) converts the vector X into a cell array 5 | % of factor matrices consistent with the size of the tensor Z. 6 | % 7 | % See also FAC_TO_VEC, CP_FUN, CP_OPT. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %% Set-up 22 | P = length(x); 23 | N = ndims(Z); 24 | sz = size(Z); 25 | 26 | %% Determine R 27 | R = P / sum(sz); 28 | 29 | %% Create A 30 | A = cell(N,1); 31 | for n = 1:N 32 | idx1 = sum(sz(1:n-1))*R + 1; 33 | idx2 = sum(sz(1:n))*R; 34 | A{n} = reshape(x(idx1:idx2),sz(n),R); 35 | end -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_cp_wfg_sparse_setup.m: -------------------------------------------------------------------------------- 1 | function Zvals = cp_wfg_sparse_setup(Z,W) 2 | %CP_WFG_SPARSE_SETUP Creates a special array. 3 | % 4 | % ZVALS = CP_WFG_SPARSE_SETUP(Z,W) creates an array ZVALS that 5 | % contains the values of Z corresponding to the indices specified 6 | % by W.subs. 7 | % 8 | % See also CP_WFG_SPARSE. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | Zsubs = Z.subs; 23 | Wsubs = W.subs; 24 | Zvals = zeros(size(W.vals)); 25 | [junk,loc] = ismember(Zsubs,Wsubs,'rows'); 26 | Zvals(loc) = Z.vals; 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_fac_to_vec.m: -------------------------------------------------------------------------------- 1 | function x = fac_to_vec(A) 2 | %FAC_TO_VEC Converts a set of factor matrices to a vector. 3 | % 4 | % X = FAC_TO_VEC(A) converts a cell array of factor matrices A to a 5 | % vector by vectorizing each matrix and stacking them. 6 | % 7 | % See also CP_VEC_TO_FAC, CP_FUN, CP_OPT. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | %% Set-up 22 | N = length(A); 23 | 24 | %% Get sizes 25 | sz = zeros(N,1); 26 | for n = 1:N 27 | sz(n) = size(A{n},1); 28 | end 29 | R = size(A{1},2); 30 | P = sum(sz)*R; 31 | 32 | %% Create x 33 | x = zeros(P,1); 34 | for n = 1:N 35 | idx1 = sum(sz(1:n-1))*R + 1; 36 | idx2 = sum(sz(1:n))*R; 37 | x(idx1:idx2) = reshape(A{n},sz(n)*R,1); 38 | end -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_ind2sub.m: -------------------------------------------------------------------------------- 1 | function subs = tt_ind2sub(siz,idx) 2 | %TT_IND2SUB Multiple subscripts from linear indices. 3 | % 4 | % SUBS = TT_IND2SUB(SIZ,INDS) returns that subscripts equivalent 5 | % to the linear indices in INDS for a tensor of size SIZ. 6 | % 7 | % See also TT_SUB2IND, IND2SUB. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if isempty(idx) 22 | subs = []; 23 | return; 24 | end 25 | 26 | k = [1 cumprod(siz(1:end-1))]; 27 | n = length(siz); 28 | idx = idx - 1; 29 | for i = n : -1 : 1 30 | subs(:,i) = floor(idx / k(i)) + 1; 31 | idx = rem(idx,k(i)); 32 | end 33 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_intvec2str.m: -------------------------------------------------------------------------------- 1 | function s = tt_intvec2str(v) 2 | %TT_VEC2STR Print integer vector to a string with brackets. 3 | % 4 | %MATLAB Tensor Toolbox. 5 | %Copyright 2012, Sandia Corporation. 6 | 7 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 8 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 9 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 10 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 11 | % work by or on behalf of the U.S. Government. Export of this data may 12 | % require a license from the United States Government. 13 | % The full license terms can be found in the file LICENSE.txt 14 | 15 | 16 | if isempty(v) 17 | s = sprintf('[]'); 18 | return; 19 | end 20 | 21 | s = ['[ ' sprintf('%d ',v(1:end)) ']']; 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_loglikelihood.m: -------------------------------------------------------------------------------- 1 | function f = tt_loglikelihood(X,M) 2 | %TT_LOGLIKELIHOOD Compute log-likelihood of data X with model M. 3 | % 4 | % F = TT_LOGLIKELIHOOD(X,M) computes the log-likelihood of model M given 5 | % data X, where M is a ktensor and X is a tensor or sptensor. 6 | % Specifically, F = - (sum_i m_i - x_i * log_i) where i is a multiindex 7 | % across all tensor dimensions. 8 | % 9 | % See also cp_apr, tensor, sptensor, ktensor. 10 | % 11 | %MATLAB Tensor Toolbox. 12 | %Copyright 2012, Sandia Corporation. 13 | 14 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 15 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 16 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 17 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 18 | % work by or on behalf of the U.S. Government. Export of this data may 19 | % require a license from the United States Government. 20 | % The full license terms can be found in the file LICENSE.txt 21 | 22 | N = ndims(X); 23 | 24 | if ~isa(M, 'ktensor') 25 | error('M must be a ktensor'); 26 | end 27 | 28 | M = normalize(M,1,1); 29 | 30 | if isa(X, 'sptensor') 31 | xsubs = X.subs; 32 | A = M.U{1}(xsubs(:,1),:); 33 | for n = 2:N 34 | A = A .* M.U{n}(xsubs(:,n),:); 35 | end 36 | f = sum(X.vals .* log(sum(A,2))) - sum(sum(M.U{1})); 37 | else 38 | f = sum(sum(double(tenmat(X,1)) .* log(double(tenmat(M,1))))) - sum(sum(M.U{1})); 39 | end 40 | 41 | 42 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_matrix2cellstr.m: -------------------------------------------------------------------------------- 1 | function S = tt_matrix2cellstr(M) 2 | %TT_MATRIX2CELLSTR Convert a matrix to a cell array of strings. 3 | % 4 | %MATLAB Tensor Toolbox. 5 | %Copyright 2012, Sandia Corporation. 6 | 7 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 8 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 9 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 10 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 11 | % work by or on behalf of the U.S. Government. Export of this data may 12 | % require a license from the United States Government. 13 | % The full license terms can be found in the file LICENSE.txt 14 | 15 | 16 | fmt = get(0,'FormatSpacing'); 17 | format compact 18 | S = evalc('disp(M)'); 19 | set(0,'FormatSpacing',fmt) 20 | S = textscan(S,'%s','delimiter','\n','whitespace',''); 21 | S = S{1}; 22 | end 23 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_size2str.m: -------------------------------------------------------------------------------- 1 | function s = tt_size2str(sz) 2 | %TT_SIZE2STR Convert size to a string that can be printed. 3 | % 4 | %MATLAB Tensor Toolbox. 5 | %Copyright 2012, Sandia Corporation. 6 | 7 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 8 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 9 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 10 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 11 | % work by or on behalf of the U.S. Government. Export of this data may 12 | % require a license from the United States Government. 13 | % The full license terms can be found in the file LICENSE.txt 14 | 15 | 16 | if isempty(sz) 17 | s = sprintf('[empty tensor]'); 18 | return; 19 | end 20 | 21 | if numel(sz) == 1 22 | s = sprintf('%d',sz); 23 | else 24 | s = [sprintf('%d x ',sz(1:end-1)) sprintf('%d', sz(end)) ]; 25 | end 26 | 27 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_sizecheck.m: -------------------------------------------------------------------------------- 1 | function ok = tt_sizecheck(siz) 2 | %TT_SIZECHECK Checks that the size is valid. 3 | % 4 | % TT_SIZECHECK(S) throws an error if S is not a valid size array, 5 | % which means that it is a row vector with strictly postitive, 6 | % real-valued, finite integer values. 7 | % 8 | % X = TT_SIZECHECK(S) returns true if S is a valid and false otherwise. 9 | % 10 | % See also TT_SUBSCHECK. 11 | % 12 | %MATLAB Tensor Toolbox. 13 | %Copyright 2012, Sandia Corporation. 14 | 15 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 16 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 17 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 18 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 19 | % work by or on behalf of the U.S. Government. Export of this data may 20 | % require a license from the United States Government. 21 | % The full license terms can be found in the file LICENSE.txt 22 | 23 | 24 | if ndims(siz) == 2 && size(siz,1) == 1 ... 25 | && isreal(siz) ... 26 | && ~any(isnan(siz(:))) && ~any(isinf(siz(:))) ... 27 | && isequal(siz,round(siz)) && all(siz(:) > 0) 28 | ok = true; 29 | else 30 | ok = false; 31 | end 32 | 33 | if ~ok && nargout == 0 34 | error('Size must be a row vector of real positive integers'); 35 | end 36 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_sub2ind.m: -------------------------------------------------------------------------------- 1 | function idx = tt_sub2ind(siz,subs) 2 | %TT_SUB2IND Converts multidimensional subscripts to linear indices. 3 | % 4 | % INDS = TT_SUB2IND(SIZ,SUBS) returns the linear indices 5 | % equivalent to the subscripts in the array SUBS for a tensor of 6 | % size SIZ. 7 | % 8 | % See also TT_IND2SUB, SUB2IND. 9 | % 10 | %MATLAB Tensor Toolbox. 11 | %Copyright 2012, Sandia Corporation. 12 | 13 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 14 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 15 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 16 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 17 | % work by or on behalf of the U.S. Government. Export of this data may 18 | % require a license from the United States Government. 19 | % The full license terms can be found in the file LICENSE.txt 20 | 21 | 22 | if isempty(subs) 23 | idx = []; 24 | return; 25 | end 26 | 27 | mult = [1 cumprod(siz(1:end-1))]; 28 | idx = (subs - 1) * mult' + 1; 29 | 30 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_subscheck.m: -------------------------------------------------------------------------------- 1 | function ok = tt_subscheck(subs) 2 | %TT_SUBSCHECK Checks for valid subscripts. 3 | % 4 | % TT_SUBSCHECK(S) throws an error if S is not a valid subscript 5 | % array, which means that S is a matrix of real-valued, finite, 6 | % positive, integer subscripts. 7 | % 8 | % X = TT_SUBSCHECK(S) returns true if S is a valid and false 9 | % otherwise. 10 | % 11 | % See also TT_SIZECHECK, TT_VALSCHECK. 12 | % 13 | %MATLAB Tensor Toolbox. 14 | %Copyright 2012, Sandia Corporation. 15 | 16 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 17 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 18 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 19 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 20 | % work by or on behalf of the U.S. Government. Export of this data may 21 | % require a license from the United States Government. 22 | % The full license terms can be found in the file LICENSE.txt 23 | 24 | % 25 | % Includes improvements offered by Marcus Brubaker. 26 | 27 | if isempty(subs) 28 | ok = true; 29 | elseif ndims(subs) == 2 && isreal(subs) ... 30 | && all(isfinite(subs(:)) & subs(:) > 0) ... 31 | && isequal(subs,round(subs)) 32 | ok = true; 33 | else 34 | ok = false; 35 | end 36 | 37 | if ~ok && nargout == 0 38 | error('Subscripts must be a matrix of real positive integers'); 39 | end 40 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_subsubsref.m: -------------------------------------------------------------------------------- 1 | function a = tt_subsubsref(obj,s) 2 | %TT_SUBSUBSREF Helper function for tensor toolbox subsref. 3 | % 4 | %MATLAB Tensor Toolbox. 5 | %Copyright 2012, Sandia Corporation. 6 | 7 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 8 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 9 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 10 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 11 | % work by or on behalf of the U.S. Government. Export of this data may 12 | % require a license from the United States Government. 13 | % The full license terms can be found in the file LICENSE.txt 14 | 15 | 16 | if length(s) == 1 17 | a = obj; 18 | else 19 | a = subsref(obj, s(2:end)); 20 | end 21 | 22 | -------------------------------------------------------------------------------- /lib/ToolBox/tensor_toolbox/tt_valscheck.m: -------------------------------------------------------------------------------- 1 | function ok = tt_valscheck(vals) 2 | %TT_VALSCHECK Checks for valid values. 3 | % 4 | % TT_VALSCHECK(S) throws an error if S is not a valid values 5 | % array, which means that S is a column array. 6 | % 7 | % X = TT_VALSCHECK(S) returns true if S is a valid and false otherwise. 8 | % 9 | %MATLAB Tensor Toolbox. 10 | %Copyright 2012, Sandia Corporation. 11 | 12 | % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. 13 | % http://www.sandia.gov/~tgkolda/TensorToolbox. 14 | % Copyright (2012) Sandia Corporation. Under the terms of Contract 15 | % DE-AC04-94AL85000, there is a non-exclusive license for use of this 16 | % work by or on behalf of the U.S. Government. Export of this data may 17 | % require a license from the United States Government. 18 | % The full license terms can be found in the file LICENSE.txt 19 | 20 | 21 | if isempty(vals) 22 | ok = true; 23 | elseif ndims(vals) == 2 && size(vals,2) == 1 24 | ok = true; 25 | else 26 | ok = false; 27 | end 28 | 29 | if ~ok && nargout == 0 30 | error('Values must be a column array'); 31 | end 32 | -------------------------------------------------------------------------------- /lib/ToolBox/tprod.m: -------------------------------------------------------------------------------- 1 | function C = tprod(A,B) 2 | 3 | % Tensor-Tensor product of two 3-way tensor: C = A*B 4 | % A - n1*n2*n3 tensor 5 | % B - n2*l*n3 tensor 6 | % C - n1*l*n3 tensor 7 | % 8 | % version 1.0 - 18/06/2016 9 | % 10 | % Written by Canyi Lu (canyilu@gmail.com) 11 | % 12 | 13 | [n1,~,n3] = size(A); 14 | l = size(B,2); 15 | A = fft(A,[],3); 16 | B = fft(B,[],3); 17 | C = zeros(n1,l,n3); 18 | for i = 1 : n3 19 | C(:,:,i) = A(:,:,i)*B(:,:,i); 20 | end 21 | C = real(ifft(C,[],3)); 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /lib/ToolBox/tran.m: -------------------------------------------------------------------------------- 1 | function Xt = tran(X) 2 | 3 | % conjugate transpose of a 3 way tensor 4 | % X - n1*n2*n3 tensor 5 | % Xt - n2*n1*n3 tensor 6 | % 7 | % version 1.0 - 18/06/2016 8 | % 9 | % Written by Canyi Lu (canyilu@gmail.com) 10 | % 11 | 12 | [n1,n2,n3] = size(X); 13 | Xt = zeros(n2,n1,n3); 14 | Xt(:,:,1) = X(:,:,1)'; 15 | if n3 > 1 16 | for i = 2 : n3 17 | Xt(:,:,i) = X(:,:,n3-i+2)'; 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/ToolBox/tsvd.m: -------------------------------------------------------------------------------- 1 | function [U,S,V,trank,tnn] = tsvd(Y) 2 | 3 | % T-SVD of a 3-way tensor, Y=U*S*V^* 4 | % 5 | % Y - n1*n2*n3 tensor 6 | % U - n1*r*n3 tensor 7 | % S - r*r*n3 tensor 8 | % V - n2*r*n3 tensor 9 | % trank - tubal rank of Y 10 | % tnn - tensor nuclear norm 11 | % 12 | % version 1.0 - 18/06/2016 13 | % 14 | % Written by Canyi Lu (canyilu@gmail.com) 15 | % 16 | 17 | [n1,n2,n3] = size(Y); 18 | n12 = min(n1,n2); 19 | Y = fft(Y,[],3); 20 | U = zeros(n1,n12,n3); 21 | V = zeros(n2,n12,n3); 22 | S = zeros(n12,n12,n3); 23 | trank = 0; 24 | for i = 1 : n3 25 | [U(:,:,i),s,V(:,:,i)] = svd(Y(:,:,i),'econ'); 26 | s = diag(s); 27 | S(:,:,i) = diag(s); 28 | tranki = length(find(s~=0)); 29 | trank = max(tranki,trank); 30 | end 31 | U = U(:,1:trank,:); 32 | V = V(:,1:trank,:); 33 | S = S(1:trank,1:trank,:); 34 | 35 | U = ifft(U,[],3); 36 | S = ifft(S,[],3); 37 | V = ifft(V,[],3); 38 | 39 | tnn = sum(sum(S(:,:,1))); 40 | 41 | -------------------------------------------------------------------------------- /lib/Unfold.m: -------------------------------------------------------------------------------- 1 | function [X] = Unfold( X, dim, i ) 2 | X = reshape(shiftdim(X,i-1), dim(i), []); -------------------------------------------------------------------------------- /lib/gen_patch_ten.m: -------------------------------------------------------------------------------- 1 | function patchTen = gen_patch_ten(img, patchSize, slideStep) 2 | 3 | % 2017-07-31 4 | % This matlab code implements the RIPT model for infrared target-background 5 | % separation. 6 | % 7 | % Yimian Dai. Questions? yimian.dai@gmail.com 8 | % Copyright: College of Electronic and Information Engineering, 9 | % Nanjing University of Aeronautics and Astronautics 10 | 11 | 12 | if ~exist('patchSize', 'var') 13 | patchSize = 50; 14 | end 15 | 16 | if ~exist('slideStep', 'var') 17 | slideStep = 10; 18 | end 19 | 20 | % img = reshape(1:9, [3 3]) 21 | % img = reshape(1:12, [3 4]) 22 | % patchSize = 2; 23 | % slideStep = 1; 24 | [imgHei, imgWid] = size(img); 25 | 26 | rowPatchNum = ceil((imgHei - patchSize) / slideStep) + 1; 27 | colPatchNum = ceil((imgWid - patchSize) / slideStep) + 1; 28 | rowPosArr = [1 : slideStep : (rowPatchNum - 1) * slideStep, imgHei - patchSize + 1]; 29 | colPosArr = [1 : slideStep : (colPatchNum - 1) * slideStep, imgWid - patchSize + 1]; 30 | 31 | %% arrayfun version, identical to the following for-loop version 32 | [meshCols, meshRows] = meshgrid(colPosArr, rowPosArr); 33 | idx_fun = @(row,col) img(row : row + patchSize - 1, col : col + patchSize - 1); 34 | patchCell = arrayfun(idx_fun, meshRows, meshCols, 'UniformOutput', false); 35 | patchTen = cat(3, patchCell{:}); 36 | 37 | %% for-loop version 38 | % patchTen = zeros(patchSize, patchSize, rowPatchNum * colPatchNum); 39 | % k = 0; 40 | % for col = colPosArr 41 | % for row = rowPosArr 42 | % k = k + 1; 43 | % tmp_patch = img(row : row + patchSize - 1, col : col + patchSize - 1); 44 | % patchTen(:, :, k) = tmp_patch; 45 | % end 46 | % end 47 | 48 | -------------------------------------------------------------------------------- /lib/my_normalized.m: -------------------------------------------------------------------------------- 1 | function X=my_normalized(X) 2 | Nway = size(X); 3 | for i=1:Nway(3) 4 | X(:,:,i) = X(:,:,i)/max(max(X(:,:,i))); 5 | end 6 | end 7 | 8 | 9 | % Nway = size(X); 10 | % for i=1:Nway(3) 11 | % X(:,:,i) = (X(:,:,i)-min(min(X(:,:,i))))/(max(max(X(:,:,i)))-min(min(X(:,:,i)))); 12 | % end 13 | % end -------------------------------------------------------------------------------- /lib/prox_l1.m: -------------------------------------------------------------------------------- 1 | function x = prox_l1(b,lambda) 2 | 3 | x = max(0,b-lambda)+min(0,b+lambda); 4 | x = max(x,0); 5 | 6 | -------------------------------------------------------------------------------- /lib/prox_z.m: -------------------------------------------------------------------------------- 1 | % clc; 2 | % clear; 3 | % close all; 4 | % Z=imread('C:/Users/Administrator/Pictures/Camera Roll/QQ图片20200914162324.jpg'); 5 | function [X] = prox_z(Z,Y1,Y2,Y3,Y4,L,tho,dV1,dV2,dV3) 6 | 7 | sizeD= size(Z); 8 | tenX = reshape(Z, sizeD); 9 | h = sizeD(1); 10 | w = sizeD(2); 11 | d = sizeD(3); 12 | %% 13 | Eny_x = ( abs(psf2otf([+1; -1], [h,w,d])) ).^2 ; 14 | Eny_y = ( abs(psf2otf([+1, -1], [h,d,w])) ).^2 ; 15 | Eny_z = ( abs(psf2otf([+1, -1], [w,d,h])) ).^2 ; 16 | Eny_y = permute(Eny_y, [1 3 2]); 17 | Eny_z = permute(Eny_z, [3 1 2]); 18 | determ = Eny_x + Eny_y + Eny_z; 19 | 20 | % v1=(psf2otf([+1; -1], [h,w,d]))'.*(dV1+Y2/tho); 21 | % v2=(permute(psf2otf([+1; -1], [h,d,w]),[1 3 2]))'.*(dV2+Y3/tho); 22 | % v3=(permute(psf2otf([+1; -1], [w,d,h]),[3 1 2]))'.*(dV3+Y4/tho); 23 | 24 | dfx1=diff(dV1+Y2/tho, 1, 1); 25 | dfy1=diff(dV2+Y3/tho, 1, 2); 26 | dfz1=diff(dV3+Y4/tho, 1, 3); 27 | 28 | v1 = zeros(sizeD); 29 | v2 = zeros(sizeD); 30 | v3 = zeros(sizeD); 31 | v1(1:end-1,:,:) = dfx1; 32 | v1(end,:,:) = tenX(1,:,:) - tenX(end,:,:); 33 | v2(:,1:end-1,:) = dfy1; 34 | v2(:,end,:) = tenX(:,1,:) - tenX(:,end,:); 35 | v3(:,:,1:end-1) = dfz1; 36 | v3(:,:,end) = tenX(:,:,1) - tenX(:,:,end); 37 | 38 | numer1=L-Y1/tho+v1+v2+v3; 39 | z = real( ifftn( fftn(numer1) ./ (determ + 1) ) ); 40 | X = reshape(z,sizeD); 41 | 42 | -------------------------------------------------------------------------------- /lib/res_patch_ten_mean.m: -------------------------------------------------------------------------------- 1 | function recImg = res_patch_ten_mean(patchTen, img, patchSize, slideStep) 2 | 3 | % 2017-07-31 4 | % This matlab code implements the RIPT model for infrared target-background 5 | % separation. 6 | % 7 | % Yimian Dai. Questions? yimian.dai@gmail.com 8 | % Copyright: College of Electronic and Information Engineering, 9 | % Nanjing University of Aeronautics and Astronautics 10 | 11 | [imgHei, imgWid] = size(img); 12 | 13 | rowPatchNum = ceil((imgHei - patchSize) / slideStep) + 1; 14 | colPatchNum = ceil((imgWid - patchSize) / slideStep) + 1; 15 | rowPosArr = [1 : slideStep : (rowPatchNum - 1) * slideStep, imgHei - patchSize + 1]; 16 | colPosArr = [1 : slideStep : (colPatchNum - 1) * slideStep, imgWid - patchSize + 1]; 17 | 18 | %% for-loop version 19 | accImg = zeros(imgHei, imgWid); 20 | weiImg = zeros(imgHei, imgWid); 21 | k = 0; 22 | onesMat = ones(patchSize, patchSize); 23 | for col = colPosArr 24 | for row = rowPosArr 25 | k = k + 1; 26 | tmpPatch = reshape(patchTen(:, :, k), [patchSize, patchSize]); 27 | accImg(row : row + patchSize - 1, col : col + patchSize - 1) = tmpPatch; 28 | weiImg(row : row + patchSize - 1, col : col + patchSize - 1) = onesMat; 29 | end 30 | end 31 | 32 | recImg = accImg ./ weiImg; 33 | -------------------------------------------------------------------------------- /lib/structure_tensor_lambda.m: -------------------------------------------------------------------------------- 1 | function [lambda_1, lambda_2] = structure_tensor_lambda(img, sz) 2 | 3 | G = fspecial('gaussian', [sz sz], 2); % Gaussian kernel 4 | u = imfilter(img, G, 'symmetric'); 5 | [Gx, Gy] = gradient(u); 6 | 7 | K = fspecial('gaussian', [sz sz], 9); % Gaussian kernel 8 | J_11 = imfilter(Gx.^2, K, 'symmetric'); 9 | J_12 = imfilter(Gx.*Gy, K, 'symmetric'); 10 | J_21 = J_12; 11 | J_22 = imfilter(Gy.^2, K, 'symmetric'); 12 | 13 | sqrt_delta = sqrt((J_11 - J_22).^2 + 4*J_12.^2); 14 | lambda_1 = 0.5*(J_11 + J_22 + sqrt_delta); 15 | lambda_2 = 0.5*(J_11 + J_22 - sqrt_delta); 16 | --------------------------------------------------------------------------------