void ip_transpose( std::pair<T*, T*> const& d, stride_type row_stride, stride_type col_stride, length_type rows, length_type cols) { ip_transpose(d.first, row_stride, col_stride, rows, cols); ip_transpose(d.second, row_stride, col_stride, rows, cols); }
static void exec(DstBlock& dst, SrcBlock const& src, row2_type, col2_type) { vsip::impl::Ext_data<DstBlock> dst_ext(dst, vsip::impl::SYNC_OUT); vsip::impl::Ext_data<SrcBlock> src_ext(src, vsip::impl::SYNC_IN); // In-place transpose if (is_same_ptr(dst_ext.data(), src_ext.data())) { // in-place transpose implies square matrix assert(dst.size(2, 0) == dst.size(2, 1)); ip_transpose(dst_ext.data(), dst_ext.stride(0), dst_ext.stride(1), dst.size(2, 0), dst.size(2, 1)); } else if (dst_ext.stride(1) == 1 && src_ext.stride(0) == 1) { transpose_unit(dst_ext.data(), src_ext.data(), dst.size(2, 1), dst.size(2, 0), // rows, cols dst_ext.stride(0), // dst_col_stride src_ext.stride(1)); // src_row_stride } else { transpose(dst_ext.data(), src_ext.data(), dst.size(2, 1), dst.size(2, 0), // rows, cols dst_ext.stride(1), dst_ext.stride(0), // dst strides src_ext.stride(1), src_ext.stride(0)); // srd strides } }
int ya_string_to_mat<eltype>::convert(const string &value, ya_type &mat) { int err_flag = 0; int ascii_flag = 0; string temp=value; // Transpose the matrix? bool transposeme=false; if (temp[temp.length()-1]=='\'') { temp[temp.length()-1]=' '; transposeme=true; } // Get the rows size_t tL=temp.length(); for (size_t i=0; i<tL; i++) if (temp[i]=='[' || temp[i]==']' || temp[i]==',') temp[i]=' '; vector<string> row_str; ya_get_tokens(';',temp,row_str); if (row_str.empty()) { mat.clear(); return err_flag; } ya_sizet row_size=row_str.size(); if (ya_whitespace(row_str[row_size-1])) row_size--; if (row_size==0) { mat.clear(); return err_flag; } // Get the columns for (ya_sizet i=0; i<row_size; i++) { vector<string> col_str; ya_get_tokens(row_str[i], col_str); if (col_str.empty()) { ostringstream owarn; owarn << "Error parsing matrix string:\n" << value << "\nat row token: " << row_str[i]; ya_addwarn(307,11,"YA_BaseT",owarn.str()); err_flag = 307; if (i==0) return err_flag; } YA_DynT rvec; size_t cS=col_str.size(); for (size_t j=0; j<cS; j++) { vector<string> e_tok; ya_get_tokens(':',col_str[j],e_tok); if (e_tok.size()>3) { ostringstream owarn; owarn << "Error parsing matrix string:\n" << value << "\nat token: " << col_str[j]; ya_addwarn(307,11,"YA_BaseT",owarn.str()); err_flag=307; if (i==0) return err_flag; } eltype jump=1; ya_sizet high_index=1; if (e_tok.size()==3) { high_index=2; ascii_flag=ya_ascii_to(e_tok[1],jump); } if (e_tok.size()>1) { eltype one,two; ascii_flag=std::max(ascii_flag,ya_ascii_to(e_tok[0],one)); ascii_flag=std::max(ascii_flag,ya_ascii_to(e_tok[high_index],two)); double intp; if (modf(static_cast<double>(two-one)/double(jump),&intp) != 0) two=static_cast<eltype>(static_cast<double>(jump)*intp+one); if ( (one>two && static_cast<int>(jump)>0) || (one<two && static_cast<int>(jump)<0) || jump==0) { ostringstream owarn; owarn << "Error parsing matrix string:\n" << value << "\nat token: " << col_str[j]; ya_addwarn(307,11,"YA_BaseT",owarn.str()); err_flag=307; if (i==0) return err_flag; } for (eltype k=one; ; k+=jump) { rvec.push_back(k); if (k==two) break; } } else { eltype one; ascii_flag=std::max(ascii_flag,ya_ascii_to(e_tok[0],one)); rvec.push_back(one); } } if (i==0) mat.setup(row_size,rvec.numel()); else if (rvec.numel()!=mat.cols()) { ostringstream owarn; owarn << "Error parsing matrix string:\n" << value << "\nat token: " << row_str[i] << "\nAll rows must have the same number of " << "columns"; ya_addwarn(307,11,"YA_BaseT",owarn.str()); err_flag=307; } mat(i,":")=rvec; } if (transposeme) ip_transpose(mat); return std::max(ascii_flag,err_flag); }
int main(int argc, char *argv[]) { CommandLine cl; MathRandom<MathMersenneTwister> rng; Error error; ya_check_debug(); YA_MatD input_matrix; YA_MatD output_matrix; // Parse the command line HandleArgs(cl,argc,argv,&error); string outputfile=""; if (cl.argsize(' ')>0) { load(cl.argstring(' ',0),input_matrix); if (cl.argsize(' ')>1) outputfile=cl.argstring(' ',1); } else read(cin,input_matrix); // Select rows if (cl['r']) { output_matrix=input_matrix(YA_RowI(cl.argstring('r',0)),":"); input_matrix=output_matrix; } // Select cols if (cl['c']) { output_matrix=input_matrix(":",YA_RowI(cl.argstring('c',0))); input_matrix=output_matrix; } // Reorder rows using modulus else if (cl['z']) { ya_sizet mod=cl.argint('z',0); if (mod==0) error.generate_error(0,"vm_slice","Cannot specify a mod_num of 0."); if (input_matrix.rows()%mod!=0) { error.buffer() << "When using -z, the number of rows in the matrix " << "must be evenly divisible by the mod_num."; error.addbuf(0,"vm_slice"); } YA_VecI row_order(input_matrix.rows()); ya_sizet offset=input_matrix.rows()/mod; for (ya_sizet i=0; i<input_matrix.rows(); i++) { div_t index=div(int(i),int(mod)); row_order(i)=index.quot+index.rem*offset; } output_matrix=input_matrix(row_order,":"); } else output_matrix=input_matrix; ya_sizet file_format=YA_DEFAULT_IO; if (cl['t']) file_format=YA_PRETTY_IO; if (cl['b']) file_format=YA_BINARY_IO; // Random subset if (cl['s']) { double percent=cl.argdouble('s',0); if (percent>1) error.generate_error(0,"mat_convert", "Random percentage must be between 0 and 1"); YA_RowI rand_perm(randperm(output_matrix.rows(),rng)); output_matrix=copy(output_matrix(rand_perm,":")); ya_sizet cut_frac=ya_sizet(percent*output_matrix.rows()); if (cl.argstring('s',1)!="NO_OUTPUT") save(cl.argstring('s',1), output_matrix(vmcount(cut_frac,":",output_matrix.rows()-1),":"), file_format); output_matrix=copy(output_matrix(vmcount(cut_frac),":")); } if (cl['q']) ip_transpose(output_matrix); if (outputfile=="") write(cout,output_matrix,file_format); else save(outputfile,output_matrix,file_format); return 0; }