Main Content

factor

Description

example

F = factor(x) returns all irreducible factors of x in vector F. If x is an integer, factor returns the prime factorization of x. If x is a symbolic expression, factor returns the subexpressions that are factors of x.

example

F = factor(x,vars) returns an array of factors F, where vars specifies the variables of interest. All factors not containing a variable in vars are separated into the first entry F(1). The other entries are irreducible factors of x that contain one or more variables from vars.

example

F = factor(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments. This syntax can use any of the input arguments from the previous syntaxes.

Examples

Factor Integer Numbers

F = factor(823429252)
F =
           2           2          59         283       12329

To factor integers greater than flintmax, convert the integer to a symbolic object using sym. Then place the number in quotation marks to represent it accurately.

F = factor(sym('82342925225632328'))
F =
[ 2, 2, 2, 251, 401, 18311, 5584781]

To factor a negative integer, convert it to a symbolic object using sym.

F = factor(sym(-92465))
F =
[ -1, 5, 18493]

Perform Prime Factorization of Large Numbers

Perform prime factorization for 41758540882408627201. Since the integer is greater than flintmax, convert it to a symbolic object using sym, and place the number in quotation marks to represent it accurately.

n = sym('41758540882408627201');
factor(n)
ans =
[ 479001599, 87178291199]

Factor Symbolic Fractions

Factor the fraction 112/81 by converting it into a symbolic object using sym.

F = factor(sym(112/81))
F =
[ 2, 2, 2, 2, 7, 1/3, 1/3, 1/3, 1/3]

Factor Polynomials

Factor the polynomial x^6-1.

syms x
F = factor(x^6-1)
F =
[ x - 1, x + 1, x^2 + x + 1, x^2 - x + 1]

Factor the polynomial y^6-x^6.

syms y
F = factor(y^6-x^6)
F =
[ -1, x - y, x + y, x^2 + x*y + y^2, x^2 - x*y + y^2]

Separate Factors Containing Specified Variables

Factor y^2*x^2 for factors containing x.

syms x y
F = factor(y^2*x^2,x)
F =
[ y^2, x, x]

factor combines all factors without x into the first element. The remaining elements of F contain irreducible factors that contain x.

Factor the polynomial y for factors containing symbolic variables b and c.

syms a b c d
y = -a*b^5*c*d*(a^2 - 1)*(a*d - b*c);
F = factor(y,[b c])
F =
[ -a*d*(a - 1)*(a + 1), b, b, b, b, b, c, a*d - b*c]

factor combines all factors without b or c into the first element of F. The remaining elements of F contain irreducible factors of y that contain either b or c.

Choose Factorization Modes

Use the FactorMode argument to choose a particular factorization mode.

Factor an expression without specifying the factorization mode. By default, factor uses factorization over rational numbers. In this mode, factor keeps rational numbers in their exact symbolic form.

syms x
factor(x^3 + 2, x)
ans =
x^3 + 2

Factor the same expression, but this time use numeric factorization over real numbers. This mode factors the expression into linear and quadratic irreducible polynomials with real coefficients and converts all numeric values to floating-point numbers.

factor(x^3 + 2, x, 'FactorMode', 'real')
ans =
[ x + 1.2599210498948731647672106072782,...
  x^2 - 1.2599210498948731647672106072782*x + 1.5874010519681994747517056392723]

Factor this expression using factorization over complex numbers. In this mode, factor reduces quadratic polynomials to linear expressions with complex coefficients. This mode converts all numeric values to floating-point numbers.

factor(x^3 + 2, x, 'FactorMode', 'complex')
ans =
[ x + 1.2599210498948731647672106072782,...
  x - 0.62996052494743658238360530363911 + 1.0911236359717214035600726141898i,...
  x - 0.62996052494743658238360530363911 - 1.0911236359717214035600726141898i]

Factor this expression using the full factorization mode. This mode factors the expression into linear expressions, reducing quadratic polynomials to linear expressions with complex coefficients. This mode keeps rational numbers in their exact symbolic form.

factor(x^3 + 2, x, 'FactorMode', 'full')
ans =
[ x + 2^(1/3),...
  x - 2^(1/3)*((3^(1/2)*1i)/2 + 1/2),...
  x + 2^(1/3)*((3^(1/2)*1i)/2 - 1/2)]

Approximate the result with floating-point numbers by using vpa. Because the expression does not contain any symbolic parameters besides the variable x, the result is the same as in complex factorization mode.

vpa(ans)
ans =
[ x + 1.2599210498948731647672106072782,...
  x - 0.62996052494743658238360530363911 - 1.0911236359717214035600726141898i,...
  x - 0.62996052494743658238360530363911 + 1.0911236359717214035600726141898i]

Approximate Results Containing RootOf

In the full factorization mode,factor also can return results as a symbolic sums over polynomial roots expressed as RootOf.

Factor this expression.

syms x
s = factor(x^3 + x - 3, x, 'FactorMode','full')
s =
[ x - root(z^3 + z - 3, z, 1),...
  x - root(z^3 + z - 3, z, 2),...
  x - root(z^3 + z - 3, z, 3)]

Approximate the result with floating-point numbers by using vpa.

 vpa(s)
ans =
[ x - 1.2134116627622296341321313773815,...
  x + 0.60670583138111481706606568869074 + 1.450612249188441526515442203395i,...
  x + 0.60670583138111481706606568869074 - 1.450612249188441526515442203395i]

Input Arguments

collapse all

Input to factor, specified as a number, or a symbolic number, expression, or function.

Variables of interest, specified as a symbolic variable or a vector of symbolic variables. Factors that do not contain a variable specified in vars are grouped into the first element of F. The remaining elements of F contain irreducible factors of x that contain a variable in vars.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: factor(x^3 - 2,x,'FactorMode','real')

Factorization mode, specified as the comma-separated pair consisting of 'FactorMode' and one of these character vectors.

'rational'Factorization over rational numbers.
'real'Factorization over real numbers. A real numeric factorization is a factorization into linear and quadratic irreducible polynomials with real coefficients. This factorization mode requires the coefficients of the input to be convertible to real floating-point numbers. All other inputs (for example, those inputs containing symbolic or complex coefficients) are treated as irreducible.
'complex'Factorization over complex numbers. A complex numeric factorization is a factorization into linear factors whose coefficients are floating-point numbers. Such factorization is only available if the coefficients of the input are convertible to floating-point numbers, that is, if the roots can be determined numerically. Symbolic inputs are treated as irreducible.
'full'Full factorization. A full factorization is a symbolic factorization into linear factors. The result shows these factors using radicals or as a symsum ranging over a RootOf.

Output Arguments

collapse all

Factors of input, returned as a symbolic vector.

Tips

  • To factor an integer greater than flintmax, wrap the integer with sym. Then place the integer in quotation marks to represent it accurately, for example, sym('465971235659856452').

  • To factor a negative integer, wrap the integer with sym, for example, sym(-3).

Version History

Introduced before R2006a