2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Bernard HUGUENEY
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
18 #include <Eigen/Sparse>
24 #include <Eigen/IterativeLinearSolvers>
25 #include <Eigen/SparseCholesky>
29 #include "tostring_common.hxx"
31 #include "matrixiterator.hxx"
32 #include "types_subtraction.hxx"
33 #include "types_addition.hxx"
34 #include "types_multiplication.hxx"
35 #include "configvariable.hxx"
36 #include "scilabWrite.hxx"
38 #include "types_tools.hxx"
40 #include "sparseOp.hxx"
44 #include "elem_common.h"
49 /* used for debuging output
51 template<typename Os, typename In, typename Sz> Os& writeData(wchar_t const* title, In beg, Sz n, Os& os)
54 /* TODO: use tostring_common (with a kind of std::boolalpha for boolean output)
56 mycopy_n(beg, n, std::ostream_iterator<typename std::iterator_traits<In>::value_type, char>(os, L" "));
63 Printer (int precision) : p(precision)
67 std::wstring emptyName( /* */) const
73 std::wstring operator()(T const& t) const
76 std::wostringstream ostr;
85 std::wstring Printer::operator()(bool const& b) const
98 std::wstring Printer::operator()(double const& d) const
100 std::wostringstream ostr;
102 getDoubleFormat(d, &df);
103 addDoubleValue(&ostr, d, &df);
108 std::wstring Printer::operator()(std::complex<double > const& c) const
110 std::wostringstream ostr;
112 DoubleFormat dfR, dfI;
113 getComplexFormat(c.real(), c.imag(), &iLen, &dfR, &dfI);
114 addDoubleComplexValue(&ostr, c.real(), c.imag(), iLen, &dfR, &dfI);
119 std::wstring Printer::emptyName<bool>() const
125 template<typename T> std::wstring toString(T const& m, int precision)
127 std::wostringstream ostr;
131 getSignedIntFormat(m.rows(), &iWidthRows);
132 getSignedIntFormat(m.cols(), &iWidthCols);
135 addUnsignedIntValue<unsigned long long>(&ostr, m.rows(), iWidthRows);
137 addUnsignedIntValue<unsigned long long>(&ostr, m.cols(), iWidthCols);
140 Printer p(precision);
143 ostr << ( p.emptyName<typename Eigen::internal::traits<T>::Scalar>());
145 ostr << L" sparse matrix\n\n";
147 auto * pIColPos = m.innerIndexPtr();
148 auto * pINbItemByRow = m.outerIndexPtr();
152 for (size_t j = 1 ; j < m.rows() + 1 ; j++)
154 for (size_t i = pINbItemByRow[j - 1] ; i < pINbItemByRow[j] ; i++)
157 addUnsignedIntValue<unsigned long long>(&ostr, (int)j, iWidthRows);
159 addUnsignedIntValue<unsigned long long>(&ostr, pIColPos[iPos] + 1, iWidthCols);
160 ostr << L")\t" << p(m.valuePtr()[iPos]) << std::endl;
169 /** utility function to compare two Eigen::Sparse matrices to equality
171 template<typename T> bool equal(T const& s1, T const& s2)
174 // only compares elts when both inner iterators are "defined", so we assert that we compared all the non zero values
175 // i.e. the inner iterators where defined for the same values
176 std::size_t nbElts(0);
178 for (int k = 0; res && k != s1.outerSize(); ++k)
180 for (typename T::InnerIterator it1(s1, k), it2(s2, k); res && it1 && it2 ; ++it1, ++it2, ++nbElts)
182 res = (it1.value() == it2.value()
183 && it1.row() == it2.row()
184 && it1.col() == it2.col());
187 return res && (nbElts == s1.nonZeros()) && (nbElts == s2.nonZeros());
190 utility function to set non zero values of an Eigen::Sparse matrix to a fixed values
191 @param s : sparse matrix to modify
192 @param v : value to set (default to 1.)
194 template<typename T> bool setNonZero(T& s, typename Eigen::internal::traits<T>::Scalar v = 1.)
196 for (auto j = 0; j < s.outerSize(); ++j)
198 for (typename T::InnerIterator it(s, j); it; ++it)
208 template<typename Src, typename Sp>
209 void doAppend(Src SPARSE_CONST& src, int r, int c, Sp& dest)
211 typedef typename Eigen::internal::traits<Sp>::Scalar data_t;
212 mycopy_n(makeMatrixIterator<data_t>(src, makeNonZerosIterator(src)), nonZeros(src)
213 , makeMatrixIterator<data_t>(dest, makeTranslatedIterator(makeNonZerosIterator(src), Coords2D(r, c))));
216 template<typename Scalar1, typename Scalar2>
217 void doAppend(Eigen::SparseMatrix<Scalar1, Eigen::RowMajor> SPARSE_CONST& src, int r, int c, Eigen::SparseMatrix<Scalar2, Eigen::RowMajor>& dest)
219 typedef typename Eigen::SparseMatrix<Scalar1, Eigen::RowMajor>::InnerIterator srcIt_t;
220 for (std::size_t k = 0; k != src.outerSize(); ++k)
222 for (srcIt_t it(src, (int)k); it; ++it)
224 dest.insert( it.row() + r, it.col() + c) = it.value();
229 Sp is an Eigen::SparseMatrix
231 template<typename Sp, typename M>
232 void cwiseInPlaceProduct(Sp& sp, M SPARSE_CONST& m)
234 // should be a transform_n() over makeNonZerosIterator(src)
235 for (std::size_t k = 0; k != sp.outerSize(); ++k)
237 for (typename Sp::InnerIterator it(sp, k); it; ++it)
239 it.valueRef() *= get<typename Eigen::internal::traits<Sp>::Scalar >(m, it.row(), it.col());
248 template<typename T, typename Arg>
249 T* create_new(Arg const& a)
255 Double* create_new(double const& d)
257 Double* res(new Double(1, 1, false));
263 Double* create_new(std::complex<double>const& c)
265 Double* res(new Double(1, 1, true));
266 res->set(0, 0, c.real());
267 res->setImg(0, 0, c.imag());
272 Double* create_new(Sparse const& s)
274 Sparse& cs(const_cast<Sparse&>(s)); // inherited member functions are not const-correct
275 Double* res(new Double(cs.getRows(), cs.getCols(), cs.isComplex()));
276 const_cast<Sparse&>(s).fill(*res);
286 Inspector::removeItem(this);
290 Sparse::Sparse(Sparse const& src)
291 : matrixReal(src.matrixReal ? new RealSparse_t(*src.matrixReal) : 0)
292 , matrixCplx(src.matrixCplx ? new CplxSparse_t(*src.matrixCplx) : 0)
295 m_iRows = const_cast<Sparse*>(&src)->getRows();
296 m_iCols = const_cast<Sparse*>(&src)->getCols();
297 m_iSize = m_iRows * m_iCols;
299 m_piDims[0] = m_iRows;
300 m_piDims[1] = m_iCols;
302 Inspector::addItem(this);
306 Sparse::Sparse(int _iRows, int _iCols, bool cplx)
307 : matrixReal(cplx ? 0 : new RealSparse_t(_iRows, _iCols))
308 , matrixCplx(cplx ? new CplxSparse_t(_iRows, _iCols) : 0)
312 m_iSize = _iRows * _iCols;
314 m_piDims[0] = _iRows;
315 m_piDims[1] = _iCols;
317 Inspector::addItem(this);
321 Sparse::Sparse(Double SPARSE_CONST& src)
324 int size = src.getSize();
325 int row = src.getRows();
326 Double* idx = new Double(src.getSize(), 2);
327 double* p = idx->get();
328 for (int i = 0; i < size; ++i)
330 p[i] = (double)(i % row) + 1;
331 p[i + size] = (double)(i / row) + 1;
333 create2(src.getRows(), src.getCols(), src, *idx);
336 Inspector::addItem(this);
340 Sparse::Sparse(Double SPARSE_CONST& src, Double SPARSE_CONST& idx)
342 int idxrow = idx.getRows();
343 int rows = static_cast<int>(*std::max_element(idx.get(), idx.get() + idxrow));
344 int cols = static_cast<int>(*std::max_element(idx.get() + idxrow, idx.get() + idxrow * 2));
346 create2(rows, cols, src, idx);
348 Inspector::removeItem(this);
352 Sparse::Sparse(Double SPARSE_CONST& src, Double SPARSE_CONST& idx, Double SPARSE_CONST& dims)
354 create2(static_cast<int>(dims.get(0)), static_cast<int>(dims.get(1)), src, idx);
356 Inspector::addItem(this);
360 Sparse::Sparse(RealSparse_t* realSp, CplxSparse_t* cplxSp): matrixReal(realSp), matrixCplx(cplxSp)
364 m_iCols = realSp->cols();
365 m_iRows = realSp->rows();
369 m_iCols = cplxSp->cols();
370 m_iRows = cplxSp->rows();
372 m_iSize = m_iCols * m_iRows;
374 m_piDims[0] = m_iRows;
375 m_piDims[1] = m_iCols;
379 Inspector::addItem(this);
383 Sparse::Sparse(Double SPARSE_CONST& xadj, Double SPARSE_CONST& adjncy, Double SPARSE_CONST& src, std::size_t r, std::size_t c)
385 Adjacency a(xadj.get(), adjncy.get());
386 create(static_cast<int>(r), static_cast<int>(c), src, makeIteratorFromVar(a), src.getSize());
388 Inspector::addItem(this);
392 Sparse::Sparse(int rows, int cols, int nonzeros, int* inner, int* outer, double* real, double* img)
399 matrixCplx = new CplxSparse_t(rows, cols);
400 matrixCplx->reserve((int)nonzeros);
401 out = matrixCplx->outerIndexPtr();
402 in = matrixCplx->innerIndexPtr();
403 matrixReal = nullptr;
407 matrixReal = new RealSparse_t(rows, cols);
408 matrixReal->reserve((int)nonzeros);
409 out = matrixReal->outerIndexPtr();
410 in = matrixReal->innerIndexPtr();
411 matrixCplx = nullptr;
414 //update outerIndexPtr
415 memcpy(out, outer, sizeof(int) * (rows + 1));
416 //update innerIndexPtr
417 memcpy(in, inner, sizeof(int) * nonzeros);
421 std::complex<double>* data = matrixCplx->valuePtr();
422 for (int i = 0; i < nonzeros; ++i)
424 data[i] = std::complex<double>(real[i], img[i]);
429 double* data = matrixReal->valuePtr();
430 for (int i = 0; i < nonzeros; ++i)
439 m_iSize = cols * rows;
441 m_piDims[0] = m_iRows;
442 m_piDims[1] = m_iCols;
444 matrixCplx ? matrixCplx->resizeNonZeros(nonzeros) : matrixReal->resizeNonZeros(nonzeros);
448 template<typename DestIter>
449 void Sparse::create(int rows, int cols, Double SPARSE_CONST& src, DestIter o, std::size_t n)
453 m_iSize = cols * rows;
455 m_piDims[0] = m_iRows;
456 m_piDims[1] = m_iCols;
461 matrixCplx = new CplxSparse_t(rows, cols);
462 matrixCplx->reserve((int)n);
463 mycopy_n(makeMatrixIterator<std::complex<double> >(src, RowWiseFullIterator(src.getRows(), src.getCols())), n, makeMatrixIterator<std::complex<double> >(*matrixCplx, o));
467 matrixReal = new RealSparse_t(rows, cols);
468 matrixReal->reserve((int)n);
470 mycopy_n(makeMatrixIterator<double >(src, RowWiseFullIterator(src.getRows(), src.getCols())), n
471 , makeMatrixIterator<double>(*matrixReal, o));
476 void Sparse::create2(int rows, int cols, Double SPARSE_CONST& src, Double SPARSE_CONST& idx)
478 int nnz = src.getSize();
479 double* i = idx.get();
480 double* j = i + idx.getRows();
481 double* valR = src.get();
487 typedef Eigen::Triplet<std::complex<double> > T;
488 std::vector<T> tripletList;
489 tripletList.reserve((int)nnz);
491 double* valI = src.getImg();
493 for (int k = 0; k < nnz; ++k)
495 tripletList.push_back(T(static_cast<int>(i[k]) - 1, static_cast<int>(j[k]) - 1, std::complex<double>(valR[k], valI[k])));
498 matrixCplx = new CplxSparse_t(rows, cols);
499 matrixCplx->setFromTriplets(tripletList.begin(), tripletList.end());
500 m_iRows = matrixCplx->rows();
501 m_iCols = matrixCplx->cols();
507 typedef Eigen::Triplet<double> T;
508 std::vector<T> tripletList;
509 tripletList.reserve((int)nnz);
511 for (int k = 0; k < nnz; ++k)
513 tripletList.push_back(T(static_cast<int>(i[k]) - 1, static_cast<int>(j[k]) - 1, valR[k]));
516 matrixReal = new RealSparse_t(rows, cols);
517 matrixReal->setFromTriplets(tripletList.begin(), tripletList.end());
519 m_iRows = matrixReal->rows();
520 m_iCols = matrixReal->cols();
523 m_iSize = m_iCols * m_iRows;
525 m_piDims[0] = m_iRows;
526 m_piDims[1] = m_iCols;
530 void Sparse::fill(Double& dest, int r, int c) SPARSE_CONST
532 Sparse & cthis(const_cast<Sparse&>(*this));
535 mycopy_n(makeMatrixIterator<std::complex<double> >(*matrixCplx, RowWiseFullIterator(cthis.getRows(), cthis.getCols())), cthis.getSize()
536 , makeMatrixIterator<std::complex<double> >(dest, RowWiseFullIterator(dest.getRows(), dest.getCols(), r, c)));
540 mycopy_n( makeMatrixIterator<double>(*matrixReal, RowWiseFullIterator(cthis.getRows(), cthis.getCols())), cthis.getSize()
541 , makeMatrixIterator<double >(dest, RowWiseFullIterator(dest.getRows(), dest.getCols(), r, c)));
545 Sparse* Sparse::set(int _iRows, int _iCols, std::complex<double> v, bool _bFinalize)
547 if (_iRows >= getRows() || _iCols >= getCols())
552 typedef Sparse* (Sparse::*set_t)(int, int, std::complex<double>, bool);
553 Sparse* pIT = checkRef(this, (set_t)&Sparse::set, _iRows, _iCols, v, _bFinalize);
561 matrixReal->coeffRef(_iRows, _iCols) = v.real();
565 matrixCplx->coeffRef(_iRows, _iCols) = v;
575 Sparse* Sparse::set(int _iRows, int _iCols, double _dblReal, bool _bFinalize)
577 if (_iRows >= getRows() || _iCols >= getCols())
582 typedef Sparse* (Sparse::*set_t)(int, int, double, bool);
583 Sparse* pIT = checkRef(this, (set_t)&Sparse::set, _iRows, _iCols, _dblReal, _bFinalize);
591 matrixReal->coeffRef(_iRows, _iCols) = _dblReal;
595 matrixCplx->coeffRef(_iRows, _iCols) = std::complex<double>(_dblReal, 0);
607 void Sparse::finalize()
611 matrixCplx->prune(&keepForSparse<std::complex<double> >);
612 matrixCplx->finalize();
616 matrixReal->prune(&keepForSparse<double>);
617 matrixReal->finalize();
622 bool Sparse::neg(InternalType *& out)
624 SparseBool * _out = new SparseBool(getRows(), getCols());
625 types::neg(getRows(), getCols(), matrixReal, _out->matrixBool);
632 bool Sparse::isComplex() const
634 return static_cast<bool>(matrixCplx != NULL);
637 // TODO: should have both a bounds checking and a non-checking interface to elt access
638 double* Sparse::get()
640 if (isComplex() == false)
642 return matrixReal->valuePtr();
648 double Sparse::get(int _iRows, int _iCols) const
650 return getReal(_iRows, _iCols);
653 double Sparse::getReal(int _iRows, int _iCols) const
658 res = matrixReal->coeff(_iRows, _iCols);
662 res = matrixCplx->coeff(_iRows, _iCols).real();
667 std::complex<double>* Sparse::getImg()
671 return matrixCplx->valuePtr();
677 std::complex<double> Sparse::getImg(int _iRows, int _iCols) const
679 std::complex<double> res;
682 res = matrixCplx->coeff(_iRows, _iCols);
686 res = std::complex<double>(matrixReal->coeff(_iRows, _iCols), 0.);
692 void Sparse::whoAmI() SPARSE_CONST
694 std::cout << "types::Sparse";
697 Sparse* Sparse::clone(void)
699 return new Sparse(*this);
702 bool Sparse::zero_set()
706 matrixReal->setZero();
710 matrixCplx->setZero();
716 // TODO: handle precision and line length
717 bool Sparse::toString(std::wostringstream& ostr)
719 int iPrecision = ConfigVariable::getFormatSize();
723 res = ::toString(*matrixReal, iPrecision);
727 res = ::toString(*matrixCplx, iPrecision);
734 Sparse* Sparse::resize(int _iNewRows, int _iNewCols)
736 typedef Sparse* (Sparse::*resize_t)(int, int);
737 Sparse* pIT = checkRef(this, (resize_t)&Sparse::resize, _iNewRows, _iNewCols);
743 if (_iNewRows <= getRows() && _iNewCols <= getCols())
745 //nothing to do: hence we do NOT fail
755 size_t iNonZeros = nonZeros();
756 RealSparse_t *newReal = new RealSparse_t(_iNewRows, _iNewCols);
757 newReal->reserve((int)iNonZeros);
761 int* pRows = new int[iNonZeros * 2];
763 int* pCols = pRows + iNonZeros;
766 double* pNonZeroR = new double[iNonZeros];
767 double* pNonZeroI = new double[iNonZeros];
768 outputValues(pNonZeroR, pNonZeroI);
770 typedef Eigen::Triplet<double> triplet;
771 std::vector<triplet> tripletList;
773 for (size_t i = 0 ; i < iNonZeros ; i++)
775 tripletList.push_back(triplet((int)pRows[i] - 1, (int)pCols[i] - 1, pNonZeroR[i]));
778 newReal->setFromTriplets(tripletList.begin(), tripletList.end());
781 matrixReal = newReal;
789 size_t iNonZeros = nonZeros();
790 CplxSparse_t *newCplx = new CplxSparse_t(_iNewRows, _iNewCols);
791 newCplx->reserve((int)iNonZeros);
794 int* pRows = new int[iNonZeros * 2];
796 int* pCols = pRows + iNonZeros;
799 double* pNonZeroR = new double[iNonZeros];
800 double* pNonZeroI = new double[iNonZeros];
801 outputValues(pNonZeroR, pNonZeroI);
803 typedef Eigen::Triplet<std::complex<double> > triplet;
804 std::vector<triplet> tripletList;
806 for (size_t i = 0 ; i < iNonZeros ; i++)
808 tripletList.push_back(triplet((int)pRows[i] - 1, (int)pCols[i] - 1, std::complex<double>(pNonZeroR[i], pNonZeroI[i])));
811 newCplx->setFromTriplets(tripletList.begin(), tripletList.end());
815 matrixCplx = newCplx;
823 m_iSize = _iNewRows * _iNewCols;
824 m_piDims[0] = m_iRows;
825 m_piDims[1] = m_iCols;
835 // TODO decide if a complex matrix with 0 imag can be == to a real matrix
836 // not true for dense (cf double.cpp)
837 bool Sparse::operator==(const InternalType& it) SPARSE_CONST
839 Sparse* otherSparse = const_cast<Sparse*>(dynamic_cast<Sparse const*>(&it));/* types::GenericType is not const-correct :( */
840 Sparse & cthis (const_cast<Sparse&>(*this));
842 if (otherSparse == NULL)
847 if (otherSparse->getRows() != cthis.getRows())
852 if (otherSparse->getCols() != cthis.getCols())
857 if (otherSparse->isComplex() != isComplex())
864 return equal(*matrixCplx, *otherSparse->matrixCplx);
868 return equal(*matrixReal, *otherSparse->matrixReal);
872 bool Sparse::one_set()
876 return setNonZero(*matrixCplx);
880 return setNonZero(*matrixReal);
884 void Sparse::toComplex()
890 matrixCplx = new CplxSparse_t(matrixReal->cast<std::complex<double> >());
903 GenericType* Sparse::insertNew(typed_list* _pArgs)
908 int iDims = (int)_pArgs->size();
909 int* piMaxDim = new int[iDims];
910 int* piCountDim = new int[iDims];
911 bool bComplex = isComplex();
912 bool bUndefine = false;
914 //evaluate each argument and replace by appropriate value and compute the count of combinations
915 int iSeqCount = checkIndexesArguments(NULL, _pArgs, &pArg, piMaxDim, piCountDim);
919 cleanIndexesArguments(_pArgs, &pArg);
920 return createEmptyDouble();
925 iSeqCount = -iSeqCount;
931 //manage : and $ in creation by insertion
933 int *piSourceDims = getDimsArray();
935 for (int i = 0 ; i < iDims ; i++)
947 piMaxDim[i] = piSourceDims[iSource];
948 piCountDim[i] = piSourceDims[iSource];
951 //replace pArg value by the new one
952 pArg[i] = createDoubleVector(piMaxDim[i]);
956 // piMaxDim[i] = piCountDim[i];
961 //remove last dimension at size 1
962 //remove last dimension if are == 1
963 for (int i = (iDims - 1) ; i >= 2 ; i--)
965 if (piMaxDim[i] == 1)
976 if (checkArgValidity(pArg) == false)
979 cleanIndexesArguments(_pArgs, &pArg);
980 //contain bad index, like <= 0, ...
988 pOut = new Sparse(piCountDim[0], 1, bComplex);
993 pOut = new Sparse(1, piCountDim[0], bComplex);
998 pOut = new Sparse(piMaxDim[0], piMaxDim[1], bComplex);
999 //pOut = createEmpty(iDims, piMaxDim, bComplex);
1002 //insert values in new matrix
1003 Sparse* pOut2 = pOut->insert(&pArg, this);
1010 cleanIndexesArguments(_pArgs, &pArg);
1015 Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1017 typedef Sparse* (Sparse::*insert_t)(typed_list*, InternalType*);
1018 Sparse* pIT = checkRef(this, (insert_t)&Sparse::insert, _pArgs, _pSource);
1024 if (_pSource->isSparse())
1026 return insert(_pArgs, _pSource->getAs<Sparse>());
1029 bool bNeedToResize = false;
1030 int iDims = (int)_pArgs->size();
1033 //sparse are only in 2 dims
1045 Double* pSource = _pSource->getAs<Double>();
1047 //evaluate each argument and replace by appropriate value and compute the count of combinations
1048 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
1052 cleanIndexesArguments(_pArgs, &pArg);
1059 if (getRows() == 1 || getCols() == 1)
1062 if (getSize() < piMaxDim[0])
1064 bNeedToResize = true;
1066 //need to enlarge sparse dimensions
1067 if (getCols() == 1 || getSize() == 0)
1070 iNewRows = piMaxDim[0];
1073 else if (getRows() == 1)
1077 iNewCols = piMaxDim[0];
1081 else if (getSize() < piMaxDim[0])
1084 cleanIndexesArguments(_pArgs, &pArg);
1091 if (piMaxDim[0] > getRows() || piMaxDim[1] > getCols())
1093 bNeedToResize = true;
1094 iNewRows = std::max(getRows(), piMaxDim[0]);
1095 iNewCols = std::max(getCols(), piMaxDim[1]);
1099 //check number of insertion
1100 if (pSource->isScalar() == false && pSource->getSize() != iSeqCount)
1103 cleanIndexesArguments(_pArgs, &pArg);
1107 //now you are sure to be able to insert values
1110 if (resize(iNewRows, iNewCols) == false)
1113 cleanIndexesArguments(_pArgs, &pArg);
1119 if (pSource->isComplex() && isComplex() == false)
1127 double* pIdx = pArg[0]->getAs<Double>()->get();
1128 for (int i = 0 ; i < iSeqCount ; i++)
1130 int iRow = static_cast<int>(pIdx[i] - 1) % getRows();
1131 int iCol = static_cast<int>(pIdx[i] - 1) / getRows();
1132 if (pSource->isScalar())
1134 if (pSource->isComplex())
1136 set(iRow, iCol, std::complex<double>(pSource->get(0), pSource->getImg(0)), false);
1140 set(iRow, iCol, pSource->get(0), false);
1145 if (pSource->isComplex())
1147 set(iRow, iCol, std::complex<double>(pSource->get(i), pSource->getImg(i)), false);
1151 set(iRow, iCol, pSource->get(i), false);
1158 double* pIdxRow = pArg[0]->getAs<Double>()->get();
1159 int iRowSize = pArg[0]->getAs<Double>()->getSize();
1160 double* pIdxCol = pArg[1]->getAs<Double>()->get();
1162 for (int i = 0 ; i < iSeqCount ; i++)
1164 if (pSource->isScalar())
1166 if (pSource->isComplex())
1168 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, std::complex<double>(pSource->get(0), pSource->getImg(0)), false);
1172 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, pSource->get(0), false);
1177 int iRowOrig = i % pSource->getRows();
1178 int iColOrig = i / pSource->getRows();
1180 if (pSource->isComplex())
1182 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, std::complex<double>(pSource->get(iRowOrig, iColOrig), pSource->getImg(iRowOrig, iColOrig)), false);
1186 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, pSource->get(iRowOrig, iColOrig), false);
1195 cleanIndexesArguments(_pArgs, &pArg);
1200 Sparse* Sparse::insert(typed_list* _pArgs, Sparse* _pSource)
1202 bool bNeedToResize = false;
1203 int iDims = (int)_pArgs->size();
1206 //sparse are only in 2 dims
1219 //evaluate each argument and replace by appropriate value and compute the count of combinations
1220 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
1224 cleanIndexesArguments(_pArgs, &pArg);
1231 if (getRows() == 1 || getCols() == 1)
1234 bNeedToResize = true;
1235 if (getSize() < piMaxDim[0])
1237 //need to enlarge sparse dimensions
1238 if (getCols() == 1 || getSize() == 0)
1241 iNewRows = piMaxDim[0];
1244 else if (getRows() == 1)
1248 iNewCols = piMaxDim[0];
1252 else if (getSize() < piMaxDim[0])
1255 cleanIndexesArguments(_pArgs, &pArg);
1262 if (piMaxDim[0] > getRows() || piMaxDim[1] > getCols())
1264 bNeedToResize = true;
1265 iNewRows = std::max(getRows(), piMaxDim[0]);
1266 iNewCols = std::max(getCols(), piMaxDim[1]);
1270 //check number of insertion
1271 if (_pSource->isScalar() == false && _pSource->getSize() != iSeqCount)
1274 cleanIndexesArguments(_pArgs, &pArg);
1278 //now you are sure to be able to insert values
1281 if (resize(iNewRows, iNewCols) == false)
1284 cleanIndexesArguments(_pArgs, &pArg);
1290 if (_pSource->isComplex() && isComplex() == false)
1297 double* pIdx = pArg[0]->getAs<Double>()->get();
1298 for (int i = 0 ; i < iSeqCount ; i++)
1300 int iRow = static_cast<int>(pIdx[i] - 1) % getRows();
1301 int iCol = static_cast<int>(pIdx[i] - 1) / getRows();
1303 if (_pSource->isScalar())
1305 if (_pSource->isComplex())
1307 set(iRow, iCol, _pSource->getImg(0, 0), false);
1311 set(iRow, iCol, _pSource->get(0, 0), false);
1316 int iRowOrig = i % _pSource->getRows();
1317 int iColOrig = i / _pSource->getRows();
1318 if (_pSource->isComplex())
1320 set(iRow, iCol, _pSource->getImg(iRowOrig, iColOrig), false);
1324 set(iRow, iCol, _pSource->get(iRowOrig, iColOrig), false);
1331 double* pIdxRow = pArg[0]->getAs<Double>()->get();
1332 int iRowSize = pArg[0]->getAs<Double>()->getSize();
1333 double* pIdxCol = pArg[1]->getAs<Double>()->get();
1335 for (int i = 0 ; i < iSeqCount ; i++)
1337 if (_pSource->isScalar())
1339 if (_pSource->isComplex())
1341 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->getImg(0, 0), false);
1345 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->get(0, 0), false);
1350 int iRowOrig = i % _pSource->getRows();
1351 int iColOrig = i / _pSource->getRows();
1352 if (_pSource->isComplex())
1354 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->getImg(iRowOrig, iColOrig), false);
1358 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->get(iRowOrig, iColOrig), false);
1367 cleanIndexesArguments(_pArgs, &pArg);
1372 GenericType* Sparse::remove(typed_list* _pArgs)
1374 Sparse* pOut = NULL;
1375 int iDims = (int)_pArgs->size();
1378 //sparse are only in 2 dims
1387 //evaluate each argument and replace by appropriate value and compute the count of combinations
1388 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
1392 cleanIndexesArguments(_pArgs, &pArg);
1396 bool* pbFull = new bool[iDims];
1397 //coord must represent all values on a dimension
1398 for (int i = 0 ; i < iDims ; i++)
1401 int iDimToCheck = getVarMaxDim(i, iDims);
1402 int iIndexSize = pArg[i]->getAs<GenericType>()->getSize();
1404 //we can have index more than once
1405 if (iIndexSize >= iDimToCheck)
1407 //size is good, now check datas
1408 double* pIndexes = getDoubleArrayFromDouble(pArg[i]);
1409 for (int j = 0 ; j < iDimToCheck ; j++)
1412 for (int k = 0 ; k < iIndexSize ; k++)
1414 if ((int)pIndexes[k] == j + 1)
1425 //only one dims can be not full/entire
1426 bool bNotEntire = false;
1428 bool bTooMuchNotEntire = false;
1429 for (int i = 0 ; i < iDims ; i++)
1431 if (pbFull[i] == false)
1433 if (bNotEntire == false)
1440 bTooMuchNotEntire = true;
1448 if (bTooMuchNotEntire == true)
1451 cleanIndexesArguments(_pArgs, &pArg);
1455 //find index to keep
1456 int iNotEntireSize = pArg[iNotEntire]->getAs<GenericType>()->getSize();
1457 double* piNotEntireIndex = getDoubleArrayFromDouble(pArg[iNotEntire]);
1458 int iKeepSize = getVarMaxDim(iNotEntire, iDims);
1459 bool* pbKeep = new bool[iKeepSize];
1461 //fill pbKeep with true value
1462 for (int i = 0 ; i < iKeepSize ; i++)
1467 for (int i = 0 ; i < iNotEntireSize ; i++)
1469 int idx = (int)piNotEntireIndex[i] - 1;
1471 //don't care of value out of bounds
1472 if (idx < iKeepSize)
1474 pbKeep[idx] = false;
1478 int iNewDimSize = 0;
1479 for (int i = 0 ; i < iKeepSize ; i++)
1481 if (pbKeep[i] == true)
1488 int* piNewDims = new int[iDims];
1489 for (int i = 0 ; i < iDims ; i++)
1491 if (i == iNotEntire)
1493 piNewDims[i] = iNewDimSize;
1497 piNewDims[i] = getVarMaxDim(i, iDims);
1501 //remove last dimension if are == 1
1502 int iOrigDims = iDims;
1503 for (int i = (iDims - 1) ; i >= 2 ; i--)
1505 if (piNewDims[i] == 1)
1517 if (iNewDimSize == 0)
1521 cleanIndexesArguments(_pArgs, &pArg);
1522 return new Sparse(0, 0);
1526 //two cases, depends of original matrix/vector
1527 if ((*_pArgs)[0]->isColon() == false && m_iDims == 2 && m_piDims[0] == 1 && m_piDims[1] != 1)
1529 //special case for row vector
1530 pOut = new Sparse(1, iNewDimSize, isComplex());
1531 //in this case we have to care of 2nd dimension
1536 pOut = new Sparse(iNewDimSize, 1, isComplex());
1542 pOut = new Sparse(piNewDims[0], piNewDims[0], isComplex());
1546 //find a way to copy existing data to new variable ...
1548 int* piIndexes = new int[iOrigDims];
1549 int* piViewDims = new int[iOrigDims];
1550 for (int i = 0 ; i < iOrigDims ; i++)
1552 piViewDims[i] = getVarMaxDim(i, iOrigDims);
1555 for (int i = 0 ; i < getSize() ; i++)
1557 bool bByPass = false;
1558 getIndexesWithDims(i, piIndexes, piViewDims, iOrigDims);
1560 //check if piIndexes use removed indexes
1561 for (int j = 0 ; j < iNotEntireSize ; j++)
1563 if ((piNotEntireIndex[j] - 1) == piIndexes[iNotEntire])
1565 //by pass this value
1571 if (bByPass == false)
1576 pOut->set(iNewPos, getImg(i));
1580 pOut->set(iNewPos, get(i));
1586 //free allocated data
1587 for (int i = 0 ; i < iDims ; i++)
1589 if (pArg[i] != (*_pArgs)[i])
1596 delete[] piViewDims;
1599 cleanIndexesArguments(_pArgs, &pArg);
1604 Sparse* Sparse::append(int r, int c, types::Sparse SPARSE_CONST* src)
1606 Sparse* pIT = checkRef(this, &Sparse::append, r, c, src);
1612 // std::wcerr << L"to a sparse of size"<<getRows() << L","<<getCols() << L" should append @"<<r << L","<<c<< "a sparse:"<< src->toString(32,80)<<std::endl;
1613 if (src->isComplex())
1619 if (src->isComplex())
1621 doAppend(*(src->matrixCplx), r, c, *matrixCplx);
1625 doAppend(*(src->matrixReal), r, c, *matrixCplx);
1630 doAppend(*(src->matrixReal), r, c, *matrixReal);
1635 return this; // realloc is meaningless for sparse matrices
1639 * create a new Sparse of dims according to resSize and fill it from currentSparse (along coords)
1641 GenericType* Sparse::extract(typed_list* _pArgs)
1643 Sparse* pOut = NULL;
1644 int iDims = (int)_pArgs->size();
1647 int* piMaxDim = new int[iDims];
1648 int* piCountDim = new int[iDims];
1650 //evaluate each argument and replace by appropriate value and compute the count of combinations
1651 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
1655 cleanIndexesArguments(_pArgs, &pArg);
1656 if (_pArgs->size() == 0)
1660 delete[] piCountDim;
1662 cleanIndexesArguments(_pArgs, &pArg);
1669 delete[] piCountDim;
1671 cleanIndexesArguments(_pArgs, &pArg);
1672 return Double::Empty();
1678 if (piMaxDim[0] <= getSize())
1683 if (getRows() == 1 && getCols() != 1 && (*_pArgs)[0]->isColon() == false)
1685 //special case for row vector
1687 iNewCols = piCountDim[0];
1691 iNewRows = piCountDim[0];
1695 pOut = new Sparse(iNewRows, iNewCols, isComplex());
1696 double* pIdx = pArg[0]->getAs<Double>()->get();
1697 for (int i = 0 ; i < iSeqCount ; i++)
1705 int iRowRead = static_cast<int>(pIdx[i] - 1) % getRows();
1706 int iColRead = static_cast<int>(pIdx[i] - 1) / getRows();
1708 int iRowWrite = static_cast<int>(i) % iNewRows;
1709 int iColWrite = static_cast<int>(i) / iNewRows;
1712 std::complex<double> dbl = getImg(iRowRead, iColRead);
1713 if (dbl.real() != 0 || dbl.imag() != 0)
1715 //only non zero values
1716 pOut->set(iRowWrite, iColWrite, dbl, false);
1721 double dbl = get(iRowRead, iColRead);
1724 //only non zero values
1725 pOut->set(iRowWrite, iColWrite, dbl, false);
1733 delete[] piCountDim;
1735 cleanIndexesArguments(_pArgs, &pArg);
1741 if (piMaxDim[0] <= getRows() && piMaxDim[1] <= getCols())
1743 double* pIdxRow = pArg[0]->getAs<Double>()->get();
1744 double* pIdxCol = pArg[1]->getAs<Double>()->get();
1746 int iNewRows = pArg[0]->getAs<Double>()->getSize();
1747 int iNewCols = pArg[1]->getAs<Double>()->getSize();
1749 pOut = new Sparse(iNewRows, iNewCols, isComplex());
1752 for (int iRow = 0 ; iRow < iNewRows ; iRow++)
1754 for (int iCol = 0 ; iCol < iNewCols ; iCol++)
1756 if ((pIdxRow[iRow] < 1) || (pIdxCol[iCol] < 1))
1761 delete[] piCountDim;
1763 cleanIndexesArguments(_pArgs, &pArg);
1768 std::complex<double> dbl = getImg((int)pIdxRow[iRow] - 1, (int)pIdxCol[iCol] - 1);
1769 if (dbl.real() != 0 || dbl.imag() != 0)
1771 //only non zero values
1772 pOut->set(iRow, iCol, dbl, false);
1777 double dbl = get((int)pIdxRow[iRow] - 1, (int)pIdxCol[iCol] - 1);
1780 //only non zero values
1781 pOut->set(iRow, iCol, dbl, false);
1791 delete[] piCountDim;
1793 cleanIndexesArguments(_pArgs, &pArg);
1801 delete[] piCountDim;
1803 cleanIndexesArguments(_pArgs, &pArg);
1808 Sparse* Sparse::extract(int nbCoords, int SPARSE_CONST* coords, int SPARSE_CONST* maxCoords, int SPARSE_CONST* resSize, bool asVector) SPARSE_CONST
1810 if ( (asVector && maxCoords[0] > getSize()) ||
1811 (asVector == false && maxCoords[0] > getRows()) ||
1812 (asVector == false && maxCoords[1] > getCols()))
1817 bool const cplx(isComplex());
1821 pSp = (getRows() == 1) ? new Sparse(1, resSize[0], cplx) : new Sparse(resSize[0], 1, cplx);
1825 pSp = new Sparse(resSize[0], resSize[1], cplx);
1827 // std::cerr<<"extracted sparse:"<<pSp->getRows()<<", "<<pSp->getCols()<<"seqCount="<<nbCoords<<"maxDim="<<maxCoords[0] <<","<< maxCoords[1]<<std::endl;
1829 ? copyToSparse(*this, Coords<true>(coords, getRows()), nbCoords
1830 , *pSp, RowWiseFullIterator(pSp->getRows(), pSp->getCols()))
1831 : copyToSparse(*this, Coords<false>(coords), nbCoords
1832 , *pSp, RowWiseFullIterator(pSp->getRows(), pSp->getCols()))))
1840 bool Sparse::invoke(typed_list & in, optional_list & /*opt*/, int /*_iRetCount*/, typed_list & out, const ast::Exp & e)
1844 out.push_back(this);
1848 InternalType * _out = extract(&in);
1851 std::wostringstream os;
1852 os << _W("Invalid index.\n");
1853 throw ast::InternalError(os.str(), 999, e.getLocation());
1855 out.push_back(_out);
1862 bool Sparse::isInvokable() const
1867 bool Sparse::hasInvokeOption() const
1872 int Sparse::getInvokeNbIn()
1877 int Sparse::getInvokeNbOut()
1883 coords are Scilab 1-based
1884 extract std::make_pair(coords, asVector), rowIter
1886 template<typename Src, typename SrcTraversal, typename Sz, typename DestTraversal>
1887 bool Sparse::copyToSparse(Src SPARSE_CONST& src, SrcTraversal srcTrav, Sz n, Sparse& sp, DestTraversal destTrav)
1889 if (!(src.isComplex() || sp.isComplex()))
1891 mycopy_n(makeMatrixIterator<double>(src, srcTrav), n
1892 , makeMatrixIterator<double>(*sp.matrixReal, destTrav));
1897 mycopy_n(makeMatrixIterator<std::complex<double> >(src, srcTrav), n
1898 , makeMatrixIterator<std::complex<double> >(*sp.matrixCplx, destTrav));
1905 // GenericType because we might return a Double* for scalar operand
1906 Sparse* Sparse::add(Sparse const& o) const
1908 RealSparse_t* realSp(0);
1909 CplxSparse_t* cplxSp(0);
1910 if (isComplex() == false && o.isComplex() == false)
1913 realSp = new RealSparse_t(*matrixReal + * (o.matrixReal));
1915 else if (isComplex() == false && o.isComplex() == true)
1917 cplxSp = new CplxSparse_t(matrixReal->cast<std::complex<double> >() + * (o.matrixCplx));
1919 else if (isComplex() == true && o.isComplex() == false)
1921 cplxSp = new CplxSparse_t(*matrixCplx + o.matrixReal->cast<std::complex<double> >());
1923 else if (isComplex() == true && o.isComplex() == true)
1925 cplxSp = new CplxSparse_t(*matrixCplx + * (o.matrixCplx));
1928 return new Sparse(realSp, cplxSp);
1931 Sparse* Sparse::substract(Sparse const& o) const
1933 RealSparse_t* realSp(0);
1934 CplxSparse_t* cplxSp(0);
1935 if (isComplex() == false && o.isComplex() == false)
1938 realSp = new RealSparse_t(*matrixReal - * (o.matrixReal));
1940 else if (isComplex() == false && o.isComplex() == true)
1943 cplxSp = new CplxSparse_t(matrixReal->cast<std::complex<double> >() - * (o.matrixCplx));
1945 else if (isComplex() == true && o.isComplex() == false)
1948 cplxSp = new CplxSparse_t(*matrixCplx - o.matrixReal->cast<std::complex<double> >());
1950 else if (isComplex() == true && o.isComplex() == true)
1953 cplxSp = new CplxSparse_t(*matrixCplx - * (o.matrixCplx));
1956 return new Sparse(realSp, cplxSp);
1959 Sparse* Sparse::multiply(double s) const
1961 return new Sparse( isComplex() ? 0 : new RealSparse_t((*matrixReal)*s)
1962 , isComplex() ? new CplxSparse_t((*matrixCplx)*s) : 0);
1965 Sparse* Sparse::multiply(std::complex<double> s) const
1967 return new Sparse( 0
1968 , isComplex() ? new CplxSparse_t((*matrixCplx) * s) : new CplxSparse_t((*matrixReal) * s));
1971 Sparse* Sparse::multiply(Sparse const& o) const
1973 RealSparse_t* realSp(0);
1974 CplxSparse_t* cplxSp(0);
1976 if (isComplex() == false && o.isComplex() == false)
1978 realSp = new RealSparse_t(*matrixReal **(o.matrixReal));
1980 else if (isComplex() == false && o.isComplex() == true)
1982 cplxSp = new CplxSparse_t(matrixReal->cast<std::complex<double> >() **(o.matrixCplx));
1984 else if (isComplex() == true && o.isComplex() == false)
1986 cplxSp = new CplxSparse_t(*matrixCplx * o.matrixReal->cast<std::complex<double> >());
1988 else if (isComplex() == true && o.isComplex() == true)
1990 cplxSp = new CplxSparse_t(*matrixCplx **(o.matrixCplx));
1993 return new Sparse(realSp, cplxSp);
1996 Sparse* Sparse::dotMultiply(Sparse SPARSE_CONST& o) const
1998 RealSparse_t* realSp(0);
1999 CplxSparse_t* cplxSp(0);
2000 if (isComplex() == false && o.isComplex() == false)
2002 realSp = new RealSparse_t(matrixReal->cwiseProduct(*(o.matrixReal)));
2004 else if (isComplex() == false && o.isComplex() == true)
2006 cplxSp = new CplxSparse_t(matrixReal->cast<std::complex<double> >().cwiseProduct( *(o.matrixCplx)));
2008 else if (isComplex() == true && o.isComplex() == false)
2010 cplxSp = new CplxSparse_t(matrixCplx->cwiseProduct(o.matrixReal->cast<std::complex<double> >()));
2012 else if (isComplex() == true && o.isComplex() == true)
2014 cplxSp = new CplxSparse_t(matrixCplx->cwiseProduct(*(o.matrixCplx)));
2017 return new Sparse(realSp, cplxSp);
2020 Sparse* Sparse::dotDivide(Sparse SPARSE_CONST& o) const
2022 RealSparse_t* realSp(0);
2023 CplxSparse_t* cplxSp(0);
2024 if (isComplex() == false && o.isComplex() == false)
2026 realSp = new RealSparse_t(matrixReal->cwiseQuotient(*(o.matrixReal)));
2028 else if (isComplex() == false && o.isComplex() == true)
2030 cplxSp = new CplxSparse_t(matrixReal->cast<std::complex<double> >().cwiseQuotient( *(o.matrixCplx)));
2032 else if (isComplex() == true && o.isComplex() == false)
2034 cplxSp = new CplxSparse_t(matrixCplx->cwiseQuotient(o.matrixReal->cast<std::complex<double> >()));
2036 else if (isComplex() == true && o.isComplex() == true)
2038 cplxSp = new CplxSparse_t(matrixCplx->cwiseQuotient(*(o.matrixCplx)));
2041 return new Sparse(realSp, cplxSp);
2044 int Sparse::newCholLLT(Sparse** _SpPermut, Sparse** _SpFactor) const
2046 typedef Eigen::SparseMatrix<double, Eigen::ColMajor> RealSparseCol_t;
2047 RealSparseCol_t spColMajor = RealSparseCol_t((const RealSparse_t&) * matrixReal);
2049 // Constructs and performs the LLT factorization of sparse
2050 Eigen::SimplicialLLT<RealSparseCol_t> pLLT(spColMajor);
2051 int iInfo = pLLT.info();
2052 if (iInfo != Eigen::Success)
2059 // Get the lower matrix of factorization.
2060 // The new RealSparse_t will be setted in Sparse without copy.
2061 *_SpFactor = new Sparse(new RealSparse_t(pLLT.matrixL()), NULL);
2063 // Get the permutation matrix.
2064 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic, int> p = pLLT.permutationP();
2065 *_SpPermut = new Sparse(p.rows(), p.cols());
2066 for (int i = 0; i < p.rows(); i++)
2068 (*_SpPermut)->set(i, p.indices()[i], 1, false);
2071 (*_SpPermut)->finalize();
2076 bool Sparse::transpose(InternalType *& out)
2078 out = new Sparse(matrixReal ? new RealSparse_t(matrixReal->transpose()) : 0, matrixCplx ? new CplxSparse_t(matrixCplx->transpose()) : 0);
2082 bool Sparse::adjoint(InternalType *& out)
2084 out = new Sparse(matrixReal ? new RealSparse_t(matrixReal->adjoint()) : 0, matrixCplx ? new CplxSparse_t(matrixCplx->adjoint()) : 0);
2090 BoolCast(std::complex<double> const& c): b(c.real() || c.imag()) {}
2091 operator bool () const
2095 operator double() const
2101 Sparse* Sparse::newOnes() const
2103 // result is never cplx
2104 return new Sparse( matrixReal
2105 ? new RealSparse_t(matrixReal->cast<bool>().cast<double>())
2106 : new RealSparse_t(matrixCplx->cast<BoolCast>().cast<double>())
2112 RealCast(std::complex<double> const& c): b(c.real()) {}
2113 operator bool () const
2117 operator double() const
2123 Sparse* Sparse::newReal() const
2125 return new Sparse( matrixReal
2127 : new RealSparse_t(matrixCplx->cast<RealCast>().cast<double>())
2131 std::size_t Sparse::nonZeros() const
2135 return matrixCplx->nonZeros();
2139 return matrixReal->nonZeros();
2142 std::size_t Sparse::nonZeros(std::size_t r) const
2147 int* piIndex = matrixReal->outerIndexPtr();
2148 res = piIndex[r + 1] - piIndex[r];
2152 int* piIndex = matrixCplx->outerIndexPtr();
2153 res = piIndex[r + 1] - piIndex[r];
2159 int* Sparse::getNbItemByRow(int* _piNbItemByRows)
2161 int* piNbItemByCols = new int[getRows() + 1];
2164 mycopy_n(matrixCplx->outerIndexPtr(), getRows() + 1, piNbItemByCols);
2168 mycopy_n(matrixReal->outerIndexPtr(), getRows() + 1, piNbItemByCols);
2171 for (int i = 0 ; i < getRows() ; i++)
2173 _piNbItemByRows[i] = piNbItemByCols[i + 1] - piNbItemByCols[i];
2176 delete[] piNbItemByCols;
2177 return _piNbItemByRows;
2180 int* Sparse::getColPos(int* _piColPos)
2184 mycopy_n(matrixCplx->innerIndexPtr(), nonZeros(), _piColPos);
2188 mycopy_n(matrixReal->innerIndexPtr(), nonZeros(), _piColPos);
2191 for (int i = 0; i < nonZeros(); i++)
2199 int* Sparse::getInnerPtr(int* count)
2204 ret = matrixCplx->innerIndexPtr();
2205 *count = matrixCplx->innerSize();
2209 ret = matrixReal->innerIndexPtr();
2210 *count = matrixReal->innerSize();
2216 int* Sparse::getOuterPtr(int* count)
2221 ret = matrixCplx->outerIndexPtr();
2222 *count = matrixCplx->outerSize();
2226 ret = matrixReal->outerIndexPtr();
2227 *count = matrixReal->outerSize();
2235 template<typename S> struct GetReal: std::unary_function<typename S::InnerIterator, double>
2237 double operator()(typename S::InnerIterator it) const
2242 template<> struct GetReal< Eigen::SparseMatrix<std::complex<double >, Eigen::RowMajor > >
2243 : std::unary_function<Sparse::CplxSparse_t::InnerIterator, double>
2245 double operator()( Sparse::CplxSparse_t::InnerIterator it) const
2247 return it.value().real();
2250 template<typename S> struct GetImag: std::unary_function<typename S::InnerIterator, double>
2252 double operator()(typename S::InnerIterator it) const
2254 return it.value().imag();
2257 template<typename S> struct GetRow: std::unary_function<typename S::InnerIterator, int>
2259 int operator()(typename S::InnerIterator it) const
2261 return it.row() + 1;
2264 template<typename S> struct GetCol: std::unary_function<typename S::InnerIterator, int>
2266 int operator()(typename S::InnerIterator it) const
2268 return it.col() + 1;
2272 template<typename S, typename Out, typename F> Out sparseTransform(S& s, Out o, F f)
2274 for (std::size_t k(0); k < s.outerSize(); ++k)
2276 for (typename S::InnerIterator it(s, (int)k); it; ++it, ++o)
2285 std::pair<double*, double*> Sparse::outputValues(double* outReal, double* outImag)const
2288 ? std::make_pair(sparseTransform(*matrixReal, outReal, GetReal<RealSparse_t>()), outImag)
2289 : std::make_pair(sparseTransform(*matrixCplx, outReal, GetReal<CplxSparse_t>())
2290 , sparseTransform(*matrixCplx, outImag, GetImag<CplxSparse_t>()));
2293 int* Sparse::outputRowCol(int* out)const
2296 ? sparseTransform(*matrixReal, sparseTransform(*matrixReal, out, GetRow<RealSparse_t>()), GetCol<RealSparse_t>())
2297 : sparseTransform(*matrixCplx, sparseTransform(*matrixCplx, out, GetRow<CplxSparse_t>()), GetCol<CplxSparse_t>());
2299 double* Sparse::outputCols(double* out) const
2303 mycopy_n(matrixCplx->innerIndexPtr(), nonZeros(), out);
2307 mycopy_n(matrixReal->innerIndexPtr(), nonZeros(), out);
2310 return std::transform(out, out, out, std::bind2nd(std::plus<double>(), 1));
2314 void Sparse::opposite(void)
2318 std::complex<double>* data = matrixCplx->valuePtr();
2319 std::transform(data, data + matrixCplx->nonZeros(), data, std::negate<std::complex<double> >());
2323 double* data = matrixReal->valuePtr();
2324 std::transform(data, data + matrixReal->nonZeros(), data, std::negate<double>());
2328 SparseBool* Sparse::newLessThan(Sparse &o)
2330 //only real values !
2332 //return cwiseOp<std::less>(*this, o);
2333 int rowL = getRows();
2334 int colL = getCols();
2336 int rowR = o.getRows();
2337 int colR = o.getCols();
2338 int row = std::max(rowL, rowR);
2339 int col = std::max(colL, colR);
2341 //create a boolean sparse matrix with dims of sparses
2342 types::SparseBool* ret = new types::SparseBool(row, col);
2343 if (isScalar() && o.isScalar())
2345 double l = get(0, 0);
2346 double r = o.get(0, 0);
2347 ret->set(0, 0, l < r, false);
2349 else if (isScalar())
2351 int nnzR = static_cast<int>(o.nonZeros());
2352 std::vector<int> rowcolR(nnzR * 2, 0);
2353 o.outputRowCol(rowcolR.data());
2355 //compare all items of R with R[0]
2356 double l = get(0, 0);
2360 ret->setTrue(false);
2363 for (int i = 0; i < nnzR; ++i)
2365 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2366 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l < r, false);
2369 else if (o.isScalar())
2371 int nnzL = static_cast<int>(nonZeros());
2372 std::vector<int> rowcolL(nnzL * 2, 0);
2373 outputRowCol(rowcolL.data());
2375 double r = o.get(0, 0);
2381 for (int i = 0; i < nnzL; ++i)
2383 double l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2384 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l < r, false);
2389 int nnzR = static_cast<int>(o.nonZeros());
2390 std::vector<int> rowcolR(nnzR * 2, 0);
2391 o.outputRowCol(rowcolR.data());
2392 int nnzL = static_cast<int>(nonZeros());
2393 std::vector<int> rowcolL(nnzL * 2, 0);
2394 outputRowCol(rowcolL.data());
2395 //set all values to %t
2396 ret->setFalse(false);
2398 for (int i = 0; i < nnzL; ++i)
2400 double l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2401 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l < 0, false);
2405 //set _pR[i] == _pL[i] for each _pR values
2406 for (int i = 0; i < nnzR; ++i)
2408 //get l and r following non zeros value of R
2409 double l = get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2410 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2411 //set value following non zeros value of R
2412 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l < r, false);
2420 SparseBool* Sparse::newNotEqualTo(Sparse const&o) const
2422 return cwiseOp<std::not_equal_to>(*this, o);
2425 SparseBool* Sparse::newLessOrEqual(Sparse &o)
2427 //only real values !
2429 //return cwiseOp<std::less>(*this, o);
2430 int rowL = getRows();
2431 int colL = getCols();
2433 int rowR = o.getRows();
2434 int colR = o.getCols();
2435 int row = std::max(rowL, rowR);
2436 int col = std::max(colL, colR);
2438 //create a boolean sparse matrix with dims of sparses
2439 types::SparseBool* ret = new types::SparseBool(row, col);
2440 if (isScalar() && o.isScalar())
2442 double l = get(0, 0);
2443 double r = o.get(0, 0);
2444 ret->set(0, 0, l <= r, false);
2446 else if (isScalar())
2448 int nnzR = static_cast<int>(o.nonZeros());
2449 std::vector<int> rowcolR(nnzR * 2, 0);
2450 o.outputRowCol(rowcolR.data());
2452 //compare all items of R with R[0]
2453 double l = get(0, 0);
2457 ret->setTrue(false);
2460 for (int i = 0; i < nnzR; ++i)
2462 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2463 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l <= r, false);
2466 else if (o.isScalar())
2468 int nnzL = static_cast<int>(nonZeros());
2469 std::vector<int> rowcolL(nnzL * 2, 0);
2470 outputRowCol(rowcolL.data());
2472 double r = o.get(0, 0);
2478 for (int i = 0; i < nnzL; ++i)
2480 double l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2481 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l <= r, false);
2486 int nnzR = static_cast<int>(o.nonZeros());
2487 std::vector<int> rowcolR(nnzR * 2, 0);
2488 o.outputRowCol(rowcolR.data());
2489 int nnzL = static_cast<int>(nonZeros());
2490 std::vector<int> rowcolL(nnzL * 2, 0);
2491 outputRowCol(rowcolL.data());
2492 //set all values to %t
2493 ret->setTrue(false);
2495 for (int i = 0; i < nnzL; ++i)
2497 double l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2498 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l <= 0, false);
2502 //set _pR[i] == _pL[i] for each _pR values
2503 for (int i = 0; i < nnzR; ++i)
2505 //get l and r following non zeros value of R
2506 double l = get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2507 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2508 //set value following non zeros value of R
2509 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l <= r, false);
2517 SparseBool* Sparse::newEqualTo(Sparse &o)
2519 int rowL = getRows();
2520 int colL = getCols();
2522 int rowR = o.getRows();
2523 int colR = o.getCols();
2524 int row = std::max(rowL, rowR);
2525 int col = std::max(colL, colR);
2527 //create a boolean sparse matrix with dims of sparses
2528 types::SparseBool* ret = new types::SparseBool(row, col);
2529 if (isScalar() && o.isScalar())
2531 if (isComplex() || o.isComplex())
2533 std::complex<double> l = getImg(0, 0);
2534 std::complex<double> r = o.getImg(0, 0);
2535 ret->set(0, 0, l == r, false);
2539 double l = get(0, 0);
2540 double r = o.get(0, 0);
2541 ret->set(0, 0, l == r, false);
2544 else if (isScalar())
2546 int nnzR = static_cast<int>(o.nonZeros());
2547 std::vector<int> rowcolR(nnzR * 2, 0);
2548 o.outputRowCol(rowcolR.data());
2550 //compare all items of R with R[0]
2551 if (isComplex() || o.isComplex())
2553 std::complex<double> l = getImg(0, 0);
2554 for (int i = 0; i < nnzR; ++i)
2556 std::complex<double> r = o.getImg(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2557 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
2562 double l = get(0, 0);
2563 for (int i = 0; i < nnzR; ++i)
2565 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2566 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
2570 else if (o.isScalar())
2572 int nnzL = static_cast<int>(nonZeros());
2573 std::vector<int> rowcolL(nnzL * 2, 0);
2574 outputRowCol(rowcolL.data());
2576 if (isComplex() || o.isComplex())
2578 std::complex<double> r = o.getImg(0, 0);
2579 for (int i = 0; i < nnzL; ++i)
2581 std::complex<double> l = getImg(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2582 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l == r, false);
2587 double r = get(0, 0);
2588 for (int i = 0; i < nnzL; ++i)
2590 double l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
2591 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l == r, false);
2597 int nnzR = static_cast<int>(o.nonZeros());
2598 std::vector<int> rowcolR(nnzR * 2, 0);
2599 o.outputRowCol(rowcolR.data());
2600 int nnzL = static_cast<int>(nonZeros());
2601 std::vector<int> rowcolL(nnzL * 2, 0);
2602 outputRowCol(rowcolL.data());
2603 //set all values to %t
2604 ret->setTrue(false);
2605 //set %f in each pL values
2606 for (int i = 0; i < nnzL; ++i)
2608 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, false, false);
2612 //set _pR[i] == _pL[i] for each _pR values
2613 if (isComplex() || o.isComplex())
2615 for (int i = 0; i < nnzR; ++i)
2617 //get l and r following non zeros value of R
2618 std::complex<double> l = getImg(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2619 std::complex<double> r = o.getImg(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2620 //set value following non zeros value of R
2621 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
2626 for (int i = 0; i < nnzR; ++i)
2628 //get l and r following non zeros value of R
2629 double l = get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2630 double r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
2631 //set value following non zeros value of R
2632 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
2641 Sparse* Sparse::reshape(int* _piDims, int _iDims)
2653 pSp = reshape(_piDims[0], iCols);
2659 Sparse* Sparse::reshape(int _iNewRows, int _iNewCols)
2661 typedef Sparse* (Sparse::*reshape_t)(int, int);
2662 Sparse* pIT = checkRef(this, (reshape_t)&Sparse::reshape, _iNewRows, _iNewCols);
2668 if (_iNewRows * _iNewCols != getRows() * getCols())
2679 size_t iNonZeros = nonZeros();
2680 RealSparse_t *newReal = new RealSparse_t(_iNewRows, _iNewCols);
2681 newReal->reserve((int)iNonZeros);
2684 int* pRows = new int[iNonZeros * 2];
2685 outputRowCol(pRows);
2686 int* pCols = pRows + iNonZeros;
2689 double* pNonZeroR = new double[iNonZeros];
2690 double* pNonZeroI = new double[iNonZeros];
2691 outputValues(pNonZeroR, pNonZeroI);
2693 typedef Eigen::Triplet<double> triplet;
2694 std::vector<triplet> tripletList;
2696 for (size_t i = 0 ; i < iNonZeros ; i++)
2698 int iCurrentPos = ((int)pCols[i] - 1) * getRows() + ((int)pRows[i] - 1);
2699 tripletList.push_back(triplet((int)(iCurrentPos % _iNewRows), (int)(iCurrentPos / _iNewRows), pNonZeroR[i]));
2702 newReal->setFromTriplets(tripletList.begin(), tripletList.end());
2705 matrixReal = newReal;
2713 size_t iNonZeros = nonZeros();
2714 CplxSparse_t *newCplx = new CplxSparse_t(_iNewRows, _iNewCols);
2715 newCplx->reserve((int)iNonZeros);
2718 int* pRows = new int[iNonZeros * 2];
2719 outputRowCol(pRows);
2720 int* pCols = pRows + iNonZeros;
2723 double* pNonZeroR = new double[iNonZeros];
2724 double* pNonZeroI = new double[iNonZeros];
2725 outputValues(pNonZeroR, pNonZeroI);
2727 typedef Eigen::Triplet<std::complex<double> > triplet;
2728 std::vector<triplet> tripletList;
2730 for (size_t i = 0 ; i < iNonZeros ; i++)
2732 int iCurrentPos = ((int)pCols[i] - 1) * getRows() + ((int)pRows[i] - 1);
2733 tripletList.push_back(triplet((int)(iCurrentPos % _iNewRows), (int)(iCurrentPos / _iNewRows), std::complex<double>(pNonZeroR[i], pNonZeroI[i])));
2736 newCplx->setFromTriplets(tripletList.begin(), tripletList.end());
2739 matrixCplx = newCplx;
2745 m_iRows = _iNewRows;
2746 m_iCols = _iNewCols;
2747 m_iSize = _iNewRows * _iNewCols;
2750 m_piDims[0] = m_iRows;
2751 m_piDims[1] = m_iCols;
2764 // SparseBool* SparseBool::new
2766 SparseBool::SparseBool(Bool SPARSE_CONST& src)
2769 int size = src.getSize();
2770 int row = src.getRows();
2771 Double* idx = new Double(src.getSize(), 2);
2772 double* p = idx->get();
2773 for (int i = 0; i < size; ++i)
2775 p[i] = (double)(i % row) + 1;
2776 p[i + size] = (double)(i / row) + 1;
2778 create2(src.getRows(), src.getCols(), src, *idx);
2781 Inspector::addItem(this);
2784 /* @param src : Bool matrix to copy into a new sparse matrix
2785 @param idx : Double matrix to use as indexes to get values from the src
2787 SparseBool::SparseBool(Bool SPARSE_CONST& src, Double SPARSE_CONST& idx)
2789 int idxrow = idx.getRows();
2790 int rows = static_cast<int>(*std::max_element(idx.get(), idx.get() + idxrow));
2791 int cols = static_cast<int>(*std::max_element(idx.get() + idxrow, idx.get() + idxrow * 2));
2792 create2(rows, cols, src, idx);
2794 Inspector::addItem(this);
2798 /* @param src : Bool matrix to copy into a new sparse matrix
2799 @param idx : Double matrix to use as indexes to get values from the src
2800 @param dims : Double matrix containing the dimensions of the new matrix
2802 SparseBool::SparseBool(Bool SPARSE_CONST& src, Double SPARSE_CONST& idx, Double SPARSE_CONST& dims)
2804 create2(static_cast<int>(dims.get(0)), static_cast<int>(dims.get(1)), src, idx);
2806 Inspector::addItem(this);
2810 SparseBool::SparseBool(int _iRows, int _iCols) : matrixBool(new BoolSparse_t(_iRows, _iCols))
2814 m_iSize = _iRows * _iCols;
2816 m_piDims[0] = _iRows;
2817 m_piDims[1] = _iCols;
2819 Inspector::addItem(this);
2823 SparseBool::SparseBool(SparseBool const& src) : matrixBool(new BoolSparse_t(*src.matrixBool))
2826 m_iRows = const_cast<SparseBool*>(&src)->getRows();
2827 m_iCols = const_cast<SparseBool*>(&src)->getCols();
2828 m_iSize = m_iRows * m_iCols;
2829 m_piDims[0] = m_iRows;
2830 m_piDims[1] = m_iCols;
2832 Inspector::addItem(this);
2836 SparseBool::SparseBool(BoolSparse_t* src) : matrixBool(src)
2838 m_iRows = src->rows();
2839 m_iCols = src->cols();
2840 m_iSize = m_iRows * m_iCols;
2842 m_piDims[0] = m_iRows;
2843 m_piDims[1] = m_iCols;
2845 Inspector::addItem(this);
2849 SparseBool::SparseBool(int rows, int cols, int trues, int* inner, int* outer)
2854 matrixBool = new BoolSparse_t(rows, cols);
2855 matrixBool->reserve((int)trues);
2856 out = matrixBool->outerIndexPtr();
2857 in = matrixBool->innerIndexPtr();
2859 //update outerIndexPtr
2860 memcpy(out, outer, sizeof(int) * (rows + 1));
2861 //update innerIndexPtr
2862 memcpy(in, inner, sizeof(int) * trues);
2864 bool* data = matrixBool->valuePtr();
2865 for (int i = 0; i < trues; ++i)
2872 m_iSize = cols * rows;
2874 m_piDims[0] = m_iRows;
2875 m_piDims[1] = m_iCols;
2877 matrixBool->resizeNonZeros(trues);
2880 void SparseBool::create2(int rows, int cols, Bool SPARSE_CONST& src, Double SPARSE_CONST& idx)
2882 int nnz = src.getSize();
2883 double* i = idx.get();
2884 double* j = i + idx.getRows();
2885 int* val = src.get();
2887 typedef Eigen::Triplet<bool> T;
2888 std::vector<T> tripletList;
2889 tripletList.reserve((int)nnz);
2891 for (int k = 0; k < nnz; ++k)
2893 tripletList.push_back(T(static_cast<int>(i[k]) - 1, static_cast<int>(j[k]) - 1, val[k] == 1));
2896 matrixBool = new BoolSparse_t(rows, cols);
2897 matrixBool->setFromTriplets(tripletList.begin(), tripletList.end());
2899 m_iRows = matrixBool->rows();
2900 m_iCols = matrixBool->cols();
2901 m_iSize = cols * rows;
2903 m_piDims[0] = m_iRows;
2904 m_piDims[1] = m_iCols;
2908 SparseBool::~SparseBool()
2912 Inspector::removeItem(this);
2916 bool SparseBool::toString(std::wostringstream& ostr)
2918 ostr << ::toString(*matrixBool, 0);
2922 void SparseBool::whoAmI() SPARSE_CONST
2924 std::cout << "types::SparseBool";
2927 SparseBool* SparseBool::clone(void)
2929 return new SparseBool(*this);
2932 SparseBool* SparseBool::resize(int _iNewRows, int _iNewCols)
2934 typedef SparseBool* (SparseBool::*resize_t)(int, int);
2935 SparseBool* pIT = checkRef(this, (resize_t)&SparseBool::resize, _iNewRows, _iNewCols);
2941 if (_iNewRows <= getRows() && _iNewCols <= getCols())
2943 //nothing to do: hence we do NOT fail
2947 SparseBool* res = NULL;
2951 size_t iNonZeros = nbTrue();
2953 BoolSparse_t *newBool = new BoolSparse_t(_iNewRows, _iNewCols);
2954 newBool->reserve((int)iNonZeros);
2957 int* pRows = new int[iNonZeros * 2];
2958 outputRowCol(pRows);
2959 int* pCols = pRows + iNonZeros;
2961 typedef Eigen::Triplet<bool> triplet;
2962 std::vector<triplet> tripletList;
2964 for (size_t i = 0 ; i < iNonZeros ; i++)
2966 tripletList.push_back(triplet((int)pRows[i] - 1, (int)pCols[i] - 1, true));
2969 newBool->setFromTriplets(tripletList.begin(), tripletList.end());
2972 matrixBool = newBool;
2975 m_iRows = _iNewRows;
2976 m_iCols = _iNewCols;
2977 m_iSize = _iNewRows * _iNewCols;
2978 m_piDims[0] = m_iRows;
2979 m_piDims[1] = m_iCols;
2990 SparseBool* SparseBool::insert(typed_list* _pArgs, SparseBool* _pSource)
2992 bool bNeedToResize = false;
2993 int iDims = (int)_pArgs->size();
2996 //sparse are only in 2 dims
3009 //evaluate each argument and replace by appropriate value and compute the count of combinations
3010 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
3014 cleanIndexesArguments(_pArgs, &pArg);
3021 if (getRows() == 1 || getCols() == 1)
3024 if (getSize() < piMaxDim[0])
3026 bNeedToResize = true;
3028 //need to enlarge sparse dimensions
3029 if (getCols() == 1 || getSize() == 0)
3032 iNewRows = piMaxDim[0];
3035 else if (getRows() == 1)
3039 iNewCols = piMaxDim[0];
3043 else if (getSize() < piMaxDim[0])
3046 cleanIndexesArguments(_pArgs, &pArg);
3053 if (piMaxDim[0] > getRows() || piMaxDim[1] > getCols())
3055 bNeedToResize = true;
3056 iNewRows = std::max(getRows(), piMaxDim[0]);
3057 iNewCols = std::max(getCols(), piMaxDim[1]);
3061 //check number of insertion
3062 if (_pSource->isScalar() == false && _pSource->getSize() != iSeqCount)
3065 cleanIndexesArguments(_pArgs, &pArg);
3069 //now you are sure to be able to insert values
3072 if (resize(iNewRows, iNewCols) == false)
3075 cleanIndexesArguments(_pArgs, &pArg);
3082 double* pIdx = pArg[0]->getAs<Double>()->get();
3083 for (int i = 0 ; i < iSeqCount ; i++)
3085 int iRow = static_cast<int>(pIdx[i] - 1) % getRows();
3086 int iCol = static_cast<int>(pIdx[i] - 1) / getRows();
3088 if (_pSource->isScalar())
3090 set(iRow, iCol, _pSource->get(0, 0), false);
3094 int iRowOrig = i % _pSource->getRows();
3095 int iColOrig = i / _pSource->getRows();
3096 set(iRow, iCol, _pSource->get(iRowOrig, iColOrig), false);
3102 double* pIdxRow = pArg[0]->getAs<Double>()->get();
3103 int iRowSize = pArg[0]->getAs<Double>()->getSize();
3104 double* pIdxCol = pArg[1]->getAs<Double>()->get();
3106 for (int i = 0 ; i < iSeqCount ; i++)
3108 if (_pSource->isScalar())
3110 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->get(0, 0), false);
3114 int iRowOrig = i % _pSource->getRows();
3115 int iColOrig = i / _pSource->getRows();
3116 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, _pSource->get(iRowOrig, iColOrig), false);
3124 cleanIndexesArguments(_pArgs, &pArg);
3129 SparseBool* SparseBool::insert(typed_list* _pArgs, InternalType* _pSource)
3131 typedef SparseBool* (SparseBool::*insert_t)(typed_list*, InternalType*);
3132 SparseBool* pIT = checkRef(this, (insert_t)&SparseBool::insert, _pArgs, _pSource);
3138 if (_pSource->isSparseBool())
3140 return insert(_pArgs, _pSource->getAs<SparseBool>());
3143 bool bNeedToResize = false;
3144 int iDims = (int)_pArgs->size();
3147 //sparse are only in 2 dims
3159 Bool* pSource = _pSource->getAs<Bool>();
3161 //evaluate each argument and replace by appropriate value and compute the count of combinations
3162 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
3166 cleanIndexesArguments(_pArgs, &pArg);
3173 if (getRows() == 1 || getCols() == 1)
3176 bNeedToResize = true;
3177 if (getSize() < piMaxDim[0])
3179 //need to enlarge sparse dimensions
3180 if (getCols() == 1 || getSize() == 0)
3183 iNewRows = piMaxDim[0];
3186 else if (getRows() == 1)
3190 iNewCols = piMaxDim[0];
3194 else if (getSize() < piMaxDim[0])
3197 cleanIndexesArguments(_pArgs, &pArg);
3204 if (piMaxDim[0] > getRows() || piMaxDim[1] > getCols())
3206 bNeedToResize = true;
3207 iNewRows = std::max(getRows(), piMaxDim[0]);
3208 iNewCols = std::max(getCols(), piMaxDim[1]);
3212 //check number of insertion
3213 if (pSource->isScalar() == false && pSource->getSize() != iSeqCount)
3216 cleanIndexesArguments(_pArgs, &pArg);
3220 //now you are sure to be able to insert values
3223 if (resize(iNewRows, iNewCols) == false)
3226 cleanIndexesArguments(_pArgs, &pArg);
3233 double* pIdx = pArg[0]->getAs<Double>()->get();
3234 for (int i = 0 ; i < iSeqCount ; i++)
3236 int iRow = static_cast<int>(pIdx[i] - 1) % getRows();
3237 int iCol = static_cast<int>(pIdx[i] - 1) / getRows();
3238 if (pSource->isScalar())
3240 set(iRow, iCol, pSource->get(0) != 0, false);
3244 set(iRow, iCol, pSource->get(i) != 0, false);
3250 double* pIdxRow = pArg[0]->getAs<Double>()->get();
3251 int iRowSize = pArg[0]->getAs<Double>()->getSize();
3252 double* pIdxCol = pArg[1]->getAs<Double>()->get();
3254 for (int i = 0 ; i < iSeqCount ; i++)
3256 if (pSource->isScalar())
3258 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, pSource->get(0) != 0, false);
3262 int iRowOrig = i % pSource->getRows();
3263 int iColOrig = i / pSource->getRows();
3265 set((int)pIdxRow[i % iRowSize] - 1, (int)pIdxCol[i / iRowSize] - 1, pSource->get(iRowOrig, iColOrig) != 0, false);
3273 cleanIndexesArguments(_pArgs, &pArg);
3277 GenericType* SparseBool::remove(typed_list* _pArgs)
3279 SparseBool* pOut = NULL;
3280 int iDims = (int)_pArgs->size();
3283 //sparse are only in 2 dims
3292 //evaluate each argument and replace by appropriate value and compute the count of combinations
3293 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
3297 cleanIndexesArguments(_pArgs, &pArg);
3301 bool* pbFull = new bool[iDims];
3302 //coord must represent all values on a dimension
3303 for (int i = 0 ; i < iDims ; i++)
3306 int iDimToCheck = getVarMaxDim(i, iDims);
3307 int iIndexSize = pArg[i]->getAs<GenericType>()->getSize();
3309 //we can have index more than once
3310 if (iIndexSize >= iDimToCheck)
3312 //size is good, now check datas
3313 double* pIndexes = getDoubleArrayFromDouble(pArg[i]);
3314 for (int j = 0 ; j < iDimToCheck ; j++)
3317 for (int k = 0 ; k < iIndexSize ; k++)
3319 if ((int)pIndexes[k] == j + 1)
3330 //only one dims can be not full/entire
3331 bool bNotEntire = false;
3333 bool bTooMuchNotEntire = false;
3334 for (int i = 0 ; i < iDims ; i++)
3336 if (pbFull[i] == false)
3338 if (bNotEntire == false)
3345 bTooMuchNotEntire = true;
3353 if (bTooMuchNotEntire == true)
3356 cleanIndexesArguments(_pArgs, &pArg);
3360 //find index to keep
3361 int iNotEntireSize = pArg[iNotEntire]->getAs<GenericType>()->getSize();
3362 double* piNotEntireIndex = getDoubleArrayFromDouble(pArg[iNotEntire]);
3363 int iKeepSize = getVarMaxDim(iNotEntire, iDims);
3364 bool* pbKeep = new bool[iKeepSize];
3366 //fill pbKeep with true value
3367 for (int i = 0 ; i < iKeepSize ; i++)
3372 for (int i = 0 ; i < iNotEntireSize ; i++)
3374 int idx = (int)piNotEntireIndex[i] - 1;
3376 //don't care of value out of bounds
3377 if (idx < iKeepSize)
3379 pbKeep[idx] = false;
3383 int iNewDimSize = 0;
3384 for (int i = 0 ; i < iKeepSize ; i++)
3386 if (pbKeep[i] == true)
3393 int* piNewDims = new int[iDims];
3394 for (int i = 0 ; i < iDims ; i++)
3396 if (i == iNotEntire)
3398 piNewDims[i] = iNewDimSize;
3402 piNewDims[i] = getVarMaxDim(i, iDims);
3406 //remove last dimension if are == 1
3407 int iOrigDims = iDims;
3408 for (int i = (iDims - 1) ; i >= 2 ; i--)
3410 if (piNewDims[i] == 1)
3422 if (iNewDimSize == 0)
3425 cleanIndexesArguments(_pArgs, &pArg);
3426 return new SparseBool(0, 0);
3430 //two cases, depends of original matrix/vector
3431 if ((*_pArgs)[0]->isColon() == false && m_iDims == 2 && m_piDims[0] == 1 && m_piDims[1] != 1)
3433 //special case for row vector
3434 pOut = new SparseBool(1, iNewDimSize);
3435 //in this case we have to care of 2nd dimension
3440 pOut = new SparseBool(iNewDimSize, 1);
3446 pOut = new SparseBool(piNewDims[0], piNewDims[0]);
3450 //find a way to copy existing data to new variable ...
3452 int* piIndexes = new int[iOrigDims];
3453 int* piViewDims = new int[iOrigDims];
3454 for (int i = 0 ; i < iOrigDims ; i++)
3456 piViewDims[i] = getVarMaxDim(i, iOrigDims);
3459 for (int i = 0 ; i < getSize() ; i++)
3461 bool bByPass = false;
3462 getIndexesWithDims(i, piIndexes, piViewDims, iOrigDims);
3464 //check if piIndexes use removed indexes
3465 for (int j = 0 ; j < iNotEntireSize ; j++)
3467 if ((piNotEntireIndex[j] - 1) == piIndexes[iNotEntire])
3469 //by pass this value
3475 if (bByPass == false)
3478 pOut->set(iNewPos, get(i));
3483 //free allocated data
3484 for (int i = 0 ; i < iDims ; i++)
3486 if (pArg[i] != (*_pArgs)[i])
3493 delete[] piViewDims;
3496 cleanIndexesArguments(_pArgs, &pArg);
3501 SparseBool* SparseBool::append(int r, int c, SparseBool SPARSE_CONST* src)
3503 SparseBool* pIT = checkRef(this, &SparseBool::append, r, c, src);
3509 doAppend(*src, r, c, *matrixBool);
3514 GenericType* SparseBool::insertNew(typed_list* _pArgs)
3517 SparseBool *pOut = NULL;
3519 int iDims = (int)_pArgs->size();
3520 int* piMaxDim = new int[iDims];
3521 int* piCountDim = new int[iDims];
3522 bool bUndefine = false;
3524 //evaluate each argument and replace by appropriate value and compute the count of combinations
3525 int iSeqCount = checkIndexesArguments(NULL, _pArgs, &pArg, piMaxDim, piCountDim);
3529 cleanIndexesArguments(_pArgs, &pArg);
3530 return createEmptyDouble();
3535 iSeqCount = -iSeqCount;
3541 //manage : and $ in creation by insertion
3543 int *piSourceDims = getDimsArray();
3545 for (int i = 0 ; i < iDims ; i++)
3547 if (pArg[i] == NULL)
3557 piMaxDim[i] = piSourceDims[iSource];
3558 piCountDim[i] = piSourceDims[iSource];
3561 //replace pArg value by the new one
3562 pArg[i] = createDoubleVector(piMaxDim[i]);
3566 // piMaxDim[i] = piCountDim[i];
3571 //remove last dimension at size 1
3572 //remove last dimension if are == 1
3573 for (int i = (iDims - 1) ; i >= 2 ; i--)
3575 if (piMaxDim[i] == 1)
3586 if (checkArgValidity(pArg) == false)
3589 cleanIndexesArguments(_pArgs, &pArg);
3590 //contain bad index, like <= 0, ...
3598 pOut = new SparseBool(piCountDim[0], 1);
3603 pOut = new SparseBool(1, piCountDim[0]);
3608 pOut = new SparseBool(piMaxDim[0], piMaxDim[1]);
3611 //insert values in new matrix
3612 SparseBool* pOut2 = pOut->insert(&pArg, this);
3619 cleanIndexesArguments(_pArgs, &pArg);
3624 SparseBool* SparseBool::extract(int nbCoords, int SPARSE_CONST* coords, int SPARSE_CONST* maxCoords, int SPARSE_CONST* resSize, bool asVector) SPARSE_CONST
3626 if ( (asVector && maxCoords[0] > getSize()) ||
3627 (asVector == false && maxCoords[0] > getRows()) ||
3628 (asVector == false && maxCoords[1] > getCols()))
3633 SparseBool * pSp (0);
3636 pSp = (getRows() == 1) ? new SparseBool(1, resSize[0]) : new SparseBool(resSize[0], 1);
3637 mycopy_n(makeMatrixIterator<bool>(*this, Coords<true>(coords, getRows())), nbCoords
3638 , makeMatrixIterator<bool>(*(pSp->matrixBool), RowWiseFullIterator(pSp->getRows(), pSp->getCols())));
3642 pSp = new SparseBool(resSize[0], resSize[1]);
3643 mycopy_n(makeMatrixIterator<bool>(*this, Coords<false>(coords, getRows())), nbCoords
3644 , makeMatrixIterator<bool>(*(pSp->matrixBool), RowWiseFullIterator(pSp->getRows(), pSp->getCols())));
3651 * create a new SparseBool of dims according to resSize and fill it from currentSparseBool (along coords)
3653 GenericType* SparseBool::extract(typed_list* _pArgs)
3655 SparseBool* pOut = NULL;
3656 int iDims = (int)_pArgs->size();
3659 int* piMaxDim = new int[iDims];
3660 int* piCountDim = new int[iDims];
3662 //evaluate each argument and replace by appropriate value and compute the count of combinations
3663 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
3667 cleanIndexesArguments(_pArgs, &pArg);
3668 if (_pArgs->size() == 0)
3671 delete[] piCountDim;
3678 delete[] piCountDim;
3680 return Double::Empty();
3686 // Check that we stay inside the input size.
3687 if (piMaxDim[0] <= getSize())
3692 if (getRows() == 1 && getCols() != 1 && (*_pArgs)[0]->isColon() == false)
3694 //special case for row vector
3696 iNewCols = piCountDim[0];
3700 iNewRows = piCountDim[0];
3704 pOut = new SparseBool(iNewRows, iNewCols);
3705 double* pIdx = pArg[0]->getAs<Double>()->get();
3706 // Write in output all elements extract from input.
3707 for (int i = 0 ; i < iSeqCount ; i++)
3715 int iRowRead = static_cast<int>(pIdx[i] - 1) % getRows();
3716 int iColRead = static_cast<int>(pIdx[i] - 1) / getRows();
3718 int iRowWrite = static_cast<int>(i) % iNewRows;
3719 int iColWrite = static_cast<int>(i) / iNewRows;
3721 bool bValue = get(iRowRead, iColRead);
3724 //only non zero values
3725 pOut->set(iRowWrite, iColWrite, true, false);
3732 delete[] piCountDim;
3734 cleanIndexesArguments(_pArgs, &pArg);
3740 // Check that we stay inside the input size.
3741 if (piMaxDim[0] <= getRows() && piMaxDim[1] <= getCols())
3743 double* pIdxRow = pArg[0]->getAs<Double>()->get();
3744 double* pIdxCol = pArg[1]->getAs<Double>()->get();
3746 int iNewRows = pArg[0]->getAs<Double>()->getSize();
3747 int iNewCols = pArg[1]->getAs<Double>()->getSize();
3749 pOut = new SparseBool(iNewRows, iNewCols);
3752 // Write in output all elements extract from input.
3753 for (int iRow = 0 ; iRow < iNewRows ; iRow++)
3755 for (int iCol = 0 ; iCol < iNewCols ; iCol++)
3757 if ((pIdxRow[iRow] < 1) || (pIdxCol[iCol] < 1))
3762 delete[] piCountDim;
3764 cleanIndexesArguments(_pArgs, &pArg);
3767 bool bValue = get((int)pIdxRow[iRow] - 1, (int)pIdxCol[iCol] - 1);
3770 //only non zero values
3771 pOut->set(iRow, iCol, true, false);
3780 delete[] piCountDim;
3782 cleanIndexesArguments(_pArgs, &pArg);
3790 delete[] piCountDim;
3792 cleanIndexesArguments(_pArgs, &pArg);
3797 bool SparseBool::invoke(typed_list & in, optional_list &/*opt*/, int /*_iRetCount*/, typed_list & out, const ast::Exp & e)
3801 out.push_back(this);
3805 InternalType * _out = extract(&in);
3808 std::wostringstream os;
3809 os << _W("Invalid index.\n");
3810 throw ast::InternalError(os.str(), 999, e.getLocation());
3812 out.push_back(_out);
3818 bool SparseBool::isInvokable() const
3823 bool SparseBool::hasInvokeOption() const
3828 int SparseBool::getInvokeNbIn()
3833 int SparseBool::getInvokeNbOut()
3838 std::size_t SparseBool::nbTrue() const
3840 return matrixBool->nonZeros() ;
3842 std::size_t SparseBool::nbTrue(std::size_t r) const
3844 int* piIndex = matrixBool->outerIndexPtr();
3845 return piIndex[r + 1] - piIndex[r];
3849 void SparseBool::setTrue(bool finalize)
3851 int rows = getRows();
3852 int cols = getCols();
3854 typedef Eigen::Triplet<bool> triplet;
3855 std::vector<triplet> tripletList;
3857 for (int i = 0; i < rows; ++i)
3859 for (int j = 0; j < cols; ++j)
3861 tripletList.push_back(triplet(i, j, true));
3865 matrixBool->setFromTriplets(tripletList.begin(), tripletList.end());
3869 matrixBool->finalize();
3873 void SparseBool::setFalse(bool finalize)
3875 int rows = getRows();
3876 int cols = getCols();
3878 typedef Eigen::Triplet<bool> triplet;
3879 std::vector<triplet> tripletList;
3881 for (int i = 0; i < rows; ++i)
3883 for (int j = 0; j < cols; ++j)
3885 tripletList.push_back(triplet(i, j, false));
3889 matrixBool->setFromTriplets(tripletList.begin(), tripletList.end());
3893 matrixBool->finalize();
3897 int* SparseBool::getNbItemByRow(int* _piNbItemByRows)
3899 int* piNbItemByRows = new int[getRows() + 1];
3900 mycopy_n(matrixBool->outerIndexPtr(), getRows() + 1, piNbItemByRows);
3902 for (int i = 0 ; i < getRows() ; i++)
3904 _piNbItemByRows[i] = piNbItemByRows[i + 1] - piNbItemByRows[i];
3907 delete[] piNbItemByRows;
3908 return _piNbItemByRows;
3911 int* SparseBool::getColPos(int* _piColPos)
3913 mycopy_n(matrixBool->innerIndexPtr(), nbTrue(), _piColPos);
3914 for (int i = 0; i < nbTrue(); i++)
3922 int* SparseBool::outputRowCol(int* out)const
3924 return sparseTransform(*matrixBool, sparseTransform(*matrixBool, out, GetRow<BoolSparse_t>()), GetCol<BoolSparse_t>());
3927 int* SparseBool::getInnerPtr(int* count)
3929 *count = matrixBool->innerSize();
3930 return matrixBool->innerIndexPtr();
3933 int* SparseBool::getOuterPtr(int* count)
3935 *count = matrixBool->outerSize();
3936 return matrixBool->outerIndexPtr();
3940 bool SparseBool::operator==(const InternalType& it) SPARSE_CONST
3942 SparseBool* otherSparse = const_cast<SparseBool*>(dynamic_cast<SparseBool const*>(&it));/* types::GenericType is not const-correct :( */
3944 && (otherSparse->getRows() == getRows())
3945 && (otherSparse->getCols() == getCols())
3946 && equal(*matrixBool, *otherSparse->matrixBool));
3949 bool SparseBool::operator!=(const InternalType& it) SPARSE_CONST
3951 return !(*this == it);
3954 void SparseBool::finalize()
3956 matrixBool->prune(&keepForSparse<bool>);
3957 matrixBool->finalize();
3960 bool SparseBool::get(int r, int c) SPARSE_CONST
3962 return matrixBool->coeff(r, c);
3965 SparseBool* SparseBool::set(int _iRows, int _iCols, bool _bVal, bool _bFinalize) SPARSE_CONST
3967 typedef SparseBool* (SparseBool::*set_t)(int, int, bool, bool);
3968 SparseBool* pIT = checkRef(this, (set_t)&SparseBool::set, _iRows, _iCols, _bVal, _bFinalize);
3974 matrixBool->coeffRef(_iRows, _iCols) = _bVal;
3984 void SparseBool::fill(Bool& dest, int r, int c) SPARSE_CONST
3986 mycopy_n(makeMatrixIterator<bool >(*matrixBool, RowWiseFullIterator(getRows(), getCols())), getSize()
3987 , makeMatrixIterator<bool >(dest, RowWiseFullIterator(dest.getRows(), dest.getCols(), r, c)));
3990 Sparse* SparseBool::newOnes() const
3992 return new Sparse(new types::Sparse::RealSparse_t(matrixBool->cast<double>()), 0);
3995 SparseBool* SparseBool::newNotEqualTo(SparseBool const&o) const
3997 return cwiseOp<std::not_equal_to>(*this, o);
4000 SparseBool* SparseBool::newEqualTo(SparseBool& o)
4002 int rowL = getRows();
4003 int colL = getCols();
4005 int rowR = o.getRows();
4006 int colR = o.getCols();
4007 int row = std::max(rowL, rowR);
4008 int col = std::max(colL, colR);
4010 //create a boolean sparse matrix with dims of sparses
4011 types::SparseBool* ret = new types::SparseBool(row, col);
4013 if (isScalar() && o.isScalar())
4016 bool r = o.get(0, 0);
4017 ret->set(0, 0, l == r, false);
4019 else if (isScalar())
4021 int nnzR = static_cast<int>(o.nbTrue());
4022 std::vector<int> rowcolR(nnzR * 2, 0);
4023 o.outputRowCol(rowcolR.data());
4025 //compare all items of R with R[0]
4027 for (int i = 0; i < nnzR; ++i)
4029 bool r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
4030 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
4033 else if (o.isScalar())
4035 int nnzL = static_cast<int>(nbTrue());
4036 std::vector<int> rowcolL(nnzL * 2, 0);
4037 outputRowCol(rowcolL.data());
4040 for (int i = 0; i < nnzL; ++i)
4042 bool l = get(rowcolL[i] - 1, rowcolL[i + nnzL] - 1);
4043 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, l == r, false);
4048 int nnzR = static_cast<int>(o.nbTrue());
4049 std::vector<int> rowcolR(nnzR * 2, 0);
4050 o.outputRowCol(rowcolR.data());
4051 int nnzL = static_cast<int>(nbTrue());
4052 std::vector<int> rowcolL(nnzL * 2, 0);
4053 outputRowCol(rowcolL.data());
4054 //set all values to %t
4055 ret->setTrue(false);
4056 //set %f in each pL values
4057 for (int i = 0; i < nnzL; ++i)
4059 ret->set(rowcolL[i] - 1, rowcolL[i + nnzL] - 1, false, false);
4063 //set _pR[i] == _pL[i] for each _pR values
4064 for (int i = 0; i < nnzR; ++i)
4066 //get l and r following non zeros value of R
4067 bool l = get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
4068 bool r = o.get(rowcolR[i] - 1, rowcolR[i + nnzR] - 1);
4069 //set value following non zeros value of R
4070 ret->set(rowcolR[i] - 1, rowcolR[i + nnzR] - 1, l == r, false);
4078 SparseBool* SparseBool::newLogicalOr(SparseBool const&o) const
4080 return cwiseOp<std::logical_or>(*this, o);
4083 SparseBool* SparseBool::newLogicalAnd(SparseBool const&o) const
4085 return cwiseOp<std::logical_and>(*this, o);
4088 SparseBool* SparseBool::reshape(int* _piDims, int _iDims)
4090 SparseBool* pSpBool = NULL;
4100 pSpBool = reshape(_piDims[0], iCols);
4106 SparseBool* SparseBool::reshape(int _iNewRows, int _iNewCols)
4108 typedef SparseBool* (SparseBool::*reshape_t)(int, int);
4109 SparseBool* pIT = checkRef(this, (reshape_t)&SparseBool::reshape, _iNewRows, _iNewCols);
4115 if (_iNewRows * _iNewCols != getRows() * getCols())
4120 SparseBool* res = NULL;
4124 size_t iNonZeros = matrixBool->nonZeros();
4125 BoolSparse_t *newBool = new BoolSparse_t(_iNewRows, _iNewCols);
4126 newBool->reserve((int)iNonZeros);
4129 int* pRows = new int[iNonZeros * 2];
4130 outputRowCol(pRows);
4131 int* pCols = pRows + iNonZeros;
4133 typedef Eigen::Triplet<bool> triplet;
4134 std::vector<triplet> tripletList;
4136 for (size_t i = 0 ; i < iNonZeros ; i++)
4138 int iCurrentPos = ((int)pCols[i] - 1) * getRows() + ((int)pRows[i] - 1);
4139 tripletList.push_back(triplet((int)(iCurrentPos % _iNewRows), (int)(iCurrentPos / _iNewRows), true));
4142 newBool->setFromTriplets(tripletList.begin(), tripletList.end());
4145 matrixBool = newBool;
4148 m_iRows = _iNewRows;
4149 m_iCols = _iNewCols;
4150 m_iSize = _iNewRows * _iNewCols;
4153 m_piDims[0] = m_iRows;
4154 m_piDims[1] = m_iCols;
4167 bool SparseBool::transpose(InternalType *& out)
4169 out = new SparseBool(new BoolSparse_t(matrixBool->transpose()));
4173 template<typename T>
4174 void neg(const int r, const int c, const T * const in, Eigen::SparseMatrix<bool, 1> * const out)
4176 for (int i = 0; i < r; i++)
4178 for (int j = 0; j < c; j++)
4180 out->coeffRef(i, j) = !in->coeff(i, j);
4184 out->prune(&keepForSparse<bool>);