# Sum, Add, Subtract, Sum of Elements

Math Operations

## Description

The Sum block performs addition or subtraction on its inputs. This block can add or subtract scalar, vector, or matrix inputs. It can also collapse the elements of a signal.

You specify the operations of the block with the List of signs parameter. Plus (`+`), minus (`-`), and spacer (`|`) characters indicate the operations to be performed on the inputs:

• If there are two or more inputs, then the number of `+` and `-` characters must equal the number of inputs. For example, "`+-+`" requires three inputs and configures the block to subtract the second (middle) input from the first (top) input, and then add the third (bottom) input.

• All nonscalar inputs must have the same dimensions. Scalar inputs will be expanded to have the same dimensions as the other inputs.

• A spacer character creates extra space between ports on the block's icon.

• For a round Sum block, the first input port is the port closest to the 12 o'clock position going in a counterclockwise direction around the block. Similarly, other input ports appear in counterclockwise order around the block.

• If only addition of all inputs is required, then a numeric parameter value equal to the number of inputs can be supplied instead of "`+`" characters.

• If only one input port is required, a single "`+`" or "`-`" collapses the element via the specified operation.

The Sum block first converts the input data type(s) to its accumulator data type, then performs the specified operations. The block converts the result to its output data type using the specified rounding and overflow modes.

### Calculation of Block Output

Output calculation for the Sum block depends on the number of block inputs and the sign of input ports:

If the Sum block has...And...The formula for output calculation is...Where...

One input port

The input port sign is +

y = e[0] + e[1] + e[2] ... + e[m]

`e[i]` is the ith element of input u

The input port sign is –

y = 0.0 – e[0] – e[1] – e[2] ... – e[m]

Two or more input ports

All input port signs are –

y = 0.0 – u[0] – u[1] – u[2] ... – u[n]

`u[i]` is the input to the ith input port

The kth input port is the first port where the sign is +

y = u[k] – u[0] – u[1] – u[2] – u[k–1] (+/–) u[k+1] ... (+/–) u[n]

## Data Type Support

The Sum block accepts real or complex signals of the following data types:

• Floating point

• Built-in integer

• Fixed point

• Boolean

The inputs can be of different data types, unless you select the Require all inputs to have the same data type parameter. For more information, see Data Types Supported by Simulink in the Simulink® documentation.

## Parameters and Dialog Box

The Main pane of the Sum block dialog box appears as follows:

The Signal Attributes pane of the Sum block dialog box appears as follows:

### Show data type assistant

Display the Data Type Assistant.

#### Settings

The Data Type Assistant helps you set the Output data type parameter.

### Icon shape

Designate the icon shape of the block.

#### Settings

Default: `round`

`rectangular`

Designate the icon shape of the block as rectangular.

`round`

Designate the icon shape of the block as round.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### List of signs

Enter plus (`+`) and minus (`-`) characters.

#### Settings

Default: ` |++`

• Addition is the default operation, so if you only want to add the inputs, enter the number of input ports.

• For a single vector input, "`+`" or "`-`" will collapse the vector using the specified operation.

• Enter as many plus (`+`) and minus (`-`) characters as there are inputs.

#### Tips

You can manipulate the positions of the input ports on the block by inserting spacers (`|`) between the signs in the List of signs parameter. For example, "`++|--`" creates an extra space between the second and third input ports.

#### Dependencies

Entering only one element enables the Sum over parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Sum over

Select dimension over which to perform the sum over operation.

#### Settings

Default: `All dimensions`

`All dimensions`

Sum all input elements, yielding a scalar.

`Specified dimension`

Display the Dimension parameter, where you specify the dimension over which to perform the operation.

#### Dependencies

Selecting `Specified dimension` enables the Dimension parameter.

List of signs (when it has only one element) enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Dimension

Specify the dimension over which to perform the operation.

#### Settings

Default: `1`

The block follows the same summation rules as the MATLAB® `sum` function.

Suppose that you have a 2-by-3 matrix U.

• Setting Dimension to `1` results in the output Y being computed as:

$Y={\sum }_{i=1}^{2}U\left(i,j\right)$

• Setting Dimension to `2` results in the output Y being computed as:

$Y={\sum }_{j=1}^{3}U\left(i,j\right)$

If the specified dimension is greater than the dimension of the input, an error message appears.

#### Dependencies

Setting Sum over to ```Specified dimension``` enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Sample time

 Note:   This parameter is not visible in the block dialog box unless it is explicitly set to a value other than `-1`. To learn more, see Blocks for Which Sample Time Is Not Recommended

### Require all inputs to have the same data type

Require that all inputs have the same data type.

#### Settings

Default: Off

On

Require that all inputs have the same data type.

Off

Do not require that all inputs have the same data type.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Lock data type settings against changes by the fixed-point tools

Select to lock data type settings of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.

#### Settings

Default: Off

On

Locks all data type settings for this block.

Off

Allows the Fixed-Point Tool and the Fixed-Point Advisor to change data type settings for this block.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Integer rounding mode

Specify the rounding mode for fixed-point operations.

#### Settings

Default: `Floor`

`Ceiling`

Rounds both positive and negative numbers toward positive infinity. Equivalent to the MATLAB `ceil` function.

`Convergent`

Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ `convergent` function.

`Floor`

Rounds both positive and negative numbers toward negative infinity. Equivalent to the MATLAB `floor` function.

`Nearest`

Rounds number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer `nearest` function.

`Round`

Rounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer `round` function.

`Simplest`

Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.

`Zero`

Rounds number toward zero. Equivalent to the MATLAB `fix` function.

#### Command-Line Information

 Parameter: `RndMeth` Type: string Value: `'Ceiling'` | `'Convergent'` | `'Floor'` | `'Nearest'` | `'Round'` | `'Simplest'` | `'Zero'` Default: `'Floor'`

### Saturate on integer overflow

Specify whether overflows saturate.

#### Settings

Default: Off

On

Overflows saturate to either the minimum or maximum value that the data type can represent.

For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.

Off

Overflows wrap to the appropriate value that is representable by the data type.

For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.

#### Tips

• Consider selecting this check box when your model has possible overflow and you want explicit saturation protection in the generated code.

• Consider clearing this check box when you want to optimize efficiency of your generated code.

Clearing this check box also helps you avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.

• When you select this check box, saturation applies to every internal operation on the block, not just the output or result.

• In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

#### Command-Line Information

 Parameter: `SaturateOnIntegerOverflow` Type: string Value: `'off'` | `'on'` Default: `'off'`

### Accumulator data type

Specify the accumulator data type.

#### Settings

Default: ```Inherit: Inherit via internal rule```

`Inherit: Inherit via internal rule`

Use internal rule to determine accumulator data type.

`Inherit: Same as first input`

Use data type of first input signal.

`double`

Accumulator data type is `double`.

`single`

Accumulator data type is `single`.

`int8`

Accumulator data type is `int8`.

`uint8`

Accumulator data type is `uint8`.

`int16`

Accumulator data type is `int16`.

`uint16`

Accumulator data type is `uint16`.

`int32`

Accumulator data type is `int32`.

`uint32`

Accumulator data type is `uint32`.

`fixdt(1,16,0)`

Accumulator data type is fixed point `fixdt(1,16,0)`.

`fixdt(1,16,2^0,0)`

Accumulator data type is fixed point `fixdt(1,16,2^0,0)`.

`<data type expression>`

The name of a data type object, for example `Simulink.NumericType`

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Mode

Select the category of accumulator data to specify

#### Settings

Default: `Inherit`

`Inherit`

Specifies inheritance rules for data types. Selecting `Inherit` enables a list of possible values:

• `Inherit via internal rule` (default)

• `Same as first input`

`Built in`

Specifies built-in data types. Selecting ```Built in``` enables a list of possible values:

• `double` (default)

• `single`

• `int8`

• `uint8`

• `int16`

• `uint16`

• `int32`

• `uint32`

`Fixed point`

Specifies fixed-point data types.

`Expression`

Specifies expressions that evaluate to data types. Selecting `Expression` enables you to enter an expression.

#### Dependency

Clicking the Show data type assistant button for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Data type override

Specify data type override mode for this signal.

#### Settings

Default: `Inherit`

`Inherit`

Inherits the data type override setting from its context, that is, from the block, `Simulink.Signal` object or Stateflow® chart in Simulink that is using the signal.

`Off`

Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.

#### Tip

The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.

#### Dependency

This parameter appears only when the Mode is ```Built in``` or `Fixed point`.

### Signedness

Specify whether you want the fixed-point data to be signed or unsigned.

#### Settings

Default: `Signed`

`Signed`

Specify the fixed-point data to be signed.

`Unsigned`

Specify the fixed-point data to be unsigned.

#### Dependencies

Selecting Mode > ```Fixed point``` for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Word length

Specify the bit size of the word that will hold the quantized integer.

#### Settings

Default: `16`

Minimum: `0`

Maximum: `32`

Large word sizes represent large values with greater precision than small word sizes.

#### Dependencies

Selecting Mode > ```Fixed point``` for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

#### Settings

Default: ```Binary point```

`Binary point`

Specify binary point location.

`Slope and bias`

Enter slope and bias.

#### Dependencies

Selecting Mode > ```Fixed point``` for the accumulator data type enables this parameter.

Selecting `Binary point` enables:

• Fraction length

Selecting `Slope and bias` enables:

• Slope

• Bias

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Fraction length

Specify fraction length for fixed-point data type.

#### Settings

Default: `0`

Binary points can be positive or negative integers.

#### Dependencies

Selecting Scaling > ```Binary point``` for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Slope

Specify slope for the fixed-point data type.

#### Settings

Default: `2^0`

Specify any positive real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Bias

Specify bias for the fixed-point data type.

#### Settings

Default: `0`

Specify any real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` for the accumulator data type enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Output minimum

Lower value of the output range that Simulink checks.

#### Settings

Default: `[]` (unspecified)

Specify this number as a finite, real, double, scalar value.

 Note:   If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, see `Simulink.BusElement`.

Simulink uses the minimum to perform:

 Note:   Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Command-Line Information

 Parameter: `OutMin` Type: string Value: `'[ ]'` Default: `'[ ]'`

### Output maximum

Upper value of the output range that Simulink checks.

#### Settings

Default: `[]` (unspecified)

Specify this number as a finite, real, double, scalar value.

 Note:   If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, see `Simulink.BusElement`.

Simulink uses the maximum value to perform:

 Note:   Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Command-Line Information

 Parameter: `OutMax` Type: string Value: `'[ ]'` Default: `'[ ]'`

### Output data type

Specify the output data type.

#### Settings

Default: ```Inherit: Inherit via internal rule```

`Inherit: Inherit via internal rule`

Simulink chooses a data type to balance numerical accuracy, performance, and generated code size, while taking into account the properties of the embedded target hardware. If you change the embedded target settings, the data type selected by the internal rule might change. It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If the internal rule doesn't meet your specific needs for numerical accuracy or performance, use one of the following options:

• Specify the output data type explicitly.

• Use the simple choice of ```Inherit: Same as first input```.

• Explicitly specify a default data type such as `fixdt(1,32,16)` and then use the Fixed-Point Tool to propose data types for your model. For more information, see `fxptdlg`.

• To specify your own inheritance rule, use ```Inherit: Inherit via back propagation``` and then use a Data Type Propagation block. Examples of how to use this block are available in the Signal Attributes library Data Type Propagation Examples block.

 Note:   The accumulator internal rule favors greater numerical accuracy, possibly at the cost of less efficient generated code. To get the same accuracy for the output, set the output data type to ```Inherit: Inherit same as accumulator```.
`Inherit: Inherit via back propagation`

Use data type of the driving block.

`Inherit: Same as first input`

Use data type of first input signal.

`Inherit: Same as accumulator`

Output data type is the same as accumulator data type.

`double`

Output data type is `double`.

`single`

Output data type is `single`.

`int8`

Output data type is `int8`.

`uint8`

Output data type is `uint8`.

`int16`

Output data type is `int16`.

`uint16`

Output data type is `uint16`.

`int32`

Output data type is `int32`.

`uint32`

Output data type is `uint32`.

`fixdt(1,16,0)`

Output data type is fixed point `fixdt(1,16,0)`.

`fixdt(1,16,2^0,0)`

Output data type is fixed point `fixdt(1,16,2^0,0)`.

`<data type expression>`

Use a data type object, for example, `Simulink.NumericType`.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Mode

Select the category of data to specify.

#### Settings

Default: `Inherit`

`Inherit`

Inheritance rules for data types. Selecting `Inherit` enables a second menu/text box to the right. Select one of the following choices:

• `Inherit via internal rule` (default)

• `Inherit via back propagation`

• `Same as first input`

• `Same as accumulator`

`Built in`

Built-in data types. Selecting `Built in` enables a second menu/text box to the right. Select one of the following choices:

• `double` (default)

• `single`

• `int8`

• `uint8`

• `int16`

• `uint16`

• `int32`

• `uint32`

`Fixed point`

Fixed-point data types.

`Expression`

Expressions that evaluate to data types. Selecting `Expression` enables a second menu/text box to the right, where you can enter the expression.

#### Dependency

Clicking the Show data type assistant button enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Signedness

Specify whether you want the fixed-point data as signed or unsigned.

#### Settings

Default: `Signed`

`Signed`

Specify the fixed-point data as signed.

`Unsigned`

Specify the fixed-point data as unsigned.

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

### Word length

Specify the bit size of the word that holds the quantized integer.

#### Settings

Default: `16`

Minimum: `0`

Maximum: `32`

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

### Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

#### Settings

Default: ```Binary point```

`Binary point`

Specify binary point location.

`Slope and bias`

Enter slope and bias.

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

Selecting `Binary point` enables:

• Fraction length

• Calculate Best-Precision Scaling

Selecting `Slope and bias` enables:

• Slope

• Bias

• Calculate Best-Precision Scaling

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Fraction length

Specify fraction length for fixed-point data type.

#### Settings

Default: `0`

Binary points can be positive or negative integers.

#### Dependencies

Selecting Scaling > ```Binary point``` enables this parameter.

### Slope

Specify slope for the fixed-point data type.

#### Settings

Default: `2^0`

Specify any positive real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` enables this parameter.

### Bias

Specify bias for the fixed-point data type.

#### Settings

Default: `0`

Specify any real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` enables this parameter.

## Examples

### How the Sum Block Reorders Inputs

If you use `-` on the first input port, the Sum block reorders the inputs so that, if possible, the first input uses a `+` operation. For example, in the expression ```output = -a-b+c```, the Sum block reorders the inputs so that ```output = c-a-b```. To initialize the accumulator, the Sum block uses the first `+` input port.

The block avoids performing a unary minus operation on the first operand `a` because doing so can change the value of `a` for fixed-point data types. In that case, the output value differs from the result of accumulating the values for `a`, `b`, and `c`.

 Tip   To explicitly specify a unary minus operation for ```output = -a-b+c```, you can use the Unary Minus block in the Math Operations library.

Suppose that you have the following modelmodel:

The following block parameters apply:

• Both Constant blocks, `Input1` and ```Input 2```, use `int8` for the Output data type.

• The Sum block uses `int8` for both Accumulator data type and Output data type.

• The Sum block has Saturate on integer overflow turned `on`.

The Sum block reorders the inputs so that the following operations occur and you get the ideal result of 127.

StepBlock Operation
1

Reorders inputs from `(–Input1 + Input2)` to ```(Input2 – Input1)```.

2

Initializes the accumulator by using the first `+` input port:

`Accumulator = int8(-1) = -1`

3

Continues to accumulate values:

```Accumulator = Accumulator – int8(-128) = 127```

4

Calculates the block output:

```Output = int8(127) = 127```

If the Sum block does not reorder the inputs, the following operations occur instead and you get the nonideal result of 126.

StepBlock Operation
1

Initializes the accumulator by using the first input port:

`Accumulator = int8(-(-128)) = 127`

Because saturation is on, the initial value of the accumulator saturates at 127 and does not wrap.

2

Continues to accumulate values:

```Accumulator = Accumulator + int8(-1) = 126```

3

Calculates the block output:

```Output = int8(126) = 126```

## Characteristics

 Direct Feedthrough Yes Sample Time Inherited from driving block Scalar Expansion Yes States 0 Dimensionalized Yes Multidimensionalized Yes, only along the specified dimension Zero-Crossing Detection No