Advertisement
kdaud

Interpolation

Apr 4th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.util.Arrays;
  4.  
  5.  
  6. /**
  7.  * <p>Static methods for doing useful math</p><hr>
  8.  *
  9.  * @author  : $Author: brian $
  10.  * @version : $Revision: 1.1 $
  11.  *
  12.  * <hr><p><font size="-1" color="#336699"><a href="http://www.mbari.org">
  13.  * The Monterey Bay Aquarium Research Institute (MBARI)</a> provides this
  14.  * documentation and code &quot;as is&quot;, with no warranty, express or
  15.  * implied, of its quality or consistency. It is provided without support and
  16.  * without obligation on the part of MBARI to assist in its use, correction,
  17.  * modification, or enhancement. This information should not be published or
  18.  * distributed to third parties without specific written permission from
  19.  * MBARI.</font></p><br>
  20.  *
  21.  * <font size="-1" color="#336699">Copyright 2002 MBARI.<br>
  22.  * MBARI Proprietary Information. All rights reserved.</font><br><hr><br>
  23.  *
  24.  */
  25.  
  26. public class Util{
  27.  
  28.  
  29.     public static final double[] interpLinear(double[] x, double[] y, double[] xi) throws IllegalArgumentException {
  30.  
  31.         if (x.length != y.length) {
  32.             throw new IllegalArgumentException("X and Y must be the same length");
  33.         }
  34.         if (x.length == 1) {
  35.             throw new IllegalArgumentException("X must contain more than one value");
  36.         }
  37.         double[] dx = new double[x.length - 1];
  38.         double[] dy = new double[x.length - 1];
  39.         double[] slope = new double[x.length - 1];
  40.         double[] intercept = new double[x.length - 1];
  41.  
  42.         // Calculate the line equation (i.e. slope and intercept) between each point
  43.         for (int i = 0; i < x.length - 1; i++) {
  44.             dx[i] = x[i + 1] - x[i];
  45.             if (dx[i] == 0) {
  46.                 throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found");
  47.             }
  48.             if (dx[i] < 0) {
  49.                 throw new IllegalArgumentException("X must be sorted");
  50.             }
  51.             dy[i] = y[i + 1] - y[i];
  52.             slope[i] = dy[i] / dx[i];
  53.             intercept[i] = y[i] - x[i] * slope[i];
  54.         }
  55.  
  56.         // Perform the interpolation here
  57.         double[] yi = new double[xi.length];
  58.         for (int i = 0; i < xi.length; i++) {
  59.             if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) {
  60.                 yi[i] = Double.NaN;
  61.             }
  62.             else {
  63.                 int loc = Arrays.binarySearch(x, xi[i]);
  64.                 if (loc < -1) {
  65.                     loc = -loc - 2;
  66.                     yi[i] = slope[loc] * xi[i] + intercept[loc];
  67.                 }
  68.                 else {
  69.                     yi[i] = y[loc];
  70.                 }
  71.             }
  72.         }
  73.  
  74.         return yi;
  75.     }
  76.    
  77.     public static final BigDecimal[] interpLinear(BigDecimal[] x, BigDecimal[] y, BigDecimal[] xi) {
  78.         if (x.length != y.length) {
  79.             throw new IllegalArgumentException("X and Y must be the same length");
  80.         }
  81.         if (x.length == 1) {
  82.             throw new IllegalArgumentException("X must contain more than one value");
  83.         }
  84.         BigDecimal[] dx = new BigDecimal[x.length - 1];
  85.         BigDecimal[] dy = new BigDecimal[x.length - 1];
  86.         BigDecimal[] slope = new BigDecimal[x.length - 1];
  87.         BigDecimal[] intercept = new BigDecimal[x.length - 1];
  88.  
  89.         // Calculate the line equation (i.e. slope and intercept) between each point
  90.         BigInteger zero = new BigInteger("0");
  91.         BigDecimal minusOne = new BigDecimal(-1);
  92.          
  93.         for (int i = 0; i < x.length - 1; i++) {
  94.             //dx[i] = x[i + 1] - x[i];
  95.             dx[i] = x[i + 1].subtract(x[i]);
  96.             if (dx[i].equals(new BigDecimal(zero, dx[i].scale()))) {
  97.                 throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found");
  98.             }
  99.             if (dx[i].signum() < 0) {
  100.                 throw new IllegalArgumentException("X must be sorted");
  101.             }
  102.             //dy[i] = y[i + 1] - y[i];
  103.             dy[i] = y[i + 1].subtract(y[i]);
  104.             //slope[i] = dy[i] / dx[i];
  105.             slope[i] = dy[i].divide(dx[i]);
  106.             //intercept[i] = y[i] - x[i] * slope[i];
  107.             intercept[i] = x[i].multiply(slope[i]).subtract(y[i]).multiply(minusOne);
  108.             //intercept[i] = y[i].subtract(x[i]).multiply(slope[i]);
  109.         }
  110.  
  111.         // Perform the interpolation here
  112.         BigDecimal[] yi = new BigDecimal[xi.length];
  113.         for (int i = 0; i < xi.length; i++) {
  114.             //if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) {
  115.             if (xi[i].compareTo(x[x.length - 1]) > 0 || xi[i].compareTo(x[0]) < 0) {
  116.                 yi[i] = null; // same as NaN
  117.             }
  118.             else {
  119.                 int loc = Arrays.binarySearch(x, xi[i]);
  120.                 if (loc < -1) {
  121.                     loc = -loc - 2;
  122.                     //yi[i] = slope[loc] * xi[i] + intercept[loc];
  123.                     yi[i] = slope[loc].multiply(xi[i]).add(intercept[loc]);
  124.                 }
  125.                 else {
  126.                     yi[i] = y[loc];
  127.                 }
  128.             }
  129.         }
  130.  
  131.         return yi;
  132.     }
  133.  
  134.     public static final double[] interpLinear(long[] x, double[] y, long[] xi) throws IllegalArgumentException {
  135.  
  136.         double[] xd = new double[x.length];
  137.         for (int i = 0; i < x.length; i++) {
  138.             xd[i] = (double) x[i];
  139.         }
  140.  
  141.         double[] xid = new double[xi.length];
  142.         for (int i = 0; i < xi.length; i++) {
  143.             xid[i] = (double) xi[i];
  144.         }
  145.  
  146.         return interpLinear(xd, y, xid);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement