Tools for Comparing Expressions
Maple 2025 provides innovative new tools for comparing expressions.
ExpressionTools
Have you ever tried to compare two large expressions but you couldn't easily see how they differed? For example:
e1 := 1/4*(2*T[x,j,g]*T[i,c,k]-2*T[x,j,c]*T[i,g,k]-2*d[j](T[x,c,i],[X])+2*d[x](T[c,j,i],[X]))*g_[a,b]+1/4*(T[a,x,b]+T[b,x,a])*T[c,j,i]-1/4*T[b,x,j]*T[c,a,i]-1/2*T[x,j,a]*T[i,b,c]+1/2*T[x,a,b]*T[j,c,i]-1/4*T[a,x,j]*T[c,b,i]+1/4*d[x](T[a,b,c],[X])+1/4*d[x](T[b,a,c],[X])-1/2*d[x](T[c,a,b],[X])-1/4*d[a](T[b,x,c],[X])+1/2*d[b](T[x,a,c],[X])-1/4*d[b](T[a,x,c],[X]);
e1≔2⁢Tx,j,g⁢Ti,c,k−2⁢Tx,j,c⁢Ti,g,k−2⁢dj⁡Tx,c,i,X+2⁢dx⁡Tc,j,i,X⁢g_a,b4+Ta,x,b+Tb,x,a⁢Tc,j,i4−Tb,x,j⁢Tc,a,i4−Tx,j,a⁢Ti,b,c2+Tx,a,b⁢Tj,c,i2−Ta,x,j⁢Tc,b,i4+dx⁡Ta,b,c,X4+dx⁡Tb,a,c,X4−dx⁡Tc,a,b,X2−da⁡Tb,x,c,X4+db⁡Tx,a,c,X2−db⁡Ta,x,c,X4
e2 := 1/4*(2*T[x,j,g]*T[i,c,k]-2*T[x,j,c]*T[i,g,k]+2*d[j](T[i,x,c],[X])-2*d[x](T[j,c,i],[X]))*g_[a,b]+1/4*(T[a,x,b]+T[b,x,a])*T[c,j,i]-1/4*T[b,x,j]*T[c,a,i]-1/2*T[x,j,a]*T[i,b,c]+1/2*T[x,a,b]*T[j,c,i]-1/4*T[a,x,j]*T[c,b,i]+1/4*d[x](T[a,b,c],[X])+1/4*d[x](T[b,a,c],[X])-1/2*d[x](T[c,a,b],[X])-1/4*d[a](T[b,x,c],[X])+1/2*d[b](T[x,a,c],[X])-1/4*d[b](T[a,x,c],[X]);
e2≔2⁢Tx,j,g⁢Ti,c,k−2⁢Tx,j,c⁢Ti,g,k+2⁢dj⁡Ti,x,c,X−2⁢dx⁡Tj,c,i,X⁢g_a,b4+Ta,x,b+Tb,x,a⁢Tc,j,i4−Tb,x,j⁢Tc,a,i4−Tx,j,a⁢Ti,b,c2+Tx,a,b⁢Tj,c,i2−Ta,x,j⁢Tc,b,i4+dx⁡Ta,b,c,X4+dx⁡Tb,a,c,X4−dx⁡Tc,a,b,X2−da⁡Tb,x,c,X4+db⁡Tx,a,c,X2−db⁡Ta,x,c,X4
evalb(e1=e2);
false
The new package ExpressionTools permits you to perform a visual comparison of two expressions.
The main command in this package is Compare, which, given two expressions, compares them and highlights their differences:
with(ExpressionTools);
Compare,Options
Compare(e1, e2);
2⁢Tx,j,g⁢Ti,c,k−2⁢Tx,j,c⁢Ti,g,k−2⁢djx⁡Txj,c,i,X+2⁢dxj⁡Tci,jx,ic,X⁢g_a,b4+Ta,x,b+Tb,x,a⁢Tc,j,i4−Tb,x,j⁢Tc,a,i4−Tx,j,a⁢Ti,b,c2+Tx,a,b⁢Tj,c,i2−Ta,x,j⁢Tc,b,i4+dx⁡Ta,b,c,X4+dx⁡Tb,a,c,X4−dx⁡Tc,a,b,X2−da⁡Tb,x,c,X4+db⁡Tx,a,c,X2−db⁡Ta,x,c,X4
In the above example, the two expressions are combined into one. Differing subexpressions are replaced by a vector containing the two corresponding subexpressions, each highlighted in a different color to indicate which of the original expressions it was a part of.
For smaller expressions (both having length less than 250, an adjustable parameter), the two expressions are printed one after the other, and the differences are simply highlighted by changing their colors:
x1 := (a-b)*(A-B)*c;
x1≔a−b⁢A−B⁢c
x2 := (b-a)*(B-A)*C;
x2≔b−a⁢B−A⁢C
Compare(x1, x2);
a+−1⁢b⁢A+−1⁢B⁢c
b+−1⁢a⁢B+−1⁢A⁢C
If a verification is provided as an option, the comparison ignores any differences it can find that satisfy that verification:
Compare(x1, x2, sign);
a−b⁢A−B⁢c
b−a⁢B−A⁢C
For sums, products, and sets, operands are compared according to the best match, even when none of the top-level subexpressions match exactly:
x3, x4 := a/2+b/3+c/4, c/5+b/6+d/7:
Compare(x3, x4, combine);
a2d7+1316⁢b+1415⁢c
Note in the above that we used the combine option, which forces display in combined format even though the expressions were small.
Here's a larger example, showing the use of some other options to control the display:
x3 := Vector(2,[w(x), z(x)]) = Vector(2,[1/15*exp(-x)*(-15*c__1+3)+1/15*(10*c__2+2)*exp(4*x)-1/3*exp(x), 1/5*exp(-x)*(-1+5*c__1)+1/5*exp(4*x)*(1+5*c__2)]);
x3≔w⁡xz⁡x=ⅇ−x⁢−15⁢c__1+315+10⁢c__2+2⁢ⅇ4⁢x15−ⅇx3ⅇ−x⁢−1+5⁢c__15+ⅇ4⁢x⁢1+5⁢c__25
x4 := Vector(2,[w(x), z(x)]) = Vector(2, [1/15*exp(-x)*(-15*_C1+3)+1/15*(10*_C2+2)*exp(4*x)-1/3*exp(x),1/5*exp(-x)*((1+5*_C2)*exp(5*x)-1+5*_C1)]);
x4≔w⁡xz⁡x=ⅇ−x⁢−15⁢_C1+315+10⁢_C2+2⁢ⅇ4⁢x15−ⅇx3ⅇ−x⁢1+5⁢_C2⁢ⅇ5⁢x−1+5⁢_C15
Compare(x3, x4, Silver, Gold, showvectorbrackets=false, showNULLoperands);
w⁡xz⁡x=ⅇ−x⁢−15⁢c__1_C1+315+10⁢c__2_C2+2⁢ⅇ4⁢x15−ⅇx3ⅇ−x⁢−1+5⁢c__15+ⅇ4−1⁢x⁢1+5⁢_C2⁢ⅇ5⁢x+5⁢c__2_C1+−15
Compare uses the uneval parameter modifier, which means it does not evaluate its arguments before comparing them:
x5 := 'hypergeom'([a,b],[c],x);
x5≔hypergeom⁡a,b,c,x
x6 := 'hypergeom'([b,a],[c],x);
x6≔hypergeom⁡b,a,c,x
evalb(x5=x6);
true
Compare(x5, x6);
hypergeom⁡a,b,c,x
hypergeom⁡b,a,c,x
Note, however, that this could lead to unexpected results:
Compare(x1, subs(C=c, x2), combine, sign);
a−b⁢A−B⁢csubs⁡C=c,x2
So be sure to evaluate results if desired before passing them to Compare:
x3 := subs(C=c, x2);
x3≔b−a⁢B−A⁢c
Compare(x1, x3, combine, sign);
The expressions satisfy the given verification
Or alternatively you can use the evaluate option to force evaluation:
Compare(x5, x6, evaluate);
The expressions are the same
Compare(x1, subs(C=c, x2), sign, evaluate);
A secondary ExpressionTools command called Options allows you to query and set the default values for each option available to the Compare command:
Options();
backgroundcolor : [LightGreen, Pink]
combine : default
combinethreshold : 250
evaluate : false
foregroundcolor : [Green, Red]
highlight : default
showNULLoperands : false
showvectorbrackets : true
verification : boolean
x1 := (2*a-3*b)*(A-B)*c:
x2 := (3*b-2*a)*(B-A)*C:
2⁢a+−3⁢b⁢A+−1⁢B⁢c
3⁢b+−2⁢a⁢B+−1⁢A⁢C
Options(combine, highlight, backgroundcolor);
Options(combine=true, highlight=background, backgroundcolor = [Yellow, Orange]);
combine : true
highlight : background
backgroundcolor : [Yellow, Orange]
2−2⁢a+−33⁢b⁢−1⁢A+−1⁢B⁢cC
Download Help Document