Question regarding using C-XSC and GiNaC together
Hi, I am a newbie as far as programming in C++ is concerned. I am trying to write a program which uses both packages C-XSC and GiNaC. 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. As in a function where Result[1] = f1(t1,t2,t3) and so on. Would somebody be able to tell me how to go about this. With Regards, Sunayana
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.
On Thu, Jan 31, 2008 at 08:47:42AM +0300, Alexei Sheplyakov wrote:
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; ^^^^^^^^^^^
The return statement is not necessary here and is obviously wrong. -- All science is either physics or stamp collecting.
participants (2)
-
Alexei Sheplyakov
-
Sunayana Ghosh