{"id":18588,"date":"2022-11-01T08:37:34","date_gmt":"2022-11-01T03:07:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/"},"modified":"2022-11-01T08:37:34","modified_gmt":"2022-11-01T03:07:34","slug":"solved-what-datatype-would-is-expected-for-the-arrays-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/","title":{"rendered":"[Solved] What datatype would is expected for the arrays in c++?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-69865077\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"69865077\" data-parentid=\"69864408\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I&#8217;m guessing here based on the many questions leading up to this.<\/p>\n<p>It is pretty obvious that you do not want sample type to be a scalar, but a 2-dimensional array.<\/p>\n<p>I will sketch a generic short-cut that would allow you to write <code>mean_square_displacement::operator()<\/code> to accept those, potentially even without knowing the concrete type(s) of the arguments.<\/p>\n<h2>Caution<\/h2>\n<p>However, I want to first caution that there is no way to know whether that will work with the <code>multi_tau_correlator<\/code> framework that you&#8217;ve never described. It seems like you are trying to apply the correlator in a parallel fashion.<\/p>\n<p>Firstly it&#8217;s unknown whether the <code>correlator<\/code> framework allows you to do that (technically) and secondly it is likely not going to be fast, as this kind of parallelism is the realm of highly optimized libraries (that use actually vectorized operations like SIMD, AVX, OpenGL\/Cuda).<\/p>\n<p>All I see here is a raw quadratic loop, which does not bode well for efficiency.<\/p>\n<h2>The blind fix<\/h2>\n<p>Since you don&#8217;t want <code>sample_type<\/code> to be what you define it to be, simply define it as something else, preferrably, the thing you need it to be!<\/p>\n<pre><code>typedef array_2d_t sample_type;\n<\/code><\/pre>\n<p>There&#8217;s a good chance that this is not what you actually are passing either (I suspect a sub_array view or (optionally strided) multi_array_view slice of a 3D multi array). But you can let the compiler figure it out based on the fact that you know you can index the arguments like this <code>arg[int][int]<\/code>:<\/p>\n<pre><code>template &lt;typename Sample&gt;\nresult_type operator()(Sample const&amp; msd_x, Sample const&amp; msd_y,\n                       Sample const&amp; msd_z) const\n<\/code><\/pre>\n<p>This lets the compiler deduce the concrete type, assuming all arguments have the same <em>statical<\/em> type. Otherwise, you can go even more open:<\/p>\n<pre><code>template &lt;typename Sx, typename Sy, typename Sz&gt;\nresult_type operator()(Sx const&amp; msd_x, Sy const&amp; msd_y,\n                       Sz const&amp; msd_z) const\n<\/code><\/pre>\n<h2>More Caution<\/h2>\n<p>I can see you&#8217;re out of your depth. E.g.<\/p>\n<ul>\n<li>in your <code>mean_square_displacement<\/code> we see <code>auto dr<\/code> being used outside its scope. It will not compile. For the above I have GUESSED that you meant to place the accumulation into the inner loop.<\/li>\n<li>you&#8217;re also <strong>explicitly<\/strong> addressing <code>msd_x<\/code> out of bounds (index N doesn&#8217;t exist, since <code>N == msd_x.size()<\/code>).<\/li>\n<li>Likely <code>msd_y<\/code>\/<code>msd_z<\/code> have the same extents, so they too have this issue, but even with +2<\/li>\n<li>If course the naive fix to loop till <code>N-2<\/code> risks <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Undefined_behavior\">UB<\/a> again unless you make sure that <code>N<\/code> is never &lt;2<\/li>\n<\/ul>\n<p>Here&#8217;s the minimum fixes that make this code look like it might technically work, though:<\/p>\n<pre><code>struct mean_square_displacement {\n    \/\/typedef fixed_vector&lt;double, 3&gt; result_type;\n    typedef double result_type;\n\n    template &lt;typename Sample&gt;\n    result_type operator()(Sample const&amp; msd_x, Sample const&amp; msd_y,\n                           Sample const&amp; msd_z) const\n    {\n        size_t N = msd_x.size();\n        assert(N&gt;=2);\n        result_type msd = 0;\n        for (unsigned int i = 0; i &lt; N-1; ++i) {\n            for (unsigned int j = 0; j &lt; N-2; ++j) {\n                auto dr = \/\/\n                    (msd_x[i + 1][j + 0] - msd_x[i][j + 0]) +\n                    (msd_y[i + 1][j + 1] - msd_y[i][j + 1]) +\n                    (msd_z[i + 1][j + 2] - msd_z[i][j + 2]);\n                \/\/ accumulate squared displacements\n                msd += dr * dr;\n            }\n        }\n        return msd \/ N;\n\n    }\n};\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">7<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What datatype would is expected for the arrays in c++? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;m guessing here based on the many questions leading up to this. It is pretty obvious that you do not want sample type to be a scalar, but a 2-dimensional array. I will sketch a generic short-cut that would allow you to write mean_square_displacement::operator() to accept those, potentially even without knowing the concrete type(s) &#8230; <a title=\"[Solved] What datatype would is expected for the arrays in c++?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\" aria-label=\"More on [Solved] What datatype would is expected for the arrays in c++?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2230,324],"class_list":["post-18588","post","type-post","status-publish","format-standard","hentry","category-solved","tag-boost","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What datatype would is expected for the arrays in c++? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What datatype would is expected for the arrays in c++? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;m guessing here based on the many questions leading up to this. It is pretty obvious that you do not want sample type to be a scalar, but a 2-dimensional array. I will sketch a generic short-cut that would allow you to write mean_square_displacement::operator() to accept those, potentially even without knowing the concrete type(s) ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T03:07:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What datatype would is expected for the arrays in c++?\",\"datePublished\":\"2022-11-01T03:07:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\"},\"wordCount\":398,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"boost\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\",\"name\":\"[Solved] What datatype would is expected for the arrays in c++? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T03:07:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What datatype would is expected for the arrays in c++?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] What datatype would is expected for the arrays in c++? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What datatype would is expected for the arrays in c++? - JassWeb","og_description":"[ad_1] I&#8217;m guessing here based on the many questions leading up to this. It is pretty obvious that you do not want sample type to be a scalar, but a 2-dimensional array. I will sketch a generic short-cut that would allow you to write mean_square_displacement::operator() to accept those, potentially even without knowing the concrete type(s) ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T03:07:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What datatype would is expected for the arrays in c++?","datePublished":"2022-11-01T03:07:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/"},"wordCount":398,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["boost","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/","name":"[Solved] What datatype would is expected for the arrays in c++? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T03:07:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-datatype-would-is-expected-for-the-arrays-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What datatype would is expected for the arrays in c++?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18588"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18588\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}