Skip to content

Instantly share code, notes, and snippets.

@mntone
Created August 17, 2012 23:26
Show Gist options
  • Save mntone/3383444 to your computer and use it in GitHub Desktop.
Save mntone/3383444 to your computer and use it in GitHub Desktop.
class 3x3 Matrix for Visual C++ 2012
#pragma once
#include <iostream>
namespace Mntone
{
// 3 次正方行列を扱うクラス
template<typename T>
class Matrix sealed
{
public:
T m11_, m12_, m13_, m21_, m22_, m23_, m31_, m32_, m33_;
// デフォルトコンストラクター
Matrix( void ){};
// コンストラクター
Matrix(
T m11, T m12, T m13,
T m21, T m22, T m23,
T m31, T m32, T m33 ):
m11_( m11 ), m12_( m12 ), m13_( m13 ),
m21_( m21 ), m22_( m22 ), m23_( m23 ),
m31_( m31 ), m32_( m32 ), m33_( m33 )
{
};
// コピーコンストラクター
Matrix( const Matrix& m ):
m11_( m.m11_ ), m12_( m.m12_ ), m13_( m.m13_ ),
m21_( m.m21_ ), m22_( m.m22_ ), m23_( m.m23_ ),
m31_( m.m31_ ), m32_( m.m32_ ), m33_( m.m33_ )
{
};
// デストラクター
~Matrix( void ){};
// 代入
Matrix& operator=( const Matrix& other_matrix )
{
m11_ = other_matrix.m11_; m12_ = other_matrix.m12_; m13_ = other_matrix.m13_;
m21_ = other_matrix.m21_; m22_ = other_matrix.m22_; m23_ = other_matrix.m23_;
m31_ = other_matrix.m31_; m32_ = other_matrix.m32_; m33_ = other_matrix.m33_;
return *this;
};
// 等価
bool operator==( const Matrix& other_matrix ) const
{
return m11_ == other_matrix.m11_ && m12_ == other_matrix.m12_ && m13_ == other_matrix.m13_
&& m21_ == other_matrix.m21_ && m22_ == other_matrix.m22_ && m23_ == other_matrix.m23_
&& m31_ == other_matrix.m31_ && m32_ == other_matrix.m32_ && m33_ == other_matrix.m33_;
};
// 足し算 (Matrix + Matrix)
Matrix operator+( const Matrix& other_matrix ) const
{
Matrix out_matrix(
m11_ + other_matrix.m11_, m12_ + other_matrix.m12_, m13_ + other_matrix.m13_,
m21_ + other_matrix.m21_, m22_ + other_matrix.m22_, m23_ + other_matrix.m23_,
m31_ + other_matrix.m31_, m32_ + other_matrix.m32_, m33_ + other_matrix.m33_ );
return out_matrix;
};
// 足し算 (+= Matrix)
Matrix& operator+=( const Matrix& other_matrix ) const
{
m11_ += other_matrix.m11_; m12_ += other_matrix.m12_; m13_ += other_matrix.m13_;
m21_ += other_matrix.m21_; m22_ += other_matrix.m22_; m23_ += other_matrix.m23_;
m31_ += other_matrix.m31_; m32_ += other_matrix.m32_; m33_ += other_matrix.m33_;
return *this;
};
// 引き算 (Matrix - Matrix)
Matrix operator-( const Matrix& other_matrix ) const
{
Matrix out_matrix(
m11_ - other_matrix.m11_, m12_ - other_matrix.m12_, m13_ - other_matrix.m13_,
m21_ - other_matrix.m21_, m22_ - other_matrix.m22_, m23_ - other_matrix.m23_,
m31_ - other_matrix.m31_, m32_ - other_matrix.m32_, m33_ - other_matrix.m33_ );
return out_matrix;
};
// 引き算 (-= Matrix)
Matrix& operator-=( const Matrix& other_matrix ) const
{
m11_ -= other_matrix.m11_; m12_ -= other_matrix.m12_; m13_ -= other_matrix.m13_;
m21_ -= other_matrix.m21_; m22_ -= other_matrix.m22_; m23_ -= other_matrix.m23_;
m31_ -= other_matrix.m31_; m32_ -= other_matrix.m32_; m33_ -= other_matrix.m33_;
return *this;
};
// 掛け算 (Matrix * Matrix)
Matrix operator*( const Matrix& other_matrix ) const
{
Matrix out_matrix;
out_matrix.m11_ = m11_ * other_matrix.m11_ + m12_ * other_matrix.m21_ + m13_ * other_matrix.m31_;
out_matrix.m12_ = m11_ * other_matrix.m12_ + m12_ * other_matrix.m22_ + m13_ * other_matrix.m32_;
out_matrix.m13_ = m11_ * other_matrix.m13_ + m12_ * other_matrix.m23_ + m13_ * other_matrix.m33_;
out_matrix.m21_ = m21_ * other_matrix.m11_ + m22_ * other_matrix.m21_ + m23_ * other_matrix.m31_;
out_matrix.m22_ = m21_ * other_matrix.m12_ + m22_ * other_matrix.m22_ + m23_ * other_matrix.m32_;
out_matrix.m23_ = m21_ * other_matrix.m13_ + m22_ * other_matrix.m23_ + m23_ * other_matrix.m33_;
out_matrix.m31_ = m31_ * other_matrix.m11_ + m32_ * other_matrix.m21_ + m33_ * other_matrix.m31_;
out_matrix.m32_ = m31_ * other_matrix.m12_ + m32_ * other_matrix.m22_ + m33_ * other_matrix.m32_;
out_matrix.m33_ = m31_ * other_matrix.m13_ + m32_ * other_matrix.m23_ + m33_ * other_matrix.m33_;
return out_matrix;
};
// スカラー倍 (Matrix * T)
Matrix operator*( const T& constant_value ) const
{
Matrix out_matrix(
m11_ * constant_value, m12_ * constant_value, m13_ * constant_value,
m21_ * constant_value, m22_ * constant_value, m23_ * constant_value,
m31_ * constant_value, m32_ * constant_value, m33_ * constant_value );
return out_matrix;
};
// スカラー倍 (*= T)
Matrix& operator*=( const T& constant_value ) const
{
m11_ *= other_matrix.m11_; m12_ *= other_matrix.m12_; m13_ *= other_matrix.m13_;
m21_ *= other_matrix.m21_; m22_ *= other_matrix.m22_; m23_ *= other_matrix.m23_;
m31_ *= other_matrix.m31_; m32_ *= other_matrix.m32_; m33_ *= other_matrix.m33_;
return *this;
};
// スカラー倍 (Matrix / T)
Matrix operator/( const T& constant_value ) const
{
Matrix out_matrix(
m11_ / constant_value, m12_ / constant_value, m13_ / constant_value,
m21_ / constant_value, m22_ / constant_value, m23_ / constant_value,
m31_ / constant_value, m32_ / constant_value, m33_ / constant_value );
return out_matrix;
};
// スカラー倍 (/= T)
Matrix& operator/=( const T& constant_value ) const
{
m11_ /= other_matrix.m11_; m12_ /= other_matrix.m12_; m13_ /= other_matrix.m13_;
m21_ /= other_matrix.m21_; m22_ /= other_matrix.m22_; m23_ /= other_matrix.m23_;
m31_ /= other_matrix.m31_; m32_ /= other_matrix.m32_; m33_ /= other_matrix.m33_;
return *this;
};
};
// スカラー倍 (T * Matrix)
template<typename T>
Matrix<T> operator*( const T& constant_value, const Matrix<T>& m )
{
Matrix<T> out_matrix( m );
out_matrix.m11_ *= constant_value;
out_matrix.m12_ *= constant_value;
out_matrix.m13_ *= constant_value;
out_matrix.m21_ *= constant_value;
out_matrix.m22_ *= constant_value;
out_matrix.m23_ *= constant_value;
out_matrix.m31_ *= constant_value;
out_matrix.m32_ *= constant_value;
out_matrix.m33_ *= constant_value;
return out_matrix;
};
// 行列式 determinant
template<typename T>
T determinant( const Matrix<T>& m )
{
return
m.m11_ * ( m.m22_ * m.m33_ - m.m23_ * m.m32_ )
- m.m12_ * ( m.m21_ * m.m33_ - m.m23_ * m.m31_ )
+ m.m13_ * ( m.m21_ * m.m32_ - m.m22_ * m.m31_ );
};
// 出力演算子 (char)
template<typename T>
std::ostream& operator<<( std::ostream& stream, const Matrix<T>& m )
{
stream << "["
<< m.m11_ << " " << m.m12_ << " " << m.m13_ << ", "
<< m.m21_ << " " << m.m22_ << " " << m.m23_ << ", "
<< m.m31_ << " " << m.m32_ << " " << m.m33_ << "]";
return stream;
};
// 出力演算子 (wchar_t)
template<typename T>
std::wostream& operator<<( std::wostream& stream, const Matrix<T>& m )
{
stream << L"["
<< m.m11_ << L" " << m.m12_ << L" " << m.m13_ << L", "
<< m.m21_ << L" " << m.m22_ << L" " << m.m23_ << L", "
<< m.m31_ << L" " << m.m32_ << L" " << m.m33_ << L"]";
return stream;
};
// 定数 零行列
const Matrix<char> zero_matrix_c( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<short> zero_matrix_s( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<int> zero_matrix( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<long> zero_matrix_l( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<long long> zero_matrix_ll( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<float> zero_matrix_f( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
const Matrix<double> zero_matrix_d( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
// 定数 単位行列
const Matrix<char> unit_matrix_c( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<short> unit_matrix_s( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<int> unit_matrix( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<long> unit_matrix_l( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<long long> unit_matrix_ll( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<float> unit_matrix_f( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
const Matrix<double> unit_matrix_d( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
}; // namespace Mntone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment