You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.7 KiB

  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 internal::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(EIGEN_CAT(REAL_SCALAR_SUFFIX,SCALAR_SUFFIX),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 vector(x,*n).unaryExpr<scalar_norm1_op>().sum();
  31. else return vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().sum();
  32. }
  33. // computes a dot product of a conjugated vector with another vector.
  34. int EIGEN_BLAS_FUNC(dotcw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
  35. {
  36. // std::cerr << "_dotc " << *n << " " << *incx << " " << *incy << "\n";
  37. if(*n<=0) return 0;
  38. Scalar* x = reinterpret_cast<Scalar*>(px);
  39. Scalar* y = reinterpret_cast<Scalar*>(py);
  40. Scalar* res = reinterpret_cast<Scalar*>(pres);
  41. if(*incx==1 && *incy==1) *res = (vector(x,*n).dot(vector(y,*n)));
  42. else if(*incx>0 && *incy>0) *res = (vector(x,*n,*incx).dot(vector(y,*n,*incy)));
  43. else if(*incx<0 && *incy>0) *res = (vector(x,*n,-*incx).reverse().dot(vector(y,*n,*incy)));
  44. else if(*incx>0 && *incy<0) *res = (vector(x,*n,*incx).dot(vector(y,*n,-*incy).reverse()));
  45. else if(*incx<0 && *incy<0) *res = (vector(x,*n,-*incx).reverse().dot(vector(y,*n,-*incy).reverse()));
  46. return 0;
  47. }
  48. // computes a vector-vector dot product without complex conjugation.
  49. int EIGEN_BLAS_FUNC(dotuw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
  50. {
  51. // std::cerr << "_dotu " << *n << " " << *incx << " " << *incy << "\n";
  52. if(*n<=0) return 0;
  53. Scalar* x = reinterpret_cast<Scalar*>(px);
  54. Scalar* y = reinterpret_cast<Scalar*>(py);
  55. Scalar* res = reinterpret_cast<Scalar*>(pres);
  56. if(*incx==1 && *incy==1) *res = (vector(x,*n).cwiseProduct(vector(y,*n))).sum();
  57. else if(*incx>0 && *incy>0) *res = (vector(x,*n,*incx).cwiseProduct(vector(y,*n,*incy))).sum();
  58. else if(*incx<0 && *incy>0) *res = (vector(x,*n,-*incx).reverse().cwiseProduct(vector(y,*n,*incy))).sum();
  59. else if(*incx>0 && *incy<0) *res = (vector(x,*n,*incx).cwiseProduct(vector(y,*n,-*incy).reverse())).sum();
  60. else if(*incx<0 && *incy<0) *res = (vector(x,*n,-*incx).reverse().cwiseProduct(vector(y,*n,-*incy).reverse())).sum();
  61. return 0;
  62. }
  63. RealScalar EIGEN_CAT(EIGEN_CAT(REAL_SCALAR_SUFFIX,SCALAR_SUFFIX),nrm2_)(int *n, RealScalar *px, int *incx)
  64. {
  65. // std::cerr << "__nrm2 " << *n << " " << *incx << "\n";
  66. if(*n<=0) return 0;
  67. Scalar* x = reinterpret_cast<Scalar*>(px);
  68. if(*incx==1)
  69. return vector(x,*n).stableNorm();
  70. return vector(x,*n,*incx).stableNorm();
  71. }
  72. int EIGEN_CAT(EIGEN_CAT(SCALAR_SUFFIX,REAL_SCALAR_SUFFIX),rot_)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar *pc, RealScalar *ps)
  73. {
  74. if(*n<=0) return 0;
  75. Scalar* x = reinterpret_cast<Scalar*>(px);
  76. Scalar* y = reinterpret_cast<Scalar*>(py);
  77. RealScalar c = *pc;
  78. RealScalar s = *ps;
  79. StridedVectorType vx(vector(x,*n,std::abs(*incx)));
  80. StridedVectorType vy(vector(y,*n,std::abs(*incy)));
  81. Reverse<StridedVectorType> rvx(vx);
  82. Reverse<StridedVectorType> rvy(vy);
  83. // TODO implement mixed real-scalar rotations
  84. if(*incx<0 && *incy>0) internal::apply_rotation_in_the_plane(rvx, vy, JacobiRotation<Scalar>(c,s));
  85. else if(*incx>0 && *incy<0) internal::apply_rotation_in_the_plane(vx, rvy, JacobiRotation<Scalar>(c,s));
  86. else internal::apply_rotation_in_the_plane(vx, vy, JacobiRotation<Scalar>(c,s));
  87. return 0;
  88. }
  89. int EIGEN_CAT(EIGEN_CAT(SCALAR_SUFFIX,REAL_SCALAR_SUFFIX),scal_)(int *n, RealScalar *palpha, RealScalar *px, int *incx)
  90. {
  91. if(*n<=0) return 0;
  92. Scalar* x = reinterpret_cast<Scalar*>(px);
  93. RealScalar alpha = *palpha;
  94. // std::cerr << "__scal " << *n << " " << alpha << " " << *incx << "\n";
  95. if(*incx==1) vector(x,*n) *= alpha;
  96. else vector(x,*n,std::abs(*incx)) *= alpha;
  97. return 0;
  98. }