Newer
Older
Gigg, Martyn Anthony
committed
#include "MantidKernel/Matrix.h"
#include "MantidKernel/V3D.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/MersenneTwister.h"
Gigg, Martyn Anthony
committed
#include <iomanip>
Gigg, Martyn Anthony
committed
using Mantid::Kernel::TimeSeriesProperty;
Gigg, Martyn Anthony
committed
namespace Kernel
Gigg, Martyn Anthony
committed
//
#define fabs(x) std::fabs((x)*1.0)
Gigg, Martyn Anthony
committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace
{
//-------------------------------------------------------------------------
// Utility methods and function objects in anonymous namespace
//-------------------------------------------------------------------------
/**
\class PIndex
\author S. Ansell
\date Aug 2005
\version 1.0
\brief Class to fill an index with a progressive count
*/
template<typename T>
struct PIndex
{
private:
int count; ///< counter
public:
/// Constructor
PIndex() : count(0) { }
/// functional
std::pair<T,int> operator()(const T& A) { return std::pair<T,int>(A,count++); }
};
/**
\class PSep
\author S. Ansell
\date Aug 2005
\version 1.0
\brief Class to access the second object in index pair.
*/
template<typename T>
struct PSep
{
/// Functional to the second object
int operator()(const std::pair<T,int>& A) { return A.second; }
};
/**
* Function to take a vector and sort the vector
* so as to produce an index. Leaves the vector unchanged.
* @param pVec :: Input vector
* @param Index :: Output vector
*/
template<typename T>
void
indexSort(const std::vector<T>& pVec,std::vector<int>& Index)
{
Index.resize(pVec.size());
std::vector<typename std::pair<T,int> > PartList;
Gigg, Martyn Anthony
committed
PartList.resize(pVec.size());
transform(pVec.begin(),pVec.end(),PartList.begin(),PIndex<T>());
sort(PartList.begin(),PartList.end());
transform(PartList.begin(),PartList.end(),Index.begin(),PSep<T>());
return;
}
template void indexSort(const std::vector<double>&,std::vector<int>&);
template void indexSort(const std::vector<float>&,std::vector<int>&);
template void indexSort(const std::vector<int>&,std::vector<int>&);
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
Alex Buts
committed
template<typename T>
std::vector<T>
Alex Buts
committed
{
Gigg, Martyn Anthony
committed
std::vector<T> rez(nx*ny);
size_t ic(0);
for(size_t i=0;i<nx;i++)
{
for(size_t j=0;j<ny;j++)
{
rez[ic]=V[i][j];
ic++;
}
}
return rez;
Alex Buts
committed
}
//
Matrix<T>::Matrix(const size_t nrow, const size_t ncol, const bool makeIdentity)
Janik Zikovsky
committed
/**
Janik Zikovsky
committed
@param nrow :: number of rows
@param ncol :: number of columns
Savici, Andrei T.
committed
@param makeIdentity :: flag for the constructor to return an identity matrix
*/
{
// Note:: nx,ny zeroed so setMem always works
setMem(nrow,ncol);
zeroMatrix();
Savici, Andrei T.
committed
if(makeIdentity==true) identityMatrix();
}
template<typename T>
Matrix<T>::Matrix(const std::vector<T>& A,const std::vector<T>& B)
: nx(0),ny(0),V(0)
Janik Zikovsky
committed
/**
Constructor to take two vectors and multiply them to
construct a matrix. (assuming that we have columns x row
vector.
Janik Zikovsky
committed
@param A :: Column vector to multiply
@param B :: Row vector to multiply
*/
{
// Note:: nx,ny zeroed so setMem always works
setMem(A.size(),B.size());
Gigg, Martyn Anthony
committed
for(size_t i=0;i<nx;i++)
{
for(size_t j=0;j<ny;j++)
{
Gigg, Martyn Anthony
committed
}
}
//
template<typename T>
Michael Reuter
committed
Matrix<T>::Matrix(const std::vector<T>&data) : nx(0),ny(0),V(0)
Gigg, Martyn Anthony
committed
size_t numel = data.size();
size_t nxt =(size_t)sqrt(double(numel));
size_t test = nxt*nxt;
if(test != numel){
throw(std::invalid_argument("number of elements in input vector have to be square of some value"));
}
setMem(nxt,nxt);
size_t ic(0);
for(size_t i=0;i<nx;i++)
{
for(size_t j=0;j<ny;j++)
{
V[i][j]=data[ic];
ic++;
}
}
Gigg, Martyn Anthony
committed
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& A,const size_t nrow,const size_t ncol)
: nx(A.nx-1),ny(A.ny-1),V(0)
/**
Constructor with for a missing row/column.
@param A :: The input matrix
@param nrow :: number of row to miss
@param ncol :: number of column to miss
*/
{
// Note:: nx,ny zeroed so setMem always works
Gigg, Martyn Anthony
committed
if (nrow>nx)
Gigg, Martyn Anthony
committed
throw Kernel::Exception::IndexError(nrow,A.nx, "Matrix::Constructor without col");
Gigg, Martyn Anthony
committed
if (ncol>ny)
Gigg, Martyn Anthony
committed
throw Kernel::Exception::IndexError(ncol,A.ny, "Matrix::Constructor without col");
setMem(nx,ny);
size_t iR(0);
for(size_t i=0;i<=nx;i++)
{
if (i!=nrow)
{
size_t jR(0);
for(size_t j=0;j<=ny;j++)
{
if (j!=ncol)
{
V[iR][jR]=A.V[i][j];
jR++;
}
}
Gigg, Martyn Anthony
committed
iR++;
Gigg, Martyn Anthony
committed
}
}
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& A)
: nx(0),ny(0),V(0)
Loading
Loading full blame...