% Input:
% L: Matrix (row-wise direction vectors)
% Ad: Matrix
% Bd: Matrix
% U: function handle (Support function of input set)
% X0: function handle (Support function of initial set)
% N: Integer (number of sets to generate)

% Output:
% results: Matrix (containing the evaluation in directions L for each set)

% Description:
% Computes the evaluations of the flowpipe in directions L for
% algorithm ConstU

function [results] = evaluateConstU(L,Ad,Bd,U,X0,N)
results = zeros(size(L,1),N);

% Algorithm for the direct evaluation of the reachability analysis
parfor j=1:size(L,1)
        
   l = L(j,:)';
   Amult = Ad';
   S = 0;
   V = @(l) U(Bd'*l);
   v =  V(l);

   results(j,:) = parallelization_helper(N,results(j,:),v,V,Amult,Ad,S,X0,l);
end;

% Description:
% Method outsourcing come computations from evaluateConstU to make
% parallelization possible
function [res] = parallelization_helper(N,res,v,V,Amult,Ad,S,X0,l)
res(1) = X0(l);
for i = 2:1:N
    S = S + v;
    res(i) = X0(Amult * l) + S;
            
    if i ~= N
       Amult = Ad' *Amult;
       v = V(Amult*l);
    end;
end;