<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>failure</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; <span lang="en-ca">template vector 
(unsuccessful)</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is just for fun. My memory is so bad that I forget what I write one month ago. And this is actually a </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>similar approach compared with my previous one. (I discovered the previous &quot;workspace&quot; accidentally.)</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <span lang="en-ca"><b>I am learning linear algebra and want to describe this 
  topic in c++. But it is not so easy..</b> </span>
  <p>
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>There is several failed 
points. </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>First the &quot;...&quot; operator is 
not reliable when the data type of template is double. I cannot figure out how 
c++</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>passes parameter when the 
type is double.</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>Second, I originally want 
to reduce the basis to an echelon format and then use it to test if a vector is 
inside</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>the space. But it is not 
right? I am confused about the algorithm.</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>Third the division of 
double and integer is always a painful issue. During reduction procedure of 
echelon </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>format, I want to avoid 
integer division and use multiplication instead of division. Then the integer is 
quickly </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>busted. </b></font></span>
</p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>How to describe a linear 
operator F: V---&gt;U&nbsp; where U, V are vector space?</b></font></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">1. vector.h</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2</b></font></span><font size="3"><b><span lang="en-ca">. main.</span></b></font><span lang="en-ca"><font size="3"><b>cpp</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: vector.h</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;iomanip&gt;

using namespace std;

const double Limit=1000;

template&lt;class T, int size&gt;
class Vector
{	
protected:
	T array[size];//the number of tuple
public:
	Vector(T elem, ...);
	Vector();
	Vector(const Vector&amp; other);//copy constructor
	Vector&amp; operator=(const Vector&amp; other);
	bool operator==(const Vector&amp; other);
	bool operator!=(const Vector&amp; other){return !this-&gt;operator==(other);}
	Vector&amp; operator+(const Vector&amp; other);
	Vector&amp; operator-(const Vector&amp; other);
	Vector&amp; operator*(T scalar);
	Vector&amp; operator/(T scalar);
	T operator[](int index);
	void display() const;
	void display();
	void shrink();
};

template&lt;class T, int size&gt;
ostream&amp; operator&lt;&lt;(ostream&amp; out, const Vector&lt;T, size&gt;&amp; vector);

template&lt;class T, int size, int dimension&gt;
class VectorArray
{
protected:
	//the two size have different meaning in math
	//since the maximum dimension of Vector is size
	//and I cannot use object array because Vector is not instanciated
	//pointer array is the only choice
	Vector&lt;T, size&gt;* array[dimension];
public:
	//I must pass value instead of reference, otherwise the pointer won't be right
	VectorArray(Vector&lt;T, size&gt; vector1, ...);
	VectorArray();
	VectorArray&amp; operator=(const VectorArray&amp; other);
	Vector&lt;T, size&gt;&amp; operator[](int index);
	VectorArray(VectorArray&amp; other);
	~VectorArray();
	void display()const;
	void display();

};

template&lt;class T, int size, int dimension&gt;
class Space
{
protected:
	VectorArray&lt;T, size, dimension&gt; basis; 
	VectorArray&lt;T, size, dimension&gt; echelon;
public:
	//must pass by value
	void simplify();
	Space(Vector&lt;T, size&gt; vector1, ...);
	void display();
	bool belong(Vector&lt;T, size&gt;&amp; vector);
};

template&lt;class T, int size, int dimension&gt;
ostream&amp; operator&lt;&lt;(ostream&amp; out, const VectorArray&lt;T, size, dimension&gt;&amp; dummy);


template&lt;class T, int size, int dimension&gt;
void Space&lt;T, size, dimension&gt;::display()
{
	printf(&quot;the original basis:\n&quot;);
	basis.display();
	printf(&quot;the echelon basis:\n&quot;);
	echelon.display();
}

template&lt;class T, int size, int dimension&gt;
bool Space&lt;T, size, dimension&gt;::belong(Vector&lt;T, size&gt;&amp; vector)
{
	double var[size];
	for (int i=size-1; i&gt;=0; i--)
	{
		var[i]=vector[i];
		for (int j=i+1; j&lt;size; j++)
		{
			var[i]-=var[j]*echelon[i][j];
		}
		if (echelon[i][i]!=0)
		{
			var[i]/=echelon[i][i];
		}
	}
	return var[0]==vector[0];	
}

template&lt;class T, int size, int dimension&gt;
void Space&lt;T, size, dimension&gt;::simplify()
{
	Vector&lt;T, size&gt; temp;
	T top, bottom;
	echelon=basis;
	for (int c=0; c&lt;size; c++)
	{		
		if (c&gt;=dimension)
		{
			break;
		}
		//swap for non-zero vector
		if (echelon[c][c]==0)
		{
			for (int i=c; i&lt;dimension; i++)
			{
				if (echelon[i][c]!=0)
				{
					temp=echelon[c];
					echelon[c]=echelon[i];
					echelon[i]=temp;
					break;
				}
			}
		}
		/*
		if (echelon[c][c]&gt;Limit)
		{
			echelon[c]/echelon[c][c];
		}
		*/
		for (int r=c+1; r&lt;dimension; r++)
		{
			//display();
			echelon[c].shrink();
			echelon[r].shrink();
			temp=echelon[c];//the top vector
			top=echelon[r][c];//
			/*
			if (top&gt;Limit)
			{
				echelon[r]/top;
				top=1;
			}
			*/
			bottom=echelon[c][c];
			temp*top;//scale
			echelon[r]*bottom;
			echelon[r]-temp;
		}
	}
}

template&lt;class T, int size, int dimension&gt;
Space&lt;T, size, dimension&gt;::Space&lt;T, size, dimension&gt;(Vector&lt;T, size&gt; vector1, ...)
{
	Vector&lt;T, size&gt;* ptr=&amp;vector1;
	for (int i=0; i&lt;dimension; i++)
	{
		//reference, not pointer here by overloading []
		basis[i]=*ptr;
		ptr++;
	}
}


template&lt;class T, int size, int dimension&gt;
VectorArray&lt;T, size, dimension&gt;::VectorArray&lt;T, size, dimension&gt;
(VectorArray&lt;T, size, dimension&gt;&amp; other)
{
	for (int i=0; i&lt;dimension; i++)
	{
		array[i]=new Vector&lt;T, size&gt;;
		*array[i]=*other.array[i];
	}
}

template&lt;class T, int size, int dimension&gt;
VectorArray&lt;T, size, dimension&gt;&amp; VectorArray&lt;T, size, dimension&gt;::operator=
	(const VectorArray&lt;T, size, dimension&gt;&amp; other)
{
	for (int i=0; i&lt;dimension; i++)
	{
		*array[i]=*other.array[i];
	}
	return *this;
}

template&lt;class T, int size, int dimension&gt;
Vector&lt;T, size&gt;&amp; VectorArray&lt;T, size, dimension&gt;::operator [](int index)
{
	if (index&lt;dimension&amp;&amp;index&gt;=0)
	{
		return *array[index];
	}
	else
	{
		//printf(&quot;index exceeds size\n&quot;);
		throw &quot;index exceeds size&quot;;
	}
}

template&lt;class T, int size, int dimension&gt;
VectorArray&lt;T, size, dimension&gt;::VectorArray&lt;T, size, dimension&gt;()
{
	for (int i=0; i&lt;dimension; i++)
	{
		array[i]=new Vector&lt;T, size&gt;;
	}
}

template&lt;class T, int size, int dimension&gt;
VectorArray&lt;T, size, dimension&gt;::~VectorArray&lt;T, size, dimension&gt;()
{
	for (int i=0; i&lt;dimension; i++)
	{
		delete array[i];
	}
}

template&lt;class T, int size, int dimension&gt;
VectorArray&lt;T, size, dimension&gt;::VectorArray&lt;T, size, dimension&gt;(Vector&lt;T, size&gt; vector1, ...)
{
	Vector&lt;T, size&gt;* ptr=&amp;vector1;
	for (int i=0; i&lt;dimension; i++)
	{
		array[i]=new Vector&lt;T, size&gt;;
		*array[i]=*ptr;
		ptr++;
	}
}

template&lt;class T, int size, int dimension&gt;
ostream&amp; operator&lt;&lt;(ostream&amp; out, const VectorArray&lt;T, size, dimension&gt;&amp; dummy)
{
	dummy.display();
	return out;
}

template&lt;class T, int size, int dimension&gt;
void VectorArray&lt;T, size, dimension&gt;::display() const
{
	for (int i=0; i&lt;dimension; i++)
	{
		array[i]-&gt;display();
	}
}

template&lt;class T, int size, int dimension&gt;
void VectorArray&lt;T, size, dimension&gt;::display()
{
	for (int i=0; i&lt;dimension; i++)
	{
		array[i]-&gt;display();
	}
}

template&lt;class T, int size&gt;
ostream&amp; operator &lt;&lt;(ostream&amp; out, const Vector&lt;T, size&gt;&amp; vector)
{
	vector.display();
	return out;
}

template&lt;class T, int size&gt;
T Vector&lt;T, size&gt;::operator [](int index)
{
	if (index&lt;size&amp;&amp;index&gt;=0)
	{
		return array[index];
	}
	else
	{
		throw &quot;index exceeds size&quot;;
	}
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;::Vector&lt;T, size&gt;(const Vector&lt;T, size&gt;&amp; other)
{
	for (int i=0; i&lt;size; i++)
	{
		array[i]=other.array[i];
	}
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;&amp; Vector&lt;T, size&gt;::operator- (const Vector&lt;T, size&gt;&amp; other)
{
	//I have to write like this, otherwise, I need to create a 
	//temp variable of &quot;other&quot; since my &quot;+&quot;, &quot;*&quot; is changed
	for (int i=0; i&lt;size; i++)
	{
		array[i]-=other.array[i];
	}
	return *this;
}

//I changed my mind since this has too benefit: efficiency, and 
//easy in echelon operation
template&lt;class T, int size&gt;
Vector&lt;T, size&gt;&amp; Vector&lt;T, size&gt;::operator*(T scalar)
{
	//Vector&lt;T, size&gt; result;
	for (int i=0; i&lt;size; i++)
	{
		array[i]=array[i]*scalar;
	}
	return *this;
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;&amp; Vector&lt;T, size&gt;::operator+(const Vector&lt;T, size&gt;&amp; other)
{
	//Vector&lt;T, size&gt; result;
	for (int i=0; i&lt;size; i++)
	{
		array[i]=array[i]+other.array[i];
	}
	return *this;
}


template&lt;class T, int size&gt;
bool Vector&lt;T, size&gt;::operator ==(const Vector&lt;T, size&gt;&amp; other)
{
	//here we don't even have to check the size of both vector
	//because the operator overloading require same &quot;size&quot;
	for (int i=0; i&lt;size; i++)
	{
		if (array[i]!=other.array[i])
		{
			return false;
		}
	}
	return true;
}


template&lt;class T, int size&gt;
Vector&lt;T, size&gt;&amp; Vector&lt;T, size&gt;::operator =(const Vector&lt;T, size&gt;&amp; other)
{
	for (int i=0; i&lt;size; i++)
	{
		array[i]=other.array[i];
	}
	return *this;
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;::Vector()
{
	for (int i=0; i&lt;size; i++)
	{
		array[i]=0;
	}
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;::Vector&lt;T, size&gt;(T elem, ...)
{
	T* ptr=&amp;elem;
	for (int i=0; i&lt;size; i++)
	{
		array[i]=*ptr;
		ptr++;
	}
}

template&lt;class T, int size&gt;
Vector&lt;T, size&gt;&amp; Vector&lt;T, size&gt;::operator /(T scalar)
{
	if (scalar!=0)
	{
		for (int i=0; i&lt;size; i++)
		{
			array[i]/=scalar;
		}
	}
	return *this;
}


template&lt;class T, int size&gt;
void Vector&lt;T, size&gt;::display()const
{
	cout.setf(ios_base::fixed||ios_base::left);
	//cout.width(2);
	//cout.precision(2);
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot;, &quot;;
	}
	cout.setf(ios_base::fixed);
	//cout.width(2);
	//cout.precision(2);
	cout&lt;&lt;endl;
}

template&lt;class T, int size&gt;
void Vector&lt;T, size&gt;::display()
{
	//int previous=cout.precision(2);
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot;, &quot;;
	}
	//cout.precision(previous);
	cout&lt;&lt;endl;
}

template&lt;class T, int size&gt;
void Vector&lt;T, size&gt;::shrink()
{
	T small=0, big=0;
	for (int i=0; i&lt;size; i++)
	{
		if (small==0)
		{
			small=array[i];
		}
		if (abs(array[i])&gt;big)
		{
			big=abs(array[i]);
		}
		if (array[i]!=0&amp;&amp;abs(array[i])&lt;abs(small))
		{
			small=array[i];
		}
	}
	if (small!=0&amp;&amp;big&gt;Limit)
	{
		this-&gt;operator/(small);
	}
}
</pre>
<pre>


</pre>
<pre></pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main.cpp</b></font></span></pre>
<pre>#include &quot;vector.h&quot;


int main()
{
	Vector&lt;int, 5&gt; V(11, 23, 2, 14, 5);
	Vector&lt;int, 5&gt; W(2, 5, 7, 11, 23), U(3,12, 2, 22, 19);
	Vector&lt;int, 5&gt; X(6, 23, 22, 4, 9), Y(1,2,3,4,4), P(2,2,2,2,5);
	V.display();

	X.display();
	W.display();
	//VectorArray&lt;double, 5, 4&gt; vArray(U*4, V*6, X*9, W);
	//vArray.display();
	Space&lt;int, 5, 5&gt; S(X, W, U, V, Y);
	S.display();
	S.simplify();
	S.display();
	if (S.belong(V))
	{
		printf(&quot;ok\n&quot;);
	}

	return 0;
}</pre>
<pre>　</pre>
<pre>　</pre>
<pre></pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>

<pre>11, 23, 2, 14, 5,
6, 23, 22, 4, 9,
2, 5, 7, 11, 23,
the original basis:
6, 23, 22, 4, 9,
2, 5, 7, 11, 23,
3, 12, 2, 22, 19,
11, 23, 2, 14, 5,
1, 2, 3, 4, 4,
the echelon basis:
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
the original basis:
6, 23, 22, 4, 9,
2, 5, 7, 11, 23,
3, 12, 2, 22, 19,
11, 23, 2, 14, 5,
1, 2, 3, 4, 4,
the echelon basis:
6, 23, 22, 4, 9,
0, -16, -2, 58, 120,
0, 0, 1, -2, -2,
0, 0, 0, 3, 6,
0, 0, 0, 0, 27,
Press any key to continue</pre>

<pre>　</pre>

<pre></pre>

<pre></pre>

<pre></pre>

<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; <a href="PocketRuler.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>