Question

I noticed the following line in the transfer function block in the Modelica library (Modelica.Blocks.Continuous.TransferFunction):

parameter Real bb[:] = vector([zeros(max(0,na-nb),1);b]);

I don't understand what the "vector" function call does, and why it is even necessary. Wouldn't

parameter Real bb[:] = [zeros(max(0,na-nb),1);b];

lead to the same result?

I found the following explanation in the Modelica Language Specification 3.2, but it doesn't really help:

"vector(A) - Returns a 1-vector, if A is a scalar and otherwise returns a vector containing all the elements of the array, provided there is at most one dimension size > 1."

I assume the whole story is based on some difference between arrays and vectors in Modelica. I would be grateful if someone helped me sort it out.

Was it helpful?

Solution

Simple answer: A vector is a one-dimensional array.

Imagine we declared something like this:

Real x[1,5,1,1];

Not there are only 5 elements in this thing and they are all contained in the same dimension. So what if we wanted to convert this to a simple vector of 5 elements. We could do this:

Real y[5] = { x[1,i,1,1] for i in 1:5 };

But that gets tedious. The alternative is:

Real y[5] = vector(x);

The compiler looks at this and automatically finds the dimension that has a size>1 and then pulls all the elements out as a vector.

And just to address one point you brought up, this is not valid:

Real z[5] = [1,2,3,4,5];

Because the left hand side is a vector (one dimensional) and the right hand side is an array. Normally, if you wanted to use the right hand side, the left hand side would have to be:

Real z[1,5] = [1,2,3,4,5];

If you want to stuff it into a vector, you need to convert the right hand side from an array to a vector, i.e.,

Real z[5] = vector([1,2,3,4,5]);

Does that help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top