473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

replacement for dynamic template parameter

I have a function that returns {sin(x),x,cos(x)} depending on a
parameter k that can take only 3 integer values: -1, 0, 1.
I am trying to do this with template specialization:

enum curv { negative=-1, zero=0, positive=1 };
template<curv k> double scurv(const double& x);
template<> double scurv<negative>(const double& x) { return sin(x); }
template<> double scurv<zero> (const double& x) { return x;}
template<> double scurv<positive> (const double& x) { return cos(x); }
curv guess_curv(const double& ym)
{
if (ym<0) return negative;
if (ym>0) return positive;
return zero;
}
Next, I would like to use this function in a class:
class Yfunc {

const curv k;
const double& y;

public:

inpa(const double& M, const double& Y)
: k(guess_curv(M+Y)), y(Y) {}

double ez(const double& a) const {
return scurv<k>(x) * pow(y*a, 0.6) ;
}
};
Such that I construct Yfunc once, k is determined, and I do not test
for k each time I call Yfunc::ez.
Template parameters do note allow this dynamical parameter setting .
Is there a smart way to overcome this, yet as performant as template
specialization? I was thinking of function pointer but it was not as
elegant.

Thanks.
Jul 23 '05 #1
6 1892
On 2005-03-28, Marcel Sebbao <se****@yahoo.fr> wrote:
Such that I construct Yfunc once, k is determined, and I do not test
for k each time I call Yfunc::ez.
Template parameters do note allow this dynamical parameter setting .
Is there a smart way to overcome this, yet as performant as template
specialization? I was thinking of function pointer but it was not as
elegant.


Function object + subclassing. Note that however you do runtime selection, if
you are doing the selection at runtime there is inherent overhead in that (if
you use polymorphism or function pointer, you have a pointer deref) So you
don't get the same performance as you would with compile-time template
selection.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
On 2005-03-28, Marcel Sebbao <se****@yahoo.fr> wrote:
Such that I construct Yfunc once, k is determined, and I do not test
for k each time I call Yfunc::ez.
Template parameters do note allow this dynamical parameter setting .
Is there a smart way to overcome this, yet as performant as template
specialization? I was thinking of function pointer but it was not as
elegant.


btw, depending on your usage, it may help to have a member function that
calls your function on a range, e.g. MyFunc(v.begin(),v.end(),result.begin());

Anyway, my point is that there might be a way to abstract the "making multiple
calls" in a way that allows you to perform the indirection only once per usage.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
Marcel Sebbao wrote:
I have a function that returns {sin(x),x,cos(x)} depending on a
parameter k that can take only 3 integer values: -1, 0, 1.
I am trying to do this with template specialization: [snip] Such that I construct Yfunc once, k is determined, and I do not
test for k each time I call Yfunc::ez.


Are you sure that computing transcendentals won't overwhelm
any savings in avoiding the runtime comparison?

--
Quidquid latine dictum sit, altum viditur.
Jul 23 '05 #4
Thanks for the feedback.
I thought the compiler was generating 3 functions and will call it
appropriately only at runtime.

I finally decided on the quickest change: make the Yfunc class a
template class:
template<curv k>
class Yfunc {

const double& y;

public:

inpa(const double& Y)
: y(Y) {}

double ez(const double& a) const {
return scurv<k>(x) * pow(y*a, 0.6) ;
}
};

Then I call it within an if statement.

What is "computing transcendentals"?
Jul 23 '05 #5
Marcel Sebbao wrote:
What is "computing transcendentals"?


Carrying out a process that takes a given number into an
approximation of its image under one of a certain class of
mathematical functions -- to which sine and cosine belong. This can
be quite computationally expensive, so the time the library calls
take is likely to drown out some more comparisons unless your
parameter k selects the identity map in most cases. Profile your code
with runtime comparisons to find out if sin() and cos() are hot
spots. If so, explore alternative implementations with your accuracy
requirements in mind, and if you need help with that, visit
sci.math.num-analysis. If you need help with profiling, visit
comp.programming or a group about your development environment.

http://mathworld.wolfram.com/Transce...lFunction.html

--
Quidquid latine dictum sit, altum viditur.
Jul 23 '05 #6
Well it looks like I had to get rid of the template stuff.
I did some tests, and the computation of transcendental functions is
more costly.
I adopted for pointer to function :(
static inline double identity(double x) { return x; }

class Yfunc {
const double& y;
public:

double (*Sc)(double x);

Yfunc(const double& M, const double& Y): y(Y) {
if (y+M<0) Sc = &sin;
else if (y+M>0) Sc = &cos;
else Sc = &identity;
}

double ez(const double& a) const {
return Sc(a) * pow(y*a, 0.6);
}

};

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: systemutvecklare | last post by:
Hi! I have an application that generates an html-form from an xml-file using an xsl-file. My problem is that I want the xsl to use some "unknown" parameters that I pass to the xslt processor...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
3
by: topmind | last post by:
I am generally new to dot.net, coming from "scriptish" web languages such as ColdFusion and Php. I have a few questions if you don't mind. First, how does one go about inserting dynamic SQL...
2
by: Mark P | last post by:
Still being relatively new to C++ I like to go back to review and rethink old designs to see where I could do things better. The issue I'm currently pondering is static vs. dynamic polymorphism,...
5
by: Daniel Frey | last post by:
Hello I'd like to match a dynamic node, given as a parameter to the stylesheet. Something like: <xsl:stylesheet ...> <xsl:param name="tomatch"/> <xsl:template match="{$tomatch}"> Hallo...
1
by: kanepart2 | last post by:
Hey all , I wanted to create a project or item template in C# such that when I use that template a parameter wizard comes up asking for values. I know how to create the template without...
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.