Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:14:36+00:00 2026-06-15T10:14:36+00:00

I am writing code to perform Gaussian integration with n points, where n is

  • 0

I am writing code to perform Gaussian integration with n points, where n is a compile time constant.

For a given n, I know how to compute abscissas and weights. The computation has to be done from scratch for each different n.

Now, I do something along these lines:

// Several structs like this one (laguerre, chebyshev, etc).
template <size_t n>
struct legendre
{
    static const size_t size = n;
    static const double x[n];
    static const double w[n];
};

template <typename Rule, typename F>
double gauss_quadrature (F&& f)
{
    double acc = 0;
    for (size_t j = 0; j < Rule::size; j++)
        acc += Rule::w[j] * f (Rule::x[j]);

    return acc;
}

to be used like this:

double i = gauss_quadrature<legendre<12>> (f);

Now, I can specialize in a translation unit the coefficients for legendre<12>, by doing

template <>
const legendre<12>::x[12] = { ... };

template <>
const legendre<12>::w[12] = { ... };

and everything is fine, as long as I only use 12-points Gauss-Legendre.

Now, I’m experimenting with different number of points, and I know how to generate the weights and nodes. I can for instance provide a routine

void compute_legendre_coeffs (size_t n, double* w, double* x);

and :

  • When I call gauss_quadrature<legendre<n>>, the template legendre<n> is automatically instantiated (this is the case).
  • When legendre<n> is instantiated for some compile-time n, I’d like the above compute_legendre_coeffs to be called at some point before main so that it fills the x and w member arrays. How do I achieve this ?

I know must define the arrays first:

template <size_t n>
const double legendre<n>::x[n] = {};

template <size_t n>
const double legendre<n>::w[n] = {};

but I can’t come up with a method to initialize them. Anyone has a trick to do so ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:14:37+00:00Added an answer on June 15, 2026 at 10:14 am
    template <size_t n>
    class legendre
    {
    public:
        static const size_t size = n;
        static const double (&getX())[n] {
           init();
           return x;
        }
        static const double (&getW())[n] {
           init();
           return x;
        }
    private:
        static double x[n];
        static double w[n];
        static void init() {
           static bool _ = do_init(x,y);
        }
        static bool do_init( double *x, double *y ) {
           // do the computation here, use local vars x, y
           return true;
        }
    };
    template <size_t n>
    double legendre<n>::x[n];
    template <size_t n>
    double legendre<n>::w[n];
    

    By providing an accessor you gain control of the entry point to your class. The accessors dispatch to a init function that uses initialization of a local static variable to call do_init only once in the program lifetime. The do_init does the actual initialization of the members.

    Notes:

    Depending on the compiler, this might not be thread safe (i.e. not all C++03 compilers provide thread safe initialization of static variables, which in turn means that the do_init might be called more than once in parallel, depending on the algorithm that might or not be an issue –i.e. if do_init computes the values aside and just writes them, the potential race condition is irrelevant as the net result will be the same). Some compilers offer mechanisms to guarantee one off execution (I believe boost has such a mechanism). Alternatively depending on your domain, you might be able to prime the coefficients before you start the threads.

    The actual arrays cannot be const in this case, as the initialization needs to happen after they are created. That should not be an issue for anything other than possible micro optimizations (i.e. the compiler does not know about the values of the coefficients, so it cannot perform sub-expression evaluation at compile time).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some code to perform explicit linking to a DLL. This code
I am writing code to perform the following steps; Register a .net .dll and
I wrote a code to perform some simple csv formatting but I know it's
I'm writing some code to perform some game theory simulations. My world has several
I am writing code for a real-time program running in an embedded Linux system.
i'm writing some code that basically receive data from socket, perform deserialization and then
When writing code, I often place debug messages in the code. The debug messages
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing code, it is pretty common to request a page with an appended
When writing code in an Eclipse project, I'm usually quite messy and undisciplined in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.