Single Vector Summation using EinsumΒΆ
This is an example of how to properly use the einsum function to compute the summation of a single vector.
from openmdao.api import Problem
import numpy as np
from omtools.api import Group
import omtools.api as ot
class ExampleVectorSummation(Group):
def setup(self):
a = np.arange(4)
vec = self.declare_input('a', val=a)
# Summation of all the entries of a vector
self.register_output('einsum_summ1', ot.einsum(
vec,
subscripts='i->',
))
prob = Problem()
prob.model = ExampleVectorSummation()
prob.setup(force_alloc_complex=True)
prob.run_model()
print('a', prob['a'].shape)
print(prob['a'])
print('einsum_summ1', prob['einsum_summ1'].shape)
print(prob['einsum_summ1'])
a (4,)
[0. 1. 2. 3.]
einsum_summ1 (1,)
[6.]