Browse Source

Added LogLinearSpline and TransformedInterpolation interpolators.

pull/236/head
Candy Chiu 12 years ago
committed by Christoph Ruegg
parent
commit
df8fb6a978
  1. 22
      src/Numerics/Interpolate.cs
  2. 141
      src/Numerics/Interpolation/LogLinearSpline.cs
  3. 76
      src/Numerics/Interpolation/TransformedSpline.cs
  4. 2
      src/Numerics/Numerics.csproj

22
src/Numerics/Interpolate.cs

@ -30,6 +30,7 @@
using System.Collections.Generic;
using MathNet.Numerics.Interpolation;
using System;
namespace MathNet.Numerics
{
@ -160,6 +161,27 @@ namespace MathNet.Numerics
return Interpolation.LinearSpline.Interpolate(points, values);
}
/// <summary>
/// Create log linear spline interpolation based on arbitrary points.
/// </summary>
/// <param name="points">The sample points t. Optimized for arrays.</param>
/// <param name="values">The sample point values x(t). Optimized for arrays.</param>
/// <returns>
/// An interpolation scheme optimized for the given sample points and values,
/// which can then be used to compute interpolations and extrapolations
/// on arbitrary points.
/// </returns>
/// <remarks>
/// The value pairs do not have to be sorted, but if they are not sorted ascendingly
/// and the passed x and y arguments are arrays, they will be sorted inplace and thus modified.
///
/// If the values are passed as an array, they will be modified inplace, even it is already sorted.
/// </remarks>
public static IInterpolation LogLinearSpline(IEnumerable<double> points, IEnumerable<double> values)
{
return new LogLinearSpline(points, values);
}
/// <summary>
/// Create an piecewise natural cubic spline interpolation based on arbitrary points,
/// with zero secondary derivatives at the boundaries.

141
src/Numerics/Interpolation/LogLinearSpline.cs

@ -0,0 +1,141 @@
// <copyright file="LogLinearSpline.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MathNet.Numerics.Interpolation
{
/// <summary>
/// Log Linear Spline Interpolation
/// </summary>
/// <remarks>This algorithm supports differentiation, not integration.</remarks>
public class LogLinearSpline : IInterpolation
{
/// <summary>
/// Internal Spline Interpolation
/// </summary>
private readonly LinearSpline _spline;
/// <summary>
/// Creates a log linear interpolation based on input data
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public LogLinearSpline(IEnumerable<double> x, IEnumerable<double> y)
{
var xx = (x as double[]) ?? x.ToArray();
var yy = (y as double[]) ?? y.ToArray();
if (xx.Length != yy.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
for (int i = 0; i < yy.Length; i++)
yy[i] = Math.Log(yy[i]);
_spline = LinearSpline.Interpolate(xx, yy);
}
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
/// <summary>
/// Interpolate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated value x(t).</returns>
public double Interpolate(double t)
{
return Math.Exp(_spline.Interpolate(t));
}
/// <summary>
/// Differentiate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
public double Differentiate(double t)
{
return Interpolate(t) * _spline.Differentiate(t);
}
/// <summary>
/// Differentiate twice at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
var linearFirstDerivative = _spline.Differentiate(t);
var linearSecondDerivative = _spline.Differentiate2(t);
var secondDerivative = Differentiate(t) * linearFirstDerivative +
Interpolate(t) * linearSecondDerivative;
return secondDerivative;
}
/// <summary>
/// Indefinite integral at point t.
/// </summary>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
throw new NotImplementedException();
}
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
throw new NotImplementedException();
}
}
}

76
src/Numerics/Interpolation/TransformedSpline.cs

@ -0,0 +1,76 @@
using MathNet.Numerics.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathNet.Numerics.Interpolation
{
public class TransformedInterpolation : IInterpolation
{
private IInterpolation _baseInterpolation;
private Func<double, double> _transformer;
public TransformedInterpolation(IInterpolation baseInterp, Func<double, double> transformer)
{
_baseInterpolation = baseInterp;
_transformer = transformer;
}
public static TransformedInterpolation Interpolate(
Func<double, double> transformer, Func<double, double> transformerInverse,
IEnumerable<double> x, IEnumerable<double> y)
{
var xx = (x as double[]) ?? x.ToArray();
var yy = (y as double[]) ?? y.ToArray();
if (xx.Length != yy.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
for (int i = 0; i < yy.Length; i++)
yy[i] = transformerInverse(yy[i]);
var baseInterp = LinearSpline.Interpolate(xx, yy);
var interp = new TransformedInterpolation(baseInterp, transformer);
return interp;
}
public bool SupportsDifferentiation
{
get { return false; }
}
public bool SupportsIntegration
{
get { return false; }
}
public double Differentiate(double t)
{
throw new NotImplementedException();
}
public double Differentiate2(double t)
{
throw new NotImplementedException();
}
public double Integrate(double t)
{
throw new NotImplementedException();
}
public double Integrate(double a, double b)
{
throw new NotImplementedException();
}
public double Interpolate(double t)
{
return _transformer(_baseInterpolation.Interpolate(t));
}
}
}

2
src/Numerics/Numerics.csproj

@ -91,8 +91,10 @@
<Compile Include="IntegralTransforms\Hartley.cs" />
<Compile Include="Interpolation\Barycentric.cs" />
<Compile Include="Interpolation\CubicSpline.cs" />
<Compile Include="Interpolation\LogLinearSpline.cs" />
<Compile Include="Interpolation\QuadraticSpline.cs" />
<Compile Include="Interpolation\StepInterpolation.cs" />
<Compile Include="Interpolation\TransformedSpline.cs" />
<Compile Include="LinearAlgebra\Options.cs" />
<Compile Include="LinearAlgebra\Solvers\DelegateStopCriterion.cs" />
<Compile Include="LinearRegression\Options.cs" />

Loading…
Cancel
Save