Skip to main content

Vector-Vector Outer Product using Einsum

This is an example of how to properly use the einsum function to compute a vector-vector outer product.

from csdl_om import Simulatorimport numpy as npfrom csdl import Modelimport csdl

class ExampleOuterVectorVector(Model):
    def define(self):        a = np.arange(4)        vec = self.declare_variable('a', val=a)
        # Outer Product of 2 vectors        self.register_output(            'einsum_outer1', csdl.einsum(vec, vec,                                         subscripts='i,j->ij'))

sim = Simulator(ExampleOuterVectorVector())sim.run()
print('a', sim['a'].shape)print(sim['a'])print('einsum_outer1', sim['einsum_outer1'].shape)print(sim['einsum_outer1'])
[0. 1. 2. 3.]einsum_outer1 (4, 4)[[0. 0. 0. 0.] [0. 1. 2. 3.] [0. 2. 4. 6.] [0. 3. 6. 9.]]