xxvector

来自「矩阵计算的c++代码」· 代码 · 共 139 行

TXT
139
字号
// xxVector internal header
#pragma once
#ifndef _XXVECTOR_
#define _XXVECTOR_

//	additional friend operations of vector
//+++++++++++++++++++++++++++++++++++++++++++++++
const CVector operator+(const CVector& left, __VectorType__ right)
{
	CVector _result(left);
	_result += right;
	return _result;
}
const CVector operator+(__VectorType__ left, const CVector& right)
{	return right + left;	}
const CVector operator+(const CVector& left, const CVector& right)
{
	CVector _result(left);
	_result += right;
	return _result;
}

//-----------------------------------------------
const CVector operator-(const CVector& left, __VectorType__ right)
{
	CVector _result(left);
	_result -= right;
	return _result;
}
const CVector operator-(__VectorType__ left, const CVector& right)
{	return (-right) + left;	}
const CVector operator-(const CVector& left, const CVector& right)
{
	CVector _result(left);
	_result -= right;
	return _result;
}

//***********************************************
const CVector operator*(const CVector& left, __VectorType__ right)
{
	CVector _result(left);
	_result *= right;
	return _result;
}
const CVector operator*(__VectorType__ left, const CVector& right)
{	return right * left;	}
const CVector operator*(const CVector& left, const CVector& right)
{
	assert(left.size() == right.size());
	if(left.IsEmpty())	return CVector();

	CVector _result(left);

	for(int i=0; i<right.size(); ++i)
	{	_result[i] *= right[i];	}
	return _result;
}

/////////////////////////////////////////////////
const CVector operator/(const CVector& left, __VectorType__ right)
{
	CVector _result(left);
	_result /= right;
	return _result;
}
const CVector operator/(__VectorType__ left, const CVector& right)
{
	CVector _result(right.size());

	for(int i=0; i<right.size(); ++i)
	{
		assert( right[i]!=0.0 );
		_result[i] = left / right[i];
	}
	return _result;
}
const CVector operator/(const CVector& left, const CVector& right)
{
	assert(left.size() == right.size());
	if(left.IsEmpty())	return CVector();

	CVector _result(left);

	for(int i=0; i<right.size(); ++i)
	{
		assert( right[i] != 0.0 );
		_result[i] /= right[i];
	}
	return _result;
}

const CVector abs(const CVector& src)
{
	if(src.IsEmpty())
	{	return CVector();	}

	int i, _M = src.size();
	CVector _result(_M);

	for(i=0; i<_M; ++i)
	{	_result[i] = std::fabs(src.At(i));	}
	return _result;
}
const CVector sqrt(const CVector& src)
{
	if(src.IsEmpty())
	{	return CVector();	}

	int i, _M = src.size();
	CVector _result(_M);

	for(i=0; i<_M; ++i)
	{
		assert( !(src[i]<0.0) );
		_result[i] = std::sqrt(src.At(i));
	}
	return _result;
}

//	check whether the both matrices equal
bool operator == (const CVector& left, const CVector& right)
{
	int i, _M, _N;
	_M = left.size();	_N = left.size();

	if( _M != _N )		return false;
	if(left.IsEmpty())	return true;
	if(&left == &right)	return true;	// 检查是否是自己

	for(i=0; i<_M; ++i)
	{	if(left[i] != right[i])	return false;	}

	return true;
}
bool operator != (const CVector& left, const CVector& right)
{	return !(left == right);	}

#endif // _XXVECTOR_

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?