Contenido principal

gcd

Greatest common divisor of symbolic numbers and polynomials

Description

G = gcd(A) finds the greatest common divisor (GCD) of all symbolic elements in A.

example

G = gcd(A,B) finds the greatest common divisor of A and B.

example

[G,M] = gcd(A) also returns the coefficients M for the linear combination of elements in A that results in G.

example

[G,U,V] = gcd(A,B,X) also returns the Bézout coefficients, U and V, such that G = A*U + B*V. For multivariate polynomials, specify the polynomial variable X such that it does not appear in the denominator of U and V. If you do not specify X, then gcd uses the default variable determined by symvar.

example

Examples

collapse all

Find the greatest common divisor of four integers, specified as elements of a symbolic vector.

A = sym([4420 -128 8984 -488]);
G = gcd(A)
G = 4

Alternatively, specify these values as elements of a symbolic matrix.

A = sym([4420 -128; 8984 -488]);
G = gcd(A)
G = 4

The greatest common divisor of rational numbers a1,a2,,an is a number g, such that a1/g,a2/g,,an/g are integers, and gcd(a1/g,a2/g,,an/g)=1.

Find the greatest common divisor of five rational numbers.

A = sym([1/4 1/3 1/2 2/3 3/4])
A = 

(1413122334)

G = gcd(A)
G = 

112

gcd computes the greatest common divisor of complex numbers over the Gaussian integers (complex numbers with real and imaginary parts that are integers). It returns a complex number with a positive real part and a nonnegative imaginary part.

Find the greatest common divisor of three complex numbers.

A = sym([3 + 1i, 4 - 2i, -5 - 5i]);
G = gcd(A)
G = 3+i

If you specify two input arguments and at least one of them is nonscalar, gcd finds the greatest common divisors element-wise.

Find the greatest common divisors for the elements of two matrices. When you specify two nonscalar input arguments, they must be the same size.

A = sym([309 186; 486 224]);
B = sym([558 444; 1024 1984]);
G = gcd(A,B)
G = 

(36232)

Find the greatest common divisors for the elements of matrix A and the value 200. Here, gcd expands 200 into a 2-by-2 matrix with all elements equal to 200.

G = gcd(A,200)
G = 

(1228)

Find the greatest common divisor of two univariate polynomials.

syms x
A = x^3 - 3*x^2 + 3*x - 1;
B = x^2 - 5*x + 4;
G = gcd(A,B)
G = x-1

Find the greatest common divisor of three multivariate polynomials. Because there are more than two polynomials, specify them as elements of a symbolic vector.

syms x y
A = [x^2*y + x^3, (x + y)^2, x^2 + x*y^2 + x*y + x + y^3 + y];
G = gcd(A)
G = x+y

A theorem in number theory states that the GCD of two numbers is the smallest positive linear combination of those numbers. Show that the GCD of 64 and 44 is a positive linear combination of those numbers.

A = sym([64 44]);
[G,M] = gcd(A)
G = 4
M = (-23)
tf = isequal(G,sum(M.*A))
tf = logical
   1

Find the greatest common divisor and the Bézout coefficients of two polynomials with respect to variable x. When computing Bézout coefficients, gcd ensures that the polynomial variable does not appear in the denominators of these coefficients.

syms x y
[G,U,V] = gcd(x^2*y + x^3, (x + y)^2, x)
G = x+y
U = 

1y2

V = 

1y-xy2

Find the greatest common divisor and the Bézout coefficients of the same polynomials with respect to variable y.

[G,U,V] = gcd(x^2*y + x^3, (x + y)^2, y)
G = x+y
U = 

1x2

V = 0

If you do not specify the polynomial variable, then the toolbox uses symvar to determine the variable.

[G,U,V] = gcd(x^2*y + x^3, (x + y)^2)
G = x+y
U = 

1y2

V = 

1y-xy2

Solve the Diophantine equation (1-z)x+z(1-2z)y=3 for x and y.

Start by defining the coefficients for x and y, and the right side of this Diophantine equation.

syms z
A = 1 - z;
B = z*(1 - 2*z);
R = 3;

Use the syntax [G,U,V] = gcd(A,B) to find the greatest common divisor G of the polynomials A and B and the Bézout coefficients U and V, such that G = A*U + B*V.

[G,U,V] = gcd(A,B)
G = 1
U = 2z+1
V = -1

If the GCD divides the right side of the equation, or in other words, R/G is an integer, then the Diophantine equation has a solution. Use the Bézout coefficients U and V to compute one particular solution and the general solution for the Diophantine equation.

if mod(R,G) == 0
  S = R/G;
  disp("Particular solution:")
  x1 = U*S
  y1 = V*S
  disp("General solution:")
  syms k integer
  x = x1 + B/G*k
  y = y1 - A/G*k
  params = assumptions(k)
else
  disp("This Diophantine equation does not have a solution.")
end
Particular solution:
x1 = 6z+3
y1 = -3
General solution:
x = 6z-kz2z-1+3
y = kz-1-3
params = kZ

Finally, confirm that the general solution satisfies the Diophantine equation.

eqCheck = simplify(A*x + B*y) == R
eqCheck = 3=3

Input Arguments

collapse all

Input value, specified as a symbolic number, variable, expression, function, or a vector or matrix of symbolic numbers, variables, expressions, or functions.

Input value, specified as a symbolic number, variable, expression, function, or a vector or matrix of symbolic numbers, variables, expressions, or functions.

If A and B are nonscalar, they must have the same size. If either A or B is scalar, then gcd expands the scalar into a vector or matrix of the same size as the nonscalar argument, with all elements equal to the corresponding scalar.

Polynomial variable, specified as a symbolic variable. By default, gcd uses the variable determined by symvar.

Output Arguments

collapse all

Greatest common divisor, returned as a symbolic number, variable, expression, function, or a vector or matrix of symbolic numbers, variables, expressions, or functions.

Coefficients for the linear combination of elements in input A that equals GCD of input, returned as a symbolic vector, matrix, or array.

Bézout coefficients, returned as symbolic numbers, variables, expressions, functions, or vectors or matrices of symbolic numbers, variables, expressions, or functions.

Tips

  • Calling gcd with numbers that are not symbolic objects invokes the MATLAB® gcd function.

  • The MATLAB gcd function does not accept rational or complex arguments. To find the greatest common divisor of rational or complex numbers, convert these numbers to symbolic objects by using sym, and then use gcd.

Version History

Introduced in R2014b

See Also