Click here to Skip to main content
13,343,328 members (57,854 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 19 May 2008

A singleton class for numerical integration

, 19 May 2008
Rate this:
Please Sign up or sign in to vote.
A singleton class for numerical integration


Numerical integration along finite element space is one of the most frequently used processes in finite element calculation and is therefore in much needs of adopting computer resources as less as possible. Therefore, I developed a singleton class of numerical quadrature for my finite element library because as a singleton, all construction and destruction processes and process in calculation coordinate and weights of each quadrature point are carried out only one time throughout all finite element calculation process. Besides, the reusability and expendability of this class is also carefully considered.


1. Design

There are many numerical integration methods with different quadrature strategies, different dimensions and different ranks. It maybe one of the most important reason that little consider to implement it as singleton. In this work, the quadrature class is design as a templated class like

template <

unsigned int n1, QuadratureScheme q1=GaussLegendre,

unsigned int n2=0, QuadratureScheme q2=GaussLegendre,

unsigned int n3=0, QuadratureScheme q3=GaussLegendre,

unsigned int n4=0, QuadratureScheme q4=GaussLegendre,

unsigned int n5=0, QuadratureScheme q5=GaussLegendre >

class Quadrature;

Therefore, any specified class is with strong type could become a singleton class. The singleton pattern of S. Meyers[1] is adopted.

2. Reusability

Template functions like

template<typename T, class coordinate> T Integrate( T (*)(coordinate) ) const;

template<typename T, class C, class coordinate> T Integrate( C, T (C::*)(coordinate) ) const;

is adopted. It can be therefore easily be called by any class, any function with any return type and any types of augument.

3. Extendability

Factory pattern of Alexandrescu[2] is adopted to generate various type of one-dimensional quadrature class. Users can add their own derivative class of QuadrarureBase class with no needs of modification in other parts of the program.

4. Limitation

Integral calculation up to five dimensions. Because it is originally developed for finite element software, three dimensional integral is enough.

Integral over range between -1 to 1. It is typical used in finite element calculation. Of course it can be easily extended to any arbitrary ranges.

Using the code

To use this calss, just include file "quadrature.h" into your project.

To do integral calculation, the code would be like

#include <span class="code-keyword"><iostream></span>
#include <span class="code-keyword"><stdlib.h></span>
#include <span class="code-string">"quadrature.h"</span>
using namespace GenericField;
struct QuadratureTest
   double afunction(double x,double y) {return x*x+y*y;}
double bfunction(double x,double y) {return x-y;}
int main(int argc, char *argv[])
  double a=Quadrature< 2,GaussLegendre,2,GaussLegendre >::Instance().Integrate<double>( &bfunction );
  typedef double (QuadratureTest::*PFN)(double,double);
  PFN pfn=&QuadratureTest::afunction;
  QuadratureTest atest;
  double b=Quadrature< 2,GaussLegendre,2,GaussLegendre >::Instance().Integrate( atest, pfn );
  std::cout << "Sucess: " << a << "   "<< b << std::endl;
  return 0;

This sample file and VS2005 project file are included in the compressed file.


1. S. Meyers: Effective C++, Addison Wesley 1997

2. A. Alexandrescu: Modern C++ Design: Generic Programming and Design Patterns Applied, Addison Wesley 2001


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Japan Japan
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 19 May 2008
Article Copyright 2008 by Xi YUAN
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid