<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><b><font color="#FF0000" size="6">&nbsp;<span lang="en-ca">Logic 
Matrix</span></font></b></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">Third</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca">third</span> edition of my </b><span lang="en-ca"><b>my Matrix class and I made some adjustment like to make a public ancestor for</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>both MathMatrix and LogicMatrix.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Idea of program</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>To make a logic matrix which is made up of 0's and 1's. In particular, I concentrate on the square matrix. </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Implement some operation like &quot;AND&quot;(similar to multiply in math matrix), &quot;OR&quot;(similar  as plus in math matrix),</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>&quot;POWER&quot;(n times &quot;and&quot; with original matrix). And some property function like &quot;reflexive, symmetric, antisymmetric,</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>transitive&quot;.</b></span></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Almost same as math matrix except use boolean operation. </b></span>¡¡</pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function</b></pre>
  <blockquote>
    <blockquote>
  <pre><b> A.<span lang="en-ca">  </span></b>Matrix&amp; operator &amp;&amp;(Matrix otherMatrix);
<span lang="en-ca">      </span>Matrix&amp; operator ||(Matrix otherMatrix);<span lang="en-ca"> </span>
<span lang="en-ca">      </span>Matrix&amp; power(int exponent);</pre>
  <pre><span lang="en-ca">Similar to multiply,plus, power of math matrix. but overloading with different operators.</span></pre>
      <blockquote>
        <p><b>     B.</b></p>
        <p>bool reflexive();<br>
        bool symmetric();<br>
        bool antiSymmetric();<br>
        bool transitive();<br>
        Matrix&amp; closure();</p>
        <p><span lang="en-ca">In closure() function, I actually change matrix 
        itself which is no good. But I am not patient to correct it.</span></p>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. <span lang="en-ca">Make Matrix the common ancestor of MathMatrix and LogicMatrix so that they can share some common </span></b></pre>
  <pre><span lang="en-ca"><b>operations. </b></span></pre>
  <pre><font color="#FF0000" size="4"><span lang="en-ca"><b>C. I am adding small improvement into third edition and it can be regarded as 3.5 edition.:)</b></span></font></pre>
  <pre><span lang="en-ca"><b>I only add 2 small methods, one is reflexive closure, the other is symmetric closure. They are so simple and </b></span></pre>
  <pre><span lang="en-ca"><b>I am shamed to mention them.	</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

const int MaxRow = 10;
const int MaxCol = 10;
const double LIMIT = 0.01;


class Matrix
{
private:
	int rowNum;
	int colNum;
	double lst[MaxRow][MaxCol];
protected:
	void mul(int dest, double scalor);
	void mul(int source, int dest, double scalor);
public:
	Matrix();
	int row() const {return rowNum;}
	int col() const {return colNum;}
	void setRow(const int newRow) { rowNum = newRow;}
	void setCol(const int newCol) { colNum = newCol;}
	void display();
	double&amp; items(int r, int c);
	void initialize();
	void readFromFile(const char* fileName);
	Matrix&amp; operator = (Matrix other);
	Matrix&amp; transposition();
	
};

class MathMatrix: public Matrix
{
public:
	void echelon(int r, bool reduced=true);
	Matrix&amp; operator+(Matrix other);
	Matrix&amp; operator *(double i);
	Matrix&amp; operator *(Matrix otherMatrix);  
};

class LogicMatrix: public Matrix
{
protected:
	
public:
	Matrix&amp; operator &amp;&amp;(Matrix otherMatrix);
	Matrix&amp; operator ||(Matrix otherMatrix);
	Matrix&amp; power(int exponent);
	bool reflexive();
	bool symmetric();
	bool antiSymmetric();
	bool transitive();
	bool equivalent();
	bool partialOrder();
	Matrix&amp; transitiveClosure();
	Matrix&amp; reflexiveClosure();
	Matrix&amp; symmetricClosure();
};


int main()
{
	LogicMatrix M, N;
	M.readFromFile(&quot;c:\\nick.txt&quot;);
	N =M;
	/*
	N = M;
	for (int i =1; i&lt;M.col()+1; i++)
	{
		cout&lt;&lt;&quot;power of &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;&lt;&lt;endl;		
		M.power(i);
		cout&lt;&lt;&quot;reflexive is :&quot;&lt;&lt;M.reflexive()&lt;&lt;endl; 
		cout&lt;&lt;&quot;symmetric is :&quot;&lt;&lt;M.symmetric()&lt;&lt;endl;
		cout&lt;&lt;&quot;antisymmetric is:&quot;&lt;&lt;M.antiSymmetric()&lt;&lt;endl;
		cout&lt;&lt;&quot;transitive is:&quot;&lt;&lt;M.transitive()&lt;&lt;endl;
		M.display();
		cout&lt;&lt;&quot;\n&quot;;	
		M = N;
	}
	M = N;
	cout&lt;&lt;&quot;closure\n&quot;;
	M.closure();
	*/
	cout&lt;&lt;&quot;reflexive is :&quot;&lt;&lt;M.reflexive()&lt;&lt;endl; 
	cout&lt;&lt;&quot;symmetric is :&quot;&lt;&lt;M.symmetric()&lt;&lt;endl;
	cout&lt;&lt;&quot;antisymmetric is:&quot;&lt;&lt;M.antiSymmetric()&lt;&lt;endl;
	cout&lt;&lt;&quot;transitive is:&quot;&lt;&lt;M.transitive()&lt;&lt;endl;
	M.display();
	cout&lt;&lt;&quot;transitive closure is\n&quot;;
	M.transitiveClosure().display();
	M = N;
	cout&lt;&lt;&quot;symmetric Closure is\n&quot;;
	M.symmetricClosure().display();
	M =N;
	cout&lt;&lt;&quot;reflextive closure is\n&quot;;
	M.reflexiveClosure().display();

	return 0;
}

Matrix&amp; LogicMatrix::symmetricClosure()
{
	for (int r=0; r&lt; row(); r++)
	{
		for (int c=0; c&lt; col(); c++)
		{
			if (items(r,c)==1)
			{
				items(c, r) = 1;
			}
		}
	}
	return *this;
}

Matrix&amp; LogicMatrix::reflexiveClosure()
{
	for (int i=0; i&lt;row(); i++)
	{
		items(i, i) = 1;
	}
	return *this;
}





bool LogicMatrix::partialOrder()
{
	return reflexive()&amp;&amp;antiSymmetric()&amp;&amp;transitive();
}


bool LogicMatrix::equivalent()
{
	return reflexive()&amp;&amp;symmetric()&amp;&amp;transitive();
}


Matrix&amp; LogicMatrix::transitiveClosure()
{
	int dim = col();
	LogicMatrix N;
	N = *this;
	for (int i = 1; i&lt;=dim; i++)
	{
		(Matrix)N = (Matrix)(N ||power(i));
	}
	*this = N;
	return *this;
}


Matrix&amp; LogicMatrix::operator ||(Matrix otherMatrix)
{
	int dim = col();
	for (int r = 0; r&lt; dim; r++)
	{
		for (int c=0; c&lt; dim; c++)
		{
			items(r,c) = (int)items(r,c) ||(int)otherMatrix.items(r, c);
		}
	}
	return *this;
}


bool LogicMatrix::reflexive()
{
	bool result = true;
	for (int i =0; i&lt;col(); i++)
	{
		result = result&amp;&amp;items(i, i);
	}
	return result;
}

bool LogicMatrix::symmetric()
{
	int dim = col();  //since the function will be called nxn times, better use variable.
	for (int r = 0; r&lt; dim; r++)
	{
		for (int c = r; c&lt; dim; c++)
		{			
			if ((int)items(r,c)^(int)items(c,r))//exclusive or
			{
				return false;
			}
		}
	}
	return true;
}

bool LogicMatrix::antiSymmetric()
{
	int dim = col();
	for (int r =0; r&lt; dim; r++)
	{
		for (int c = r; c&lt; dim; c++)
		{
			if (items(r,c)&amp;&amp;items(c,r))
			{
				if (r != c)
				{
					return false;
				}
			}
		}
	}
	return true;
}

bool LogicMatrix::transitive()
{
	int dim = col();
	for (int r =0; r&lt; dim; r++)
	{
		for (int c=0; c&lt; dim; c++)
		{
			if (items(r,c))//if find an entry which is 1's
			{
				for (int i =0; i&lt; dim; i++) // try to find in c'th row 
				{
					if (items(c, i))
					{
						if (!items(r, i))
						{
							return false;
						}
					}
				}
			}
		}
	}
	return true;
}

Matrix&amp; LogicMatrix::operator &amp;&amp;(Matrix otherMatrix)
{
	bool dummy=false;
	int dim = col();  //as this function is used so many times, better use variable, 
	for (int r =0; r&lt;dim; r++)
	{
		for (int c =0; c&lt;dim; c++)
		{
			for (int i=0; i&lt;dim; i++)
			{
				dummy = dummy||(items(r,i)&amp;&amp;otherMatrix.items(i, c));
			}
			items(r, c) = dummy;
			dummy = false;
		}
	}
	return *this;
}

Matrix&amp; LogicMatrix::power(int exponent)
{
	Matrix N;
	N = *this; //I have to copy a temp Matrix to keep original one
	for (int i=1; i&lt;exponent; i++)
	{
		this-&gt;operator &amp;&amp;(N);
	}
	return (*this);
}

Matrix&amp; Matrix::transposition()
{
	double hold;
	int temp;
	for (int r =0; r&lt; rowNum; r++)
	{
		for (int c=0; c&lt; r; c++)
		{
			hold = lst[r][c];
			lst[r][c] = lst[c][r];
			lst[c][r] = hold;
		}
	}
	temp = rowNum;
	rowNum = colNum;
	colNum = temp;
	return (*this);
}


Matrix&amp; MathMatrix::operator +(Matrix other)
{
	if (row()!= other.row() || col()!= other.col())
	{
		cout&lt;&lt;&quot;\nTwo matrix has different row or col number!\n&quot;;
		return (*this);
	}
	else
	{
		for (int r=0; r&lt; row(); r++)
		{
			for (int c=0; c&lt; col(); c++)
			{
				items(r, c) +=other.items(r, c);
			}
		}
		return (*this);
	}
}

Matrix&amp; MathMatrix::operator *(Matrix other)
{
	double total =0;
	Matrix temp;
	temp.setRow(row());
	temp.setCol(other.col());

	if (col()!=other.row())
	{
		cout&lt;&lt;&quot;\nrow &amp; col are not same!\n&quot;;
	
	}
	else
	{
		for (int r =0; r&lt; row(); r++)
		{
			for (int c=0; c&lt;other.col(); c++)
			{
				total =0;
				for (int i=0; i&lt;col(); i++)
				{
					total += items(r,i) * other.items(i, c);
				}
				temp.items(r, c) = total;
			}
		}
		(Matrix)(*this) = temp;
	
	}
	return (*this);
}
				
Matrix&amp; Matrix::operator =(Matrix other)
{
	setRow(other.row());
	setCol(other.col());
	for (int r=0; r&lt; other.row(); r++)
	{
		for (int c=0; c&lt; other.col(); c++)
		{
			lst[r][c] = other.items(r, c);
		}
	}
	
	return (*this);
}

void Matrix::mul(int dest, double scalor)
{
	for (int c=0; c&lt; colNum; c++)
	{
		lst[dest][c] *= scalor;
	}
}

Matrix&amp; MathMatrix::operator *(double i)
{
	for (int r=0; r&lt;row(); r++)
	{
		for (int c=0; c&lt;col(); c++)
		{
			items(r, c) *= i;
		}
	}
	return (*this);
}

void MathMatrix::echelon(int r, bool reduced)
{
	int c=r;
	if (r&lt;row())
	{
		while (c&lt; col())
		{
			if (items(r, c) ==0)
			{
				c++;
			}
			else
			{
				mul(r, 1/items(r,c));   //make it 1 for this row
				for (int i=(!reduced?r+1:0); i&lt; row(); i++)
				{
					if (i!=r)
					{
						mul(r, i, -items(i,c));
					}
				}
				echelon(r+1, reduced);
				break;
			}
		}
	}
	
}

void Matrix::mul(int source, int dest, double scalor)
{
	for (int c=0; c&lt; colNum; c++)
	{
		lst[dest][c] += lst[source][c]*scalor;
	}
}


double&amp; Matrix::items(int r, int c)
{
	return lst[r][c];
}

void Matrix::readFromFile(const char* fileName)
{
	int r=0, c=0;
	char ch;
	ifstream f;
	f.open(fileName);
	while (!f.eof())
	{
		ch = f.peek();
		
		if (ch!=10)  //return char
		{
			
			f&gt;&gt;lst[r][c];
			c++;
			if (c&gt;colNum)
				colNum = c;
		}
		else
		{
			f.ignore();
			r++;
			setCol(c);
			c =0;
		}
	}
	if (r!=0)
	{
		setRow(r+1);
	}
}


void Matrix::initialize()
{
	for (int r=0; r &lt; rowNum; r++)
	{
		for (int c=0; c&lt; colNum; c++)
		{
			lst[r][c] = r*2+c;
		}
	}	
}


void Matrix::display()
{
//	int temp;
	long preFlag;
	preFlag = cout.flags();
//	temp = cout.precision(4);
//	cout.setf(ios::fixed);
	
	cout&lt;&lt;&quot;row\\col&quot;;
	for (int c=0; c&lt; colNum; c++)
	{
		cout&lt;&lt;&quot;  &quot;&lt;&lt;c;
	}
	cout&lt;&lt;&quot;\n\n&quot;;
	for (int r = 0; r&lt; rowNum; r++)
	{
		cout&lt;&lt;r&lt;&lt;&quot;\t &quot;;
		for (c = 0; c&lt; colNum; c++)
		{
			cout&lt;&lt;(fabs(lst[r][c])&lt;LIMIT?0:lst[r][c])&lt;&lt;&quot;  &quot;;			
		}
		cout&lt;&lt;endl;
	}
//	cout.precision(temp);
	cout.flags(preFlag);
}

Matrix::Matrix()
{
	rowNum = 5;
	colNum = 5;
	initialize();
}
</pre>
<pre>
</pre>

<p>¡¡          


</p>

<p><span lang="en-ca">The original matrix is like <a href="nick.txt">following</a>:<a name="output"></a></span></p>

<p>1 1 0 0 1<br>
0 1 1 1 0<br>
0 0 1 0 1<br>
0 1 0 0 0<br>
0 0 0 0 0</p>

<p>¡¡</p>

<p><span lang="en-ca">And the output file is like <a href="nickMatrix.txt">
following</a>:</span></p>

<pre>reflexive is :0
symmetric is :0
antisymmetric is:0
transitive is:0
row\col  0  1  2  3  4

0	 1  1  0  0  1  
1	 0  1  1  1  0  
2	 0  0  1  0  1  
3	 0  1  0  0  0  
4	 0  0  0  0  0  
transitive closure is
row\col  0  1  2  3  4

0	 1  1  1  1  1  
1	 0  1  1  1  1  
2	 0  0  1  0  1  
3	 0  1  1  1  1  
4	 0  0  0  0  0  
symmetric Closure is
row\col  0  1  2  3  4

0	 1  1  0  0  1  
1	 1  1  1  1  0  
2	 0  1  1  0  1  
3	 0  1  0  0  0  
4	 1  0  1  0  0  
reflextive closure is
row\col  0  1  2  3  4

0	 1  1  0  0  1  
1	 0  1  1  1  0  
2	 0  0  1  0  1  
3	 0  1  0  1  0  
4	 0  0  0  0  1  
</pre>

<p><span lang="en-ca"> <br>
¡¡</span></p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="Monopoly.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>