for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// We don't create the Scale if the corresponding element
// in the A matrix is 0.
feedback[i][j] = new Scale(this, "feedback_" + i + "_" + j);
feedback[i][j].factor.setExpression("A(" + i + ", " + j
+ ")");
feedback[i][j].input.link(states[j]);
connect(feedback[i][j].output, stateAdders[i].plus);
}
}
// Inputs
Scale[][] inputScales = new Scale[n][m];
IORelation[] inputs = new IORelation[m];
for (int j = 0; j < m; j++) {
inputs[j] = new TypedIORelation(this, "relation_input_" + j);
input.link(inputs[j]);
// Create input scales.
for (int i = 0; i < n; i++) {
// We create a scale for each input even if the
// corresponding element in B is 0. Otherwise,
// if the elements of A's in this row are also zero,
// then we will have an illegal topology.
inputScales[i][j] = new Scale(this, "b_" + i + "_" + j);
inputScales[i][j].factor.setExpression("B(" + i + ", " + j
+ ")");
inputScales[i][j].input.link(inputs[j]);
connect(inputScales[i][j].output, stateAdders[i].plus);
}
}
// Outputs
AddSubtract[] outputAdders = new AddSubtract[r];
Scale[][] outputScales = new Scale[r][n];
for (int l = 0; l < r; l++) {
outputAdders[l] = new AddSubtract(this, "outputAdder" + l);
connect(outputAdders[l].output, output);
// Create the output scales only if the corresponding
// 'c' element is not 0.
for (int i = 0; i < n; i++) {
outputScales[l][i] = new Scale(this, "outputScale_" + l
+ "_" + i);
outputScales[l][i].factor.setExpression("C(" + l + ", " + i
+ ")");
outputScales[l][i].input.link(states[i]);
connect(outputScales[l][i].output, outputAdders[l].plus);
}
}
// Direct feed through.
Scale[][] feedThrough = new Scale[r][m];
for (int l = 0; l < r; l++) {
for (int j = 0; j < m; j++) {
// Create the scale only if the element is not 0.
feedThrough[l][j] = new Scale(this, "feedThrough_" + l
+ "_" + j);
feedThrough[l][j].factor.setExpression("D(" + l + ", " + j
+ ")");
feedThrough[l][j].input.link(inputs[j]);
connect(feedThrough[l][j].output, outputAdders[l].plus);