SkylineMatrixBase.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Guillaume Saupin <guillaume.saupin@cea.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SKYLINEMATRIXBASE_H
00026 #define EIGEN_SKYLINEMATRIXBASE_H
00027 
00028 #include "SkylineUtil.h"
00029 
00030 namespace Eigen { 
00031 
00041 template<typename Derived> class SkylineMatrixBase : public EigenBase<Derived> {
00042 public:
00043 
00044     typedef typename internal::traits<Derived>::Scalar Scalar;
00045     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00046     typedef typename internal::index<StorageKind>::type Index;
00047 
00048     enum {
00049         RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00055         ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00062         SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
00063         internal::traits<Derived>::ColsAtCompileTime>::ret),
00068         MaxRowsAtCompileTime = RowsAtCompileTime,
00069         MaxColsAtCompileTime = ColsAtCompileTime,
00070 
00071         MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
00072         MaxColsAtCompileTime>::ret),
00073 
00074         IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1,
00080         Flags = internal::traits<Derived>::Flags,
00085         CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
00090         IsRowMajor = Flags & RowMajorBit ? 1 : 0
00091     };
00092 
00093 #ifndef EIGEN_PARSED_BY_DOXYGEN
00094 
00100     typedef typename NumTraits<Scalar>::Real RealScalar;
00101 
00103     typedef Matrix<Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime),
00104                            EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime) > SquareMatrixType;
00105 
00106     inline const Derived& derived() const {
00107         return *static_cast<const Derived*> (this);
00108     }
00109 
00110     inline Derived& derived() {
00111         return *static_cast<Derived*> (this);
00112     }
00113 
00114     inline Derived& const_cast_derived() const {
00115         return *static_cast<Derived*> (const_cast<SkylineMatrixBase*> (this));
00116     }
00117 #endif // not EIGEN_PARSED_BY_DOXYGEN
00118 
00120     inline Index rows() const {
00121         return derived().rows();
00122     }
00123 
00125     inline Index cols() const {
00126         return derived().cols();
00127     }
00128 
00131     inline Index size() const {
00132         return rows() * cols();
00133     }
00134 
00137     inline Index nonZeros() const {
00138         return derived().nonZeros();
00139     }
00140 
00143     Index outerSize() const {
00144         return (int(Flags) & RowMajorBit) ? this->rows() : this->cols();
00145     }
00146 
00149     Index innerSize() const {
00150         return (int(Flags) & RowMajorBit) ? this->cols() : this->rows();
00151     }
00152 
00153     bool isRValue() const {
00154         return m_isRValue;
00155     }
00156 
00157     Derived& markAsRValue() {
00158         m_isRValue = true;
00159         return derived();
00160     }
00161 
00162     SkylineMatrixBase() : m_isRValue(false) {
00163         /* TODO check flags */
00164     }
00165 
00166     inline Derived & operator=(const Derived& other) {
00167         this->operator=<Derived > (other);
00168         return derived();
00169     }
00170 
00171     template<typename OtherDerived>
00172     inline void assignGeneric(const OtherDerived& other) {
00173         derived().resize(other.rows(), other.cols());
00174         for (Index row = 0; row < rows(); row++)
00175             for (Index col = 0; col < cols(); col++) {
00176                 if (other.coeff(row, col) != Scalar(0))
00177                     derived().insert(row, col) = other.coeff(row, col);
00178             }
00179         derived().finalize();
00180     }
00181 
00182     template<typename OtherDerived>
00183             inline Derived & operator=(const SkylineMatrixBase<OtherDerived>& other) {
00184         //TODO
00185     }
00186 
00187     template<typename Lhs, typename Rhs>
00188             inline Derived & operator=(const SkylineProduct<Lhs, Rhs, SkylineTimeSkylineProduct>& product);
00189 
00190     friend std::ostream & operator <<(std::ostream & s, const SkylineMatrixBase& m) {
00191         s << m.derived();
00192         return s;
00193     }
00194 
00195     template<typename OtherDerived>
00196     const typename SkylineProductReturnType<Derived, OtherDerived>::Type
00197     operator*(const MatrixBase<OtherDerived> &other) const;
00198 
00200     template<typename DenseDerived>
00201     void evalTo(MatrixBase<DenseDerived>& dst) const {
00202         dst.setZero();
00203         for (Index i = 0; i < rows(); i++)
00204             for (Index j = 0; j < rows(); j++)
00205                 dst(i, j) = derived().coeff(i, j);
00206     }
00207 
00208     Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime> toDense() const {
00209         return derived();
00210     }
00211 
00217     EIGEN_STRONG_INLINE const typename internal::eval<Derived, IsSkyline>::type eval() const {
00218         return typename internal::eval<Derived>::type(derived());
00219     }
00220 
00221 protected:
00222     bool m_isRValue;
00223 };
00224 
00225 } // end namespace Eigen
00226 
00227 #endif // EIGEN_SkylineMatrixBase_H