Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

sum() on array elements

sum() on array elements

Hi,
Currently I have a model with a connector "con", which contains a vector (size 4) "vector[3]" and some other values ("x"," y" , "z"), theres a total of 29 values in  the connector.
Now there's a model which has more than one of this connector. Since there is a certain number of connectors (for example 2) on one side i did it like this

model test
con a[2];
...

equation
connect("
end test;

the model test is in con a connected to two other models
connect(test2.a,test.a[1]);
connect(test3.a,test.a[2]);

I guess the array as seen from model "test" now has this form:
[x, y, z, vector[1], vector[2], vector[3]]   ||<- a[1]
[x, y, z, vector[1], vector[2], vector[3]]   ||<- a[2]

so it's a [2, 6] matrix.

What I want to know is, how can I build the sum of the vector[1] of a[1] and vector[1] of a[2]?
I know I can do it with a loop, but is there a nicer way? Is it somehow possible to use the sum function just for a column of a matrix?
What I tried is: sum(a.vector[1], but this isn't working. Since the real matrix is a lot bigger and has a variable size, i dont want to do it by manually adding the values but rather use a sum function...

Thanks!

Re: sum() on array elements

I would probably use something like sum(a[1,i] for i in size(a,2)) or similar.

Re: sum() on array elements

Ok, thx so far.
Is the order of the elements in the connector-vector the same as in the connector definition?

so if its
connector con
Real x;
Real[3] vector;
Real y;
Real z;

then it is {x, vector[1], vector[2], vector[3], y, z, } and so on?

Re: sum() on array elements

I'm stilling having problems. It seems like the matrix somehow has other dimensions as I thought.
Is it right that my connector con is a vector of size 6, so it's 1 dimension?
and also is it right that a=con[3] has the size [3, 6] (or 6, 3??) and two dimensions?

Re: sum() on array elements

Ok, I tried a lot, but it seems as if a connector in modelica doesn't work like a normal vector and you can't call them only by number-coordinates as in my example, also I couldn't get the size()-function to work. But what I found out is this:

if you have a matrix like this:
a={{1, 2, 5, 9 ,10},
       {3, 4, 4, 1, 2},
      {5, 6, 2, 1, 5}};

And you want to build sums columnwise, you can do it like this
b=sum(:,3) for the 3rd colum (b=5+4+2)

Of course you can also do it rowwise by swapping the coordinates.

Just in case anybody is searching the forum for help on the sum() function...

There are 0 guests and 0 other users also viewing this topic