Пример #1
0
/*
 * stf_read_workbook:
 * @fo: file opener
 * @enc: encoding of file
 * @context: command context
 * @book: workbook
 * @input: file to read from+convert
 *
 * Main routine, handles importing a file including all dialog mumbo-jumbo
 */
static void
stf_read_workbook (G_GNUC_UNUSED GOFileOpener const *fo,  gchar const *enc,
		   GOIOContext *context, GoView *view, GsfInput *input)
{
	DialogStfResult_t *dialogresult = NULL;
	char *name, *nameutf8 = NULL;
	char *data = NULL;
	size_t data_len;
	WorkbookView *wbv = GNM_WORKBOOK_VIEW (view);

	if (!GNM_IS_WBC_GTK (context->impl)) {
		go_io_error_string (context, _("This importer can only be used with a GUI."));
		return;
	}

	name = g_path_get_basename (gsf_input_name (input));
	nameutf8 = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
	g_free (name);
	if (!nameutf8) {
		g_warning ("Failed to convert filename to UTF-8.  This shouldn't happen here.");
		goto out;
	}

	data = stf_preparse (context, input, &data_len);
	if (!data)
		goto out;

	dialogresult = stf_dialog (WBC_GTK (context->impl),
				   enc, FALSE, NULL, FALSE,
				   nameutf8, data, data_len);
	if (dialogresult != NULL) {
		Workbook *book = wb_view_get_workbook (wbv);
		int cols = dialogresult->colcount, rows = dialogresult->rowcount;
		Sheet *sheet;

		gnm_sheet_suggest_size (&cols, &rows);
		sheet = sheet_new (book, nameutf8, cols, rows);
		workbook_sheet_attach (book, sheet);
		if (stf_store_results (dialogresult, sheet, 0, 0)) {
			workbook_recalc_all (book);
			resize_columns (sheet);
			workbook_set_saveinfo
				(book,
				 GO_FILE_FL_WRITE_ONLY,
				 go_file_saver_for_id
				 ("Gnumeric_stf:stf_assistant"));
		} else {
			/* the user has cancelled */
			/* the caller should notice that we have no sheets */
			workbook_sheet_delete (sheet);
		}
	}

 out:
	g_free (nameutf8);
	g_free (data);
	if (dialogresult != NULL)
		stf_dialog_result_free (dialogresult);
}
Пример #2
0
void abcd::aijAugmentMatrix(std::vector<CompCol_Mat_double> &M)
{

    int nbcols = A.dim(1);
    std::map<int,std::vector<CompCol_Mat_double> > C;
    std::map<int,std::vector<int> > stCols;
#ifdef WIP
    double filter_c = dcntl[Controls::aug_filter];
#endif //WIP
    stC.assign(M.size(), -1);

    for( size_t i = 0; i < M.size() - 1; i++ ){
        for ( size_t j = i+1; j < M.size(); j++ ) {
            std::vector<int> intersect;
            std::set_intersection(column_index[i].begin(),
                                  column_index[i].end(),
                                  column_index[j].begin(),
                                  column_index[j].end(),
                                  std::back_inserter(intersect));

            if (intersect.empty()) continue;

            CompCol_Mat_double A_ij(sub_matrix(M[i], intersect));
            CompCol_Mat_double A_ji(sub_matrix(M[j], intersect));

            double *jv = A_ji.val_ptr();
            for (int k = 0; k < A_ji.NumNonzeros(); k++) {
                jv[k] *= -1.0;
            }
            

#ifdef WIP
            if(filter_c != 0 || icntl[Controls::aug_iterative] != 0) {
                std::vector<int> selected_cols;
                std::vector<double> frob_ij, mu;

                frob_ij.reserve(A_ij.dim(1));
                mu.reserve(A_ij.dim(1));
                double card_max = 0;
                double frob_sum = 0;
                double nu;

                for (int k = 0; k < A_ij.dim(1); ++k){
                    VECTOR_int A_ij_k_ind, A_ji_k_ind;
                    VECTOR_double A_ij_k = middleCol(A_ij, k, A_ij_k_ind);
                    VECTOR_double A_ji_k = middleCol(A_ji, k, A_ji_k_ind);

                    double card_current = A_ij_k_ind.size() * A_ji_k_ind.size();

                    // exploit the sparcity of the vectors!
                    frob_ij.push_back(sqrt( squaredNorm(A_ij_k, A_ij_k_ind) * squaredNorm(A_ji_k, A_ji_k_ind)));
                    frob_sum += frob_ij[k];

                    card_max = card_max > card_current ? card_max : card_current;
                }

                nu = (frob_sum / frob_ij.size()) / sqrt(card_max);

                for (int k = 0; k < A_ij.dim(1); ++k){
                    VECTOR_int A_ij_k_ind, A_ji_k_ind;
                    VECTOR_double A_ij_k = middleCol(A_ij, k, A_ij_k_ind);
                    VECTOR_double A_ji_k = middleCol(A_ji, k, A_ji_k_ind);

                    double inf_ij = infNorm(A_ij_k);
                    double inf_ji = infNorm(A_ji_k);

                    double p = 0, q = 0;
                    for (int l = 0; l < A_ij_k_ind.size(); ++l){
                        if (abs(A_ij_k(A_ij_k_ind(l))) >= nu/inf_ji) p++;
                    }
                    for (int l = 0; l < A_ji_k_ind.size(); ++l){
                        if (abs(A_ji_k(A_ji_k_ind(l))) >= nu/inf_ij) q++;
                    }

                    p = ( p==0 ? A_ij_k_ind.size() : p );
                    q = ( q==0 ? A_ji_k_ind.size() : q );

                    double mu_ij_k = frob_ij[k] / sqrt(p*q);
                    mu.push_back(mu_ij_k);

                    if(mu_ij_k >= filter_c){
                        selected_cols.push_back(k);
                    }

                    if(icntl[Controls::aug_iterative] != 0){ 
                        if (dcntl[Controls::aug_precond] < 0) {
                            if ((nbcols + k - n_o) % abs((int)dcntl[Controls::aug_precond]) == 0)
                                selected_S_columns.push_back( nbcols + k - n_o);
                            else
                                skipped_S_columns.push_back( nbcols + k - n_o);
                        } else {
                            if (mu_ij_k >= dcntl[Controls::aug_precond])
                                selected_S_columns.push_back( nbcols + k - n_o);
                            else
                                skipped_S_columns.push_back( nbcols + k - n_o);
                        }
                    }

                }

                if (selected_cols.empty()) continue;

                if( icntl[Controls::aug_iterative] != 2 ) { // don't reduce the A_ij/A_ji, we just need the selected columns!
                    A_ij = sub_matrix(A_ij, selected_cols);
                    A_ji = sub_matrix(A_ji, selected_cols);
                }
            }
#endif //WIP

            stCols[i].push_back(nbcols);
            stCols[j].push_back(nbcols);
            C[i].push_back(A_ij);
            C[j].push_back(A_ji);

            nbcols += A_ij.dim(1);
        }
    }

    size_c = nbcols - A.dim(1);
    n = nbcols;
    LINFO << "Size of C : " << size_c;

#ifdef WIP    
    if(icntl[Controls::aug_analysis] != 0) return;
#endif // WIP    

    // Augment the matrices
    for(size_t k = 0; k < M.size(); k++){
        if(stCols[k].size() == 0) continue;
        // now augment each partition!
        stC[k] = stCols[k][0];
        M[k] = concat_columns(M[k], C[k], stCols[k]);
        M[k] = resize_columns(M[k], nbcols);
    }
}
Пример #3
0
/*
 * stf_read_workbook_auto_csvtab:
 * @fo: file opener
 * @enc: optional encoding
 * @context: command context
 * @book: workbook
 * @input: file to read from+convert
 *
 * Attempt to auto-detect CSV or tab-delimited file
 */
static void
stf_read_workbook_auto_csvtab (G_GNUC_UNUSED GOFileOpener const *fo, gchar const *enc,
			       GOIOContext *context,
			       GoView *view, GsfInput *input)
{
	Sheet *sheet;
	Workbook *book;
	char *name;
	char *data;
	GString *utf8data;
	size_t data_len;
	StfParseOptions_t *po;
	const char *gsfname;
	int cols, rows, i;
	GStringChunk *lines_chunk;
	GPtrArray *lines;
	WorkbookView *wbv = GNM_WORKBOOK_VIEW (view);

	g_return_if_fail (context != NULL);
	g_return_if_fail (wbv != NULL);

	book = wb_view_get_workbook (wbv);

	data = stf_preparse (context, input, &data_len);
	if (!data)
		return;

	enc = go_guess_encoding (data, data_len, enc, &utf8data, NULL);
	g_free (data);

	if (!enc) {
		go_cmd_context_error_import (GO_CMD_CONTEXT (context),
				     _("That file is not in the given encoding."));
		return;
	}

	clear_stray_NULs (context, utf8data);

	/*
	 * Try to get the filename we're reading from.  This is not a
	 * great way.
	 */
	gsfname = gsf_input_name (input);

	{
		const char *ext = gsf_extension_pointer (gsfname);
		gboolean iscsv = ext && strcasecmp (ext, "csv") == 0;
		if (iscsv)
			po = stf_parse_options_guess_csv (utf8data->str);
		else
			po = stf_parse_options_guess (utf8data->str);
	}

	lines_chunk = g_string_chunk_new (100 * 1024);
	lines = stf_parse_general (po, lines_chunk,
				   utf8data->str, utf8data->str + utf8data->len);
	rows = lines->len;
	cols = 0;
	for (i = 0; i < rows; i++) {
		GPtrArray *line = g_ptr_array_index (lines, i);
		cols = MAX (cols, (int)line->len);
	}
	gnm_sheet_suggest_size (&cols, &rows);
	stf_parse_general_free (lines);
	g_string_chunk_free (lines_chunk);

	name = g_path_get_basename (gsfname);
	sheet = sheet_new (book, name, cols, rows);
	g_free (name);
	workbook_sheet_attach (book, sheet);

	if (stf_parse_sheet (po, utf8data->str, NULL, sheet, 0, 0)) {
		gboolean is_csv;
		workbook_recalc_all (book);
		resize_columns (sheet);
		if (po->cols_exceeded || po->rows_exceeded) {
			stf_warning (context,
				     _("Some data did not fit on the "
				       "sheet and was dropped."));
		}
		is_csv = po->sep.chr && po->sep.chr[0] == ',';
		workbook_set_saveinfo
			(book,
			 GO_FILE_FL_WRITE_ONLY,
			 go_file_saver_for_id
			 (is_csv ? "Gnumeric_stf:stf_csv" : "Gnumeric_stf:stf_assistant"));
	} else {
		workbook_sheet_delete (sheet);
		go_cmd_context_error_import (GO_CMD_CONTEXT (context),
			_("Parse error while trying to parse data into sheet"));
	}


	stf_parse_options_free (po);
	g_string_free (utf8data, TRUE);
}