[GiNaC-list] About non commutative transformations in GiNaC

abpetrov abpetrov at ufacom.ru
Sun Jan 22 20:35:56 CET 2017


Hello,
       I wrote a program to do calculations with non commutative
variables in GiNaC.
But when I made attempt to apply noncommutative rule I got error:

terminate called after throwing an instance of 'std::logic_error'
  what():  add::eval(): sum of non-commutative objects has non-zero
numeric term

Below is simple example of program with program output.
How to change the class ncsymbol so that it was possible to apply rules
like a*ap==ap*a+1?


Best regards Petrov A.B.
--------------------------------------------------------------------
Output for program:

Starting: ./test2
e1: a*ap*a
terminate called after throwing an instance of 'std::logic_error'
  what():  add::eval(): sum of non-commutative objects has non-zero
numeric term
*** Crashed with return code: 0 ***


------------------------------------------------------------------

Program:

#include <iostream>
using namespace std;

#include<ginac/ginac.h>
using namespace GiNaC;

class ncsymbol : public symbol
{
	GINAC_DECLARE_REGISTERED_CLASS(ncsymbol, symbol)
public:
	ncsymbol(const string &s);

protected:
	unsigned return_type() const { return
return_types::noncommutative_composite; }

};

GINAC_IMPLEMENT_REGISTERED_CLASS(ncsymbol, symbol)
ncsymbol::ncsymbol() { }
ncsymbol::ncsymbol(const string &s) :  inherited(s) { }

int ncsymbol::compare_same_type(const basic &other) const
{
	GINAC_ASSERT(is_a<ncsymbol>(other));
	const ncsymbol *o = static_cast<const ncsymbol *>(&other);
	if (serial==o->serial) return 0;
	return serial < o->serial ? -1 : 1;
}

void calc_example(){
  ncsymbol a("a"),ap("ap");

  ex e1 = a*ap*a;
  cout << "e1: " << e1 << endl;

  e1 = e1.subs(a*ap==ap*a+1);
  cout << "e1 subs: " << e1 << endl;
}

int main(int argc, char** argv)
{
	calc_example();
	return 0;
}


More information about the GiNaC-list mailing list