Combine terms of same algebraic structure

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


combine(f, <IgnoreAnalyticConstraints>)
combine(f, target, <IgnoreAnalyticConstraints>)
combine(f, [target1, target2, …], <IgnoreAnalyticConstraints>)


combine(f) rewrites products of powers in the expression f as a single power.

combine(f, target) combines several calls to the target function(s) in the expression f to a single call.

combine(f) applies these rewriting rules to products of powers occurring as subexpressions in an arithmetical expression f:

  • ,

  • ,

  • .

The last two rules are only valid under certain additional restrictions, such as when b is an integer. Except for the third rule, this behavior of combine is the inverse functionality of expand. See Example 1.


In certain cases, the MuPAD® internal simplifier automatically applies these rules in the reverse direction, and combine sometimes has no effect. See Example 2.

combine(f, target) applies rewriting rules applicable to the target function(s) to an arithmetical expression f. Some of the rules are only valid under certain additional restrictions. For most of the rules, combine implements the inverse functionality of expand. This list shows the rewriting rules for the targets.

  • target = arctan:

    for -1 < x < 1  and -1 < y < 1 .

  • target = exp (see Example 4):

    • ,

    • ,

      where valid, reacting to properties.

  • target = int (see Example 5):

    • af(x)dx=af(x)dx.

    • f(x)dx+g(x)dx=f(x)+g(x)dx.

    • abf(x)dx+abg(x)dx=abf(x)+g(x)dx.

    • abf(x)dx+abg(y)dy=abf(y)+g(y)dy.

    • abyf(x)dx+abxg(y)dy=abyf(c)+xf(c)dc.

  • target = gamma (see Example 6):

    • ,

    • ,

    • ,

    • ,

    • ,

      for positive integers n.

  • target = ln (see Example 7):

    The rules do not hold for arbitrary complex values of a, b. Specify appropriate properties for a or b to enable these rewriting rules. These rules are only applied to natural logarithms.

  • target = sincos (see Example 3):

    • ,

      where similar rules apply to sin(x) cos(y) and cos(x) cos(y):

    • .

  • target = sinhcosh:

    • ,

      where similar rules apply to sinh(x) cosh(y) and cosh(x) cosh(y).

    • These rules apply recursively to powers of sinh and cosh with positive integral exponents.

combine works recursively on the subexpressions of f.

If the second argument is a list of targets, then combine is applied to f subsequently for each of the targets in the list. See Example 10.

If f is an array, a list, or a set, combine is applied to all entries of f. See Example 11. If f is a polynomial or a series expansion, of type Series::Puiseux or Series::gseries, combine is applied to each coefficient. See Example 12.

Environment Interactions

combine reacts to properties of identifiers appearing in the input.


Example 1

Combine powers of the same base using combine.

combine(sin(x) + x*y*x^(exp(1)))

combine also combines powers with the same exponent in certain cases:


Example 2

In most cases, however, combine does not combine powers with the same exponent:


Example 3

Rewrite products of sines and cosines as a sum of sines and cosines by setting the second argument to sincos:

combine(sin(a)*cos(b) + sin(b)^2, sincos)

Rewrite sums of sines and cosines by setting the second argument to sincos:

combine(cos(a) + sin(a), sincos)

Powers of sines or cosines with negative integer exponents are not rewritten:

combine(sin(b)^(-2), sincos)

Example 4

Combine terms with the exponential function by specifying the second argument as exp.

combine(exp(3)*exp(2), exp)

combine(exp(a)^2, exp)

Example 5

Rewrite integrals by setting the second argument to int.


combine combines a constant term with the integral.


combine combines integrals with the same limits.


Example 6

Combine calls to gamma by specifying the target as gamma. The combine function simplifies quotients of gammas to rational expressions.

combine(gamma(n+3)*gamma(n+4/3) / gamma(n+1) / gamma(n+10/3), gamma)

Example 7

This example shows the application of rules for the logarithm, and their dependence on properties of the identifiers appearing in the input. In the complex plane, the logarithm of a product does not always equal the sum of the logarithms of its factors. For real positive numbers, however, this rule can apply.

Try to combine terms with calls to ln by specifying the target as ln.

combine(ln(a) + ln(b), ln)

combine does not combine the terms. Set the appropriate assumptions to combine the terms.

assume(a > 0): assume(b > 0):
combine(ln(a) + ln(b), ln)

unassume(a): unassume(b):

Example 8

If a and b are integer or rational numbers and b is less than 1000, combine returns logarithms.

combine(3*ln(2), ln)

If b is greater than or equal to 1000, combine returns results as :

combine(1234*ln(5), ln)

You can change the limit on the number b by using the Pref::autoExpansionLimit function. For example, when you use the default value N = 1000, combine returns the following result for this logarithm:

combine(12*ln(12), ln)

If you set the value of Pref::autoExpansionLimit to 10, combine returns this logarithm in its original form:

combine(12*ln(12), ln)

For further computations, restore the default value of Pref::autoExpansionLimit:


Example 9

The IgnoreAnalyticConstraints option applies a set of purely algebraic simplifications including the equality of sum of logarithms and a logarithm of a product. Using the IgnoreAnalyticConstraints option, you get a simpler result, but one that might be incorrect for some of the values of a.

Combine logarithms using the IgnoreAnalyticConstraints option.

combine(ln(a^5) - ln(a^4), ln, IgnoreAnalyticConstraints)

Without using this option, you get a mathematically correct, but long result:

combine(ln(a^5) - ln(a^4), ln)

Example 10

The second argument also can be a list of targets. Then the rewriting rules for each of the targets in the list are applied.

Rewrite ln and sincos terms in the expression.

combine(ln(2) + ln(3) + sin(a)*cos(a), [ln, sincos])

Example 11

combine maps to sets:

combine({sqrt(2)*sqrt(5), sqrt(2)*sqrt(11)})

Example 12

combine maps to the coefficients of polynomials:

combine(poly(sin(x)*cos(x)*y, [y]), sincos)

However, it does not touch the polynomial's indeterminates:

combine(poly(sin(x)*cos(x)), sincos)



An arithmetical expression, an array, a list, a polynomial, or a set


One of the identifiers: arctan, exp, int, gamma, ln, sincos, or sinhcosh



Apply purely algebraic simplifications to an expression. For more information see the options for the Simplify command.

Return Values

Object of the same type as the input object f.

Overloaded By



Advanced users can extend the functionality of combine by implementing additional rewriting rules for other target functions. To extend functionality, define a new slot target of combine. To define a new slot, you need to first unprotect the identifier combine using unprotect. Afterwards, the command combine(f, target) leads to the call combine::target(f) of the corresponding slot routine.

By default, combine handles a subexpression g(x1,x2,...) of f by calling itself recursively for the operands x1, x2, etc. Users can change this behavior for their own mathematical function given by a function environment g by implementing a combine slot of g. To handle the subexpression g(x1,x2,...), combine then calls the slot routine g::combine with the argument sequence x1,x2,... of g.

Introduced in R2007b