forked from tsai/mathnet-numerics
committed by
Christoph Ruegg
4 changed files with 241 additions and 0 deletions
@ -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(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue