# [GiNaC-list] collecting on similar series expansion

Charlls Quarra charlls_quarra at yahoo.com.ar
Tue Jan 30 20:43:33 CET 2007

```--- Charlls Quarra <charlls_quarra at yahoo.com.ar>
escribió:
> now seeing that example (and trying it out) i
> realize
> what was my error; i was calling jacobian.expand()
> and
> assumed that was enough to change the jacobian
> expression, but when i called collect later on the
> (still unexpanded) jacobian ex it wasn't doing the
> job
> at all.

The other problem i had with this program is that
there wasnt a builtin way to get coefficients from
multivariate polynomials, and i thought that i could
do that in a simple manner with collect and subs, but
i got this map_function which does what i want (run
the program):

the degree_list function is really meant to be a
general all-in-once degree/coeff. i think that a
function like this could be more optimal than to check
full ranges between ldegree to degree, just a list
with the available degrees.. just a thought

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

lst degree_list(const ex& poly_ex ,const ex& var)
{
lst result = lst();
ex cf;
for (int i=poly_ex.ldegree( var ) ; i <=
poly_ex.degree( var ) ; i++)
{
cf = poly_ex.coeff( var , i );
if (cf != 0)
{
result.append( lst( i , cf ) );
}
else
{
//cout << " this says that " << poly_ex << " coeff
for degree "<< i <<" is " << cf << endl;
}
}
return result;
};

struct match_terms : public map_function
{
lst l_ex;
exmap my_factors;
lst exp_stack;
match_terms(lst& _l) : l_ex(_l) , my_factors() ,
exp_stack()
{
}

void clear_factors()
{
my_factors.clear();
}

ex operator()(const ex& e)
{
//cout << " Term is " << e << endl;
lst temp_subs;
ex _exp_e = e.expand().normal().expand();
//.to_polynomial(temp_subs).normal();
//cout << " expanding term gives us " << _exp_e <<
endl;
//cout << " where " << temp_subs << endl;
lst ocurrences;
//map< ex , ex > factors;
//factors.clear();
coeff_multi_helper( _exp_e , 0 );
dump_factors();
return e;
}

void dump_factors()
{
exmap::iterator it;
cout << "actual coefficient decomposition: " <<
endl;
for (it = my_factors.begin() ; it !=
my_factors.end() ; it++)
{
cout << " coefficients of term : " << it->first <<
endl;
cout << " ::-->> " << it->second << endl;
}
cout << "no other factors " << endl;
}

void coeff_multi_helper( const ex& e , int var )
{
lst cf;
ex c_;
if (var == l_ex.nops() ) //last frame
{
//cf = degree_list( e , l_ex.op( var ) );
//cout << "CMH: var "<< var << "="<< l_ex.op( var )
<< " expression: " << e << endl;
//cout << "CMH: obtained degree coeffs " << cf <<
endl;
cout << "entering last frame " << endl;
exmap::iterator it;
ex prev_coeff;
ex key = 1;
for (int j=0; j< exp_stack.nops() ; j++)
{
key *= pow( l_ex.op( j ) , exp_stack.op( j ) );
}
cout << " before factor find key " << key << endl;
dump_factors();
it = my_factors.find( key );
if (it == my_factors.end())
{
my_factors[ key ] = collect_common_factors( e );
cout << " coeff of " << key << " <---- " <<
my_factors[ key ] << endl;
cout << "factor dump again: " << endl;
dump_factors();
} else
{
cout << "what the hell we do in here? KEY: " <<
key << " VALUE: "<< it->second << endl;
cout << " wait.. key is " << key << " while " <<
it->first << " but " << endl;
if (key == it->first)
cout << "considered equal! " << endl;
else
cout << " they look diferent " << endl;
prev_coeff = it->second;
my_factors[ key ] = collect_common_factors( e +
prev_coeff );
cout << " coeff of " << key << " <---- " <<
my_factors[ key ] << endl;
dump_factors();
}
}
else
{
cf = degree_list( e , l_ex.op( var ) );
cout << "CMH: var "<< var << "="<< l_ex.op( var )
<< " expression: " << e << endl;
cout << "CMH: obtained degree coeffs " << cf <<
endl;
for (int i=0; i < cf.nops() ; i++ )
{
c_ = cf.op( i );
exp_stack.append( c_.op(0) ); //push stack
coeff_multi_helper( c_.op(1) , var + 1 );
exp_stack.remove_last(); //pop stack
}
}
}
};

int main()
{
cout << latex;
symbol x("x"), y("y");
symbol q("q") , p("p");

cout << "now we should be using map " << endl;
//rord = rord.map( post_tr );
lst l_ex;
l_ex = lst( x , y , p );
//l_ex = lst( F(q) , F(q).diff(q) , F(q).diff(q,2) ,
F(q).diff(q,3) , F(q).diff(q,4) );
match_terms mtch( l_ex );
ex rord = pow( x - y , 3 ) + pow( y - p , 3 ) +
3*p*pow( y , 2 );
rord = mtch( rord.expand() );

};

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

```