安裝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");
}
}
}
}