安装mono c#开发环境:
apt-get update
apt-get install mono-devel
.so文件生成:
g++ -fPIC -shared -o libmathlib52.so mathlib52.cpp
root@ubuntu:/home/cpptest/cstest# mcs mean.cs
root@ubuntu:/home/cpptest/cstest# mono mean.exe
avg=3,avg1=3,mode=2,stdDev=1.30930734141595,stdDev1=1.30930734141595
avg=3,avg1=3,mode=2,stdDev=1.30930734141595,stdDev1=1.30930734141595
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
public class RefComm
{
[DllImport("libmathlib52.so", EntryPoint="mean")]
public static extern double mean(double[] a,int n);// 平均数
// 计算由num个浮点数组成的数组的平均值
private static double Mean(IEnumerable<double> values)
{
int num=values.Count();
double avg=0;
foreach (double v in values)
{
avg+=v;
}
avg/=num;
return avg;
}
// 返回样本的众数值,如果众数超过一个,则返回最前面的一个
static T find_mode<T>(T[] a)
{
int num=a.Length;
T md=a[0],oldmode=a[0];
int count,oldcount=0;
for(int t=0;t<num;t++)
{
md=a[t];
count=1;
for(int w=t+1;w<num;w++)
if(md.Equals(a[w]))//此处不能用==
count++;//准众数值
if(count>oldcount)
{
oldmode=md;
oldcount=count;
}
}
return oldmode;
}
private static double StdDev(IEnumerable<double> values)
{
double ret = 0;
if (values.Count() > 0)
{
// 计算平均数
double avg = values.Average();
// 计算各数值与平均数的差值的平方,然后求和
double sum = values.Sum(d => Math.Pow(d - avg, 2));
// 除以数量,然后开方
ret = Math.Sqrt(sum / values.Count());
}
return ret;
}
// 计算标准均方差
private static double std_dev(IEnumerable<double> values)
{
double avg=Mean(values);
double std=0;
foreach (double v in values)
{
std+=(v-avg)*(v-avg);
}
std/=values.Count();
std=Math.Sqrt(std);
return std;
}
public static void Main()
{
{
double[] a= {1, 2,2, 3, 4,4, 5};
List<double> L = new List<double>(a);
double avg=L.Average();
double avg1=Mean(L);
double mode=find_mode<double>(a);
double stdDev=StdDev(L);
double stdDev1=std_dev(L);
Console.WriteLine("avg="+avg.ToString()+",avg1="+avg1.ToString()+",mode="+mode.ToString()+",stdDev="+stdDev.ToString()+",stdDev1="+stdDev1.ToString());
}
{
int[] a= {1, 2, 2,3, 4, 4,5};
List<int> L = new List<int>(a);
var L1 = L.ToArray().Select(Convert.ToDouble);
double avg=L.Average();
double avg1=Mean(L1);
int mode=find_mode<int>(a);
double stdDev=StdDev(L1);
double stdDev1=std_dev(L1);
Console.WriteLine("avg="+avg.ToString()+",avg1="+avg1.ToString()+",mode="+mode.ToString()+",stdDev="+stdDev.ToString()+",stdDev1="+stdDev1.ToString());
}
}
}
root@ubuntu:/home/cpptest/cstest# mcs Mtx3.cs
root@ubuntu:/home/cpptest/cstest# mono Mtx3.exe
输入3*3矩阵中的9个数:
1
2
3
2
1
4
3
4
1
ret=0
特征多项式为1x^3-3x^2-26x+20
ret1=1
3个根为:
x1=6.51813069033468+0i
x2=0.723408161707996+0i
x3=-4.24153902277408+0i
该3*3矩阵是对称非正定矩阵!
using System;
using System.Runtime.InteropServices;
public class Mtx3
{
[DllImport("libmathlib52.so", EntryPoint="poly3")]
public static extern int poly3(double[] mtx3,double[] abcd);//计算三阶方阵的特征多项式,返回值0表示是对称矩阵,1表示不是对称矩阵
[DllImport("libmathlib52.so", EntryPoint="Cardano")]
public static extern int Cardano(double a,double b,double c,double d,double[] y);
public static void Main()
{
double [] a=new double[9];
double [] b=new double[4];
Console.WriteLine("输入3*3矩阵中的9个数:");
for(int i=0;i<a.Length;i++)
a[i]= Convert.ToDouble(Console.ReadLine());
int ret=poly3(a,b);
Console.WriteLine("ret={0}",ret);
Console.WriteLine("特征多项式为"+b[0].ToString()+"x^3"+b[1].ToString()+"x^2"+b[2].ToString()+"x"+(b[3]>0?"+":"")+b[3].ToString());
double [] y=new double[6];
int ret1=Cardano(b[0],b[1],b[2],b[3],y);
Console.WriteLine("ret1={0}",ret1);
Console.WriteLine("3个根为:");
Console.WriteLine("x1={0}+{1}i",y[0],y[1]);
Console.WriteLine("x2={0}+{1}i",y[2],y[3]);
Console.WriteLine("x3={0}+{1}i",y[4],y[5]);
if(ret==0)
{
if(y[0]>0 && y[2]>0 && y[4]>0)
{
Console.WriteLine("该3*3矩阵是对称正定矩阵!");
}
else
{
Console.WriteLine("该3*3矩阵是对称非正定矩阵!");
}
}
}
}
root@ubuntu:/home/cpptest/cstest# mcs Cardano.cs
root@ubuntu:/home/cpptest/cstest# mono Cardano.exe
依次输入实系数a、b、c、d:
1
-15
-18
0
ret=1
一元三次方程1x^3-15x^2-180的3个根为:
x1=16.116843969807+0i
x2=-1.77737236484177E-07+0i
x3=-1.11684413602586+0i
using System;
using System.Runtime.InteropServices;
public class RefComm
{
[DllImport("libmathlib52.so", EntryPoint="Cardano")]
/*
函数功能: 计算实系数一元三次方程ax^3+bx^2+cx+d=0的3个复根
参数:
输入: 4个实系数
输出: 3个复根(数组y依次存储第1个复根的实部、虚部、第2个复根的实部、虚部、第3个复根的实部、虚部)
返回值:
0: delta=0,有3个实根,其中y_2和y_3是重根
1: delta>0 && delta2>=0||delta<0,有3个相异实根
2: delta>0 && delta2<0,1实2虚
*/
public static extern int Cardano(double a,double b,double c,double d,double[] y);
public static void Main()
{
double [] y=new double[6];
Console.WriteLine("依次输入实系数a、b、c、d:");
for(int i=0;i<4;i++)
y[i]= Convert.ToDouble(Console.ReadLine());
double a=y[0],b=y[1],c=y[2],d=y[3];
int ret=Cardano(a,b,c,d,y);
Console.WriteLine("ret={0}",ret);
Console.WriteLine("一元三次方程"+a.ToString()+"x^3"+b.ToString()+"x^2"+c.ToString()+d.ToString()+"的3个根为:");
Console.WriteLine("x1={0}+{1}i",y[0],y[1]);
Console.WriteLine("x2={0}+{1}i",y[2],y[3]);
Console.WriteLine("x3={0}+{1}i",y[4],y[5]);
}
}
root@ubuntu:/home/cpptest/cstest# mcs brinv.cs
root@ubuntu:/home/cpptest/cstest# mono brinv.exe
Hello,MONO
root@ubuntu:/home/cpptest/cstest# mcs -d:MS_BUILD brinv.cs
root@ubuntu:/home/cpptest/cstest# mono brinv.exe
Hello,VS.NET
root@ubuntu:/home/cpptest/cstest# mono brinv.exe
Hello,MONO
输入3*3矩阵中的9个数:
3
4
5
6
1
2
4
6
7
-0.555555555555556
0.222222222222222
0.333333333333333
-3.77777777777778
0.111111111111111
2.66666666666667
3.55555555555556
-0.222222222222223
-2.33333333333333
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
public class RefComm
{
#if MS_BUILD
public static void Hello()
{
Console.WriteLine("Hello,VS.NET");
}
#else
public static void Hello()
{
Console.WriteLine("Hello,MONO");
}
#endif
}
class DLLSOTest
{
#if USE_DLL
[DllImport("MATHLIB52.DLL", EntryPoint="_brinv@8", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
#else
[DllImport("libmathlib52.so", EntryPoint="brinv")]
#endif
public static extern int brinv(double[] a,int n);
//public static extern int brinv(ref double a,int n);
public static void Main()
{
RefComm.Hello();
//Console.ReadKey();
double [] a=new double[9];
Console.WriteLine("输入3*3矩阵中的9个数:");
for(int i=0;i<a.Length;i++)
a[i]= Convert.ToDouble(Console.ReadLine());
int ret=brinv(a,3);
if(ret!=0)
{
foreach(double j in a)
Console.WriteLine(j.ToString()+"\t");
}
}
}
}