Hello List, I am new in GiNaC and in C++ (but very familiar with another CAS and C). I would like to implement a function which returns table. Unless I missed something, there is no ready to use table object in GiNaC. My first thought was to use exhashmap<T> , but I am no more sure about it as I do not see how to proceed. Any hint is welcomes. Thanks in advance, Jerome -- Jerome BENOIT jgmbenoit_at_mailsnare_dot_net
Hi, On Mon, Feb 19, 2007 at 12:29:55AM +0800, Jerome BENOIT wrote:
I am new in GiNaC and in C++ (but very familiar with another CAS and C).
I would like to implement a function which returns table. Unless I missed something, there is no ready to use table object in GiNaC.
I guess you want to use GiNaC::exmap (and [re-]read your favourite C++ textbook, at least the chapter which describes STL containers, in particular associative arrays). For example, #include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; exmap foo(const ex& x, const ex& y) { exmap something; something[x] = x + y/2; something[y] = cos(x); return something; } int main(int argc, char** argv) { symbol x("x"), y("y"); exmap tbl; tbl = foo(x-y, 2*y); for (exmap::const_iterator i=tbl.begin(); i!=tbl.end(); ++i) cout << i->first << " => " << i->second << endl; // will print something like: // 2*y => cos(-y+x) // -y+x => x return 0; } Best regards, Alexei -- All science is either physics or stamp collecting.
Hello, thanks for the quick reply. I am afraid I have to specify my request: by function I meant GiNaC function (the one declared with DECLARE_FUNCTION_<N>P). As the object exmap (or exhashmap<T>) is no an ex object, it cannot be returned by such a function. If I would like to return list, there would be no trouble as lst are well defined (are ex object). My question is rather a GiNaC question than a C++ question. Jerome Sheplyakov Alexei wrote:
Hi,
On Mon, Feb 19, 2007 at 12:29:55AM +0800, Jerome BENOIT wrote:
I am new in GiNaC and in C++ (but very familiar with another CAS and C).
I would like to implement a function which returns table. Unless I missed something, there is no ready to use table object in GiNaC.
I guess you want to use GiNaC::exmap (and [re-]read your favourite C++ textbook, at least the chapter which describes STL containers, in particular associative arrays).
For example,
#include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC;
exmap foo(const ex& x, const ex& y) { exmap something; something[x] = x + y/2; something[y] = cos(x); return something; }
int main(int argc, char** argv) { symbol x("x"), y("y"); exmap tbl; tbl = foo(x-y, 2*y); for (exmap::const_iterator i=tbl.begin(); i!=tbl.end(); ++i) cout << i->first << " => " << i->second << endl; // will print something like: // 2*y => cos(-y+x) // -y+x => x
return 0; }
Best regards, Alexei
-- Jerome BENOIT jgmbenoit_at_mailsnare_dot_net
Hi, On Mon, Feb 19, 2007 at 01:58:10AM +0800, Jerome BENOIT wrote:
I am afraid I have to specify my request: by function I meant GiNaC function (the one declared with DECLARE_FUNCTION_<N>P).
What about a list (GiNaC::lst) of relational objects, e.g. DECLARE_FUNCTION_2P(foo) static ex eval_foo(const ex& x, const ex& y) { lst l; l = x == x + y/2, y == cos(x); return l; } REGISTER_FUNCTION(foo, eval_func(eval_foo))
As the object exmap (or exhashmap<T>) is no an ex object, it cannot be returned by such a function.
Exactly, since most operations with expressions do not make any sense for associative arrays. So I wonder why you want to treat associative arrays as expressions. Best regards, Alexei -- All science is either physics or stamp collecting.
Hello, thank you for the reply. Let be more specific. During some kind of computation I have to compute (multi-indexed) coefficients: 1] these coefficients are long to compute; 2] they are computed by sub set through (consuming) recurrence formulae; 3] these coefficients are meant to be used randomly and very often. Clearly the remember machinery can be use to overcome point [1] and [3]: for GiNaC function this is transparent for the user. Point [2] causes me some difficulty: I want to compute each subset only once. Hence the idea to compute each subset through a GiNaC function (with the remember option) which return tables, a second function (without the remember option) can then call this function and pick up the desired coefficient --- a kind of wrapper function. Another possibility is to write down only one GiNaC function (with remember option) which record not only the desired value (as it is done by the remember machinery) but also the all coefficients of the computed subset. The latter alternative may be good, but unfortunately (unless I missed something in GiNaC code) we can do such a thing. Anyhow, the former approach is better in the current case because some computation may need to deal directly with the subsets. The use of a lst is also a possibility (currently my function returns lst), but I want to pick up rapidly each coefficient. Right now, my guess is that I have to introduce a super class with a exhashmap<SOMETHING> as (principal) member: I thought that there was a better GiNaC way to do so, but apparently not. Otherwise, I thing that using table of expressions makes as sense as using lst of expressions. Best regards, Jerome Sheplyakov Alexei wrote:
Hi,
On Mon, Feb 19, 2007 at 01:58:10AM +0800, Jerome BENOIT wrote:
I am afraid I have to specify my request: by function I meant GiNaC function (the one declared with DECLARE_FUNCTION_<N>P).
What about a list (GiNaC::lst) of relational objects, e.g.
DECLARE_FUNCTION_2P(foo) static ex eval_foo(const ex& x, const ex& y) { lst l; l = x == x + y/2, y == cos(x); return l; } REGISTER_FUNCTION(foo, eval_func(eval_foo))
As the object exmap (or exhashmap<T>) is no an ex object, it cannot be returned by such a function.
Exactly, since most operations with expressions do not make any sense for associative arrays. So I wonder why you want to treat associative arrays as expressions.
Best regards, Alexei
-- Jerome BENOIT jgmbenoit_at_mailsnare_dot_net
On Mon, Feb 19, 2007 at 03:45:03PM +0800, Jerome BENOIT wrote:
Let be more specific.
During some kind of computation I have to compute (multi-indexed) coefficients: 1] these coefficients are long to compute; 2] they are computed by sub set through (consuming) recurrence formulae; 3] these coefficients are meant to be used randomly and very often.
Clearly the remember machinery can be use to overcome point [1] and [3]: for GiNaC function this is transparent for the user.
Point [2] causes me some difficulty: I want to compute each subset only once.
Hence the idea to compute each subset through a GiNaC function (with the remember option) which return tables,
Why do you need _GiNaC_ function to compute coefficients? What's wrong with C++ function, e.g. ex compute(const ex& data) { static exmap remember; exmap::iterator i = remember.find(data); if (i != remember.end()) return i->second; ex result; // actually compute it, making use of compute() itself remember.insert(exmap::value_type(data, result)); return result; }
Right now, my guess is that I have to introduce a super class with a exhashmap<SOMETHING> as (principal) member: I thought that there was a better GiNaC way to do so, but apparently not.
Unless I'm missing something fundamental, this approach seems to be unnecessary complicated. Best regards, Alexei -- All science is either physics or stamp collecting.
participants (2)
-
Jerome BENOIT
-
varg@theor.jinr.ru