Main Content

coder.noImplicitExpansionInFunction

Disable implicit expansion within the specified function in the generated code

    Description

    example

    coder.noImplicitExpansionInFunction disables implicit expansion in the code generated for the MATLAB® function within which it is called.

    Disable implicit expansion to apply binary operations and functions in the generated code without automatic size change to the operands, additional code generation, and performance variations. See Generate Code With Implicit Expansion Enabled, Optimize Implicit Expansion in Generated Code and Compatible Array Sizes for Basic Operations.

    coder.noImplicitExpansionInFunction only affects binary operations in the generated code. This function does not disable implicit expansion in MATLAB code. To apply specific binary operations and functions in MATLAB without implicit expansion, use coder.sameSizeBinaryOp.

    Examples

    collapse all

    Use coder.noImplicitExpansionInFunction to disable implicit expansion in the code generated for a MATLAB function by calling it in within the required function. Disable implicit expansion to apply binary operations and functions without:

    • Automatic size change of compatible size operands

    • Additional code generation

    • Performance variations

    In this example, the functions DisableImpExpinFunction and ImpExpinFunction add two operands of compatible sizes with identical code but the former disables implicit expansion. The code generated for DisableImpExpinFunction does not contain additional code to apply implicit expansion. The code generated for ImpExpinFunction contains loops that must be executed if the operands are of compatible size and can be implicitly expanded at run-time.

    In the code generation report for the two functions, the sizes of the expression assigning the out values are different.

    Define a function DisableImpExpinFunction that calculates the addition of two operands without implicit expansion by calling coder.noImplicitExpansionInFunction within the required function.

    type DisableImpExpinFunction.m
    function out = DisableImpExpinFunction(a,b)
    coder.noImplicitExpansionInFunction;
    out = a + b;
    end
    

    Define a function ImpExpinFuntion that calculates the addition of two operands with implicit expansion enabled.

    type ImpExpinFunction.m
    function out = ImpExpinFunction(a,b)
    out = a + b;
    end
    

    Define a fixed-size and variable-size input type for these functions.

    a = coder.typeof(1,[2 1]);
    b = coder.typeof(1,[2 inf]);

    Generate code for the functions by using these commands:

    codegen DisableImpExpinFunction -args {a,b} -config:lib -lang:c++ -report
    Code generation successful: To view the report, open('codegen/lib/DisableImpExpinFunction/html/report.mldatx')
    
    codegen ImpExpinFunction.m -args {a,b} -config:lib -lang:c++ -report
    Code generation successful: To view the report, open('codegen/lib/ImpExpinFunction/html/report.mldatx')
    

    Compare Generated Code for Functions

    To apply implicit expansion, the generated code introduces additional code to automatically expand the operands. The difference between the generated code for the two functions is shown in this code.

    type codegen/lib/DisableImpExpinFunction/DisableImpExpinFunction.cpp
    //
    // File: DisableImpExpinFunction.cpp
    //
    // MATLAB Coder version            : 5.3
    // C/C++ source code generated on  : 01-Sep-2021 08:21:13
    //
    
    // Include Files
    #include "DisableImpExpinFunction.h"
    #include "coder_array.h"
    
    // Function Definitions
    //
    // Arguments    : const double a[2]
    //                const coder::array<double, 2U> &b
    //                double out[2]
    // Return Type  : void
    //
    void DisableImpExpinFunction(const double a[2],
                                 const coder::array<double, 2U> &b, double out[2])
    {
      out[0] = a[0] + b[0];
      out[1] = a[1] + b[1];
    }
    
    //
    // File trailer for DisableImpExpinFunction.cpp
    //
    // [EOF]
    //
    
    type codegen/lib/ImpExpinFunction/ImpExpinFunction.cpp
    //
    // File: ImpExpinFunction.cpp
    //
    // MATLAB Coder version            : 5.3
    // C/C++ source code generated on  : 01-Sep-2021 08:21:21
    //
    
    // Include Files
    #include "ImpExpinFunction.h"
    #include "coder_array.h"
    
    // Function Definitions
    //
    // Arguments    : const double a[2]
    //                const coder::array<double, 2U> &b
    //                coder::array<double, 2U> &out
    // Return Type  : void
    //
    void ImpExpinFunction(const double a[2], const coder::array<double, 2U> &b,
                          coder::array<double, 2U> &out)
    {
      int loop_ub;
      out.set_size(2, b.size(1));
      loop_ub = b.size(1);
      for (int i{0}; i < loop_ub; i++) {
        out[2 * i] = a[0] + b[2 * i];
        out[2 * i + 1] = a[1] + b[2 * i + 1];
      }
    }
    
    //
    // File trailer for ImpExpinFunction.cpp
    //
    // [EOF]
    //
    

    When implicit expansion is enabled, the generated code includes additional code to change the size of the operands, as seen in ImpExpinFunction. If implicit expansion is disabled, the generated code does not include additional code to apply any size changes to the operands, as seen in DisableImpExpinFunction.

    Compare the size of the output variables in the code generation report. The expression assigning the out variable in DisableImpExpinFunction is of the size 2x1.

    The expression assigning the out variable in ImpExpinFunction is of the size 2x:?.

    Introduced in R2021b