Blame view

stim/math/matrix.h 1.9 KB
57729e5b   David Mayerich   changed directory...
1
2
3
4
5
6
  #ifndef RTS_MATRIX_H

  #define RTS_MATRIX_H

  

  //#include "rts/vector.h"

  #include <string.h>

  #include <iostream>

cce7daf9   Pavel Govyadinov   added glObj.h to ...
7
  #include <stim/math/vector.h>

308a743c   David Mayerich   fixed class compa...
8
  #include <stim/math/vec3.h>

7d1d153a   Pavel Govyadinov   fixed the include...
9
  #include <stim/cuda/cudatools/callable.h>

57729e5b   David Mayerich   changed directory...
10
  

8a86bd56   David Mayerich   changed rts names...
11
  namespace stim{

57729e5b   David Mayerich   changed directory...
12
13
14
15
16
17
18
  

  template <class T, int N>

  struct matrix

  {

  	//the matrix will be stored in column-major order (compatible with OpenGL)

  	T M[N*N];

  

d609550e   David Mayerich   fixed bug in plan...
19
  	CUDA_CALLABLE matrix()

57729e5b   David Mayerich   changed directory...
20
21
22
23
24
25
26
27
28
  	{

  		for(int r=0; r<N; r++)

  			for(int c=0; c<N; c++)

  				if(r == c)

  					(*this)(r, c) = 1;

  				else

  					(*this)(r, c) = 0;

  	}

  

b2d10ff1   Pavel Govyadinov   final edits, expa...
29
  	CUDA_CALLABLE matrix(T rhs[N*N])

8e56a0a7   Pavel Govyadinov   Added the propose...
30
  	{

b2d10ff1   Pavel Govyadinov   final edits, expa...
31
32
33
34
35
36
  		memcpy(M,rhs, sizeof(T)*N*N);

  	}

  

  	CUDA_CALLABLE matrix<T,N> set(T rhs[N*N])

  	{

  		memcpy(M, rhs, sizeof(T)*N*N);

8e56a0a7   Pavel Govyadinov   Added the propose...
37
38
  		return *this;

  	}

b2d10ff1   Pavel Govyadinov   final edits, expa...
39
  

d609550e   David Mayerich   fixed bug in plan...
40
  	CUDA_CALLABLE T& operator()(int row, int col)

57729e5b   David Mayerich   changed directory...
41
42
43
44
  	{

  		return M[col * N + row];

  	}

  

d609550e   David Mayerich   fixed bug in plan...
45
  	CUDA_CALLABLE matrix<T, N> operator=(T rhs)

57729e5b   David Mayerich   changed directory...
46
47
48
49
50
51
52
53
  	{

  		int Nsq = N*N;

  		for(int i=0; i<Nsq; i++)

  			M[i] = rhs;

  

  		return *this;

  	}

  

ecfd14df   David Mayerich   implemented D fie...
54
  	template<typename Y>

8e4f8364   David Mayerich   started a new opt...
55
  	vec<Y> operator*(vec<Y> rhs){

487a9b49   David Mayerich   added the ability...
56
57
  		unsigned int N = rhs.size();

  

9339fbad   David Mayerich   implementing mie ...
58
  		vec<Y> result;

487a9b49   David Mayerich   added the ability...
59
  		result.resize(N);

57729e5b   David Mayerich   changed directory...
60
61
62
63
64
65
66
67
  

  		for(int r=0; r<N; r++)

  			for(int c=0; c<N; c++)

  				result[r] += (*this)(r, c) * rhs[c];

  

  		return result;

  	}

  

8e4f8364   David Mayerich   started a new opt...
68
69
70
71
72
73
74
75
76
77
  	template<typename Y>

  	CUDA_CALLABLE vec3<Y> operator*(vec3<Y> rhs){

  		vec3<Y> result = 0;

  		for(int r=0; r<3; r++)

  			for(int c=0; c<3; c++)

  				result[r] += (*this)(r, c) * rhs[c];

  

  		return result;

  	}

  

b2d10ff1   Pavel Govyadinov   final edits, expa...
78
  	std::string toStr()

57729e5b   David Mayerich   changed directory...
79
80
81
82
83
  	{

  		std::stringstream ss;

  

  		for(int r = 0; r < N; r++)

  		{

b2d10ff1   Pavel Govyadinov   final edits, expa...
84
  			ss << "| ";

57729e5b   David Mayerich   changed directory...
85
86
  			for(int c=0; c<N; c++)

  			{

b2d10ff1   Pavel Govyadinov   final edits, expa...
87
  				ss << (*this)(r, c) << " ";

57729e5b   David Mayerich   changed directory...
88
  			}

b2d10ff1   Pavel Govyadinov   final edits, expa...
89
  			ss << "|" << std::endl;

57729e5b   David Mayerich   changed directory...
90
91
92
93
  		}

  

  		return ss.str();

  	}

57729e5b   David Mayerich   changed directory...
94
95
96
97
98
  };

  

  }	//end namespace rts

  

  template <typename T, int N>

ef70330e   David Mayerich   changed rts to stim
99
  std::ostream& operator<<(std::ostream& os, stim::matrix<T, N> M)

57729e5b   David Mayerich   changed directory...
100
101
102
103
104
105
106
107
108
109
  {

      os<<M.toStr();

      return os;

  }

  

  //#if __GNUC__ > 3 && __GNUC_MINOR__ > 7

  //template<class T, int N> using rtsMatrix = rts::matrix<T, N>;

  //#endif

  

  #endif