/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;

import com.approximatrix.draco.math.MatrixSupport;

/*
 * Created on Apr 16, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author jeff
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class InterpolationSupport {
	
	public static final int LINEAR = 1;
	
	public static double[][] Interpolate(double[] x_desired, 
			                             double[][] all_data, 
										 int indep_column, 
										 int method) {
		
		if(method == LINEAR)
			return InterpolateLinear(x_desired, SortAscending(all_data,indep_column), indep_column);
		else
			return null;
		
	}
	
	private static double[][] InterpolateLinear(double[] x_desired, 
            							        double[][] all_data, 
										        int indep_column) {
		double[][] return_val = new double[x_desired.length][all_data[0].length];
		
		for(int i=0;i<x_desired.length;i++) {
			double[] interp_return = InterpolateOneLinear(x_desired[i],all_data,indep_column);
			for(int j=0;j<interp_return.length;j++) {
				return_val[i][j] = interp_return[j];
			}
		}
		
		return return_val;
	}
	
	private static double[] InterpolateOneLinear(double x_desired,
			                                     double[][] all_data,
												 int indep_column) {
		
		double[] return_val = new double[all_data[0].length];
		
		// Check if we're out of bounds
		if(x_desired < all_data[0][indep_column]) {
			for(int j=0;j<return_val.length;j++) {
				if(j == indep_column)
					return_val[j] = x_desired;
				else
					return_val[j] = all_data[0][j];
			}
		} else if(x_desired > all_data[all_data.length-1][indep_column]) {
			for(int j=0;j<return_val.length;j++) {
				if(j == indep_column)
					return_val[j] = x_desired;
				else
					return_val[j] = all_data[all_data.length-1][j];
			}
		} else {
			
			// Perform interpolation
	        int high_index = 1;
			while(high_index < all_data.length && all_data[high_index][indep_column]<x_desired) high_index++;
			
			int low_index = high_index-1;
			double m_run = all_data[high_index][indep_column] - all_data[low_index][indep_column];
			while(m_run == 0) {
				if(high_index<all_data.length-1) high_index++;
				else if(low_index>1) low_index--;
				else return null;
				m_run = all_data[high_index][indep_column] - all_data[low_index][indep_column];
			}
			double xmx0 = x_desired - all_data[low_index][indep_column];
			
			for(int j=0;j<return_val.length;j++) {
				if(j == indep_column)
					return_val[j] = x_desired;
				else {
					return_val[j] = (all_data[high_index][j] - all_data[low_index][j])/m_run*xmx0;
					return_val[j] = return_val[j] + all_data[low_index][j];
				}
			}
		}
		
		return return_val;
		
	}
	
	private static double[][] SortAscending(double[][] in_array, final int indep_column) {
		double[][] ret = MatrixSupport.CopyMatrix(in_array);
		
		java.util.Arrays.sort(ret, new java.util.Comparator<double[]>() {
			public int compare(double[] o1, double[] o2) {
				if(o1[indep_column] > o2[indep_column]) {
					return 1;
				} else if(o1[indep_column] < o2[indep_column]) {
					return -1;
				} else {
					return 0;
				}
			}

		});
		
		return ret;
	}
	
}
