关闭

R4 欧氏空间Schmidt正交化方法

68人阅读 评论(0) 收藏 举报
分类:
#include<iostream>
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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3342次
    • 积分:360
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类