<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.strata.math.impl.interpolation;

import java.util.Set;

import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.Property;
import org.joda.beans.impl.light.LightMetaBean;

import com.opengamma.strata.math.impl.MathException;
import com.opengamma.strata.math.impl.interpolation.data.ArrayInterpolator1DDataBundle;
import com.opengamma.strata.math.impl.interpolation.data.Interpolator1DDataBundle;

/**
 * A one-dimensional linear interpolator. The interpolated value of the function
 * <i>y</i> at <i>x</i> between two data points <i>(x<sub>1</sub>,
 * y<sub>1</sub>)</i> and <i>(x<sub>2</sub>, y<sub>2</sub>)</i> is given by:<br>
 * <i>y = y<sub>1</sub> + (x - x<sub>1</sub>) * (y<sub>2</sub> - y<sub>1</sub>)
 * / (x<sub>2</sub> - x<sub>1</sub>)</i>
 */
@BeanDefinition(style = "light", constructorScope = "public")
public final class LinearInterpolator1D
    extends Interpolator1D
    implements ImmutableBean {

  @Override
  public double interpolate(Interpolator1DDataBundle data, double value) {
    int lowerIndex = data.getLowerBoundIndex(value);
    double y1 = data.getValue(lowerIndex);
    if (lowerIndex == data.size() - 1) {
      return y1;
    }
    double x1 = data.getKey(lowerIndex);
    double x2 = data.getKey(lowerIndex + 1);
    double y2 = data.getValue(lowerIndex + 1);
    return y1 + (value - x1) / (x2 - x1) * (y2 - y1);
  }

  @Override
  public double firstDerivative(Interpolator1DDataBundle data, double value) {
    int lowerIndex = data.getLowerBoundIndex(value);
    if (lowerIndex == data.size() - 1) {
      if (data.size() == 1) {
        return 0d;
      }
      if (value > data.lastKey()) {
        throw new MathException("Value of " + value + " after last key. Use extrapolator");
      }
      lowerIndex--;
    }
    double x1 = data.getKey(lowerIndex);
    double y1 = data.getValue(lowerIndex);
    double x2 = data.getKey(lowerIndex + 1);
    double y2 = data.getValue(lowerIndex + 1);
    return (y2 - y1) / (x2 - x1);
  }

  @Override
  public double[] getNodeSensitivitiesForValue(Interpolator1DDataBundle data, double value) {
    int size = data.size();
    double[] result = new double[size];
    int lowerIndex = data.getLowerBoundIndex(value);
    if (lowerIndex == size - 1) {
      result[size - 1] = 1d;
      return result;
    }
    double x1 = data.getKey(lowerIndex);<commit_msg>Improve performance of linear interpolation<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.strata.math.impl.interpolation;

import java.util.Set;

import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.Property;
import org.joda.beans.impl.light.LightMetaBean;

import com.opengamma.strata.math.impl.interpolation.data.ArrayInterpolator1DDataBundle;
import com.opengamma.strata.math.impl.interpolation.data.Interpolator1DDataBundle;

/**
 * A one-dimensional linear interpolator. The interpolated value of the function
 * <i>y</i> at <i>x</i> between two data points <i>(x<sub>1</sub>,
 * y<sub>1</sub>)</i> and <i>(x<sub>2</sub>, y<sub>2</sub>)</i> is given by:<br>
 * <i>y = y<sub>1</sub> + (x - x<sub>1</sub>) * (y<sub>2</sub> - y<sub>1</sub>)
 * / (x<sub>2</sub> - x<sub>1</sub>)</i>
 */
@BeanDefinition(style = "light", constructorScope = "public")
public final class LinearInterpolator1D
    extends Interpolator1D
    implements ImmutableBean {

  @Override
  public double interpolate(Interpolator1DDataBundle data, double value) {
    int lowerIndex = data.getLowerBoundIndex(value);
    double y1 = data.getValue(lowerIndex);
    // check if x-value is at or beyond the last node
    if (lowerIndex == data.size() - 1) {
      // return the value of the last node (flat extrapolation)
      return y1;
    }
    double x1 = data.getKey(lowerIndex);
    double x2 = data.getKey(lowerIndex + 1);
    double y2 = data.getValue(lowerIndex + 1);
    return y1 + (value - x1) / (x2 - x1) * (y2 - y1);
  }

  @Override
  public double firstDerivative(Interpolator1DDataBundle data, double value) {
    int lowerIndex = data.getLowerBoundIndex(value);
    // check if x-value is at or beyond the last node
    if (lowerIndex == data.size() - 1) {
      // return zero to match flat extrapolation in interpolate()
      if (value > data.lastKey()) {
        return 0d;
      }
      // if value is at last node, calculate the gradient from the previous interval
      lowerIndex--;
    }
    double x1 = data.getKey(lowerIndex);
    double y1 = data.getValue(lowerIndex);
    double x2 = data.getKey(lowerIndex + 1);
    double y2 = data.getValue(lowerIndex + 1);
    return (y2 - y1) / (x2 - x1);
  }

  @Override
  public double[] getNodeSensitivitiesForValue(Interpolator1DDataBundle data, double value) {
    int size = data.size();
    double[] result = new double[size];
    int lowerIndex = data.getLowerBoundIndex(value);
    // check if x-value is at or beyond the last node
    if (lowerIndex == size - 1) {
      // sensitivity is entirely to the last node
      result[size - 1] = 1d;
      return result;
    }
    double x1 = data.getKey(lowerIndex);
{'fraction_non_alphanumeric': 0.13435804701627485, 'fraction_numerical': 0.018625678119349006, 'mean_word_length': 3.442570281124498, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2775331', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1856, 'n_words': 465}