posts - 64, comments - 569, trackbacks - 4

A C++ Interpolation Class for Tabled Data

I have wrapped the interpolation method using the Fenics Project into a template class. The class is template on the interpolation basis function so that the user may choose the order of interpolation. The class also contains an array of y values so that it may serve as a table of values. Part of this is a map to the variable name for convenience when calling for tabled values.

Since all the work goes into extrapolating to a higher basis function when the Constructor is called, lookups are cheap to retrieve. The cost should be about the level of evaluating a polynomial of the order of the interpolation level (since the weights are known).

An example of it being used:

 

    boost::shared_ptr<Vector> xs (new Vector(n));

    boost::shared_ptr<Vector> ys1 (new Vector(n));

    boost::shared_ptr<Vector> ys2 (new Vector(n));

 

    std::vector<boost::shared_ptr<Vector> > yss;

    std::vector<string> names;

 

    // Populate test data
						

    for(int i=0; i<n; i++)

    {

        xs->setitem(i,i+i*0.1);

        ys1->setitem(i,(*xs)[i]*(*xs)[i]);

        ys2->setitem(i,(*xs)[i]*(*xs)[i]*(*xs)[i]);

    }

 

    yss.push_back(ys1);

    names.push_back("squared");

 

    yss.push_back(ys2);

    names.push_back("cubed");

 

 

 

 

    InterpolationTable<Interpolate3::FunctionSpace> interpTable (xs, yss, names);

 

    std::cout << interpTable.eval(0,2.12) << "\t" << interpTable.eval("squared",2.12) << "\n";

    std::cout << interpTable.eval(1,2.12) << "\t" << interpTable.eval("cubed",2.12) << "\n";

 

 

The code listing:

 

// InterpolationTable.h

// Class for interpolating tabled values using the Fenics Project.

// Charles R. Cook  v1.0  29 Nov 2011

 

// Copyright (c) 2011 Charles R. Cook

// http://www.charlesrcook.com

//

// Permission is hereby granted, free of charge, to any person obtaining a copy

// of this software and associated documentation files (the "Software"), to

// deal in the Software without restriction, including without limitation the

// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or

// sell copies of the Software, and to permit persons to whom the Software is

// furnished to do so, subject to the following conditions:

//

// The above copyright notice and this permission notice shall be included in

// all copies or substantial portions of the Software.

//

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING

// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS

// IN THE SOFTWARE.

 

 

#ifndef _INTERPOLATIONTABLE_H

#define _INTERPOLATIONTABLE_H

 

 

#include <dolfin.h>

 

#include "Interpolate1.h"

#include "Interpolate2.h"

#include "Interpolate3.h"

#include "Interpolate4.h"

 

 

using namespace dolfin;

 

template <class TFunctionSpace>

class InterpolationTable

{

public:

    InterpolationTable(boost::shared_ptr<Vector> xs_in,

            std::vector<boost::shared_ptr<Vector> > ys_in);

    InterpolationTable(boost::shared_ptr<Vector> xs_in,

            std::vector<boost::shared_ptr<Vector> > ys_in,

            std::vector<std::string> names_in);

    double eval(int index, double x);

    double eval(std::string name, double x);

 

    // These vectors contain the raw data

    boost::shared_ptr<Vector> xs;

    boost::shared_ptr<std::vector<boost::shared_ptr<Vector> > > ys;

 

    // The mesh (defined as nodes at x locations)

    boost::shared_ptr<Mesh> mesh;

 

    boost::shared_ptr<Interpolate1::FunctionSpace> V1;

    boost::shared_ptr<TFunctionSpace> V2;

 

    boost::shared_ptr<std::vector<Function> > fy1;

    boost::shared_ptr<std::vector<Function> > fy2;

 

    boost::shared_ptr<std::vector<std::string> > Names;

 

private:

    void Setup(boost::shared_ptr<Vector> xs_in,

            std::vector<boost::shared_ptr<Vector> > ys_in);

    boost::shared_ptr<std::map<std::string,int> > mapping;

 

};

 

template <class TFunctionSpace>

double InterpolationTable<TFunctionSpace>::eval(

        std::string name, double x)

{

    if((*Names).size()==0)

        throw ("names not defined");

 

    // check key exists in map

    if ((*mapping).find(name) == (*mapping).end() )

        throw ("name was not found");

 

 

    int index = (*mapping)[name];

 

    return eval(index, x);

 

}

 

template <class TFunctionSpace>

double InterpolationTable<TFunctionSpace>::eval(

        int index, double x)

{

    if(index >= (*ys).size())

        throw ("index out of range");

 

    if(x < mesh->coordinates()[0] ||

            x > mesh->coordinates()[(*mesh).num_cells()])

        throw ("x location out of range");

 

    return (*fy2)[index](x);

 

}

 

 

template <class TFunctionSpace>

void InterpolationTable<TFunctionSpace>::Setup(

        boost::shared_ptr<Vector> xs_in,

        std::vector<boost::shared_ptr<Vector> > ys_in)

{

 

    // copy the x locations in

    xs = xs_in;

 

    // copy the data in

    for (uint i=0; i<ys_in.size(); i++)

    {

        (*ys)[i] = ys_in[i];

    }

 

    // Setup the mesh to interpolate on

    // Note the count is the number of elements, not nodes

    //  when the interval is constructed

    for(uint i=0; i<xs->size(); i++)

        mesh->coordinates()[i] = (*xs)[i];

 

    // Setup the Function space(s)

    for (uint i=0; i<ys->size(); i++)

    {

        Function fv1(V1,(*ys)[i]);

        fy1->push_back(fv1);

 

        Function fv2(V2);

        fv2.extrapolate(fv1);

 

        fy2->push_back(fv2);

 

    }

}

 

template <class TFunctionSpace>

InterpolationTable<TFunctionSpace>::InterpolationTable(boost::shared_ptr<Vector> xs_in,

        std::vector<boost::shared_ptr<Vector> > ys_in,

        std::vector<std::string> names_in) :

            xs(new Vector(xs_in->size())),

            ys(new std::vector<boost::shared_ptr<Vector> > (ys_in.size())),

            mesh(new Interval(xs_in->size()-1,0,1)),

            V1(new Interpolate1::FunctionSpace(mesh)),

            V2(new TFunctionSpace(mesh)),

            fy1(new std::vector<Function>),

            fy2(new std::vector<Function>),

            Names(new std::vector<std::string>),

            mapping(new std::map<std::string, int>)

{

 

    Setup(xs_in, ys_in);

 

    (*Names) = names_in;

 

    for(uint i=0; i < (*Names).size(); i++)

        (*mapping)[(*Names)[i]] = i;

 

}

 

 

template <class TFunctionSpace>

InterpolationTable<TFunctionSpace>::InterpolationTable(boost::shared_ptr<Vector> xs_in,

        std::vector<boost::shared_ptr<Vector> > ys_in) :

            xs(new Vector(xs_in->size())),

            ys(new std::vector<boost::shared_ptr<Vector> > (ys_in.size())),

            mesh(new Interval(xs_in->size()-1,0,1)),

            V1(new Interpolate1::FunctionSpace(mesh)),

            V2(new TFunctionSpace(mesh)),

            fy1(new std::vector<Function>),

            fy2(new std::vector<Function>),

            Names(new std::vector<std::string>),

            mapping(new std::map<std::string, int>)

 

{

 

    Setup(xs_in, ys_in);

 

}

 

 

 

#endif /* _INTERPOLATIONTABLE_H */

 

Print | posted on Tuesday, November 29, 2011 12:27 PM | Filed Under [ Technical Computing ]

Feedback

Gravatar

# re: A C++ Interpolation Class for Tabled Data

How does the Genetic Traits Data Table and the pedigree chart you created relate to how scientists study human genetics?
12/15/2011 4:14 AM | singing lessons
Gravatar

# re: A C++ Interpolation Class for Tabled Data

In order to warm the kids, numerous parents give their children put on the children version of the ugg boots, both warm and sale uggs followed the trend. Nevertheless, experts pointed out that this uggs retailer is indeed relatively warm shoes for walking or simply is not going to learn to walk, exercise small children, so that far more appropriate shoes. But the soles of shoes and some flexibility is poor, to the 3 a long time of age, exercise more and a lot uggs womens shoes more children, we must choose carefully, and the best parents to choose children more than 3 a long time old soles have some flexibility within the thermal gradient and shoes.
12/20/2011 3:59 AM | adrianaolen
Gravatar

# re: A C++ Interpolation Class for Tabled Data

Local or NFL Jerseys seo (SEO) is typicallyoverlooked within the strategy planning stages of a brand new website online optimization campaign but when truth be told, might be more appropriate for thereforeme businesses than an all-encompassing affiliate marketing blast. So what's local or regional SEO? at the same time asgeneral seo will take a look on the placement of surekeywords at the NFL Jerseys on sale engines like google, it's going to lack deal with keywords that come with an area or regional marker, as an example 'SEO agency' as opposed to 'SEO agency Nottingham' or 'car repairs' as opposed to 'car upkeepNottingham'. an arealy focused campaign will still Premier NFL Jerseys all the similar types of white hat seo techniques this type ofs optimised content and back link building, but will deal withoutshall weand keywords which are attuned to the encompassing area or USA NFL Jerseys catchment zone. the purpose of local SEO is to extfinishonline visibility and logoawareness not on the webas an entire but within the NFL Jerseys On Sale served by the business. This niche techniquewill deliver a a lot better ROI and be better to an organization that may be only capable of service leads within a undeniable radius in their physical location. Local Cheap NFL Jerseys will occasionallytake a miles shorter period of time to turn results than a campaign including short tail keywords as festivalat the terms is less intense. you have to keep in mind smartrule of thumb is the fewer finishing touchper term, the lower the traffic volume. an areaised or regionalised SEO campaign can be just right for you must you offer a Authentic NFL Jerseys - this type ofs building, surveying, mechanic, windows etc. - that make it non-profitcapable of travel too far to do a quote or job. Or, when you have a store selling product or service and do not want to ship nationally or simplyneed to encourage more Replica NFL Jerseys trade, an area campaign will be the solution.
12/22/2011 5:20 AM | city
Gravatar

# re: A C++ Interpolation Class for Tabled Data

As we know well PE Replica designer handbags are extremely weak and sometimes only used one time to hold products on the malls to our homes. So, we often throw away after using those for a really little period. It is a huge litter amount that sums up quickly. The litter of bags in landfills also affects the living environment seriously. The stronger woven woven pp shopping bags may be used thousands of times for lots of applications. sdongp2013
1/4/2012 1:31 AM | Replica designer handbags
Gravatar

# re: A C++ Interpolation Class for Tabled Data

The use of tables is pervasive throughout all communication, research and information analysis. Tables appear in print media, handwritten notes, computer software,architectural ornamentation, traffic signs and dozens other places.
1/11/2012 3:37 AM | funny messages
Gravatar

# uggs canada

Outstanding wellbeing in addition to wellbeing and in many cases in the end, overall flexibility, flexibility and as well sooner or later, an individual pair of a couple of flexible, one pair of flexible, lightweight Nike Air Max comfortable shoes; leisure hours and ultimately, one pair Liang Li Shu-made shoes.nike air max 95 These beautiful casual shoes make you s heart it.air max 95 If you have experience outside of what they ask such a favor of Nike sports shoes, Nike Air Max may be the best of the family.
Celebrity endorsement must be one of the most significant reasons. Well-known female stars from Sarah Jessica Parker to Kate Moss were photographed wearing sheepskin boots all weathers regularly in tabloid magazines.ugg canada Oprah listed them on her favorite things collection even.uggs canada online The second reason for popularity on UGG Clearance boots must be its universal designs. They are for men as well. Stars such as Ronnie Wood, guitarist of Rolling Stones, Leonardo Di Caprio and Justin Timberlake have started wearing these sheepskin boots recently.
1/14/2012 2:37 AM | uggs canada
Gravatar

# re: A C++ Interpolation Class for Tabled Data

Which way you make a decision long evening dresses cheap to carry to existence? Fairy princess? Passionate, Attractive Barbie? Anything is possible, almost everything is ready to suit your needs. Hundreds of costumes evening gown devised for you by gifted designers, skillful sewn by tailors and selected specialists in wedding ceremony fashion. High quality - flawless option - grandiose.
1/15/2012 6:07 AM | boxweddingdress
Gravatar

# re: A C++ Interpolation Class for Tabled Data

You have really imparted useful tips/ knowledge...
1/28/2012 2:15 AM | unihomes 3 noida
Gravatar

# re: A bit more on the personal side …

Lots of timepieces have an finish of life indicator, which helps you to understand when to replace the battery. It only takes a couple of minutes every day to keep your timepieces searching wonderful...invest the time.The innovative frames perfectly portray the mind blowing skills of its Italian craftsmen and supply elegance and style for the wearer. louis vuitton speedy are many of the main components that prompt persons to personal a pair of louis vuitton sale shades. Besides being fashionable, these designer louis vuitton artsy sunglasses supply 100% UV protection.As designs changed to become extra fashionable, you will find people who have to have extra than a pair from the traditional sunglasses. Traditionally, individuals who have eyesight difficulties happen to be required to make use of a pair with the clip ons to cover their prescription eyeglasses.louis vuitton neverfull normally provides the assurance of superior excellent. To keep pace using the altering trends, these louis vuitton luggage are the very best bet. Even Hollywood celebs appreciate donning these masterpieces.
2/21/2012 9:14 PM | louis vuitton sale
Gravatar

# re: A C++ Interpolation Class for Tabled Data

The truth is that louis vuitton store is actually actually quite simple to care for a luxury timepiece. Most of these models are extremely comparable and will require exactly the same attention, so whether or not you own a Louis vuitton sale bags or any other designer piece...these tips will help you to keep you watch looking and running like new.Louis vuitton outlet is synonymous with ultra fashionable and trendy accessories. One of the oldest brands within the fashion world, from the fashion hub - Italy, Louis vuitton is loved and admired by fashion lovers for its diverse and distinctive designs.Whether or not you're element with the globe of high fashion or the average shopper, most of us are familiar with all the name of Gucci. The Home of louis vuitton outlet store, commonly referred to as Gucci, was begun in 1921 by Gucio Gucci in Florence, Italy. louis vuitton factory outlet was identified, for the duration of it early beginnings, for producing top quality leather goods for example high fashion luggage and shoes.
2/21/2012 9:24 PM | tresor paris bracelets

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET