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

Online Help

All Products    Maple    MapleSim


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]);

e12Tx,j,gTi,c,k2Tx,j,cTi,g,k2djTx,c,i,X+2dxTc,j,i,Xg_a,b4+Ta,x,b+Tb,x,aTc,j,i4Tb,x,jTc,a,i4Tx,j,aTi,b,c2+Tx,a,bTj,c,i2Ta,x,jTc,b,i4+dxTa,b,c,X4+dxTb,a,c,X4dxTc,a,b,X2daTb,x,c,X4+dbTx,a,c,X2dbTa,x,c,X4

(1)

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]);

e22Tx,j,gTi,c,k2Tx,j,cTi,g,k+2djTi,x,c,X2dxTj,c,i,Xg_a,b4+Ta,x,b+Tb,x,aTc,j,i4Tb,x,jTc,a,i4Tx,j,aTi,b,c2+Tx,a,bTj,c,i2Ta,x,jTc,b,i4+dxTa,b,c,X4+dxTb,a,c,X4dxTc,a,b,X2daTb,x,c,X4+dbTx,a,c,X2dbTa,x,c,X4

(2)

evalb(e1=e2);

false

(3)
• 

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

(4)

Compare(e1, e2);

2Tx,j,gTi,c,k2Tx,j,cTi,g,k2djxTxj,c,i,X+2dxjTci,jx,ic,Xg_a,b4+Ta,x,b+Tb,x,aTc,j,i4Tb,x,jTc,a,i4Tx,j,aTi,b,c2+Tx,a,bTj,c,i2Ta,x,jTc,b,i4+dxTa,b,c,X4+dxTb,a,c,X4dxTc,a,b,X2daTb,x,c,X4+dbTx,a,c,X2dbTa,x,c,X4

(5)

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;

x1abABc

(6)

x2 := (b-a)*(B-A)*C;

x2baBAC

(7)

Compare(x1, x2);

a+−1bA+−1Bc

b+−1aB+−1AC

(8)
• 

If a verification is provided as an option, the comparison ignores any differences it can find that satisfy that verification:

Compare(x1, x2, sign);

abABc

baBAC

(9)
• 

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+1316b+1415c

(10)

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)]);

x3wxzx=ⅇx15c__1+315+10c__2+2ⅇ4x15ⅇx3ⅇx1+5c__15+ⅇ4x1+5c__25

(11)

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)]);

x4wxzx=ⅇx15_C1+315+10_C2+2ⅇ4x15ⅇx3ⅇx1+5_C2ⅇ5x1+5_C15

(12)

Compare(x3, x4, Silver, Gold, showvectorbrackets=false, showNULLoperands);

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

(13)
• 

Compare uses the uneval parameter modifier, which means it does not evaluate its arguments before comparing them:

x5 := 'hypergeom'([a,b],[c],x);

x5hypergeoma,b,c,x

(14)

x6 := 'hypergeom'([b,a],[c],x);

x6hypergeomb,a,c,x

(15)

evalb(x5=x6);

true

(16)

Compare(x5, x6);

hypergeoma,b,c,x

hypergeomb,a,c,x

(17)
• 

Note, however, that this could lead to unexpected results:

Compare(x1, subs(C=c, x2), combine, sign);

abABcsubsC=c,x2

(18)
• 

So be sure to evaluate results if desired before passing them to Compare:

x3 := subs(C=c, x2);

x3baBAc

(19)

Compare(x1, x3, combine, sign);

The expressions satisfy the given verification

(20)

Or alternatively you can use the evaluate option to force evaluation:

Compare(x5, x6, evaluate);

The expressions are the same

(21)

Compare(x1, subs(C=c, x2), sign, evaluate);

The expressions satisfy the given verification

(22)
• 

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

(23)

x1 := (2*a-3*b)*(A-B)*c:

x2 := (3*b-2*a)*(B-A)*C:

Compare(x1, x2);

2a+−3bA+−1Bc

3b+−2aB+−1AC

(24)

Options(combine, highlight, backgroundcolor);

combine : default

highlight : default

backgroundcolor : [LightGreen, Pink]

(25)

Options(combine=true, highlight=background, backgroundcolor = [Yellow, Orange]);

combine : true

highlight : background

backgroundcolor : [Yellow, Orange]

(26)

Compare(x1, x2);

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

(27)