[GiNaC-list] collecting on similar series expansion

Sheplyakov Alexei varg at theor.jinr.ru
Thu Feb 1 19:26:41 CET 2007

On Tue, Jan 30, 2007 at 04:43:33PM -0300, Charlls Quarra wrote:

> The other problem i had with this program is that
> there wasnt a builtin way to get coefficients from
> multivariate polynomials

I use this function to convert a polynomial into an exmap 
(std::map<ex, ex, ex_is_less>):

exmap vargs_collect(const ex& e, const lst& l)
	ex x = e.expand();
	exmap cmap;
	cmap[_ex1] = _ex0;
	for (const_iterator xi=x.begin(); xi!=x.end(); ++xi) {
		ex key = _ex1;
		ex pre_coeff = *xi;
		for (lst::const_iterator li=l.begin(); li!=l.end(); ++li) {
			int cexp = pre_coeff.degree(*li);
			pre_coeff = pre_coeff.coeff(*li, cexp);
			key *= pow(*li, cexp);
		exmap::iterator ci = cmap.find(key);
		if (ci != cmap.end())
			ci->second += pre_coeff;
			cmap.insert(exmap::value_type(key, pre_coeff));
	return cmap;

After such a conversion getting coefficients is trivial:

ex test = pow(x+y+z, 10);
exmap tm = vargs_collect(test, lst(x, y));
cout << tm[pow(x,7)*y] << endl; // 360*z^2

Best regards,

All science is either physics or stamp collecting.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-list/attachments/20070201/de3e8543/attachment.pgp

More information about the GiNaC-list mailing list