From: Mikko Rasa Date: Thu, 30 Aug 2012 20:41:33 +0000 (+0300) Subject: Basic vector and matrix classes X-Git-Url: http://git.tdb.fi/?p=libs%2Fmath.git;a=commitdiff_plain;h=4d9e2d03503ba3d90633d2acdaadf64869bf2ab4 Basic vector and matrix classes Some classes are still incomplete --- 4d9e2d03503ba3d90633d2acdaadf64869bf2ab4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e00c88d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.config +/libmsplinal.so +/msplinal.pc +/temp diff --git a/Build b/Build new file mode 100644 index 0000000..51f75b9 --- /dev/null +++ b/Build @@ -0,0 +1,12 @@ +package "msplinal" +{ + library "msplinal" + { + source "source"; + install true; + install_map + { + map "source" "include/msp/linal"; + }; + }; +}; diff --git a/source/dummy.cpp b/source/dummy.cpp new file mode 100644 index 0000000..dd69a99 --- /dev/null +++ b/source/dummy.cpp @@ -0,0 +1,8 @@ +#include "vector.h" +#include "vector3.h" +#include "matrix.h" +#include "squarematrix.h" +#include "matrix4.h" + +/* This dummy file is needed to make Builder happy, and also serves as a syntax +check by pulling in all the headers. */ diff --git a/source/matrix.h b/source/matrix.h new file mode 100644 index 0000000..00e960d --- /dev/null +++ b/source/matrix.h @@ -0,0 +1,149 @@ +#ifndef MSP_LIEAL_MATMIX_H_ +#define MSP_LIEAL_MATMIX_H_ + +#include "vector.h" + +namespace Msp { +namespace LinAl { + +/** +A general mathematical matrix. +*/ +template +class Matrix +{ +private: + T data[M*N]; + +public: + Matrix(); + Matrix(const T *); + template + Matrix(const Matrix &); + static Matrix from_columns(const Vector *); + static Matrix from_rows(const Vector *); + + T &operator()(unsigned, unsigned); + const T &operator()(unsigned, unsigned) const; + + Matrix &operator*=(T); + Matrix &operator/=(T); + Matrix &operator+=(const Matrix &); + Matrix &operator-=(const Matrix &); + + Matrix transpose() const; +}; + +template +inline T &Matrix::operator()(unsigned i, unsigned j) +{ + return data[i+M*j]; +} + +template +inline const T &Matrix::operator()(unsigned i, unsigned j) const +{ + return data[i+M*j]; +} + +template +inline Matrix &Matrix::operator*=(T s) +{ + for(unsigned i=0; i +inline Matrix operator*(const Matrix &m, T s) +{ + Matrix r(m); + return r *= s; +} + +template +inline Matrix operator*(T s, const Matrix &m) +{ + return m*s; +} + +template +inline Matrix &Matrix::operator/=(T s) +{ + for(unsigned i=0; i +inline Matrix operator/(const Matrix &m, T s) +{ + Matrix r(m); + return r /= s; +} + +template +inline Matrix &Matrix::operator+=(const Matrix &m) +{ + for(unsigned i=0; i +inline Matrix operator+(const Matrix &m1, const Matrix &m2) +{ + Matrix r(m1); + return r += m2; +} + +template +inline Matrix &Matrix::operator-=(const Matrix &m) +{ + for(unsigned i=0; i +inline Matrix operator-(const Matrix &m1, const Matrix &m2) +{ + Matrix r(m1); + return r -= m2; +} + +template +Matrix operator*(const Matrix &m1, const Matrix &m2) +{ + Matrix r; + for(unsigned i=0; i +Vector operator*(const Matrix &m, const Vector &v) +{ + Vector r; + for(unsigned i=0; i +Vector operator*(const Vector &v, const Matrix &m) +{ + Vector r; + for(unsigned j=0; j +class Matrix4: public SquareMatrix +{ +}; + +} // namespace LinAl +} // namespace Msp + +#endif diff --git a/source/squarematrix.h b/source/squarematrix.h new file mode 100644 index 0000000..6cda86a --- /dev/null +++ b/source/squarematrix.h @@ -0,0 +1,27 @@ +#ifndef MSP_LINAL_SQUAREMATRIX_H_ +#define MSP_LINAL_SQUAREMATRIX_H_ + +#include "matrix.h" + +namespace Msp { +namespace LinAl { + +template +class SquareMatrix: public Matrix +{ +public: + SquareMatrix(); + SquareMatrix(const T *); + template + SquareMatrix(const Matrix &); + static SquareMatrix identity(); + + SquareMatrix &operator*=(const SquareMatrix &); + + void invert(); +}; + +} // namespace LinAl +} // namespace Msp + +#endif diff --git a/source/vector.h b/source/vector.h new file mode 100644 index 0000000..933611e --- /dev/null +++ b/source/vector.h @@ -0,0 +1,175 @@ +#ifndef MSP_LINAL_VECTOR_H_ +#define MSP_LINAL_VECTOR_H_ + +#include +#include + +namespace Msp { +namespace LinAl { + +/** +A general mathematical vector. +*/ +template +class Vector +{ +protected: + T data[N]; + +public: + Vector(); + Vector(const T *d); + template + Vector(const Vector &v); + + T &operator[](unsigned i); + const T &operator[](unsigned i) const; + + Vector &operator*=(T); + Vector &operator/=(T); + Vector &operator+=(const Vector &); + Vector &operator-=(const Vector &); + + T norm() const; + Vector &normalize(); +}; + + +template +inline Vector::Vector() +{ + std::fill(data, data+N, T()); +} + +template +inline Vector::Vector(const T *d) +{ + std::copy(d, d+N, data); +} + +template +template +inline Vector::Vector(const Vector &v) +{ + std::copy(v.data, v.data+N, data); +} + +template +T &Vector::operator[](unsigned i) +{ + return data[i]; +} + +template +const T &Vector::operator[](unsigned i) const +{ + return data[i]; +} + +template +inline Vector &Vector::operator*=(T s) +{ + for(unsigned i=0; i +inline Vector operator*(const Vector &v, T s) +{ + Vector r(v); + return r *= s; +} + +template +inline Vector operator*(T s, const Vector &v) +{ + return v*s; +} + +template +inline Vector &Vector::operator/=(T s) +{ + for(unsigned i=0; i +inline Vector operator/(const Vector &v, T s) +{ + Vector r(v); + return r /= s; +} + +template +inline Vector &Vector::operator+=(const Vector &v) +{ + for(unsigned i=0; i +inline Vector operator+(const Vector &v1, const Vector &v2) +{ + Vector r(v1); + return r += v2; +} + +template +inline Vector &Vector::operator-=(const Vector &v) +{ + for(unsigned i=0; i +inline Vector operator-(const Vector &v1, const Vector &v2) +{ + Vector r(v1); + return r -= v2; +} + +template +inline Vector operator-(const Vector &v) +{ + Vector r(v); + for(unsigned i=0; i +inline T inner_product(const Vector &v1, const Vector &v2) +{ + T r = T(); + for(unsigned i=0; i +inline T Vector::norm() const +{ + return sqrt(inner_product(*this, *this)); +} + +template +inline Vector &Vector::normalize() +{ + return *this /= norm(); +} + +template +inline Vector normalize(const Vector &v) +{ + Vector r(v); + return r.normalize(); +} + +} // namespace LinAl +} // namespace Msp + +#endif diff --git a/source/vector3.h b/source/vector3.h new file mode 100644 index 0000000..90a5ff5 --- /dev/null +++ b/source/vector3.h @@ -0,0 +1,57 @@ +#ifndef MSP_LINAL_VECTOR3_H_ +#define MSP_LINAL_VECTOR3_H_ + +#include "vector.h" + +namespace Msp { +namespace LinAl { + +/** +A three-dimensional vector, applicable for Euclidean space. +*/ +template +class Vector3: public Vector +{ +public: + Vector3() { } + Vector3(const T *); + Vector3(T, T, T); + template + Vector3(const Vector &); +}; + +template +inline Vector3::Vector3(const T *d): + Vector(d) +{ } + +template +inline Vector3::Vector3(T x, T y, T z) +{ + this->data[0] = x; + this->data[1] = y; + this->data[2] = z; +} + +template +template +inline Vector3::Vector3(const Vector &v): + Vector(v) +{ } + +template +inline T dot(const Vector3 &v1, const Vector3 &v2) +{ + return inner_product(v1, v2); +} + +template +inline Vector3 cross(const Vector3 &v1, const Vector3 &v2) +{ + return Vector3(v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0]); +} + +} // namespace LinAl +} // namespace LinAl + +#endif