tag:blogger.com,1999:blog-26653368.post3668602590297991459..comments2022-03-05T01:47:39.748+05:30Comments on Tech Musings and Assorted Thoughts: Expression Templates Demystified: Part 2Unknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-26653368.post-76578232704642017862008-12-08T01:45:00.000+05:302008-12-08T01:45:00.000+05:30Well one more important thing that I missed - you ...Well one more important thing that I missed - you will need to change the operator() signature for Constant and also for ComplexExpression:<BR/><BR/><BR/>struct Constant {<BR/>Constant(double d) : d_(d) { }<BR/>Constant(int d) : d_(d) { }<BR/>double operator() (double, double) {<BR/>return d_;<BR/>}<BR/><BR/>double d_;<BR/>};<BR/><BR/>And for the ComplexExpression operator() - it should be like:<BR/><BR/>double operator() (double x, double y) {<BR/>return Op::apply(l_(x, y), r_(x, y));<BR/>}<BR/><BR/>I am yet to test any of this for lack of time; if you find time and are able to ... please let me know. Meanwhile, I will try to put this together for a third part to this series. Thanks a lot!Arindam Mukherjeehttps://www.blogger.com/profile/05966433815221748351noreply@blogger.comtag:blogger.com,1999:blog-26653368.post-31348154288072698352008-12-08T01:24:00.000+05:302008-12-08T01:24:00.000+05:30Hmm, good question and I did ponder about it - if ...Hmm, good question and I did ponder about it - if only a little. I haven't had the time or chance to google it - but off the top of my head what I can think is this:<BR/><BR/>Whenever we talk of a function of N variables, aHmm, good question and I did ponder about it - if only a little. I haven't had the time or chance to google it - but off the top of my head what I can think is this:<BR/><BR/>Whenever we talk of a function of N variables, all our expressions should be modelled in that space. So in an (x,y) plane, the expression <B>x</B> can perhaps be thought of as the complex expression f(x,0) where:<BR/><BR/>f(x,y) = x+y<BR/><BR/>In other words, we would try to model the expression <B>x</B> as a function of two variables. Likewise for everything. A constant would become:<BR/><BR/>f(x,y)=4<BR/><BR/>for all values of <B>x</B> and <B>y</B>. And therefore each and every overloaded operator() should take two double arguments instead of 1. The Variable class could become slightly more complicated:<BR/><BR/>struct XAxis {};<BR/>struct YAxis {};<BR/><BR/>template <class T><BR/>struct Variable {<BR/>};<BR/><BR/>template<><BR/>struct Variable<XAxis> {<BR/>double operator()(double x, double y) {<BR/>return x;<BR/>}<BR/>};<BR/><BR/>template><<BR/>struct Variable<YAxis> {<BR/>double operator()(double x, double y) {<BR/>return y;<BR/>}<BR/>};<BR/><BR/>So your would declare your basic independent variables as:<BR/><BR/>Variable<XAxis> x;<BR/>Variable<YAxis> y;Arindam Mukherjeehttps://www.blogger.com/profile/05966433815221748351noreply@blogger.comtag:blogger.com,1999:blog-26653368.post-79559445478592630232008-12-02T03:23:00.000+05:302008-12-02T03:23:00.000+05:30Hi,Thanks for an informative article. I'm curi...Hi,<BR/><BR/>Thanks for an informative article. I'm curious to know if there is a way to generalize this to multiple variables?<BR/><BR/>Ex :<BR/>Variable x,y;<BR/>cout << (x * y)(2.0); //prints 4.0<BR/><BR/>Is there a way I can make it print 6.0 for :<BR/>cout<< (x * y)(2.0, 3.0);Carlhttps://www.blogger.com/profile/06093900723081172040noreply@blogger.comtag:blogger.com,1999:blog-26653368.post-76468670942137906562008-10-24T23:54:00.000+05:302008-10-24T23:54:00.000+05:30Thanks man, your tutorial is very lucid and easy t...Thanks man, your tutorial is very lucid and easy to understand. I was desperately looking for such a tutorial on expression templates. Thaks again.Raghavender Boorgapallyhttps://www.blogger.com/profile/07056259259994703866noreply@blogger.com