blob: 6c7edd7eb374b74a2fcfea8dd8d30b62addf96fa [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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
10#include "common.h"
11
12struct scalar_norm1_op {
13 typedef RealScalar result_type;
14 EIGEN_EMPTY_STRUCT_CTOR(scalar_norm1_op)
15 inline RealScalar operator() (const Scalar& a) const { return numext::norm1(a); }
16};
17namespace Eigen {
18 namespace internal {
19 template<> struct functor_traits<scalar_norm1_op >
20 {
21 enum { Cost = 3 * NumTraits<Scalar>::AddCost, PacketAccess = 0 };
22 };
23 }
24}
25
26// computes the sum of magnitudes of all vector elements or, for a complex vector x, the sum
27// res = |Rex1| + |Imx1| + |Rex2| + |Imx2| + ... + |Rexn| + |Imxn|, where x is a vector of order n
Austin Schuhc55b0172022-02-20 17:52:35 -080028RealScalar EIGEN_CAT(REAL_SCALAR_SUFFIX, EIGEN_BLAS_FUNC(asum))(int *n, RealScalar *px, int *incx)
Brian Silverman72890c22015-09-19 14:37:37 -040029{
30// std::cerr << "__asum " << *n << " " << *incx << "\n";
31 Complex* x = reinterpret_cast<Complex*>(px);
32
33 if(*n<=0) return 0;
34
Austin Schuh189376f2018-12-20 22:11:15 +110035 if(*incx==1) return make_vector(x,*n).unaryExpr<scalar_norm1_op>().sum();
36 else return make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().sum();
Brian Silverman72890c22015-09-19 14:37:37 -040037}
38
Austin Schuhc55b0172022-02-20 17:52:35 -080039int EIGEN_CAT(i, EIGEN_BLAS_FUNC(amax))(int *n, RealScalar *px, int *incx)
40{
41 if(*n<=0) return 0;
42 Scalar* x = reinterpret_cast<Scalar*>(px);
43
44 DenseIndex ret;
45 if(*incx==1) make_vector(x,*n).unaryExpr<scalar_norm1_op>().maxCoeff(&ret);
46 else make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().maxCoeff(&ret);
47 return int(ret)+1;
48}
49
50int EIGEN_CAT(i, EIGEN_BLAS_FUNC(amin))(int *n, RealScalar *px, int *incx)
51{
52 if(*n<=0) return 0;
53 Scalar* x = reinterpret_cast<Scalar*>(px);
54
55 DenseIndex ret;
56 if(*incx==1) make_vector(x,*n).unaryExpr<scalar_norm1_op>().minCoeff(&ret);
57 else make_vector(x,*n,std::abs(*incx)).unaryExpr<scalar_norm1_op>().minCoeff(&ret);
58 return int(ret)+1;
59}
60
Brian Silverman72890c22015-09-19 14:37:37 -040061// computes a dot product of a conjugated vector with another vector.
62int EIGEN_BLAS_FUNC(dotcw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
63{
64// std::cerr << "_dotc " << *n << " " << *incx << " " << *incy << "\n";
Austin Schuh189376f2018-12-20 22:11:15 +110065 Scalar* res = reinterpret_cast<Scalar*>(pres);
Brian Silverman72890c22015-09-19 14:37:37 -040066
Austin Schuh189376f2018-12-20 22:11:15 +110067 if(*n<=0)
68 {
69 *res = Scalar(0);
70 return 0;
71 }
Brian Silverman72890c22015-09-19 14:37:37 -040072
73 Scalar* x = reinterpret_cast<Scalar*>(px);
74 Scalar* y = reinterpret_cast<Scalar*>(py);
Brian Silverman72890c22015-09-19 14:37:37 -040075
Austin Schuh189376f2018-12-20 22:11:15 +110076 if(*incx==1 && *incy==1) *res = (make_vector(x,*n).dot(make_vector(y,*n)));
77 else if(*incx>0 && *incy>0) *res = (make_vector(x,*n,*incx).dot(make_vector(y,*n,*incy)));
78 else if(*incx<0 && *incy>0) *res = (make_vector(x,*n,-*incx).reverse().dot(make_vector(y,*n,*incy)));
79 else if(*incx>0 && *incy<0) *res = (make_vector(x,*n,*incx).dot(make_vector(y,*n,-*incy).reverse()));
80 else if(*incx<0 && *incy<0) *res = (make_vector(x,*n,-*incx).reverse().dot(make_vector(y,*n,-*incy).reverse()));
Brian Silverman72890c22015-09-19 14:37:37 -040081 return 0;
82}
83
84// computes a vector-vector dot product without complex conjugation.
85int EIGEN_BLAS_FUNC(dotuw)(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar* pres)
86{
Austin Schuh189376f2018-12-20 22:11:15 +110087 Scalar* res = reinterpret_cast<Scalar*>(pres);
Brian Silverman72890c22015-09-19 14:37:37 -040088
Austin Schuh189376f2018-12-20 22:11:15 +110089 if(*n<=0)
90 {
91 *res = Scalar(0);
92 return 0;
93 }
Brian Silverman72890c22015-09-19 14:37:37 -040094
95 Scalar* x = reinterpret_cast<Scalar*>(px);
96 Scalar* y = reinterpret_cast<Scalar*>(py);
Brian Silverman72890c22015-09-19 14:37:37 -040097
Austin Schuh189376f2018-12-20 22:11:15 +110098 if(*incx==1 && *incy==1) *res = (make_vector(x,*n).cwiseProduct(make_vector(y,*n))).sum();
99 else if(*incx>0 && *incy>0) *res = (make_vector(x,*n,*incx).cwiseProduct(make_vector(y,*n,*incy))).sum();
100 else if(*incx<0 && *incy>0) *res = (make_vector(x,*n,-*incx).reverse().cwiseProduct(make_vector(y,*n,*incy))).sum();
101 else if(*incx>0 && *incy<0) *res = (make_vector(x,*n,*incx).cwiseProduct(make_vector(y,*n,-*incy).reverse())).sum();
102 else if(*incx<0 && *incy<0) *res = (make_vector(x,*n,-*incx).reverse().cwiseProduct(make_vector(y,*n,-*incy).reverse())).sum();
Brian Silverman72890c22015-09-19 14:37:37 -0400103 return 0;
104}
105
Austin Schuhc55b0172022-02-20 17:52:35 -0800106RealScalar EIGEN_CAT(REAL_SCALAR_SUFFIX, EIGEN_BLAS_FUNC(nrm2))(int *n, RealScalar *px, int *incx)
Brian Silverman72890c22015-09-19 14:37:37 -0400107{
108// std::cerr << "__nrm2 " << *n << " " << *incx << "\n";
109 if(*n<=0) return 0;
110
111 Scalar* x = reinterpret_cast<Scalar*>(px);
112
113 if(*incx==1)
Austin Schuh189376f2018-12-20 22:11:15 +1100114 return make_vector(x,*n).stableNorm();
Brian Silverman72890c22015-09-19 14:37:37 -0400115
Austin Schuh189376f2018-12-20 22:11:15 +1100116 return make_vector(x,*n,*incx).stableNorm();
Brian Silverman72890c22015-09-19 14:37:37 -0400117}
118
Austin Schuhc55b0172022-02-20 17:52:35 -0800119int EIGEN_BLAS_FUNC(EIGEN_CAT(REAL_SCALAR_SUFFIX, rot))(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy, RealScalar *pc, RealScalar *ps)
Brian Silverman72890c22015-09-19 14:37:37 -0400120{
121 if(*n<=0) return 0;
122
123 Scalar* x = reinterpret_cast<Scalar*>(px);
124 Scalar* y = reinterpret_cast<Scalar*>(py);
125 RealScalar c = *pc;
126 RealScalar s = *ps;
127
Austin Schuh189376f2018-12-20 22:11:15 +1100128 StridedVectorType vx(make_vector(x,*n,std::abs(*incx)));
129 StridedVectorType vy(make_vector(y,*n,std::abs(*incy)));
Brian Silverman72890c22015-09-19 14:37:37 -0400130
131 Reverse<StridedVectorType> rvx(vx);
132 Reverse<StridedVectorType> rvy(vy);
133
134 // TODO implement mixed real-scalar rotations
135 if(*incx<0 && *incy>0) internal::apply_rotation_in_the_plane(rvx, vy, JacobiRotation<Scalar>(c,s));
136 else if(*incx>0 && *incy<0) internal::apply_rotation_in_the_plane(vx, rvy, JacobiRotation<Scalar>(c,s));
137 else internal::apply_rotation_in_the_plane(vx, vy, JacobiRotation<Scalar>(c,s));
138
139 return 0;
140}
141
Austin Schuhc55b0172022-02-20 17:52:35 -0800142int EIGEN_BLAS_FUNC(EIGEN_CAT(REAL_SCALAR_SUFFIX, scal))(int *n, RealScalar *palpha, RealScalar *px, int *incx)
Brian Silverman72890c22015-09-19 14:37:37 -0400143{
144 if(*n<=0) return 0;
145
146 Scalar* x = reinterpret_cast<Scalar*>(px);
147 RealScalar alpha = *palpha;
148
149// std::cerr << "__scal " << *n << " " << alpha << " " << *incx << "\n";
150
Austin Schuh189376f2018-12-20 22:11:15 +1100151 if(*incx==1) make_vector(x,*n) *= alpha;
152 else make_vector(x,*n,std::abs(*incx)) *= alpha;
Brian Silverman72890c22015-09-19 14:37:37 -0400153
154 return 0;
155}