Home > source > @penlab > Y2vec.m

# Y2vec

## PURPOSE

Copy matrix data (with respect to the given pattern) to xall,

## SYNOPSIS

function vec = Y2vec(obj, Y, Ydefault)

## DESCRIPTION

     Copy matrix data (with respect to the given pattern) to xall,
     only data from the lower triangle and on the pattern will be used (the rest
     is ignored). Quite slow but used only during initialization (e.g., Yinit).

     Be careful that here Y can be just part of full length obj.Y,
     imagine obj.Yinit{2}=...; then Y{1}=[], Y{2}=... and there is no Y{k>=3} !
     Ydefault specifies what should be the default value if Y{k} is not present
     on input or it is an empty matrix, possibilities:
       -/+Inf/0 ... vector of all -/+Inf/0
       anything else (or not present) ... the current point
     Y{k} is empty or not present at all --> default value
          is of the same size as internal Y --> only lower triangle elements
          matching the pattern the internal Y will be considered
          is a scalar --> this value will be distributed to all Y in the pattern
          (eg. acts as if you put Y{k}=scalar*ones(size(Ymap{k}))

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- penlab PenLab (Pennon Laboratory for Matlab, previously PennonM)

## SOURCE CODE

    0001 % Copy matrix data (with respect to the given pattern) to xall,
    0002 % only data from the lower triangle and on the pattern will be used (the rest
    0003 % is ignored). Quite slow but used only during initialization (e.g., Yinit).
    0004 %
    0005 % Be careful that here Y can be just part of full length obj.Y,
    0006 % imagine obj.Yinit{2}=...; then Y{1}=[], Y{2}=... and there is no Y{k>=3} !
    0007 % Ydefault specifies what should be the default value if Y{k} is not present
    0008 % on input or it is an empty matrix, possibilities:
    0009 %   -/+Inf/0 ... vector of all -/+Inf/0
    0010 %   anything else (or not present) ... the current point
    0011 % Y{k} is empty or not present at all --> default value
    0012 %      is of the same size as internal Y --> only lower triangle elements
    0013 %      matching the pattern the internal Y will be considered
    0014 %      is a scalar --> this value will be distributed to all Y in the pattern
    0015 %      (eg. acts as if you put Y{k}=scalar*ones(size(Ymap{k}))
    0016 function vec = Y2vec(obj, Y, Ydefault)
    0017
    0018   % use a point to fill in the gabs if there are in Y
    0019   % if Y is a full size array, it won't influence it anyway
    0020   % important only for Y being partially assigned
    0021   if (nargin<=2)
    0022     % current point
    0023     vec=obj.xall(obj.Nx+1:end);
    0024   else
    0025     if (Ydefault==-Inf)
    0026       vec=-Inf(obj.NYnnz,1);
    0027     elseif (Ydefault==Inf)
    0028       vec=Inf(obj.NYnnz,1);
    0029     elseif (Ydefault==0)
    0030       vec=zeros(obj.NYnnz,1);
    0031     else
    0032       vec=obj.xall(obj.Nx+1:end);
    0033     end
    0034   end
    0035
    0036   if (length(Y)>obj.NY)
    0037     error('Incompatible Y on input, cannot transfer data');
    0038   end
    0039
    0040   for k=1:length(Y)
    0041     if (~isempty(Y{k}))
    0042       mapper=obj.vec2Ymap{k};
    0043
    0044       [dim, dim2] = size(Y{k});
    0045       if (dim==1 && dim2==1)
    0046         % scalar --> apply this bound on all elements
    0047         vec(mapper.xmap)=Y{k};
    0048
    0049       elseif (dim==dim2 && dim==mapper.dim)
    0050         % matching dimension, get the appropriate elements
    0051
    0052         if (mapper.dense)
    0053           % mapper.xmap includes double assignments (from both triangles)
    0054           % to be sure which half of the user's data gets used, symmetrize
    0055           % and use lower only
    0056
    0057           Ytmp=tril(Y{k}) + tril(Y{k},-1)';
    0058           vec(mapper.xmap)=reshape(full(Ytmp),dim*dim,1);
    0059         else
    0060           len=length(mapper.xmap);
    0061           % have to do it element by element because MATRIX(irow,icol) will
    0062           % be a submatrix not an array of arguments. Find is also not suitable
    0063           % because it won't register zeros (which are on the pattern of variables)
    0064           for idx=1:len
    0065             ridx=mapper.irow(idx);
    0066             cidx=mapper.icol(idx);
    0067             if (ridx>=cidx)
    0068               % lower triangle only
    0069               vec(mapper.xmap(idx))=Y{k}(ridx,cidx);
    0070             end
    0071           end
    0072         end
    0073       else
    0074         % not matching dimension and not a scalar (dim~=dim2 || dim~=mapper.dim)
    0075         error('Y{k} has different size.');
    0076       end
    0077     end
    0078   end
    0079
    0080
    0081