Aggregate two points

Hi !
I want to aggregate two Points x and z.

import openturns as ot
x = ot.Point([1.0, 2.0, 3.0])
y = ot.Point([4.0, 5.0])

I would like to produce the Point([1.0, 2.0, 3.0, 4.0, 5.0]) by concatenating the values from x and y.

The first version that came to my mind was the “naive” way, using two for loops.

dimX = x.getDimension()
dimY = y.getDimension()
dim = dimX + dimY
z = ot.Point(dim)
for i in range(dimX):
    z[i] = x[i]
for i in range(dimY):
    z[i + dimX] = y[i]

Looking at the API more closely, I found the add method, which simplifies the scripts a bit:

z = ot.Point(x)
for i in range(dimY):
    z.add(y[i])

However, I have two regrets:

  • we cannot add a full Point in one single statement with z.add(y) so that we cannot avoid a for loop here (without using Numpy),
  • the verb add really means append (add may be confused with the vector addition).

Is there any other method that I missed in the API?
Regards,
Michaël

I would do it this way:

import openturns as ot
x = ot.Point([1.0, 2.0, 3.0])
y = ot.Point([4.0, 5.0])

z = ot.Point(x)
z.add(y)
print(z)

Output:

[1,2,3,4,5]

The example from the doc did not state that this is possible. Great, thank you!

Regards,

Michaël

Then I will update the doc when I have some time.

Same idea for Sample.add. I do not know if it is already documented.
Same concerning Sample::stack

Concerning the Sample.add and Sample.stack methods, would not Sample.vstack and Sample.hstack better names ?

I agree with with you: add is a poor name choice. Horizontal and vertical names would be fine to me, but this refer to the Sample as an np.array, while the Sample has a dimension and a size. Would addDimensions and addPoints be clearer?

1 Like