47 typedef ptrdiff_t
INTT;
71 typedef std::list< int >
group;
76 static inline bool isZero(
const T lambda) {
77 return static_cast<double>(abs<T>(lambda)) < 1e-99;
81 static inline bool isEqual(
const T lambda1,
const T lambda2) {
82 return static_cast<double>(abs<T>(lambda1-lambda2)) < 1e-99;
87 static inline T
softThrs(
const T x,
const T lambda) {
90 }
else if (x < -lambda) {
98 static inline T
hardThrs(
const T x,
const T lambda) {
99 return (x > lambda || x < -lambda) ? x : 0;
102 template <
typename T>
103 static inline T
alt_log(
const T x);
107 template <
typename T>
111 }
else if (x < 1e-20) {
114 return x*alt_log<T>(x);
118 template <
typename T>
123 return alt_log<T>( T(1.0) + exp_alt<T>( x ) );
130 template <
typename T>
class Data {
134 const int i)
const = 0;
135 virtual inline T
operator[](
const int index)
const = 0;
136 virtual int n()
const = 0;
137 virtual int m()
const = 0;
138 virtual int V()
const = 0;
146 virtual int n()
const = 0;
147 virtual int m()
const = 0;
151 const T alpha = 1.0,
const T beta = 0.0)
const = 0;
155 const T alpha = 1.0,
const T beta = 0.0)
const = 0;
158 const T alpha = 1.0,
const T beta = 0.0)
const = 0;
162 const bool transA =
false,
const bool transB =
false,
163 const T a = 1.0,
const T b = 0.0)
const = 0;
166 const bool transA =
false,
const bool transB =
false,
167 const T a = 1.0,
const T b = 0.0)
const = 0;
171 const bool transA =
false,
const bool transB =
false,
172 const T a = 1.0,
const T b = 0.0)
const = 0;
175 virtual void XtX(
Matrix<T>& XtX)
const = 0;
177 virtual void copyRow(
const int i,
Vector<T>& x)
const = 0;
179 virtual void copyTo(
Matrix<T>& copy)
const = 0;
180 virtual T dot(
const Matrix<T>& x)
const = 0;
182 virtual void print(
const string& name)
const = 0;
190 virtual int n()
const = 0;
191 virtual int m()
const = 0;
193 virtual void copyCol(
const int i,
Vector<T>& Xi)
const = 0;
195 virtual void add_rawCol(
const int i, T* col,
const T a)
const = 0;
197 virtual void extract_rawCol(
const int i,T* Xi)
const = 0;
199 virtual void diag(
Vector<T>& diag)
const = 0;
201 virtual inline T operator()(
const int index1,
const int index2)
const = 0;
222 inline int m()
const {
return _m; };
224 inline int n()
const {
return _n; };
226 inline T& operator()(
const int i,
const int j);
228 inline T operator()(
const int i,
const int j)
const;
232 inline T
operator[](
const int index)
const {
return _X[index]; };
234 inline void copyCol(
const int i,
Vector<T>& x)
const;
236 inline void copyRow(
const int i,
Vector<T>& x)
const;
238 inline void extract_rawCol(
const int i, T* x)
const;
240 virtual void add_rawCol(
const int i, T* DtXi,
const T a)
const;
247 inline void refCol(
int i,
Vector<T>& x)
const;
249 inline void refSubMat(
int i,
int n,
Matrix<T>& mat)
const;
251 inline void subMatrixSym(
const Vector<int>& indices,
254 inline T*
rawX()
const {
return _X; };
256 inline const T*
X()
const {
return _X; };
262 inline void copyRef(
const Matrix<T>& mat);
266 inline void print(
const string& name)
const;
273 inline void resize(
int m,
int n);
275 inline void setData(T* X,
int m,
int n);
277 inline void setm(
const int m) { _m =
m; };
279 inline void setn(
const int n) { _n =
n; };
281 inline void setZeros();
283 inline void set(
const T a);
287 inline void setAleat();
291 inline void normalize();
293 inline void normalize2();
295 inline void center();
297 inline void center_rows();
301 inline void scal(
const T a);
304 inline void fillSymmetric();
305 inline void fillSymmetric2();
307 inline void fakeSize(
const int m,
const int n) { _n =
n; _m=
m;};
309 inline void whiten(
const int V);
311 inline void whiten(
Vector<T>& mean,
const bool pattern =
false);
315 inline void unwhiten(
Vector<T>& mean,
const bool pattern =
false);
317 inline void sum_cols(
Vector<T>& sum)
const;
321 inline bool isNormalized()
const;
323 inline int fmax()
const;
325 inline T fmaxval()
const;
327 inline int fmin()
const;
336 inline void incrDiag();
337 inline void addDiag(
const Vector<T>& diag);
338 inline void addDiag(
const T diag);
339 inline void addToCols(
const Vector<T>& diag);
340 inline void addVecToCols(
const Vector<T>& diag,
const T a = 1.0);
343 inline void svdRankOne(
const Vector<T>& u0,
345 inline void singularValues(
Vector<T>& u)
const;
351 inline void eigLargestSymApprox(
const Vector<T>& u0,
358 inline T eigLargestMagnSym(
const Vector<T>& u0,
362 inline T eigLargestMagnSym()
const;
364 inline void invSym();
367 const T alpha = 1.0,
const T beta = 0.0)
const;
372 inline void multTrans(
const SpVector<T>& x,
Vector<T>& b,
const T alpha =1.0,
const T beta = 0.0)
const;
375 const T alpha = 1.0,
const T beta = 0.0)
const;
378 const T alpha = 1.0,
const T beta = 0.0)
const;
381 const bool transA =
false,
const bool transB =
false,
382 const T a = 1.0,
const T b = 0.0)
const;
385 const bool transA =
false,
const bool transB =
false,
386 const T a = 1.0,
const T b = 0.0)
const;
389 const bool transB =
false,
const T a = 1.0,
390 const T b = 0.0)
const;
392 inline void multDiagLeft(
const Vector<T>& diag);
394 inline void multDiagRight(
const Vector<T>& diag);
410 inline void setDiag(
const T val);
415 inline void Invsqrt();
420 Vector<T>& y,
const T a = 1.0,
const T b = 0.0)
const;
424 inline void add(
const Matrix<T>& mat,
const T alpha = 1.0);
426 inline void add(
const T alpha);
428 inline T dot(
const Matrix<T>& mat)
const;
432 inline void inv_elem();
434 inline void inv() { this->inv_elem(); };
436 inline T trace()
const;
438 inline T asum()
const;
440 inline T normF()
const;
442 inline T mean()
const;
444 inline T normFsq()
const;
446 inline T
nrm2sq()
const {
return this->normFsq(); };
448 inline T norm_inf_2_col()
const;
450 inline T norm_1_2_col()
const;
452 inline void norm_2_cols(
Vector<T>& norms)
const;
454 inline void norm_2_rows(
Vector<T>& norms)
const;
456 inline void norm_inf_cols(
Vector<T>& norms)
const;
458 inline void norm_inf_rows(
Vector<T>& norms)
const;
460 inline void norm_l1_rows(
Vector<T>& norms)
const;
464 inline void norm_2sq_rows(
Vector<T>& norms)
const;
465 inline void thrsmax(
const T nu);
466 inline void thrsmin(
const T nu);
467 inline void thrsabsmin(
const T nu);
469 inline void softThrshold(
const T nu);
470 inline void hardThrshold(
const T nu);
472 inline void thrsPos();
475 const T alpha = 1.0);
478 const T alpha = 1.0);
481 const T alpha = 1.0);
484 const T alpha = 1.0);
487 const T alpha = 1.0);
490 const T alpha = 1.0);
492 inline void meanCol(
Vector<T>& mean)
const;
494 inline void meanRow(
Vector<T>& mean)
const;
496 inline void fillRow(
const Vector<T>& row);
498 inline void extractRow(
const int i,
Vector<T>& row)
const;
499 inline void setRow(
const int i,
const Vector<T>& row);
500 inline void addRow(
const int i,
const Vector<T>& row,
const T a=1.0);
504 const T tol = 1e-4,
const int = 4)
const;
507 inline void drop(
char* fileName)
const;
509 inline void NadarayaWatson(
const Vector<int>& ind,
const T sigma);
511 inline void blockThrshold(
const T nu,
const int sizeGroup);
513 inline void sparseProject(
Matrix<T>& out,
const T thrs,
const int mode = 1,
const T lambda1 = 0,
514 const T lambda2 = 0,
const T lambda3 = 0,
const bool pos =
false,
const int numThreads=-1);
515 inline void transformFilter();
521 inline void toSparseTrans(
SpMatrix<T>& matrixTrans);
523 inline void toVect(
Vector<T>& vec)
const;
525 inline int V()
const {
return 1;};
548 template<
typename T>
class Vector {
567 inline void print(
const char* name)
const;
569 inline int max()
const;
571 inline int min()
const;
573 inline T maxval()
const;
575 inline T minval()
const;
577 inline int fmax()
const;
579 inline int fmin()
const;
581 inline T fmaxval()
const;
583 inline T fminval()
const;
591 inline int n()
const {
return _n; };
593 inline T*
rawX()
const {
return _X; };
597 inline void logspace(
const int n,
const T a,
const T b);
598 inline int nnz()
const;
602 inline void setZeros();
604 inline void resize(
const int n);
606 inline void setPointer(T* X,
const int n);
607 inline void setData(T* X,
const int n) { this->setPointer(X,n); };
609 inline void randperm(
int n);
611 inline void setAleat();
615 inline void softThrshold(
const T nu);
616 inline void hardThrshold(
const T nu);
618 inline void thrsmax(
const T nu);
619 inline void thrsmin(
const T nu);
620 inline void thrsabsmin(
const T nu);
622 inline void thrshold(
const T nu);
624 inline void thrsPos();
626 inline void set(
const T val);
627 inline void setn(
const int n) { _n =
n; };
628 inline bool alltrue()
const;
629 inline bool allfalse()
const;
633 inline T nrm2()
const;
635 inline T nrm2sq()
const;
641 inline void add(
const Vector<T>& x,
const T a = 1.0);
643 inline void add(
const SpVector<T>& x,
const T a = 1.0);
645 inline void add(
const T a);
663 inline void Invsqrt();
672 inline void normalize();
674 inline void normalize2();
676 inline void whiten(
Vector<T>& mean,
const bool pattern =
false);
678 inline void whiten(
Vector<T>& mean,
const 681 inline void whiten(
const int V);
689 inline void unwhiten(
Vector<T>& mean,
const bool pattern =
false);
691 inline void scal(
const T a);
701 inline T softmax(
const int y);
703 inline T asum()
const;
704 inline T lzero()
const;
706 inline T afused()
const;
708 inline T sum()
const;
713 inline void l1project(
Vector<T>& out,
const T thrs,
const bool simplex =
false)
const;
714 inline void l1project_weighted(
Vector<T>& out,
const Vector<T>& weights,
const T thrs,
const bool residual =
false)
const;
715 inline void l1l2projectb(
Vector<T>& out,
const T thrs,
const T gamma,
const bool pos =
false,
717 inline void sparseProject(
Vector<T>& out,
const T thrs,
const int mode = 1,
const T lambda1 = 0,
718 const T lambda2 = 0,
const T lambda3 = 0,
const bool pos =
false);
719 inline void project_sft(
const Vector<int>& labels,
const int clas);
720 inline void project_sft_binary(
const Vector<T>& labels);
724 inline void l1l2project(
Vector<T>& out,
const T thrs,
const T gamma,
const bool pos =
false)
const;
725 inline void fusedProject(
Vector<T>& out,
const T lambda1,
const T lambda2,
const int itermax);
726 inline void fusedProjectHomotopy(
Vector<T>& out,
const T lambda1,
const T lambda2,
const T lambda3 = 0,
727 const bool penalty =
true);
732 inline void sort(
const bool mode);
736 inline void sort2(
Vector<int>& key,
const bool mode);
738 inline void applyBayerPattern(
const int offset);
766 SpMatrix(T* v,
int* r,
int* pB,
int* pE,
int m,
int n,
int nzmax);
779 inline int pB(
const int i)
const {
return _pB[i]; };
781 inline int r(
const int i)
const {
return _r[i]; };
783 inline T
v(
const int i)
const {
return _v[i]; };
785 inline int nzmax()
const {
return _nzmax; };
787 inline int n()
const {
return _n; };
789 inline int m()
const {
return _m; };
791 inline int V()
const {
return 1; };
798 inline void print(
const string& name)
const;
800 inline T asum()
const;
802 inline T normFsq()
const;
804 inline int*
pB()
const {
return _pB; };
806 inline int*
pE()
const {
return _pE; };
808 inline int*
r()
const {
return _r; };
810 inline T*
v()
const {
return _v; };
812 inline int nnz()
const {
return _pB[_n]; };
813 inline void add_direct(
const SpMatrix<T>& mat,
const T a);
821 inline void resize(
const int m,
const int n,
const int nzmax);
823 inline void scal(
const T a)
const;
839 Matrix<T>& XAt,
const int numthreads=-1)
const;
844 const T alpha = 1.0,
const T beta = 0.0)
const;
846 const T alpha = 1.0,
const T beta = 0.0)
const;
849 const T alpha = 1.0,
const T beta = 0.0)
const;
852 const T alpha = 1.0,
const T beta = 0.0)
const;
855 const bool transA =
false,
const bool transB =
false,
856 const T a = 1.0,
const T b = 0.0)
const;
859 const bool transA =
false,
const bool transB =
false,
860 const T a = 1.0,
const T b = 0.0)
const;
863 const bool transB =
false,
const T a = 1.0,
864 const T b = 0.0)
const;
869 inline void copyRow(
const int i,
Vector<T>& x)
const;
870 inline void sum_cols(
Vector<T>& sum)
const;
875 inline void toFull(
Matrix<T>& matrix)
const;
877 inline void toFullTrans(
Matrix<T>& matrix)
const;
888 inline void norm_0_cols(
Vector<T>& norms)
const;
890 inline void norm_1_cols(
Vector<T>& norms)
const;
891 inline void addVecToCols(
const Vector<T>& diag,
const T a = 1.0);
892 inline void addVecToColsWeighted(
const Vector<T>& diag,
const T* weights,
const T a = 1.0);
918 template <
typename T>
class SpVector {
924 SpVector(T* v,
int* r,
int L,
int nzmax);
935 inline T
nzmax()
const {
return _nzmax; };
939 inline T asum()
const;
941 inline T nrm2sq()
const;
943 inline T nrm2()
const;
945 inline T fmaxval()
const;
947 inline void print(
const string& name)
const;
949 inline void refIndices(
Vector<int>& indices)
const;
951 inline void refVal(
Vector<T>& val)
const;
953 inline int r(
const int i)
const {
return _r[i]; };
955 inline T
v(
const int i)
const {
return _v[i]; };
956 inline T*
rawX()
const {
return _v; };
958 inline int L()
const {
return _L; };
960 inline void setL(
const int L) { _L=L; };
970 inline void resize(
const int nzmax);
974 const int m,
const int n)
const;
976 void inline toFull(
Vector<T>& out)
const;
1013 inline void setMatrices(
const Matrix<T>& D,
const bool high_memory=
true);
1014 inline void setMatrices(
const Matrix<T>& D,
1015 const Matrix<T>& X,
const bool high_memory=
true);
1017 inline void copyCol(
const int i,
Vector<T>& DtXi)
const;
1019 inline void extract_rawCol(
const int i,T* DtXi)
const;
1021 virtual void add_rawCol(
const int i, T* DtXi,
const T a)
const;
1023 void inline addDiag(
const T diag);
1025 void inline diag(
Vector<T>& diag)
const;
1027 inline int n()
const {
return _n;};
1029 inline int m()
const {
return _m;};
1031 inline T operator()(
const int index1,
const int index2)
const;
1053 _externAlloc(true), _X(X), _m(m), _n(n) { };
1059 #pragma omp critical 1086 std::cerr << name << std::endl;
1087 std::cerr <<
_m <<
" x " <<
_n << std::endl;
1088 for (
int i = 0; i<
_m; ++i) {
1089 for (
int j = 0; j<
_n; ++j) {
1090 printf(
"%10.5g ",static_cast<double>(
_X[j*_m+i]));
1101 assert(i >= 0 && i<
_n);
1103 cblas_copy<T>(
_m,
_X+i*
_m,1,x.
_X,1);
1108 assert(i >= 0 && i<
_m);
1116 assert(i >= 0 && i<
_n);
1117 cblas_copy<T>(
_m,
_X+i*
_m,1,x,1);
1122 assert(i >= 0 && i<
_n);
1123 cblas_axpy<T>(
_m,a,
_X+i*
_m,1,x,1);
1132 const vector_groups& groups,
const int i)
const {
1133 const group& gr = groups[i];
1134 const int N = gr.size();
1137 for (group::const_iterator it = gr.begin(); it != gr.end(); ++it) {
1138 cblas_copy<T>(
_m,
_X+(*it)*
_m,1,data.
_X+count*
_m,1);
1145 assert(i >= 0 && i<
_n);
1159 for (
int i = 0; i<
_n; ++i) {
1160 T norm=cblas_nrm2<T>(
_m,
_X+_m*i,1);
1161 if (fabs(norm - 1.0) > 1e-6)
return false;
1167 template <
typename T>
1174 for (
int i = 0; i<
_n; ++i) {
1175 for (
int j = i+1; j<
_n; ++j) {
1176 if (prG[i*_n+j] > 0.99) {
1189 return cblas_iamax<T>(
_n*
_m,
_X,1);
1194 return _X[cblas_iamax<T>(
_n*
_m,
_X,1)];
1200 return cblas_iamin<T>(
_n*
_m,
_X,1);
1206 int L = indices.
n();
1208 T* out = subMatrix.
_X;
1209 int* rawInd = indices.
rawX();
1210 for (
int i = 0; i<L; ++i)
1211 for (
int j = 0; j<=i; ++j)
1212 out[i*L+j]=
_X[rawInd[i]*
_n+rawInd[j]];
1218 if (
_n==n &&
_m==m)
return;
1223 #pragma omp critical 1241 memset(
_X,0,
_n*
_m*
sizeof(T));
1246 for (
int i = 0; i<
_n*
_m; ++i)
_X[i]=a;
1260 for (
int i = 0; i<_n*_m; ++i) _X[i]=normalDistrib<T>();
1266 for (
int i = 0; i<
MIN(
_n,
_m); ++i)
_X[i*
_m+i] = T(1.0);
1272 for (
int i = 0; i<
_n; ++i) {
1273 T norm=cblas_nrm2<T>(
_m,
_X+_m*i,1);
1276 cblas_scal<T>(
_m,invNorm,
_X+_m*i,1);
1289 for (
int i = 0; i<
_n; ++i) {
1290 T norm=cblas_nrm2<T>(
_m,
_X+_m*i,1);
1293 cblas_scal<T>(
_m,invNorm,
_X+_m*i,1);
1300 for (
int i = 0; i<
_n; ++i) {
1304 col.
add(-sum/static_cast<T>(
_m));
1312 for (
int i = 0; i<
_n; ++i)
1313 for (
int j = 0; j<
_m; ++j)
1314 mean_rows[j] +=
_X[i*_m+j];
1315 mean_rows.
scal(T(1.0)/_n);
1316 for (
int i = 0; i<
_n; ++i)
1317 for (
int j = 0; j<
_m; ++j)
1318 _X[i*_m+j] -= mean_rows[j];
1324 for (
int i = 0; i<
_n; ++i) {
1327 T sum = col.
sum()/
static_cast<T
>(
_m);
1335 cblas_scal<T>(
_n*
_m,a,
_X,1);
1341 cblas_copy<T>(
_m*
_n,mat.
_X,1,
_X,1);
1352 for (
int i = 0; i<
_n; ++i) {
1353 for (
int j =0; j<i; ++j) {
1359 for (
int i = 0; i<
_n; ++i) {
1360 for (
int j =0; j<i; ++j) {
1368 const int sizePatch=
_m/
V;
1369 for (
int i = 0; i<
_n; ++i) {
1370 for (
int j = 0; j<
V; ++j) {
1372 for (
int k = 0; k<sizePatch; ++k) {
1373 mean+=
_X[i*
_m+sizePatch*j+k];
1376 for (
int k = 0; k<sizePatch; ++k) {
1386 const int n =
static_cast<int>(sqrt(static_cast<T>(
_m)));
1388 for (
int i = 0; i<4; ++i) count[i]=0;
1389 for (
int i = 0; i<
_n; ++i) {
1391 for (
int j = 0; j<
n; ++j) {
1392 offsetx= (offsetx+1) % 2;
1394 for (
int k = 0; k<
n; ++k) {
1395 offsety= (offsety+1) % 2;
1396 mean[2*offsetx+offsety]+=
_X[i*
_m+j*n+k];
1397 count[2*offsetx+offsety]++;
1401 for (
int i = 0; i<4; ++i)
1402 mean[i] /= count[i];
1403 for (
int i = 0; i<
_n; ++i) {
1405 for (
int j = 0; j<
n; ++j) {
1406 offsetx= (offsetx+1) % 2;
1408 for (
int k = 0; k<
n; ++k) {
1409 offsety= (offsety+1) % 2;
1410 _X[i*
_m+j*n+k]-=mean[2*offsetx+offsety];
1415 const int V = mean.
n();
1416 const int sizePatch=
_m/
V;
1417 for (
int i = 0; i<
_n; ++i) {
1418 for (
int j = 0; j<
V; ++j) {
1419 for (
int k = 0; k<sizePatch; ++k) {
1420 mean[j]+=
_X[i*
_m+sizePatch*j+k];
1424 mean.
scal(T(1.0)/(_n*sizePatch));
1425 for (
int i = 0; i<
_n; ++i) {
1426 for (
int j = 0; j<
V; ++j) {
1427 for (
int k = 0; k<sizePatch; ++k) {
1428 _X[i*
_m+sizePatch*j+k]-=mean[j];
1437 const int V = mean.
n();
1438 const int sizePatch=
_m/
V;
1440 for (
int i = 0; i<
_n; ++i) {
1441 for (
int j = 0; j<
V; ++j) {
1442 for (
int k = 0; k<sizePatch; ++k) {
1443 mean[j]+=
_X[i*
_m+sizePatch*j+k];
1447 for (
int i = 0; i<
V; ++i)
1448 mean[i] /= _n*
cblas_asum(sizePatch,mask.
_X+i*sizePatch,1);
1449 for (
int i = 0; i<
_n; ++i) {
1450 for (
int j = 0; j<
V; ++j) {
1451 for (
int k = 0; k<sizePatch; ++k) {
1452 if (mask[sizePatch*j+k])
1453 _X[i*
_m+sizePatch*j+k]-=mean[j];
1462 const int n =
static_cast<int>(sqrt(static_cast<T>(
_m)));
1463 for (
int i = 0; i<
_n; ++i) {
1465 for (
int j = 0; j<
n; ++j) {
1466 offsetx= (offsetx+1) % 2;
1468 for (
int k = 0; k<
n; ++k) {
1469 offsety= (offsety+1) % 2;
1470 _X[i*
_m+j*n+k]+=mean[2*offsetx+offsety];
1475 const int V = mean.
n();
1476 const int sizePatch=
_m/
V;
1477 for (
int i = 0; i<
_n; ++i) {
1478 for (
int j = 0; j<
V; ++j) {
1479 for (
int k = 0; k<sizePatch; ++k) {
1480 _X[i*
_m+sizePatch*j+k]+=mean[j];
1492 for (
int i = 0; i<
_n; ++i)
1493 for (
int j = 0; j<
_m; ++j)
1494 out[j*_n+i] =
_X[i*_m+j];
1499 for (
int i = 0; i<
_n*
_m; ++i)
_X[i]=-
_X[i];
1509 for (
int i = 0; i<
MIN(
_n,
_m); ++i)
_X[i*
_m+i] += d[i];
1514 for (
int i = 0; i<
MIN(
_n,
_m); ++i)
_X[i*
_m+i] += diag;
1520 for (
int i = 0; i<
_n; ++i) {
1529 for (
int i = 0; i<
_n; ++i) {
1549 for (i = 0; i<max_iter; ++i) {
1555 if (i > 10 && (1 - fabs(theta)) < eps)
break;
1568 }
else if (
_n > 10*
_m) {
1578 gesvd<T>(
no,
no,
_m,
_n,copyX.
_X,
_m,u.
rawX(),vu,1,vv,1);
1597 for (
int i = 0; i<num_eig; ++i)
1599 inveigs[i]=T(1.0)/S[i];
1604 }
else if (
_n > 10*
_m) {
1609 U.
mult(*
this,V,
true,
false);
1612 for (
int i = 0; i<num_eig; ++i)
1614 inveigs[i]=T(1.0)/S[i];
1622 gesvd<T>(
reduced,
reduced,
_m,
_n,copyX.
_X,
_m,S.
rawX(),U.
rawX(),
_m,V.
rawX(),num_eig);
1632 const int max_iter=100;
1642 for (j = 0; j<2;++j) {
1644 for (i = 0; i<max_iter; ++i) {
1649 if ((1 - fabs(theta)) < eps)
break;
1653 if (isnan(lambda)) {
1654 std::cerr <<
"eigLargestSymApprox failed" << std::endl;
1657 if (j == 1 && lambda < eps) {
1661 if (theta >= 0)
break;
1663 for (i = 0; i<
_m; ++i)
_X[i*_m+i]-=lambda;
1674 const int max_iter=1000;
1682 for (
int i = 0; i<max_iter; ++i) {
1686 if (norm > 0) u.
scal(1.0/norm);
1687 if (norm == 0 || fabs(norm-lambda)/norm < eps)
break;
1696 const int max_iter=1000;
1704 for (
int i = 0; i<max_iter; ++i) {
1708 if (fabs(norm-lambda) < eps)
break;
1744 Vector<T>& b,
const T a,
const T c)
const {
1747 cblas_gemv<T>(
CblasColMajor,
CblasTrans,
_m,
_n,a,
_X,
_m,x.
_X,1,c,b.
_X,1);
1752 Vector<T>& b,
const T alpha,
const T beta)
const {
1756 for (
int i = 0; i<
_n; ++i) {
1758 b.
_X[i] = alpha*col.
dot(x);
1762 for (
int i = 0; i<
_n; ++i) {
1764 b.
_X[i] = beta*b.
_X[i]+alpha*col.
dot(x);
1773 bool* pr_active=active.
rawX();
1774 for (
int i = 0; i<
_n; ++i) {
1784 Vector<T>& b,
const T a,
const T c)
const {
1787 cblas_gemv<T>(
CblasColMajor,
CblasNoTrans,
_m,
_n,a,
_X,
_m,x.
_X,1,c,b.
_X,1);
1792 Vector<T>& b,
const T a,
const T a2)
const {
1795 }
else if (a2 != 1.0) {
1799 for (
int i = 0; i<x.
_L; ++i) {
1803 for (
int i = 0; i<x.
_L; ++i) {
1811 Matrix<T>& C,
const bool transA,
const bool transB,
1812 const T a,
const T b)
const {
1834 cblas_gemm<T>(
CblasColMajor,trA,trB,
m,
n,k,a,
_X,
_m,B.
_X,B.
_m,
1839 template <
typename T>
1841 const bool transA,
const bool transB,
1842 const T a,
const T b)
const {
1843 B.
mult(*
this,C,transB,transA,a,b);
1847 template <
typename T>
1849 const bool transA,
const bool transB,
1850 const T a,
const T b)
const {
1861 for (
int i = 0; i<
_n; ++i) {
1863 B.
mult(colA,rowC,a);
1875 for (
int i = 0; i<B.
n(); ++i) {
1891 for (
int i = 0; i<
_n; ++i) {
1905 for (
int i = 0; i<B.
n(); ++i) {
1908 this->
mult(colB,colC,a,T(1.0));
1916 template <
typename T>
1921 for (
int i = 0; i<
_n; ++i) {
1922 for (
int j = 0; j<
_m; ++j) {
1934 for (
int i = 0; i<
_n; ++i) {
1935 for (
int j = 0; j<
_m; ++j) {
1977 for (
int i = 0; i<L; ++i) {
1988 T*
const d = dv.
rawX();
1989 for (
int i = 0; i<size_diag; ++i)
1996 T*
const d = dv.
rawX();
1997 for (
int i = 0; i<size_diag; ++i)
2004 for (
int i = 0; i<size_diag; ++i)
2027 for (
int i = 0; i<L; ++i)
2028 for (
int j = 0; j<L; ++j)
2029 sum +=
_X[r[i]*
_m+r[j]]*v[i]*v[j];
2035 const int size_y= y.
n();
2036 const int nn =
_n/size_y;
2040 for (
int i = 0; i<size_y; ++i) {
2042 y[i]=b*y[i]+a*tmp.
quad(vec1,vec2);
2054 for (
int i = 0; i<L; ++i) {
2056 sum += v[i]*col.
dot(vec1);
2063 assert(mat.
_m ==
_m && mat.
_n ==
_n);
2064 cblas_axpy<T>(
_n*
_m,alpha,mat.
_X,1,
_X,1);
2069 assert(mat.
_m ==
_m && mat.
_n ==
_n);
2070 return cblas_dot<T>(
_n*
_m,mat.
_X,1,
_X,1);
2076 for (
int i = 0; i<
_n*
_m; ++i)
_X[i]+=alpha;
2086 return cblas_asum<T>(
_n*
_m,
_X,1);
2093 for (
int i = 0; i<
m; ++i)
2100 return cblas_nrm2<T>(
_n*
_m,
_X,1);
2111 return cblas_dot<T>(
_n*
_m,
_X,1,
_X,1);
2118 for (
int i = 0; i<
_n; ++i) {
2120 T norm_col = col.
nrm2();
2131 for (
int i = 0; i<
_n; ++i) {
2143 for (
int i = 0; i<
_n; ++i)
2144 for (
int j = 0; j<
_m; ++j)
2145 norms[j] +=
_X[i*_m+j]*
_X[i*_m+j];
2146 for (
int j = 0; j<
_m; ++j)
2147 norms[j]=sqrt(norms[j]);
2155 for (
int i = 0; i<
_n; ++i)
2156 for (
int j = 0; j<
_m; ++j)
2157 norms[j] +=
_X[i*_m+j]*
_X[i*_m+j];
2166 for (
int i = 0; i<
_n; ++i) {
2168 norms[i] = col.
nrm2();
2177 for (
int i = 0; i<
_n; ++i) {
2187 for (
int i = 0; i<
_n; ++i)
2188 for (
int j = 0; j<
_m; ++j)
2189 norms[j] =
MAX(abs<T>(
_X[i*_m+j]),norms[j]);
2196 for (
int i = 0; i<
_n; ++i)
2197 for (
int j = 0; j<
_m; ++j)
2198 norms[j] += abs<T>(
_X[i*_m+j]);
2208 for (
int i = 0; i<
_n; ++i) {
2214 template <
typename T>
2219 for (
int i = 0; i<
_n; ++i) {
2228 ones.
set(T(1.0/
_n));
2229 this->
mult(ones,mean,1.0,0.0);
2235 ones.
set(T(1.0/
_m));
2242 for (
int i = 0; i<
_n; ++i) {
2244 for (
int j = 0; j<
_m; ++j) {
2254 for (
int i = 0; i<
_n; ++i) {
2262 for (
int i = 0; i<
_n; ++i) {
2271 for (
int i = 0; i<
_n; ++i) {
2275 for (
int i = 0; i<
_n; ++i) {
2276 _X[i*
_m+j]+=a*row[i];
2321 const int sizeGroup) {
2322 for (
int i = 0; i<
_n; ++i) {
2324 for (j = 0; j<
_m-sizeGroup+1; j+=sizeGroup) {
2326 for (
int k = 0; k<sizeGroup; ++k)
2327 nrm +=
_X[i*
_m +j+k]*
_X[i*
_m +j+k];
2330 for (
int k = 0; k<sizeGroup; ++k)
2333 T
scal = (nrm-nu)/nrm;
2334 for (
int k = 0; k<sizeGroup; ++k)
2335 _X[i*
_m +j+k]*=scal;
2340 _X[j]=softThrs<T>(
_X[j],nu);
2345 const T thrs,
const int mode,
const T lambda1,
2346 const T lambda2,
const T lambda3,
const bool pos,
2347 const int numThreads) {
2349 int NUM_THREADS=
init_omp(numThreads);
2351 for (
int i = 0; i<NUM_THREADS; ++i) {
2356 #pragma omp parallel for private(i) 2357 for (i = 0; i<
_n; ++i) {
2359 int numT=omp_get_thread_num();
2395 assert(vec2.
_n ==
_n);
2397 for (
int i = 0; i<
_n; ++i) {
2398 for (
int j = 0; j<vec1.
_L; ++j) {
2399 _X[i*
_m+r[j]] += v[j]*X2[i];
2403 for (
int i = 0; i<
_n; ++i) {
2404 for (
int j = 0; j<vec1.
_L; ++j) {
2405 _X[i*
_m+r[j]] += alpha*v[j]*X2[i];
2411 template <
typename T>
2416 const int nn = vec1b.
n();
2417 const int size_A =
_n/nn;
2419 for (
int i = 0; i<nn; ++i) {
2433 for (
int i = 0; i<vec2.
_L; ++i) {
2434 for (
int j = 0; j<vec1.
_L; ++j) {
2435 _X[r2[i]*
_m+r[j]] += v[j]*v2[i];
2439 for (
int i = 0; i<vec2.
_L; ++i) {
2440 for (
int j = 0; j<vec1.
_L; ++j) {
2441 _X[r[i]*
_m+r[j]] += alpha*v[j]*v2[i];
2454 for (
int i = 0; i<vec2.
_L; ++i) {
2456 Xi.
add(vec1,v[i]*alpha);
2466 for (
int i = 0; i<vec1.
_L; ++i) {
2467 for (
int j = 0; j<vec1.
_L; ++j) {
2468 _X[r[i]*
_m+r[j]] += v[j]*v[i];
2472 for (
int i = 0; i<vec1.
_L; ++i) {
2473 for (
int j = 0; j<vec1.
_L; ++j) {
2474 _X[
_m*r[i]+r[j]] += alpha*v[j]*v[i];
2486 this->
mult(x,R,T(-1.0),T(1.0));
2491 while (normR > tol && k < itermax) {
2493 alpha = normR/P.
dot(AP);
2507 f.flags(std::ios_base::scientific);
2508 f.open(fileName, ofstream::trunc);
2509 std::cout <<
"Matrix written in " << fileName << std::endl;
2510 for (
int i = 0; i<
_n; ++i) {
2511 for (
int j = 0; j<
_m; ++j)
2512 f <<
_X[i*_m+j] <<
" ";
2521 if (ind.
n() !=
_n)
return;
2525 const int Ngroups=ind.
maxval();
2527 #pragma omp parallel for private(i) 2528 for (i = 1; i<=Ngroups; ++i) {
2531 for (
int j = 0; j<
_n; ++j)
2532 if (ind[j] == i) indicesGroup[count++]=j;
2535 for (
int j= 0; j<count; ++j) {
2536 this->
refCol(indicesGroup[j],col);
2544 weights.
scal(T(-2.0));
2549 weights.
scal(-sigma);
2552 weights.
mult(ones,den);
2556 Xm.
mult(weights,num);
2557 for (
int j= 0; j<count; ++j) {
2558 this->
refCol(indicesGroup[j],col);
2570 #pragma omp critical 2575 for (
int i = 0; i<
_n*
_m; ++i)
2576 if (
_X[i] != 0) ++count;
2579 #pragma omp critical 2585 for (
int i = 0; i<
_n; ++i) {
2587 for (
int j = 0; j<
_m; ++j) {
2588 if (
_X[i*_m+j] != 0) {
2589 v[count]=
_X[i*_m+j];
2611 #pragma omp critical 2616 for (
int i = 0; i<
_n*
_m; ++i)
2617 if (
_X[i] != 0) ++count;
2620 #pragma omp critical 2626 for (
int i = 0; i<
_m; ++i) {
2628 for (
int j = 0; j<
_n; ++j) {
2629 if (
_X[i+j*_m] != 0) {
2630 v[count]=
_X[j*_m+i];
2660 this->
mult(B,G,
true,
false);
2661 std::list<int> list;
2662 for (
int i = 0; i<G.
n(); ++i) {
2666 if (fmax < 0.995) list.push_back(i);
2670 for (
int i = 0; i<K; ++i) {
2677 for (std::list<int>::const_iterator it = list.begin();
2678 it != list.end(); ++it) {
2699 #pragma omp critical 2712 #pragma omp critical 2716 cblas_copy<T>(
_n,vec.
_X,1,
_X,1);
2726 printf(
"%s, %d\n",name,
_n);
2727 for (
int i = 0; i<
_n; ++i) {
2728 printf(
"%g ",
_X[i]);
2735 printf(
"%s, %d\n",name,
_n);
2736 for (
int i = 0; i<
_n; ++i) {
2737 printf(
"%g ",
_X[i]);
2744 printf(
"%s, %d\n",name,
_n);
2745 for (
int i = 0; i<
_n; ++i) {
2746 printf(
"%d ",
_X[i]);
2753 printf(
"%s, %d\n",name,
_n);
2754 for (
int i = 0; i<
_n; ++i) {
2755 printf(
"%d ",
_X[i] ? 1 : 0);
2764 for (
int j = 1; j<
_n; ++j) {
2778 for (
int j = 1; j<
_n; ++j) {
2790 return _X[this->
max()];
2795 return _X[this->
min()];
2800 return fabs(
_X[this->
fmax()]);
2805 return fabs(
_X[this->
fmin()]);
2808 template <
typename T>
2812 T step = (last-first)/(n-1);
2815 for (
int i = 1; i<
_n; ++i)
2817 for (
int i = 0; i<
_n; ++i)
2818 _X[i]=pow(T(10.0),
_X[i]);
2821 template <
typename T>
2824 for (
int i = 0; i<
_n; ++i)
2825 if (
_X[i] != T()) ++
sum;
2832 tmp.
logspace(n,
double(a),
double(b));
2836 for (
int i = 1; i<
_n-1; ++i) {
2837 int candidate=
static_cast<int>(floor(static_cast<double>(tmp[i])));
2838 _X[i]= candidate >
_X[i-1] ? candidate :
_X[i-1]+1;
2844 return cblas_iamax<T>(
_n,
_X,1);
2849 return cblas_iamin<T>(
_n,
_X,1);
2854 assert(i>=0 && i<
_n);
2860 assert(i>=0 && i<
_n);
2867 cblas_copy<T>(
_n,x.
_X,1,
_X,1);
2872 memset(
_X,0,
_n*
sizeof(T));
2877 if (
_n == n)
return;
2879 #pragma omp critical 2900 for (
int i = 0; i<
n; ++i)
2903 for (
int i = 0; i<
n; ++i) {
2904 const int ind=random() % size;
2906 table[ind]=table[size-1];
2913 for (
int i = 0; i<_n; ++i) _X[i]=normalDistrib<T>();
2926 for (
int i = 0; i<
_n; ++i) {
2929 }
else if (
_X[i] < -nu) {
2939 for (
int i = 0; i<
_n; ++i) {
2940 if (!(
_X[i] > nu ||
_X[i] < -nu)) {
2949 for (
int i = 0; i<
_n; ++i)
2955 for (
int i = 0; i<
_n; ++i)
2961 for (
int i = 0; i<
_n; ++i)
2968 for (
int i = 0; i<
_n; ++i)
2969 if (abs<T>(
_X[i]) < nu)
2974 for (
int i = 0; i<
_n; ++i) {
2975 if (
_X[i] < 0)
_X[i]=0;
2981 for (
int i = 0; i<
_n; ++i) {
2982 if (!
_X[i])
return false;
2989 for (
int i = 0; i<
_n; ++i) {
2990 if (
_X[i])
return false;
2998 for (
int i = 0; i<
_n; ++i)
_X[i]=val;
3003 return cblas_nrm2<T>(
_n,
_X,1);
3008 return cblas_dot<T>(
_n,
_X,1,
_X,1);
3014 return cblas_dot<T>(
_n,
_X,1,x.
_X,1);
3021 const int* r = x.
_r;
3022 for (
int i = 0; i<x.
_L; ++i) {
3023 sum +=
_X[r[i]]*v[i];
3031 cblas_axpy<T>(
_n,a,x.
_X,1,
_X,1);
3038 for (
int i = 0; i<x.
_L; ++i)
3041 for (
int i = 0; i<x.
_L; ++i)
3048 for (
int i = 0; i<
_n; ++i)
_X[i]+=a;
3059 for (
int i = 0; i<x.
_L; ++i)
3130 if (norm > T(1.0))
scal(1.0/norm);
3137 const int n =
static_cast<int>(sqrt(static_cast<T>(
_n)));
3139 for (
int i = 0; i<4; ++i) count[i]=0;
3141 for (
int j = 0; j<
n; ++j) {
3142 offsetx= (offsetx+1) % 2;
3144 for (
int k = 0; k<
n; ++k) {
3145 offsety= (offsety+1) % 2;
3146 meanv[2*offsetx+offsety]+=
_X[j*n+k];
3147 count[2*offsetx+offsety]++;
3150 for (
int i = 0; i<4; ++i)
3151 meanv[i] /= count[i];
3153 for (
int j = 0; j<
n; ++j) {
3154 offsetx= (offsetx+1) % 2;
3156 for (
int k = 0; k<
n; ++k) {
3157 offsety= (offsety+1) % 2;
3158 _X[j*n+k]-=meanv[2*offsetx+offsety];
3162 const int V = meanv.
n();
3163 const int sizePatch=
_n/V;
3164 for (
int j = 0; j<V; ++j) {
3166 for (
int k = 0; k<sizePatch; ++k) {
3167 mean+=
_X[sizePatch*j+k];
3170 for (
int k = 0; k<sizePatch; ++k) {
3181 const int V = meanv.
n();
3182 const int sizePatch=
_n/V;
3183 for (
int j = 0; j<V; ++j) {
3185 for (
int k = 0; k<sizePatch; ++k) {
3186 mean+=
_X[sizePatch*j+k];
3189 for (
int k = 0; k<sizePatch; ++k) {
3190 if (mask[sizePatch*j+k])
3199 const int sizePatch=
_n/V;
3200 for (
int j = 0; j<V; ++j) {
3202 for (
int k = 0; k<sizePatch; ++k) {
3203 mean+=
_X[sizePatch*j+k];
3206 for (
int k = 0; k<sizePatch; ++k) {
3218 for (
int i = 0; i<
_n; ++i) {
3219 if (
_X[i] > 1e-20) {
3220 if (prY[i] < 1e-60) {
3223 sum +=
_X[i]*log_alt<T>(
_X[i]/prY[i]);
3228 sum += T(-1.0) + Y.
sum();
3236 const int n =
static_cast<int>(sqrt(static_cast<T>(
_n)));
3238 for (
int j = 0; j<
n; ++j) {
3239 offsetx= (offsetx+1) % 2;
3241 for (
int k = 0; k<
n; ++k) {
3242 offsety= (offsety+1) % 2;
3243 _X[j*n+k]+=meanv[2*offsetx+offsety];
3247 const int V = meanv.
n();
3248 const int sizePatch=
_n/V;
3249 for (
int j = 0; j<V; ++j) {
3251 for (
int k = 0; k<sizePatch; ++k) {
3261 return this->
sum()/
_n;
3268 for (
int i = 0; i<
_n; ++i) {
3273 return sqr_alt<T>(
std);
3278 return cblas_scal<T>(
_n,a,
_X,1);
3283 for (
int i = 0; i<
_n; ++i)
_X[i]=-
_X[i];
3293 for (
int i=0; i<_n; ++i) _X[i]=alt_log<T>(
_X[i]);
3298 for (
int i = 0; i<
_n; ++i) {
3301 }
else if (
_X[i] < 30) {
3302 _X[i]= alt_log<T>( T(1.0) + exp_alt<T>(
_X[i] ) );
3314 }
else if (max < -30) {
3319 return alt_log<T>(this->
sum());
3325 return cblas_asum<T>(
_n,
_X,1);
3330 for (
int i = 0; i<
_n; ++i)
3331 if (
_X[i] != 0) ++count;
3338 for (
int i = 1; i<
_n; ++i) {
3339 sum += abs<T>(
_X[i]-
_X[i-1]);
3346 for (
int i = 0; i<
_n; ++i) sum +=
_X[i];
3352 T* prSign=signs.
rawX();
3353 for (
int i = 0; i<
_n; ++i) {
3357 prSign[i] =
_X[i] > 0 ? 1.0 : -1.0;
3365 const T thrs,
const bool simplex)
const {
3370 vAbs<T>(
_n,out.
_X,out.
_X);
3372 T norm1 = out.
sum();
3373 if (norm1 <= thrs) {
3374 if (!simplex) out.
copy(*
this);
3385 swap(prU[0],prU[sizeU/2]);
3390 for (
int i = 1; i<sizeU; ++i) {
3391 if (prU[i] >= pivot) {
3393 swap(prU[sizeG++],prU[i]);
3397 if (sum + sumG - pivot*(sum_card + sizeG) <= thrs) {
3407 T lambda = (sum-thrs)/sum_card;
3418 const T thrs,
const bool residual)
const {
3424 vAbs<T>(
_n,out.
_X,out.
_X);
3427 for (
int i = 0; i<
_n; ++i) keys[i]=i;
3428 out.
sort2(keys,
false);
3432 for (
int i = 0; i<
_n; ++i) {
3433 const T lambda_old=lambda;
3434 const T fact=weights[keys[i]]*weights[keys[i]];
3437 sum1 += fact*lambda;
3438 if (sum1 - lambda*sum2 >= thrs) {
3445 lambda=
MAX(0,(sum1-thrs)/sum2);
3448 for (
int i = 0; i<
_n; ++i) {
3449 out.
_X[i]=
_X[i] > 0 ?
MIN(
_X[i],lambda*weights[i]) :
MAX(
_X[i],-lambda*weights[i]);
3452 for (
int i = 0; i<
_n; ++i) {
3453 out.
_X[i]=
_X[i] > 0 ?
MAX(0,
_X[i]-lambda*weights[i]) :
MIN(0,
_X[i]+lambda*weights[i]);
3459 template <
typename T>
3465 for (
int i = 0; i<
_n; ++i) {
3467 const T val = y[i]*
_X[i];
3471 }
else if (val < -1.0) {
3476 mean = this->
mean();
3477 thrs= mean * _n/(_n-n_seuils);
3481 template <
typename T>
3488 for (
int i = 0; i<
_n; ++i) {
3490 if (labels[i]==clas) {
3502 mean = this->
mean();
3503 thrs= mean * _n/(_n-n_seuils);
3507 template <
typename T>
3509 const T lambda2,
const T lambda3,
const bool pos) {
3513 }
else if (mode == 2) {
3515 if (lambda1 > 1e-10) {
3516 this->
scal(lambda1);
3517 this->
l1l2project(out,thrs,2.0/(lambda1*lambda1),pos);
3518 this->
scal(T(1.0/lambda1));
3519 out.
scal(T(1.0/lambda1));
3523 out.
scal(sqrt(thrs));
3525 }
else if (mode == 3) {
3528 }
else if (mode == 4) {
3536 out.
scal(sqr_alt<T>(thrs/nrm));
3537 }
else if (mode == 5) {
3546 }
else if (mode==6) {
3551 if (lambda1 < 1e-10) {
3556 out.
scal(sqrt(thrs));
3557 }
else if (lambda1 > 0.999999) {
3560 this->
sparseProject(out,thrs/(1.0-lambda1),2,lambda1/(1-lambda1),0,0,pos);
3566 template <
typename T>
3572 this->
l1l2project(out,thrs,2.0/(gamma*gamma),pos);
3573 this->
scal(T(1.0/gamma));
3574 out.
scal(T(1.0/gamma));
3575 }
else if (mode == 2) {
3578 }
else if (mode == 3) {
3592 template <
typename T>
3600 vAbs<T>(
_n,out.
_X,out.
_X);
3602 T norm = out.
sum() + gamma*out.
nrm2sq();
3604 if (!pos) out.
copy(*
this);
3617 swap(prU[0],prU[sizeU/2]);
3620 T sumG=pivot+0.5*gamma*pivot*pivot;
3622 for (
int i = 1; i<sizeU; ++i) {
3623 if (prU[i] >= pivot) {
3624 sumG += prU[i]+0.5*gamma*prU[i]*prU[i];
3625 swap(prU[sizeG++],prU[i]);
3628 if (sum + sumG - pivot*(1+0.5*gamma*pivot)*(sum_card + sizeG) <
3629 thrs*(1+gamma*pivot)*(1+gamma*pivot)) {
3639 T a = gamma*gamma*thrs+0.5*gamma*sum_card;
3640 T b = 2*gamma*thrs+sum_card;
3642 T delta = b*b-4*a*c;
3643 T lambda = (-b+sqrt(delta))/(2*a);
3650 out.
scal(T(1.0/(1+lambda*gamma)));
3653 template <
typename T>
3661 return sign3 ? 0 : c2;
3663 return sign3 ? -c2-c1 : -c1;
3667 return sign3 ? c1 : c1+c2;
3669 return sign3 ? -c2 : 0;
3674 template <
typename T>
3676 const T lambda1,
const T lambda2,
const T lambda3,
3677 const bool penalty) {
3688 T* pr_gamma = gamma.
rawX();
3690 T* pr_Du = Du.
rawX();
3691 T* pr_DDu = DDu.
rawX();
3693 T* pr_scores = scores.
rawX();
3697 int* pr_ind = ind.
rawX();
3698 bool* pr_signs = signs.
rawX();
3701 T sumBeta = this->
sum();
3704 pr_gamma[0]=sumBeta/K;
3706 alpha.
set(pr_gamma[0]);
3709 for (
int j = K-2; j>=0; --j)
3710 pr_DtR[j] += pr_DtR[j+1];
3714 pr_signs[0] = pr_DtR[0] > 0;
3716 int currentInd=this->
fmax();
3717 T currentLambda=abs<T>(pr_DtR[currentInd]);
3718 bool newAtom =
true;
3721 for (
int i = 1; i<K; ++i) {
3724 if (penalty && currentLambda <= lambda2)
break;
3729 scores.
scal(T(1.0/(1.0+lambda3*currentLambda/lambda2)));
3730 if (lambda1*scores.
asum()+lambda2*scores.
afused()+0.5*
3731 lambda3*scores.
nrm2sq() >= T(1.0))
break;
3737 for (j = 1; j<i; ++j)
3738 if (pr_ind[j] > currentInd)
break;
3739 for (
int k = i; k>j; --k) {
3740 pr_ind[k]=pr_ind[k-1];
3742 pr_signs[k]=pr_signs[k-1];
3744 pr_ind[j]=currentInd;
3745 pr_signs[j]=pr_DtR[currentInd] > 0;
3746 pr_c[j-1]=T(1.0)/(pr_ind[j]-pr_ind[j-1]);
3747 pr_c[j]=T(1.0)/(pr_ind[j+1]-pr_ind[j]);
3751 pr_u[0]= pr_signs[1] ? -pr_c[0] : pr_c[0];
3753 pr_u[1]=pr_signs[1] ? pr_c[0]+pr_c[1] : -pr_c[0]-pr_c[1];
3755 pr_u[1]=pr_signs[1] ? pr_c[0]+pr_c[1] : -pr_c[0]-pr_c[1];
3756 pr_u[1]+=pr_signs[2] ? -pr_c[1] : pr_c[1];
3757 for (
int j = 2; j<i; ++j) {
3758 pr_u[j]=2*fusedHomotopyAux<T>(pr_signs[j-1],
3759 pr_signs[j],pr_signs[j+1], pr_c[j-1],pr_c[j]);
3761 pr_u[i] = pr_signs[i-1] ? -pr_c[i-1] : pr_c[i-1];
3762 pr_u[i] += pr_signs[i] ? pr_c[i-1]+pr_c[i] : -pr_c[i-1]-pr_c[i];
3767 for (
int k = 1; k<pr_ind[1]; ++k)
3769 for (
int j = 1; j<=i; ++j) {
3770 pr_Du[pr_ind[j]]=pr_Du[pr_ind[j]-1]+pr_u[j];
3771 for (
int k = pr_ind[j]+1; k<pr_ind[j+1]; ++k)
3772 pr_Du[k]=pr_Du[pr_ind[j]];
3777 for (
int j = K-2; j>=0; --j)
3778 pr_DDu[j] += pr_DDu[j+1];
3783 max_step1 = currentLambda-lambda2;
3789 for (
int j = 1; j<=i; ++j) {
3790 T ratio = -pr_gamma[pr_ind[j]]/pr_u[j];
3791 if (ratio > 0 && ratio <= max_step2) {
3798 for (
int j = 1; j<K; ++j) {
3799 T sc1 = (currentLambda-pr_DtR[j])/(T(1.0)-pr_DDu[j]);
3800 T sc2 = (currentLambda+pr_DtR[j])/(T(1.0)+pr_DDu[j]);
3803 pr_scores[j]=
MIN(sc1,sc2);
3805 for (
int j = 0; j<=i; ++j) {
3808 currentInd = scores.
fmin();
3809 max_step3 = pr_scores[currentInd];
3810 T step =
MIN(max_step1,
MIN(max_step3,max_step2));
3811 if (step == 0 || step ==
INFINITY)
break;
3814 for (
int j = 0; j<=i; ++j) {
3815 pr_gamma[pr_ind[j]]+=step*pr_u[j];
3818 this->
add(DDu,-step);
3819 currentLambda -= step;
3820 if (step == max_step2) {
3822 for (
int k = step_out; k<=i; ++k)
3823 pr_ind[k]=pr_ind[k+1];
3825 for (
int k = step_out; k<=i; ++k)
3826 pr_signs[k]=pr_signs[k+1];
3827 pr_c[step_out-1]=T(1.0)/(pr_ind[step_out]-pr_ind[step_out-1]);
3828 pr_c[step_out]=T(1.0)/(pr_ind[step_out+1]-pr_ind[step_out]);
3838 alpha.
scal(T(1.0/(1.0+lambda3)));
3841 alpha.
scal(T(1.0/(1.0+lambda3*currentLambda/lambda2)));
3845 template <
typename T>
3847 const int itermax) {
3848 T* pr_alpha= alpha.
rawX();
3850 const int K = alpha.
n();
3852 T total_alpha =alpha.
sum();
3854 for (
int i = K-2; i>=0; --i)
3855 pr_beta[i]+=pr_beta[i+1];
3857 for (
int i = 0; i<itermax; ++i) {
3861 T gamma_old=pr_alpha[0];
3862 pr_alpha[0]=(K*gamma_old+pr_beta[0]-
3864 T diff = pr_alpha[0]-gamma_old;
3866 sum_alpha += pr_alpha[0];
3867 total_alpha +=K*diff;
3870 for (
int j = 1; j<K; ++j) {
3871 pr_alpha[j]+=sum_diff;
3872 T gamma_old=pr_alpha[j]-pr_alpha[j-1];
3873 T gamma_new=
softThrs((K-j)*gamma_old+pr_beta[j]-
3874 (total_alpha-sum_alpha),lambda2)/(K-j);
3875 pr_alpha[j]=pr_alpha[j-1]+gamma_new;
3876 T diff = gamma_new-gamma_old;
3878 sum_alpha+=pr_alpha[j];
3879 total_alpha +=(K-j)*diff;
3887 template <
typename T>
3898 template <
typename T>
3904 template <
typename T>
3910 template <
typename T>
3913 out.
sort2(key,mode);
3916 template <
typename T>
3919 int n =
static_cast<int>(sqrt(static_cast<T>(sizePatch)));
3922 for (
int i = 0; i<
n; ++i) {
3923 const int step = (i % 2) ? 1 : 2;
3924 const int off = (i % 2) ? 0 : 1;
3925 for (
int j = off; j<
n; j+=step) {
3930 for (
int i = 0; i<
n; ++i) {
3932 const int off = (i % 2) ? 1 : 0;
3933 for (
int j = off; j<
n; j+=step) {
3934 _X[sizePatch+i*n+j]=0;
3938 for (
int i = 0; i<
n; ++i) {
3939 const int step = (i % 2) ? 2 : 1;
3941 for (
int j = off; j<
n; j+=step) {
3942 _X[2*sizePatch+i*n+j]=0;
3945 }
else if (offset == 1) {
3947 for (
int i = 0; i<
n; ++i) {
3948 const int step = (i % 2) ? 2 : 1;
3949 const int off = (i % 2) ? 1 : 0;
3950 for (
int j = off; j<
n; j+=step) {
3955 for (
int i = 0; i<
n; ++i) {
3957 const int off = (i % 2) ? 0 : 1;
3958 for (
int j = off; j<
n; j+=step) {
3959 _X[sizePatch+i*n+j]=0;
3963 for (
int i = 0; i<
n; ++i) {
3964 const int step = (i % 2) ? 1 : 2;
3966 for (
int j = off; j<
n; j+=step) {
3967 _X[2*sizePatch+i*n+j]=0;
3970 }
else if (offset == 2) {
3972 for (
int i = 0; i<
n; ++i) {
3973 const int step = (i % 2) ? 1 : 2;
3975 for (
int j = off; j<
n; j+=step) {
3980 for (
int i = 0; i<
n; ++i) {
3982 const int off = (i % 2) ? 0 : 1;
3983 for (
int j = off; j<
n; j+=step) {
3984 _X[sizePatch+i*n+j]=0;
3988 for (
int i = 0; i<
n; ++i) {
3989 const int step = (i % 2) ? 2 : 1;
3990 const int off = (i % 2) ? 1 : 0;
3991 for (
int j = off; j<
n; j+=step) {
3992 _X[2*sizePatch+i*n+j]=0;
3995 }
else if (offset == 3) {
3997 for (
int i = 0; i<
n; ++i) {
3998 const int step = (i % 2) ? 2 : 1;
4000 for (
int j = off; j<
n; j+=step) {
4005 for (
int i = 0; i<
n; ++i) {
4007 const int off = (i % 2) ? 1 : 0;
4008 for (
int j = off; j<
n; j+=step) {
4009 _X[sizePatch+i*n+j]=0;
4013 for (
int i = 0; i<
n; ++i) {
4014 const int step = (i % 2) ? 1 : 2;
4015 const int off = (i % 2) ? 0 : 1;
4016 for (
int j = off; j<
n; j+=step) {
4017 _X[2*sizePatch+i*n+j]=0;
4030 for (
int i = 0; i<
_n; ++i) {
4040 template <
typename T>
4044 for (
int i = 0; i<
_n; ++i) {
4046 out[pointer++]=
_X[i];
4051 template <
typename T>
4055 for (
int i = 0; i<mask.
n(); ++i)
4059 for (
int i = 0; i<
_n; ++i) {
4061 for (
int j = 0; j<_m; ++j) {
4063 out[i*count+pointer]=
_X[i*_m+j];
4079 int m,
int n,
int nzmax) :
4080 _externAlloc(true), _v(v), _r(r), _pB(pB), _pE(pE), _m(m),
_n(n), _nzmax(nzmax)
4086 #pragma omp critical 4101 template <
typename T>
4106 memcpy(
_pB,mat.
_pB,(
_n+1)*
sizeof(
int));
4122 vec.
_L=
_pE[i]-_pB[i];
4128 cerr << name << endl;
4129 cerr <<
_m <<
" x " <<
_n <<
" , " <<
_nzmax << endl;
4130 for (
int i = 0; i<
_n; ++i) {
4131 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4132 cerr <<
"(" <<
_r[j] <<
"," << i <<
") = " <<
_v[j] << endl;
4137 template<
typename T>
4139 const int num_col=(index/
_m);
4140 const int num_row=index -num_col*
_m;
4142 for (
int j =
_pB[num_col]; j<
_pB[num_col+1]; ++j) {
4143 if (
_r[j]==num_row) {
4150 template<
typename T>
4154 for (
int i =
_pB[index]; i<
_pB[index+1]; ++i)
4158 template<
typename T>
4160 const group& gr = groups[i];
4161 const int N = gr.size();
4165 for (group::const_iterator it = gr.begin(); it != gr.end(); ++it) {
4174 return cblas_asum<T>(
_pB[
_n],
_v,1);
4182 template <
typename T>
4189 template <
typename T>
4196 template <
typename T>
4222 const int n,
const int nzmax) {
4223 if (n ==
_n && m ==
_m && nzmax ==
_nzmax)
return;
4229 #pragma omp critical 4233 _pB =
new int[
_n+1];
4236 for (
int i = 0; i<=
_n; ++i)
_pB[i]=0;
4241 cblas_scal<T>(
_pB[
_n],a,
_v,1);
4245 template <
typename T>
4247 const T alpha,
const T beta)
const {
4254 const T* prX = x.
rawX();
4255 for (
int i = 0; i<
_n; ++i) {
4257 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4258 sum+=
_v[j]*prX[
_r[j]];
4265 template <
typename T>
4267 const T alpha,
const T beta)
const {
4276 for (
int i = 0; i<
_n; ++i) {
4278 prY[i] += alpha*x.
dot(col);
4284 template <
typename T>
4286 const T alpha,
const T beta)
const {
4293 const T* prX = x.
rawX();
4294 for (
int i = 0; i<
_n; ++i) {
4295 T sca=alpha* prX[i];
4296 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4297 y[
_r[j]] += sca*
_v[j];
4304 template <
typename T>
4306 const T alpha,
const T beta)
const {
4314 for (
int i = 0; i<x.
L(); ++i) {
4316 T val = alpha * x.
v(i);
4317 for (
int j =
_pB[ind]; j<
_pE[ind]; ++j) {
4318 prY[
_r[j]] += val *
_v[j];
4324 template <
typename T>
4326 const bool transA,
const bool transB,
4327 const T a,
const T b)
const {
4338 for (
int i = 0; i<
_n; ++i) {
4352 for (
int i = 0; i<
_n; ++i) {
4368 for (
int i = 0; i<B.
m(); ++i) {
4371 this->
mult(row,col,a,T(1.0));
4382 for (
int i = 0; i<B.
n(); ++i) {
4385 this->
mult(colB,colC,a,T(1.0));
4392 template <
typename T>
4394 const bool transA,
const bool transB,
4395 const T a,
const T b)
const {
4406 for (
int i = 0; i<
_n; ++i) {
4420 for (
int i = 0; i<
_n; ++i) {
4436 for (
int i = 0; i<
_n; ++i) {
4450 for (
int i = 0; i<B.
n(); ++i) {
4453 this->
mult(colB,colC,a);
4460 template <
typename T>
4462 const bool transA,
const bool transB,
4463 const T a,
const T b)
const {
4464 B.
mult(*
this,C,transB,transA,a,b);
4467 template <
typename T>
4470 for (
int i = 0; i<
_n; ++i)
4471 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4472 sum+=
_v[j]*x(
_r[j],j);
4478 template <
typename T>
4482 for (
int i = 0; i<
_n; ++i) {
4483 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4486 }
else if (
_r[j] > ind) {
4493 template <
typename T>
4496 const T* pr_vec = vec.
rawX();
4498 for (
int i = 0; i<
_n; ++i)
4499 for (
int j =
_pB[i]; j<
_pE[i]; ++j)
4500 _v[j] += pr_vec[
_r[j]];
4502 for (
int i = 0; i<
_n; ++i)
4503 for (
int j =
_pB[i]; j<
_pE[i]; ++j)
4504 _v[j] += a*pr_vec[
_r[j]];
4508 template <
typename T>
4510 const Vector<T>& vec,
const T* weights,
const T a) {
4511 const T* pr_vec = vec.
rawX();
4513 for (
int i = 0; i<
_n; ++i)
4514 for (
int j =
_pB[i]; j<
_pE[i]; ++j)
4515 _v[j] += pr_vec[
_r[j]]*weights[j-
_pB[i]];
4517 for (
int i = 0; i<
_n; ++i)
4518 for (
int j =
_pB[i]; j<
_pE[i]; ++j)
4519 _v[j] += a*pr_vec[
_r[j]]*weights[j-
_pB[i]];
4523 template <
typename T>
4528 for (
int i = 0; i<
_n; ++i) {
4543 T* aatT=
new T[NUM_THREADS*K*K];
4544 for (j = 0; j<NUM_THREADS*K*K; ++j) aatT[j]=T();
4546 #pragma omp parallel for private(i,j,k) 4547 for (i = 0; i<M; ++i) {
4549 int numT=omp_get_thread_num();
4553 T* write_area=aatT+numT*K*K;
4554 for (j =
_pB[i]; j<
_pE[i]; ++j) {
4555 for (k =
_pB[i]; k<=j; ++k) {
4556 write_area[
_r[j]*K+
_r[k]]+=
_v[j]*
_v[k];
4561 cblas_copy<T>(K*K,aatT,1,aat.
_X,1);
4562 for (i = 1; i<NUM_THREADS; ++i)
4563 cblas_axpy<T>(K*K,1.0,aatT+K*K*i,1,aat.
_X,1);
4568 template <
typename T>
4574 for (
int i = 0; i<
_n; ++i) {
4592 T* aatT=
new T[NUM_THREADS*K*K];
4593 for (j = 0; j<NUM_THREADS*K*K; ++j) aatT[j]=T();
4595 #pragma omp parallel for private(i,j,k) 4596 for (i = 0; i<M; ++i) {
4597 int ii = indices[i];
4599 int numT=omp_get_thread_num();
4603 T* write_area=aatT+numT*K*K;
4604 for (j =
_pB[ii]; j<
_pE[ii]; ++j) {
4605 for (k =
_pB[ii]; k<=j; ++k) {
4606 write_area[
_r[j]*K+
_r[k]]+=
_v[j]*
_v[k];
4611 cblas_copy<T>(K*K,aatT,1,aat.
_X,1);
4612 for (i = 1; i<NUM_THREADS; ++i)
4613 cblas_axpy<T>(K*K,1.0,aatT+K*K*i,1,aat.
_X,1);
4628 T* aatT=
new T[NUM_THREADS*K*K];
4629 for (j = 0; j<NUM_THREADS*K*K; ++j) aatT[j]=T();
4631 #pragma omp parallel for private(i,j,k) 4632 for (i = 0; i<M; ++i) {
4634 int numT=omp_get_thread_num();
4638 T* write_area=aatT+numT*K*K;
4639 for (j =
_pB[i]; j<
_pE[i]; ++j) {
4640 for (k =
_pB[i]; k<=j; ++k) {
4641 write_area[
_r[j]*K+
_r[k]]+=w.
_X[i]*
_v[j]*
_v[k];
4646 cblas_copy<T>(K*K,aatT,1,aat.
_X,1);
4647 for (i = 1; i<NUM_THREADS; ++i)
4648 cblas_axpy<T>(K*K,1.0,aatT+K*K*i,1,aat.
_X,1);
4664 T* XatT=
new T[NUM_THREADS*n*K];
4665 for (j = 0; j<NUM_THREADS*n*K; ++j) XatT[j]=T();
4667 #pragma omp parallel for private(i,j) 4668 for (i = 0; i<M; ++i) {
4670 int numT=omp_get_thread_num();
4674 T* write_area=XatT+numT*n*K;
4675 for (j =
_pB[i]; j<
_pE[i]; ++j) {
4676 cblas_axpy<T>(
n,
_v[j],X.
_X+i*
n,1,write_area+
_r[j]*
n,1);
4680 cblas_copy<T>(n*K,XatT,1,XAt.
_X,1);
4681 for (i = 1; i<NUM_THREADS; ++i)
4682 cblas_axpy<T>(n*K,1.0,XatT+n*K*i,1,XAt.
_X,1);
4697 T* XatT=
new T[NUM_THREADS*n*K];
4698 for (j = 0; j<NUM_THREADS*n*K; ++j) XatT[j]=T();
4700 #pragma omp parallel for private(i,j) 4701 for (i = 0; i<M; ++i) {
4702 int ii = indices[i];
4704 int numT=omp_get_thread_num();
4708 T* write_area=XatT+numT*n*K;
4709 for (j =
_pB[ii]; j<
_pE[ii]; ++j) {
4710 cblas_axpy<T>(
n,
_v[j],X.
_X+i*
n,1,write_area+
_r[j]*
n,1);
4714 cblas_copy<T>(n*K,XatT,1,XAt.
_X,1);
4715 for (i = 1; i<NUM_THREADS; ++i)
4716 cblas_axpy<T>(n*K,1.0,XatT+n*K*i,1,XAt.
_X,1);
4729 assert(numRepX*Mx == M);
4732 int NUM_THREADS=
init_omp(numThreads);
4733 T* XatT=
new T[NUM_THREADS*n*K];
4734 for (j = 0; j<NUM_THREADS*n*K; ++j) XatT[j]=T();
4736 #pragma omp parallel for private(i,j,l) 4737 for (i = 0; i<Mx; ++i) {
4739 int numT=omp_get_thread_num();
4743 T * write_area=XatT+numT*n*K;
4744 for (l = 0; l<numRepX; ++l) {
4745 int ind=numRepX*i+l;
4747 for (j =
_pB[ind]; j<
_pE[ind]; ++j) {
4748 cblas_axpy<T>(
n,w.
_X[ind]*
_v[j],X.
_X+i*
n,1,write_area+
_r[j]*
n,1);
4753 cblas_copy<T>(n*K,XatT,1,XAt.
_X,1);
4754 for (i = 1; i<NUM_THREADS; ++i)
4755 cblas_axpy<T>(n*K,1.0,XatT+n*K*i,1,XAt.
_X,1);
4764 for (
int i=0; i<
_n; ++i) {
4765 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4777 for (
int i=0; i<
_n; ++i) {
4778 for (
int j =
_pB[i]; j<
_pE[i]; ++j) {
4788 const int M = rM.
n();
4789 const int L = rM.
m();
4790 const int*
r = rM.
X();
4791 const T*
v = vM.
X();
4793 for (
int i = 0; i<M*L; ++i)
if (r[i] != -1) ++count;
4796 for (
int i = 0; i<M; ++i) {
4798 for (
int j = 0; j<L; ++j) {
4799 if (r[i*L+j] == -1)
break;
4801 _r[count++]=r[i*L+j];
4811 const int M = vM.
n();
4812 const int L = vM.
m();
4814 const T*
v = vM.
X();
4816 for (
int i = 0; i<L; ++i)
if (r[i] != -1) ++LL;
4819 for (
int i = 0; i<M; ++i) {
4821 for (
int j = 0; j<LL; ++j) {
4831 template <
typename T>
4835 for (
int i = 0; i<
_n; ++i) {
4841 template <
typename T>
4845 for (
int i = 0; i<
_n; ++i) {
4847 norms[i] =
static_cast<T
>(col.
length());
4851 template <
typename T>
4855 for (
int i = 0; i<
_n; ++i) {
4857 norms[i] =col.
asum();
4874 #pragma omp critical 4892 return cblas_asum<T>(
_L,
_v,1);
4897 return cblas_dot<T>(
_L,
_v,1,
_v,1);
4902 return cblas_nrm2<T>(
_L,
_v,1);
4913 std::cerr << name << std::endl;
4914 std::cerr <<
_nzmax << std::endl;
4915 for (
int i = 0; i<
_L; ++i)
4916 cerr <<
"(" <<
_r[i] <<
", " <<
_v[i] <<
")" << endl;
4936 template <
typename T>
4941 while (countI <
_L && countJ < vec.
_L) {
4942 const int rI =
_r[countI];
4943 const int rJ = vec.
_r[countJ];
4946 }
else if (rJ > rI) {
4949 sum+=
_v[countI]*vec.
_v[countJ];
4977 #pragma omp critical 4986 SpMatrix<T>& out,
const int m,
const int n)
const {
4988 cblas_copy<T>(
_L,
_v,1,out.
_v,1);
4992 int* out_pB=out.
_pB;
4993 out_pB[0]=current_col;
4994 for (
int i = 0; i<
_L; ++i) {
4996 if (col > current_col) {
4997 out_pB[current_col+1]=i;
5001 out_r[i]=
_r[i]-col*m;
5004 for (current_col++ ; current_col < n+1; ++current_col)
5005 out_pB[current_col]=_L;
5012 for (
int i = 0; i<
_L; ++i)
5032 const bool high_memory) {
5033 if (high_memory) _DtX =
new Matrix<T>();
5034 this->setMatrices(D,high_memory);
5039 const bool high_memory) {
5040 if (high_memory) _DtX =
new Matrix<T>();
5041 this->setMatrices(D,X,high_memory);
5045 const bool high_memory) {
5046 _high_memory=high_memory;
5050 D.
mult(X,*_DtX,
true,
false);
5060 const Matrix<T>& D,
const bool high_memory) {
5061 _high_memory=high_memory;
5077 _DtX->copyCol(i,DtXi);
5081 _D->multTrans(Xi,DtXi);
5082 if (_addDiag && _m == _n) DtXi[i] += _addDiag;
5089 _DtX->extract_rawCol(i,DtXi);
5094 _D->multTrans(Xi,vDtXi);
5095 if (_addDiag && _m == _n) DtXi[i] += _addDiag;
5102 _DtX->add_rawCol(i,DtXi,a);
5107 _D->multTrans(Xi,vDtXi,a,T(1.0));
5108 if (_addDiag && _m == _n) DtXi[i] += a*_addDiag;
5115 _DtX->addDiag(diag);
5124 return (*_DtX)[index];
5126 const int index2=index/this->_m;
5127 const int index1=index-this->_m*index2;
5129 _D->refCol(index1,col1);
5130 _X->refCol(index2,col2);
5131 return col1.
dot(col2);
5137 const int index2)
const {
5139 return (*_DtX)(index1,index2);
5142 _D->refCol(index1,col1);
5143 _X->refCol(index2,col2);
5144 return col1.
dot(col2);
5154 for (
int i = 0; i <_m; ++i) {
5157 diag[i] = col1.
dot(col2);
5168 void inline convertIndicesI(
Vector<int>& ind)
const;
5169 void inline convertIndicesJ(
Vector<int>& ind)
const;
5170 int inline n()
const {
return _indicesJ.n(); };
5171 int inline m()
const {
return _indicesI.n(); };
5172 void inline extract_rawCol(
const int i, T* pr)
const;
5174 inline void copyCol(
const int i,
Vector<T>& DtXi)
const;
5176 inline void add_rawCol(
const int i, T* DtXi,
const T a)
const;
5178 inline void diag(
Vector<T>& diag)
const;
5179 inline T operator()(
const int index1,
const int index2)
const;
5187 template <
typename T>
5190 _indicesI.copy(indI);
5191 _indicesJ.copy(indJ);
5196 int* pr_ind = ind.
rawX();
5197 for (
int i = 0; i<ind.
n(); ++i) {
5198 if (pr_ind[i] == -1)
break;
5199 pr_ind[i]=_indicesI[pr_ind[i]];
5205 int* pr_ind = ind.
rawX();
5206 for (
int i = 0; i<ind.
n(); ++i) {
5207 if (pr_ind[i] == -1)
break;
5208 pr_ind[i]=_indicesJ[pr_ind[i]];
5213 int* pr_ind=_indicesI.rawX();
5214 int* pr_ind2=_indicesJ.rawX();
5215 for (
int j = 0; j<_indicesI.n(); ++j) {
5216 pr[j]=(*_matrix)(pr_ind[j],pr_ind2[i]);
5222 this->extract_rawCol(i,DtXi.
rawX());
5227 int* pr_ind=_indicesI.rawX();
5228 int* pr_ind2=_indicesJ.rawX();
5229 for (
int j = 0; j<_indicesI.n(); ++j) {
5230 pr[j]+=a*(*_matrix)(pr_ind[j],pr_ind2[i]);
5235 T* pr = diag.
rawX();
5236 int* pr_ind=_indicesI.rawX();
5237 for (
int j = 0; j<_indicesI.n(); ++j) {
5238 pr[j]=(*_matrix)(pr_ind[j],pr_ind[j]);
5243 const int index2)
const {
5244 return (*_matrix)(_indicesI[index1],_indicesJ[index2]);
5250 ShiftMatrix(
const AbstractMatrixB<T>& inputmatrix,
const int shifts,
const bool center =
false) : _shifts(shifts), _inputmatrix(&inputmatrix), _centered(false) {
5251 _m=_inputmatrix->m()-shifts+1;
5252 _n=_inputmatrix->n()*shifts;
5253 if (center) this->center();
5255 int n()
const {
return _n; };
5256 int m()
const {
return _m; };
5260 const T alpha = 1.0,
const T beta = 0.0)
const;
5264 const T alpha = 1.0,
const T beta = 0.0)
const;
5267 const T alpha = 1.0,
const T beta = 0.0)
const;
5271 const bool transA =
false,
const bool transB =
false,
5272 const T a = 1.0,
const T b = 0.0)
const;
5275 const bool transA =
false,
const bool transB =
false,
5276 const T a = 1.0,
const T b = 0.0)
const;
5280 const bool transA =
false,
const bool transB =
false,
5281 const T a = 1.0,
const T b = 0.0)
const;
5286 virtual void copyRow(
const int i,
Vector<T>& x)
const;
5288 virtual void copyTo(
Matrix<T>& copy)
const;
5291 virtual void print(
const string& name)
const;
5298 ones.
set(T(1.0)/_m);
5299 this->multTrans(ones,_means);
5317 const int nn=_inputmatrix->
n();
5318 for (
int i = 0; i<_shifts; ++i) {
5320 subvec2.
setData(tmp.rawX()+i,_m);
5323 _inputmatrix->multTrans(tmp,subvec,alpha,beta);
5326 b.
add(_means,-alpha*x.
sum());
5340 const int nn=_inputmatrix->n();
5341 const int mm=_inputmatrix->m();
5347 for (
int i = 0; i<_shifts; ++i) {
5350 _inputmatrix->mult(sptmp,tmp2,alpha,0);
5355 b.
add(-alpha*_means.dot(x));
5363 const int nn=_inputmatrix->n();
5364 const int mm=_inputmatrix->m();
5372 for (
int i = 0; i<_shifts; ++i) {
5374 _inputmatrix->mult(tmp,tmp2,alpha,0);
5379 b.
add(-alpha*_means.dot(x));
5385 B,
Matrix<T>& C,
const bool transA,
const bool transB,
const T a,
const T
5387 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5391 const bool transA,
const bool transB,
const T a,
const T b)
const {
5392 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5398 const T a,
const T b)
const {
5399 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5403 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5408 const int mm=_inputmatrix->m();
5409 for (
int i = 0; i<_shifts; ++i) {
5411 _inputmatrix->copyRow(ind+i,sub_vec);
5413 if (_centered) x.
sub(_means);
5417 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5422 cerr <<
"Shift Matrix is used in inadequate setting" << endl;
5427 cerr << name << endl;
5428 cerr <<
"Shift Matrix: " << _shifts <<
" shifts" << endl;
5429 _inputmatrix->print(name);
5437 _m=2*inputmatrix.
m();
5439 int n()
const {
return _n; };
5440 int m()
const {
return _m; };
5444 const T alpha = 1.0,
const T beta = 0.0)
const;
5448 const T alpha = 1.0,
const T beta = 0.0)
const;
5451 const T alpha = 1.0,
const T beta = 0.0)
const;
5455 const bool transA =
false,
const bool transB =
false,
5456 const T a = 1.0,
const T b = 0.0)
const;
5459 const bool transA =
false,
const bool transB =
false,
5460 const T a = 1.0,
const T b = 0.0)
const;
5464 const bool transA =
false,
const bool transB =
false,
5465 const T a = 1.0,
const T b = 0.0)
const;
5470 virtual void copyRow(
const int i,
Vector<T>& x)
const;
5472 virtual void copyTo(
Matrix<T>& copy)
const;
5475 virtual void print(
const string& name)
const;
5488 const int mm = _inputmatrix->m();
5490 for (
int i = 0; i<mm; ++i)
5491 tmp[i]=x[2*i]+x[2*i+1];
5492 _inputmatrix->multTrans(tmp,b,alpha,beta);
5505 const int mm = _inputmatrix->m();
5507 _inputmatrix->mult(x,tmp,alpha);
5508 for (
int i = 0; i<mm; ++i) {
5523 const int mm = _inputmatrix->m();
5525 _inputmatrix->mult(x,tmp,alpha);
5526 for (
int i = 0; i<mm; ++i) {
5534 B,
Matrix<T>& C,
const bool transA,
const bool transB,
const T a,
const T
5537 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5541 const bool transA,
const bool transB,
const T a,
const T b)
const {
5543 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5549 const T a,
const T b)
const {
5551 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5556 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5560 const int indd2=
static_cast<int>(floor(static_cast<double>(ind)/2.0));
5561 _inputmatrix->copyRow(indd2,x);
5566 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5572 cerr <<
"Double Matrix is used in inadequate setting" << endl;
5577 cerr << name << endl;
5578 cerr <<
"Double Row Matrix" << endl;
5579 _inputmatrix->print(name);
void normalize()
Normalize all columns to unit l2 norm.
virtual void add_rawCol(const int i, T *DtXi, const T a) const
compute DtX(:,i)
void add_direct(const SpMatrix< T > &mat, const T a)
void norm_l1_rows(Vector< T > &norms) const
returns the linf norms of the columns
void center()
center the columns of the matrix
void sub(const Matrix< T > &mat)
substract the matrix mat to the current matrix
void addVecToCols(const Vector< T > &diag, const T a=1.0)
void incrDiag()
add one to the diagonal
void print(const string &name) const
Print the matrix to std::cout.
void scal(const T a) const
scale the matrix by a
virtual void multSwitch(const Matrix< T > &B, Matrix< T > &C, const bool transA=false, const bool transB=false, const T a=1.0, const T b=0.0) const
perform C = a*B*A + b*C, possibly transposing A or B.
int * r() const
Direct access to _r.
void addToCols(const Vector< T > &diag)
void addVecToColsWeighted(const Vector< T > &diag, const T *weights, const T a=1.0)
T quad(const Vector< T > &vec1, const SpVector< T > &vec2) const
return vec1'*A*vec2, where vec2 is sparse
void normalize2()
normalize the vector
T asum() const
computes the sum of the magnitude of the elements
void toFull(Matrix< T > &matrix) const
copy the sparse matrix into a dense matrix
void copyRow(const int i, Vector< T > &x) const
void multSwitch(const Matrix< T > &B, Matrix< T > &C, const bool transA=false, const bool transB=false, const T a=1.0, const T b=0.0) const
perform C = a*B*A + b*C, possibly transposing A or B.
void multTrans(const Vector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
b <- alpha A'x + beta b
void rank1Update(const Vector< T > &vec1, const Vector< T > &vec2, const T alpha=1.0)
perform A <- A + alpha*vec1*vec2'
virtual void XtX(Matrix< T > &XtX) const
XtX = A'*A.
void inv_elem()
inverse the elements of the matrix
T sum() const
returns the sum of the vector
void wAAt(const Vector< T > &w, Matrix< T > &aat) const
aat <- sum_i w_i A(:,i)*A(:,i)'
void norm_2_cols(Vector< T > &norms) const
returns the l2 norms of the columns
void unwhiten(Vector< T > &mean, const bool pattern=false)
whiten
void Invsqrt()
A <- 1 ./ sqrt(A)
void meanCol(Vector< T > &mean) const
Compute the mean of the columns.
T fmaxval() const
return the 1D-index of the value of greatest magnitude
void sign(Vector< T > &signs) const
puts in signs, the sign of each point in the vector
bool _externAlloc
is the data allocation external or not
Contains miscellaneous functions.
int min() const
returns the index of the minimum value
void sum_cols(Vector< T > &sum) const
whiten
static T alt_log(const T x)
int pB(const int i) const
returns pB[i]
void Sqrt(const Vector< T > &x)
A <- 1 ./ sqrt(x)
void copy(const Vector< T > &x)
make a copy of x
void normalize()
normalize the vector
void sparseProject(Vector< T > &out, const T thrs, const int mode=1, const T lambda1=0, const T lambda2=0, const T lambda3=0, const bool pos=false)
void norm_2sq_rows(Vector< T > &norms) const
returns the l2 norms of the columns
virtual void add_rawCol(const int i, T *DtXi, const T a) const
Copy the column i into x.
void scal(const T a)
scale the vector by a
T nrm2() const
returns ||A||_2
void inv(const Vector< T > &x)
A <- 1./x.
void XtX(Matrix< T > &XtX) const
XtX = A'*A.
T sqr(const T x)
template version of the fabs function
T normFsq() const
compute the sum of the matrix elements
void setAleat()
Put white Gaussian noise in the matrix.
void setn(const int n)
modify _n
void log()
replace each value by its logarithm
void l1l2projectb(Vector< T > &out, const T thrs, const T gamma, const bool pos=false, const int mode=1)
returns true if the returned vector is null
Vector()
Empty constructor.
T fmaxval() const
returns the maximum magnitude
void copyRow(const int i, Vector< T > &x) const
Copy the column i into x.
void add_rawCol(const int i, T *DtXi, const T a) const
compute DtX(:,i)
int fmin() const
returns the index of the value with smallest magnitude
T * v() const
Direct access to _v.
void add(const Vector< T > &x, const T a=1.0)
A <- A + a*x.
int * _pB
indices of the beginning of columns
void XtX(Matrix< T > &XtX) const
XtX = A'*A.
T dot_direct(const SpMatrix< T > &mat) const
void addDiag(const Vector< T > &diag)
T KL(const Vector< T > &X)
compute the Kuhlback-Leiber divergence
void applyBayerPattern(const int offset)
sort the vector
static constexpr double E
static T xlogx(const T x)
void normalize2()
Normalize all columns which l2 norm is greater than one.
void merge(const Matrix< T > &B, Matrix< T > &C) const
merge two dictionaries
int * pE() const
Direct access to _pE.
void multDiagLeft(const Vector< T > &diag)
mult by a diagonal matrix on the left
void getData(Vector< T > &data, const int i) const
Copy the column i into x.
void mult(const Vector< T > &x, const Vector< T > &y)
A <- x .* y.
void sort2(Vector< T > &out, Vector< int > &key, const bool mode) const
void setDiag(const Vector< T > &d)
set the diagonal
T * rawX() const
returns a modifiable reference of the data, DANGEROUS
T operator[](const int index) const
Return the value X(i) (1D indexing)
std::vector< group > vector_groups
void transpose(Matrix< T > &trans)
void norm_inf_rows(Vector< T > &norms) const
returns the linf norms of the columns
Matrix()
Empty constructor.
Matrix< T > * _DtX
Depending on the mode, DtX is a matrix, or two matrices.
void subMatrixSym(const Vector< int > &indices, Matrix< T > &subMatrix) const
extract a sub-matrix of a symmetric matrix
void convertIndicesJ(Vector< int > &ind) const
void mult_elementWise(const Vector< T > &B, Vector< T > &C) const
int _nzmax
number of non-zero values
void copy_direct(const SpMatrix< T > &mat)
static void sort(int *irOut, T *prOut, int beg, int end)
void copyTo(Matrix< T > &mat) const
make a copy of the matrix mat in the current matrix
void wXAt(const Vector< T > &w, const Matrix< T > &X, Matrix< T > &XAt, const int numthreads=-1) const
XAt <- sum_i w_i X(:,i)*A(:,i)'.
T softmax(const int y)
replace each value by its exponential
T maxval() const
returns the maximum value
void sqr(const Vector< T > &x)
A <- x .^ 2.
static T softThrs(const T x, const T lambda)
int nzmax() const
returns the maximum number of non-zero elements
void diag(Vector< T > &d) const
extract the diagonal
T operator[](const int index) const
returns X[index]
void copyCol(const int i, Vector< T > &DtXi) const
compute DtX(:,i)
void norm_0_cols(Vector< T > &norms) const
returns the l0 norms of the columns
void copyMask(Vector< T > &out, Vector< bool > &mask) const
extract the rows of a matrix corresponding to a binary mask
void conjugateGradient(const Vector< T > &b, Vector< T > &x, const T tol=1e-4, const int=4) const
compute x, such that b = Ax,
void copyCol(const int i, Vector< T > &x) const
Copy the column i into x.
T afused() const
compute the sum of the differences
virtual ~Matrix()
Destructor.
void addRow(const int i, const Vector< T > &row, const T a=1.0)
fill the matrix with the row given
void refCol(int i, SpVector< T > &vec) const
reference the column i into vec
virtual void XtX(Matrix< T > &XtX) const
XtX = A'*A.
void convertIndicesI(Vector< int > &ind) const
T operator()(const int index1, const int index2) const
returns the value of an index
void thrsmin(const T nu)
perform thresholding of the matrix, with the threshold nu
void unwhiten(Vector< T > &mean, const bool pattern=false)
whiten
T * _X
pointer to the data
void diag(Vector< T > &diag) const
compute DtX(:,i)
void multSwitch(const Matrix< T > &B, Matrix< T > &C, const bool transA=false, const bool transB=false, const T a=1.0, const T b=0.0) const
perform C = a*B*A + b*C, possibly transposing A or B.
const T & max(const T &a, const T &b)
Return the maximum between a and b.
void sort(Vector< T > &out, const bool mode) const
sort the vector
SpVector()
Empty constructor.
void svdRankOne(const Vector< T > &u0, Vector< T > &u, Vector< T > &v) const
virtual T dot(const Matrix< T > &x) const
void copyTo(Matrix< T > &mat) const
make a copy of the matrix mat in the current matrix
virtual T operator[](const int index) const =0
void hardThrshold(const T nu)
perform soft-thresholding of the matrix, with the threshold nu
auto Sqrt(const ExprT &expr) -> decltype(utl::F< utl::Functor::Sqrt< typename ExprT::ValueType > >(expr))
AbstractMatrix< T > * _matrix
Contains various variables and class timer.
int n() const
returns the size of the vector
virtual void print(const string &name) const
void div_elementWise(const Matrix< T > &B, Matrix< T > &C) const
C = A .* B, elementwise multiplication.
void quad_mult(const Vector< T > &vec1, const SpVector< T > &vec2, Vector< T > &y, const T a=1.0, const T b=0.0) const
return vec1'*A*vec2, where vec2 is sparse
Data class, abstract class, useful in the class image.
void center_rows()
center the columns of the matrix
~ProdMatrix()
Constructor, D'*X is represented, with optional transpositions.
T norm_1_2_col() const
return ||At||_{1,2} (max of l2 norm of the columns)
virtual void copyTo(Matrix< T > ©) const
bool _externAlloc
external allocation
int n() const
returns the number of columns
bool _externAlloc
if the data has been externally allocated
void project_sft_binary(const Vector< T > &labels)
void refCol(int i, Vector< T > &x) const
Reference the column i into the vector x.
void copyMask(Matrix< T > &out, Vector< bool > &mask) const
extract the rows of a matrix corresponding to a binary mask
virtual void mult(const SpVector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
perform b = alpha*A*x + beta*b, when x is sparse
T length() const
returns the length of the vector
virtual ~AbstractMatrixB()
int * _pE
indices of the end of columns
void setZeros()
Set all values to zero.
void toSparseTrans(SpMatrix< T > &matrixTrans)
make a sparse copy of the current matrix
const T & min(const T &a, const T &b)
Return the minimum between a and b.
static T hardThrs(const T x, const T lambda)
void thrshold(const T nu)
performs soft-thresholding of the vector
T asum() const
computes the sum of the magnitudes of the vector
void print(const string &name) const
print the sparse matrix
T dot(const Vector< T > &x) const
returns A'x
void AAt(Matrix< T > &aat) const
aat <- A*A'
T v(const int i) const
returns v[i]
void thrsabsmin(const T nu)
performs thresholding of the vector
T operator()(const int index1, const int index2) const
virtual void mult(const SpVector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
perform b = alpha*A*x + beta*b, when x is sparse
void multDiagRight(const Vector< T > &diag)
mult by a diagonal matrix on the right
void whiten(Vector< T > &mean, const bool pattern=false)
whiten
void multTrans(const Vector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
b <- alpha A'x + beta b
void toVect(Vector< T > &vec) const
make a reference of the matrix to a vector vec
T trace() const
return the trace of the matrix
virtual void getData(Vector< T > &data, const int i) const =0
T abs(const T x)
template version of the fabs function
void refIndices(Vector< int > &indices) const
create a reference on the vector r
void clear()
clear the vector
void norm_2sq_cols(Vector< T > &norms) const
returns the l2 norms ^2 of the columns
void print(const string &name) const
print the vector to std::cerr
void invSym()
inverse the matrix when it is symmetric
int fmax() const
return the 1D-index of the value of greatest magnitude
T dot(const SpVector< T > &vec) const
dot product
static T fusedHomotopyAux(const bool &sign1, const bool &sign2, const bool &sign3, const T &c1, const T &c2)
void setData(T *X, int m, int n)
Change the data in the matrix.
bool isNormalized() const
Check wether the columns of the matrix are normalized or not.
void copy(const Matrix< T > &mat)
make a copy of the matrix mat in the current matrix
void set(const T a)
Set all the values to a scalar.
SpMatrix()
Empty constructor.
Class representing the product of two matrices.
void softThrshold(const T nu)
perform soft-thresholding of the matrix, with the threshold nu
void logspace(const int n, const T a, const T b)
generate logarithmically spaced values
void toSparse(SpVector< T > &vec) const
make a sparse copy
void copy(const SpMatrix< T > &mat)
void thrsmax(const T nu)
performs soft-thresholding of the vector
virtual void getGroup(Matrix< T > &data, const vector_groups &groups, const int i) const
extract the group i
void exp()
each element of the matrix is replaced by its exponential
void blockThrshold(const T nu, const int sizeGroup)
performs soft-thresholding of the vector
void fakeSize(const int n)
change artificially the size of the vector, DANGEROUS
void extract_rawCol(const int i, T *DtXi) const
compute DtX(:,i)
int nnz() const
number of nonzeros elements
const AbstractMatrixB< T > * _inputmatrix
void thrsmin(const T nu)
performs thresholding of the vector
void fakeSize(const int m, const int n)
change artificially the size of the matrix, DANGEROUS
void extractRow(const int i, Vector< T > &row) const
fill the matrix with the row given
void setPointer(T *X, const int n)
change the data of the vector
void clear()
clear the matrix
virtual void print(const string &name) const
T dot(const Matrix< T > &x) const
dot product;
void exp()
replace each value by its exponential
void drop(char *fileName) const
bool _externAlloc
if the data has been externally allocated
void set(const T val)
set each value of the vector to val
void thrsPos()
performs soft-thresholding of the vector
int _nzmax
maximum number of nonzeros elements
void fillRow(const Vector< T > &row)
fill the matrix with the row given
T fmaxval() const
computes the linf norm of the vector
int max() const
returns the index of the largest value
T dot(const Matrix< T > &mat) const
add alpha*mat to the current matrix
T nrm2sq() const
computes the l2 norm ^2 of the vector
T asum() const
compute the sum of the matrix elements
T & operator[](const int index)
returns a reference to X[index]
int m() const
returns the number of columns
void fusedProjectHomotopy(Vector< T > &out, const T lambda1, const T lambda2, const T lambda3=0, const bool penalty=true)
void thrsmax(const T nu)
perform thresholding of the matrix, with the threshold nu
int m() const
returns the number of rows
virtual void getGroup(Matrix< T > &data, const vector_groups &groups, const int i) const =0
void mult(const Vector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
perform b = alpha*A*x+beta*b
int n() const
Number of columns.
T eigLargestMagnSym() const
int fmin() const
return the 1D-index of the value of lowest magnitude
void refSubMat(int i, int n, Matrix< T > &mat) const
Reference the column i to i+n into the Matrix mat.
void toSpMatrix(SpMatrix< T > &out, const int m, const int n) const
resize the vector as a sparse matrix
T normF() const
return ||A||_F
void diag(Vector< T > &diag) const
add something to the diagonal
void singularValues(Vector< T > &u) const
void copyRef(const Matrix< T > &mat)
make a copy of the matrix mat in the current matrix
void norm_2sq_cols(Vector< T > &norms) const
returns the l2 norms ^2 of the columns
float alt_log< float >(const float x)
T nrm2() const
computes the l2 norm of the vector
virtual void multSwitch(const Matrix< T > &B, Matrix< T > &C, const bool transA=false, const bool transB=false, const T a=1.0, const T b=0.0) const
perform C = a*B*A + b*C, possibly transposing A or B.
void clear()
clears the vector
void sparseProject(Matrix< T > &out, const T thrs, const int mode=1, const T lambda1=0, const T lambda2=0, const T lambda3=0, const bool pos=false, const int numThreads=-1)
performs sparse projections of the columns
static void quick_sort(int *irOut, T *prOut, const int beg, const int end, const bool incr)
T & operator()(const int i, const int j)
Return a modifiable reference to X(i,j)
T nrm2sq() const
returns ||A||_2^2
T cblas_asum(const INTT N, const T *X, const INTT incX)
void setZeros()
Set all the values to zero.
void print(const char *name) const
void resize(const int n)
resize the vector
virtual void norm_2sq_cols(Vector< T > &norms) const
int V() const
returns the number of columns
static bool isEqual(const T lambda1, const T lambda2)
void XXt(Matrix< T > &XXt) const
XXt = A*A'.
void clean()
clean a dictionary matrix
int r(const int i) const
returns r[i]
void convert2(const Matrix< T > &v, const Vector< int > &r, const int K)
use the data from v, r for _v, _r
void XAt(const Matrix< T > &X, Matrix< T > &XAt) const
XAt <- X*A'.
void resize(const int m, const int n, const int nzmax)
resize the matrix
T nrm2sq() const
return ||A||_F^2
void svd(Matrix< T > &U, Vector< T > &S, Matrix< T > &V) const
void softThrshold(const T nu)
performs soft-thresholding of the vector
void Sqrt()
each element of the matrix is replaced by its square root
void addVecToCols(const Vector< T > &diag, const T a=1.0)
void norm_inf_cols(Vector< T > &norms) const
returns the linf norms of the columns
void refVal(Vector< T > &val) const
creates a reference on the vector val
void hardThrshold(const T nu)
performs soft-thresholding of the vector
void meanRow(Vector< T > &mean) const
Compute the mean of the rows.
double alt_log< double >(const double x)
void upperTriXXt(Matrix< T > &XXt, const int L) const
XXt = A*A' where A is an upper triangular matrix.
void getGroup(Matrix< T > &data, const vector_groups &groups, const int i) const
virtual ~DoubleRowMatrix()
static T logexp(const T x)
void eigLargestSymApprox(const Vector< T > &u0, Vector< T > &u) const
int fmax() const
returns the index of the value with largest magnitude
void getData(Vector< T > &data, const int index) const
void whiten(const int V)
whiten
int * pB() const
Direct access to _pB.
static bool isZero(const T lambda)
void extract_rawCol(const int i, T *x) const
Copy the column i into x.
void resize(const int nzmax)
resizes the vector
T minval() const
returns the minimum value
static int init_omp(const int numThreads)
SubMatrix(AbstractMatrix< T > &G, Vector< int > &indI, Vector< int > &indJ)
T fminval() const
returns the minimum magnitude
DoubleRowMatrix(const AbstractMatrixB< T > &inputmatrix)
void fusedProject(Vector< T > &out, const T lambda1, const T lambda2, const int itermax)
T norm_inf_2_col() const
return ||At||_{inf,2} (max of l2 norm of the columns)
const AbstractMatrixB< T > * _inputmatrix
void setMatrices(const Matrix< T > &D, const bool high_memory=true)
set_matrices
int n() const
returns the number of rows
virtual T dot(const Matrix< T > &x) const
void inv()
inverse the elements of the matrix
void eye()
set the matrix to the identity;
virtual void copyTo(Matrix< T > ©) const
T asum() const
compute the sum of the magnitude of the matrix values
void multTrans(const Vector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
perform b = alpha*A'x + beta*b
T operator[](const int index) const
returns the value of an index
void setAleat()
put random values in the vector (White Gaussian Noise)
std::list< group > list_groups
void toFull(Vector< T > &out) const
resize the vector as a sparse matrix
void convert(const Matrix< T > &v, const Matrix< int > &r, const int K)
use the data from v, r for _v, _r
void norm_2_rows(Vector< T > &norms) const
returns the l2 norms of the columns
void rank1Update_mult(const Vector< T > &vec1, const Vector< T > &vec1b, const SpVector< T > &vec2, const T alpha=1.0)
void copyCol(const int i, Vector< T > &DtXi) const
compute DtX(:,i)
virtual ~Vector()
Destructor.
void mult_elementWise(const Matrix< T > &B, Matrix< T > &C) const
C = A .* B, elementwise multiplication.
void div(const Vector< T > &x)
A <- A ./ x.
void norm_1_cols(Vector< T > &norms) const
returns the l1 norms of the columns
int sign(const T &x)
Return the sign of x.
void resize(int m, int n)
Resize the matrix.
void setRow(const int i, const Vector< T > &row)
fill the matrix with the row given
T normFsq() const
return ||A||_F^2
T v(const int i) const
access table r
void l1l2project(Vector< T > &out, const T thrs, const T gamma, const bool pos=false) const
void thrsPos()
perform soft-thresholding of the matrix, with the threshold nu
void sum_cols(Vector< T > &sum) const
void toSparse(SpMatrix< T > &matrix) const
make a sparse copy of the current matrix
const T * X() const
return a non-modifiable reference to the data
void multTrans(const Vector< T > &x, Vector< T > &y, const T alpha=1.0, const T beta=0.0) const
y <- A'*x
void sub(const Vector< T > &x)
A <- A - x.
void toFullTrans(Matrix< T > &matrix) const
copy the sparse matrix into a dense transposed matrix
void NadarayaWatson(const Vector< int > &ind, const T sigma)
compute a Nadaraya Watson estimator
T & operator[](const int index)
Return a modifiable reference to X(i) (1D indexing)
void extract_rawCol(const int i, T *pr) const
copy X(:,i) into Xi
void logexp()
replace each value by its exponential
virtual void copyRow(const int i, Vector< T > &x) const
void randperm(int n)
put a random permutation of size n (for integral vectors)
virtual void copyRow(const int i, Vector< T > &x) const
virtual ~AbstractMatrix()
void setData(T *X, const int n)
void addDiag(const T diag)
add something to the diagonal
ShiftMatrix(const AbstractMatrixB< T > &inputmatrix, const int shifts, const bool center=false)
void setm(const int m)
modify _m
void add(const Matrix< T > &mat, const T alpha=1.0)
add alpha*mat to the current matrix
void mult(const SpVector< T > &x, Vector< T > &b, const T alpha=1.0, const T beta=0.0) const
perform b = alpha*A*x + beta*b, when x is sparse
void Sqrt()
A <- 1 ./ sqrt(x)
void l1project_weighted(Vector< T > &out, const Vector< T > &weights, const T thrs, const bool residual=false) const
void l1project(Vector< T > &out, const T thrs, const bool simplex=false) const
int r(const int i) const
access table r
void scal(const T a)
scale the matrix by the a
void project_sft(const Vector< int > &labels, const int clas)
T * rawX() const
reference a modifiable reference to the data, DANGEROUS
void clear()
Clear the matrix.