Skip to main content

Multiple Vector Summation using Einsum with Sparse Partials

This is an example of how to properly use the einsum function to compute the summation of multiple vectors while using sparse partial derivatives.

from csdl_om import Simulatorimport numpy as npfrom csdl import Modelimport csdl

class ExampleMultipleVectorSumSparse(Model):
    def define(self):
        a = np.arange(4)        vec = self.declare_variable('a', val=a)        self.register_output(            'einsum_special2_sparse_derivs',            csdl.einsum(                vec,                vec,                subscripts='i,j->',                partial_format='sparse',            ))

sim = Simulator(ExampleMultipleVectorSumSparse())sim.run()
print('a', sim['a'].shape)print(sim['a'])print('einsum_special2_sparse_derivs',      sim['einsum_special2_sparse_derivs'].shape)print(sim['einsum_special2_sparse_derivs'])
[0. 1. 2. 3.]einsum_special2_sparse_derivs (1,)[36.]