Symbolic Math Toolbox | ![]() ![]() |
Simple Example
Suppose we want to write an M-file that takes two polynomials or two integers and returns their greatest common divisor. For example, the greatest common divisor of 14 and 21 is 7. The greatest common divisor of x^2-y^2 and x^3-y^3 is x - y.
The first thing we need to know is how to call the greatest common divisor function in Maple. We use the mhelp
function to bring up the Maple online help for the greatest common divisor (gcd
).
gcd - greatest common divisor of polynomials lcm - least common multiple of polynomials Calling Sequence: gcd(a,b,'cofa','cofb') lcm(a,b,...) Parameters: a, b - multivariate polynomials over an algebraic number field or an algebraic function field cofa,cofb - (optional) unevaluated names Description: - The gcd function computes the greatest common divisor of two polynomials a and b. - If the coefficients of a and b are integers, then a primitive unit normal greatest common divisor is returned. In other words, the coefficients of the result are relatively prime integers and the leading coefficient is a positive integer. - If the coefficients of a or b are rational numbers or belong to an algebraic number or function field, then the monic greatest common divisor of a and b is computed. See type,algnum and type,algfun. - Algebraic numbers and functions may be represented by radicals (see type,radical) or with the RootOf notation. See evala. - Names occurring inside a RootOf or a radical are viewed as elements of the coefficient field, provided the RootOf defines an algebraic function. Therefore, they may occur in denominators as well. Other names are not allowed in denominators. - If a or b contains objects that are not algebraic numbers or algebraic functions, these objects will be frozen before the computation proceeds. See frontend. - The RootOf and the radicals defining the algebraic numbers must form an independent set of algebraic quantities, otherwise an error is returned. Note that this condition needs not be satisfied if the expression contains only algebraic numbers in radical notation (i.e. 2^(1/2), 3^(1/2), 6^(1/2)). A basis over Q for the radicals can be computed by Maple in this case. - Since the ordering of the variables depends on the session, the result may also depend on the session when a and b have several variables. - The lcm function computes the least common multiple of an arbitrary number of polynomials. - The optional third argument cofa is assigned the cofactor a/gcd(a,b). - The optional fourth argument cofb is assigned the cofactor b/gcd(a,b). . . .
Since we now know the Maple calling syntax for gcd
, we can write a simple M-file to calculate the greatest common divisor. First, create the M-file gcd
in the @sym
directory and include the commands below.
Now let's extend our function so that we can take the gcd
of two matrices in a pointwise fashion:
function g = gcd(a,b) if any(size(a) ~= size(b)) error('Inputs must have the same size.') end for k = 1: prod(size(a)) g(k) = maple('gcd',a(k), b(k)); end g = reshape(g,size(a));
Running this on some test data
![]() | Using Maple Functions | Vectorized Example | ![]() |