ルギア君の戯言

雑多な記事。

ベクトル

//
// C++ Implementation: mvector.h
//
// Description: Mathematical Vector Calculation
//
//
// Author: Lugia Kun <lugia.kun@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifndef _MVECTOR_H_
#define _MVECTOR_H_

#ifdef __cplusplus
#include <vector>
#else
#error mvector.h is only for C++.
#endif // __cplusplus

namespace std {

template <class _Tp, class _Alloc = std::allocator<_Tp> >
class mvector : public vector<_Tp, _Alloc> {

	typedef mvector<_Tp, _Alloc> _mvector;
	typedef vector<_Tp, _Alloc> _vector;

	typedef _Vector_base<_Tp, _Alloc> _Base;
	typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;

    public:
	typedef _Tp value_type;
	typedef typename _Tp_alloc_type::pointer pointer;
	typedef typename _Tp_alloc_type::const_pointer const_pointer;
	typedef typename _Tp_alloc_type::reference reference;
	typedef typename _Tp_alloc_type::const_reference const_reference;
	typedef __gnu_cxx::__normal_iterator<pointer, _vector> iterator;
	typedef __gnu_cxx::__normal_iterator<const_pointer, _vector>
	const_iterator;
	typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
	typedef std::reverse_iterator<iterator> reverse_iterator;
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;
	typedef _Alloc allocator_type;
	
	explicit mvector() : _vector() { }
	explicit mvector(int n) : _vector(n) { }
	explicit mvector(int n, _Tp x) : _vector(n, x) { }
	template <class _InputIterator>
	mvector(_InputIterator first, _InputIterator last, const _Alloc& al = _Alloc())
		: _vector(first, last, al) { }
	mvector(const mvector& x) : _vector((_vector&)x) { }

	~mvector() { ~_vector(); }
	
	_mvector  operator+(_mvector b){ }	// addition
	_mvector  operator-(_mvector b){ }	// substraction
	_mvector  operator-(){ }		// Multiply -1.
	_mvector  operator*(_Tp b){ }		// Scalor Multiplication
	_mvector  operator/(_Tp b){ }		// Scalor Division
	     _Tp  operator*(_mvector b){ }	// Inner Product.

	template <class T, class A>
	friend mvector<T, A> operator*(mvector<T, A>* a, mvector<T, A> b);
						// Outer Product.

	_mvector  operator*(){ }		// Pointer of mvector.
	_mvector* operator*(int){ return this; }// c = a ** b will be Outer Product.
						// (Also can be c = &a * b)
};

}

#endif // _MVECTOR_H_

数学用のベクトル計算用のヘッダです。まだ、何も実装していません。


いまのところ、基本的な演算(加減、スカラー倍、内積、外積)を盛り込んであります。

c = a + b;  // 加算
c = a - b;  // 減算
c = -a;     // 逆方向
c = a * k;  // スカラー倍 (スカラーを後ろからかけることに注意)
c = a / k;  // スカラー倍 (1/k 倍)
k = a * b;  // 内積
c = a ** b; // 外積 (この記述にするために工夫してあります)
c = &a * b; // この記述も外積になります。


以上です。本体の実装とかはまた今度。


あと、このソースコードは g++ じゃないと通りません。(VC++ では使えません)