complex conjugation

Chris Dams chrisd at sci.kun.nl
Mon Dec 8 11:29:37 CET 2003


Hi!

Here is a patch that adds the operation of complex conjugation to GiNaC.
On strings of gamma matrices, it acts as hermitian conjugation. On
function the default behaviour is to complex conjugate all arguments of
the function. A different behaviour can be specified by using a function
option. The patch also includes my previous patch to for the expansion of
muls and powers, because nobody seems to have read my mail about that yet.

Have a nice day,
Chris Dams
-------------- next part --------------
Index: ginac/basic.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/basic.cpp,v
retrieving revision 1.81
diff -r1.81 basic.cpp
267a268,272
> ex basic::conjugate() const
> {
> 	return *this;
> }
> 
Index: ginac/basic.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/basic.h,v
retrieving revision 1.67
diff -r1.67 basic.h
129a130,132
> 	// complex conjugation
> 	virtual ex conjugate() const;
> 
Index: ginac/clifford.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/clifford.cpp,v
retrieving revision 1.70
diff -r1.70 clifford.cpp
486a487,498
> ex diracgamma5::conjugate() const
> {	return -1*ex(*this);
> }
> 
> ex diracgammaL::conjugate() const
> {	return (new diracgammaR) -> setflag(status_flags::dynallocated);
> }
> 
> ex diracgammaR::conjugate() const
> {	return (new diracgammaL) -> setflag(status_flags::dynallocated);
> }
> 
Index: ginac/clifford.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/clifford.h,v
retrieving revision 1.45
diff -r1.45 clifford.h
106a107,109
> public:
> 	ex conjugate() const;
> 
119a123,125
> public:
> 	ex conjugate() const;
> 
131a138,140
> 
> public:
> 	ex conjugate() const;
Index: ginac/container.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/container.h,v
retrieving revision 1.11
diff -r1.11 container.h
367a368,391
> 	ex conjugate() const
> 	{	STLT*newcont = 0;
> 		for(const_iterator i=seq.begin(); i!=seq.end(); ++i)
> 		{	if(newcont)
> 			{	newcont -> push_back(i->conjugate());
> 				continue;
> 			}
> 			ex x = i->conjugate();
> 			if(are_ex_trivially_equal(x,*i))
> 				continue;
> 			newcont = new STLT;
> 			reserve( *newcont, seq.size() );
> 			for(const_iterator j=seq.begin(); j!=i; ++j)
> 				newcont -> push_back(*j);
> 			newcont -> push_back(x);
> 		}
> 		if(newcont) {
> 			ex result = thiscontainer(*newcont);
> 			delete newcont;
> 			return result;
> 		}
> 		return *this;
> 	}
> 
Index: ginac/ex.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/ex.h,v
retrieving revision 1.73
diff -r1.73 ex.h
134a135,137
> 	// complex conjugation
> 	ex conjugate() const { return bp->conjugate(); }
> 
710a714,716
> 
> inline ex conjugate(const ex & thisex)
> { return thisex.conjugate(); }
Index: ginac/expair.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/expair.cpp,v
retrieving revision 1.3
diff -r1.3 expair.cpp
25a26
> #include "operators.h"
35a37,46
> 
> const expair expair::conjugate() const
>    {  
>       ex newrest=rest.conjugate();
>       ex newcoeff=coeff.conjugate();
>       if(are_ex_trivially_equal(newrest,rest)
>                                     && are_ex_trivially_equal (newcoeff,coeff))
>          return *this;
>       return expair(newrest, newcoeff);
>    }
Index: ginac/expair.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/expair.h,v
retrieving revision 1.27
diff -r1.27 expair.h
87c87,89
< 	
---
> 
> 	const expair conjugate() const;
> 
Index: ginac/expairseq.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/expairseq.cpp,v
retrieving revision 1.73
diff -r1.73 expairseq.cpp
312a313,344
> epvector* conjugateepvector(const epvector&epv)
> {
> 	epvector *newepv = 0;
> 	for(epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i)
> 	{	if(newepv)
> 		{	newepv -> push_back( i->conjugate() );
> 			continue;
> 		}
> 		expair x = i->conjugate();
> 		if( x.is_equal(*i))
> 			continue;
> 		newepv = new epvector; 
> 		newepv -> reserve(epv.size());
> 		for(epvector::const_iterator j=epv.begin(); j!=i; ++j)
> 			newepv -> push_back(*j);
> 		newepv -> push_back(x);
> 	}
> 	return newepv;
> }
> 
> ex expairseq::conjugate() const
> {	
> 	epvector *newepv = conjugateepvector(seq);
> 	ex x = overall_coeff.conjugate();
> 	if( !newepv && are_ex_trivially_equal( x, overall_coeff))
> 		return *this;
> 	ex result=thisexpairseq( newepv ? *newepv : seq , x );
> 	if(newepv)
> 		delete newepv;
> 	return result;
> }
> 
Index: ginac/expairseq.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/expairseq.h,v
retrieving revision 1.43
diff -r1.43 expairseq.h
51a52,55
> /** Complex conjugate every element of an epvector. Returns zero if this
>  *  does not change anything. */
> epvector* conjugateepvector(const epvector&);
> 
79a84
> 	ex conjugate() const;
Index: ginac/function.pl
===================================================================
RCS file: /home/cvs/GiNaC/ginac/function.pl,v
retrieving revision 1.84
diff -r1.84 function.pl
56a57,60
> $typedef_conjugate_funcp=generate(
> 'typedef ex (* conjugate_funcp_${N})(${SEQ1});'."\n",
> 'const ex &','','');
> 
72a77,78
> $conjugate_func_interface=generate('    function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
> 
112a119,124
> $conjugate_switch_statement=generate(
> 	<<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
> 	case ${N}:
> 		return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
> END_OF_DIFF_SWITCH_STATEMENT
> 
156a169,178
> $conjugate_func_implementation=generate(
> 	<<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
> function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
> {
> 	test_and_set_nparams(${N});
> 	conjugate_f = conjugate_funcp(c);
> 	return *this;
> }
> END_OF_CONJUGATE_FUNC_IMPLEMENTATION
> 
229a252
> typedef ex (* conjugate_funcp)();
236a260
> $typedef_conjugate_funcp
245a270
> typedef ex (* conjugate_funcp_exvector)(const exvector &);
266a292
> $conjugate_func_interface
272a299
> 	function_options & conjugate_func(conjugate_funcp_exvector d);
304a332
> 	conjugate_funcp conjugate_f;
321a350
> 	bool conjugate_use_exvector_args;
371a401
> 	ex conjugate() const;
499c529
< 	eval_f = evalf_f = derivative_f = series_f = 0;
---
> 	eval_f = evalf_f = conjugate_f = derivative_f = series_f = 0;
503a534
> 	conjugate_use_exvector_args = false;
531a563
> $conjugate_func_implementation
547a580,585
> function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
> {
> 	conjugate_use_exvector_args = true;
> 	conjugate_f = conjugate_funcp(c);
> 	return *this;
> }
923a962,983
> 		// end of generated lines
> 	}
> 	throw(std::logic_error("function::series(): invalid nparams"));
> }
> 
> /** Implementation of ex::conjugate for functions.
>  *  \@see ex::series */
> ex function::conjugate() const
> {
> 	GINAC_ASSERT(serial<registered_functions().size());
> 	const function_options &opt = registered_functions()[serial];
> 
> 	if (opt.conjugate_f==0) {
> 		return exprseq::conjugate();
> 	}
> 
> 	if (opt.conjugate_use_exvector_args)
> 		return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
> 
> 	switch (opt.nparams) {
> 		// the following lines have been generated for max. ${maxargs} parameters
> ${conjugate_switch_statement}
Index: ginac/idx.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/idx.h,v
retrieving revision 1.49
diff -r1.49 idx.h
194a195,197
> 	// complex conjugation
> 	ex conjugate() const { return toggle_dot(); }
> 
Index: ginac/inifcns.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/inifcns.cpp,v
retrieving revision 1.73
diff -r1.73 inifcns.cpp
71a72,75
> static ex abs_conjugate(const ex & x)
> {	return abs(x);
> }
> 
76c80,81
<                        print_func<print_csrc_double>(abs_print_csrc_float));
---
>                        print_func<print_csrc_double>(abs_print_csrc_float).
>                        conjugate_func(abs_conjugate));
135a141,145
> static ex csgn_conjugate(const ex&x)
> {
> 	return csgn(x);
> }
> 
138c148,149
<                         series_func(csgn_series));
---
>                         series_func(csgn_series).
>                         conjugate_func(csgn_conjugate));
212a224,228
> static ex eta_conjugate(const ex & x, const ex & y)
> {
> 	return -eta(x,y);
> }
> 
217c233,234
<                        set_symmetry(sy_symm(0, 1)));
---
>                        set_symmetry(sy_symm(0, 1)).
>                        conjugate_func(eta_conjugate));
418a436,440
> static ex factorial_conjugate(const ex & x)
> {
> 	return factorial(x);
> }
> 
420c442,443
<                              evalf_func(factorial_evalf));
---
>                              evalf_func(factorial_evalf).
>                              conjugate_func(factorial_conjugate));
438a462,469
> // At the moment the numeric evaluation of a binomail function always
> // gives a real number, but if this would be implemented using the gamma
> // function, also complex conjugation should be changed (or rather, deleted).
> static ex binomial_conjugate(const ex & x, const ex & y)
> {
> 	return binomial(x,y);
> }
> 
440c471,472
<                             evalf_func(binomial_evalf));
---
>                             evalf_func(binomial_evalf).
>                             conjugate_func(binomial_conjugate));
472a505,509
> static ex Order_conjugate(const ex & x)
> {
> 	return Order(x);
> }
> 
477c514,515
<                          latex_name("\\mathcal{O}"));
---
>                          latex_name("\\mathcal{O}").
>                          conjugate_func(Order_conjugate));
Index: ginac/matrix.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/matrix.cpp,v
retrieving revision 1.96
diff -r1.96 matrix.cpp
499a500,524
> // Complex conjugate every matrix entry.
> ex matrix::conjugate() const
> {	exvector * ev = 0;
> 	for(exvector::const_iterator i=m.begin(); i!=m.end(); ++i) {
> 		ex x = i->conjugate();
> 		if(ev) {
> 			ev->push_back(x);
> 			continue;
> 		}
> 		if(are_ex_trivially_equal(x, *i))
> 			continue;
> 		ev = new exvector;
> 		ev->reserve(m.size());
> 		for(exvector::const_iterator j=m.begin(); j!=i; ++j)
> 			ev->push_back(*j);
> 		ev->push_back(x);
> 	}
> 	if(ev) {
> 		ex result = matrix(row, col, *ev);
> 		delete ev;
> 		return result;
> 	}
> 	return *this;
> }
> 
Index: ginac/matrix.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/matrix.h,v
retrieving revision 1.64
diff -r1.64 matrix.h
122a123
> 	ex conjugate() const;
Index: ginac/mul.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/mul.cpp,v
retrieving revision 1.79
diff -r1.79 mul.cpp
920c920
< 			                    setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
---
> 			                    setflag(status_flags::dynallocated));
922,923c922
< 		return ((new add(distrseq))->
< 		        setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
---
> 		return ex((new add(distrseq))->setflag(status_flags::dynallocated)).expand();
Index: ginac/ncmul.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/ncmul.cpp,v
retrieving revision 1.49
diff -r1.49 ncmul.cpp
419a420,436
> ex ncmul::conjugate() const
> {
> 	if(return_type() != return_types::noncommutative)
> 		return exprseq::conjugate();
> 
> 	if(return_type_tinfo() & 0xffffff00U != TINFO_clifford)
> 		return exprseq::conjugate();
> 
> 	exvector ev;
> 	ev.reserve(nops());
> 	for(const_iterator i=end(); i!=begin();) {
> 		--i;
> 		ev.push_back( i->conjugate() );
> 	}
> 	return (new ncmul(ev, true)) -> setflag(status_flags::dynallocated);
> }
> 
Index: ginac/ncmul.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/ncmul.h,v
retrieving revision 1.38
diff -r1.38 ncmul.h
62a63
> 	ex conjugate() const;
Index: ginac/numeric.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/numeric.cpp,v
retrieving revision 1.106
diff -r1.106 numeric.cpp
669a670,676
> ex numeric::conjugate() const
> {	
> 	if(is_real())
> 		return *this;
> 	return real().sub(I.mul(imag()));
> }
> 
Index: ginac/numeric.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/numeric.h,v
retrieving revision 1.67
diff -r1.67 numeric.h
105a106
> 	ex conjugate() const;
Index: ginac/power.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/power.cpp,v
retrieving revision 1.88
diff -r1.88 power.cpp
518a519,528
> ex power::conjugate() const
> {	
> 	ex newbasis=basis.conjugate();
> 	ex newexponent=exponent.conjugate();
> 	if(are_ex_trivially_equal(basis, newbasis)
> 			&& are_ex_trivially_equal(exponent, newexponent))
> 		return *this;
> 	return (new power(newbasis, newexponent)) -> setflag(status_flags::dynallocated);
> }
> 
760,761c770
< 	return (new add(result))->setflag(status_flags::dynallocated |
< 	                                  status_flags::expanded);
---
> 	return ex((new add(result))->setflag(status_flags::dynallocated)).expand();
828c837
< 	return (new add(sum))->setflag(status_flags::dynallocated | status_flags::expanded);
---
> 	return ex((new add(sum))->setflag(status_flags::dynallocated)).expand();
Index: ginac/power.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/power.h,v
retrieving revision 1.47
diff -r1.47 power.h
60a61
> 	ex conjugate() const;
Index: ginac/pseries.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/pseries.cpp,v
retrieving revision 1.72
diff -r1.72 pseries.cpp
409a410,426
> ex pseries::conjugate() const
> {
> 	epvector *newseq = conjugateepvector(seq);
> 	ex newvar = var.conjugate();
> 	ex newpoint = point.conjugate();
> 
> 	if( !newseq
> 			&& are_ex_trivially_equal(newvar, var)
> 			&& are_ex_trivially_equal(point, newpoint) )
> 		return *this;
> 	
> 	ex result=(new pseries( newvar==newpoint, newseq ? *newseq : seq )) -> setflag(status_flags::dynallocated);
> 	if(newseq)
> 		delete newseq;
> 	return result;
> }
> 
Index: ginac/pseries.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/pseries.h,v
retrieving revision 1.35
diff -r1.35 pseries.h
53a54
> 	ex conjugate() const;


More information about the GiNaC-devel mailing list