Hi guys, Wrinting the code below I can define op() function, but I cant define let_op() (I now, it seems to be a very simple problem...) #include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; class covdiff : public indexed{ GINAC_DECLARE_REGISTERED_CLASS(covdiff,indexed) public: idx index; ex arg; covdiff(const ex, idx) ; ex & let_op(size_t i); size_t nops() const; } ex covdiff::op(size_t i) const { switch (i){ case 0: return arg; case 1: return index; default: throw std::range_error("covdiff::op(): no such operand"); } } ex & covdiff::let_op(size_t i) { ensure_if_modifiable(); switch (i){ case 0: return arg; case 1: return index; <--------error occur here default: throw std::range_error("covdiff::op(): no such operand"); } } test.cpp: In member function `virtual GiNaC::ex& covdiff::let_op(unsigned int) ': test.cpp:88: error: invalid initialization of reference of type 'GiNaC::ex&' from expression of type 'GiNaC::idx' Thanks, Marco ------------------------------------------------- This mail sent through IMP: http://horde.org/imp/
Dear Marco, On Thu, 24 Feb 2005 mdias@ift.unesp.br wrote:
test.cpp:88: error: invalid initialization of reference of type 'GiNaC::ex&' from expression of type 'GiNaC::idx'
It is better to make covdiff::index into an ex. What if somebody is going to initialize a covdiff from a varidx? The varidx will get stripped into an idx. I don't think that is what you want. Best wishes, Chris
Thanks Chris, I modified and compiled ok. but subs() make nothing. I have to delare a new subs() function? Is there in source a example? Thanks a lot Marco class covdiff : public indexed{ GINAC_DECLARE_REGISTERED_CLASS(covdiff,indexed) public: idx index; ex arg; ex new_index;<--------I added this covdiff(const ex, idx) ; ex & let_op(size_t i); size_t nops() const; ex op(size_t i) const;} ex & covdiff::let_op(size_t i) {new_index= index;<--- Get index value ensure_if_modifiable(); switch (i){ case 0: return arg; case 1: return new_index;<--- modified default: throw std::range_error("covdiff::op(): no such operand"); } } Quoting Chris Dams <C.Dams@science.ru.nl>:
Dear Marco,
On Thu, 24 Feb 2005 mdias@ift.unesp.br wrote:
test.cpp:88: error: invalid initialization of reference of type 'GiNaC::ex&' from expression of type 'GiNaC::idx'
It is better to make covdiff::index into an ex. What if somebody is going to initialize a covdiff from a varidx? The varidx will get stripped into an idx. I don't think that is what you want.
Best wishes, Chris
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
------------------------------------------------- This mail sent through IMP: http://horde.org/imp/
Dear Marco, On Thu, 24 Feb 2005 mdias@ift.unesp.br wrote:
Thanks Chris, I modified and compiled ok. but subs() make nothing. I have to delare a new subs() function? Is there in source a example?
Well, you made covdiff inherit from indexed. Indexed already has a .subs() method that comes from the type container that it is based on, so you are actually subsing in the underlying indexed object. You could, of course, add your own .subs()-method, but before you do so, you should ask yourself wheter inheriting from indexed is really what you want to do. If you want to inherit from indexed, why do you need to define your own member variables index and arg? Indexed already has the data structures to hold these. Also, making these public looks like a bad idea. Furthermore, why does this class have a member index and besides that a member new_index? If I were to write such a class, my first inclination would be to indeed try to make it a child class of indexed, but then to simply hold an integer that would hold, say 3, if the indexed object has 3 indices and further indices indicate differentiation. This would behave more or less as expected with respect to summations conventions and so on. Best wishes, Chris
Thanks Chris, I would like that my class covdiff be inherited form indexed. I make this a child class, remove new_index member, but I stupidly cant remove index and arg. I dont have idea to define for example the method compare_same_type, without using this. But the question is: It prints my covdiff ok: D~gamma (A~beta.c) but when a subs(gamma=nu): (A~beta.c)~nu and returns to be a indexed, not a covdiff object. there is a example to write a subs code like this? Thanks a lot, Marco class covdiff : public indexed{ GINAC_DECLARE_REGISTERED_CLASS(covdiff,indexed) public: covdiff(ex arg, ex i1); ex & let_op(size_t i); ex subs(const lst & ls, const lst & lr, unsigned options =0) const; size_t nops() const; ex op(size_t i) const; protected: ex index; ex arg; void do_print(const print_context &c, unsigned level = 0) const; }; covdiff::covdiff(ex arg_, ex i1_): indexed(arg_,i1_),arg(arg_),index(i1_){ tinfo_key = TINFO_covdiff; } int covdiff::compare_same_type(const basic &other) const { GINAC_ASSERT(is_a<covdiff>(other)); const covdiff &o = static_cast<const covdiff &>(other); if(arg != o.arg){ //diferent arg return arg < o.arg ? -1 : 1; } return inherited::compare_same_type(other); }
Well, you made covdiff inherit from indexed. Indexed already has a .subs() method that comes from the type container that it is based on, so you are actually subsing in the underlying indexed object. You could, of course, add your own .subs()-method, but before you do so, you should ask yourself wheter inheriting from indexed is really what you want to do. If you want to inherit from indexed, why do you need to define your own member variables index and arg? Indexed already has the data structures to hold these. Also, making these public looks like a bad idea. Furthermore, why does this class have a member index and besides that a member new_index?
If I were to write such a class, my first inclination would be to indeed try to make it a child class of indexed, but then to simply hold an integer that would hold, say 3, if the indexed object has 3 indices and further indices indicate differentiation. This would behave more or less as expected with respect to summations conventions and so on.
Best wishes, Chris
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
------------------------------------------------- This mail sent through IMP: http://horde.org/imp/
Dear Marco, On Sat, 26 Feb 2005 mdias@ift.unesp.br wrote:
and returns to be a indexed, not a covdiff object. there is a example to write a subs code like this?
The signature of your subs method is not correct. It should be ex subs(const exmap & m, unsigned options = 0) const; Examples of such methods can be found in some of the ginac/*.cpp files in the GiNaC distribution. If you are on some flavour of unix, type grep ::subs *.cpp to find out in which. Best wishes, Chris
participants (2)
-
Chris Dams
-
mdias@ift.unesp.br