FdmMesherComposite::FdmMesherComposite( const boost::shared_ptr<Fdm1dMesher>& m1, const boost::shared_ptr<Fdm1dMesher>& m2, const boost::shared_ptr<Fdm1dMesher>& m3) : FdmMesher(getLayoutFromMeshers(build_vector(m1, m2, m3))), mesher_(build_vector(m1, m2, m3)) { }
/** * Return the coefficients for MUL (left multiplication): a NUM_BLOCKS * ROWS * by NUM_BLOCKS * COLS block diagonal matrix where each diagonal block is the * constant data BLOCK. * * Parameters: linOp with type MUL * * Returns: vector containing coefficient matrix COEFFS * */ std::vector<Matrix> get_mul_mat(LinOp &lin) { assert(lin.type == MUL); Matrix block = get_constant_data(lin, false); int block_rows = block.rows(); int block_cols = block.cols(); // Don't replicate scalars if(block_rows == 1 && block_cols == 1){ return build_vector(block); } int num_blocks = lin.size[1]; Matrix coeffs (num_blocks * block_rows, num_blocks * block_cols); std::vector<Triplet> tripletList; tripletList.reserve(num_blocks * block.nonZeros()); for (int curr_block = 0; curr_block < num_blocks; curr_block++) { int start_i = curr_block * block_rows; int start_j = curr_block * block_cols; for ( int k = 0; k < block.outerSize(); ++k ) { for ( Matrix::InnerIterator it(block, k); it; ++it ) { tripletList.push_back(Triplet(start_i + it.row(), start_j + it.col(), it.value())); } } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for INDEX: a N by ROWS*COLS matrix * where N is the number of total elements in the slice. Element i, j * is 1 if element j in the vectorized matrix is the i-th element of the * slice and 0 otherwise. * * Parameters: LinOp of type INDEX * * Returns: vector containing coefficient matrix COEFFS * */ std::vector<Matrix> get_index_mat(LinOp &lin) { assert(lin.type == INDEX); int rows = lin.args[0]->size[0]; int cols = lin.args[0]->size[1]; Matrix coeffs (lin.size[0] * lin.size[1], rows * cols); /* If slice is empty, return empty matrix */ if (coeffs.rows () == 0 || coeffs.cols() == 0) { return build_vector(coeffs); } std::vector<std::vector<int> > slices = get_slice_data(lin, rows, cols); /* Row Slice Data */ int row_start = slices[0][0]; int row_end = slices[0][1]; int row_step = slices[0][2]; /* Column Slice Data */ int col_start = slices[1][0]; int col_end = slices[1][1]; int col_step = slices[1][2]; /* Set the index coefficients by looping over the column selection * first to remain consistent with CVXPY. */ std::vector<Triplet> tripletList; int col = col_start; int counter = 0; while (true) { if (col < 0 || col >= cols) { break; } int row = row_start; while (true) { if (row < 0 || row >= rows) { break; } int row_idx = counter; int col_idx = col * rows + row; tripletList.push_back(Triplet(row_idx, col_idx, 1.0)); counter++; row += row_step; if ((row_step > 0 && row >= row_end) || (row_step < 0 && row <= row_end)) { break; } } col += col_step; if ((col_step > 0 && col >= col_end) || (col_step < 0 && col <= col_end)) { break; } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for RMUL (right multiplication): a ROWS * N * by COLS * N matrix given by the kronecker product between the * transpose of the constant matrix CONSTANT and a N x N identity matrix. * * Parameters: linOp of type RMUL * * Returns: vector containing the corresponding coefficient matrix COEFFS * */ std::vector<Matrix> get_rmul_mat(LinOp &lin) { assert(lin.type == RMUL); Matrix constant = get_constant_data(lin, false); int rows = constant.rows(); int cols = constant.cols(); int n = lin.size[0]; Matrix coeffs(cols * n, rows * n); std::vector<Triplet> tripletList; tripletList.reserve(n * constant.nonZeros()); for ( int k = 0; k < constant.outerSize(); ++k ) { for ( Matrix::InnerIterator it(constant, k); it; ++it ) { double val = it.value(); // each element of CONSTANT occupies an N x N block in the matrix int row_start = it.col() * n; int col_start = it.row() * n; for (int i = 0; i < n; i++) { int row_idx = row_start + i; int col_idx = col_start + i; tripletList.push_back(Triplet(row_idx, col_idx, val)); } } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for UPPER_TRI: an ENTRIES by ROWS * COLS matrix * where the i, j entry in the original matrix has a 1 in row COUNT and * corresponding column if j > i and 0 otherwise. * * Parameters: LinOp with type UPPER_TRI. * Returns: vector of coefficients for upper triangular matrix linOp */ std::vector<Matrix> get_upper_tri_mat(LinOp &lin) { assert(lin.type == UPPER_TRI); int rows = lin.args[0]->size[0]; int cols = lin.args[0]->size[1]; int entries = lin.size[0]; Matrix coeffs(entries, rows * cols); std::vector<Triplet> tripletList; tripletList.reserve(entries); int count = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (j > i) { // index in the extracted vector int row_idx = count; count++; // index in the original matrix int col_idx = j * rows + i; tripletList.push_back(Triplet(row_idx, col_idx, 1.0)); } } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for KRON. * * Parameters: linOp LIN with type KRON * Returns: vector containing the coefficient matrix for the Kronecker product. */ std::vector<Matrix> get_kron_mat(LinOp &lin) { assert(lin.type == KRON); Matrix constant = get_constant_data(lin, false); int lh_rows = constant.rows(); int lh_cols = constant.cols(); int rh_rows = lin.args[0]->size[0]; int rh_cols = lin.args[0]->size[1]; int rows = rh_rows * rh_cols * lh_rows * lh_cols; int cols = rh_rows * rh_cols; Matrix coeffs(rows, cols); std::vector<Triplet> tripletList; tripletList.reserve(rh_rows * rh_cols * constant.nonZeros()); for ( int k = 0; k < constant.outerSize(); ++k ) { for ( Matrix::InnerIterator it(constant, k); it; ++it ) { int row = (rh_rows * rh_cols * (lh_rows * it.col())) + (it.row() * rh_rows); int col = 0; for(int j = 0; j < rh_cols; j++){ for(int i = 0; i < rh_rows; i++) { tripletList.push_back(Triplet(row + i, col, it.value())); col++; } row += lh_rows * rh_rows; } } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for RESHAPE: a 1x1 matrix [1]. In Eigen, this * requires special case handling to multiply against an arbitrary m x n * matrix. * * Parameters: LinOp with type RESHAPE * * Returns: vector containing the coefficient matrix ONE. * */ std::vector<Matrix> get_reshape_mat(LinOp &lin) { assert(lin.type == RESHAPE); Matrix one(1, 1); one.insert(0, 0) = 1; one.makeCompressed(); return build_vector(one); }
/** * Return the coefficients for PROMOTE: a column vector of size N with all * entries 1. Note this is treated as sparse for consistency of later * multiplications with sparse matrices. * * Parameters: linOP with type PROMOTE * * Returns: vector containing coefficient matrix ONES. * */ std::vector<Matrix> get_promote_mat(LinOp &lin) { assert(lin.type == PROMOTE); int num_entries = lin.size[0] * lin.size[1]; Matrix ones = sparse_ones(num_entries, 1); ones.makeCompressed(); return build_vector(ones); }
/** * Return the coefficients for NEG: -I, where I is an identity of size m * n. * * Parameters: linOp with type NEG * * Returns: vector containing the coefficient matrix COEFFS */ std::vector<Matrix> get_neg_mat(LinOp &lin) { assert(lin.type == NEG); int n = lin.size[0] * lin.size[1]; Matrix coeffs = sparse_eye(n); coeffs *= -1; coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficient matrix for SUM_ENTRIES. A single row vector of 1's * of size 1 by (data.rows x data.cols). * * Parameters: LinOp with type SUM_ENTRIES * * Returns: vector containing the coefficient matrix COEFFS */ std::vector<Matrix> get_sum_entries_mat(LinOp &lin) { assert(lin.type == SUM_ENTRIES); // assumes all args have the same size int rows = lin.args[0]->size[0]; int cols = lin.args[0]->size[1]; Matrix coeffs = sparse_ones(1, rows * cols); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for DIV: a diagonal matrix where each diagonal * entry is 1 / DIVISOR. * * Parameters: linOp with type DIV * * Returns: vector containing the coefficient matrix COEFFS * */ std::vector<Matrix> get_div_mat(LinOp &lin) { assert(lin.type == DIV); // assumes scalar divisor double divisor = get_divisor_data(lin); int n = lin.size[0] * lin.size[1]; Matrix coeffs = sparse_eye(n); coeffs /= divisor; coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for TRACE: A single row vector v^T \in R^(n^2) * with 1 if v_{i} corresponds to a diagonal entry (i.e. i * n + i) and 0 * otherwise. * * Parameters: LinOp with type TRACE * * Returns: vector containing the coefficient matrix COEFFS * */ std::vector<Matrix> get_trace_mat(LinOp &lin) { assert(lin.type == TRACE); int rows = lin.args[0]->size[0]; Matrix coeffs (1, rows * rows); for (int i = 0; i < rows; i++) { coeffs.insert(0, i * rows + i) = 1; } coeffs.makeCompressed(); return build_vector(coeffs); }
std::unordered_set<int> const build_unordered_set() { typedef std::unordered_set<int> int_set; typedef std::vector<int> int_vector; int_set result; int_vector const data = build_vector(); int_vector::const_iterator it = data.begin(); int_vector::const_iterator const end = data.end(); result.insert(it, end); return result; }
std::map<int, int> const build_map() { typedef std::map<int, int> int_map; typedef std::vector<int> int_vector; int_map result; int_vector const data = build_vector(); int_vector::const_iterator it = data.begin(); int_vector::const_iterator const end = data.end(); for (; it != end; ++it) { int const value = *it; result[value] = 100 * value; } return result; }
static tree rewrite_reciprocal (block_stmt_iterator *bsi) { tree stmt, lhs, rhs, stmt1, stmt2, var, name, tmp; tree real_one; stmt = bsi_stmt (*bsi); lhs = GENERIC_TREE_OPERAND (stmt, 0); rhs = GENERIC_TREE_OPERAND (stmt, 1); /* stmt must be GIMPLE_MODIFY_STMT. */ var = create_tmp_var (TREE_TYPE (rhs), "reciptmp"); add_referenced_var (var); DECL_GIMPLE_REG_P (var) = 1; if (TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE) { int i, len; tree list = NULL_TREE; real_one = build_real (TREE_TYPE (TREE_TYPE (rhs)), dconst1); len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)); for (i = 0; i < len; i++) list = tree_cons (NULL, real_one, list); real_one = build_vector (TREE_TYPE (rhs), list); } else real_one = build_real (TREE_TYPE (rhs), dconst1); tmp = build2 (RDIV_EXPR, TREE_TYPE (rhs), real_one, TREE_OPERAND (rhs, 1)); stmt1 = build_gimple_modify_stmt (var, tmp); name = make_ssa_name (var, stmt1); GIMPLE_STMT_OPERAND (stmt1, 0) = name; tmp = build2 (MULT_EXPR, TREE_TYPE (rhs), name, TREE_OPERAND (rhs, 0)); stmt2 = build_gimple_modify_stmt (lhs, tmp); /* Replace division stmt with reciprocal and multiply stmts. The multiply stmt is not invariant, so update iterator and avoid rescanning. */ bsi_replace (bsi, stmt1, true); bsi_insert_after (bsi, stmt2, BSI_NEW_STMT); SSA_NAME_DEF_STMT (lhs) = stmt2; /* Continue processing with invariant reciprocal statement. */ return stmt1; }
/** * Return the coefficients for DIAG_VEC (vector to diagonal matrix): a * N^2 by N matrix where each column I has a 1 in row I * N + I * corresponding to the diagonal entry and 0 otherwise. * * Parameters: linOp of type DIAG_VEC * * Returns: vector containing coefficient matrix COEFFS * */ std::vector<Matrix> get_diag_vec_mat(LinOp &lin) { assert(lin.type == DIAG_VEC); int rows = lin.size[0]; Matrix coeffs(rows * rows, rows); std::vector<Triplet> tripletList; tripletList.reserve(rows); for (int i = 0; i < rows; i++) { // index in the diagonal matrix int row_idx = i * rows + i; //index in the original vector int col_idx = i; tripletList.push_back(Triplet(row_idx, col_idx, 1.0)); } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for MUL_ELEM: an N x N diagonal matrix where the * n-th element on the diagonal corresponds to the element n = j*rows + i in * the data matrix CONSTANT. * * Parameters: linOp of type MUL_ELEM * * Returns: vector containing the coefficient matrix COEFFS * */ std::vector<Matrix> get_mul_elemwise_mat(LinOp &lin) { assert(lin.type == MUL_ELEM); Matrix constant = get_constant_data(lin, true); int n = constant.rows(); // build a giant diagonal matrix std::vector<Triplet> tripletList; tripletList.reserve(n); for ( int k = 0; k < constant.outerSize(); ++k ) { for ( Matrix::InnerIterator it(constant, k); it; ++it ) { tripletList.push_back(Triplet(it.row(), it.row(), it.value())); } } Matrix coeffs(n, n); coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for TRANSPOSE: a ROWS*COLS by ROWS*COLS matrix * such that element ij in the vectorized matrix is mapped to ji after * multiplication (i.e. entry (rows * j + i, i * cols + j) = 1 and else 0) * * Parameters: linOp of type TRANSPOSE * * Returns: vector containing coefficient matrix COEFFS * */ std::vector<Matrix> get_transpose_mat(LinOp &lin) { assert(lin.type == TRANSPOSE); int rows = lin.size[0]; int cols = lin.size[1]; Matrix coeffs(rows * cols, rows * cols); std::vector<Triplet> tripletList; tripletList.reserve(rows * cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int row_idx = rows * j + i; int col_idx = i * cols + j; tripletList.push_back(Triplet(row_idx, col_idx, 1.0)); } } coeffs.setFromTriplets(tripletList.begin(), tripletList.end()); coeffs.makeCompressed(); return build_vector(coeffs); }
/** * Return the coefficients for CONV operator. The coefficient matrix is * constructed by creating a toeplitz matrix with the constant vector * in DATA as the columns. Multiplication by this matrix is equivalent * to convolution. * * Parameters: linOp LIN with type CONV. Data should should contain a * column vector that the variables are convolved with. * * Returns: vector of coefficients for convolution linOp */ std::vector<Matrix> get_conv_mat(LinOp &lin) { assert(lin.type == CONV); Matrix constant = get_constant_data(lin, false); int rows = lin.size[0]; int nonzeros = constant.rows(); int cols = lin.args[0]->size[0]; Matrix toeplitz(rows, cols); std::vector<Triplet> tripletList; tripletList.reserve(nonzeros * cols); for (int col = 0; col < cols; col++) { int row_start = col; for ( int k = 0; k < constant.outerSize(); ++k ) { for ( Matrix::InnerIterator it(constant, k); it; ++it ) { int row_idx = row_start + it.row(); tripletList.push_back(Triplet(row_idx, col, it.value())); } } } toeplitz.setFromTriplets(tripletList.begin(), tripletList.end()); toeplitz.makeCompressed(); return build_vector(toeplitz); }
std::deque<int> const build_deque() { std::vector<int> const data = build_vector(); return std::deque<int>(data.begin(), data.end()); }
std::list<int> const build_list() { std::vector<int> const data = build_vector(); return std::list<int>(data.begin(), data.end()); }
int main(int argc,char **argv) { char stdi=0; double *pm; long i,j; FILE *file; double **mat,**inverse,*vec,**coeff,**diff,avpm; if (scan_help(argc,argv)) show_options(argv[0]); scan_options(argc,argv); #ifndef OMIT_WHAT_I_DO if (verbosity&VER_INPUT) what_i_do(argv[0],WID_STR); #endif infile=search_datafile(argc,argv,NULL,verbosity); if (infile == NULL) stdi=1; if (outfile == NULL) { if (!stdi) { check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1)); strcpy(outfile,infile); strcat(outfile,".ar"); } else { check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1)); strcpy(outfile,"stdin.ar"); } } if (!stdo) test_outfile(outfile); if (column == NULL) series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset, verbosity); else series=(double**)get_multi_series(infile,&length,exclude,&dim,column, dimset,verbosity); check_alloc(my_average=(double*)malloc(sizeof(double)*dim)); set_averages_to_zero(); if (poles >= length) { fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); exit(AR_MODEL_TOO_MANY_POLES); } check_alloc(vec=(double*)malloc(sizeof(double)*poles*dim)); check_alloc(mat=(double**)malloc(sizeof(double*)*poles*dim)); for (i=0;i<poles*dim;i++) check_alloc(mat[i]=(double*)malloc(sizeof(double)*poles*dim)); check_alloc(coeff=(double**)malloc(sizeof(double*)*dim)); inverse=build_matrix(mat); for (i=0;i<dim;i++) { build_vector(vec,i); coeff[i]=multiply_matrix_vector(inverse,vec); } check_alloc(diff=(double**)malloc(sizeof(double*)*dim)); for (i=0;i<dim;i++) check_alloc(diff[i]=(double*)malloc(sizeof(double)*length)); pm=make_residuals(diff,coeff); if (stdo) { avpm=pm[0]*pm[0]; for (i=1;i<dim;i++) avpm += pm[i]*pm[i]; avpm=sqrt(avpm/dim); printf("#average forcast error= %e\n",avpm); printf("#individual forecast errors: "); for (i=0;i<dim;i++) printf("%e ",pm[i]); printf("\n"); for (i=0;i<dim*poles;i++) { printf("# "); for (j=0;j<dim;j++) printf("%e ",coeff[j][i]); printf("\n"); } if (!run_model || (verbosity&VER_USR1)) { for (i=poles;i<length;i++) { if (run_model) printf("#"); for (j=0;j<dim;j++) if (verbosity&VER_USR2) printf("%e %e ",series[j][i]+my_average[j],diff[j][i]); else printf("%e ",diff[j][i]); printf("\n"); } } if (run_model && (ilength > 0)) iterate_model(coeff,pm,NULL); } else { file=fopen(outfile,"w"); if (verbosity&VER_INPUT) fprintf(stderr,"Opened %s for output\n",outfile); avpm=pm[0]*pm[0]; for (i=1;i<dim;i++) avpm += pm[i]*pm[i]; avpm=sqrt(avpm/dim); fprintf(file,"#average forcast error= %e\n",avpm); fprintf(file,"#individual forecast errors: "); for (i=0;i<dim;i++) fprintf(file,"%e ",pm[i]); fprintf(file,"\n"); for (i=0;i<dim*poles;i++) { fprintf(file,"# "); for (j=0;j<dim;j++) fprintf(file,"%e ",coeff[j][i]); fprintf(file,"\n"); } if (!run_model || (verbosity&VER_USR1)) { for (i=poles;i<length;i++) { if (run_model) fprintf(file,"#"); for (j=0;j<dim;j++) if (verbosity&VER_USR2) fprintf(file,"%e %e ",series[j][i]+my_average[j],diff[j][i]); else fprintf(file,"%e ",diff[j][i]); fprintf(file,"\n"); } } if (run_model && (ilength > 0)) iterate_model(coeff,pm,file); fclose(file); } if (outfile != NULL) free(outfile); if (infile != NULL) free(infile); free(vec); for (i=0;i<poles*dim;i++) { free(mat[i]); free(inverse[i]); } free(mat); free(inverse); for (i=0;i<dim;i++) { free(coeff[i]); free(diff[i]); } free(coeff); free(diff); free(pm); return 0; }
/* Build a Scheme expression from an action stack. */ static bool build(bool init, obj_t *actions, obj_t **obj_out) { if (init) { ACTION_BEGIN_LIST = make_C_procedure(&&begin_list, NIL, NIL); ACTION_BEGIN_VECTOR = make_C_procedure(&&begin_vector, NIL, NIL); ACTION_BEGIN_BYTEVEC = make_C_procedure(&&begin_bytevector, NIL, NIL); ACTION_ABBREV = make_C_procedure(&&abbrev, NIL, NIL); ACTION_END_SEQUENCE = make_C_procedure(&&end_sequence, NIL, NIL); ACTION_DOT_END = make_C_procedure(&&dot_end, NIL, NIL); ACTION_DISCARD = make_C_procedure(&&discard, NIL, NIL); return false; } PUSH_ROOT(actions); AUTO_ROOT(vstack, NIL); AUTO_ROOT(reg, NIL); AUTO_ROOT(tmp, NIL); while (!stack_is_empty(actions)) { obj_t *op = stack_pop(&actions); if (is_procedure(op) && procedure_is_C(op)) goto *procedure_body(op); /* default: */ reg = make_pair(op, reg); continue; begin_list: reg = make_pair(reg, stack_pop(&vstack)); continue; begin_vector: reg = build_vector(reg); reg = make_pair(reg, stack_pop(&vstack)); continue; begin_bytevector: reg = build_bytevec(reg); reg = make_pair(reg, stack_pop(&vstack)); continue; abbrev: tmp = make_pair(pair_cadr(reg), NIL); tmp = make_pair(pair_car(reg), tmp); reg = make_pair(tmp, pair_cddr(reg)); continue; end_sequence: stack_push(&vstack, reg); reg = NIL; continue; dot_end: stack_push(&vstack, pair_cdr(reg)); reg = pair_car(reg); continue; discard: reg = pair_cdr(reg); continue; } assert(stack_is_empty(vstack)); bool success = false; if (!is_null(reg)) { assert(is_null(pair_cdr(reg))); *obj_out = pair_car(reg); success = true; } POP_FUNCTION_ROOTS(); return success; }