## C#与F#混合编程 原

风半程

c#版本

``````        public static List<double> LinearFittingByLeastSquares(List<double> xaxis, List<double> yaxis, bool flag)// Least Squares Linear Fitting
{

// Var Init
//double[] result = new double[3];
List<double> ans = new List<double>();
double interceptb = 0, slopek = 0, sumx = 0, sumy = 0, sumxy = 0, sumxsq = 0, corrcoef;
int i, num;
num = xaxis.Count;

// Calculate Sums
for (i = 0; i < num; i++)
{
sumx += xaxis[i];
sumy += yaxis[i];
sumxy += xaxis[i] * yaxis[i];
sumxsq += xaxis[i] * xaxis[i];
}

sumx = sumx / num;
sumy = sumy / num;

// Calculate Intercept & Slope
slopek = (sumxy - num * xaxis.Average() * yaxis.Average()) / (sumxsq - num * Pow(xaxis.Average(), 2));
interceptb = sumy - slopek * sumx;

// Output
//result[0] = slopek;
//result[1] = interceptb;

double avgx = xaxis.Average(), avgy = yaxis.Average(),
upper = 0, lowerx = 0, lowery = 0;
for (i = 0; i < num; i++)
{

upper += (xaxis[i] - avgx) * (yaxis[i] - avgy);
lowerx += Pow(xaxis[i] - avgx, 2);
lowery += Pow(yaxis[i] - avgy, 2);

}
corrcoef = upper / Pow(lowerx * lowery, 0.5);
//result[2] = corrcoef;
return ans;

}``````

F#版本

``````let LeastSquareMethod (samples:(float*float) list)=
match samples with
|[]|[_]->failwith "ItemNumber Error"
|_->let count=float samples.Length
let sumx=samples|>List.sumBy (fun (x,y)->x)
let sumy=samples|>List.sumBy (fun (x,y)->y)
let sumxy=samples|>List.sumBy (fun (x,y)->x*y)
let sumx2=samples|>List.sumBy (fun (x,y)->x|>square)
let dishu=(count*sumx2-square sumx)
match dishu with
|0.->failwith "Items All the same"
|_->let k=(count*sumxy-sumx*sumy)/dishu
let b=(sumy-k*sumx)/count
let corrcoef=samples|>List.map (fun (x,y)->(y-k*x-b)|>square)|>List.sum|>sqrt
{K=k;b=b;Corrcoef=corrcoef}``````

### 风半程

[CLI/C++].NET托管代码和非托管代码的相互调用

Yong_Luo
2010/10/01
0
0

2017/09/27
2.1K
27
C#数学计算包 Math.NET

1.Math.NET基本介绍 Math.NET官方网站：http://www.mathdotnet.com/ Math.NET的目标是为提供一款自身包含清晰框架的符号运算和数学运算/科学运算，它是C#开发的开源类库。Math.NET含了一个支...

CSharpKit
2017/12/18
0
0
OSC 第 82 期高手问答 — Matlab 与 .NET 混合编程调用

OSCHINA 本期高手问答（7月8日- 7月14日）我们请来了 @asxinyu （董斌辉）为大家解答关于 .NET 学习，.NET 开源项目资源，以及 Matlab 与 .NET 混合编程调用 方面的问题。 董斌辉，@asxinyu ...

2015/07/08
4.4K
31

【IT168 资讯】在Ignite大会上，微软列出了将改变科技产业的3项关键技术，分别是混合现实、人工智能和量子计算。微软在移动时代错失了影响力，这是毋庸置疑的，但是它正在努力弥补。 　　...

it168网站
2017/09/27
0
0

https://blog.csdn.net/Blockchain_lemon/article/details/80589123

Moks角木
31分钟前
1
0

31分钟前
1
0

wanxiangming
33分钟前
0
0
CentOS7安装xrdp环境可实现远程桌面访问

CentOS7安装xrdp环境可实现远程桌面访问 2018-07-14 06:39:28 分类：运维 阅读(2051) 评论(0) 在"Ubuntu系统安装xrdp桌面客户端及实现远程连接桌面"文章中有分享过在Ubuntu系统中安装XRDP环境...

linjin200
57分钟前
4
0
ConfigurationProperties

package cn.enjoy.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @ConfigurationProperties(pr......

2
0