29 template <
class T>
class Vec;
30 template <
class T>
class Mat;
66 VecShell(T *p,
int size) : m_pBuf(p), m_nSize(size) {};
72 void Reset(T *p,
int size) { m_pBuf = p; m_nSize = size; }
83 for (
int i = 0; i <
m_nSize; i++)
103 void Reset(
int size = 0);
121 MatShell() : m_pBuf(NULL), m_nRow(0), m_nCol(0) {};
122 MatShell(T *pbuf,
int row,
int col) :m_pBuf(pbuf), m_nRow(row), m_nCol(col) {};
125 T&
Get(
unsigned int i,
unsigned int j) {
return m_pBuf[i*m_nCol + j]; }
130 void Reset(T *pbuf,
int row,
int col) { m_pBuf = pbuf; m_nRow = row; m_nCol = col; }
131 void Read(
File &file);
132 void Write(
File &file);
149 Mat(
int row,
int col) : m_nBufSize(0) {
Reset(row, col); }
151 void Reset(
int row = 0,
int col = 0);
168 Mat3dShell() :m_pBuf(NULL), m_nXDim(0), m_nYDim(0), m_nZDim(0) {};
169 Mat3dShell(T* p,
int xdim,
int ydim,
int zdim) :m_pBuf(p), m_nXDim(xdim), m_nYDim(ydim), m_nZDim(zdim) {};
172 T&
Get(
int x,
int y,
int z) {
return m_pBuf[x*m_nYDim*m_nZDim + y*m_nZDim + z]; }
173 int GetSize()
const {
return m_nXDim * m_nYDim * m_nZDim; }
178 void Reset(T* p,
int xdim,
int ydim,
int zdim) { m_pBuf = p; m_nXDim = xdim; m_nYDim = ydim; m_nZDim = zdim; }
180 void Write(
File &file);
181 void Read(
File &file);
196 Mat3d(
int xdim,
int ydim,
int zdim) : m_nBufSize(0) {
Reset(xdim, ydim, zdim); }
198 void Reset(
int xdim=0,
int ydim=0,
int zdim=0);
214 for (
int i = 0; i < v1.
GetSize(); i++) {
215 if (v1[i] != v2[i]) {
226 lout_error(
"[VecAdd] Vec Size are not equal: v1.size=" 229 for (
int i = 0; i < res.
GetSize(); i++) {
243 for (
int i = 0; i < v1.
GetSize(); i++) {
262 for (
int i = 0; i < m.
GetRow(); i++) {
265 for (
int j = 0; j < m.
GetCol(); j++) {
284 for (
int i = 0; i <
m_nSize; i++) {
315 for (
int i = 0; i <
GetSize(); i++) {
325 for (
int i = 0; i <
GetSize(); i++) {
332 for (
int i = 0; i <
GetSize(); i++) {
339 for (
int i = 0; i <
GetSize(); i++) {
348 for (
int i = 0; i <
m_nSize; i++) {
366 this->m_nBufSize = 0;
370 if (size <= this->m_nBufSize) {
381 this->m_nSize = size;
382 this->m_nBufSize = size;
402 for (
int i = 0; i < m_nRow*m_nCol; i++) {
411 for (
int i = 0; i < m_nRow*m_nCol; i++) {
420 ofstream os(file.
fp);
421 for (
int i = 0; i < m_nRow; i++) {
422 for (
int j = 0; j < m_nCol; j++) {
423 os << Get(i, j) <<
" ";
431 ifstream is(file.
fp);
432 for (
int i = 0; i < m_nRow; i++) {
433 for (
int j = 0; j < m_nCol; j++) {
447 if (row * col == 0) {
452 this->m_nBufSize = 0;
456 int size = row * col;
457 if (size <= this->m_nBufSize) {
466 memcpy(p, this->
m_pBuf,
sizeof(T)*this->m_nRow*this->m_nCol);
471 this->m_nBufSize = size;
478 memcpy(this->
m_pBuf, m.
GetBuf(),
sizeof(T)*this->m_nRow*this->m_nCol);
487 for (
int i = 0; i <
GetSize(); i++) {
494 ofstream os(file.
fp);
496 for (x = 0; x < m_nXDim; x++) {
497 for (y = 0; y < m_nYDim; y++) {
499 for (z = 0; z < m_nZDim-1; z++)
500 os<<Get(x, y, z)<<
" ";
501 os << Get(x, y, z) <<
"]";
509 ifstream is(file.
fp);
512 for (x = 0; x < m_nXDim; x++) {
513 for (y = 0; y < m_nYDim; y++) {
515 for (z = 0; z < m_nZDim - 1; z++)
517 is >> Get(x, y, z) >> c;
529 for (
int i = 0; i <
GetSize(); i++) {
538 if (xdim*ydim*zdim == 0) {
544 this->m_nBufSize = 0;
548 int size = xdim * ydim * zdim;
549 if (size <= this->m_nBufSize) {
550 this->m_nXDim = xdim;
551 this->m_nYDim = ydim;
552 this->m_nZDim = zdim;
562 this->m_nXDim = xdim;
563 this->m_nYDim = ydim;
564 this->m_nZDim = zdim;
565 this->m_nBufSize = size;
void Copy(Mat3dShell< T > &m)
T & Get(unsigned int i, unsigned int j)
MatShell(T *pbuf, int row, int col)
friend T VecDot(VecShell< T > &v1, VecShell< T > &v2)
calculate V*V
void Reset(int xdim=0, int ydim=0, int zdim=0)
T & Get(int x, int y, int z)
VecShell< T > GetSub(int nPos, int nLen)
bool operator==(MatShell &m)
virtual int Scanf(const char *p_pMessage,...)
scanf
void Reset(T *p, int size)
a definition of a class Log, which can output to the cmd window and the log file simultaneously. In wb-log.cpp, there are a Log variable "lout", which can be directly used just like "cout". For example:
friend T MatVec2(MatShell< T > &m, VecShell< T > &v1, VecShell< T > &v2)
calculate V1*M*V2
friend void VecAdd(VecShell< T > &res, VecShell< T > &v1, VecShell< T > &v2)
calculate V + V
bool operator==(VecShell v)
Mat3d(int xdim, int ydim, int zdim)
#define SAFE_DELETE_ARRAY(p)
void VecAdd(VecShell< T > &res, VecShell< T > &v1, VecShell< T > &v2)
calculate V + V
friend bool VecEqual(VecShell< T > &v1, VecShell< T > &v2)
define the friend function
bool operator==(Mat3dShell &m)
void Reset(int row=0, int col=0)
void operator+=(VecShell v)
Mat3dShell(T *p, int xdim, int ydim, int zdim)
T VecDot(VecShell< T > &v1, VecShell< T > &v2)
calculate V*V
T MatVec2(MatShell< T > &m, VecShell< T > &v1, VecShell< T > &v2)
calculate V1*M*V2
bool VecEqual(VecShell< T > &v1, VecShell< T > &v2)
calculate V==V
void Copy(MatShell< T > &m)
void operator-=(VecShell v)
void Copy(VecShell< T > v)
void Reset(T *p, int xdim, int ydim, int zdim)
define all the code written by Bin Wang.
void Reset(T *pbuf, int row, int col)
Defination of simple dynamic array/stack/queue and so on.
void operator=(VecShell v)