R4 欧氏空间Schmidt正交化方法

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值