R4 欧氏空间Schmidt正交化方法

#include
using namespace std;

class Euclidean{
public:
	Euclidean(double x1 = 0, double x2 = 0, double x3 = 0, double x4 = 0) :
		x1(x1), x2(x2), x3(x3), x4(x4){}
	friend Euclidean operator-(Euclidean &a1, Euclidean &a2);
	friend Euclidean operator*(double a, Euclidean &a1);
	friend ostream & operator<<(ostream &out, const Euclidean &a);
	friend double Cal(Euclidean &a1, Euclidean &a2);
	void Assignment(double a1, double a2, double a3, double a4)
	{
		this->x1 = a1;
		this->x2 = a2;
		this->x3 = a3;
		this->x4 = a4;
	}
private:
	double x1,x2,x3,x4;
};

Euclidean operator-(Euclidean &a1, Euclidean &a2)
{
	return Euclidean(a1.x1 - a2.x1, a1.x2 - a2.x2, a1.x3 - a2.x3, a1.x4 - a2.x4);
}

Euclidean operator*(double a, Euclidean &a1)
{
	return Euclidean (a*a1.x1, a*a1.x2, a*a1.x3, a*a1.x4);
}

ostream & operator<<(ostream &out, const Euclidean &a)
{
	out << "(" << a.x1 << "," << a.x2 << "," << a.x3 << "," << a.x4 << ")";
	return out;
}

double Cal(Euclidean &a1, Euclidean &a2)
{
	return ((a1.x1*a2.x1 + a1.x2*a2.x2 + a1.x3*a2.x3 + a1.x4*a2.x4) /
		(a2.x1*a2.x1 + a2.x2*a2.x2 + a2.x3*a2.x3 + a2.x4*a2.x4));
}

int main()
{
	cout << "Please enter the number of vectors(n<=4): ";
	int n;
	cin >> n;

	Euclidean*a = new Euclidean[n];
	double aa[4];

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < 4; j++)
			cin >> aa[j];
		a[i].Assignment(aa[0], aa[1], aa[2], aa[3]);
	}

	Euclidean*p = new Euclidean[n];

	for (int i = 0; i < n; i++)
	{
		p[i] = a[i];
		for (int j = 0; j < i; j++)
			p[i] = p[i]-Cal(a[i], p[j])*p[j];
	}

	for (int i = 0; i < n; i++)
		cout << p[i] << endl;

	delete[]a;
	delete[]p;

	return 0;
}

你可能感兴趣的:(小问题)