⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fuzzymatrix.cs

📁 模糊矩阵类 可以根据不同的模糊算子计算模糊矩阵的并运算C=A∪B,交运算 C=A∩B,求余运算 C=1-A 等
💻 CS
字号:
using System;


namespace KarstModel
{
	/// <summary>
	/// 模糊矩阵类,数据全是。
	/// </summary>
	public class TFuzzyMatrix
	{
		protected int row,col;//矩阵的行数和列数
		protected	double [,]mat;//标准化后数据,全是[0,1]范围数据
		
		//用于计算∧和∨的标识,共有9种 1:zadeh,2:环和,乘积,3:有界算子
		//4:取大,乘积,5:有界和,取小,6:有界和,乘积,7:einstainc
		//8:hamacher,9:定义为静态性,因为是只有两个矩阵对象计算时才用,故要为类共有,而不是对象所有.
		private static long m_methodOfMaxMin=1;

		
		//构造函数,矩阵数据未知
		public TFuzzyMatrix()
		{}
		public TFuzzyMatrix(int row,int col)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];			
		}
		public TFuzzyMatrix(int row,int col,int methodOfMaxMin)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];
			this.methodOfMaxMin=methodOfMaxMin;
		}
		public void SetData(int row,int col,long methodOfMaxMin)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];			
			//mat=(double[,])data.Clone();
			this.methodOfMaxMin=methodOfMaxMin;
		}
		//以下构造函数用一个常数data来初始化模糊矩阵
		//bool isSourse 表示是原始数据或者是标准化模糊数据
		public TFuzzyMatrix(int row,int col,double data,int methodOfMaxMin)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];			
			for( int i=0; i<row; i++)
				for( int j=0; j<col; j++)
					mat[i,j]=data;
			this.methodOfMaxMin=methodOfMaxMin;
		}
		//以下构造函数用一个二维DATA矩阵来初始化模糊矩阵
		//以下构造函数用一个二维DATA矩阵来初始化模糊矩阵
		public TFuzzyMatrix(int row,int col,double[,] data,int methodOfMaxMin)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];			
			mat=(double[,])data.Clone();
			this.methodOfMaxMin=methodOfMaxMin;
		}
		//以下构造函数用来初始化模糊矩阵为特殊矩阵0,I,E
		public TFuzzyMatrix(int row,int col,char ch,int methodOfMaxMin)
		{
			this.col=col;
			this.row=row;
			mat=new double[this.row,this.col];
			if (ch=='E')//初始化为E矩阵
			{
				for( int i=0; i<row; i++)
					for( int j=0; j<col; j++)
					{
						mat[i,j]=1;
					}
			}
			else if(ch=='I')//初始化为I矩阵
			{
				for( int i=0; i<row; i++)
					for( int j=0; j<col; j++)
					{
						if (i==j)
							mat[i,j]=1;
						else
							mat[i,j]=0;
					}
			}
			else if(ch=='0')// 初始化为0矩阵
			{
				for( int i=0; i<row; i++)
					for( int j=0; j<col; j++)
					{
						mat[i,j]=0;
					}
			}
			this.methodOfMaxMin=methodOfMaxMin;
		}
		//以下为了在程序中可以用类似fuzzyMatrix1[i,j]取得值或赋值
		public double this[int i, int j]//
		{
			set { mat[i,j] = value; }
			get { return mat[i,j]; }
		}

		/// <summary>
		/// 表示用于计算^v的编号
		/// </summary>
		public long methodOfMaxMin
		{
			get
			{
				return m_methodOfMaxMin;
			}
			set
			{
				m_methodOfMaxMin=value;
			}
		}
//以下定义各种运算
		//判断相等A==B
		public static bool operator ==(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			bool equal;
			equal=true;
			if ((A.col!=B.col)||(A.row!=B.row))
				equal=false;
			else
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						if (A[i,j]!=B[i,j])
						{
							equal=false;
							break;
						}

			}
			return equal;
		}
         //
		public static bool operator !=(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			bool equal;
			equal=false;
			if ((A.col!=B.col)||(A.row!=B.row))
				equal=true;
			else
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						if (A[i,j]!=B[i,j])
						{
							equal=true;
							break;
						}

			}
			return equal;
		}
		public override bool Equals(object o)
		{
		    return true;
		 }
		public override int GetHashCode()
		{
			return 0;
		}

		public static bool operator <=(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			bool minEqual;
			minEqual=true;
			if ((A.col!=B.col)||(A.row!=B.row))
				minEqual=false;// 应该是不能比较
			else
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						if (A[i,j]>B[i,j])
						{
							minEqual=false;
							break;
						}

			}
			return minEqual;
		}
		public static bool operator >=(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			bool maxEqual;
			maxEqual=true;
			if ((A.col!=B.col)||(A.row!=B.row))
				maxEqual=false;// 应该是不能比较
			else
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						if (A[i,j]<B[i,j])
						{
							maxEqual=false;
							break;
						}

			}
			return maxEqual;
		}

		//并运算C=A∪B
		public static TFuzzyMatrix operator +(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			if ((A.col!=B.col)||(A.row!=B.row))
			{
				TFuzzyMatrix t=new TFuzzyMatrix(A.row,A.col,'0');
				return t;// 应该是不能比较
			}
			else 
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						C[i,j]=max(A[i,j],B[i,j]);

			}
			return C;
		}
		//交运算 C=A∩B
		public static TFuzzyMatrix operator *(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			if ((A.col!=B.col)||(A.row!=B.row))
			{
				TFuzzyMatrix t=new TFuzzyMatrix(A.row,A.col,'0');
				return t;// 应该是不能比较
			}
			else 
			{
				for(int i=0;i<A.row;i++)
					for(int j=0;j<A.col;j++)
						C[i,j]=min(A[i,j],B[i,j]);

			}
			return C;
		}
		//求余运算 C=1-A
		public static TFuzzyMatrix operator !(TFuzzyMatrix A)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			for(int i=0;i<A.row;i++)
				for(int j=0;j<A.col;j++)
					C[i,j]=1-A[i,j];
			return C;
		}
		//转置 rij=rji
		public static TFuzzyMatrix operator ~(TFuzzyMatrix A)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			for(int i=0;i<A.row;i++)
				for(int j=0;j<A.col;j++)
					C[i,j]=A[j,i];
			return C;
		}
		//合成运算
		public static TFuzzyMatrix operator ^(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			double temp;
			for(int i=0;i<A.row;i++)
				for(int j=0;j<B.col;j++)
				{
					int k=0;
					temp=min(A[i,k],B[k,j]);
					for(k=1;k<A.col;k++)
						temp=max(temp,min(A[i,k],B[k,j]));
					C[i,j]=temp;

				}
			return C;
		}
		//幂运算 C=A^n
		public static TFuzzyMatrix operator ^(TFuzzyMatrix A,int n)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(A.row,A.col);
			copy(C,A);
			for(int i=1;i<=n;i++)
				C=C^A;
			return C;
		}
		//将B复制给A
		public static void copy(TFuzzyMatrix A,TFuzzyMatrix B)
		{
			for(int i=0;i<A.row;i++)
				for(int j=0;j<A.col;j++)
					A[i,j]=B[i,j];
			A.methodOfMaxMin=B.methodOfMaxMin;
		}
		//入-截矩阵
		public TFuzzyMatrix section(double e)
		{
			TFuzzyMatrix C=new TFuzzyMatrix(row,col);
			for(int i=0;i<row;i++)
				for(int j=0;j<col;j++)
					if (this.mat[i,j]>=e)
						C[i,j]=1;
					else
						C[i,j]=0;
			return C;
		}

		//基本定理的判断
		//是自反矩阵吗?
		public bool isBack()
		{
            TFuzzyMatrix C=new TFuzzyMatrix(row,col,'I');
			if(this>=C)
				return true;
			else
				return false;
		}
		//是对称矩阵吗?
		public bool isSymmetry()
		{
			TFuzzyMatrix C=new TFuzzyMatrix(row,col);
			C=~this;
            if(this==C)
				return true;
			else
				return false;
		}
		//是模糊传递矩阵吗?
		public bool isTransfer()
		{
			TFuzzyMatrix C=new TFuzzyMatrix(row,col);
			C=this^this;
            if(this>=C)
				return true;
			else
				return false;
		}
		//是模糊等价矩阵吗?
		public bool isEquivalence()
		{
			if(this.isBack()==true && this.isSymmetry()==true && this.isTransfer()==true)
				return true;
			else
				return false;
		}
		//是模糊相似矩阵吗?
		public bool isSimilitude()
		{
			if (this.isBack()==true && this.isSymmetry()==true)
				return true;
			else
				return false;
		}




		 
		//用m_methodOfMaxMin表示计算∧和∨的标识,共有9种 1:zadeh,2:环和,乘积,3:有界算子
		//4:取大,乘积,5:有界和,取小,6:有界和,乘积,7:einstainc
		//8:hamacher,9:
		//下面定义∨运算
		private static double max(double a,double b)
		{
			double temp;
			switch(m_methodOfMaxMin)
			{
				case 1:
				case 4:if (a>=b)
						   temp=a;
					   else temp=b;break;
				case 2:temp=a+b-a*b;break;
				case 3:
				case 5:
				case 6:if(a+b>1)
						   temp=1;
					   else temp=a+b;break;
				case 7:temp=(a+b)/(1+a*b);break;
				case 8:
				case 9:
				default :if (a>=b)
							 temp=a;
						 else temp=b;break;
			}
			return temp;
		}
		//下面定义∧运算
		private static double min(double a,double b)
		{
			double temp;
			switch(m_methodOfMaxMin)
			{
				case 1:
				case 5:if (a>=b)
						   temp=b;
					   else temp=a;break;
				case 2:
				case 4:
				case 6:temp=a*b;break;
				case 3:if(a+b-1>0)
						   temp=a+b-1;
					   else
						   temp=0;	break;						
				case 7:temp=(a*b)/(1+(1-a)*(1-b));break;
				case 8:
				case 9:
				default :if (a>=b)
							 temp=b;
						 else temp=a;break;
			}
			return temp;
		}
		public virtual void input()//输入函数
		{
			string s;
			double temp;
			for( int i=0; i<row; i++)
				for( int j=0; j<col; j++)
				{
					Console.Write("please input matrix[{0}][{1}]:",i,j);
					s=Console.ReadLine();
					temp=Convert.ToDouble(s);
					mat[i,j]=temp;
				}
		}
		public virtual void display()//输出函数
		{
			for( int i=0; i<row; i++)
			{
				for(int j=0; j<col; j++)
					Console.Write("\t{0:}",mat[i,j]);
				Console.WriteLine();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -