12 #include <gsl/gsl_blas.h>
13 #include <gsl/gsl_statistics_double.h>
14 #include <gsl/gsl_sort_vector.h>
15 #include <gsl/gsl_vector_complex_double.h>
16 #include <gsl/gsl_complex_math.h>
27 void QVectorC::ArrayCopy(
const gsl_complex* orig,
const UInt_t origStride,
28 gsl_complex *dest,
const UInt_t destStride,
32 if(size > 0 && orig && dest) {
33 if(origStride == 1 && destStride == 1)
34 memcpy(dest,orig,
sizeof(gsl_complex)*size);
36 for(UInt_t i =0; i < size; i++) {
37 dest[i*destStride] = orig[i*origStride];
78 for (UInt_t i=0; i<
Size(); i++) {
95 for (UInt_t i=0; i<
Size(); i++)
112 InitCopy(orig,dataOwner);
115 fData=(
double *)data;
119 fAllocSize = (UInt_t)(2*size*stride);
125 void QVectorC::InitCopy(
const QVectorC& orig,
const bool dataOwner)
127 fDataOwner = dataOwner;
133 if(orig.fSize!=0 && orig.fData ) {
138 ArrayCopy((gsl_complex *)orig.fData,orig.fStride,
139 (gsl_complex *)fData,1,fSize/2);
143 fStride=orig.fStride;
146 fAllocSize = orig.fAllocSize;
182 if(newSize*2 > fAllocSize) {
184 fAllocSize=newSize*2;
189 else if(newSize !=
Size() ) {
192 if(fMathVec) free(fMathVec);
211 for(UInt_t i=0;i<size;i++){
212 fData[i*2*fStride]=re[i];
213 fData[i*2*fStride+1]=im[i];
218 for(UInt_t i=0;i<
Size();i++) {
219 fData[2*i*fStride]=re;
220 fData[2*i*fStride+1]=im;
228 int newSize =
Size()/rebin;
229 double norm = (sum ? 1. : 1./rebin);
230 for(
int i=0;i<newSize;i++){
231 (*this)[i]=norm*(*this)[rebin*i];
232 for(
int j=1;j<rebin;j++){
233 (*this)[i]+=norm*(*this)[rebin*i+j];
242 bool gtz = nstep > 0 ? true :
false;
243 if(nstep == 0 ||
Size() == 0)
return *
this;
244 unsigned int unstep =
abs(nstep) %
Size();
247 for(
unsigned int i = 0; i < unstep; i++) rest[i] = (*
this)[
Size() - unstep + i];
248 for(
int i =
Size() - unstep - 1; i >= 0 ; i--) (*
this)[i + unstep] = (*this)[i];
249 for(
unsigned int i = 0; i < unstep; i++) (*
this)[i] = rest[i];
251 for(
unsigned int i = 0; i < unstep; i++) rest[i] = (*
this)[i];
252 for(
unsigned int i = unstep; i <
Size(); i++) (*
this)[i - unstep] = (*this)[i];
253 for(
unsigned int i = 0; i < unstep; i++) (*
this)[i +
Size() - unstep] = rest[i];
260 int nstep = (int) ceil(fstep);
262 double fracs = nstep - fstep;
263 Diana::QComplex zero = (*this)[0];
264 for(
int i = 0; i < (int)
Size() - 1; i++) {
265 (*this)[i] = (*this)[i]*(1.-fracs) + fracs*(*
this)[i+1];
267 (*this)[
Size()-1] = (*this)[
Size()-1]*(1.-fracs) + fracs*zero;
275 return (fData+i*2*fStride);
277 std::stringstream stream;
278 stream<<
"Size: "<<
Size()<<
" Index: "<<i;
286 return (fData+i*2*fStride);
288 std::stringstream stream;
289 stream<<
"Size: "<<
Size()<<
" Index: "<<i;
297 ArrayCopy((gsl_complex *)orig.fData,orig.fStride,
298 (gsl_complex *)fData,fStride,fSize/2);
331 for(UInt_t i = 0; i <
Size(); i++) {
340 for(UInt_t i=0;i<
Size();i++){
341 res[i]=sqrt( fData[i*2*fStride]*fData[i*2*fStride] + fData[i*2*fStride+1]*fData[i*2*fStride+1] );
348 for(UInt_t i=0;i<
Size();i++) {
349 res[i]=fData[i*2*fStride]*fData[i*2*fStride] + fData[i*2*fStride+1]*fData[i*2*fStride+1];
368 gsl_vector_complex_mul(tmp.fMathVec,
b.
fMathVec);
379 gsl_vector_complex_mul(fMathVec,
b.
fMathVec);
392 for (UInt_t i=0; i<
Size(); i++){
409 gsl_vector_complex_div(tmp.fMathVec,
b.
fMathVec);
421 gsl_vector_complex_div(fMathVec,
b.
fMathVec);
432 for (UInt_t i=0;i<
Size(); i++) {
452 gsl_complex scale = gsl_complex_rect(t,0);
453 gsl_vector_complex_scale(fMathVec, scale);
466 gsl_vector_complex_add(fMathVec,
b.
fMathVec);
482 other.SetMathVector();
484 gsl_blas_zdotu(fMathVec,other.fMathVec,&res);
529 gsl_vector_complex_add_constant(fMathVec, sub);
540 gsl_vector_complex_scale(fMathVec, inv);
568 for(UInt_t i=0;i<
Size();i++)
569 fData[i*2*fStride+1]*=-1;
583 ArrayCopy((gsl_complex *)fData,fStride,(gsl_complex *)temp,1,
Size());
588 temp[fSize-2]=val.
Re();
589 temp[fSize-1]=val.
Im();
592 if(fMathVec) free(fMathVec);
597 "Not data owner, you are trying to resize a "
598 "QVectorC using Append"));
609 for(UInt_t i=0;i<
vec.
Size();i++) {
618 for (UInt_t i=0;i<
Size(); i++) {
619 fData[2*i*fStride]=re[i];
623 std::stringstream msg;
624 msg<<
"You are trying to use QVectorC::SetRe with a wrong size QVector: ("
625 <<re.
Size()<<
") instead of ("<<
Size()<<
")"<<std::endl;
631 for (UInt_t i=0; i <
Size(); i++) {
632 fData[2*i*fStride+1]=im[i];
636 std::stringstream msg;
637 msg<<
"You are trying to use QVectorC::SetIm with a wrong size QVector: ("
638 <<im.
Size()<<
") instead of ("<<
Size()<<
")"<<std::endl;
641 double QVectorC::Norma()
const
644 return gsl_blas_dznrm2(fMathVec);
646 QVector QVectorC::SingleVector()
const
649 for (UInt_t i=0;i<
Size();i++) {
650 res[i]=fData[2*i*fStride];
651 res[i+
Size()]=fData[2*i*fStride+1];
655 QVector &QVectorC::InterleavedVector()
const
659 "Can not interleave QVectorC if Stride !=1!"));
660 if(fInterleaved==NULL)
661 fInterleaved=
new QVector(fData,fSize,1);
662 return *fInterleaved;
664 QVectorC QVectorC::GetSubVector(UInt_t
N,UInt_t start,UInt_t stride){
667 "QVectorC: Sub-vector would overrun parent vector!"));
668 stride = (stride < 1 ? 1 : stride);
669 QVectorC ret((gsl_complex *)fData+start,
N,fStride*stride);
672 const QVectorC QVectorC::GetSubVector(UInt_t
N,UInt_t start,UInt_t stride)
676 "QVectorC: Sub-vector would overrun parent vector!"));
677 stride = (stride < 1 ? 1 : stride);
678 QVectorC ret((gsl_complex *)fData+start,
N,fStride*stride);
682 QComplex QVectorC::Sum(
const UInt_t nelem,
const UInt_t first)
const
685 UInt_t last = first + nelem;
686 for(UInt_t i = first; i < last; i++) sum += this->
operator[](i);
691 void QVectorC::SetMathVector()
const
694 fMathVec = (gsl_vector_complex*) malloc(
sizeof(gsl_vector_complex));
695 fMathVec->size =
Size();
696 fMathVec->stride = fStride;
697 fMathVec->data = fData;
702 void QVectorC::SetParts()
const
704 if(!fRe) fRe =
new QVector(fData,
Size(),fStride*2);
705 if(!fIm) fIm =
new QVector(fData+1,
Size(),fStride*2);
709 std::cout <<
"fSize " <<
Size() << std::endl;
710 std::cout <<
"fStride " << fStride << std::endl;
711 std::cout <<
"fData " << fData << std::endl;
712 std::cout <<
"fData ";
713 for(UInt_t i=0;i<
Size();i++)
714 std::cout << (*
this)[i].Re() <<
" " << (*this)[i].Im() <<
" ";
715 std::cout << std::endl;
726 for (UInt_t i = 0; i <(UInt_t)
vec.
Size(); i++) s<<
vec[i]<<
" ";
731 Diana::QVectorC
operator*(
double t,
const Diana::QVectorC &v)
733 Diana::QVectorC tmp(v);
737 const Diana::QVector
Re(
const Diana::QVectorC &Z) {
return Z.Re(); }
739 const Diana::QVector
Im(
const Diana::QVectorC &Z) {
return Z.Im(); }
#define Q_BEGIN_NAMESPACE
const Diana::QVector Im(const Diana::QVectorC &Z)
Get the imag part of the vector.
Diana::QVectorC operator*(double t, const Diana::QVectorC &v)
QObjectImp(Diana::QVectorC)
const Diana::QVector Re(const Diana::QVectorC &Z)
Get the real part of the vector.
Q_END_NAMESPACE std::ostream & operator<<(std::ostream &s, const Diana::QVectorC &vec)
Diana::QVector abs(const Diana::QVector &v)
gsl_complex Get_gsl_complex() const
error class with error type and description
Interface for complex matrices in Diana analysis.
void SetRow(const UInt_t nrow, const QVectorC &vec)
set row to specific vectors
void SetCol(const UInt_t ncol, const QVectorC &vec)
set column to specific vector
virtual void Print() const
print content on screen
Interface for complex vectors in Diana analysis.
void SetArray(const double *re, const double *im, UInt_t size)
const QVectorC & DivIn(const QVectorC &other)
division element by element by the given vector
QVector GetMagnitudesSquare() const
QVectorC operator+(const QVectorC &other) const
sum vector
const QVectorC & ShiftReal(const double fstep)
Cyclic shift of vector by a real number, first shift by the integer part of fstep then linear interpo...
const QVectorC & operator+=(const QVectorC &v)
QVectorC operator-() const
revert sign to all components
QMatrixC H() const
transpose conjugate
const QVectorC & operator*=(const double t)
multiplication by scalar
QVectorC()
default constructor
QVectorC operator/(const double t) const
divide by scalar
void Resize(const UInt_t newsize)
const QVectorC & Shift(const int nstep)
Cyclic shift of vector.
void Initialize(const double re=0, const double im=0)
initialize elements (default to 0)
QComplex operator*(const QVectorC &other) const
complex conjugate scalar product (v^H * u)
void SetRe(const QVector &re)
Set real part.
const QVectorC & operator=(const QVectorC &orig)
copy the content of another vector
QVectorC Mult(const QVectorC &other) const
multiplication element by element
UInt_t Size() const
size of QVector
QVectorC Div(const QVectorC &other) const
multiplication element by element
~QVectorC()
copy only some of the elements
void SetIm(const QVector &im)
Set imag part.
void Append(const QComplex &val)
append
const QVectorC & MultIn(const QVectorC &other)
multiplication element by element by the given vector
const QVectorC & operator/=(const double v)
divide by scalar
QComplex operator[](const UInt_t i)
retrieve an element
QVector GetMagnitudes() const
const QVectorC & operator-=(const QComplex &t)
add a QVectorC
const QVectorC & Rebin(const int rebin, bool sum=false)
Rebin the vector.
Interface for vectors in Diana analysis.
UInt_t Size() const
size of QVector
UInt_t fSize
size of the array
static void ArrayFree(double *array)
void SetMathVector() const
static double * ArrayAlloc(const UInt_t size)
gsl_vector * fMathVec
temporary gsl_vector to use the gsl library