%% Generate Code for Binary Operations and Functions Without Implicit Expansion
% This example shows how to generate code for binary operations and functions without implicitly expanding its operands.
% The coder.sameSizeBinaryOp function applies the required operation and bypasses additional code generation and change in output size associated with implicit expansion.
% This example highlights the difference between the code generated for coder.sameSizeBinaryOp and the code generated for the minus function.
%% Subtracting Two Operands With Implicit Expansion
% For this code snippet, the generated code implicitly expands the output.

type fooImpEx.m

% Define the operand types.

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

% Generate code for the function by running this command:

codegen fooImpEx -config:lib -args {a,b}

% The code generated for the function fooImpEx is shown here.

type codegen/lib/fooImpEx/fooImpEx.c

% The generated code includes code to automatically expand the size of compatible operands.
%
%% Subtracting Two Same-Size Operands Without Implicit Expansion
% This code snippet uses coder.sameSizeBinaryOp to apply the operation without using implicit expansion.

type fooSameSize.m

% Generate code for the function by running this command:

codegen fooSameSize -config:lib -args {a,b}

% The code generated for the function fooImpEx is shown here.

type codegen/lib/fooSameSize/fooSameSize.c

% In this case, the variable out is fixed-size and the code generated for the operation applied by the coder.sameSizeBinaryOp function does not expand the operands. The generated function fooSameSize does not contain additional loops to increase the size of the operands.
% Copyright 2020 The MathWorks, Inc.