2d字典或查找表?

时间:2017-05-31 21:05:24

标签: c# sql-server linq csv bilinear-interpolation

我不完全确定该怎么称呼我。

enter image description here

我将这些数据放在CSV文件和SQL数据库中。我需要做的是能够使用表格进行双线性插值。那么当说d = 2.5和HVL = 1.6时,解决这个问题的最佳方法是什么?我可以执行的计算只是不确定如何从表中选择适当的数字,某种形式的LINQ语句?

编辑:

使用数字1.6和2.5,我需要选择它周围的数字作为2x2矩阵(Bilinear interpolation

enter image description here

由于

1 个答案:

答案 0 :(得分:4)

1)我拿了一小部分矩阵,看起来像这样:

enter image description here

2)然后我们拿你的matix并动态标准化它(见cte0),看起来像这样:

enter image description here

现在,为了性能,如果这是一个静态矩阵,我建议您实际存储数据是这种方式。

3)根据您的目标值@X和@Y,我们需要确定所需的行和列(在cte1中)。结果如下:

enter image description here

  • 字段和值是所需的行/列号

4)在cte2中,我们想要获取X / Y范围和值,看起来像这样:

enter image description here

4)最终查询是X / Y范围/值的插值的一个小问题。我使用UDF只是为了方便数学。

我应该注意到结果的验证如下所示:

enter image description here

好的,代码怎么样?

我应该补充一点,我将这些部分留作cte's,这样你就可以看到各个部分如何协同工作。当然可以进一步压缩(参见下面的编辑)。

Declare @YourTable Table ([y] int,[x1.5] float,[x2.0] float,[x3.0] float,[x4.0] float)
Insert Into @YourTable Values
 (1,1.050,1.055,1.057,1.057)
,(2,1.080,1.089,1.097,1.098)
,(3,1.091,1.103,1.114,1.446)

Declare @XML xml = (Select * from @YourTable for XML Raw) 
Declare @Y float = 2.5
Declare @X float = 1.6

;with cte0 as (
                Select RowNr = Dense_Rank() over (Order By r.value('@y','float'))
                      ,ColNr = Dense_Rank() over (Order By convert(float,replace(attr.value('local-name(.)','varchar(100)'),'x','')))
                      ,Y     = r.value('@y','float')
                      ,X     = convert(float,replace(attr.value('local-name(.)','varchar(100)'),'x',''))
                      ,V      = attr.value('.','float') 
                 From  @XML.nodes('/row') as A(r)
                 Cross Apply A.r.nodes('./@*') AS B(attr)
                 Where attr.value('local-name(.)','varchar(100)') not in ('y') )
     ,cte1 as ( 
                Select *
                 From (Select R1=max(RowNr),R2=max(RowNr)+1 From  cte0 A where Y<@Y) A
                 Join (Select C1=max(ColNr),C2=max(ColNr)+1 From  cte0 A where X<@X) B
                   on 1=1
              )
     ,cte2 as (
                Select X1  = max(case when C=1 then X end)
                      ,X2  = max(case when C=2 then X end)
                      ,Y1  = max(case when R=1 then Y end)
                      ,Y2  = max(case when R=2 then Y end)
                      ,Q11 = max(case when R=1 and C=1 then V end)
                      ,Q12 = max(case when R=1 and C=2 then V end)
                      ,Q21 = max(case when R=2 and C=1 then V end)
                      ,Q22 = max(case when R=2 and C=2 then V end)
                 From (
                        Select *
                              ,R=Dense_Rank() over (Order By RowNr)
                              ,C=Dense_Rank() over (Order By ColNr)
                         From  cte0 A
                         Cross Join cte1
                         Where RowNr between R1 and R2
                           and ColNr between C1 and C2
                      ) A
              )
Select Value = [dbo].[udf-Stat-Interpolate](@Y,Y1,Y2,[dbo].[udf-Stat-Interpolate](@X,X1,X2,Q11,Q12) ,[dbo].[udf-Stat-Interpolate](@X,X1,X2,Q21,Q22) )
 From  cte2

<强>返回

Value
1.0876

最后,UDF如果感兴趣

CREATE Function [dbo].[udf-Stat-Interpolate] (@PosNr float,@PosMin float,@PosMax float,@ValMin float,@ValMax float)  
Returns Float as  
Begin 
   Return (((@PosNr-@PosMin)/(@PosMax-@PosMin)*(@ValMax-@ValMin)))+@ValMin
End
  

编辑 - 如果存储矩阵如上图所示(#2)

Declare @Y float = 2.5
Declare @X float = 1.6

Select Value = [dbo].[udf-Stat-Interpolate](@Y,Y1,Y2,[dbo].[udf-Stat-Interpolate](@X,X1,X2,Q11,Q12),[dbo].[udf-Stat-Interpolate](@X,X1,X2,Q21,Q22) )
 From (
        Select X1  = max(case when C=1 then X end)
              ,X2  = max(case when C=2 then X end)
              ,Y1  = max(case when R=1 then Y end)
              ,Y2  = max(case when R=2 then Y end)
              ,Q11 = max(case when R=1 and C=1 then V end)
              ,Q12 = max(case when R=1 and C=2 then V end)
              ,Q21 = max(case when R=2 and C=1 then V end)
              ,Q22 = max(case when R=2 and C=2 then V end)
        From (
               Select *
                     ,R=Dense_Rank() over (Order By RowNr)
                     ,C=Dense_Rank() over (Order By ColNr)
                From  YourTable A
                Cross Join (
                            Select *
                             From (Select R1=max(RowNr),R2=max(RowNr)+1 From  YourTable A where Y<@Y) A
                             Join (Select C1=max(ColNr),C2=max(ColNr)+1 From  YourTable A where X<@X) B on 1=1
                           ) B
                Where RowNr between R1 and R2
                  and ColNr between C1 and C2
            ) A
      ) A
相关问题