Accelerating the pace of engineering and science

# collect

Collect terms with the same powers

### Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

## Syntax

```collect(p, x, <f>)
collect(p, [x1, x2, …], <f>)
```

## Description

collect(p, x) groups terms with the same powers of x in an expression p.

collect(p, [x1, x2, ...]) groups terms with the same powers of x1, x2, … in a multivariate expression p.

If you pass a function name f as a third argument to collect, the procedure collects the powers of x (x1, x2, … for multivariate expression). Then it applies the function f to the coefficients.

collect(p, x) presents p as a sum . The coefficients ai are not polynomials in x. These coefficients can contain some terms with x, for example, sin(x) or .

collect returns a modified copy of a polynomial. The function does not change the polynomial itself. See Example 1.

If p is a rational expression in x, collect handles the numerator and denominator separately.

If p is a multivariate expression, collect(p, [x1, x2, …]) returns an expression in the following form:

.

The coefficients ai1, i2, … are not polynomials in x. These coefficients can contain some terms with x1, x2, …, for example, .

If p is a rational expression in x1, x2, …, the collect command handles the numerator and denominator separately.

For polynomial expressions, collect internally calls two functions: poly and then expr. The function poly converts an expression p into a polynomial in the given unknowns. This function returns a polynomial with the terms collected by the same powers. Then expr converts this polynomial into a polynomial expression. See poly for more information and examples. When applied to a rational expression, collect handles the numerator and denominator separately.

You can use arbitrary expressions as indeterminates. See Example 2.

You can specify a function name instead of a variable. In this case, collect treats all calls of the function with different arguments as different variables. See Example 4.

collect does not recursively collect the operands of nonpolynomial subexpressions of p. See Example 2.

If p is not a polynomial expression, collect can return the unchanged expression p. See Example 5.

## Examples

### Example 1

You can define a polynomial expression p and collect terms with the same powers of x and y:

```p := x*y + z*x*y + y*x^2 - z*y*x^2 + x + z*x;
collect(p, [x, y])```

collect does not modify the original expression:

`p`

You can collect terms with same powers of x:

`collect(p, [x])`

If an expression contains only one indeterminate, you can omit the square brackets in the second argument of the function call:

`collect(p, x)`

To factor coefficients in a resulting expression, pass factor as a third argument to collect:

`collect(p, x, factor)`

### Example 2

collect does not modify nonpolynomial subexpressions even if they contain a given indeterminate. In particular, collect does not recursively handle the operands of a nonpolynomial subexpression:

`collect(sin((x + 1)^2)*(x + 1) + 5*sin((x + 1)^2) + x, x)`

collect accepts nonpolynomial subexpressions as indeterminates:

```collect(sin((x + 1)^2)*(x + 1) + 5*sin((x + 1)^2) + x,
sin((x + 1)^2))```

### Example 3

collect normalizes a rational expression, and then handles the numerator and denominator separately:

`collect(z/(x+y) + 3*z/(x+z), z)`

### Example 4

If you specify the name of a function as an indeterminate, collect handles functions calls with different arguments as different indeterminates:

`collect(a*f(1) + c*f(1) + f(2) + d*f(2), f)`

`collect(a*sin(x) + b*sin(x) + c*sin(y) + d*sin(y), sin)`

```p:= diff(besselJ(0, x), x \$ 4);
collect(p, besselJ);
collect(p, besselJ, expand);```

### Example 5

If p is not a polynomial expression, collect can return the unchanged expression p:

```p :=  y^2*sin(x) + y*sin(x) + y^2*cos(x) + y*cos(x);
collect(p, x)```

The expression p is a polynomial expression in y. You can group the terms with the same powers in this variable:

`collect(p, y)`

## Parameters

 p x, x1, x2, … The indeterminates: typically, identifiers or indexed identifiers. f A function.