evaluating a simple b-spline function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • happyjack27
    New Member
    • Dec 2007
    • 4

    evaluating a simple b-spline function

    I've searched the net all over.
    All I want to do is generate a cubic b-spline function from a set of 2-d points and evaluate it at different x. I need the kind where the curve runs through the control points (knots). I want to use it for interpolating a cumulative distribution function. I'm looking for two simple c/c++ functions:

    [code=c]
    void calculateBSplin e(some_simple_d ata_type* bspline, double** source_points, int num_points);

    double evaluateBSpline (some_simple_da ta_type* bspline, int num_points, double x);
    [/code]

    Where the double** is an array of points such that the x coordinate of point n is points[n][0], and the y coordinate is points[n][1]. The first function would create the spline from "source_poi nts" and pass it back in "bspline", and the second would evaluate "bspline" at a given point "x". I imagine I can generate the inverse function just by swapping the x and y coordinates of the source_points, so that would provide me with a way of evaluating the spline at y. If not, an inverse eval would also be helpful.

    In sum, my question is simple:

    [code=c]
    void calculateBSplin e(some_simple_d ata_type* bspline, double** source_points, int num_points) {
    <what goes here?>
    }

    double evaluateBSpline (some_simple_da ta_type* bspline, int num_points, double x) {
    <and what goes here?>
    }
    [/code]

    Thanks.
    Last edited by happyjack27; Dec 14 '07, 07:37 PM. Reason: insert code tags
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Note that a double** is not a 2D array. It's a pointer to a pointer, or a pointer to a 1D array. A double*[] is a 2D array. There's a good article in the Howtos on this.

    Comment

    • happyjack27
      New Member
      • Dec 2007
      • 4

      #3
      To be more precise, I'm looking for how to create and evaluate a "natural cubic spline".

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by happyjack27
        To be more precise, I'm looking for how to create and evaluate a "natural cubic spline".
        1. Solve a specific "natural cubic spline" problem and document the steps
        2. Create a general algorithm to solve any regular "natural cubic spline" problems using the documentation from the first step
        3. create pseudocode from the algorithm in step 2
        4. Code the pseudocode from step 3.

        Comment

        • happyjack27
          New Member
          • Dec 2007
          • 4

          #5
          Originally posted by sicarie
          1. Solve a specific "natural cubic spline" problem and document the steps
          2. Create a general algorithm to solve any regular "natural cubic spline" problems using the documentation from the first step
          3. create pseudocode from the algorithm in step 2
          4. Code the pseudocode from step 3.
          Very funny.

          I think step 1 is completely unnecessary, as is step 3 (why code twice?). And step 4 is essentially the same as step 2. So essentially what you're saying is "create a general algorithm to solve any regular "natural cubic spline" problems", which is obvious enough, as that's what I'm trying to do in the first place. These are the steps I would take:

          1. Find the formulas necessary to solve the problem.
          2. Code up the solution in C/C++.
          3. Optimize the code.

          The last step is unnecessary, strictly speaking, but I like to do it anyways.

          In any case, I found what I was looking for on Wikipedia: http://en.wikipedia.org/wiki/Spline_interpolation . Apparently I just had to get a better idea of exactly what it was that I was looking for. For anyone else looking to solve this problem, just take the "Definition " section at the top of the article and combine it with the "Interpolat ion using natural cubic spline" section. That should give you enough information to code up a solution.

          Thanks all for your help.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Glad you got it figured out. If you have a question with the implementation (as we are a Programming Forum...), please feel free to post that question.

            Originally posted by happyjack27
            Very funny.

            I think step 1 is completely unnecessary, as is step 3 (why code twice?).
            And I completely disagree. While you would seem to be of the more intelligent percentage of people in the world who can make the jump, it actually helps develop skills initially to break down the steps.

            Anyway, as I said, if you run into a coding problem, please feel free to post it.

            Comment

            Working...