function [FacMat lambda fixed] = SamBaTen(X,F,sample_factor,repitions,nonneg)
%Ekta Gujral,Ravdeep Pasricha , Vagelis Papalexakis 2017
%Computer Science and Engineering, University of California, Riverside
%Demonstrating the use of the SambaTen algorithm, using samplaing based
%approach for online tensors
%Input :  X= 3-mode tesor of dimension I x J x K
% F= rank of tensor or number of independent clusters
% sample_factor= Sampling factor for each mode. 
% repitions= Number of repetitions  
% Output: FactMat: factor matrix for tensor
% lamda and fixed indices of previous data.
if nargin == 5
    nonneg = 0;
end
maxRank=F;
p = 1;
s = size(X); I = s(1); J = s(2); K = s(3);
A = sparse(I,F); B = sparse(J,F);C = sparse(K,F);

for i = 1:repitions
   if i == 1
        [Xs idx_i idx_j idx_k] = SamBaTen_initilize(X,sample_factor);
        fixed_i = idx_i(1:ceil(p*length(idx_i)));
        fixed_j = idx_j(1:ceil(p*length(idx_j)));
        fixed_k = idx_k(1:ceil(p*length(idx_k)));  % again
        fixed{1} = fixed_i; fixed{2} = fixed_j; fixed{3}=fixed_k;
else 
        [Xs idx_i idx_j idx_k] = SamBaTen_initilize(X,sample_factor,fixed);
end
    initOpt.printitn = 0;    
    initOpt.maxiters = 100;
    if nnz(Xs)>0
        if nonneg
           factors = cp_nmu(Xs,F,initOpt);
        else
           factors = cp_als(Xs,F,initOpt);
       
        end
        As=sparse(factors.U{1});Bs=sparse(factors.U{2});Cs=sparse(factors.U{3});%lambda = factors.lambda;As = As*diag(lambda);

        lambda(:,i) = factors.lambda;

        As = As*diag(lambda(:,i).^(1/3)); Bs = Bs*diag(lambda(:,i).^(1/3)); Cs = Cs*diag(lambda(:,i).^(1/3));  
    else
       As = 0; Bs = 0; Cs = 0; 
    end
    
    
    Atemp = sparse(I,F); Btemp = sparse(J,F);Ctemp = sparse(K,F);
    Atemp(idx_i,:) = As; Btemp(idx_j,:) = Bs; Ctemp(idx_k,:) = Cs;
     %now, normalize the common part of every column
    for f = 1:F
       norm_a = norm(Atemp(fixed_i,f),2);Atemp(:,f) = Atemp(:,f)/norm_a; lambda(f,i) = norm_a;
       norm_b = norm(Btemp(fixed_j,f),2);Btemp(:,f) = Btemp(:,f)/norm_b; lambda(f,i) = lambda(f,i)*norm_b;
       norm_c = norm(Ctemp(fixed_k,f),2);Ctemp(:,f) = Ctemp(:,f)/norm_c; lambda(f,i) = lambda(f,i)*norm_c;
    end
    fixed_val=cell(3,1);
    fixed_val{1}=fixed_i;
    fixed_val{2}=fixed_j;
    fixed_val{3}=fixed_k;
    
    %Do the merge
    Aold{1}=Atemp; Bold{1}=Btemp; Cold{1}=Ctemp; allCorrespondences = zeros(F,1);
    if i ==1
        
         
         [A,B,C]=domergeTurbo(Aold,Bold,Cold,i,F,fixed_val);
         Aold{2}=A; Bold{2}=B; Cold{2}=C;
      %  A = Atemp; B = Btemp; C = Ctemp;
    else
        
%          A = match_and_permute(Aold{1},Aold{2},fixed_val{1});
%          B = match_and_permute(Bold{1},Bold{2},fixed_val{2});
%          C = match_and_permute(Cold{1},Cold{2},fixed_val{3});
%         
        
        [A,B,C]=domergeTurbo(Aold,Bold,Cold,i,F,fixed_val);
         Aold{2}=A; Bold{2}=B; Cold{2}=C;
    end
end
 lambda  =    mean(lambda,2);

%A = sparse(A*diag(lambda));
A(A<10^-4) = 0;
B(B<10^-4) = 0;
C(C<10^-4) = 0;
FacMat{1}=A;FacMat{2}=B;FacMat{3}=C;
end
