[GiNaC-list] Re: Question regarding using C-XSC and GiNaC together

Alexei Sheplyakov varg at theor.jinr.ru
Thu Jan 31 06:47:42 CET 2008


Hello!

On Wed, Jan 30, 2008 at 04:08:43PM +0100, Sunayana Ghosh wrote:
> I have a function in C-XSC:
> 
> GTvector F (const GTvector& x)
> {
>  GTvector Result(3);
>  Result[1] = 
> -3.0/4.0*(3.0*sin(2*x[1])+sin(x[1]+x[2])+sin(x[1]+x[3])-sin(x[2]+x[3]));
>  Result[2] = 
> -3.0/4.0*(3.0*sin(2*x[2])+sin(x[1]+x[2])-sin(x[1]+x[3])+sin(x[2]+x[3]));
>  Result[3] = 
> -3.0/4.0*(3.0*sin(2*x[3])-sin(x[1]+x[2])+sin(x[1]+x[3])+sin(x[2]+x[3]));
>  return Result;
> }
> 
> My question is :
> if I want to generalize this function so that it can be used for any 
> three functions f1, f2, f3 belonging to the class "ex" of GiNaC, where
> f1 , f2, f3  are functions of  (t1,t2,t3), where t1, t2 and t3 belong to 
> class "symbol" in GiNaC.

First of all, using GiNaC for floating point arithmetics is not a good
idea. Anyway, if you insist on it,

struct ff 
{
	const ex f0, f1, f2; // should depend on 3 variables
	const lst sym;
	ff(const ex& f0_, const ex& f1_, const ex& f2_, const lst& sym_) :
		 	f0(f0_), f1(f1_), f2(f2_), sym(sym_)
	{ }

	void operator()(GTvector& res, const GTvector& x) const
	{
		lst args;
		args = x[0], x[1], x[2];
		res[0] = f0.subs(sym, args) /* may be .evalf() ? */;
		res[1] = f1.subs(sym, args) /* may be .evalf() ? */;
		res[2] = f2.subs(sym, args) /* may be .evalf() ? */;
		return res;
	}
};

void do_something()
{
	symbol x0("x0"), x1("x1"), x2("x2");
	lst sym;
	sym = x0, x1, x2;
	const ex f0 = (numeric(3)/4)*(3*sin(2*x0) + sin(x0 + x1)
	                            + sin(x0 + x2)  - sin(x1 + x2));
	// can be any function you want

	const ex f1 = (numeric(3)/4)*(3*sin(2*x0) + sin(x0 + x1)
	                            - sin(x0 + x2)  + sin(x1 + x2));

	const ex f2 = (numeric(3)/4)*(3*sin(2*x0) - sin(x0 + x1)
	                            + sin(x0 + x2)  + sin(x1 + x2));
	ff foo(f0, f1, f2, sym);
	GTvector x(3);
	// initialize x
	GTvector y(3);
	foo(y, x);
	// print the result (y) or whatever...
}

Best regards,
	Alexei

-- 
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/20080131/6beb1858/attachment.pgp


More information about the GiNaC-list mailing list