aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2008-03-15 11:05:38 +0000
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2008-03-15 11:05:38 +0000
commit29184ad27df1b398d4619dea78d0fb8aee445c1f (patch)
tree71600836aa627db8e1c6700bf37661f9af4be19d /Eigen/src
parentfb3438e609be743c066af9167e6ed83cd17af394 (diff)
- introduce sum() returning the sum of the coeffs of a vector
- reimplement trace() as just diagonal().sum() - apidoc fixes
Diffstat (limited to 'Eigen/src')
-rw-r--r--Eigen/src/Core/DiagonalCoeffs.h8
-rw-r--r--Eigen/src/Core/Dot.h6
-rw-r--r--Eigen/src/Core/MatrixBase.h8
-rw-r--r--Eigen/src/Core/MatrixStorage.h6
-rw-r--r--Eigen/src/Core/SumOfCoeffs.h102
-rw-r--r--Eigen/src/Core/Trace.h80
6 files changed, 120 insertions, 90 deletions
diff --git a/Eigen/src/Core/DiagonalCoeffs.h b/Eigen/src/Core/DiagonalCoeffs.h
index 47d24fea4..69fab3066 100644
--- a/Eigen/src/Core/DiagonalCoeffs.h
+++ b/Eigen/src/Core/DiagonalCoeffs.h
@@ -27,10 +27,12 @@
/** \class DiagonalCoeffs
*
- * \brief Expression of the main diagonal of a square matrix
+ * \brief Expression of the main diagonal of a matrix
*
* \param MatrixType the type of the object in which we are taking the main diagonal
*
+ * The matrix is not required to be square.
+ *
* This class represents an expression of the main diagonal of a square matrix.
* It is the return type of MatrixBase::diagonal() and most of the time this is
* the only way it is used.
@@ -84,7 +86,9 @@ template<typename MatrixType> class DiagonalCoeffs
const typename MatrixType::XprCopy m_matrix;
};
-/** \returns an expression of the main diagonal of *this, which must be a square matrix.
+/** \returns an expression of the main diagonal of the matrix \c *this
+ *
+ * \c *this is not required to be square.
*
* Example: \include MatrixBase_diagonal.cpp
* Output: \verbinclude MatrixBase_diagonal.out
diff --git a/Eigen/src/Core/Dot.h b/Eigen/src/Core/Dot.h
index 4147dee56..33f69f987 100644
--- a/Eigen/src/Core/Dot.h
+++ b/Eigen/src/Core/Dot.h
@@ -82,12 +82,12 @@ MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
ei_dot_unroller<SizeAtCompileTime-1,
SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT ? SizeAtCompileTime : Dynamic,
Derived, MatrixBase<OtherDerived> >
- ::run(*static_cast<const Derived*>(this), other, res);
+ ::run(derived(), other, res);
else
{
- res = (*this).coeff(0) * ei_conj(other.coeff(0));
+ res = coeff(0) * ei_conj(other.coeff(0));
for(int i = 1; i < size(); i++)
- res += (*this).coeff(i)* ei_conj(other.coeff(i));
+ res += coeff(i)* ei_conj(other.coeff(i));
}
return res;
}
diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h
index f51c06d6c..95fc05b8e 100644
--- a/Eigen/src/Core/MatrixBase.h
+++ b/Eigen/src/Core/MatrixBase.h
@@ -230,8 +230,7 @@ template<typename Derived> class MatrixBase
{ return matrix*scalar; }
//@}
- /** \name Matrix product and related notions
- * including the trace...
+ /** \name Matrix product
*/
//@{
template<typename OtherDerived>
@@ -244,7 +243,12 @@ template<typename Derived> class MatrixBase
template<typename OtherDerived>
Derived& operator*=(const MatrixBase<OtherDerived>& other);
+ //@}
+ /** \name Sums of coefficients
+ */
+ //@{
+ Scalar sum() const;
Scalar trace() const;
//@}
diff --git a/Eigen/src/Core/MatrixStorage.h b/Eigen/src/Core/MatrixStorage.h
index 4bc0423f2..91290ea59 100644
--- a/Eigen/src/Core/MatrixStorage.h
+++ b/Eigen/src/Core/MatrixStorage.h
@@ -26,11 +26,11 @@
#ifndef EIGEN_MATRIXSTORAGE_H
#define EIGEN_MATRIXSTORAGE_H
-/** \class ei_matrix_storage
+/** \internal
*
- * \brief Stores the data of a matrix
+ * \class ei_matrix_storage
*
- * \internal
+ * \brief Stores the data of a matrix
*
* This class stores the data of fixed-size, dynamic-size or mixed matrices
* in a way as compact as possible.
diff --git a/Eigen/src/Core/SumOfCoeffs.h b/Eigen/src/Core/SumOfCoeffs.h
new file mode 100644
index 000000000..36e75d3e9
--- /dev/null
+++ b/Eigen/src/Core/SumOfCoeffs.h
@@ -0,0 +1,102 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra. Eigen itself is part of the KDE project.
+//
+// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
+//
+// Eigen is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 3 of the License, or (at your option) any later version.
+//
+// Alternatively, you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License and a copy of the GNU General Public License along with
+// Eigen. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef EIGEN_SUMOFCOEFFS_H
+#define EIGEN_SUMOFCOEFFS_H
+
+template<int Index, int Size, typename Derived>
+struct ei_sumofcoeffs_unroller
+{
+ static void run(const Derived &v1, typename Derived::Scalar &dot)
+ {
+ ei_sumofcoeffs_unroller<Index-1, Size, Derived>::run(v1, dot);
+ dot += v1.coeff(Index);
+ }
+};
+
+template<int Size, typename Derived>
+struct ei_sumofcoeffs_unroller<0, Size, Derived>
+{
+ static void run(const Derived &v1, typename Derived::Scalar &dot)
+ {
+ dot = v1.coeff(0);
+ }
+};
+
+template<int Index, typename Derived>
+struct ei_sumofcoeffs_unroller<Index, Dynamic, Derived>
+{
+ static void run(const Derived&, typename Derived::Scalar&) {}
+};
+
+// prevent buggy user code from causing an infinite recursion
+template<int Index, typename Derived>
+struct ei_sumofcoeffs_unroller<Index, 0, Derived>
+{
+ static void run(const Derived&, typename Derived::Scalar&)
+{}
+};
+
+/** \returns the sum of all coefficients of *this
+ *
+ * \only_for_vectors
+ *
+ * \sa trace()
+ */
+template<typename Derived>
+typename ei_traits<Derived>::Scalar
+MatrixBase<Derived>::sum() const
+{
+ assert(IsVectorAtCompileTime);
+ Scalar res;
+ if(EIGEN_UNROLLED_LOOPS
+ && SizeAtCompileTime != Dynamic
+ && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT)
+ ei_sumofcoeffs_unroller<SizeAtCompileTime-1,
+ SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT ? SizeAtCompileTime : Dynamic,
+ Derived>
+ ::run(derived(),res);
+ else
+ {
+ res = coeff(0);
+ for(int i = 1; i < size(); i++)
+ res += coeff(i);
+ }
+ return res;
+}
+
+/** \returns the trace of \c *this, i.e. the sum of the coefficients on the main diagonal.
+ *
+ * \c *this can be any matrix, not necessarily square.
+ *
+ * \sa diagonal(), sum()
+ */
+template<typename Derived>
+typename ei_traits<Derived>::Scalar
+MatrixBase<Derived>::trace() const
+{
+ return diagonal().sum();
+}
+
+#endif // EIGEN_SUMOFCOEFFS_H
diff --git a/Eigen/src/Core/Trace.h b/Eigen/src/Core/Trace.h
deleted file mode 100644
index dcb317de5..000000000
--- a/Eigen/src/Core/Trace.h
+++ /dev/null
@@ -1,80 +0,0 @@
-// This file is part of Eigen, a lightweight C++ template library
-// for linear algebra. Eigen itself is part of the KDE project.
-//
-// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
-//
-// Eigen is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// Alternatively, you can redistribute it and/or
-// modify it under the terms of the GNU General Public License as
-// published by the Free Software Foundation; either version 2 of
-// the License, or (at your option) any later version.
-//
-// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
-// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License and a copy of the GNU General Public License along with
-// Eigen. If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef EIGEN_TRACE_H
-#define EIGEN_TRACE_H
-
-template<int Index, int Rows, typename Derived> struct ei_trace_unroller
-{
- static void run(const Derived &mat, typename Derived::Scalar &trace)
- {
- ei_trace_unroller<Index-1, Rows, Derived>::run(mat, trace);
- trace += mat.coeff(Index, Index);
- }
-};
-
-template<int Rows, typename Derived> struct ei_trace_unroller<0, Rows, Derived>
-{
- static void run(const Derived &mat, typename Derived::Scalar &trace)
- {
- trace = mat.coeff(0, 0);
- }
-};
-
-template<int Index, typename Derived> struct ei_trace_unroller<Index, Dynamic, Derived>
-{
- static void run(const Derived&, typename Derived::Scalar&) {}
-};
-
-// prevent buggy user code from causing an infinite recursion
-template<int Index, typename Derived> struct ei_trace_unroller<Index, 0, Derived>
-{
- static void run(const Derived&, typename Derived::Scalar&) {}
-};
-
-/** \returns the trace of *this, which must be a square matrix.
- *
- * \sa diagonal() */
-template<typename Derived>
-typename ei_traits<Derived>::Scalar
-MatrixBase<Derived>::trace() const
-{
- assert(rows() == cols());
- Scalar res;
- if(EIGEN_UNROLLED_LOOPS
- && RowsAtCompileTime != Dynamic
- && RowsAtCompileTime <= EIGEN_UNROLLING_LIMIT)
- ei_trace_unroller<RowsAtCompileTime-1,
- RowsAtCompileTime <= EIGEN_UNROLLING_LIMIT ? RowsAtCompileTime : Dynamic, Derived>
- ::run(*static_cast<const Derived*>(this), res);
- else
- {
- res = coeff(0, 0);
- for(int i = 1; i < rows(); i++)
- res += coeff(i, i);
- }
- return res;
-}
-
-#endif // EIGEN_TRACE_H