Main Content

rewrite

R2026b

Rewrite expression in terms of another function

Description

R = rewrite(expr,target) rewrites the symbolic expression expr in terms of another function as specified by target. The rewritten expression is mathematically equivalent to the original expression. If expr is a vector or matrix, rewrite acts element-wise on expr.

example

Examples

collapse all

Rewrite any trigonometric function in terms of the exponential function by specifying the target "exp".

syms x
sin2exp = rewrite(sin(x),"exp")
sin2exp = 

e-x i i2-ex i i2

tan2exp = rewrite(tan(x),"exp")
tan2exp = 

-e2 x i i-ie2 x i+1

Rewrite the exponential function in terms of any trigonometric function by specifying the trigonometric function as the target. For a full list of target options, see target.

syms x
exp2sin = rewrite(exp(x),"sin")
exp2sin = 

-2 sin(x i2)2-sin(x i) i+1

exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1),"tan")
exp2tan = 

-tan(x)-i itan(x)+i+itan(x)-itan(x)+i-1

Simplify exp2tan to the expected form by using simplify.

exp2tan = simplify(exp2tan)
exp2tan = tan(x)

Rewrite any trigonometric function in terms of any other trigonometric function by specifying the target. For a full list of target options, see target.

Rewrite tan(x) in terms of the sine function by specifying the target "sin".

syms x
tan2sin = rewrite(tan(x),"sin")
tan2sin = 

-sin(x)2 sin(x2)2-1

Rewrite any inverse trigonometric function in terms of the logarithm function by specifying the target "log". For a full list of target options, see target.

Rewrite acos(x) and acot(x) in terms of the log function.

syms x
acos2log = rewrite(acos(x),"log")
acos2log = -log(x+1-x2 i) i
acot2log = rewrite(acot(x),"log")
acot2log = 

log(1-ix) i2-log(1+ix) i2

Similarly, rewrite the logarithm function in terms of any inverse trigonometric function by specifying the inverse trigonometric function as the target.

Rewrite each element of a matrix by calling rewrite on the matrix.

Rewrite all elements of a matrix in terms of the exp function.

syms x
matrix = [sin(x) cos(x); sinh(x) cosh(x)];
R = rewrite(matrix,"exp")
R = 

(e-x i i2-ex i i2e-x i2+ex i2ex2-e-x2e-x2+ex2)

Rewrite the cosine function in terms of the sine function. Here, the rewrite function rewrites the cosine function using the identity cos(x)=1-2sin(x2)2, which is valid for any x.

syms x
R = rewrite(cos(x),"sin")
R = 

1-2 sin(x2)2

rewrite does not rewrite sin(x) as either -1-cos2(x) or 1-cos2(x) because these expressions are not valid for all x. However, using the square of these expressions to represent sin(x)^2 is valid for all x. Thus, rewrite can rewrite sin(x)^2.

syms x
R1 = rewrite(sin(x),"cos")
R1 = sin(x)
R2 = rewrite(sin(x)^2,"cos")
R2 = 1-cos(x)2

Since R2023a

Find the roots of a polynomial by using root. The result is a column vector in terms of the root function with k = 1, 2, 3, 4, or 5 for the kth root of the polynomial.

syms x
sols = root(x^5 - x^4 - 1,x)
sols = 

(root(x5-x4-1,x,1)root(x5-x4-1,x,2)root(x5-x4-1,x,3)root(x5-x4-1,x,4)root(x5-x4-1,x,5))

Expand the root function in sols by using rewrite with the "expandroot" option. The result is in terms of arithmetic operations such as ^, *, /, +, and – that operate on exact symbolic numbers. Because the expanded result can involve many terms that operate arithmetically, numerical approximation of this result can be inaccurate (due to accumulation of round-off errors).

R = rewrite(sols,"expandroot")
R = 

(12-3 i212+3 i213 σ1+σ1-16 σ1-σ12-3 13 σ1-σ1 i2-16 σ1-σ12+3 13 σ1-σ1 i2)where  σ1=23 108108+121/3

As an alternative, you can numerically approximate sols directly by using vpa to return variable-precision symbolic numbers. The resulting numeric values have the default 32 significant digits, which are more accurate.

solsVpa = vpa(sols)
solsVpa = 

(-0.66235897862237301298045442723905-0.56227951206230124389918214490937 i-0.66235897862237301298045442723905+0.56227951206230124389918214490937 i0.5-0.86602540378443864676372317075294 i0.5+0.86602540378443864676372317075294 i1.3247179572447460259609088544781)

To use sols without Symbolic Math Toolbox™, you can generate code and convert sols to a MATLAB® function by using matlabFunction. The generated file uses the roots function that operates on the numeric double data type.

matlabFunction(sols,"File","myfile");
type myfile
function sols = myfile
%MYFILE
%    SOLS = MYFILE

%    This function was generated by the Symbolic Math Toolbox version 26.2.
%    07-Aug-2026 03:36:09

t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t2 = t0(1);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t3 = t0(2);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t4 = t0(3);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t5 = t0(4);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t6 = t0(5);
sols = [t2;t3;t4;t5;t6];
end

Since R2023a

Find the indefinite integral of a polynomial fraction. The result is in terms of the symsum and root functions as represented by the ∑ and root symbols.

syms x
F = int(1/(x^3 + x - 1),x)
F = 

∑k=13log(root(z3-3 z31-131,z,k) 3 x+root(z3-3 z31-131,z,k) 6 x-9) root(z3-3 z31-131,z,k)

Rewrite the result in terms of arithmetic operations. Because the symbolic summation is the outermost operation, first apply the rewrite function with the "expandsum" option to expand symsum. Then, apply rewrite again with the "expandroot" option to rewrite the root function. The resulting symbolic expression is in terms of arithmetic operations such as ^, *, /, +, and –.

R = rewrite(rewrite(F,"expandsum"),"expandroot")
R = 

log(3 x+6 x-9 σ3 σ3) σ3-log(-3 x-6 x-9 σ2 σ2) σ2-log(-3 x-6 x-9 σ1 σ1) σ1where  σ1=162 σ4+σ42-3 131 σ4-σ4 i2  σ2=162 σ4+σ42+3 131 σ4-σ4 i2  σ3=131 σ4+σ4  σ4=27 119164119164+1621/3

Since R2026b

Create a piecewise function in x- and y-coordinates.

syms f(x,y)
f(x,y) = piecewise(x^2 + y^2 <= 10, x^2 + y^2, x - y)
f(x, y) = 

{x2+y2 if  x2+y2≤10x-y otherwise

Compute the Jacobian matrix of this piecewise function with respect to x and y.

J = jacobian(f)
J(x, y) = 

({2 x if  x2+y2<101 otherwise{2 y if  x2+y2<10-1 otherwise)

The size of the matrix is 1-by-2, where each element is a piecewise function.

size(J(x,y))
ans = 1×2

     1     2

Extract the formula from the Jacobian matrix.

Jformula = formula(J);

You can then access the x and y components of the Jacobian matrix.

Jx = Jformula(1,1)
Jx = 

{2 x if  x2+y2<101 otherwise

Jy = Jformula(1,2)
Jy = 

{2 y if  x2+y2<10-1 otherwise

Rewrite the Jacobian matrix as a single piecewise function with vector values.

Jscalar = rewrite(J,"matrix2piecewise")
Jscalar(x, y) = 

{(2 x2 y) if  x2+y2<10(1-1) otherwise

Extract the values and conditions from the piecewise function by using children.

Jbody = children(Jscalar)
Jbody = 2×2 cell array
    {[2*x    2*y]}    {[x^2 + y^2 < 10]}
    {[1    -1   ]}    {[             1]}

Using this cell array, you can access the vector values and their corresponding conditions. For example, access the first vector value and its condition.

Jval1 = Jbody{1,1}
Jval1 = (2 x2 y)
Jcond1 = Jbody{1,2}
Jcond1 = x2+y2<10

Input Arguments

collapse all

Input to rewrite or replace, specified as a symbolic number, variable, expression, function, vector, matrix, or multidimensional array.

Target function or function to expand, specified as a string scalar or character vector. This table summarizes the rewriting rules for all allowed target options.

TargetRewrite or Replace These FunctionsIn Terms of These Functions
"exp"All trigonometric and hyperbolic functions including inverse functionsexp, log
"log"All inverse trigonometric and hyperbolic functionslog
"sincos"tan, cot, exp, sinh, cosh, tanh, cothsin, cos
"sin", "cos", "tan", "cot"sin, cos, exp, tan, cot, sinh, cosh, tanh, coth except the targetTarget trigonometric function
"sinhcosh"tan, cot, exp, sin, cos, tanh, cothsinh, cosh
"sinh", "cosh", "tanh", "coth"tan, cot, exp, sin, cos, sinh, cosh, tanh, coth except the targetTarget hyperbolic function
"asin", "acos", "atan", "acot"log, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse trigonometric function
"asinh", "acosh", "atanh", "acoth"log, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse hyperbolic function
"sqrt"abs(x + 1i*y)sqrt(x^2 + y^2)
"heaviside"sign, triangularPulse, rectangularPulseheaviside

"expandroot" (since R2023a)

rootArithmetic operations such as ^, *, /, +, and –

"expandsum" (since R2023a)

symsumArithmetic operations such as + and –
"piecewise"abs, heaviside, sign, triangularPulse, rectangularPulsepiecewise

"matrix2piecewise" (since R2026b)

All matrices or vectors whose elements are piecewise functionsA single piecewise function whose values, for each condition, are matrices or vectors

Tips

  • rewrite replaces symbolic function calls in expr with another function as specified by target only if the replacement is mathematically valid. Otherwise, it keeps the original function calls. For example, see Rewrite Between Sine and Cosine Functions.

Version History

Introduced in R2012a

expand all