//根据先序和中序遍历结果构建树 void bulid_by_preorder_inorder(){ int apreoder[] = { 1, 2, 4, 3, 5, 6}; int ainoder[] = { 4, 2, 1, 5, 3, 6}; int len = sizeof(apreoder)/sizeof(int); vector<int> preoder = to_vector(apreoder,len); vector<int> inoder = to_vector(ainoder,len); treenode* root = bulid_by_preorder_inorder(preoder,inoder,0,len-1,0,len-1); cout<<root->data<<endl; levelorder(root); }
void setup_vertex_normal_buffer_object(bool smoothed) { std::vector<glm::vec3> vertices = trig.Vertices(); std::vector<glm::vec3> normals; if (smoothed) { // initialize map of normals to zero // note that we store readily hashable vector<double> types instead of // vec3s and convert between the two as required // ...avoids some of the pain using <map> without much C++ knowledge std::map< std::vector<double>, std::vector<double> > normal_map; for (int i = 0; i < vertices.size(); i++) { std::vector<double> zeros; zeros.push_back(0.0); zeros.push_back(0.0); zeros.push_back(0.0); normal_map[to_vector(vertices[i])] = zeros; } for (int i = 0; i < vertices.size(); i += 3) { // get vertices of the current triangle glm::vec3 v1 = vertices[i]; glm::vec3 v2 = vertices[i + 1]; glm::vec3 v3 = vertices[i + 2]; std::vector<double> v1_key = to_vector(v1); std::vector<double> v2_key = to_vector(v2); std::vector<double> v3_key = to_vector(v3); // compute face normal glm::vec3 face_normal = glm::cross(v3 - v2, v1 - v2); // get the old vertex normal glm::vec3 v1_old = to_vec3(normal_map[v1_key]); glm::vec3 v2_old = to_vec3(normal_map[v2_key]); glm::vec3 v3_old = to_vec3(normal_map[v3_key]); // replace the old value with the new value normal_map.erase(v1_key); normal_map.erase(v2_key); normal_map.erase(v3_key); normal_map[v1_key] = to_vector(glm::normalize(v1_old + face_normal)); normal_map[v2_key] = to_vector(glm::normalize(v2_old + face_normal)); normal_map[v3_key] = to_vector(glm::normalize(v3_old + face_normal)); } // convert the map of normals to a vector of normals for (int i = 0; i < vertices.size(); i++) { normals.push_back(to_vec3(normal_map[to_vector(vertices[i])])); } } else { for (int i = 0; i < vertices.size(); i += 3) { // get vertices of this triangle glm::vec3 v1 = vertices[i]; glm::vec3 v2 = vertices[i + 1]; glm::vec3 v3 = vertices[i + 2]; // compute face normal glm::vec3 face_normal = glm::cross(v3 - v2, v1 - v2); normals.push_back(glm::normalize(face_normal)); normals.push_back(glm::normalize(face_normal)); normals.push_back(glm::normalize(face_normal)); } } glGenBuffers(1, &vertex_normal_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_normal_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * normals.size(), &normals[0], GL_STATIC_DRAW); }
/** Setup fixture */ corrected_intensity_write_read_fixture_v3() { typedef metric_type::uint_t uint_t; typedef metric_type::ushort_t ushort_t; uint_t calledCounts1[] = {52, 1049523, 654071, 500476, 982989}; ushort_t correctedIntCalled1[] = {245, 252, 61, 235}; expected_metrics.push_back(metric_type(7, 1114, 1, to_vector(correctedIntCalled1), to_vector(calledCounts1))); uint_t calledCounts2[] = {0, 1063708, 582243, 588028, 953132}; ushort_t correctedIntCalled2[] = {232, 257, 68, 228}; expected_metrics.push_back(metric_type(7, 1114, 2, to_vector(correctedIntCalled2), to_vector(calledCounts2))); uint_t calledCounts3[] = {0, 1022928, 617523, 594836, 951825}; ushort_t correctedIntCalled3[] = {227, 268, 68, 229}; expected_metrics.push_back(metric_type(7, 1114, 3, to_vector(correctedIntCalled3), to_vector(calledCounts3))); setup_write_read(); }
CUGIP_DECL_DEVICE TElement & get(coord_t aCoords) { TElement *buffer = reinterpret_cast<TElement *>(data); return buffer[get_linear_access_index(to_vector(TStaticSize()), aCoords)]; }
void rtlr::initialize(multilayer_perceptron* mlp) { assert(mlp); _mlp = mlp; _netValueDerivates = _mlp->_netValueDerivates.get_arrays() | to_vector(); _pValuesPool = _mlp->context()->device_array_management()->create_pool(false); _uLayersCount = _mlp->_layers.size() - 1; _maxULayerSize = _mlp->_layers | where([](row_numbered<layer_ptr>& l) { return l.row_num() != 0; }) | max_value([](row_numbered<layer_ptr>& l) { return l.value()->size(); }); for (idx_t lidx = 1; lidx < _mlp->_layers.size(); lidx++) { _inputLayerInfos.emplace_back(); _pValues.emplace_back(); auto& layer = _mlp->_layers[lidx].value(); _pValues.back().emplace_back(create_p_values_for_weights(_mlp->_biases.get(lidx))); for (auto& inputLayer : layer->input_layers()) { idx_t iidx = _mlp->get_layer_index(inputLayer); _inputLayerInfos.back().emplace_back(); auto& info = _inputLayerInfos.back().back(); info.index = iidx - 1; info.size = inputLayer->size(); info.weights = _mlp->_weights.get(make_pair(iidx, lidx)); info.is_element_of_u = inputLayer != _mlp->_layers[0].value(); _pValues.back().emplace_back(create_p_values_for_weights(info.weights)); } } _pValuesPool->allocate(); }
std::vector<std::string> peer_groups() const { zlist_t* peerGroups = zyre_peer_groups(m_self); std::vector<std::string> ret; if(peerGroups == NULL) { return ret; } ret = to_vector(peerGroups); zlist_destroy(&peerGroups); return ret; }
std::vector<std::string> own_groups() const { zlist_t* ownGroups = zyre_own_groups(m_self); std::vector<std::string> ret; if(ownGroups == NULL) { return ret; } ret = to_vector(ownGroups); zlist_destroy(&ownGroups); return ret; }
void test_movable() { MyMovableRange range{3}; auto transformed = sl::ranges::transform(std::move(range), [](my_movable el) { return el.get_val(); }); auto vec = transformed.to_vector(); slassert(3 == vec.size()); slassert(1 == vec[0]); slassert(2 == vec[1]); slassert(3 == vec[2]); }
void BackupSystem::generate_keypair(const std::wstring &recipient, const std::wstring &filename, const std::string &symmetric_key){ while (1){ CryptoPP::RSA::PrivateKey rsaPrivate; rsaPrivate.GenerateRandomWithKeySize(*random_number_generator, 4 << 10); if (!rsaPrivate.Validate(*random_number_generator, 3)) continue; CryptoPP::RSA::PublicKey rsaPublic(rsaPrivate); if (!rsaPublic.Validate(*random_number_generator, 3)) continue; auto pri = to_vector(rsaPrivate); auto pub = to_vector(rsaPublic); RsaKeyPair pair(pri, pub, symmetric_key); boost::filesystem::ofstream file(filename, std::ios::binary); SerializerStream ss(file); ss.begin_serialization(pair, false); break; } }
static Vec3d* this_vector(lua_State* l) { CelxLua celx(l); Vec3d* v3 = to_vector(l, 1); if (v3 == NULL) { celx.doError("Bad vector object!"); } return v3; }
unsigned int GameInstSet::hash() const { unsigned int hash = 0xbabdabe; std::vector<GameInst*> as_vector = to_vector(); for (int i = 0; i < as_vector.size(); i++) { GameInst* inst = as_vector[i]; if (!dynamic_cast<AnimatedInst*>(inst)) { hash ^= inst->integrity_hash(); hash ^= hash * 31337; //Ad hoc hashing yay } } return hash; }
/** Setup fixture */ corrected_intensity_hardcoded_fixture_v2() { typedef metric_type::uint_t uint_t; typedef metric_type::ushort_t ushort_t; ushort_t correctedIntAll1[] = {1213, 966, 960, 1095}; ushort_t correctedIntCalled1[] = {4070, 4074, 4029, 3972}; uint_t calledCounts1[] = {0, 698433, 548189, 548712, 646638}; expected_metrics.push_back( metric_type(1, 1104, 25, 1063, 11.9458876f, to_vector(correctedIntCalled1), to_vector(correctedIntAll1), to_vector(calledCounts1))); ushort_t correctedIntAll2[] = {1558, 1151, 1158, 1293}; uint_t calledCounts2[] = {10938, 733661, 537957, 543912, 615504}; ushort_t correctedIntCalled2[] = {5013, 4983, 4915, 4932}; expected_metrics.push_back( metric_type(1, 1104, 1, 1295, 13.3051805f, to_vector(correctedIntCalled2), to_vector(correctedIntAll2), to_vector(calledCounts2))); ushort_t correctedIntAll3[] = {1171, 932, 912, 1069}; uint_t calledCounts3[] = {0, 706987, 556441, 556067, 653959}; ushort_t correctedIntCalled3[] = {3931, 3931, 3923, 3878}; expected_metrics.push_back( metric_type(1, 1105, 25, 1025, 11.7396259f, to_vector(correctedIntCalled3), to_vector(correctedIntAll3), to_vector(calledCounts3))); int tmp[] = {2, 48, 1, 0, 80, 4, 25, 0, 39, 4, 189, 4, 198, 3, 192, 3, 71, 4, 230, 15, 234, 15, 189, 15, 132, 15, 0, 0, 0, 0, 65, 168, 10, 0, 93, 93, 8, 0, 104, 95, 8, 0, 238, 221, 9, 0, 91, 34, 63, 65, 1, 0, 80, 4, 1, 0, 15, 5, 22, 6, 127, 4, 134, 4, 13, 5, 149, 19, 119, 19, 51, 19, 68, 19, 186, 42, 0, 0, 221, 49, 11, 0, 101, 53, 8, 0, 168, 76, 8, 0, 80, 100, 9, 0, 5, 226, 84, 65, 1, 0, 81, 4, 25, 0, 1, 4, 147, 4, 164, 3, 144, 3, 45, 4, 91, 15, 91, 15, 83, 15, 38, 15, 0, 0, 0, 0, 171, 201, 10, 0, 153, 125, 8, 0, 35, 124, 8, 0, 135, 250, 9, 0, 130, 213, 59, 65 }; setup_hardcoded_binary(tmp, header_type()); }
SkVector SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t) { SkASSERT(src); SkASSERT(t >= 0 && t <= SK_Scalar1); Sk2s P0 = from_point(src[0]); Sk2s P1 = from_point(src[1]); Sk2s P2 = from_point(src[2]); Sk2s B = P1 - P0; Sk2s A = P2 - P1 - B; Sk2s T = A * Sk2s(t) + B; return to_vector(T + T); }
CUGIP_DECL_DEVICE void load(TView aView, coord_t aCorner/*region<cDimension> aRegion*/) { int index = threadOrderFromIndex(); TElement *buffer = reinterpret_cast<TElement *>(data); while (index < cElementCount) { auto coords = min_coords( aView.dimensions()-coord_t(1, FillFlag()), max_coords(coord_t(), aCorner + index_from_linear_access_index(to_vector(TStaticSize()), index))); buffer[index] = aView[coords]; index += TThreadBlockSize::count(); } }
void test_const_ref() { std::vector<my_movable> vec{}; vec.emplace_back(my_movable(41)); vec.emplace_back(my_movable(42)); vec.emplace_back(my_movable(43)); const std::vector<my_movable> vec2 = std::move(vec); const std::vector<my_movable>& vec2ref = vec2; int sum = 0; auto transformed1 = sl::ranges::transform(sl::ranges::refwrap(vec2ref), [&sum](const my_movable& el) { sum += el.get_val(); return std::ref(el); }); auto filtered = sl::ranges::filter(std::move(transformed1), [](const my_movable&) { return false; }, sl::ranges::ignore_offcast<const my_movable&>); auto res = filtered.to_vector(); }
SkVector SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t) { // The derivative equation is 2(b - a +(a - 2b +c)t). This returns a // zero tangent vector when t is 0 or 1, and the control point is equal // to the end point. In this case, use the quad end points to compute the tangent. if ((t == 0 && src[0] == src[1]) || (t == 1 && src[1] == src[2])) { return src[2] - src[0]; } SkASSERT(src); SkASSERT(t >= 0 && t <= SK_Scalar1); Sk2s P0 = from_point(src[0]); Sk2s P1 = from_point(src[1]); Sk2s P2 = from_point(src[2]); Sk2s B = P1 - P0; Sk2s A = P2 - P1 - B; Sk2s T = A * Sk2s(t) + B; return to_vector(T + T); }
void test_transform_refwrapped() { std::vector<my_movable> vec{}; vec.emplace_back(my_movable(41)); vec.emplace_back(my_movable(42)); vec.emplace_back(my_movable(43)); std::list<my_movable> li{}; li.emplace_back(91); li.emplace_back(92); auto transformed = sl::ranges::transform(sl::ranges::refwrap(vec), [](my_movable& el) { el.set_val(el.get_val() + 10); return std::ref(el); }); auto filtered = sl::ranges::filter(std::move(transformed), [](my_movable& el) { return 52 != el.get_val(); }, sl::ranges::ignore_offcast<my_movable&>); auto transformed2 = sl::ranges::transform(std::move(filtered), [](my_movable& el) { el.set_val(el.get_val() - 10); return std::ref(el); }); auto refwrapped2 = sl::ranges::refwrap(li); auto concatted = sl::ranges::concat(std::move(transformed2), std::move(refwrapped2)); //auto res will work too std::vector<std::reference_wrapper<my_movable>> res = concatted.to_vector(); slassert(4 == res.size()); slassert(41 == res[0].get().get_val()); slassert(43 == res[1].get().get_val()); slassert(91 == res[2].get().get_val()); slassert(92 == res[3].get().get_val()); slassert(3 == vec.size()); slassert(41 == vec[0].get_val()); slassert(52 == vec[1].get_val()); slassert(43 == vec[2].get_val()); slassert(2 == li.size()); slassert(91 == li.begin()->get_val()); }
void test_refwrap_to_value() { std::vector<my_movable> vec{}; vec.emplace_back(41); vec.emplace_back(42); vec.emplace_back(43); auto transformed = sl::ranges::transform(sl::ranges::refwrap(vec), [](my_movable& el) { return std::move(el); }); auto filtered = sl::ranges::filter(std::move(transformed), [](my_movable& el) { return 42 != el.get_val(); }, sl::ranges::ignore_offcast<my_movable>); auto res = filtered.to_vector(); slassert(2 == res.size()); slassert(41 == res[0].get_val()); slassert(43 == res[1].get_val()); slassert(3 == vec.size()); slassert(-1 == vec[0].get_val()); slassert(-1 == vec[1].get_val()); slassert(-1 == vec[2].get_val()); }
/** Setup fixture */ corrected_intensity_write_read_fixture_v2() { typedef metric_type::uint_t uint_t; typedef metric_type::ushort_t ushort_t; ushort_t correctedIntAll1[] = {1213, 966, 960, 1095}; ushort_t correctedIntCalled1[] = {4070, 4074, 4029, 3972}; uint_t calledCounts1[] = {0, 698433, 548189, 548712, 646638}; expected_metrics.push_back( metric_type(1, 1104, 25, 1063, 11.9458876f, to_vector(correctedIntCalled1), to_vector(correctedIntAll1), to_vector(calledCounts1))); ushort_t correctedIntAll2[] = {1558, 1151, 1158, 1293}; uint_t calledCounts2[] = {10938, 733661, 537957, 543912, 615504}; ushort_t correctedIntCalled2[] = {5013, 4983, 4915, 4932}; expected_metrics.push_back( metric_type(1, 1104, 1, 1295, 13.3051805f, to_vector(correctedIntCalled2), to_vector(correctedIntAll2), to_vector(calledCounts2))); ushort_t correctedIntAll3[] = {1171, 932, 912, 1069}; uint_t calledCounts3[] = {0, 706987, 556441, 556067, 653959}; ushort_t correctedIntCalled3[] = {3931, 3931, 3923, 3878}; expected_metrics.push_back( metric_type(1, 1105, 25, 1025, 11.7396259f, to_vector(correctedIntCalled3), to_vector(correctedIntAll3), to_vector(calledCounts3))); setup_write_read(); }
int main(int argc, char* argv[]) { size_t num_voices; char **fn_voices; char* in_fname; char* output_fname; FILE * outfp; char* dur_fname; FILE * durfp; bool print_label = false; bool print_utt = false; bool write_raw = false; bool write_durlabel = false; CFSAString LexFileName, LexDFileName; HTS_Engine engine; double speed = 1.1; size_t fr = 48000; size_t fp = 240; float alpha = 0.55; float beta = 0.0; float ht = 2.0; float th = 0.5; float gvw1 = 1.0; float gvw2 = 1.2; FSCInit(); fn_voices = (char **) malloc(argc * sizeof (char *)); if (argc < 11) { fprintf(stderr, "Viga: liiga vähe parameetreid\n\n"); PrintUsage(); } for (int i = 0; i < argc; i++) { if (CFSAString("-lex") == argv[i]) { if (i + 1 < argc) { LexFileName = argv[++i]; } else { return PrintUsage(); } } if (CFSAString("-lexd") == argv[i]) { if (i + 1 < argc) { LexDFileName = argv[++i]; } else { return PrintUsage(); } } if (CFSAString("-m") == argv[i]) { if (i + 1 < argc) { fn_voices[0] = argv[i + 1]; } else { fprintf(stderr, "Viga: puudub *.htsvoice fail\n"); PrintUsage(); exit(0); } } if (CFSAString("-o") == argv[i]) { if (i + 1 < argc) { output_fname = argv[i + 1]; cfileexists(output_fname); } else { fprintf(stderr, "Viga: puudb väljundfaili nimi\n"); PrintUsage(); exit(0); } } if (CFSAString("-f") == argv[i]) { if (i + 1 < argc) { in_fname = argv[i + 1]; } else { fprintf(stderr, "Viga: puudb sisendfaili nimi\n"); PrintUsage(); exit(0); } } if (CFSAString("-s") == argv[i]) { if (i + 1 < argc) { samplerate(fr, fp, alpha, atoi(argv[i + 1])); } } if (CFSAString("-r") == argv[i]) { if (i + 1 < argc) { speed = atof(argv[i + 1]); } } if (CFSAString("-ht") == argv[i]) { if (i + 1 < argc) { ht = atof(argv[i + 1]); } } if (CFSAString("-gvw1") == argv[i]) { if (i + 1 < argc) { gvw1 = atof(argv[i + 1]); } } if (CFSAString("-gvw2") == argv[i]) { if (i + 1 < argc) { gvw2 = atof(argv[i + 1]); } } if (CFSAString("-debug") == argv[i]) { print_label = true; } if (CFSAString("-utt") == argv[i]) { print_utt = true; } if (CFSAString("-raw") == argv[i]) { write_raw = true; } if (CFSAString("-dur") == argv[i]) { if (i + 1 < argc) { dur_fname = argv[i + 1]; cfileexists(dur_fname); write_durlabel = true; } else { fprintf(stderr, "Viga: puudb kestustefaili nimi\n"); PrintUsage(); exit(0); } } } Linguistic.Open(LexFileName); Disambiguator.Open(LexDFileName); CFSWString text; ReadUTF8Text(text, in_fname); HTS_Engine_initialize(&engine); if (HTS_Engine_load(&engine, fn_voices, 1) != TRUE) { fprintf(stderr, "Viga: puudub *.htsvoice. %p\n", fn_voices[0]); free(fn_voices); HTS_Engine_clear(&engine); exit(1); } free(fn_voices); HTS_Engine_set_sampling_frequency(&engine, (size_t) fr); HTS_Engine_set_phoneme_alignment_flag(&engine, FALSE); HTS_Engine_set_fperiod(&engine, (size_t) fp); HTS_Engine_set_alpha(&engine, alpha); HTS_Engine_set_beta(&engine, beta); HTS_Engine_set_speed(&engine, speed); HTS_Engine_add_half_tone(&engine, ht); HTS_Engine_set_msd_threshold(&engine, 1, th); /* HTS_Engine_set_duration_interpolation_weight(&engine, 1, diw); HTS_Engine_set_parameter_interpolation_weight(&engine, 0, 0, piw1); HTS_Engine_set_parameter_interpolation_weight(&engine, 0, 1, piw2); HTS_Engine_set_gv_interpolation_weight(&engine, 0, 0, giw1); HTS_Engine_set_gv_interpolation_weight(&engine, 0, 1, giw2); */ HTS_Engine_set_gv_weight(&engine, 0, gvw1); HTS_Engine_set_gv_weight(&engine, 1, gvw2); text = DealWithText(text); CFSArray<CFSWString> res = do_utterances(text); INTPTR data_size = 0; outfp = fopen(output_fname, "wb"); if (write_durlabel) durfp = fopen(dur_fname, "w"); if (!write_raw) HTS_Engine_write_header(&engine, outfp, 1); for (INTPTR i = 0; i < res.GetSize(); i++) { CFSArray<CFSWString> label = do_all(res[i], print_label, print_utt); std::vector<std::string> v; v = to_vector(label); std::vector<char*> vc; fill_char_vector(v, vc); size_t n_lines = vc.size(); if (HTS_Engine_synthesize_from_strings(&engine, &vc[0], n_lines) != TRUE) { fprintf(stderr, "Viga: süntees ebaonnestus.\n"); HTS_Engine_clear(&engine); exit(1); } clean_char_vector(vc); data_size += HTS_Engine_engine_speech_size(&engine); if (write_durlabel) HTS_Engine_save_durlabel(&engine, durfp); HTS_Engine_save_generated_speech(&engine, outfp); HTS_Engine_refresh(&engine); } //synth loop if (!write_raw) HTS_Engine_write_header(&engine, outfp, data_size); if (write_durlabel) fclose(durfp); fclose(outfp); HTS_Engine_clear(&engine); Linguistic.Close(); FSCTerminate(); return 0; }
#include <examples.h> const std::vector<int> vec1to4 = iota(1) | limit(4) | to_vector(); const std::vector<int> vec5to8 = iota(5) | limit(4) | to_vector(); const std::vector<int> vec1to8 = iota(1) | limit(8) | to_vector(); int main(int argc, char** argv) { std::cout << "flow examples" << std::endl << std::endl; std::cout << "vec1to4 = "; vec1to4 | dump(); std::cout << std::endl << "vec5to8 = "; vec5to8 | dump(); std::cout << std::endl << std::endl; concat_example(); drop_while_example(); zip_example(); terminal_example(); system("pause"); return 0; }
/* * Counts occurrences of three-node functional motifs in a weighted graph. * Returns intensity and (optionally) coherence and motif counts. */ MATRIX_T* BCT_NAMESPACE::motif3funct_wei(const MATRIX_T* W, MATRIX_T** Q, MATRIX_T** F) { if (safe_mode) check_status(W, SQUARE | WEIGHTED, "motif3funct_wei"); // load motif34lib M3 M3n ID3 N3 VECTOR_T* ID3; VECTOR_T* N3; MATRIX_T* M3 = motif3generate(&ID3, &N3); // n=length(W); int n = length(W); // I=zeros(13,n); MATRIX_T* I = zeros(13, n); // Q=zeros(13,n); if (Q != NULL) { *Q = zeros(13, n); } // F=zeros(13,n); if (F != NULL) { *F = zeros(13, n); } // A=1*(W~=0); MATRIX_T* A = compare_elements(W, fp_not_equal, 0.0); // As=A|A.'; MATRIX_T* A_transpose = MATRIX_ID(alloc)(A->size2, A->size1); MATRIX_ID(transpose_memcpy)(A_transpose, A); MATRIX_T* As = logical_or(A, A_transpose); MATRIX_ID(free)(A_transpose); // for u=1:n-2 for (int u = 0; u < n - 2; u++) { // V1=[false(1,u) As(u,u+1:n)]; VECTOR_T* V1 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V1, As, u); for (int i = 0; i <= u; i++) { VECTOR_ID(set)(V1, i, 0.0); } // for v1=find(V1) VECTOR_T* find_V1 = find(V1); if (find_V1 != NULL) { for (int i_find_V1 = 0; i_find_V1 < (int)find_V1->size; i_find_V1++) { int v1 = (int)VECTOR_ID(get)(find_V1, i_find_V1); // V2=[false(1,u) As(v1,u+1:n)]; VECTOR_T* V2 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V2, As, v1); for (int i = 0; i <= u; i++) { VECTOR_ID(set)(V2, i, 0.0); } // V2(V1)=0; logical_index_assign(V2, V1, 0.0); // V2=([false(1,v1) As(u,v1+1:n)])|V2; VECTOR_T* V2_1 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V2_1, As, u); for (int i = 0; i <= v1; i++) { VECTOR_ID(set)(V2_1, i, 0.0); } VECTOR_T* V2_2 = V2; V2 = logical_or(V2_1, V2_2); VECTOR_ID(free)(V2_1); VECTOR_ID(free)(V2_2); // for v2=find(V2) VECTOR_T* find_V2 = find(V2); if (find_V2 != NULL) { for (int i_find_V2 = 0; i_find_V2 < (int)find_V2->size; i_find_V2++) { int v2 = (int)VECTOR_ID(get)(find_V2, i_find_V2); // w=[W(v1,u) W(v2,u) W(u,v1) W(v2,v1) W(u,v2) W(v1,v2)]; int WA_rows[] = { v1, v2, u, v2, u, v1 }; int WA_cols[] = { u, u, v1, v1, v2, v2 }; VECTOR_T* w = VECTOR_ID(alloc)(6); for (int i = 0; i < 6; i++) { VECTOR_ID(set)(w, i, MATRIX_ID(get)(W, WA_rows[i], WA_cols[i])); } // a=[A(v1,u);A(v2,u);A(u,v1);A(v2,v1);A(u,v2);A(v1,v2)]; MATRIX_T* a = MATRIX_ID(alloc)(6, 1); for (int i = 0; i < 6; i++) { MATRIX_ID(set)(a, i, 0, MATRIX_ID(get)(A, WA_rows[i], WA_cols[i])); } // ind=(M3*a)==N3; MATRIX_T* M3_mul_a_m = mul(M3, a); MATRIX_ID(free)(a); VECTOR_T* M3_mul_a = to_vector(M3_mul_a_m); MATRIX_ID(free)(M3_mul_a_m); VECTOR_T* ind = compare_elements(M3_mul_a, fp_equal, N3); VECTOR_ID(free)(M3_mul_a); // m=sum(ind); int m = (int)sum(ind); if (m > 0) { // M=M3(ind,:).*repmat(w,m,1); VECTOR_T* M3_cols = sequence(0, M3->size2 - 1); MATRIX_T* M = log_ord_index(M3, ind, M3_cols); VECTOR_ID(free)(M3_cols); for (int i = 0; i < (int)M->size1; i++) { VECTOR_ID(view) M_row_i = MATRIX_ID(row)(M, i); VECTOR_ID(mul)(&M_row_i.vector, w); } // id=ID3(ind); VECTOR_T* id = logical_index(ID3, ind); VECTOR_ID(add_constant)(id, -1.0); // l=N3(ind); VECTOR_T* l = logical_index(N3, ind); // x=sum(M,2)./l; VECTOR_T* x = sum(M, 2); VECTOR_ID(div)(x, l); // M(M==0)=1; MATRIX_T* M_eq_0 = compare_elements(M, fp_equal, 0.0); logical_index_assign(M, M_eq_0, 1.0); MATRIX_ID(free)(M_eq_0); // i=prod(M,2).^(1./l); VECTOR_T* prod_M = prod(M, 2); MATRIX_ID(free)(M); VECTOR_T* l_pow_neg_1 = pow_elements(l, -1.0); VECTOR_ID(free)(l); VECTOR_T* i = pow_elements(prod_M, l_pow_neg_1); VECTOR_ID(free)(prod_M); VECTOR_ID(free)(l_pow_neg_1); // q = i./x; VECTOR_T* q = copy(i); VECTOR_ID(div)(q, x); VECTOR_ID(free)(x); // [idu j]=unique(id); VECTOR_T* j; VECTOR_T* idu = unique(id, "last", &j); VECTOR_ID(free)(id); // j=[0;j]; VECTOR_T* temp = VECTOR_ID(alloc)(j->size + 1); VECTOR_ID(set)(temp, 0, -1.0); VECTOR_ID(view) temp_subv = VECTOR_ID(subvector)(temp, 1, j->size); VECTOR_ID(memcpy)(&temp_subv.vector, j); VECTOR_ID(free)(j); j = temp; // mu=length(idu); int mu = length(idu); // i2=zeros(mu,1); VECTOR_T* i2 = zeros_vector(mu); // q2=i2; f2=i2; VECTOR_T* q2 = copy(i2); VECTOR_T* f2 = copy(i2); // for h=1:mu for (int h = 0; h < mu; h++) { // i2(h)=sum(i(j(h)+1:j(h+1))); int j_h = (int)VECTOR_ID(get)(j, h); int j_h_add_1 = (int)VECTOR_ID(get)(j, h + 1); VECTOR_T* iq_indices = sequence(j_h + 1, j_h_add_1); VECTOR_T* i_idx = ordinal_index(i, iq_indices); VECTOR_ID(set)(i2, h, sum(i_idx)); VECTOR_ID(free)(i_idx); // q2(h)=sum(q(j(h)+1:j(h+1))); VECTOR_T* q_idx = ordinal_index(q, iq_indices); VECTOR_ID(free)(iq_indices); VECTOR_ID(set)(q2, h, sum(q_idx)); VECTOR_ID(free)(q_idx); // f2(h)=j(h+1)-j(h); VECTOR_ID(set)(f2, h, (FP_T)(j_h_add_1 - j_h)); } VECTOR_ID(free)(i); VECTOR_ID(free)(q); VECTOR_ID(free)(j); // I(idu,[u v1 v2])=I(idu,[u v1 v2])+[i2 i2 i2]; // Q(idu,[u v1 v2])=Q(idu,[u v1 v2])+[q2 q2 q2]; // F(idu,[u v1 v2])=F(idu,[u v1 v2])+[f2 f2 f2]; FP_T IQF_cols[] = { (FP_T)u, (FP_T)v1, (FP_T)v2 }; VECTOR_ID(view) IQF_cols_vv = VECTOR_ID(view_array)(IQF_cols, 3); MATRIX_T* I_idx = ordinal_index(I, idu, &IQF_cols_vv.vector); MATRIX_T* Q_idx = NULL; MATRIX_T* F_idx = NULL; if (Q != NULL) { Q_idx = ordinal_index(*Q, idu, &IQF_cols_vv.vector); } if (F != NULL) { F_idx = ordinal_index(*F, idu, &IQF_cols_vv.vector); } for (int j = 0; j < 3; j++) { VECTOR_ID(view) I_idx_col_j = MATRIX_ID(column)(I_idx, j); VECTOR_ID(add)(&I_idx_col_j.vector, i2); if (Q != NULL) { VECTOR_ID(view) Q_idx_col_j = MATRIX_ID(column)(Q_idx, j); VECTOR_ID(add)(&Q_idx_col_j.vector, q2); } if (F != NULL) { VECTOR_ID(view) F_idx_col_j = MATRIX_ID(column)(F_idx, j); VECTOR_ID(add)(&F_idx_col_j.vector, f2); } } VECTOR_ID(free)(i2); VECTOR_ID(free)(q2); VECTOR_ID(free)(f2); ordinal_index_assign(I, idu, &IQF_cols_vv.vector, I_idx); MATRIX_ID(free)(I_idx); if (Q != NULL) { ordinal_index_assign(*Q, idu, &IQF_cols_vv.vector, Q_idx); MATRIX_ID(free)(Q_idx); } if (F != NULL) { ordinal_index_assign(*F, idu, &IQF_cols_vv.vector, F_idx); MATRIX_ID(free)(F_idx); } VECTOR_ID(free)(idu); } VECTOR_ID(free)(w); VECTOR_ID(free)(ind); } VECTOR_ID(free)(find_V2); } VECTOR_ID(free)(V2); } VECTOR_ID(free)(find_V1); } VECTOR_ID(free)(V1); } VECTOR_ID(free)(ID3); VECTOR_ID(free)(N3); MATRIX_ID(free)(M3); MATRIX_ID(free)(A); MATRIX_ID(free)(As); return I; }
/* * Counts occurrences of four-node functional motifs in a binary graph. */ VECTOR_T* BCT_NAMESPACE::motif4funct_bin(const MATRIX_T* W, MATRIX_T** F) { if (safe_mode) check_status(W, SQUARE | BINARY, "motif4funct_bin"); // load motif34lib M4 ID4 N4 VECTOR_T* ID4; VECTOR_T* N4; MATRIX_T* M4 = motif4generate(&ID4, &N4); // n=length(W); int n = length(W); // f=zeros(199,1); VECTOR_T* f = zeros_vector(199); // F=zeros(199,n); if (F != NULL) { *F = zeros(199, n); } // A=1*(W~=0); MATRIX_T* A = compare_elements(W, fp_not_equal, 0.0); // As=A|A.'; MATRIX_T* A_transpose = MATRIX_ID(alloc)(A->size2, A->size1); MATRIX_ID(transpose_memcpy)(A_transpose, A); MATRIX_T* As = logical_or(A, A_transpose); MATRIX_ID(free)(A_transpose); // for u=1:n-3 for (int u = 0; u < n - 3; u++) { // V1=[false(1,u) As(u,u+1:n)]; VECTOR_T* V1 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V1, As, u); for (int i = 0; i <= u; i++) { VECTOR_ID(set)(V1, i, 0.0); } // for v1=find(V1) VECTOR_T* find_V1 = find(V1); if (find_V1 != NULL) { for (int i_find_V1 = 0; i_find_V1 < (int)find_V1->size; i_find_V1++) { int v1 = (int)VECTOR_ID(get)(find_V1, i_find_V1); // V2=[false(1,u) As(v1,u+1:n)]; VECTOR_T* V2 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V2, As, v1); for (int i = 0; i <= u; i++) { VECTOR_ID(set)(V2, i, 0.0); } // V2(V1)=0; logical_index_assign(V2, V1, 0.0); // V2=V2|([false(1,v1) As(u,v1+1:n)]); VECTOR_T* V2_1 = V2; VECTOR_T* V2_2 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V2_2, As, u); for (int i = 0; i <= v1; i++) { VECTOR_ID(set)(V2_2, i, 0.0); } V2 = logical_or(V2_1, V2_2); VECTOR_ID(free)(V2_1); VECTOR_ID(free)(V2_2); // for v2=find(V2) VECTOR_T* find_V2 = find(V2); if (find_V2 != NULL) { for (int i_find_V2 = 0; i_find_V2 < (int)find_V2->size; i_find_V2++) { int v2 = (int)VECTOR_ID(get)(find_V2, i_find_V2); // vz=max(v1,v2); int vz = (v1 > v2) ? v1 : v2; // V3=([false(1,u) As(v2,u+1:n)]); VECTOR_T* V3 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V3, As, v2); for (int i = 0; i <= u; i++) { VECTOR_ID(set)(V3, i, 0.0); } // V3(V2)=0; logical_index_assign(V3, V2, 0.0); // V3=V3|([false(1,v2) As(v1,v2+1:n)]); VECTOR_T* V3_1 = V3; VECTOR_T* V3_2 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V3_2, As, v1); for (int i = 0; i <= v2; i++) { VECTOR_ID(set)(V3_2, i, 0.0); } V3 = logical_or(V3_1, V3_2); VECTOR_ID(free)(V3_1); VECTOR_ID(free)(V3_2); // V3(V1)=0; logical_index_assign(V3, V1, 0.0); // V3=V3|([false(1,vz) As(u,vz+1:n)]); V3_1 = V3; V3_2 = VECTOR_ID(alloc)(n); MATRIX_ID(get_row)(V3_2, As, u); for (int i = 0; i <= vz; i++) { VECTOR_ID(set)(V3_2, i, 0.0); } V3 = logical_or(V3_1, V3_2); VECTOR_ID(free)(V3_1); VECTOR_ID(free)(V3_2); // for v3=find(V3) VECTOR_T* find_V3 = find(V3); if (find_V3 != NULL ) { for (int i_find_V3 = 0; i_find_V3 < (int)find_V3->size; i_find_V3++) { int v3 = (int)VECTOR_ID(get)(find_V3, i_find_V3); // a=[A(v1,u);A(v2,u);A(v3,u);A(u,v1);A(v2,v1);A(v3,v1);A(u,v2);A(v1,v2);A(v3,v2);A(u,v3);A(v1,v3);A(v2,v3)]; int A_rows[] = { v1, v2, v3, u, v2, v3, u, v1, v3, u, v1, v2 }; int A_cols[] = { u, u, u, v1, v1, v1, v2, v2, v2, v3, v3, v3 }; MATRIX_T* a = MATRIX_ID(alloc)(12, 1); for (int i = 0; i < 12; i++) { MATRIX_ID(set)(a, i, 0, MATRIX_ID(get)(A, A_rows[i], A_cols[i])); } // ind=(M4*a)==N4; MATRIX_T* M4_mul_a_m = mul(M4, a); MATRIX_ID(free)(a); VECTOR_T* M4_mul_a = to_vector(M4_mul_a_m); MATRIX_ID(free)(M4_mul_a_m); VECTOR_T* ind = compare_elements(M4_mul_a, fp_equal, N4); VECTOR_ID(free)(M4_mul_a); // id=ID4(ind); VECTOR_T* id = logical_index(ID4, ind); VECTOR_ID(free)(ind); if (id != NULL) { VECTOR_ID(add_constant)(id, -1.0); // [idu j]=unique(id); VECTOR_T* j; VECTOR_T* idu = unique(id, "last", &j); VECTOR_ID(free)(id); // j=[0;j]; VECTOR_T* temp = VECTOR_ID(alloc)(j->size + 1); VECTOR_ID(set)(temp, 0, -1.0); VECTOR_ID(view) temp_subv = VECTOR_ID(subvector)(temp, 1, j->size); VECTOR_ID(memcpy)(&temp_subv.vector, j); VECTOR_ID(free)(j); j = temp; // mu=length(idu); int mu = length(idu); // f2=zeros(mu,1); VECTOR_T* f2 = zeros_vector(mu); // for h=1:mu for (int h = 0; h < mu; h++) { // f2(h)=j(h+1)-j(h); FP_T j_h_add_1 = VECTOR_ID(get)(j, h + 1); FP_T j_h = VECTOR_ID(get)(j, h); VECTOR_ID(set)(f2, h, j_h_add_1 - j_h); } VECTOR_ID(free)(j); // f(idu)=f(idu)+f2; VECTOR_T* f_idu_add_f2 = ordinal_index(f, idu); VECTOR_ID(add)(f_idu_add_f2, f2); ordinal_index_assign(f, idu, f_idu_add_f2); VECTOR_ID(free)(f_idu_add_f2); // if nargout==2; F(idu,[u v1 v2 v3])=F(idu,[u v1 v2 v3])+[f2 f2 f2 f2]; end if (F != NULL) { FP_T F_cols[] = { (FP_T)u, (FP_T)v1, (FP_T)v2, (FP_T)v3 }; VECTOR_ID(view) F_cols_vv = VECTOR_ID(view_array)(F_cols, 4); MATRIX_T* F_idx = ordinal_index(*F, idu, &F_cols_vv.vector); for (int i = 0; i < 4; i++) { VECTOR_ID(view) F_idx_col_i = MATRIX_ID(column)(F_idx, i); VECTOR_ID(add)(&F_idx_col_i.vector, f2); } ordinal_index_assign(*F, idu, &F_cols_vv.vector, F_idx); MATRIX_ID(free)(F_idx); } VECTOR_ID(free)(idu); VECTOR_ID(free)(f2); } } VECTOR_ID(free)(find_V3); } VECTOR_ID(free)(V3); } VECTOR_ID(free)(find_V2); } VECTOR_ID(free)(V2); } VECTOR_ID(free)(find_V1); } VECTOR_ID(free)(V1); } VECTOR_ID(free)(ID4); VECTOR_ID(free)(N4); MATRIX_ID(free)(M4); MATRIX_ID(free)(A); MATRIX_ID(free)(As); return f; }
/** * Same as head, return vector<flexible_type>, used for testing. */ std::vector<flexible_type> _head(size_t nrows) { auto result = head(nrows); auto ret = result->to_vector(); return ret; }
VImage operator<( VImage a, double b ) { return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESS, to_vector( b ) ) ); }
VImage operator<( double a, VImage b ) { return( b.relational_const( VIPS_OPERATION_RELATIONAL_MORE, to_vector( a ) ) ); }
VImage operator%( VImage a, double b ) { return( a.remainder_const( to_vector( b ) ) ); }
VImage operator<<( VImage a, double b ) { return( a.boolean_const( VIPS_OPERATION_BOOLEAN_LSHIFT, to_vector( b ) ) ); }
std::vector<flexible_type> _tail(size_t nrows=10) { auto result = tail(nrows); auto ret = result->to_vector(); return ret; }
VImage operator^( double a, VImage b ) { return( b.boolean_const( VIPS_OPERATION_BOOLEAN_EOR, to_vector( a ) ) ); }