level1_cplx_impl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "common.h"
  10. struct scalar_norm1_op {
  11. typedef RealScalar result_type;
  12. EIGEN_EMPTY_STRUCT_CTOR(scalar_norm1_op)
  13. inline RealScalar operator() (const Scalar& a) const { return numext::norm1(a); }
  14. };
  15. namespace Eigen {
  16. namespace internal {
  17. template<> struct functor_traits<scalar_norm1_op >
  18. {
  19. enum { Cost = 3 * NumTraits<Scalar>::AddCost, PacketAccess = 0 };
  20. };
  21. }
  22. }
  23. // computes the sum of magnitudes of all vector elements or, for a complex vector x, the sum
  24. // res = |Rex1| + |Imx1| + |Rex2| + |Imx2| + ... + |Rexn| + |Imxn|, where x is a vector of order n
  25. RealScalar EIGEN_CAT(REAL_SCALAR_SUFFIX, EIGEN_BLAS_FUNC(asum))(int *n, RealScalar *px, int *incx)
  26. {
  27. // std::cerr << "__asum " << *n << " " << *incx << "\n";
  28. Complex* x = reinterpret_cast<Complex*>(px);
  29. if(*n<=0) return 0;
  30. if(*incx==1) return make_vector(x,*n).unaryExpr<scalar_norm1_op>().sum();
  31. else return make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().sum();
  32. }
  33. int EIGEN_CAT(i, EIGEN_BLAS_FUNC(amax))(int *n, RealScalar *px, int *incx)
  34. {
  35. if(*n<=0) return 0;
  36. Scalar* x = reinterpret_cast<Scalar*>(px);
  37. DenseIndex ret;
  38. if(*incx==1) make_vector(x,*n).unaryExpr<scalar_norm1_op>().maxCoeff(&ret);
  39. else make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().maxCoeff(&ret);
  40. return int(ret)+1;
  41. }
  42. int EIGEN_CAT(i, EIGEN_BLAS_FUNC(amin))(int *n, RealScalar *px, int *incx)
  43. {
  44. if(*n<=0) return 0;
  45. Scalar* x = reinterpret_cast<Scalar*>(px);
  46. DenseIndex ret;
  47. if(*incx==1) make_vector(x,*n).unaryExpr<scalar_norm1_op>().minCoeff(&ret);
  48. else make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().minCoeff(&ret);
  49. return int(ret)+1;
  50. }
  51. // computes a dot product of a conjugated vector with another vector.
  52. int EIGEN_BLAS_FUNC(dotcw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
  53. {
  54. // std::cerr << "_dotc " << *n << " " << *incx << " " << *incy << "\n";
  55. Scalar* res = reinterpret_cast<Scalar*>(pres);
  56. if(*n<=0)
  57. {
  58. *res = Scalar(0);
  59. return 0;
  60. }
  61. Scalar* x = reinterpret_cast<Scalar*>(px);
  62. Scalar* y = reinterpret_cast<Scalar*>(py);
  63. if(*incx==1 && *incy==1) *res = (make_vector(x,*n).dot(make_vector(y,*n)));
  64. else if(*incx>0 && *incy>0) *res = (make_vector(x,*n,*incx).dot(make_vector(y,*n,*incy)));
  65. else if(*incx<0 && *incy>0) *res = (make_vector(x,*n,-*incx).reverse().dot(make_vector(y,*n,*incy)));
  66. else if(*incx>0 && *incy<0) *res = (make_vector(x,*n,*incx).dot(make_vector(y,*n,-*incy).reverse()));
  67. else if(*incx<0 && *incy<0) *res = (make_vector(x,*n,-*incx).reverse().dot(make_vector(y,*n,-*incy).reverse()));
  68. return 0;
  69. }
  70. // computes a vector-vector dot product without complex conjugation.
  71. int EIGEN_BLAS_FUNC(dotuw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
  72. {
  73. Scalar* res = reinterpret_cast<Scalar*>(pres);
  74. if(*n<=0)
  75. {
  76. *res = Scalar(0);
  77. return 0;
  78. }
  79. Scalar* x = reinterpret_cast<Scalar*>(px);
  80. Scalar* y = reinterpret_cast<Scalar*>(py);
  81. if(*incx==1 && *incy==1) *res = (make_vector(x,*n).cwiseProduct(make_vector(y,*n))).sum();
  82. else if(*incx>0 && *incy>0) *res = (make_vector(x,*n,*incx).cwiseProduct(make_vector(y,*n,*incy))).sum();
  83. else if(*incx<0 && *incy>0) *res = (make_vector(x,*n,-*incx).reverse().cwiseProduct(make_vector(y,*n,*incy))).sum();
  84. else if(*incx>0 && *incy<0) *res = (make_vector(x,*n,*incx).cwiseProduct(make_vector(y,*n,-*incy).reverse())).sum();
  85. else if(*incx<0 && *incy<0) *res = (make_vector(x,*n,-*incx).reverse().cwiseProduct(make_vector(y,*n,-*incy).reverse())).sum();
  86. return 0;
  87. }
  88. RealScalar EIGEN_CAT(REAL_SCALAR_SUFFIX, EIGEN_BLAS_FUNC(nrm2))(int *n, RealScalar *px, int *incx)
  89. {
  90. // std::cerr << "__nrm2 " << *n << " " << *incx << "\n";
  91. if(*n<=0) return 0;
  92. Scalar* x = reinterpret_cast<Scalar*>(px);
  93. if(*incx==1)
  94. return make_vector(x,*n).stableNorm();
  95. return make_vector(x,*n,*incx).stableNorm();
  96. }
  97. int EIGEN_BLAS_FUNC(EIGEN_CAT(REAL_SCALAR_SUFFIX, rot))(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar *pc, RealScalar *ps)
  98. {
  99. if(*n<=0) return 0;
  100. Scalar* x = reinterpret_cast<Scalar*>(px);
  101. Scalar* y = reinterpret_cast<Scalar*>(py);
  102. RealScalar c = *pc;
  103. RealScalar s = *ps;
  104. StridedVectorType vx(make_vector(x,*n,std::abs(*incx)));
  105. StridedVectorType vy(make_vector(y,*n,std::abs(*incy)));
  106. Reverse<StridedVectorType> rvx(vx);
  107. Reverse<StridedVectorType> rvy(vy);
  108. // TODO implement mixed real-scalar rotations
  109. if(*incx<0 && *incy>0) internal::apply_rotation_in_the_plane(rvx, vy, JacobiRotation<Scalar>(c,s));
  110. else if(*incx>0 && *incy<0) internal::apply_rotation_in_the_plane(vx, rvy, JacobiRotation<Scalar>(c,s));
  111. else internal::apply_rotation_in_the_plane(vx, vy, JacobiRotation<Scalar>(c,s));
  112. return 0;
  113. }
  114. int EIGEN_BLAS_FUNC(EIGEN_CAT(REAL_SCALAR_SUFFIX, scal))(int *n, RealScalar *palpha, RealScalar *px, int *incx)
  115. {
  116. if(*n<=0) return 0;
  117. Scalar* x = reinterpret_cast<Scalar*>(px);
  118. RealScalar alpha = *palpha;
  119. // std::cerr << "__scal " << *n << " " << alpha << " " << *incx << "\n";
  120. if(*incx==1) make_vector(x,*n) *= alpha;
  121. else make_vector(x,*n,std::abs(*incx)) *= alpha;
  122. return 0;
  123. }