From: Mikko Rasa Date: Sat, 27 Apr 2013 16:51:08 +0000 (+0300) Subject: Rename the library to mspmath and make linal a sublibrary X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=839a88cd2b9a2f054323627c4e76cdcefc652c63;p=libs%2Fmath.git Rename the library to mspmath and make linal a sublibrary --- diff --git a/.gitignore b/.gitignore index e00c88d..f0ea9e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /.config -/libmsplinal.so -/msplinal.pc +/libmspmath.so +/mspmath.pc /temp diff --git a/Build b/Build index 51f75b9..517bb2d 100644 --- a/Build +++ b/Build @@ -1,12 +1,12 @@ -package "msplinal" +package "mspmath" { - library "msplinal" + library "mspmath" { - source "source"; + source "source/linal"; install true; install_map { - map "source" "include/msp/linal"; + map "source" "include/msp"; }; }; }; diff --git a/source/dummy.cpp b/source/dummy.cpp deleted file mode 100644 index dd69a99..0000000 --- a/source/dummy.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#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/linal/dummy.cpp b/source/linal/dummy.cpp new file mode 100644 index 0000000..dd69a99 --- /dev/null +++ b/source/linal/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/linal/matrix.h b/source/linal/matrix.h new file mode 100644 index 0000000..00e960d --- /dev/null +++ b/source/linal/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/linal/squarematrix.h b/source/linal/squarematrix.h new file mode 100644 index 0000000..6cda86a --- /dev/null +++ b/source/linal/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/linal/vector.h b/source/linal/vector.h new file mode 100644 index 0000000..933611e --- /dev/null +++ b/source/linal/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/linal/vector3.h b/source/linal/vector3.h new file mode 100644 index 0000000..90a5ff5 --- /dev/null +++ b/source/linal/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 diff --git a/source/matrix.h b/source/matrix.h deleted file mode 100644 index 00e960d..0000000 --- a/source/matrix.h +++ /dev/null @@ -1,149 +0,0 @@ -#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 deleted file mode 100644 index 6cda86a..0000000 --- a/source/squarematrix.h +++ /dev/null @@ -1,27 +0,0 @@ -#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 deleted file mode 100644 index 933611e..0000000 --- a/source/vector.h +++ /dev/null @@ -1,175 +0,0 @@ -#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 deleted file mode 100644 index 90a5ff5..0000000 --- a/source/vector3.h +++ /dev/null @@ -1,57 +0,0 @@ -#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