Commit 10fb217b authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

quantity.Discrete: simple blkdiag - only works for quantities with same gridName and grid.

parent d02e7f7a
......@@ -532,7 +532,7 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete
[fineGrid, fineGridName] = getFinestGrid(objCell{~isEmpty});
for it = 1 : (numel(varargin) + 1) % +1 because the first entry is a
assert(all(strcmp(unique(fineGridName), unique(objCell{it}(1).gridName))), ...
assert(all(strcmp(fineGridName, objCell{it}(1).gridName)), ...
'gridNames of objects that are concatenated must be equal');
objCell{it} = objCell{it}.changeGrid(fineGrid, fineGridName);
end
......@@ -541,6 +541,30 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete
c = builtin('cat', argin{:});
end
function Y = blkdiag(A, varargin)
% blkdiag Block diagonal concatenation of matrix input arguments.
% |A 0 .. 0|
% Y = blkdiag(A,B,...) produces |0 B .. 0|
% |0 0 .. |
% Yet, A, B, ... must have the same gridName and grid.
if nargin == 1
Y = copy(A);
else
B = varargin{1};
if isempty(B)
Y = A;
else
assert(isequal(A(1).gridName, B(1).gridName), 'only implemented for same grid and gridName');
assert(isequal(A(1).grid, B(1).grid), 'only implemented for same grid and gridName');
Y = [A, zeros(size(A, 1), size(B, 2)); ...
zeros(size(B, 1), size(A, 2)), B];
end
if nargin > 2
Y = blkdiag(Y, varargin{2:end});
end
end
end % blkdiag()
function solution = solveAlgebraic(obj, rhs, gridName, objLimit)
%% this method solves
% obj(gridName) == rhs
......
......@@ -4,6 +4,38 @@ function [tests] = testDiscrete()
tests = functiontests(localfunctions);
end
function testBlkdiag(tc)
% init some data
syms z zeta
t = linspace(0, 1, 25);
A = quantity.Symbolic(...
[1+z*zeta, -zeta; -z, z^2], 'grid', {linspace(0, 1, 21), linspace(0, 1, 41)},...
'variable', {z, zeta}, 'name', 'q');
B = 2*A(:, 1);
C = 3*A(1);
% 1 input
tc.verifyEqual(A.on, A.blkdiag.on());
% 2 x same input
AA = blkdiag(A, A);
tc.verifyEqual(AA(1:2, 1:2).on(), A.on());
tc.verifyEqual(AA(3:4, 3:4).on(), A.on());
tc.verifyEqual(AA(1:2, 3:4).on(), 0*A.on());
tc.verifyEqual(AA(3:4, 1:2).on(), 0*A.on());
% 3 different sized quantites
ABCB = blkdiag(A, B, C, B);
tc.verifyEqual(ABCB(1:2, 1:2).on(), A.on());
tc.verifyEqual(ABCB(3:4, 3).on(), B.on());
tc.verifyEqual(ABCB(5, 4).on(), C.on());
tc.verifyEqual(ABCB(6:7, 5).on(), B.on());
zeroElements = ~logical(blkdiag(true(size(A)), true(size(B)), true(size(C)), true(size(B))));
tc.verifyEqual(ABCB(zeroElements(:)).on(), 0*ABCB(zeroElements(:)).on());
end % testBlkdiag()
function testSetName(tc)
blub = quantity.Discrete(ones(11, 2), 'grid', linspace(0, 1, 11), 'gridName', 'z');
blub.setName('asdf');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment