Skip to main content

Average of Multiple Vectors

This is an example of computing the average of a multiple vector inputs.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleMultipleVector(Model):
    def define(self):        n = 3
        # Declare a vector of length 3 as input        v1 = self.declare_variable('v1', val=np.arange(n))
        # Declare another vector of length 3 as input        v2 = self.declare_variable('v2', val=np.arange(n, 2 * n))
        # Output the elementwise average of vectors v1 and v2        self.register_output(            'multiple_vector_average',            csdl.average(v1, v2),        )

sim = Simulator(ExampleMultipleVector())sim.run()
print('v1', sim['v1'].shape)print(sim['v1'])print('v2', sim['v2'].shape)print(sim['v2'])print('multiple_vector_average', sim['multiple_vector_average'].shape)print(sim['multiple_vector_average'])
[0. 1. 2.]v2 (3,)[3. 4. 5.]multiple_vector_average (3,)[1.5 2.5 3.5]