Skip to main content

Vector-Vector Inner Product using Einsum

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

from csdl_om import Simulatorimport numpy as npfrom csdl import Modelimport csdl

class ExampleInnerVectorVector(Model):
    def define(self):        a = np.arange(4)
        vec = self.declare_variable('a', val=a)
        # Inner Product of 2 vectors        self.register_output('einsum_inner1',                             csdl.einsum(vec, vec, subscripts='i,i->'))

sim = Simulator(ExampleInnerVectorVector())sim.run()
print('a', sim['a'].shape)print(sim['a'])print('einsum_inner1', sim['einsum_inner1'].shape)print(sim['einsum_inner1'])
[0. 1. 2. 3.]einsum_inner1 (1,)[14.]