/* * OK: we need to find the three attributes with the greatest * potential gains for the group, and return them wrapped in a vector * * so: copy the vector; * sort it * take the top three indices * put them in their own vector * and return that. * * Easy! */ std::vector<int> IdealGirl::training_indices() { /* * to start: copy the vector */ std::vector<IdealAttr> copyvec( stats.begin(), stats.end() ); /* * now: sort it */ sort( copyvec.begin(), copyvec.end(), sort_IdealAttr ); /* * OK: now we need an integer vector * * (I could use an array here - but I want to make this * easy to change if I need to make the number of * atrributes trained into a config variable */ std::vector<int> indices; /* * and we use it to store the attribute indices * of the top three elements (or less if there are less) */ for( u_int i = 0; i < 3; i++ ) { //indices[i] = copyvec[i].attr_index(); indices.push_back( copyvec[i].attr_index() ); } return indices; }
static TYPE *gen_dupvec(int N, TYPE padval, int npadX, TYPE *X, int incX, int npadY, int incY) { int i, n; TYPE *y, *yy, *xx=X+(npadX SHIFT); y = getvec(npadY, padval, N, incY); yy = y + (npadY SHIFT); if (incY < 1) yy -= ((N-1)SHIFT) * incY; if (incX < 1) xx -= ((N-1)SHIFT) * incX; copyvec(N, xx, incX, yy, incY); return(y); }
static int bundled_item_replace_data (bundled_item *item, GretlType type, void *ptr, int size, int copy) { int err = 0; if (ptr == item->data) { return 0; } if (item->type == GRETL_TYPE_DOUBLE) { double *dp = item->data; *dp = *(double *) ptr; } else if (item->type == GRETL_TYPE_STRING) { free(item->data); if (copy) { item->data = gretl_strdup((char *) ptr); } else { item->data = ptr; } } else if (item->type == GRETL_TYPE_MATRIX) { gretl_matrix_free(item->data); if (copy) { item->data = gretl_matrix_copy((gretl_matrix *) ptr); } else { item->data = ptr; } } else if (item->type == GRETL_TYPE_MATRIX_REF) { release_matrix_pointer((gretl_matrix **) &item->data); item->data = ptr; } else if (item->type == GRETL_TYPE_SERIES) { free(item->data); if (copy) { item->data = copyvec((const double *) ptr, size); } else { item->data = ptr; } item->size = size; } else if (item->type == GRETL_TYPE_BUNDLE) { gretl_bundle_destroy((gretl_bundle *) item->data); if (copy) { item->data = gretl_bundle_copy((gretl_bundle *) ptr, &err); } else { item->data = ptr; } } else if (item->type == GRETL_TYPE_ARRAY) { gretl_array_destroy((gretl_array*) item->data); if (copy) { item->data = gretl_array_copy((gretl_array *) ptr, &err); } else { item->data = ptr; } } else { return E_DATA; } if (!err && item->data == NULL) { err = E_ALLOC; } if (item->note != NULL) { free(item->note); item->note = NULL; } return err; }
static bundled_item *bundled_item_new (GretlType type, void *ptr, int size, int copy, const char *note, int *err) { bundled_item *item = malloc(sizeof *item); if (item == NULL) { *err = E_ALLOC; } else { item->type = type; item->size = 0; item->note = NULL; switch (item->type) { case GRETL_TYPE_DOUBLE: item->data = malloc(sizeof(double)); if (item->data != NULL) { double *dp = item->data; *dp = *(double *) ptr; } break; case GRETL_TYPE_STRING: if (copy) { item->data = gretl_strdup((char *) ptr); } else { item->data = ptr; } break; case GRETL_TYPE_MATRIX: if (copy) { item->data = gretl_matrix_copy((gretl_matrix *) ptr); } else { item->data = ptr; } break; case GRETL_TYPE_MATRIX_REF: item->data = ptr; break; case GRETL_TYPE_SERIES: if (copy) { item->data = copyvec((const double *) ptr, size); } else { item->data = ptr; } item->size = size; break; case GRETL_TYPE_BUNDLE: if (copy) { item->data = gretl_bundle_copy((gretl_bundle *) ptr, err); } else { item->data = ptr; } break; case GRETL_TYPE_ARRAY: if (copy) { item->data = gretl_array_copy((gretl_array *) ptr, err); } else { item->data = ptr; } break; default: *err = E_TYPES; break; } if (!*err && item->data == NULL) { free(item); item = NULL; *err = E_ALLOC; } if (item != NULL && note != NULL) { item->note = gretl_strdup(note); } } return item; }
static int process_panel_vars (DATASET *dwinfo, dw_opts *opts) { int n = dataset->n; double *uid = NULL; double *tid = NULL; int uv, tv; int nunits = 0; int nperiods = 0; int err = 0; /* FIXME sub-sampled dataset? */ err = translate_panel_vars(opts, &uv, &tv); if (err) { return err; } if (uv == tv) { /* "can't happen" */ errbox(_("The unit and time index variables must be distinct")); return E_DATA; } uid = copyvec(dataset->Z[uv], n); tid = copyvec(dataset->Z[tv], n); if (uid == NULL || tid == NULL) { nomem(); err = E_ALLOC; } if (!err) { qsort(uid, n, sizeof *uid, gretl_compare_doubles); nunits = count_distinct_values(uid, n); qsort(tid, n, sizeof *tid, gretl_compare_doubles); nperiods = count_distinct_values(tid, n); /* heuristic: if a variable represents either the panel unit or period, it must have at least two distinct values, and must have fewer values than the total number of observations. Further, the product of the number of distinct values for the unit and time variables must be at least equal to the number of observations, otherwise there will be duplicated rows (i.e. more than one row claiming to represent unit i, period t, for some i, t). Note that the product (nunits * nperiods) may be _greater_ than total n: this may mean that we have some implicit missing observations. */ if (nunits == 1 || nperiods == 1 || nunits == n || nperiods == n || nunits * nperiods < n) { errbox(_("The selected index variables do not represent " "a panel structure")); err = E_DATA; } else { dwinfo->n = nunits; dwinfo->pd = nperiods; } } free(uid); free(tid); return err; }
Matrix operator * (const Matrix& A, const Matrix& B) { if (A.Clo() != B.Rlo() || A.Chi() != B.Rhi()) Matpack.Error("Matrix operator * (const Matrix&, const Matrix&): " "non conformant arguments\n"); // allocate return matrix Matrix C(A.Rlo(),A.Rhi(),B.Clo(),B.Chi()); //------------------------------------------------------------------------// // the BLAS version //------------------------------------------------------------------------// #if defined ( _MATPACK_USE_BLAS_ ) if ( LT(B) ) { // full matrix * lower triangle #ifdef DEBUG cout << "GM*LT\n"; #endif checksquare(B); // copy A to C to protect from overwriting copyvec(C.Store(),A.Store(),A.Elements()); charT side('L'), uplo('U'), transc('N'), diag('N'); intT m(C.Cols()), n(C.Rows()), ldb(B.Cols()), ldc(C.Cols()); doubleT alpha(1.0); F77NAME(dtrmm)(&side,&uplo,&transc,&diag,&m,&n, &alpha,B.Store(),&ldb, C.Store(),&ldc); } else if ( UT(B) ) { // full matrix * upper triangle #ifdef DEBUG cout << "GM*UT\n"; #endif checksquare(B); // copy A to C to protect from overwriting copyvec(C.Store(),A.Store(),A.Elements()); charT side('L'), uplo('L'), transc('N'), diag('N'); intT m(C.Cols()), n(C.Rows()), ldb(B.Cols()), ldc(C.Cols()); doubleT alpha(1.0); F77NAME(dtrmm)(&side,&uplo,&transc,&diag,&m,&n, &alpha,B.Store(),&ldb, C.Store(),&ldc); } else if ( LT(A) ) { // lower triangle * full matrix #ifdef DEBUG cout << "LT*GM\n"; #endif checksquare(A); // copy B to C to protect from overwriting copyvec(C.Store(),B.Store(),B.Elements()); charT side('R'), uplo('U'), transc('N'), diag('N'); intT m(C.Cols()), n(C.Rows()), ldb(A.Cols()), ldc(C.Cols()); doubleT alpha(1.0); F77NAME(dtrmm)(&side,&uplo,&transc,&diag,&m,&n, &alpha,A.Store(),&ldb, C.Store(),&ldc); } else if ( UT(A) ) { // upper triangle * full matrix #ifdef DEBUG cout << "UT*GM\n"; #endif checksquare(A); // copy A to C to protect from overwriting copyvec(C.Store(),B.Store(),B.Elements()); charT side('R'), uplo('L'), transc('N'), diag('N'); intT m(C.Cols()), n(C.Rows()), ldb(A.Cols()), ldc(C.Cols()); doubleT alpha(1.0); F77NAME(dtrmm)(&side,&uplo,&transc,&diag,&m,&n, &alpha,A.Store(),&ldb, C.Store(),&ldc); } else /* GM(A) and GM(B) */ { // GM*GM: full matrix * full matrix #ifdef DEBUG cout << "GM*GM\n"; #endif charT t('N'); intT m(B.Cols()), n(A.Rows()), k(B.Rows()), lda(A.Cols()), ldb(B.Cols()), ldc(C.Cols()); doubleT alpha(1.0), beta(0.0); F77NAME(dgemm)(&t,&t, &m,&n,&k, &alpha,B.Store(),&ldb, A.Store(),&lda, &beta,C.Store(),&ldc); } //------------------------------------------------------------------------// // the non-BLAS version //------------------------------------------------------------------------// #else int cl = A.cl, ch = A.ch, arl = A.rl, arh = A.rh, bcl = B.cl, bch = B.ch; // avoid call to index operator that optimizes very badely double **a = A.M, **b = B.M, **c = C.M; for (int i = arl; i <= arh; i++) { for (int j = bcl; j <= bch; j++) c[i][j] = 0.0; for (int l = cl; l <= ch; l++) { if ( a[i][l] != 0.0 ) { double temp = a[i][l]; for (int j = bcl; j <= bch; j++) c[i][j] += temp * b[l][j]; } } } #endif return C.Value(); }