[GiNaC-list] Ordering and pattern matching

Chris Kelly giltirn at gmail.com
Tue Aug 25 17:12:37 CEST 2009

Dear all,

I am new to GiNaC and am currently trying to get to grips with the clifford
algebra classes. I am trying to perform the simple substitution:  A_mu
gamma_mu B_nu gamma_nu = A_mu A_nu delta_mu,nu (Euclidean space).

The problem i am having is that the pattern match is not consistent over
repeated use of the executable: sometimes the pattern will match, at other
times it will not. It looks to me that this is related to the ordering of
the expressions.

Here is some example code:

using namespace std;
using namespace GiNaC;

//Aim is to identify A_mu gamma_mu B_nu gamma_nu == A_mu B_nu delta_mu,nu
int main(){

  ex metric = unit_matrix(4);
  idx i(symbol("i"),4) , j(symbol("j"),4);
  ex al_i = clifford_unit(i,metric); //Use generalised clifford gamma
matrices for euclidean space
  ex al_j = clifford_unit(j,metric);

  //Create generic 'slashed' symbols with euclidean metric

  ex s1 = indexed(symbol("A"),i)*al_i;
  ex s2 = indexed(symbol("B"),j)*al_j;

  //Form the product
  ex prod = s1*s2;

  cout << s1*s2 << endl;

  //Attempt to match to patterns
  idx w1(wild(1),4), w2(wild(2),4);
  indexed in2_w1 = indexed(wild(2),w1);
  indexed in3_w2 = indexed(wild(3),w2);
  ex al_w1 = clifford_unit(w1,metric);
  ex al_w2 = clifford_unit(w2,metric);

  ex spat2_w1 = in2_w1*al_w1;
  ex spat3_w2 = in3_w2*al_w2;

  ex mpat = spat2_w1 * spat3_w2;
  cout << "MATCHING TO PATTERN: " << mpat << endl;
  cout << prod.match(mpat) << endl;

  ex mpat2 = in2_w1 * in3_w2 * al_w1 * al_w2;
  cout << "MATCHING TO PATTERN: " << mpat2 << endl;
  cout << prod.match(mpat2) << endl;

  ex mpat3 = in3_w2 * in2_w1 * al_w1 * al_w2;
  cout << "MATCHING TO PATTERN: " << mpat3 << endl;
  cout << prod.match(mpat3) << endl;

  return 0;

The output of this example is as follows:

B.j*(e.i*e.j)*A.i    <----- matching this against pattern
MATCHING TO PATTERN: (e.($1)*e.($2))*$3.($2)*$2.($1)
MATCHING TO PATTERN: (e.($1)*e.($2))*$3.($2)*$2.($1)
MATCHING TO PATTERN: (e.($1)*e.($2))*$3.($2)*$2.($1)

So the expressions seem to all be reordered consistently, but in a different
ordering to the object to which we wish to match.

If i then add more code, reproducing the above code for the first pattern
but using different wildcard indices:

  //Create new indices, perhaps hash ordering will be correct for these?

  idx w4(wild(4),4), w5(wild(5),4);
  indexed in2_w4 = indexed(wild(2),w4);
  indexed in3_w5 = indexed(wild(3),w5);
  ex al_w4 = clifford_unit(w4,metric);
  ex al_w5 = clifford_unit(w5,metric);

  ex spat2_w4 = in2_w4*al_w4;
  ex spat3_w5 = in3_w5*al_w5;

  ex mpat4 = spat2_w4 * spat3_w5;
  cout << "MATCHING TO PATTERN: " << mpat4 << endl;
  cout << prod.match(mpat4) << endl;


The output sometimes (but not always) matches:

First run:  (object to match output as  B.j*(e.i*e.j)*A.i  )

MATCHING TO PATTERN: $2.($4)*(e.($4)*e.($5))*$3.($5)

Second run: (object to match output as (e.i*e.j)*B.j*A.i )

MATCHING TO PATTERN: (e.($4)*e.($5))*$3.($5)*$2.($4)

Third run: (object to match output as B.j*(e.i*e.j)*A.i  )  <---- notice
this is the same as the first run

MATCHING TO PATTERN: (e.($4)*e.($5))*$3.($5)*$2.($4)

So for some reason the pattern matching is ordering dependent in a way that
cannot be controlled. Strangely even if the pattern matches
for a given run, the substitution code

  //Pattern sometimes matches, try a substitution for identity

  ex ident = in2_w4 * in3_w5 * delta_tensor(w4,w5);

  cout << "SUBSTITUTION RESULT:" << prod.subs(mpat4==ident) << endl;
prod.subs(mpat4==ident,subs_options::algebraic) << endl;

always seems to fail (or is ignored?)

Second run: (object to match output as (e.i*e.j)*B.j*A.i )


Third run: (object to match output as B.j*(e.i*e.j)*A.i  )


Does anyone know how to solve these issues?

Chris K
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cebix.net/pipermail/ginac-list/attachments/20090825/94fa9ecf/attachment.htm>

More information about the GiNaC-list mailing list