Skip to content
Snippets Groups Projects
SMatrix.icc 17.7 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
// @(#)root/smatrix:$Name:  $:$Id: SMatrix.iccv 1.0 2005/11/24 12:00:00 moneta Exp $
// Authors: T. Glebe, L. Moneta    2005  

#ifndef ROOT_Math_SMatrix_icc
#define ROOT_Math_SMatrix_icc
// ********************************************************************
//
// source:
//
// type:      source code
//
// created:   21. Mar 2001
//
// author:    Thorsten Glebe
//            HERA-B Collaboration
//            Max-Planck-Institut fuer Kernphysik
//            Saupfercheckweg 1
//            69117 Heidelberg
//            Germany
//            E-mail: T.Glebe@mpi-hd.mpg.de
//
// Description: SMatrix implementation file
//
// changes:
// 21 Mar 2001 (TG) creation
// 26 Mar 2001 (TG) place_in_row(), place_in_col() added
// 03 Apr 2001 (TG) invert() added
// 07 Apr 2001 (TG) CTOR from SVertex (dyadic product) added
// 09 Apr 2001 (TG) CTOR from array added
// 25 Mai 2001 (TG) row(), col() added
// 11 Jul 2001 (TG) added #include Functions.hh
// 11 Jan 2002 (TG) added operator==(), operator!=()
// 14 Jan 2002 (TG) added more operator==(), operator!=(), operator>(), operator<()
//
// ********************************************************************
#include <iostream>
#include <iomanip>
#include <assert.h>
#include "Math/Dsinv.h"
#include "Math/Dsfact.h"
#include "Math/Dfact.h"
#include "Math/Dinv.h"
#include "Math/Functions.h"


namespace ROOT { 

  namespace Math { 



//==============================================================================
// Constructors
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>::SMatrix() {
  // operator=(0);   // if operator=(T ) is defined
    for(unsigned int i=0; i<D1*D2; ++i) {
      fArray[i] = 0;
    }
}

template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>::SMatrix(const SMatrix<T,D1,D2>& rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] = rhs.fArray[i];
  }
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>::SMatrix(const Expr<A,T,D1,D2>& rhs) {
  operator=(rhs);
}
 
#ifdef OLD_IMPL
   
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>::SMatrix(const T& rhs, const bool diagonal) {
  if(diagonal) {
    // symmetric matrix!
    assert(D1==D2);
    // 50% faster than with nested loops
    for(unsigned int i=0; i<D1*D2; ++i)
      fArray[i] = 0;
    for(unsigned int i=0; i<D1; ++i)
      fArray[i*D1+i] = rhs;
  } else {
    for(unsigned int i=0; i<D1*D2; ++i) {
      fArray[i] = rhs;
    }
  }
}

template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>::SMatrix(const SVector<T,D1>& rhs) {
  for(unsigned int i=0; i<D1; ++i) {
    const unsigned int row = i*D1;
    for(unsigned int j=0; j<D1; ++j) {
      fArray[row+j] = rhs[i]*rhs[j];
    }
  }
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>::SMatrix(const Expr<A,T,D1>& rhs) {
  for(unsigned int i=0; i<D1; ++i) {
    const unsigned int row = i*D1;
    for(unsigned int j=0; j<D1; ++j) {
      fArray[row+j] = rhs.apply(i)*rhs.apply(j);
    }
  }
}

template <class T, unsigned int D1, unsigned int D2>
template <class T1>
SMatrix<T,D1,D2>::SMatrix(const T1* a, const bool triang, const unsigned int len) {
  // fill from array with lower triangular matrix?
  if(triang==false) {
    for(unsigned int i=0; i<len; ++i) {
      fArray[i] = a[i];
    }
    if(len<D1*D2)
      for(unsigned int i=len; i<D1*D2; ++i)
	fArray[i] = 0.;
  } else {
    // symmetric matrix required!
    assert(D1==D2);

    unsigned int k=0;
    if(len == (Square(D1)+D1)/2) {
      for(unsigned int i=0; i<D1; ++i) {
	const unsigned int row = i*D1;
	for(unsigned int j=0; j<=i && k<len; ++j) {
	  fArray[row+j] = fArray[j*D1+i] = a[k++];
	}
      }
    } else {
      unsigned int k=0;
      for(unsigned int i=0; i<D1; ++i) {
	const unsigned int row = i*D1;
	for(unsigned int j=0; j<=i; ++j) {
	  if(k<len)
	    fArray[row+j] = fArray[j*D1+i] = a[k++];
	  else
	    fArray[row+j] = fArray[j*D1+i] = 0.;
	} // for j
      } // for i
    } // if len == 
  } // if triang
}


//==============================================================================
// operator=
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator=(const T&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] = rhs;
  }
  return *this;
}

#endif


template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator=(const Expr<A,T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] = rhs.apply(i);
  }
  return *this;
}


//==============================================================================
// operator+=
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator+=(const SMatrix<T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] += rhs.apply(i);
  }
  return *this;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator+=(const Expr<A,T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] += rhs.apply(i);
  }
  return *this;
}


//==============================================================================
// operator-=
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator-=(const SMatrix<T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] -= rhs.apply(i);
  }
  return *this;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator-=(const Expr<A,T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] -= rhs.apply(i);
  }
  return *this;
}


//==============================================================================
// operator*=
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator*=(const SMatrix<T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] *= rhs.apply(i);
  }
  return *this;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator*=(const Expr<A,T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] *= rhs.apply(i);
  }
  return *this;
}


//==============================================================================
// operator/=
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator/=(const SMatrix<T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] /= rhs.apply(i);
  }
  return *this;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::operator/=(const Expr<A,T,D1,D2>&  rhs) {
  for(unsigned int i=0; i<D1*D2; ++i) {
    fArray[i] /= rhs.apply(i);
  }
  return *this;
}


//==============================================================================
// operator== (element wise comparison)
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator==(const T& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] == rhs);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator==(const SMatrix<T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] == rhs.fArray[i]);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
bool SMatrix<T,D1,D2>::operator==(const Expr<A,T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] == rhs.apply(i));
  }
  return rc;
}

//==============================================================================
// operator!= (element wise comparison)
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::operator!=(const T& rhs) const {
  return !operator==(rhs);
}

template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::operator!=(const SMatrix<T,D1,D2>& rhs) const {
  return !operator==(rhs);
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
inline bool SMatrix<T,D1,D2>::operator!=(const Expr<A,T,D1,D2>& rhs) const {
  return !operator==(rhs);
}


//==============================================================================
// operator> (element wise comparison)
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator>(const T& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] > rhs);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator>(const SMatrix<T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] > rhs.fArray[i]);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
bool SMatrix<T,D1,D2>::operator>(const Expr<A,T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] > rhs.apply(i));
  }
  return rc;
}

//==============================================================================
// operator< (element wise comparison)
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator<(const T& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] < rhs);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
bool SMatrix<T,D1,D2>::operator<(const SMatrix<T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] < rhs.fArray[i]);
  }
  return rc;
}

template <class T, unsigned int D1, unsigned int D2>
template <class A>
bool SMatrix<T,D1,D2>::operator<(const Expr<A,T,D1,D2>& rhs) const {
  bool rc = true;
  for(unsigned int i=0; i<D1*D2; ++i) {
    rc = rc && (fArray[i] < rhs.apply(i));
  }
  return rc;
}

//==============================================================================
// sinvert
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::Sinvert() {
  return Dsinv<T,D1,D1>(fArray);
}

//==============================================================================
// sdet
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::Sdet(T& det) {
  return Dsfact<SMatrix<T,D1,D1>, D1, D1>(*this,det);
}

//==============================================================================
// invert
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::Invert() {
  return Inverter<D2>::Dinv(*this);
}

//==============================================================================
// det
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline bool SMatrix<T,D1,D2>::Det(T& det) {
  return Dfact<SMatrix<T,D1,D1>, D1, D1>(*this,det);
}

//==============================================================================
// place_in_row
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <unsigned int D>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_in_row(const SVector<T,D>& rhs,
						 const unsigned int row,
						 const unsigned int col) {

  assert(col+D <= D2);

  for(unsigned int i=row*D2+col, j=0; j<D; ++i, ++j) {
    fArray[i] = rhs.apply(j);
  }
  return *this;
}

//==============================================================================
// place_in_row
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <class A, unsigned int D>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_in_row(const Expr<A,T,D>& rhs,
						 const unsigned int row,
						 const unsigned int col) {

  assert(col+D <= D2);

  for(unsigned int i=row*D2+col, j=0; j<D; ++i, ++j) {
    fArray[i] = rhs.apply(j);
  }
  return *this;
}

//==============================================================================
// place_in_col
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <unsigned int D>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_in_col(const SVector<T,D>& rhs,
						 const unsigned int row,
						 const unsigned int col) {

  assert(row+D <= D1);

  for(unsigned int i=row*D2+col, j=0; j<D; i+=D2, ++j) {
    fArray[i] = rhs.apply(j);
  }
  return *this;
}

//==============================================================================
// place_in_col
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <class A, unsigned int D>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_in_col(const Expr<A,T,D>& rhs,
						 const unsigned int row,
						 const unsigned int col) {

  assert(row+D <= D1);

  for(unsigned int i=row*D2+col, j=0; j<D; i+=D2, ++j) {
    fArray[i] = rhs.apply(j);
  }
  return *this;
}

//==============================================================================
// place_at
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <unsigned int D3, unsigned int D4>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_at(const SMatrix<T,D3,D4>& rhs,
					     const unsigned int row,
					     const unsigned int col) {
  assert(row+D3 <= D1 && col+D4 <= D2);

  const unsigned int offset = row*D2+col;

  for(unsigned int i=0; i<D3*D4; ++i) {
    fArray[offset+(i/D4)*D2+i%D4] = rhs.apply(i);
  }

  return *this;
}

//==============================================================================
// place_at
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
template <class A, unsigned int D3, unsigned int D4>
SMatrix<T,D1,D2>& SMatrix<T,D1,D2>::Place_at(const Expr<A,T,D3,D4>& rhs,
					     const unsigned int row,
					     const unsigned int col) {
  assert(row+D3 <= D1 && col+D4 <= D2);

  const unsigned int offset = row*D2+col;

  for(unsigned int i=0; i<D3*D4; ++i) {
    fArray[offset+(i/D4)*D2+i%D4] = rhs.apply(i);
  }

  return *this;
}

//==============================================================================
// row
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SVector<T,D2> SMatrix<T,D1,D2>::Row(const unsigned int therow) const {

  const unsigned int offset = therow*D2;

  static SVector<T,D2> tmp;
  for(unsigned int i=0; i<D2; ++i) {
    tmp[i] = fArray[offset+i];
  }
  return tmp;
}

//==============================================================================
// col
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
SVector<T,D1> SMatrix<T,D1,D2>::Col(const unsigned int thecol) const {

  static SVector<T,D1> tmp;
  for(unsigned int i=0; i<D1; ++i) {
    tmp[i] = fArray[thecol+i*D2];
  }
  return tmp;
}

//==============================================================================
// print
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
std::ostream& SMatrix<T,D1,D2>::Print(std::ostream& os) const {
  os.setf(std::ios::right,std::ios::adjustfield);
  //  os.setf(ios::fixed);

  os << "[ ";
  for (unsigned int i=0; i < D1; ++i) {
    for (unsigned int j=0; j < D2; ++j) {
      os << std::setw(12) << fArray[i*D2+j];
      if ((!((j+1)%12)) && (j < D2-1))
	os << std::endl << "         ...";
    }
    if (i != D1 - 1)
      os << std::endl  << "  ";
  }
  os << " ]";
  
  return os;
}

//==============================================================================
// Access functions
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline T SMatrix<T,D1,D2>::apply(unsigned int i) const { return fArray[i]; }

template <class T, unsigned int D1, unsigned int D2>
inline const T* SMatrix<T,D1,D2>::Array() const { return fArray; }

template <class T, unsigned int D1, unsigned int D2>
inline T* SMatrix<T,D1,D2>::Array() { return fArray; }

//==============================================================================
// Operators
//==============================================================================
template <class T, unsigned int D1, unsigned int D2>
inline const T& SMatrix<T,D1,D2>::operator()(unsigned int i, unsigned int j) const {
  return fArray[i*D2+j];
}

template <class T, unsigned int D1, unsigned int D2>
inline T& SMatrix<T,D1,D2>::operator()(unsigned int i, unsigned int j) {
  return fArray[i*D2+j];
}


  }  // namespace Math

}  // namespace ROOT
          


#endif