Single Vector Summation using Einsum with Sparse PartialsΒΆ
This is an example of how to properly use the einsum function to compute the summation of a single vector while using sparse partial derivative.
from openmdao.api import Problem
import numpy as np
from omtools.api import Group
import omtools.api as ot
class ExampleVectorSummationSparse(Group):
def setup(self):
a = np.arange(4)
vec = self.declare_input('a', val=a)
self.register_output(
'einsum_summ1_sparse_derivs',
ot.einsum_new_api(vec, operation=[(33, )],
partial_format='sparse'))
prob = Problem()
prob.model = ExampleVectorSummationSparse()
prob.setup(force_alloc_complex=True)
prob.run_model()
print('a', prob['a'].shape)
print(prob['a'])
print('einsum_summ1_sparse_derivs', prob['einsum_summ1_sparse_derivs'].shape)
print(prob['einsum_summ1_sparse_derivs'])
a (4,)
[0. 1. 2. 3.]
einsum_summ1_sparse_derivs (1,)
[6.]