LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2013, 02:33 AM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
Perl script for linear interpolation


May I know the code to apply linear interpolation in my data? For example, I have a data
Code:
#X Y
1 1
3 9
5 25
9 81
By using the linear interpolation in perl, my output should be
Code:
#X Y
1 1
2 5
3 9
4 18
5 25
6 39
7 53
8 67
9 81
Currently I have this code, but it wont run...
Code:
#! /usr/bin/perl -w
use strict;

my $prev_id = 0;
my $prev_val = 0;
my $next_id;
my $next_val;

while (<>)
{
    my ($id, $val) = split;
    for (my $i = $prev_id + 1; $i < $next_id; $i++)
    {
         $val = (($id - $next_id) / ($prev_id - $next_id)) * $prev_val + (($id - $prev_id) / ($next_id - $prev_id)) * $next_val;
         printf ("%d %s\n", $i, $val);
    }
    printf ("%d %s\n", $id, $val);
    ($prev_val, $prev_id) = ($val, $id);
    ($next_val, $next_id) = ($prev_val, $prev_id);  
}
Help me.. Thanks
 
Old 01-16-2013, 03:49 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You have a huge mess in your variables and the logic of your code. You use the values of the variables before you initialize them. You use the wrong variables to hold the wrong values.
What is $val and $id?
What is $prev_val and $prev_id?
What is $next_val and $next_id?

also, you should put a line there to handle the first line (comment)

btw, a very creative way of computing linear interpolation.
 
Old 01-16-2013, 06:25 AM   #3
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
You have a huge mess in your variables and the logic of your code. You use the values of the variables before you initialize them. You use the wrong variables to hold the wrong values.
What is $val and $id?
What is $prev_val and $prev_id?
What is $next_val and $next_id?

also, you should put a line there to handle the first line (comment)

btw, a very creative way of computing linear interpolation.
$ID is the current ID that need to fill in, it probably exists or does not exist.
$val is the current value that need to fill in, it probably exists or does not exist.
$prev_val is the previous existing value.
$prev_ID is the previous existing ID.
$next_val is the next existing value.
$next_ID is the next existing ID.

Any idea?
 
Old 01-16-2013, 06:43 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
As I implied, the problem is you confuse the variables.
OK, let's go through the code, then

Code:
my ($id, $val) = split;
So, you assign the new values to $id and $val.

Code:
for (my $i = $prev_id + 1; $i < $next_id; $i++)
Here, you iterate over $i. What is the purpose of $i, anyway?

Code:
$val = (($id - $next_id) / ($prev_id - $next_id)) * $prev_val + (($id - $prev_id) / ($next_id - $prev_id)) * $next_val;
The basic idea behind this is OK, but how comes nothing here depends on the variable $i you iterate over? You just keep computing this with the same set of values each time.
Also, $next_id and $next_val are not defined yet (at least in the first iteration).

Code:
($prev_val, $prev_id) = ($val, $id);
What are the values of $val and $id at this point?

Code:
($next_val, $next_id) = ($prev_val, $prev_id);
What is your reasoning behind this line?
 
Old 01-16-2013, 07:14 AM   #5
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by millgates View Post
As I implied, the problem is you confuse the variables.
OK, let's go through the code, then

Code:
my ($id, $val) = split;
So, you assign the new values to $id and $val.

Code:
for (my $i = $prev_id + 1; $i < $next_id; $i++)
Here, you iterate over $i. What is the purpose of $i, anyway?

Code:
$val = (($id - $next_id) / ($prev_id - $next_id)) * $prev_val + (($id - $prev_id) / ($next_id - $prev_id)) * $next_val;
The basic idea behind this is OK, but how comes nothing here depends on the variable $i you iterate over? You just keep computing this with the same set of values each time.
Also, $next_id and $next_val are not defined yet (at least in the first iteration).

Code:
($prev_val, $prev_id) = ($val, $id);
What are the values of $val and $id at this point?

Code:
($next_val, $next_id) = ($prev_val, $prev_id);
What is your reasoning behind this line?
$i basically used for the reformed ID.
the main problem is $next_id and $next_val because it depends on the next existing value.
the $val and $id is the existing value and existing ID.
 
Old 01-16-2013, 08:07 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Think about your algorithm a bit.
What you need for each interval, is id and value for both
1) the begining of the interval (you need to remember that from the previous iteration) and
2) the end of the interval (which is what you have just read from the input file).
In the for loop, you interpolate values for all ids between the begining and end of the interval.
In the end you just need to remember the last id and value processed, which happens to be the end of the interval. These will become the begining of the interval for the next iteration.
That's all there is to it. Don't make it more complicated than it needs to be.
 
Old 01-17-2013, 12:10 AM   #7
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Think about your algorithm a bit.
What you need for each interval, is id and value for both
1) the begining of the interval (you need to remember that from the previous iteration) and
2) the end of the interval (which is what you have just read from the input file).
In the for loop, you interpolate values for all ids between the begining and end of the interval.
In the end you just need to remember the last id and value processed, which happens to be the end of the interval. These will become the begining of the interval for the next iteration.
That's all there is to it. Don't make it more complicated than it needs to be.
Here is the updated one... Any Idea?
Code:
#! /usr/bin/perl -w
use strict;

my ($prev_id, $prev_val) = (0,0);

while (<>)
{
  my ($id, $val) = split;
  for( my $temp_id=$prev_id+1; $prev_id>0 && $temp_id<$id; $temp_id++ )
  {
    printf( "*%s is between %s(%s) and %s(%s) <- all numbers required to generate value, use them!\n", $temp_id, $prev_id, $prev_val, $id, $val );
  }
  printf( "%d %s\n", $id, $val );
  ($prev_id, $prev_val) = ($id, $val);
}
 
Old 01-17-2013, 01:59 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
So, what is the problem with it? Doesn't it do what you want?
Where has the interpolation gone?
 
Old 01-17-2013, 02:57 AM   #9
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Angry

Quote:
Originally Posted by millgates View Post
So, what is the problem with it? Doesn't it do what you want?
Where has the interpolation gone?
Partially is what I want, because i do not know how to apply the interpolation...
 
Old 01-17-2013, 03:16 AM   #10
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Same as before, only instead of $id use $temp_id and instead of $next_id use $id
 
Old 01-20-2013, 03:43 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by eminempark View Post
... i do not know how to apply the interpolation...
?????????????

Interpolation is a function/formula. Have you looked up what interpolation is in the first place ? Regardless of Perl.
 
Old 01-21-2013, 12:42 AM   #12
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
?????????????

Interpolation is a function/formula. Have you looked up what interpolation is in the first place ? Regardless of Perl.
I use this awk to solve the linear interpolation. For sure, I understand what is the meaning of linear interpolation.
Code:
awk '
{
  P[$1]=$2
  I[i++]=$1
} 
END{
  j=0; s=I[j]; t=I[j+1]
  for(i=m;i<=n;i++){
    if(I[j+2] && i>t){
      j++; s=I[j]; t=I[j+1]
    }                        
    print i,P[s]+(i-s)*(P[t]-P[s])/(t-s)
  }                                     
}
' m=1001 n=1100 infile
 
  


Reply

Tags
math, perl, text processing



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does vma linear address is strictly inside linear address pointed by task->mm Deep Narayan Dubey Linux - Kernel 0 12-16-2010 11:56 PM
bash for loop in perl - command interpolation casperdaghost Programming 6 03-19-2010 06:39 AM
call perl script through another perl script with arguments nanda22 Linux - Newbie 21 07-21-2009 12:18 AM
LXer: Linear Optimization with the GNU Linear Programming Kit LXer Syndicated Linux News 0 08-12-2006 04:21 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration