@@ -526,11 +526,12 @@ def base_form_assembly_visitor(self, expr, tensor, *args):
526526 raise ValueError ("Expecting a scalar weight expression" )
527527 weights .append (w )
528528
529+ # Scalar FormSum
529530 if all (isinstance (op , numbers .Complex ) for op in args ):
530- result = sum ( weight * arg for weight , arg in zip ( weights , args ) )
531+ result = numpy . dot ( weights , args )
531532 return tensor .assign (result ) if tensor else result
532533
533- # Compress repeated terms
534+ # Accumulate coefficients in a dictionary for each unique Dat/Mat
534535 terms = defaultdict (int )
535536 for arg , weight in zip (args , weights ):
536537 t = self ._as_pyop2_type (arg )
@@ -550,15 +551,17 @@ def base_form_assembly_visitor(self, expr, tensor, *args):
550551 else :
551552 raise ValueError ("Expecting tensor to be None, Function, Cofunction, or Matrix" )
552553
553- # Compute the sum
554+ # Compute the linear combination
554555 if (all (isinstance (op , firedrake .Cofunction ) for op in args )
555556 or all (isinstance (op , firedrake .Function ) for op in args )):
557+ # Vector FormSum
556558 V , = set (a .function_space () for a in args )
557559 result = tensor if tensor else firedrake .Function (V )
558560 dats , weights = zip (* terms .items ())
559561 result .dat .maxpy (weights , dats )
560562 return result
561563 elif all (isinstance (op , ufl .Matrix ) for op in args ):
564+ # Matrix FormSum
562565 result = tensor .petscmat if tensor else PETSc .Mat ()
563566 for (op , w ) in terms .items ():
564567 if result :
0 commit comments