-3

So for example we have 1, 5, and 10 and we want to interpolate between these with 12 points, we should get:

1.0000        
1.7273   
2.4545    
3.1818   
3.9091    
4.6364   
5.4545   
6.3636   
7.2727  
8.1818    
9.0909    
10.0000   

say we have 5, 10, and 4 and again 12 points, we should get:

5.0000
5.9091
6.8182
7.7273
8.6364
9.5455
9.4545
8.3636
7.2727
6.1818
5.0909
4.0000
Mini
  • 445
  • 5
  • 17
  • @TheGeneral this seems to only specify a way for two points – Mini Aug 02 '18 at 04:16
  • this might interest you https://stackoverflow.com/questions/30433391/how-can-i-produce-multi-point-linear-interpolation – TheGeneral Aug 02 '18 at 04:19
  • @TheGeneral That seems to use points with (x,y) and (x2,x2). Is there a way to do this with just n numbers? I may be misunderstanding. – Mini Aug 02 '18 at 04:30
  • @MinimumEntropy, if you have only numbers, not points, there are infinite number of possible results. Also your sequences not accurate contain second number. – Slava Utesinov Aug 02 '18 at 06:39
  • Please explain the type of interpolation you want more clearly, as the only way now to know if you want a linear interpolation or some sort of spline interpolation is to plot or *carefully* analyze the numbers you've posted. – Lasse V. Karlsen Aug 02 '18 at 07:25

2 Answers2

-1

The problem is an interpolation of two straight lines with different slopes given the end points and the intersection.

Interpolation is defined as following : In the mathematical field of numerical analysis, interpolation is a method of constructing new data points within the range of a discrete set of known data points.

I'm tired of people giving negative points for solutions to hard problems. This is not a simply problem, but a problem that require "thinking out of the box". lets looks at the solution for following input : 1 12 34

I picked these numbers because the results are all integers

The step size L (Lower) = distance of elements from 1 to 12 = 2

The step size H (Higher) = distance of elements from 12 to 34 = 4

So the answer is : 1 3 5 7 9 11 [12] 14 18 22 26 30 34

Notice the distance between the 6th point 11 and center is 1 (half of L)

Notice the distance between the center point 12 and the 7th point is 2 (half of H)

Finally notice the distance between the 6th and 7th points is 3.

My results are scaled exactly the same as the OPs first example. It is hard to see the sequence with the fractional inputs the OP posted. If you look at the OP first example and calculate the step distance of the first 6 points you get 0.72. The last 6 points the distance is 0.91. Then calculate the distance from the 6th point to the center is .36 (half 0.72). Then center to 7th point 0.45 (half 0.91). Excuse me for rounding the numbers a little bit.


It is a sequence problem just like the in junior high school where you learned arithmetic and geometric sequences. Then as a bonus question you got the sequence 23, 28, 33, 42,51,59,68,77,86 which turns out to be the train stations on the NYC 3rd Ave subway system. Solving problems like this you need to think "Outside the Box" which comes from the tests IBM gives to Job Applicants. These are the people who can solve the Nine Point Problem : http://www.brainstorming.co.uk/puzzles/ninedotsnj.html


I did the results when the number of points is EVEN which in you case is 12. You will need to complete the code if the number of points is ODD.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        const int NUMBER_POINTS = 12;
        static void Main(string[] args)
        {
            List<List<float>> tests = new List<List<float>>() {
                new List<float>() { 1,5, 10},
                new List<float>() { 5,10, 4}
            };

            foreach (List<float> test in tests)
            {
                List<float> output = new List<float>();

                float midPoint = test[1];

                if(NUMBER_POINTS % 2 == 0)
                {
                    //even number of points

                    //add lower numbers
                    float lowerDelta = (test[1] - test[0])/((NUMBER_POINTS / 2) - .5F);
                    for (int i = 0; i < NUMBER_POINTS / 2; i++)
                    {
                        output.Add(test[0] + (i * lowerDelta)); 
                    }
                    float upperDelta = (test[2] - test[1]) / ((NUMBER_POINTS / 2) - .5F); ;
                    for (int i = 0; i < NUMBER_POINTS / 2; i++)
                    {
                        output.Add(test[1] + (i * upperDelta) + (upperDelta / 2F));
                    }
                }
                else
                {
                }

                Console.WriteLine("Numbers = {0}", string.Join("   ", output.Select(x => x.ToString())));

            }
            Console.ReadLine();

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
-1

This is a generalized solution that works by these principles:

  • Performs linear interpolation
  • It calculates a "floating point index" into the input array
  • This index is used to select 1 (if the fractional parts is very close to 0) or 2 numbers from the input array
  • The integer part of this index is the base input array index
  • The fractional part says how far towards the next array element we should move

This should work with whatever size input arrays and output collections you would need.

public IEnumerable<double> Interpolate(double[] inputs, int count)
{
    double maxCountForIndexCalculation = count - 1;
    for (int index = 0; index < count; index++)
    {
        double floatingIndex = (index / maxCountForIndexCalculation) * (inputs.Length - 1);

        int baseIndex = (int)floatingIndex;
        double fraction = floatingIndex - baseIndex;

        if (Math.Abs(fraction) < 1e-5)
            yield return inputs[baseIndex];
        else
        {
            double delta = inputs[baseIndex + 1] - inputs[baseIndex];
            yield return inputs[baseIndex] + fraction * delta;
        }
    }
}

It produces the two collections of outputs you showed in your question but beyond that, I have not tested it. Little error checking is performed so you should add the necessary bits.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • The problem is not a simple interpolation. It is complicated and I don't think this code will work. The solution requires two ranges and different parameters depending on the number of points being even or odd. – jdweng Aug 02 '18 at 20:59
  • That may well be but please provide a test scenario you know/believe is incorrectly handled, ie. input collection + count -> expected output. Merely saying that this doesn't do the right thing isn't enough since it perfectly produces the output from the question, and doesn't help me fix the code either. So comment and downvote aside, I have no idea how to improve on this since no clear direction is given. – Lasse V. Karlsen Aug 03 '18 at 09:00
  • Read my response which gives the details. I updated my response after I made the comment after somebody gave me negative points and after seeing your answer which was wrong. – jdweng Aug 03 '18 at 09:43
  • I see nothing in your answer which invalidates mine. In fact I will venture a guess that your example sequence of 1->12->34 given 12 numbers shouldn't produce the numbers you've produced. The example given by the OP doesn't contain 10, simply because 10 falls between two output numbers, as such I don't believe your sequence should produce 12 either. Given that none of us know 100% what is right here, only the example sequences that the OP posted I still believe my answer is correct. – Lasse V. Karlsen Aug 03 '18 at 09:47
  • I think the OP is using misleading terms though, this isn't strictly interpolation, sampling is probably the right term for his sequences. – Lasse V. Karlsen Aug 03 '18 at 09:49
  • It is a sequence problem just like the in junior high school where you learned arithmetic and geometric sequences. Then as a bonus question you got the sequence 23, 28, 33, 42,51,59,68,77,86 which turns out to be the train stations on the NYC 3rd Ave subway system. Solving problems like this you need to think "Outside the Box" which comes from the tests IBM gives to Job Applicants. These are the people who can solve the Nine Point Problem : http://www.brainstorming.co.uk/puzzles/ninedotsnj.html – jdweng Aug 03 '18 at 10:07
  • It is hard to see the sequence with the fractional inputs the OP posted. If you look at the OP first example and calculate the step distance of the first 6 points you get 0.72. The last 6 points the distance is 0.91. Then calculate the distance from the 6th point to the center is .36 (half 0.72). Then center to 7th point 0.45 (half 0.91). Excuse me for rounding the numbers a little bit. – jdweng Aug 03 '18 at 10:41