Compare - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


ExpressionTools

  

Compare

  

compare two expressions and display their differences

 

Calling Sequence

Parameters

Description

Examples

Compatibility

Calling Sequence

Compare(expression1, expression2)

Compare(expression1, expression2, ver, color1, color2, opts)

Parameters

expression1

-

any maple expression except an expression sequence

expression2

-

any maple expression except an expression sequence

ver

-

(optional) a verification

color1, color2

-

(optional) a sequence of 2 color names

opts

-

(optional) one or more equations of the form key = value, where key is one of combine, highlight, showvectorbrackets, showNULLoperands, evaluate.

Description

• 

Compare analyzes the pair of expressions expression1 and expression2 and tries to find a best match between their corresponding operands such that in the resulting identification the operands which differ are of minimal size. It then displays these differences using various possible output formats.

• 

For small expressions - both less than length 250 - or when the combine option is set to false, the two expressions will be printed one after the other in the output, with those subexpressions which have been determined to differ between them highlighted using a different color than the rest of the expressions.

• 

Alternatively, for large expressions, or if combine is set to true, the two expressions will be combined into one, in essence glued at subexpressions which match. Where the subexpressions don't match, a vector of the two different corresponding subexpressions will appear instead, again with special colors highlighting the differences.

• 

If a verification ver is supplied as an extra argument, then corresponding subexpressions x1 and x2 of expression1 and expression2 respectively which are found to satisfy verify(x1, x2, ver) are considered by Compare to be equivalent and their differences will thus be ignored.

• 

There are various other options that control the format of the output when the two expressions are found to differ:

• 

In the combined view, the vectors showing corresponding differences can be displayed without parentheses using the showvectorbrackets = false option.

• 

In some cases a subexpression in one of expression1 or expression2 has no corresponding subexpression in the other with which to match; when this happens the vector will have just one entry, with only the color distinguishing whether it was a subexpression of expression1 or expression2. When this output may be confusing, the user can instead supply the additional option showNULLoperands, which forces display of both of the corresponding subexpressions even when one is missing. In this case the vector will contain an invisible entry making the position of the visible entry indicate which of expression1 or expression2 it is a subexpression of.

• 

Supplying color1 and color2 as additional arguments controls the color highlighting for the corresponding subexpressions of expression1 and expression2 which differ.

• 

By default (if the highlight option is not given, or if it is set to default), the foreground colours of subexpressions that differ will be changed for expressions which are small (as defined above). For large expressions, the background colour will be changed instead. By supplying the option highlight=foreground or highlight=background, these defaults can be overriden.

• 

Compare uses the uneval parameter modifier. Providing the evaluate option tells Maple to fully evaluate expression1 and expression2 before computing and displaying their differences.

• 

The default value of each Compare option can be queried and/or reset using the Options command, as can the defaults for the highlight colors and the length which triggers the combined view.

• 

It is not recommended at this time to use this command in Maple's text-based interface.

Examples

withExpressionTools

Compare,Options

(1)

Given two expressions, Compare prints a combined version of both of them with the subexpressions which differ between them highlighted:

x12a3bABc

x12a3bABc

(2)

x23b2aBAC

x23b2aBAC

(3)

Comparex1,x2

2a+−3bA+−1Bc

3b+−2aB+−1AC

(4)

If a verification is included as an extra argument, the comparison will ignore any differences that are found to satisfy that verification:

Comparex1,x2,sign

2a3bABc

3b2aBAC

(5)

If the two input expressions are deemed to be the same (or equivalent under the supplied verification ver), then a message stating that fact is printed instead of printing the identical input expressions with no highlighting modifications.

x3subsC=c,x2

x33b2aBAc

(6)

Comparex1,x3,sign

The expressions satisfy the given verification

(7)

There are many options that can be used to control the form of the output.

With the combine option, the two expressions are merged along subexpressions. Where corresponding subexpressions are the same, they become one; where they differ, a vector of the two different subexpressions appears instead:

Comparex1,x2,combine

2−2a+−33b−1A+−1BcC

(8)

In the combined output, the vector's brackets can also be hidden:

Comparex1,x2,combine,showvectorbrackets=false

2−2a+−33b−1A+−1BcC

(9)

Also the missing operands can be made to take up space, even though they are invisible:

Comparex1,x2,combine,showvectorbrackets=false,showNULLoperands

2−2a+−33b−1A+−1BcC

(10)

The color highlighting can be made to apply to the background instead of the foreground:

Comparex1,x2,combine,showvectorbrackets=false,highlight=background

2−2a+−33b−1A+−1BcC

(11)

The default highlight colors can also be changed:

Comparex1,x2,Gray,Brown

2a+−3bA+−1Bc

3b+−2aB+−1AC

(12)

Comparex1,x2,combine,Silver,Gold,showvectorbrackets=false,highlight=background

2−2a+−33b−1A+−1BcC

(13)

For larger examples (at least one of the expressions has length at least 250), the combine option is the default and so is the background highlighting:

x4,x5Vector2,wx,zx=Vector2,115expx15c__1+3+11510c__2+2exp4x13expx,15expx1+5c__1+15exp4x1+5c__2,Vector2,wx,zx=Vector2,115expx15_C1+3+11510_C2+2exp4x13expx,15expx1+5_C2exp5x1+5_C1:

Comparex4,x5

wxzx=ⅇx15c__1_C1+315+10c__2_C2+2ⅇ4x15ⅇx3ⅇx1+5c__15+ⅇ4−1x1+5_C2ⅇ5x+5c__2_C1+−15

(14)

For sums, products, and sets, the algorithm tries to compare the operands according to the best match, even when none of the top-level subexpressions match exactly:

x6,x7a2+b3+c4,c5+b6+d7:

Comparex6,x7,combine

a2d7+1316b+1415c

(15)

Two expressions (or subexpressions) which are not considered equal in Maple according to evalb, but nevertheless have the same internal structure (such as two rtables which are identical copies of one another) are considered by Compare to be the same and their differences are ignored.

v11,2

v112

(16)

v21,2

v212

(17)

evalbv1=v2

false

(18)

Comparev1,v2

The expressions are the same

(19)

Compare uses the uneval parameter modifier, which means that it does not evaluate its first two arguments x1 and x2 before comparing them, unless they are names, in which case the evaluation is only performed to one level. This is helpful when comparing two expressions which may have unevaluated function calls in them. One of the main use cases of the Compare command is expected to be comparing expected answers with computed answers when testing code changes. Fully evaluating both expected and evaluated answers before comparing them could hide important differences which could signify problems in the code being tested.

x8,x9hypergeoma,b,c,x,hypergeomb,a,c,x

x8,x9hypergeoma,b,c,x,hypergeomb,a,c,x

(20)

Comparex8,x9

hypergeoma,b,c,x

hypergeomb,a,c,x

(21)

If you prefer the input arguments to be fully evaluated, you can provide the evaluate option:

Comparex8,x9,evaluate

The expressions are the same

(22)

Keep in mind, this lack of evaluation by default could lead to unexpected results, such as if we had passed the following arguments to Compare instead of creating the expression x3 above:

Comparex1,subsC=c,x1,sign

2a3bABc

subsC=c,x1

(23)

It is the user's responsibility to evaluate each expression as much as desired before passing them to Compare. Alternatively, they can use the evaluate option.

The default value of each Compare option can be queried and/or reset using the Options command.

Compatibility

• 

The ExpressionTools[Compare] command was introduced in Maple 2025.

• 

For more information on Maple 2025 changes, see Updates in Maple 2025.

See Also

ExpressionTools

ExpressionTools,Options

type/verification

verify