TRIG_EXPAND

This function expands trigonometric expressions by applying angle-sum and related expansion identities. It is useful for converting compact forms into additive and multiplicative components.

For example, one key identity is:

\sin(a+b)=\sin(a)\cos(b)+\cos(a)\sin(b)

The deep option controls whether expansion recursively traverses nested subexpressions.

Excel Usage

=TRIG_EXPAND(expression, deep)
  • expression (str, required): Trigonometric expression to expand (expression string).
  • deep (bool, optional, default: true): Apply expansion recursively to nested terms (true/false).

Returns (str): Expanded expression as a string, or an error message.

Example 1: Expands sine of a sum

Inputs:

expression
sin(a + b)

Excel formula:

=TRIG_EXPAND("sin(a + b)")

Expected output:

"sin(a)*cos(b) + sin(b)*cos(a)"

Example 2: Expands cosine of a sum

Inputs:

expression
cos(a + b)

Excel formula:

=TRIG_EXPAND("cos(a + b)")

Expected output:

"-sin(a)*sin(b) + cos(a)*cos(b)"

Example 3: Expands trig factor inside a larger product

Inputs:

expression
sin(x + y)*(x + y)

Excel formula:

=TRIG_EXPAND("sin(x + y)*(x + y)")

Expected output:

"(x + y)*(sin(x)*cos(y) + sin(y)*cos(x))"

Example 4: Disables deep recursion when requested

Inputs:

expression deep
sin((a + b) + c) false

Excel formula:

=TRIG_EXPAND("sin((a + b) + c)", FALSE)

Expected output:

"(-sin(b)*sin(c) + cos(b)*cos(c))*sin(a) + (sin(b)*cos(c) + sin(c)*cos(b))*cos(a)"

Python Code

from sympy import sympify, expand_trig as sympy_expand_trig

def trig_expand(expression, deep=True):
    """
    Expand trigonometric compound-angle expressions into component terms.

    See: https://docs.sympy.org/latest/modules/core.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        expression (str): Trigonometric expression to expand (expression string).
        deep (bool, optional): Apply expansion recursively to nested terms (true/false). Default is True.

    Returns:
        str: Expanded expression as a string, or an error message.
    """
    try:
        expr_text = expression.replace("^", "**") if isinstance(expression, str) else expression
        expr_obj = sympify(expr_text)
        result = sympy_expand_trig(expr_obj, deep=deep)
        return str(result)
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Trigonometric expression to expand (expression string).
Apply expansion recursively to nested terms (true/false).