Home > matpower7.1 > mp-opt-model > lib > @opt_model > varsets_x.m

varsets_x

PURPOSE ^

VARSETS_X Returns a cell array of sub-vectors of X specified by VARSETS

SYNOPSIS ^

function xx = varsets_x(om, x, vs, want_vector)

DESCRIPTION ^

VARSETS_X  Returns a cell array of sub-vectors of X specified by VARSETS
   X = OM.VARSETS_X(X, VARSETS)
   X = OM.VARSETS_X(X, VARSETS, 'vector')

   Returns a cell array of sub-vectors of X specified by VARSETS, or
   the full optimization vector X, if VARSETS is empty.

   If a 3rd argument is present (value is ignored) the returned value is
   a single numeric vector with the individual components stacked vertically.

   See also VARSET_LEN

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function xx = varsets_x(om, x, vs, want_vector)
0002 %VARSETS_X  Returns a cell array of sub-vectors of X specified by VARSETS
0003 %   X = OM.VARSETS_X(X, VARSETS)
0004 %   X = OM.VARSETS_X(X, VARSETS, 'vector')
0005 %
0006 %   Returns a cell array of sub-vectors of X specified by VARSETS, or
0007 %   the full optimization vector X, if VARSETS is empty.
0008 %
0009 %   If a 3rd argument is present (value is ignored) the returned value is
0010 %   a single numeric vector with the individual components stacked vertically.
0011 %
0012 %   See also VARSET_LEN
0013 
0014 %   MP-Opt-Model
0015 %   Copyright (c) 2017-2020, Power Systems Engineering Research Center (PSERC)
0016 %   by Ray Zimmerman, PSERC Cornell
0017 %
0018 %   This file is part of MP-Opt-Model.
0019 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0020 %   See https://github.com/MATPOWER/mp-opt-model for more info.
0021 
0022 
0023 persistent sn;
0024 if isempty(vs)          %% all rows of x
0025     xx = x;
0026 else                    %% selected rows of x
0027     vsN = length(vs);
0028     xx = cell(1, vsN);
0029 
0030     %% calls to substruct() are relatively expensive, so we pre-build the
0031     %% struct for addressing numeric array fields, updating only
0032     %% the subscripts before use
0033     if isempty(sn)
0034         sn = struct('type', {'.', '()'}, 'subs', {'', 1});
0035     end
0036 
0037     ki = 0;
0038     for v = 1:length(vs)
0039         vname = vs(v).name;
0040         vidx = vs(v).idx;
0041         if isempty(vidx)
0042             i1 = om.var.idx.i1.(vname);         %% starting row in full x
0043             iN = om.var.idx.iN.(vname);         %% ending row in full x
0044         else
0045             % (calls to substruct() are relatively expensive ...
0046             % sn = substruct('.', vname, '()', vidx);
0047             % ... so replace it with these more efficient lines)
0048             sn(1).subs = vname;
0049             sn(2).subs = vidx;
0050             i1 = subsref(om.var.idx.i1, sn);    %% starting row in full x
0051             iN = subsref(om.var.idx.iN, sn);    %% ending row in full x
0052         end
0053         if isscalar(i1)             %% simple named set, or indexed named set
0054             ki = ki + 1;
0055             xx{ki} = x(i1:iN);              %% single set of indices for varset
0056         else                        %% multi-dim named set w/no index specified
0057             ii1 = permute(i1, ndims(i1):-1:1);
0058             iiN = permute(iN, ndims(i1):-1:1);
0059             for j = 1:length(ii1(:))
0060                 ki = ki + 1;
0061                 xx{ki} = x(ii1(j):iiN(j));  %% multiple sets of indices for varset
0062             end
0063         end
0064     end
0065     if nargin > 3
0066         xx = vertcat(xx{:});
0067     end
0068 end

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005