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 611861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:49:48+00:00 2026-05-13T17:49:48+00:00

Example: I have a function that works with vectors: double interpolate2d(const vector<double> & xvals,

  • 0

Example:

I have a function that works with vectors:

double interpolate2d(const vector<double> & xvals, const vector<double> & yvals, double xv, double yv, const vector<vector<double> > &fvals) {
    int xhi, xlo, yhi, ylo;
    double xphi, yphi;
    bracketval(xvals,xv,xhi,xlo,xphi);
    bracketval(yvals,yv,yhi,ylo,yphi);
    return (fvals[xhi][yhi]*xphi+fvals[xlo][yhi]*(1.-xphi))*yphi + (fvals[xhi][ylo]*xphi+fvals[xlo][ylo]*(1.-xphi))*(1.-yphi);
}

But now I want to call it with boost::array elements for the first 2 arguments (same with bracketval()), if std::vector and boost::array were self-implemented, I would be able to derive both from a common base class (interface-like) enforcing an implementation of operator[], since both are library-provided, is there any way to cast/specify such a restriction?

I can always resort to plain c-arrays, but it’s not very neat.

Edit: FWIW, here is the original bracketval implementation:

void bracketval(const vector<double> &vals, double v, int &hi, int &lo, double &prophi){
    hi=vals.size()-1;
    lo=0;
    while(abs(hi-lo)>1) {
        int md = (hi+lo)/2;
        if(vals[md]>v) hi=md; else lo=md;
    }
    if(vals[hi]!=vals[lo])
        prophi = (v-vals[lo])/(vals[hi]-vals[lo]);
    else
        prophi = 0.5;

}
  • 1 1 Answer
  • 1 View
  • 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-05-13T17:49:48+00:00Added an answer on May 13, 2026 at 5:49 pm

    This works with std::vector, boost::array, built-in arrays an generally with anything that is indexable. I’ve also included a suggestion of how you should implement the bracketval function:

    template<class Vec>
    void bracketval(Vec const & xvals, double xv, int xhi, int xlo, double xphi)
    {
    }
    
    template <class Vec, class VecOfVecs>
    double interpolate2d(Vec const & xvals, Vec const & yvals, 
                         double xv, double yv,
                         VecOfVecs const & fvals)
    {
        int xhi, xlo, yhi, ylo;
        double xphi, yphi;
        bracketval(xvals,xv,xhi,xlo,xphi);
        bracketval(yvals,yv,yhi,ylo,yphi);
        return (fvals[xhi][yhi]*xphi+fvals[xlo][yhi]*(1.-xphi))
                 *yphi + (fvals[xhi][ylo]*xphi+fvals[xlo][ylo]
                 *(1.-xphi))*(1.-yphi);
    }
    
    int main()
    {
        {
            std::vector<double> v, w;
            std::vector<std::vector<double> > vv;
            interpolate2d(v, w, 1., 2., vv);
        }
        {
            boost::array<double, 4> v, w;
            boost::array<boost::array<double, 4>, 4> vv;
            interpolate2d(v, w, 1., 2., vv);
        }
        {
            double v[4], w[4];
            double vv[4][4];
            interpolate2d(v, w, 1., 2., vv);
        }    
    }
    

    You can even add an extra template parameter if you envision the possibility that the second vector could be of a different type that the first (e.g., the first a vector and the second a boost::array):

    template <class VecX, class VecY, class VecOfVecs>
    double interpolate2d(VecX const & xvals, VecY const & yvals, 
                         double xv, double yv,
                         VecOfVecs const & fvals)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This: CArray <SomeClass> collection; is equivalent to this: CArray <SomeClass,… May 14, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer SWT can do that for you. When you use the… May 14, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer In this example: testCall: function() testCall is now a function… May 14, 2026 at 4:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.