1 文本格式
using System;
namespace Legalsoft.Truffer
{
/// <summary>
/// 双线性插值
/// interpolation routines for two dimensions
/// Object for bilinear interpolation on a matrix.
/// Construct with a vector of x1.
/// values, a vector of x2 values,
/// and a matrix of tabulated function values yij
/// Then call interp for interpolated values.
/// </summary>
public class Bilin_interp
{
private int m { get; set; }
private int n { get; set; }
private double[,] y { get; set; }
private Linear_interp x1terp { get; set; } = null;
private Linear_interp x2terp { get; set; } = null;
public Bilin_interp(double[] x1v, double[] x2v, double[,] ym)
{
this.m = x1v.Length;
this.n = x2v.Length;
this.y = ym;
this.x1terp = new Linear_interp(x1v, x1v);
this.x2terp = new Linear_interp(x2v, x2v);
}
public double interp(double x1p, double x2p)
{
int i = x1terp.cor > 0 ? x1terp.hunt(x1p) : x1terp.locate(x1p);
int j = x2terp.cor > 0 ? x2terp.hunt(x2p) : x2terp.locate(x2p);
double t = (x1p - x1terp.xx[i]) / (x1terp.xx[i + 1] - x1terp.xx[i]);
double u = (x2p - x2terp.xx[j]) / (x2terp.xx[j + 1] - x2terp.xx[j]);
double yy = (1.0 - t) * (1.0 - u) * y[i, j] + t * (1.0 - u) * y[i + 1, j] + (1.0 - t) * u * y[i, j + 1] + t * u * y[i + 1, j + 1];
return yy;
}
}
}
2 代码格式
using System;
namespace Legalsoft.Truffer
{
/// <summary>
/// 双线性插值
/// interpolation routines for two dimensions
/// Object for bilinear interpolation on a matrix.
/// Construct with a vector of x1.
/// values, a vector of x2 values,
/// and a matrix of tabulated function values yij
/// Then call interp for interpolated values.
/// </summary>
public class Bilin_interp
{
private int m { get; set; }
private int n { get; set; }
private double[,] y { get; set; }
private Linear_interp x1terp { get; set; } = null;
private Linear_interp x2terp { get; set; } = null;
public Bilin_interp(double[] x1v, double[] x2v, double[,] ym)
{
this.m = x1v.Length;
this.n = x2v.Length;
this.y = ym;
this.x1terp = new Linear_interp(x1v, x1v);
this.x2terp = new Linear_interp(x2v, x2v);
}
public double interp(double x1p, double x2p)
{
int i = x1terp.cor > 0 ? x1terp.hunt(x1p) : x1terp.locate(x1p);
int j = x2terp.cor > 0 ? x2terp.hunt(x2p) : x2terp.locate(x2p);
double t = (x1p - x1terp.xx[i]) / (x1terp.xx[i + 1] - x1terp.xx[i]);
double u = (x2p - x2terp.xx[j]) / (x2terp.xx[j + 1] - x2terp.xx[j]);
double yy = (1.0 - t) * (1.0 - u) * y[i, j] + t * (1.0 - u) * y[i + 1, j] + (1.0 - t) * u * y[i, j + 1] + t * u * y[i + 1, j + 1];
return yy;
}
}
}