diff --git a/+misc/Gain.m b/+misc/Gain.m index 592a391a5ead8fd0f41422e2bb2ac811121d211f..f69acac6e2a88232bc77a25fe994734997aa5167 100644 --- a/+misc/Gain.m +++ b/+misc/Gain.m @@ -95,19 +95,16 @@ classdef Gain < handle & matlab.mixin.Copyable end end % exchange() - function newObj = mtimes(obj, a) + function newObj = mtimes(obj, a, inputType, outputType) % mtimes is the method for left-side multiplication of the constant double-array a. arguments obj misc.Gain; a double; + inputType (1, 1) string = obj.inputType; + outputType (1, 1) string = obj.outputType; end - assert(isscalar(a) || all(size(a) == [1, 1]*sum(obj.lengthOutput)), ... - "double to be multiplied with misc.Gain must be scalar or quadratic matrix " ... - + "of size sum(obj.lengthOutput) x sum(obj.lengthOutput)"); - % otherwise there is confusion what the resulting output should be. - newObj = obj.copy(); - newObj.value = a*obj.value; + newObj = misc.Gain(inputType, a*obj.value, "outputType", outputType); end % mtimes() function mySeries = series(obj, nextGain) diff --git a/+misc/Gains.m b/+misc/Gains.m index bafed6ebb1b4a6c6a322196446d241b29fbc02a6..b67f51ad8dffc43a680680b1e11e7ec2c33d5841 100644 --- a/+misc/Gains.m +++ b/+misc/Gains.m @@ -95,11 +95,25 @@ classdef Gains < handle & matlab.mixin.Copyable c.verifySizes(); end %plus() - function newObj = mtimes(obj, a) + function newObj = mtimes(obj, a, inputType, outputType) % mtimes is the method for left-side multiplication of the constant double-array a. + arguments + obj misc.Gains; + a double; + inputType string = string([]); + outputType string = string([]); + end % arguments newObj = misc.Gains(); - for it = 1 : obj.numTypes - newObj.add(mtimes(obj.gain(it), a)); + if isempty(inputType) || isempty(outputType) + for it = 1 : obj.numTypes + newObj.add(mtimes(obj.gain(it), a)); + end + else + assert(numel(inputType) == obj.numTypes); + assert(numel(outputType) == obj.numTypes); + for it = 1 : obj.numTypes + newObj.add(mtimes(obj.gain(it), a, inputType(it), outputType(it))); + end end end % mtimes() diff --git a/+unittests/+misc/testGains.m b/+unittests/+misc/testGains.m index 28e50cc733e52aae65d604a8c3a73e964cad7059..b454657fa72e02995582374f79f70f0295de7a02 100644 --- a/+unittests/+misc/testGains.m +++ b/+unittests/+misc/testGains.m @@ -5,12 +5,10 @@ tests = functiontests(localfunctions()); end function testMtimes(tc) -e1 = misc.Gain('in1', ones(3, 2), ... - 'outputType', {'bliblublu', 'bliblablub'}, ... - 'lengthOutput', [2; 1]); -e2 = misc.Gain('in2', ones(3, 2),... - 'outputType', {'xyz', 'bliblabla'}, ... - 'lengthOutput', [1; 2]); +e1 = misc.Gain("in1", ones(3, 2), ... + "outputType", "bliblublu"); +e2 = misc.Gain("in2", ones(3, 2),... + "outputType", "xyz"); ee = e1 + e2; a = triu(ones(3)); @@ -60,65 +58,65 @@ tc.verifyEqual(G12.outputType, G1.outputType); end % testBlkdiag() function testRemove(tc) -a = misc.Gain('in1', ones(10),... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); +a = misc.Gain("in1", ones(10),... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); A = misc.Gains(a); -A.remove('asdf'); +A.remove("asdf"); tc.verifyEqual(a.value, A.value) B = copy(A); -tc.verifyTrue(isempty(B.remove('in1'))); - -e1 = misc.Gain('in1', ones(3, 2), ... - 'outputType', {'bliblublu', 'bliblablub'}, ... - 'lengthOutput', [2; 1]); -e2 = misc.Gain('in2', ones(4, 2),... - 'outputType', {'xyz', 'bliblabla'}, ... - 'lengthOutput', [2; 2]); +tc.verifyTrue(isempty(B.remove("in1"))); + +e1 = misc.Gain("in1", ones(3, 2), ... + "outputType", ["bliblublu", "bliblablub"], ... + "lengthOutput", [2; 1]); +e2 = misc.Gain("in2", ones(4, 2),... + "outputType", ["xyz", "bliblabla"], ... + "lengthOutput", [2; 2]); ee = copy(e1 + e2); -tc.verifyTrue(isequal(ee.remove('in1').gain(1), e2)); +tc.verifyTrue(isequal(ee.remove("in1").gain(1), e2)); end % testRemove() function testRemoveOutput(tc) -e1 = misc.Gain('in1', ones(3, 2), ... - 'outputType', {'bliblublu', 'bliblablub'}, ... - 'lengthOutput', [2; 1]); -e2 = misc.Gain('in2', ones(4, 2),... - 'outputType', {'xyz', 'bliblabla'}, ... - 'lengthOutput', [2; 2]); +e1 = misc.Gain("in1", ones(3, 2), ... + "outputType", ["bliblublu", "bliblablub"], ... + "lengthOutput", [2; 1]); +e2 = misc.Gain("in2", ones(4, 2),... + "outputType", ["xyz", "bliblabla"], ... + "lengthOutput", [2; 2]); ee = copy(e1 + e2); -ee = ee.removeOutput('xyz'); -ee = ee.removeOutput('bliblabla'); +ee = ee.removeOutput("xyz"); +ee = ee.removeOutput("bliblabla"); tc.verifyTrue(isequal(ee, misc.Gains(e1))) end % testRemove() function testStrrepOutputType(tc) -e1 = misc.Gain('in1', ones(3, 2), ... - 'outputType', {'bliblublu', 'bliblablub'}, ... - 'lengthOutput', [2; 1]); -e2 = misc.Gain('in2', ones(4, 2),... - 'outputType', {'xyz', 'bliblabla'}, ... - 'lengthOutput', [2; 2]); +e1 = misc.Gain("in1", ones(3, 2), ... + "outputType", ["bliblublu", "bliblablub"], ... + "lengthOutput", [2; 1]); +e2 = misc.Gain("in2", ones(4, 2),... + "outputType", ["xyz", "bliblabla"], ... + "lengthOutput", [2; 2]); ee = e1 + e2; -e1BLA = strrepOutputType(copy(e1), 'bla', 'BLA'); -e2BLA = strrepOutputType(copy(e2), 'bla', 'BLA'); -eBLA = strrepOutputType(copy(ee), 'bla', 'BLA'); +e1BLA = strrepOutputType(copy(e1), "bla", "BLA"); +e2BLA = strrepOutputType(copy(e2), "bla", "BLA"); +eBLA = strrepOutputType(copy(ee), "bla", "BLA"); tc.verifyEqual(e1BLA.outputType, ["bliblublu"; "bliBLAblub"]); tc.verifyEqual(e2BLA.outputType, ["xyz"; "bliBLABLA"]); tc.verifyEqual(eBLA.outputType, ["bliblublu"; "bliBLAblub"; "xyz"; "bliBLABLA"]); end % testStrrepOutputType() function testEqual(tc) -a = misc.Gain('in1', ones(10),... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); +a = misc.Gain("in1", ones(10),... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); b = copy(a); -e1 = misc.Gain('in1', ones(10)+blkdiag(zeros(4), 1, zeros(5)), ... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); -e2 = misc.Gain('in1', ones(10),... - 'outputType', {'x', 'y', 'xw', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); +e1 = misc.Gain("in1", ones(10)+blkdiag(zeros(4), 1, zeros(5)), ... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); +e2 = misc.Gain("in1", ones(10),... + "outputType", ["x", "y", "xw", "X"], ... + "lengthOutput", [2; 3; 4; 1]); tc.verifyTrue(isequal(a, b)); tc.verifyFalse(isequal(a, a, e1)); @@ -127,68 +125,68 @@ tc.verifyTrue(isequal(a+b+e1, a+b+e1)); tc.verifyTrue(isequal(a+b+e1, a+e1+b)); % misc.Gains -e12 = misc.Gain('in2', ones(10)+blkdiag(zeros(4), 1, zeros(5)), ... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); -b2 = misc.Gain('in3', ones(10),... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); +e12 = misc.Gain("in2", ones(10)+blkdiag(zeros(4), 1, zeros(5)), ... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); +b2 = misc.Gain("in3", ones(10),... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); tc.verifyTrue(isequal(a+b2+e12, a+b2+e12)); tc.verifyFalse(isequal(a+b2+e12, a+e12+b2)); end % testEqual() function testExchange(tc) -a = misc.Gain('in1', ones(10),... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); -newY = misc.Gain('in1', 2*ones(3, 10), 'outputType', 'y'); +a = misc.Gain("in1", ones(10),... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); +newY = misc.Gain("in1", 2*ones(3, 10), "outputType", "y"); a.exchange(newY); -tc.verifyEqual(a.valueOfOutput('y'), 2*ones(3, 10)); +tc.verifyEqual(a.valueOfOutput("y"), 2*ones(3, 10)); end % testExchange() function testGainParallel(tc) -a = misc.Gain('in', ones(2),... - 'outputType', {'x', 'y'}, ... - 'lengthOutput', [1; 1]); -b = misc.Gain('in', ones(1, 2),... - 'outputType', {'y'}); +a = misc.Gain("in", ones(2),... + "outputType", ["x", "y"], ... + "lengthOutput", [1; 1]); +b = misc.Gain("in", ones(1, 2),... + "outputType", ["y"]); c = parallel(a, b); -tc.verifyEqual(c.valueOfOutput('x'), [1, 1]); -tc.verifyEqual(c.valueOfOutput('y'), 2*[1, 1]); +tc.verifyEqual(c.valueOfOutput("x"), [1, 1]); +tc.verifyEqual(c.valueOfOutput("y"), 2*[1, 1]); d = parallel(b, a); -tc.verifyEqual(d.valueOfOutput('x'), [1, 1]); -tc.verifyEqual(d.valueOfOutput('y'), 2*[1, 1]); +tc.verifyEqual(d.valueOfOutput("x"), [1, 1]); +tc.verifyEqual(d.valueOfOutput("y"), 2*[1, 1]); %d = parallel(b, a); end % testGainParallel() function testGain2Gains(tc) -a = misc.Gain('in1', ones(2),... - 'outputType', {'x', 'y'}, ... - 'lengthOutput', [1; 1]); -b = misc.Gain('in2', ones(1, 2),... - 'outputType', {'y'}); +a = misc.Gain("in1", ones(2),... + "outputType", ["x", "y"], ... + "lengthOutput", [1; 1]); +b = misc.Gain("in2", ones(1, 2),... + "outputType", ["y"]); c = a + b; tc.verifyEqual(c.value, [ones(2), [zeros(1, 2); ones(1, 2)]]); end % testGains2Gains() function testGains2Gains(tc) -a = misc.Gains(misc.Gain('in', ones(2),... - 'outputType', {'x', 'y'}, ... - 'lengthOutput', [1; 1])); -b = misc.Gains(misc.Gain('in', ones(1, 2),... - 'outputType', {'y'})); +a = misc.Gains(misc.Gain("in", ones(2),... + "outputType", ["x", "y"], ... + "lengthOutput", [1; 1])); +b = misc.Gains(misc.Gain("in", ones(1, 2),... + "outputType", ["y"])); c = a + b; tc.verifyEqual(c.value, a.value + [zeros(1, 2); b.value]); end % testGains2Gains() function testLengthOfOutput(tc) -a = misc.Gain('in1', ones(10),... - 'outputType', {'x', 'y', 'xy', 'X'}, ... - 'lengthOutput', [2; 3; 4; 1]); -tc.verifyEqual(a.lengthOfOutput('x', 'y'), [2; 3]) -tc.verifyEqual(a.lengthOfOutput('y', 'x'), [3; 2]) -tc.verifyEqual(a.lengthOfOutput('x', 'y', 'xy', 'X'), [2; 3; 4; 1]) -tc.verifyEqual(a.lengthOfOutput('X', 'x', 'y', 'xy'), [1; 2; 3; 4]) +a = misc.Gain("in1", ones(10),... + "outputType", ["x", "y", "xy", "X"], ... + "lengthOutput", [2; 3; 4; 1]); +tc.verifyEqual(a.lengthOfOutput("x", "y"), [2; 3]) +tc.verifyEqual(a.lengthOfOutput("y", "x"), [3; 2]) +tc.verifyEqual(a.lengthOfOutput("x", "y", "xy", "X"), [2; 3; 4; 1]) +tc.verifyEqual(a.lengthOfOutput("X", "x", "y", "xy"), [1; 2; 3; 4]) end % testLengthOfOutput() \ No newline at end of file