void HMM::compute_ess_dhmm(mat startprob, mat transmat, mat obsmat, field<rowvec> data, double &loglik, mat &exp_num_trans, mat &exp_num_visits1, mat &exp_num_emit, mat &exp_num_visitsT) { /* * Compute the Expected Sufficient Statistics for an HMM with discrete outputs * * INPUTS: * startprob(i) * transmat(i,j) * obsmat(i,o) * data{seq}(t) * dirichlet - weighting term for uniform dirichlet prior on expected emissions * * OUTPUTS: * exp_num_trans(i,j) = sum_l sum_{t=2}^T Pr(X(t-1) = i, X(t) = j| Obs(l)) * exp_num_visits1(i) = sum_l Pr(X(1)=i | Obs(l)) * exp_num_visitsT(i) = sum_l Pr(X(T)=i | Obs(l)) * exp_num_emit(i,o) = sum_l sum_{t=1}^T Pr(X(t) = i, O(t)=o| Obs(l)) * where Obs(l) = O_1 .. O_T for sequence l. * * */ //cout << "compute_ess_dhmm" << endl; int numex = data.n_elem; int O = obsmat.n_cols; for (int ex=0; ex<numex; ex++) { // initialization of EM rowvec obs = data(ex,0); if (obs.n_elem == 0) continue; mat obslik = multinomial_prob(obs, obsmat); int T = obs.n_elem; int Q = obslik.n_rows; int N = obslik.n_cols; double current_ll = 0; mat gamma = zeros(Q,N); mat xi_summed; // if compute_xi is zeros(Q,Q), else is null fwdback(startprob, transmat, obslik, gamma, current_ll, xi_summed); loglik = loglik + current_ll; exp_num_trans = exp_num_trans + xi_summed; exp_num_visits1 = exp_num_visits1 + gamma.col(0); exp_num_visitsT = exp_num_visitsT + gamma.col(T-1); if (T < O) { for(int t=1; t<T; t++) { int o = obs[t]; exp_num_emit.col(o-1) = exp_num_emit.col(o-1) + gamma.col(t); } } else { for(int o=0; o<=O; o++) { uvec ndx = find(obs==o); if (!ndx.is_empty()) { mat sumgamma = sum(get_cols(gamma,ndx), 1); exp_num_emit.col(o-1) = exp_num_emit.col(o-1) + sumgamma; } } } } }
void Image::set_value_in2d(uchar value, int i, int j) { set_value_in1d(value, i*get_cols() + j); }
int main( int argc, char *argv[]) { /* Array for the log entries for the specified job */ FILE *fp; int i, j; int file_count; char *filenames[MAX_LOG_FILES_PER_DAY]; /* full path of logfiles to read */ struct tm *tm_ptr; time_t t, t_save; signed char c; char *prefix_path = NULL; int number_of_days = 1; char *endp; short error = 0; int opt; char no_acct = 0, no_svr = 0, no_mom = 0, no_schd = 0; char verbosity = 1; int wrap = -1; int log_filter = 0; int event_type; char filter_excessive = 0; int excessive_count; #if defined(FILTER_EXCESSIVE) filter_excessive = 1; #endif #if defined(EXCESSIVE_COUNT) excessive_count = EXCESSIVE_COUNT; #endif while ((c = getopt(argc, argv, "qzvamslw:p:n:f:c:")) != EOF) { switch (c) { case 'q': verbosity = 0; break; case 'v': verbosity = 2; break; case 'a': no_acct = 1; break; case 's': no_svr = 1; break; case 'm': no_mom = 1; break; case 'l': no_schd = 1; break; case 'z': filter_excessive = filter_excessive ? 0 : 1; break; case 'c': excessive_count = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'w': wrap = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'p': prefix_path = optarg; break; case 'n': number_of_days = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'f': if (!strcmp(optarg, "error")) log_filter |= PBSEVENT_ERROR; else if (!strcmp(optarg, "system")) log_filter |= PBSEVENT_SYSTEM; else if (!strcmp(optarg, "admin")) log_filter |= PBSEVENT_ADMIN; else if (!strcmp(optarg, "job")) log_filter |= PBSEVENT_JOB; else if (!strcmp(optarg, "job_usage")) log_filter |= PBSEVENT_JOB_USAGE; else if (!strcmp(optarg, "security")) log_filter |= PBSEVENT_SECURITY; else if (!strcmp(optarg, "sched")) log_filter |= PBSEVENT_SCHED; else if (!strcmp(optarg, "debug")) log_filter |= PBSEVENT_DEBUG; else if (!strcmp(optarg, "debug2")) log_filter |= PBSEVENT_DEBUG2; else if (isdigit(optarg[0])) { log_filter = strtol(optarg, &endp, 16); if (*endp != '\0') error = 1; } else error = 1; break; default: error = 1; break; } } /* END while ((c = getopt(argc,argv,"zvamslw:p:n:f:c:")) != EOF) */ /* no jobs */ if ((error != 0) || (argc == optind)) { printf("USAGE: %s [-a|s|l|m|q|v|z] [-c count] [-w size] [-p path] [-n days] [-f filter_type] <JOBID>\n", strip_path(argv[0])); printf( " -p : path to PBS_SERVER_HOME\n" " -w : number of columns of your terminal\n" " -n : number of days in the past to look for job(s) [default 1]\n" " -f : filter out types of log entries, multiple -f's can be specified\n" " error, system, admin, job, job_usage, security, sched, debug, \n" " debug2, or absolute numeric hex equivalent\n" " -z : toggle filtering excessive messages\n"); printf( " -c : what message count is considered excessive\n" " -a : don't use accounting log files\n" " -s : don't use server log files\n" " -l : don't use scheduler log files\n" " -m : don't use mom log files\n" " -q : quiet mode - hide all error messages\n" " -v : verbose mode - show more error messages\n"); printf("default prefix path = %s\n", PBS_SERVER_HOME); #if defined(FILTER_EXCESSIVE) printf("filter_excessive: ON\n"); #else printf("filter_excessive: OFF\n"); #endif return(1); } /* END if ((error != 0) || (argc == optind)) */ if (wrap == -1) wrap = get_cols(); time(&t); t_save = t; for (opt = optind;opt < argc;opt++) { for (i = 0, t = t_save;i < number_of_days;i++, t -= SECONDS_IN_DAY) { tm_ptr = localtime(&t); for (j = 0;j < 4;j++) { if ((j == IND_ACCT && no_acct) || (j == IND_SERVER && no_svr) || (j == IND_MOM && no_mom) || (j == IND_SCHED && no_schd)) continue; file_count = log_path(prefix_path, j, tm_ptr, filenames); /* there can be multiple server and mom log files per day */ /* traverse filenames until we have got them all */ if (file_count < 0) { printf("Error getting file names\n"); continue; } for (; file_count > 0; file_count--) { if ((fp = fopen(filenames[file_count-1], "r")) == NULL) { if (verbosity >= 1) perror(filenames[file_count-1]); continue; } if (parse_log(fp, argv[opt], j) < 0) { /* no valid entries located in file */ if (verbosity >= 1) { fprintf(stderr, "%s: No matching job records located\n", filenames[file_count-1]); } } else if (verbosity >= 2) { fprintf(stderr, "%s: Successfully located matching job records\n", filenames[file_count-1]); } fclose(fp); free(filenames[file_count-1]); } /* end of for file_count */ } } /* END for (i) */ if (filter_excessive) filter_excess(excessive_count); qsort(log_lines, ll_cur_amm, sizeof(struct log_entry), sort_by_date); if (ll_cur_amm != 0) { printf("\nJob: %s\n\n", log_lines[0].name); } for (i = 0;i < ll_cur_amm;i++) { if (log_lines[i].log_file == 'A') event_type = 0; else event_type = strtol(log_lines[i].event, &endp, 16); if (!(log_filter & event_type) && !(log_lines[i].no_print)) { printf("%-20s %-5c", log_lines[i].date, log_lines[i].log_file); line_wrap(log_lines[i].msg, 26, wrap); } } } /* END for (opt) */ return(0); } /* END main() */
uchar Image::get_value_from2d(int i, int j) const { int pos = i * get_cols() + j; return get_value_from1d(pos); }
int main (int argc, char * const argv[]) { PROG = "aa-start"; const char *path_repo = "/run/services"; const char *path_list = NULL; int i; aa_secs_timeout = DEFAULT_TIMEOUT_SECS; for (;;) { struct option longopts[] = { { "double-output", no_argument, NULL, 'D' }, { "help", no_argument, NULL, 'h' }, { "listdir", required_argument, NULL, 'l' }, { "dry-list", no_argument, NULL, 'n' }, { "repodir", required_argument, NULL, 'r' }, { "timeout", required_argument, NULL, 't' }, { "version", no_argument, NULL, 'V' }, { "verbose", no_argument, NULL, 'v' }, { "no-wants", no_argument, NULL, 'W' }, { NULL, 0, 0, 0 } }; int c; c = getopt_long (argc, argv, "Dhl:nr:t:VvW", longopts, NULL); if (c == -1) break; switch (c) { case 'D': aa_set_double_output (1); break; case 'h': dieusage (0); case 'l': unslash (optarg); path_list = optarg; break; case 'n': if (mode & AA_MODE_IS_DRY) mode |= AA_MODE_IS_DRY_FULL; else mode |= AA_MODE_IS_DRY; break; case 'r': unslash (optarg); path_repo = optarg; break; case 't': if (!uint0_scan (optarg, &aa_secs_timeout)) aa_strerr_diefu2sys (ERR_IO, "set default timeout to ", optarg); break; case 'V': aa_die_version (); case 'v': verbose = 1; break; case 'W': no_wants = 1; break; default: dieusage (1); } } argc -= optind; argv += optind; cols = get_cols (1); is_utf8 = is_locale_utf8 (); if (!path_list && argc < 1) dieusage (1); if (aa_init_repo (path_repo, (mode & AA_MODE_IS_DRY) ? AA_REPO_READ : AA_REPO_WRITE) < 0) aa_strerr_diefu2sys (ERR_IO, "init repository ", path_repo); if (path_list) { stralloc sa = STRALLOC_ZERO; int r; if (*path_list != '/' && *path_list != '.') stralloc_cats (&sa, LISTDIR_PREFIX); stralloc_catb (&sa, path_list, strlen (path_list) + 1); r = aa_scan_dir (&sa, 1, it_start, NULL); stralloc_free (&sa); if (r < 0) aa_strerr_diefu3sys (-r, "read list directory ", (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list, (*path_list != '/' && *path_list != '.') ? path_list : ""); } tain_now_g (); for (i = 0; i < argc; ++i) if (str_equal (argv[i], "-")) { if (process_names_from_stdin ((names_cb) add_service, NULL) < 0) aa_strerr_diefu1sys (ERR_IO, "process names from stdin"); } else add_service (argv[i], NULL); mainloop (mode, scan_cb); if (!(mode & AA_MODE_IS_DRY)) { aa_bs_noflush (AA_OUT, "\n"); put_title (1, PROG, "Completed.", 1); aa_show_stat_nb (nb_already, "Already up", ANSI_HIGHLIGHT_GREEN_ON); aa_show_stat_nb (nb_done, "Started", ANSI_HIGHLIGHT_GREEN_ON); show_stat_service_names (&ga_timedout, "Timed out", ANSI_HIGHLIGHT_RED_ON); show_stat_service_names (&ga_failed, "Failed", ANSI_HIGHLIGHT_RED_ON); show_stat_service_names (&ga_depend, "Dependency failed", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_io, "I/O error", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_unknown, "Unknown", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_skipped, "Skipped", ANSI_HIGHLIGHT_YELLOW_ON); } genalloc_free (int, &ga_timedout); genalloc_free (int, &ga_failed); genalloc_free (int, &ga_depend); genalloc_free (size_t, &ga_io); genalloc_free (size_t, &ga_unknown); genalloc_free (size_t, &ga_skipped); genalloc_free (pid_t, &ga_pid); genalloc_free (int, &aa_tmp_list); genalloc_free (int, &aa_main_list); stralloc_free (&aa_names); genalloc_deepfree (struct progress, &ga_progress, free_progress); aa_free_services (close_fd); genalloc_free (iopause_fd, &ga_iop); return rc; }
/* Prints a usage message based on contents of optlist. * Parameters: * scanner - The scanner, already initialized with scanopt_init(). * fp - The file stream to write to. * usage - Text to be prepended to option list. * Return: Always returns 0 (zero). * The output looks something like this: [indent][option, alias1, alias2...][indent][description line1 description line2...] */ int scanopt_usage (scanopt_t *scanner, FILE *fp, const char *usage) { struct _scanopt_t *s; int i, columns, indent = 2; usg_elem *byr_val = NULL; /* option indices sorted by r_val */ usg_elem *store; /* array of preallocated elements. */ int store_idx = 0; usg_elem *ue; int maxlen[2]; int desccol = 0; int print_run = 0; maxlen[0] = 0; maxlen[1] = 0; s = (struct _scanopt_t *) scanner; if (usage) { fprintf (fp, "%s\n", usage); } else { /* Find the basename of argv[0] */ const char *p; p = s->argv[0] + strlen (s->argv[0]); while (p != s->argv[0] && *p != '/') --p; if (*p == '/') p++; fprintf (fp, _("Usage: %s [OPTIONS]...\n"), p); } fprintf (fp, "\n"); /* Sort by r_val and string. Yes, this is O(n*n), but n is small. */ store = malloc((size_t) s->optc * sizeof (usg_elem)); for (i = 0; i < s->optc; i++) { /* grab the next preallocate node. */ ue = store + store_idx++; ue->idx = i; ue->next = ue->alias = NULL; /* insert into list. */ if (!byr_val) byr_val = ue; else { int found_alias = 0; usg_elem **ue_curr, **ptr_if_no_alias = NULL; ue_curr = &byr_val; while (*ue_curr) { if (RVAL (s, (*ue_curr)->idx) == RVAL (s, ue->idx)) { /* push onto the alias list. */ ue_curr = &((*ue_curr)->alias); found_alias = 1; break; } if (!ptr_if_no_alias && strcasecmp (NAME (s, (*ue_curr)->idx), NAME (s, ue->idx)) > 0) { ptr_if_no_alias = ue_curr; } ue_curr = &((*ue_curr)->next); } if (!found_alias && ptr_if_no_alias) ue_curr = ptr_if_no_alias; ue->next = *ue_curr; *ue_curr = ue; } } #if 0 if (1) { printf ("ORIGINAL:\n"); for (i = 0; i < s->optc; i++) printf ("%2d: %s\n", i, NAME (s, i)); printf ("SORTED:\n"); ue = byr_val; while (ue) { usg_elem *ue2; printf ("%2d: %s\n", ue->idx, NAME (s, ue->idx)); for (ue2 = ue->alias; ue2; ue2 = ue2->next) printf (" +---> %2d: %s\n", ue2->idx, NAME (s, ue2->idx)); ue = ue->next; } } #endif /* Now build each row of output. */ /* first pass calculate how much room we need. */ for (ue = byr_val; ue; ue = ue->next) { usg_elem *ap; int len = 0; int nshort = 0, nlong = 0; #define CALC_LEN(i) do {\ if(FLAGS(s,i) & IS_LONG) \ len += (nlong++||nshort) ? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\ else\ len += (nshort++||nlong)? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\ }while(0) if (!(FLAGS (s, ue->idx) & IS_LONG)) CALC_LEN (ue->idx); /* do short aliases first. */ for (ap = ue->alias; ap; ap = ap->next) { if (FLAGS (s, ap->idx) & IS_LONG) continue; CALC_LEN (ap->idx); } if (FLAGS (s, ue->idx) & IS_LONG) CALC_LEN (ue->idx); /* repeat the above loop, this time for long aliases. */ for (ap = ue->alias; ap; ap = ap->next) { if (!(FLAGS (s, ap->idx) & IS_LONG)) continue; CALC_LEN (ap->idx); } if (len > maxlen[0]) maxlen[0] = len; /* It's much easier to calculate length for description column! */ len = (int) strlen (DESC (s, ue->idx)); if (len > maxlen[1]) maxlen[1] = len; } /* Determine how much room we have, and how much we will allocate to each col. * Do not address pathological cases. Output will just be ugly. */ columns = get_cols () - 1; if (maxlen[0] + maxlen[1] + indent * 2 > columns) { /* col 0 gets whatever it wants. we'll wrap the desc col. */ maxlen[1] = columns - (maxlen[0] + indent * 2); if (maxlen[1] < 14) /* 14 is arbitrary lower limit on desc width. */ maxlen[1] = INT_MAX; } desccol = maxlen[0] + indent * 2; #define PRINT_SPACES(fp,n)\ do{\ int _n;\ _n=(n);\ while(_n-- > 0)\ fputc(' ',(fp));\ }while(0) /* Second pass (same as above loop), this time we print. */ /* Sloppy hack: We iterate twice. The first time we print short and long options. The second time we print those lines that have ONLY long options. */ while (print_run++ < 2) { for (ue = byr_val; ue; ue = ue->next) { usg_elem *ap; int nwords = 0, nchars = 0, has_short = 0; /* TODO: get has_short schtick to work */ has_short = !(FLAGS (s, ue->idx) & IS_LONG); for (ap = ue->alias; ap; ap = ap->next) { if (!(FLAGS (s, ap->idx) & IS_LONG)) { has_short = 1; break; } } if ((print_run == 1 && !has_short) || (print_run == 2 && has_short)) continue; PRINT_SPACES (fp, indent); nchars += indent; /* Print, adding a ", " between aliases. */ #define PRINT_IT(i) do{\ if(nwords++)\ nchars+=fprintf(fp,", ");\ nchars+=fprintf(fp,"%s",s->options[i].opt_fmt);\ }while(0) if (!(FLAGS (s, ue->idx) & IS_LONG)) PRINT_IT (ue->idx); /* print short aliases first. */ for (ap = ue->alias; ap; ap = ap->next) { if (!(FLAGS (s, ap->idx) & IS_LONG)) PRINT_IT (ap->idx); } if (FLAGS (s, ue->idx) & IS_LONG) PRINT_IT (ue->idx); /* repeat the above loop, this time for long aliases. */ for (ap = ue->alias; ap; ap = ap->next) { if (FLAGS (s, ap->idx) & IS_LONG) PRINT_IT (ap->idx); } /* pad to desccol */ PRINT_SPACES (fp, desccol - nchars); /* Print description, wrapped to maxlen[1] columns. */ if (1) { const char *pstart; pstart = DESC (s, ue->idx); while (1) { int n = 0; const char *lastws = NULL, *p; p = pstart; while (*p && n < maxlen[1] && *p != '\n') { if (isspace ((unsigned char)(*p)) || *p == '-') lastws = p; n++; p++; } if (!*p) { /* hit end of desc. done. */ fprintf (fp, "%s\n", pstart); break; } else if (*p == '\n') { /* print everything up to here then wrap. */ fprintf (fp, "%.*s\n", n, pstart); PRINT_SPACES (fp, desccol); pstart = p + 1; continue; } else { /* we hit the edge of the screen. wrap at space if possible. */ if (lastws) { fprintf (fp, "%.*s\n", (int)(lastws - pstart), pstart); pstart = lastws + 1; } else { fprintf (fp, "%.*s\n", n, pstart); pstart = p + 1; } PRINT_SPACES (fp, desccol); continue; } } } } } /* end while */ free (store); return 0; }
/*! * A const function that for every value in a vector calculates the matrix * exponential of the matrix multiplied with that value * The exponential is calculated by finding the eigenvalues and eigenvectors * of the matrix, exponentiating the eigenvalues. The eigenvalues is stored in * a matrix V, eigenvectors is stored in a matrix A, inv(A) is calculated. * The product A*V*inv(A) is returned. * @param s A vector with values to be multiplied with the matrix before * the exponent is calculated. * @return A vector with the exponential of the matrix multiplied with every * value in s */ MatVec Matrix::expm(const DblVec &s) const { // Can only calculate eigenvalues and vectors of square matrices if (get_rows() != get_cols()) throw std::out_of_range("Matrix needs to be square"); int size = get_rows(); DblVec eg_val_real(size, 0); // Real part of eigenvalues DblVec eg_val_im(size, 0); // Imaginary part of eigenvalues // should be zero double dummy[1]; int dummy_size = 1; double dummy_one = 1; int info[1]; char n = 'N'; // Do not want to use this argument char v = 'V'; // Want to use this argument double workspace_size[1]; int w_query = -1; // Need to make a copy of the data in Q to send into dgeev_ because // the data sent in is overwritten int data_size = get_rows()*get_cols(); DblVec data(m_data); // Matrix for the eigenvectors Matrix t_mat = Matrix(size, size); //workspace-query // SUBROUTINE DGEEV( JOBVL, JOBVR, N, A, LDA, WR, WI, VL, LDVL, VR, // LDVR, WORK, LWORK, INFO ) dgeev_(&n, &v, &size, &data[0], &size, &eg_val_real[0], &eg_val_im[0], dummy, &dummy_size, &t_mat.m_data[0], &size, workspace_size, &w_query, info); DblVec workspace_vec(static_cast<int>(workspace_size[0]), 0); int w_size = static_cast<int>(workspace_size[0]); // Real calculation of eigenvalues and eigenvectors for Q dgeev_(&n, &v, &size, &data[0], &size, &eg_val_real[0], &eg_val_im[0], dummy, &dummy_size, &t_mat.m_data[0], &size, &workspace_vec[0], &w_size, info); // Calculating inverse of matrix with eigenvectors Matrix t_mat_inv(t_mat); int ipiv[size]; // LU factorization, t_mat_inv.m_data is overwritten with the LU factorization dgetrf_(&size, &size, &t_mat_inv.m_data[0], &size, ipiv, info); //workspace-query, nothing happens with t_mat_inv.m_data dgetri_(&size, &t_mat_inv.m_data[0], &size, ipiv, workspace_size, &w_query, info); double workspace_vec2[static_cast<int>(workspace_size[0])]; w_size = static_cast<int>(workspace_size[0]); // Inverse calculation from LU values, the inverse is stored in t_mat_inv.m_data dgetri_(&size, &t_mat_inv.m_data[0], &size, ipiv, workspace_vec2, &w_size, info); MatVec result; result.reserve(s.size()); // e^(this) = T*D*T^-1 // T = matrix with eigenvectors (t_mat), D = matrix with exponentiated eigenvalues // Calculate for every value in incoming vector s DblVec eg_val_exp; eg_val_exp.reserve(size); for (DblVec::const_iterator it=s.begin(); it != s.end(); it++){ for (int i=0; i<size; i++) eg_val_exp.push_back(exp(eg_val_real[i]*(*it))); Matrix left = Matrix::mult(t_mat, Matrix(eg_val_exp)); Matrix res = Matrix::mult( left, t_mat_inv); result.push_back(res); eg_val_exp.clear(); } return result; }
/*! * A function that applies ln() on every element in the matrix. */ void Matrix::mlog(){ int size = get_rows()*get_cols(); for (int i=0; i<size; i++) (*this)(i) = log((*this)(i)); }