Planet Octavehttp://planet.octave.org/atom.xml2019-01-23T17:45:10+00:00Planet/2.0 +http://www.planetplanet.orgGSoC: final posttag:blogger.com,1999:blog-7386105313127736439.post-486730930999067572018-09-07T16:10:23+00:00<div dir="ltr">Welcome to the final post regarding my Google Summer of Code 2018 project. In this post, I'd like to talk about the overall work product and how it corresponds (or varies) from the original plan. Then, I would like to acknowledge some suggestions of my mentors and talk about some new ideas that were recently discussed with them.<br /><br />However, before talking about any of those things, I'd like to share the code that was written down in the last twelve weeks. So <a href="https://bitbucket.org/peesu_97/octave/commits/all" target="_blank">here</a> is the link to my public repository where all the code can be found and <a href="https://drive.google.com/open?id=1uPkZzIZXQ-UjbT8hjxtYTuZXsegkRP4J" target="_blank">here</a> is a patch that can be merged with the main line of development.<br /><br />Now, coming to final work product, functionality wise, the feature turned out to be exactly what it was supposed to be, a fast and accurate way to suggest corrections for typographic errors, made while working on the command window of Octave. The difference, however, was in the way of implementation.<br /><br />My original idea was to make a Neural Network for this problem and I did go to some lengths to make that happen. Precisely, I did collect some data about the most common typographic errors made by Octave users and did code up a small model that could learn the correct spellings of a few commands of Octave. At the time, the motivation behind the Neural Network model was to have an algorithm that could work better than the existing algorithms that are used to compare two strings, in terms of the speed-accuracy trade-off. <br /><br />However, during the community bonding period, some loopholes in my Neural Network implementation were pointed out by a few members of the Octave community. As a student who wants to pursue a career in data science, those counter points, and further research that was done on the Neural Network approach during the third phase of coding, turned out to be invaluable, for it taught me that 'Neural Networks + Data' is not a magical combination that solves every problem of this world. Maybe they can, but sometimes, simpler, more optimal solutions exist, and in those times, one must look at those solutions and optimize them further according to the problem at hand. Somewhere down the line, it also gave me a better understanding of the nature of Neural Networks.<br /><br />Now, coming back to the technical details of this project, to summarize it all, I used the faster variation of the edit distance algorithm, the one that uses dynamic programming, and optimized it further by reducing the sampling space on which the algorithm had to work on. To reduce the sample space, I analyzed the data that I had originally collected to make a Neural Network and based on the results of the analysis, I was able to make certain assumptions about the misspellings. These assumptions coupled with some clever data organization techniques helped me code up a fast, and yet very accurate version of the edit distance algorithm. One can read about this implementation in great detail in the previous blog posts.<br /><br />The plan was to replace this algorithm with Neural Networks, during the third phase, 'if' they happen to perform better. As of now however, I found no way to make a Neural Networks perform better than what had been already made and so the suggestion engine still uses my original algorithm. <br /><br />Additionally, I had to write the documentation and the tests for all of my code during the third phase of coding and I am glad to say that this work has been successfully completed. The main documentation for the m-scripts can be seen in the help text of those scripts. Besides that, I've also written down the documentation for the database file in a markdown file that is included with the database.<br /><br />I must acknowledge the fact that Nick had guided me very well on how the documentation should be done, during the second phase evaluations. I did keep his guidance in mind while writing the documentation and the tests, and have, hopefully, made a well documented, well tested product.<br /><br />Now, although, the main documentation should be enough for anyone who wishes to understand how the feature works, if any additional help is required by anyone, the previous posts of this blog (which contain a very detailed explanation), and the public mailing list of Octave (which I shall continue to follow), should be a good place to visit.<br /><br />During the community bonding period, Rik and I had discussed the importance of an on/off switch for this feature. This switch was already created by the time the first evaluations took place, but during the third phase, I took some time to wrap up this toggling command into a nice m-script. The users can now do a simple >>command_correction ("off") to switch off the feature and do a simple >>command_correction ("on") to turn it back on.<br /><br />Next, I'd like to talk about something that Doug recently mentioned to me. He asked me if I could think of some way in which we can track the identifiers that don't get resolved by my software. Essentially, this problem is directly related to the maintenance of the database file. With Octave under constant development, new identifiers will be created and some identifiers will deprecate as well. To make sure that the correction suggestion feature does not loose its value, the database of the identifiers would have to be updated in some regular intervals of time. Maybe an update every 6 months would be enough.<br /><br />Currently, I've included a markdown file with the database that explains how this update can be done, and for now, this update could be done manually only. For now, I cannot not think of a way in which the database file gets automatically updated. Later on, maybe I or someone else could come up with a way to make a program read the release notices of Octave and its various packages and then modify the database accordingly. Maybe this could be a GSoC project for a future batch of students? <br /><br />So in conclusion, the planned part of the project is absolutely complete and we have already started thinking of ways in which this feature can be improved. For further testing of the current implementation of the feature, I'd need the support of the members of the community. I would really appreciate it if anyone could try this feature for themselves and see if they could break it, or find any other kind of bugs, or maybe suggest some changes to the suggestion engine that could speed up the feature, or, maybe do something as small as pointing out some pieces of code where the coding style has not been followed properly.<br /><br />Finally, I'd like to thank the Octave community. Working with them was an invaluable learning experience and I hope to be able to continue to associate myself with them for the years to come. :)</div>Sudeepam Pandeynoreply@blogger.comhttps://sudeepam.blogspot.com/Final Posttag:eriveltongualter.github.io,2018-08-13:/GSoC2018/final.html2018-08-13T05:00:00+00:00<p>The Google Summer of Code program is over and I am positive I have gained so many experience in this period and additionally I have been done a significant work for GNU Octave about the Sisotool. Therefore, in this last post I all will go over in the project, describe …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/GSoC Summaryhttps://me-ydv-5.github.io/gsoc2018/summary2018-08-07T12:18:00+00:00<h1 id="octave-code-sharing">Octave Code Sharing</h1>
<hr />
<p><strong>UPDATE</strong>: This post can actually be seen in the <a href="https://bitbucket.org/me_ydv_5/octave/wiki/Home">project wiki</a> as well. I forgot to push it on github.io.</p>
<hr />
<p><strong><a href="https://me-ydv-5.github.io/feed.xml#code">CLICK HERE</a> TO VIEW THE ACTUAL CODE CHANGES.</strong></p>
<p>Now that the coding period has finally ended, I’m happy to share the summary of my project.</p>
<p>My project was Octave Code Sharing. As the name suggests, it mainly focused (the first half of the project) on “sharing” the Octave code. The intended design was, the user should be able to push their octave script onto <a href="https://wiki.octave.org">wiki.octave.org</a> for others to use and see.</p>
<p>For this, I needed a script to produce the script in a format that is acceptable by MediaWiki. My mentor had already done this bit by implementing the <a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/publish__.m"><code class="highlighter-rouge">publish</code></a> function. What this does is, it takes the user script and converts it to the prescribed format (if available), by default being <code class="highlighter-rouge">html</code>. So to convert it to wiki format, we needed <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/miscellaneous/private/__publish_wiki_output__.m">__publish_wiki_output__.m</a>, which I refactored a bit. This file is responsible for producing the wiki output. <code class="highlighter-rouge">publish</code> is used to parse the script file. After this, the main task was to find a way to upload the formatted file to the wiki server. An excellent reference point for getting started with this was <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/wikiLogin.sh">this script</a>. The script was written by the mentor as a bash script. So, I had to convert this script into an Octave counterpart so that we won’t need to execute third party code (bash script in this case) to transfer the content of the file to the wiki server.</p>
<p>Because the backend of Octave is mostly C++, I needed to use <a href="https://curl.haxx.se/libcurl/">libcurl</a> library to perform the actual transfer, because Octave does not have its own implementation of such a library and many other areas of the software use libcurl as well.</p>
<p>The workflow is like this:</p>
<ol>
<li>Input the script, user’s password and username to <a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/publish_to_wiki.m"><code class="highlighter-rouge">publish_to_wiki</code></a> function, which converts the script to wiki format using <code class="highlighter-rouge">publish</code> function internally and saves the output file to a directory named <code class="highlighter-rouge">html</code> (analogous to MATLAB).</li>
<li>The same function then picks out the figures from the script, if any.</li>
<li>Then it inputs the figures, formatted output file content and credentials to <a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/wiki_upload.m"><code class="highlighter-rouge">wiki_upload</code></a> script.</li>
<li><code class="highlighter-rouge">wiki_upload</code> script then establishes the connection with the wiki server by asking for a login token.</li>
<li>It stores the cookies in a temporary file.</li>
<li>CSRF Login is then performed to the wiki server and an edit token (for editing a page) is obtained.</li>
<li>The wiki formatted output file from <code class="highlighter-rouge">publish</code> is then uploaded to the server.</li>
<li>Figures are uploaded at last, with proper verification such that it doesn’t upload a particular figure if it alreadty exists on the server.</li>
</ol>
<p>To test yourself, you may want to use the following command:</p>
<p><code class="highlighter-rouge">publish_to_wiki ("script_file", "username", "password")</code></p>
<p>This should place your script on a URL similar to <code class="highlighter-rouge">https://wiki.octave.org/script_file</code>.</p>
<p><strong>An already performed example of this is, when I tried to publish a script named <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/intro.m">intro.m</a> on the <a href="https://wiki.octave.space">test wiki server</a> set up by the mentor.
I used the command,</strong></p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>publish_to_wiki("intro", "myUserName", "myPassword");
</code></pre></div></div>
<p><strong>and I got <a href="https://wiki.octave.space/index.php/Intro">this page</a> as the result. This page has <em>not</em> been written manually on the server, rather its all about the function.</strong></p>
<p>An important point in this process is the storing of cookies, which was performed by libcurl and set up in <a href="https://bitbucket.org/me_ydv_5/octave/src/aa660b7dcae72a2e769ecbfe71b56c86012ca2db/liboctave/util/url-transfer.h#lines-158"><code class="highlighter-rouge">liboctave/url-transfer.h</code></a>. The most challenging part in this part of the project was to figure out the uploading of figures, which I was able to do in a week or so. This needed me to read a lot of documentation and codes. Finally, the figures were uploaded using the <a href="https://bitbucket.org/me_ydv_5/octave/src/aa660b7dcae72a2e769ecbfe71b56c86012ca2db/liboctave/util/url-transfer.cc#lines-721"><code class="highlighter-rouge">form_data_post</code></a> function.</p>
<p>This was the first half part of the project.</p>
<p>Next was implementing the MATLAB compatible <a href="https://github.com/octave-de/octave-web#the-intended-design">RESTful services for Octave</a>. The project included implementing the <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/web/weboptions.m"><code class="highlighter-rouge">weboptions</code></a>, <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/web/webread.m"><code class="highlighter-rouge">webread</code></a> and <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/web/webwrite.m"><code class="highlighter-rouge">webwrite</code></a> functions. Other implementations of the suite will be followed in post GSoC work.</p>
<p>In this part of the project, working on <code class="highlighter-rouge">weboptions</code> was very fun. I got to learn a lot about the internal processing of function files and how Octave uses handle classes to cater the needs of user defined classes. Also, the way getters and setters work is really nice, even though there is still space for Classdef in octave.</p>
<p><code class="highlighter-rouge">weboptions</code> is used to put header fields in the other two functions.</p>
<p><code class="highlighter-rouge">webwrite</code> is used to POST something to a URI. It can also take a weboptions object as an argument to amend the header fields.</p>
<p><code class="highlighter-rouge">webread</code> is used to GET something from a URI. It also accepts weboptions object as an input.</p>
<p>You can refer to the help text of the functions to know more about them. A few of the weboptions’ options are not yet implemented, which are explicitly listed in its help text, after discussion with mentor, so they won’t work with webwrite and webread either.</p>
<p>The challenging part in this part of the project was, passing the weboptions object from the octave script function to the C++ backend and then performing the required operations on them. These functions, too, use <code class="highlighter-rouge">libcurl</code> for sending HTTP requests.</p>
<p>All the goals of the project were met. There were times when I was unable to get the thing done, be it trivial or not, but with continuous lookup and exploring, I was able to get them done. Sometimes, there are reasons that are out of your reach, like the sudden power failures that I encountered in my machine during the last phase. So, Kai, my mentor, being at his best, understood the concern and extended the weekly plan by a couple of days after I requested. I had also implemented a <a href="https://bitbucket.org/me_ydv_5/server_code">test server</a> in java for GET and POST request only to find a week later that there’s one easy alternative of <a href="https://httpbin.org/get">https://httpbin.org/get</a> and <a href="https://httpbin.org/post">https://httpbin.org/post</a>, respectively.</p>
<h2 id="further-work-after-gsoc-is-over">Further work after GSoC is over.</h2>
<p>As for the <code class="highlighter-rouge">publish_to_wiki</code> function, all the tests that I did were as desired, however if any error is reported it will be sorted out then and there.</p>
<p>There are a few options left in the <code class="highlighter-rouge">weboptions</code> which are not currently implemented (this was intended). It would be extended as well using <code class="highlighter-rouge">jsonencode</code> and <code class="highlighter-rouge">jsondecode</code>, essential for the next round of renovation in this function.</p>
<p><code class="highlighter-rouge">webwrite</code> works pretty good in case of sending HTTP requests in text form, but it is still unable to resolve a query list of JSON form. That is one thing that’s on my to-dos.</p>
<p>I’m currently trying to polish <code class="highlighter-rouge">webread</code>, although it is in workable state, but still there’s space for some improvement. For example, when I try to GET an image from a URI, I am unable to decode the binary data from the output stream to convert it to an image. So this is what I’ve been working on currently.</p>
<hr />
<p>The period between the start of the second phase and mid of the third phase were really fruitful. I had challenges to solve and that’s when I got adrenaline rushes many times.</p>
<p>All in all, the project was very exciting, as opposed to my initial impression, where I thought it to be a web development project and using HTML/CSS and all. I really learned, not only about Octave and its codebase but also many more things like, bitbucket, libcurl, MediaWiki, etc. to name a few. I also learned to manage things on time. This was one of the greatest perks of doing the GSoC.</p>
<p>Needless to say, this all happened with the help of my supportive and understanding mentor, Kai, who timely synced up with me, even though I used to hear from my peers that their mentor doesn’t respond timely, etc. He always appreciated my efforts and had a sound idea of what task would take what duration. He also expressed his unsatisfaction at some time, which really gave me a boost to perform better. And not to forget the little things that he noticed, like checking out which student has been putting regular posts on planet octave, etc. I’ll always be thankful to him for selecting me for GSoC. Also, I’m thankful to jwe, for introducing such a cool software, that too at no cost. The list may go on without a stop, but my heatiest thanks to all the maintainers and mentors who helped me bring the best out of myself. I’ll want to keep working with Octave community in future as well!</p>
<p>To a great “codeful” summer, 2018!</p>
<hr />
<h2 id="code">CODE</h2>
<p><a href="https://bitbucket.org/me_ydv_5/octave/branches/compare/ocs%0D%40#diff">Complete diff</a> of the work done in the project.</p>
<p>Commit to the first half part, Octave code sharing - <a href="https://bitbucket.org/me_ydv_5/octave/commits/a5b41a9dcf6979c39953f8770b87cbadc23a08cd">a5b41a9</a></p>
<p>Commit to the second half part, RESTful services - <a href="https://bitbucket.org/me_ydv_5/octave/commits/a5e8a2f640ac4bc398be655ecfcd4336ca82f89c">a5e8a2f</a></p>
<hr />
<p>The bookmark ‘ocs’ is for GSoC project.</p>
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 15https://me-ydv-5.github.io/gsoc2018/week152018-07-29T12:38:00+00:00<p>With the last week of GSoC arriving, I’m also in a position to wrap up the project. This week I’m focusing on thorough testing, documentation and other code optimizations. I’ve already refactored the existing code of third evaluations a bit in two different drives. In those, I’ve made the same internal function for setting up various cURL options for webread and webwrite functions. Also I’ve reduced a significant lines of code as well in some files, because I believe writing quality and useful code is better than writing quantity and useless code, as was suggested by Kai as well in early days of the project.</p>
<p>I’ve also implemented <a href="https://bitbucket.org/me_ydv_5/octave/commits/026e8e95e076a63fe37706eae19e0b60648545f5#chg-scripts/miscellaneous/webread.m">webread</a> function the previous week.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 13 & 14https://me-ydv-5.github.io/gsoc2018/week13-142018-07-19T16:10:00+00:00<p><strong>UPDATE 2:</strong> I found the cause of the <code class="highlighter-rouge">urlread</code> function not working correctly. It’s because curl sends the request as <code class="highlighter-rouge">application/x-www-form-urlencoded</code> whereas it should be <code class="highlighter-rouge">application/json</code>, which is explicitly changed in the cURL request that I’ve written down. I think this could be solved using the <code class="highlighter-rouge">webwrite</code> function that I’ve written.</p>
<p>Now coming to the project work, I’ve been wandering inside the codebase for the last three days to implement the <code class="highlighter-rouge">webwrite</code> function. This is what I’ve implemented as <a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/webwrite.m"><code class="highlighter-rouge">webwrite.m</code></a>. It takes input and arranges them for further processing in currently what we call as <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/libinterp/corefcn/urlwrite.cc#lines-839"><code class="highlighter-rouge">__internal__</code></a> function. A sudden question could be why two different, so this is because the processing of the input is easier in Octave than in C++ and we really need to work with cURL here so C++ cannot be sidelined. MATLAB can take the following as a string with 42 as a number <code class="highlighter-rouge">str = ["His", "age", "is", 42]</code> whereas in Octave, we get the ascii value corresponding to 42 in its place. This is one difference what I observed. Due to this, I’m asking users to pass strings all along, without any numbers. But I’m not sure how I can test above <code class="highlighter-rouge">str</code> if it contains a number, so that I could warn/error the user that what he entered is not what Octave will get as. See this <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/scripts/miscellaneous/webwrite.m#lines-62"><code class="highlighter-rouge">FIXME</code></a>. And earlier I was sending a flag if the user has supplied some weboptions or not, but now if they haven’t, I’m supplying the default weboptions object, afterall it’s good to use your code when you’ve put some time to write it!</p>
<p>Coming to the <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/libinterp/corefcn/urlwrite.cc#lines-839"><code class="highlighter-rouge">__internal__</code></a> function, this took my most of the time. I’d like to explicitly write two lines of code here, even if you do not understand them, kindly read them.</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code> octave_classdef *obj = args(nargin-1).classdef_object_value ();
cdef_object object = obj -> get_object ();
</code></pre></div></div>
<p>These two lines look small, but took my most time and I was happiest when I got these working. This takes the weboptions object supplied from the m-script and gives us the C++ equivalent. I wrote this here just for my satisfaction anyway. The supposed working is, unpack the object and send the contents to an internal function to set the cURL headers which need to be amended. I was first trying to use <a href="http://octave.org/doxygen/4.0/d4/d6c/classcdef__object.html#a1e1f017e8766d4c475b9b74aca0f767b"><code class="highlighter-rouge">map_value ()</code></a> function of the <code class="highlighter-rouge">cdef_object</code> which essentially, maps the Keys (as strings) to their values (as <code class="highlighter-rouge">cdef_property</code>), see <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/libinterp/octave-value/ov-classdef.cc#l1234">this</a> but because this always gives a <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/libinterp/octave-value/ov-classdef.cc#l1238">warning</a>, which is right, I refrained from using it because employing this in a function will decrease the UX quality of the software. Instead, I extracted Keys as strings and then queried their values. To get away with the warinings, I excluded <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/libinterp/corefcn/urlwrite.cc#lines-897"><code class="highlighter-rouge">two Keys</code></a> (“delete” and “display”) for now. But this is a bad design because although there won’t be any such keys in a weboptions object, but still we should be handling it some other way, currently which I do not know, I’ll ask Kai or jwe about this.</p>
<p>The other thing that I just got to think was, I’m unpacking the object in <code class="highlighter-rouge">__internal__</code> function, and then sending it to some other function. Why not just send the object there and unpack it there? Reducing a lot of lines of code? To unpack, I wrote an equivalent <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/liboctave/util/url-transfer.h#lines-44"><code class="highlighter-rouge">struct</code></a> and then I’m passing this to <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/liboctave/util/url-transfer.cc#lines-752"><code class="highlighter-rouge">set_weboptions</code></a> function, which then puts the corresponding options in cURL. I’ll change this to simpler format so that we won’t need struct. There are a few <code class="highlighter-rouge">FIXME</code> tags in this function for which I’ll need the help of mentor and other maintainers. For example, I searched the code to find how to push back values in an <code class="highlighter-rouge">Array<std::string></code>, but couldn’t find it out. This will save us a lot of time because now that I know how Classdef’s API work in C++.</p>
<p>I also changed <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/liboctave/util/url-transfer.cc#lines-645"><code class="highlighter-rouge">http_action</code></a> function and completely removed the previous <code class="highlighter-rouge">http_get</code> and <code class="highlighter-rouge">http_post</code> functions to accomodate new HTTP methods like <code class="highlighter-rouge">DELETE</code> and <code class="highlighter-rouge">PUT</code>, Kai too had complained about this being a bad design back in phase one, so his query should also have resolved by this. Also the error in this function is not propagated correctly to the interpreter. See the FIXME in the function. Oh, and the function <a href="https://bitbucket.org/me_ydv_5/octave/src/7b22a241cd5e265c323376b935e78d8182155dde/liboctave/util/url-transfer.cc#lines-704"><code class="highlighter-rouge">set_header</code></a> that I wrote in phase two can be of good use now in phase three, so that’s good.</p>
<p>Also, <a href="http://httpbin.org"><code class="highlighter-rouge">http://httpbin.org</code></a> is a cool site to throw requests for testing, rendering my server useless. Anyway, I learnt a thing or two about that as well with this.</p>
<p>A few other small detailings are left which will be cleared as soon as me and Kai meet on the IRC.</p>
<p>I must say, initially I was over expecting, but Kai correctly saw the depth of the project and resisted me to add more work given that I still spend around 30-35 hours a week to the project (I hope this is not offending anyone!). Writing this function was a great experience. I generally get headache working on laptop, but I could seemlessly work without any long break for around 18 hours a day. Wrapping up this week’s post, I hope someone might reach here reading it. Feel free to suggest/advice me on the work.</p>
<p>Thanks and have a good time.</p>
<hr />
<p><strong>UPDATE:</strong> There’s some problem with <code class="highlighter-rouge">urlread</code> function in octave when sending post requests. Because my this week’s work is related to the same, I’ll check what exactly is getting wrong. You can try the curl command in the meantime. Also, the server will respond (most probably) at every time form now on.</p>
<hr />
<p>Hi!</p>
<p>I’m pleased to tell you all that I’ve got good remarks in second evaluation of GSoC! Kai didn’t complin about what I was lacking in the first evaluation. I’m really thankful to him for this.</p>
<p>Coming to the next and final phase of the GSoC, I have to implement <a href="https://in.mathworks.com/help/matlab/ref/webread.html">webread</a> and <a href="https://in.mathworks.com/help/matlab/ref/webwrite.html">webwrite</a> functions in Octave. I had some issues with getters and setters in <code class="highlighter-rouge">weboptions</code> which have now been rectified.</p>
<p>To check the working of the two functions, I needed a server that could entertain HTTP request and let us verify the desired results. For this, I chose <a href="https://www.playframework.com/"><code class="highlighter-rouge">Java Play! Framework</code></a>, which is a super cool tool in JAVA that serves our purpose well. Needless to say, it is RESTful by default. It works on <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC design pattern</a>. Without wasting everyone’s time, I’d like to introduce a few files that are relevant to our project.</p>
<p>The first and the foremost is the <a href="https://bitbucket.org/me_ydv_5/server_code/src/master/conf/routes"><code class="highlighter-rouge">routes</code></a> file, which binds our app to the outer world. Routes can also be called as endpoints, if anyone is aware of. We write all the HTTP requests (GET, POST, PUT, etc.) here and map those endpoints to our corresponding functions that take action on the requests. As an example, you can see that GET request to the <code class="highlighter-rouge">webhook</code> endpoint is mapped to <a href="https://bitbucket.org/me_ydv_5/server_code/src/7da1bc07f3384c270920d705a0e1a44dc12bce32/app/controllers/HomeController.java#lines-30"><code class="highlighter-rouge">webhookGet()</code></a> function. Similarly, we can write functions as per our need and do the needful.</p>
<p>Next file is a controller. Controllers are the files that essentially parse/decode your request into smaller pieces and the applies some computation with the help of auxillary functions defined elsewhere. The above mentioned webhookGet() function is what is written in a controller.</p>
<p>The last one is <code class="highlighter-rouge">application.conf</code> which has all the configurations that are needed to run the project. The server is run using the <code class="highlighter-rouge">sbt run</code> command and shipped for production using <code class="highlighter-rouge">sbt dist</code> command. Only you need to have a JAVA environment to make these command work. You can try to play with it using the source code from <a href="https://bitbucket.org/me_ydv_5/server_code/src">here</a>. Note that the latter command cannot be run on the production server unless you have high memory for it, I myself, do the dist command on my machine and then <code class="highlighter-rouge">rsync</code> the zip file to the server, although there are other easier methods available like using CI server, etc. Kindly feel free to ask me anything related to the framework or anything else related to project, in general.</p>
<p>Now that the server has been set up, we can hit GET and PUSH requests on it and tweak the behaviour of what happens with our requests as per our need. Currently, the server will behave as an echo server, so it will let you know what you sent.</p>
<p>To check if the server works as expected for you, you can issue the following command in Octave:</p>
<p><code class="highlighter-rouge">s = urlread("https://batterylow.me:9000/webhook", "get", {"mode", "testing", "verify_token", "theTokenToAccount", "message", "Post back this message to me"})</code></p>
<p>The last parameter in the above cell string should be returned to you. Note that <code class="highlighter-rouge">mode</code> and <code class="highlighter-rouge">verify_token</code> should be <code class="highlighter-rouge">testing</code> and <code class="highlighter-rouge">theTokenToAccount</code> respectively, because a request only succeeds when the above two pairs match, as you can see in the source code. <strong>Remember to use https protocol</strong> while sending the request, because it doesn’t accept requests from http (even I spent considerable time on this silly mistake!). You can use curl as well by invoking the following command:</p>
<p><code class="highlighter-rouge"> curl -H "Content-Type: application/json" -X POST "https://batterylow.me:9000/webhook" -d '{"name": "yourName"}'</code></p>
<p>Ofcourse, you can easily send the above two requests using octave as well like this:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>GET Request:
s = urlread("https://batterylow.me:9000/webhook", "get", {"mode", "testing", "verify_token", "theTokenToAccount", "message", "Post back this message to me"})
POST Request:
s = urlread("https://batterylow.me:9000/webhook", "post", {"name", "yourname"})
</code></pre></div></div>
<p>One hurdle for me right now is my system, it keeps crashing! At all unknown times! I’m trying to get it work asap so got late to complete the <code class="highlighter-rouge">webread</code> function. Today as well, I got some space now to write the post. I’ll update this post in a day or two with my implementation details of the function. (I had intimidated Kai about this.)</p>
<p><strong>NOTE:</strong> You may <em>not</em> get the response from the server because I myself need to run it on that machine. Kindly let me know on IRC if you need to suggest/advice anything. I’ll definitely reply if my machine might be working and I may be online!</p>
<p>Till then! :-)</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 12https://me-ydv-5.github.io/gsoc2018/week122018-07-09T15:02:00+00:00<p>I’ve implemented the <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/miscellaneous/weboptions.m"><code class="highlighter-rouge">weboptions</code></a> function. I also added relevant comments wherever needed, including the code of first evaluation. This completes the second phase evaluation task, i.e, implementing <code class="highlighter-rouge">wiki_upload</code> script and <code class="highlighter-rouge">weboptions</code>. There was an unprecendented delay of a week when I was trying to upload the images on the server. It would’ve given me a head start if I had completed the task without the delay, although I am still on the track. There’s one thing left, the help text that I wrote for <code class="highlighter-rouge">weboptions</code> isn’t being displayed when I issue <code class="highlighter-rouge">help weboptions</code> because it’s a <code class="highlighter-rouge">Classdef</code> and not <code class="highlighter-rouge">function</code>. Other than this, I feel the task has been executed correctly.</p>
<p>Coming to the next task, I plan to implement <code class="highlighter-rouge">webread</code> and <code class="highlighter-rouge">webwrite</code> function with MATLAB compatibilty. I think I can change <code class="highlighter-rouge">wiki_upload</code> script to be a generic one and then use it for both wiki and webwrite. This would help us extend it with any other functionality in the future.</p>
<p>Note that a few of the <code class="highlighter-rouge">weboptions</code> fields are currently for MATLAB compatibilty, as described in the previous post. Also, second phase evaluation will take place this week. Hoping for not giving Kai a chance to complain! :-)</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Second Evaluation - week 8tag:eriveltongualter.github.io,2018-07-06:/GSoC2018/week8.html2018-07-06T16:36:00+00:00<p>So, here is my last post before the second evaluation. If you have been following <a class="reference external" href="https://eriveltongualter.github.io/GSoC2018/">my blog</a> or the <a class="reference external" href="http://planet.octave.org/">octave blog</a>, you know that the purpose of this google summer of code project is to create an <a class="reference external" href="https://summerofcode.withgoogle.com/projects/#5842927301951488">Interactive Tool for Single Input Single Output (SISO) Linear Control System Design</a>. Also …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/GSoC project progress: part threetag:blogger.com,1999:blog-7386105313127736439.post-83640144326560726982018-07-03T10:09:51+00:00<div dir="ltr">The goal for the second evaluations was to code up a complete, working, command line suggestion feature that supports identifiers and graphic properties of core octave and all the octave forge packages. I am happy to say that this goal has been achieved and we do have a working suggestion feature now. The link to public repository where the code can be found is <a href="https://bitbucket.org/peesu_97/octave/commits/all" target="_blank">this</a>.<br /><br />If you haven't already, you should read my previous posts to find out what the community wanted the feature to look like and how much progress had been already made. You may need that to understand the contents of this post. In this post, I would like to talk about the additional work that has been done and the work that will be done in the days to come.<br /><br />At the time of the first evaluations, one of my mentors, Nicholas, expressed how he would be interested in seeing how the rest of the project progresses, including the aspects related to user interface and maintainability of the code by other developers. I'd like to address these points first.<br /><br />So the UI is relatively simple. You enter a misspelling and some suggestions are displayed. We could have tried adding some GUI pop-ups but I refrained myself from trying to do those. There were two primary reasons for that.<br /><ul><li>First reason is that a GUI pop-up looks very unpleasant when you are working on the CLI of Octave, but honestly, that is more of a personal opinion I suppose.</li><li>Second, and the more strong reason is that adding a GUI pop-up would have been a really complicated task due to the way octave handles errors and would have resulted in things like displaying of the "undefined near line..." error messages for the misspelled command, <b>after</b> the correct command has been executed. </li></ul>There are some other reasons as well which have been discussed with the members of the community before. Obviously we can try changing things later on, if we really want to, but as of now, suggestions are simply displayed and the user can just use the upward arrow key of their keyboard and edit the previous command to quickly correct their misspelling.<br /><br />I have accounted for code maintainability as well. I moved a few pieces of code here and there (see commit log) and have made the feature in a way that . . . all the code related to the UI, or how the feature presents itself to the user is in a separate file (scripts/help/__suggestions__.m) and all the code related to the suggestion engine, that generates the possible corrections for the misspelled identifier is in a separate file (scripts/help/__generate__.m). A lot of comments have been included in the code and the code is simple enough to be red and understood by anyone who knows how the Octave or MATLAB programming language works. Another important point is that, all the graphic properties and identifiers of Octave core and forge with which a misspelling can be compared have been stored in a database file called func.db (examples/data/func.db). I had described this file in my in <a href="https://sudeepam.blogspot.com/2018/06/gsoc-project-progress-part-two.html" target="_blank">my previous post</a>.<br /><br />Maintainability shall be very easy due to such an implementation. If UI changes are required, major changes must be done only to the file __suggestions__.m. If the algorithm of the suggestion engine has to be changed, changing the code of the file __generate__.m shall be enough and if new identifiers are added to octave (something that will be constantly done), including them in the well organized database file (which can be very easily done with a load>edit>save) would be enough.<br /><br />Now I'd like to describe the other tasks that have been done in this coding phase. These include adding the support for the remaining packages of Octave forge and adding support for the graphic properties.<br /><br />Including the remaining packages of Octave forge was very easy, all I had to do was, fetch the list of identifiers, clean up the data a little, and include it in the database file.<br /><br />The challenging part was adding the support for graphic properties. This was mainly because of the fact that it required me to write a C++ code for a missing_property_hook() function which had to be similar in architecture to the already existing, missing_function_hook() function.<br /><br />In the codebase of Octave, missing_function_hook() is a function that points to a particular m-script which is called when an unknown command is encountered by the parser. Like I had described earlier, I had extended its functionality to trigger the suggestion feature when an unknown identifier was found. The missing_property_hook() had to do something similar, call a certain m-script when an unknown graphic property is encountered.<br /><br />Rik helped a lot with this part and finally, I was able to code up a missing_property_hook() function which would trigger the suggestion feature when an unknown graphic property is encountered. Although, the code does what it is supposed to, I'd be honest here and say that this part is still a little black-box to me. I'd appreciate it if some other maintainer who is good with c++ and familiar with the code of the missing_function_hook() function would take a look at the missing_property_hook() function and point out or fix any issues that they find.<br /><br />I'd like to mention that the suggestion feature differentiates between the levels of parsing, i.e. whether the trigger is an unknown property or an unknown command, by looking at the number of input arguments. The rest of the functionality is same.<br /><br />With all these things done, I was able to realize a complete and working command line suggestion feature and complete the goal that was set for the phase two evaluations. Future work that had been planed for phase three of coding includes writing the documentation, writing some tests, fixing any and every bug that is reported, and seeing if I could use a better algorithm for the suggestion engine. An additional thing that I would like to do is to nicely wrap up the on/off button and other such user settings into a single m-script for better user experience.<br /><br />Since the phase two work is done, I'll start working on these things that have been planned for phase three from tomorrow onwards. I'll publish another post when I make some more significant changes, till then, thank you for reading and goodbye.</div>Sudeepam Pandeynoreply@blogger.comhttps://sudeepam.blogspot.com/Week 11https://me-ydv-5.github.io/gsoc2018/week112018-07-01T16:31:00+00:00<p>This week I fixed some styling in the previous codebase and other minor issues. Me and Kai had a very fruitful discussion this week for the roadmap ahead. We discussed many things.</p>
<p>Kai verified that <a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/wiki_upload.m"><code class="highlighter-rouge">wiki_upload.m</code></a> (an important dependency for <code class="highlighter-rouge">publish_to_wiki.m</code>) works as expected including the checks for uploading the same image again, wikimedia itself does a hashcheck before uploading and sends an error if the image is identical to the previous one or a warning if the image already exists on the wiki server. You can check this by calling:</p>
<p><code class="highlighter-rouge">publish_to_wiki ("script", "username", "password")</code></p>
<p>where <code class="highlighter-rouge">script</code>, <code class="highlighter-rouge">username</code> and <code class="highlighter-rouge">password</code> correspond to the script that you want to publish, your username and password for <a href="https://wiki.octave.space"><code class="highlighter-rouge">wiki.octave.space</code></a> (this will be changed to <code class="highlighter-rouge">wiki.octave.org</code> later) respectively. One problem that I noted was Octave hangs up if the connection gets lost in the middle of the transfer. But this is not native to this function, it affects all the files that require to use libcurl’s interface to Octave. Nevertheless, I tried to look into it, with the function <a href="https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html"><code class="highlighter-rouge">CURLOPT_TIMEOUT</code></a> and other, as well. Unfortunately, I couldn’t get a viable answer because the timeout value in the above function is measured from the start of the transfer and <strong>NOT</strong> when the connection gets lost. So suppose if someone has a large transfer, then setting this value is potentially dangerous because their tansfer will get cancelled in the middle of the process even though the connection is good. Other than this, there were provisions to exit the API if the speed gets below a given threshold. This again cannot be guaranteed because it might be that the user may have a lesser internet speed. So, the idea of resuming Octave was dropped altogether, its again upto the user to get a proper internet connection! Other than this, there was the quest of making test cases for <code class="highlighter-rouge">wiki_upload.m</code> for which I suggested to measure the <code class="highlighter-rouge">Content-Length</code> of the query string that is formed as part of the transfer. This again has problems, because the libcurl API for Octave doesn’t let the developer know the actual query string which is transferred over the connection. FYI, the query string formation takes place <a href="https://hg.savannah.gnu.org/hgweb/octave/file/7dad5fa7e88e/liboctave/util/url-transfer.cc#l743"><code class="highlighter-rouge">here</code></a>. This would only add a big overhead for the returned values from the C++ API to the Octave code because all of the functions that use the <code class="highlighter-rouge">perform ()</code> (it actually performs the transfer) function will need to be changed. I initially had suggested this because during debugging I had actually changed the <code class="highlighter-rouge">form_query_string</code> function according to my needs. But then I realised that this is not feasible because:</p>
<ul>
<li>
<p>As mentioned above, there would be unnecessary overhead to change all the functions because of change in <code class="highlighter-rouge">form_query string</code> function. Also, a number of other already implemented functions use it. So, regression can occur.</p>
</li>
<li>
<p>We actually cannot measure the actual Content-Length properly, because the content is sent in <em>UTF-8</em> format which replaces all the non-alphabetical characters with their UTF-8 version. So a whitespace is changed to <code class="highlighter-rouge">%20</code>. Of course there are other ways to measure the UTF values for all the characters by running various for loops and adding offsets to a given values to find another value, etc. But I do not think this is a good measure to test the main working of <em>wiki_upload</em> which essentially needs to check if the file has been uploaded or not on the wiki.</p>
</li>
</ul>
<p>Will see if there’s anything else I could do about this, else I will need to manually check at various times if the file has been uploaded correctly or not.</p>
<p>Lastly, the commenting part, which I don’t want to loose marks for, is left and I’ll be doing that in the coming days, before the evaluation so that Kai doesn’t get a chance to bash me on this. ;-)</p>
<p><em>THIS COMPLETES THE FIRST HALF OF THE PROJECT, i.e., OCTAVE CODE SHARING.</em></p>
<hr />
<p>Next lined up is, setting up <strong>RESTful web services for Octave</strong>.</p>
<p>We had a good discussion over this too. First to be implemented is, <a href="https://www.mathworks.com/help/matlab/ref/weboptions.html"><code class="highlighter-rouge">weboptions</code></a>. I initially thought of implementing it using a <code class="highlighter-rouge">struct</code> in the backend. But then Kai suggested to use <em><code class="highlighter-rouge">Classdef</code></em> in Octave and making an object. Of course, he was right, one simple and foolish reason being, weboptions in MATLAB doesn’t display the <code class="highlighter-rouge">Password</code> field and if <code class="highlighter-rouge">struct</code> is deployed for this, there’s no way that the password is stored in a plain text and displayed as obscured.</p>
<p><a href="https://bitbucket.org/me_ydv_5/octave/src/default/scripts/miscellaneous/weboptions.m">Here</a> is what I’ve implemented for this. It almost acts like the weboptions in Matlab with subtle differences. For example, when you call the following, it will show the answer that follows:</p>
<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code>>> d = weboptions
<object weboptions>
</code></pre></div></div>
<p>MATLAB shows the values of the fields instead. For that I’ve created a method <code class="highlighter-rouge">values</code> so that when you call <code class="highlighter-rouge">d.values</code> or <code class="highlighter-rouge">values(d)</code>, you get to see all the values that are set for the object. There are a few problems which will most probably get rectified as me and Kai have another meeting on IRC. Basically, I’ll need to find a way to represent cell string in it’s input form, i.e, something like</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code> ans = {"foo", "bar"}
</code></pre></div></div>
<p>and not</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ans =
{
[1,1] = "foo"
[1,2] = "bar"
}
</code></pre></div></div>
<p>You can set values for the various fields and members in the object by calling <code class="highlighter-rouge">d.field_name = your_desired_value;</code>. This will do as desired. Some of the fields are currently for the MATLAB compatiblity and will be dealt with later, like <code class="highlighter-rouge">ContentReader</code>, <code class="highlighter-rouge">MediaType</code>, <code class="highlighter-rouge">CertificateFilename</code>, etc.</p>
<p>Oh and kindly strip your latest commit on <code class="highlighter-rouge">ocs</code> (a merge commit) in your local repo, else it’ll create a new head. Sorry about that!</p>
<p>Wrapping up for now! Apologies for such a long post.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Edit Compensantor Dynamicstag:eriveltongualter.github.io,2018-07-01:/GSoC2018/week7.html2018-07-01T14:34:00+00:00<p>So far, to design a controller using sisotool we need to select the desired feature to add to the compensator, such as real and complex pole or zero. In order to perform this task, we have two options. First, we can go to the main tab and select the feature …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Back to Codingtag:eriveltongualter.github.io,2018-06-24:/GSoC2018/week5-6.html2018-06-24T06:19:00+00:00<p>Results from the first evaluation came 9 days ago. All of the three gsoc student were successful! For the readers of my blog, you can find them at <a class="reference external" href="http://planet.octave.org/">http://planet.octave.org/</a>. If you already a reader of planet octave, you are in the right place.</p>
<p>The feedback from my …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Week 9 & 10https://me-ydv-5.github.io/gsoc2018/week9-102018-06-23T12:38:00+00:00<p><strong>UPDATE:</strong> I am more than happy to announce that I’ve been able to upload the images on to the wiki server!! The problem was not in <code class="highlighter-rouge">form_data_post</code> but in <a href="https://bitbucket.org/me_ydv_5/octave/src/59d3d9c4af257ca141e60420e9ba3c8ab32af0af/liboctave/util/url-transfer.cc#lines-663"><code class="highlighter-rouge">http_post</code></a> (in my version of Octave’s repository). There were two faults:</p>
<ul>
<li>I was calling <code class="highlighter-rouge">http_action</code> which then used to call <code class="highlighter-rouge">http_post</code> inturn which then POSTed the data, despite the fact that there was no <code class="highlighter-rouge">POSTFIELD</code> or POSTable data with me, it was <code class="highlighter-rouge">FORM</code> submission instead. I’ve now added the <code class="highlighter-rouge">perform()</code> function in the <code class="highlighter-rouge">form_data_post</code> itself so that I won’t need to interact with the former.</li>
<li>I had set the <a href="https://bitbucket.org/me_ydv_5/octave/src/59d3d9c4af257ca141e60420e9ba3c8ab32af0af/liboctave/util/url-transfer.cc#lines-671"><code class="highlighter-rouge">Content-Length</code></a> parameter to get from the length of POSTFIELDS, which isn’t needed as of now.</li>
</ul>
<p>Nevertheless, I’m pumped up again for furthering my work!</p>
<hr />
<p>There has been some problem in completing the <code class="highlighter-rouge">upload_to_wiki</code> script. Unfortunately, I’m unable to send the images on to the servers. Other than that, everything is working fine. I’ve been trying to do the same since the past week, but still no success, even after tracking down the cURL’s source code.</p>
<p>Basically we need to have a linked list of pointers to the information that we want to send as a form data. I tried to traverse that (It is native to cURL and unseen for the end user). Everything looks fine there. The only bottleneck that I can observe for now is, the <a href="https://bitbucket.org/me_ydv_5/octave/src/59d3d9c4af257ca141e60420e9ba3c8ab32af0af/liboctave/util/url-transfer.cc#lines-710"><code class="highlighter-rouge">form_data_post</code></a> function is not working as desired. I’ve tried all other alternatives to check where my code is not responding as per standards. But I think only the above function must have problem.</p>
<p>I also discussed this issue with the mentors and other people at IRC channel. However, there’s one more thing to it. The functionality that I’m currently using (<a href="https://curl.haxx.se/libcurl/c/CURLOPT_HTTPPOST.html">CURLOPT_HTTPPOST</a>) is deprecated now from cURL version 7.56 and instead MIME API is being used. (See <a href="https://github.com/curl/curl/pull/1839">this</a> for more.) I asked Kai about this and he too agreed on the fact that I shuold be keeping backwards compatibilty, but then jwe and andy suggested me to use the latter funcitonality with MIME api and then make the feature available only to those who have cURL 7.56+. I’ll ask the mentor once again if either the problem gets solved for now and then I change it to MIME API once the work gets over, or I should instead do it now only.</p>
<p>Other than that, I will start implementing the RESTful services from now on, the other half part of the project. So, we’ll see how much of these functions that I’ve implemented so far get reused.</p>
<p>Until then! :-)</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 8https://me-ydv-5.github.io/gsoc2018/week82018-06-11T15:15:00+00:00<p>Hi! Now that my exams are over, I am back on the track. First evaluation will take place this week. I will continue my work from the last task, i.e, <code class="highlighter-rouge">wiki_login.m</code>, to complete its implementation and then use it in <code class="highlighter-rouge">publish_to_wiki</code> function that will help us upload the wiki to user’s online wiki. After that I plant to implement the RESTful services part of the project, which has <code class="highlighter-rouge">weboptions</code> function lined up at first.</p>
<p>I’ll update this blog with any subsequent progresses.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/GSoC project progress: part twotag:blogger.com,1999:blog-7386105313127736439.post-32333378504685285972018-06-11T10:03:19+00:00<div dir="ltr">In my previous post, I talked about all the major discussions that have been made with the community, what the suggestion feature would be like, how I plan to realize this feature, and how I have extended the functionality of the scripts/help/__unimplemented__.m function file to integrate the command line suggestion feature with Octave. In this post, I would like to share my progress and talk about how the current implementation of the suggestion feature is working. The link to the public repository that contains the code for this feature can be found <a href="https://bitbucket.org/peesu_97/octave/commits/all" target="_blank">here</a>.<br /><br />The goal for the first evaluations was to code up a small model that would show how this feature integrates itself with Octave. That part, however, was completed by the time I had made my last blog post. I had been working on a full-fledged command line suggestion feature since then and till now, I have been able to complete a working command suggestion feature that supports identifiers from core octave and 40 octave-forge packages. Lets start looking at various parts of the feature.<br /><br />Whenever the __unimplemented__.m function file, fails to identify, whatever the user entered, as a valid, unimplemented octave command, it calls one of my m-script, called <b>__suggestions__.m</b> and the command suggestion feature gets triggered. This script, __suggestions__.m, does the following things...<br /><ul><li>Firstly, based on the setting of the custom preference (set by the user with the command <b>setpref ("Octave", "autosuggestion", true/false</b>) it decides whether to display/not to display any suggestions. If the preference is 'false', it realizes that the user has turned off the feature and so it returns the control without calculating or displaying any suggestions. </li><li>However, if the preference is true, it checks if whatever the user has entered is at-least a two letter string. If not, it again, returns the control without calculating or displaying any suggestions. This is done because it is less likely that a one letter strings is a misspelled form of some command.</li><li>However, if the string entered by the user is two lettered or more, the script goes on to calculate the commands that closely match the misspelling. The work of calculation is done by a different script and __suggestions__.m, only calls that script to get the closest matching commands. These commands are then displayed to the user as potential corrections.</li><li>If the misspelling of the user is short (length of the misspelling < 5), the script entertains one typo only, However, if the length of the misspelling is more than or equal to 5, two typos are entertained as well. This essentially means that for short misspellings, commands which are at an edit distance of 1 from the misspelling are shown as potential corrections and for relatively longer misspellings, commands which are at an edit distance of 2 from the misspelling are also shown as potential corrections.</li></ul>Commands that closely match the misspelling are calculated by a different m-script. This m-script is called <b>__generate__.m</b>. It loads a list of possible commands from a database file called <b>func.db</b> and then calculates the edit distance between the misspelling and each entry of the list using a different script called <b>edit_distance.m</b>. The commands having an edit distance equal to one or two are accepted as close matches and a list of all such commands along with their edit distances is returned to __suggestions__.m which displays some or all of these suggestions depending on the logic described before.<br /><br />I'd like to mention that the strings package of Octave forge also has a function file that calculates the edit distance. It is called "editdistance.m". Therefore, to avoid compatibility issues or to avoid having two different function files that do the same thing, later on, I will include the edit_distance function that I wrote within the __generate__.m script.<br /><br /><h3><u>Improving the speed of the generation script</u> </h3>If we go on and calculate the edit distance between the misspelling and <b>each and every</b> identifier of octave (core+forge), our algorithm would take nearly 20 years to generate an output for each typographic error that the user makes. We, however, would like the time to be 20 milliseconds or so. For that, we use some smart techniques that reduce the sample space on which the algorithm has to operate.<br /><br />To reduce the time, I've made a small assumption. I have assumed that the user never mistypes the first letter of a command. A rough analysis of the misspelling data that I received from Shane of octave-online before the commencement of the project, suggests that this is a reasonable assumption and would hardly reduce the accuracy of the suggestion feature. How good is this assumption for the speed? Well, I'd just say that, for a misspelling starting with the letter 'n', this small assumption reduces the sample size from 1492 to 36 (and that is not the best case!). The worst case was that of the letter 's' in which 178 out of 1492 commands were left. Even that corresponds to an 88% reduction in the sample size.<br /><br />It is important to mention that doing this alphabetical separation at run-time would be a redundant task and a stupid idea, that would correspond to the algorithm taking 20 years again.<br /><br />Another thing that we should consider, to improve the speed, is to show suggestions from octave core + <b>loaded packages only</b>. Obviously it is not a good idea to check among the commands that belong to a package which the user is not currently using (or worst, a package that is not installed on the user's machine).<br /><br />Keeping these things in mind, I have created the func.db database file in such a way that the commands belonging to different packages are stored in different structures and are alphabetically separated as fields of that structure. So for example, func.db contains a structure called <b>control</b> which holds the identifiers from the control package only, and another structure <b>core</b> which holds the identifiers of core Octave only, and another structure <b>signal</b> which holds the identifiers of the signal package only and so on. The field <b>a</b> of the control structure (accessed by typing <i>control.a</i>) contains all the identifiers of control package starting with 'a', The field <b>b</b> (accessed by typing <i>control.b</i>) contains those identifiers of the control package that start with b, and so on. This has been repeated for all the packages available.<br /><br />To make our __generate__.m script memory efficient as well, we load the core structure (which is always required) and then check for the loaded packages and load the structures corresponding to the loaded packages only, then, using a switch case, fetch all those commands which have the same first letter as that of the misspelling (in O(1), thanks to the way in which the database is arranged) and then proceed to the next step.<br /><br />To understand the next step, we need to understand that the if a misspelling is of length p (say), and we are accepting corrections that are at an edit distance of one or two from the misspelling, then the corrections could have the following lengths only...<br /><ul><li><b>p-2</b>: Two deletes in the misspelling,</li><li><b>p-1</b>: One delete and one substitution, or one delete only.</li><li><b>p</b>: One delete and one addition, or one or two substitutions.</li><li><b>p+1</b>: One addition and one substitution, or one substitution only.</li><li><b>p+2</b>: Two additions to the misspelling.</li></ul>This fact, allows us to reduce the list further and would cut out some 5-10 more entries for normal length misspellings. This logic, however, is particularly useful for large length misspellings, because commands with large lengths are very less in number. If a user misspells the command "<i>suppress_verbose_help_message" </i>the script would take days to suggest a correction for this command without this logic, this is because edit distance algorithm is O(n1*n2) with dynamic programming, where n1 and n2 are the lengths of the strings being compared. This O(n1*n2) is repeated m times where m is the number of possible commands that could be close matches. With this logic however, the possible list would be cut down to one or two commands only. Thus, the value of m will be reduced and the close matches will be found within one or two iterations.<br /><br />That summarizes all the measures that I have taken to improve the speed of the suggestion feature. The control flow had been described before this and so that concludes the working of the suggestion feature.<br /><br /><h3><u>Conclusion</u></h3><div>This concludes phase one. What's left is to include more forge packages and to include graphic properties within the scope of this feature. Writing the documentation, writing the tests, and debugging also remains but these shall be the tasks for subsequent coding phases. Till then, goodbye, see you in the next blog post. :)</div></div>Sudeepam Pandeynoreply@blogger.comhttps://sudeepam.blogspot.com/First Evaluation - week 4tag:eriveltongualter.github.io,2018-06-10:/GSoC2018/week4.html2018-06-10T16:36:00+00:00<p>So, here is my last post before the first evaluation. If you have been following <a class="reference external" href="https://eriveltongualter.github.io/GSoC2018/">my blog</a> or <a class="reference external" href="http://planet.octave.org/">octave blog</a>, you know that the purpose of this google summer of code project is to create an <a class="reference external" href="https://summerofcode.withgoogle.com/projects/#5842927301951488">Interactive Tool for Single Input Single Output (SISO) Linear Control System Design</a>. Also, well-known …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/GSoC project progress: part onetag:blogger.com,1999:blog-7386105313127736439.post-50332356560679309452018-06-10T14:37:02+00:00<div dir="ltr">p { margin-bottom: 0.25cm; line-height: 120%; } <br /><div><h3><u><b>An Initial note....</b></u></h3><br />Alright, so first of all, I would like to apologize for not writing a proper blog post up till now. I had my Final examinations during the first week of the coding period and immediately after that, to catch up, I got so involved with the coding part that I forgot to share the progress of the project on the blog. On the positive side however, I have completed a lot of work. I can safely say that I have completed the goals that were set for phase 1 evaluations (possible style fixes may be left), but that’s not the entire good news. The phase two evaluations goal is also halfway done!<br /><br />Now, I do realize that I have not shared any details of my project until now, and so, in this blog post, I’ll share a lot of important details and talk about everything that has been discussed and done so far. I promise to post more often after this, ‘cumulative’ post. Here goes...<br /><br /><h3><u><b>The Project</b> <b>Idea</b>....</u></h3><br />If you've red the last blog post, you'd know that I plan to add something called a 'Command Line suggestion feature' to Octave and you may be wondering what that means. Basically this feature would do something like this...<br /><br />Whenever the users make a typographic error while working on Octave's command window, the command line suggestion feature would suggest a correction to them and say something like "The command you entered was not recognized, did you mean any of the following...?"<br /><br />Now I could share a detailed time-line explaining 'when' I plan to do 'what' but I believe that not everyone would be interested in reading that and so I'll skip that for now. Instead, I'll quickly talk about the following...<br /><ul><li>What the community wants the overall project to be like.</li><li>What are the challenging parts of the project.</li><li>What are my evaluation goals.</li><li>What discussions have been made, and</li><li>How much progress has been made.</li></ul>If you really would like to see my time-line then just ask for it in the comments section and I'll share a link.<br /><br /><h3><u><b>The Community Bonding Period...</b></u></h3><b><br /></b>By the time you finish reading this section, probably the only thing left to talk about would be "How much progress has been made". That is just a glimpse of how much the community has been involved in this project. It also shows how successful GNU Octave is as an open source community, not every open-source community is as open when it comes to discussions.<br /><br />Now the first thing to understand is that this project is essentially a UX improvement, and as such, Octave is not bound by 'MATLAB compatibility issues'. This is one of the primary reasons why there was so much to discuss in the community bonding period. Here are the main points that summarize the collective decision of the community on what the overall project should be like:<br /><ul><li>First of all, it was decided that the user interface, or the part handling 'how this feature hooks itself to octave' should be well separated from how the 'suggestions are generated'. This need was realized, immediately after realizing the fact that there are a lot of algorithms available that could be used to generate suggestions. Separating the integration and generation part would allow us to make sure that, in future, if a faster or a more accurate algorithm to generate suggestions is discovered, replacing the existing implementation becomes easier.</li><li>Secondly, a few problems such as, a very large output layer size, and failure on dynamic package loading were found with my proposed Neural Networks, based approach. Therefore, we decided to use a well established approach called the Edit distance algorithm for now and the Neural Networks based approach will be the 'research part' of the project. Essentially, the plan is to first use 'smart implementations' of the good old Edit-distance algorithm to realize this feature and to research and see if a Neural Network could do better after that has been done. If later on, we realize that a Neural Network (or for that matter, any other approach), really could do better than the Edit-distance approach, the algorithm can be replaced very easily (thanks to the previous point).</li><li>Next, we decided to include keywords, functions, and graphic properties within the scope of this feature. Very short keywords, user variables, and internal functions will not be included in its scope. Deprecated functions would also be included in the scope for now. Essentially, corrections would be suggested for typos close to anything that is within the scope of this feature and would not be suggested for anything that isn't.</li><li>Also, we decided to use the missing_function_hook() to realize the integration part of this feature. More about this later in this post.</li><li>Lastly, we decided that it is absolutely necessary to include an 'on/off switch' type of command that would let the users decide whether they want to use this feature or not. We plan to use custom preference for now to do this.</li></ul>That summarizes the most important discussions that took place and with that, we are in a position to talk about how the second point and the last point are directly related to what are the 'challenging parts of the project'. Let's start with that.</div><div></div><div><br />Essentially, the second point talks about the algorithm that will be used to generate the corrections that are ultimately going to be shown to the user. The challenging part is that this algorithm should provide a <b>minimum speed-accuracy trade-off</b>. I did know about the Edit-distance algorithm beforehand but I initially believed that a Neural Network would outperform it in terms of the speed accuracy trade-off. Discussing the idea with the community made me realize that there are some critical loopholes in the Neural Network based model and although they could definitely be improved with more research, I should not jeopardize the entire project just to proof that Neural Networks could do better. We therefore decided to do what I had described earlier in the second point. <br /><br />At this point, defining a 'smart implementation' of Edit distance remains. Basically, Edit distance is a very accurate algorithm that quantifies how dissimilar two strings are. The only problem with it is its speed (my primary reason for initially proposing a trained Neural Network). Essentially, by a smart implementation of the algorithm, we mean an implementation which would maximize the computation speed by reducing the sample space, on which the algorithm has to work on. This would be done using some clever data management techniques and some probability based assumptions. Some discussions related to these were also done during the community bonding and since then, I have been looking at a lot of suggestion features of other free and open source softwares to device some clever techniques. Good progress has been made but I'll share that in another blog post.<br /><br />The last point talks about a very important 'on/off' feature, the tricky part with this was that Octave comes in both a GUI and a CLI and so a common method that does the job could have been hard to find. However, this problem was solved with relative ease, and we decided to use <b>custom preference </b>to realize this part. This gave us a simple and common command to switch on/ switch off the feature.<br /><br /></div><div>These discussions led me to reset my term evaluation goals which are as follows now:- <br /><ul><li><b><u>Phase-1 goal</u></b>: To code up and push an algorithm independent version of the suggestion feature into my public repository. Essentially this would show how this feature integrates itself with Octave.</li><li><u><b>Phase-2 goal</b></u><b>:</b> A development version of Octave with a working (but maybe bugged and surely undocumented) command line suggestion feature integrated into it.</li><li><u><b>Phase-3 goal</b></u>: The primary goal would be to have a well documented, well tested and bug free command line suggestion feature. The secondary goal would be to research and try to produce a Neural Network based correction generation script that outperforms the edit distance algorithm.</li></ul>...and that, marked the end of the major discussions and the community bonding period.</div><div><br /></div><div><h3><u><b>Progress made so far...</b></u></h3></div><div><b><br /></b></div><div>So far, I have coded up the phase-1 goal. The public repository can be seen <a href="https://bitbucket.org/peesu_97/octave/commits/all" target="_blank">here</a>. It very well shows how we have used the missing_function_hook() to integrate the feature with octave. The following points summarize the working:<br /><ul><li>Essentially, whenever the parser fails to identify something as a valid octave command it calls the missing_function_hook() which points to an internal function file, '__unimplemented__.m'.</li><li>This file checks if whatever the user entered is a valid, unimplemented octave (core or forge) command or if it is a implemented command but belongs to an unloaded forge package. If yes, it returns an appropriate message to the user and if not, it does, or rather, used to, do nothing.</li><li>To realize the suggestion feature, I have extended its functionality to check for typographic errors whenever the command entered was not identified as a valid unimplemented/ forge command.</li></ul>By using the missing_function_hook() the keywords and built-in functions were automatically bought into the scope of this feature. Graphic properties remain because there is no missing_property_hook() in octave right now. I have discussed this with the community and I'll try to code it up in the subsequent weeks.<br />Besides that I have also figured out how the Edit Distance algorithm can be made 'smart'. I'll push an update and write another blog post as soon I master and code up the entire thing. For now, thanks for reading, see you in the next post. :)</div></div>Sudeepam Pandeynoreply@blogger.comhttps://sudeepam.blogspot.com/Geeting closer to the First Evaluation - week 3tag:eriveltongualter.github.io,2018-06-03:/GSoC2018/week3.html2018-06-03T10:48:00+00:00<p>First Evaluation period is around in the corner. As was proposed on my <a class="reference external" href="https://eriveltongualter.github.io/GSoC2018/welcome-gsoc.html">first post</a> of my timeline, the work I have been doing is on time.</p>
<p>For this past week, I added some functionalities to the Root Locus Editor. This time, the user can add: real poles, complex poles …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Plots are Working - week 2tag:eriveltongualter.github.io,2018-05-29:/GSoC2018/week2.html2018-05-29T17:38:00+00:00<p>Here we go one more week of code. This week I have continued my work from previous week related to interface of sisotool. Just reiterating what was done last week, I created a couple GUI to understand a little better how the UI Elements works in Octave. For this week …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Week 6 & 7https://me-ydv-5.github.io/gsoc2018/week6-72018-05-29T17:26:00+00:00<p>As I’ve already completed my first evaluation work and got it reviewed the same from mentor, I won’t be doing much work in these two weeks due to my end semester exams. However, I’ll be continuing my post from week 8, positively.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Code begins - week 1tag:eriveltongualter.github.io,2018-05-21:/GSoC2018/week1.html2018-05-21T16:27:00+00:00<p>The first week of coding has been completed.</p>
<p>As I mentioned in the <a class="reference external" href="https://eriveltongualter.github.io/GSoC2018/bonding.html">last post</a>, the goal of this previous week was to create a fixed layout to study the plot diagrams for the sisotool, as well as to add some UI Element functionality to control the interface. The following …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Week 5https://me-ydv-5.github.io/gsoc2018/week52018-05-21T11:06:00+00:00<p>With 25th May approaching, I’m confident to say that I’ve completed my first evaluation (although some grammatical and styling rechecking is left). Now, as directed by Kai, I’ve implemented the <a href="https://bitbucket.org/me_ydv_5/octave/src/ocs/scripts/miscellaneous/wiki_login.m"><code class="highlighter-rouge">wiki_login.m</code></a> function from using the internals of <a href="https://bitbucket.org/me_ydv_5/octave/src/3fe1c77dfbb320306d228e0cd10608b7f8057165/liboctave/util/url-transfer.cc#lines-731"><code class="highlighter-rouge">url-transfer.cc/h</code></a> class. I’ve scapped the earlier design of implementing <code class="highlighter-rouge">cookie_manager.m</code> script and <code class="highlighter-rouge">libcurl_wrapper.cc</code> class for doing the cookie thing and directly implemented <a href="https://bitbucket.org/me_ydv_5/octave/src/3fe1c77dfbb320306d228e0cd10608b7f8057165/libinterp/corefcn/urlwrite.cc#lines-739"><code class="highlighter-rouge">__wiki_login__</code></a> as an internal function in <code class="highlighter-rouge">urlwrite.cc</code>. A user is now able to log into api.php</p>
<p>To test my implemtation:</p>
<ul>
<li>
<p>Make a test account on <code class="highlighter-rouge">wiki.octave.space</code> with username and password of your choice.</p>
</li>
<li>
<p><code class="highlighter-rouge">hg clone https://me_ydv_5@bitbucket.org/me_ydv_5/octave</code></p>
</li>
<li>
<p><code class="highlighter-rouge">hg up ocs</code></p>
</li>
<li>
<p><code class="highlighter-rouge">cd path/to/your/build/tree/</code> and execute <code class="highlighter-rouge">make -jX</code></p>
</li>
<li>
<p><code class="highlighter-rouge">./run-octave</code></p>
</li>
<li>
<p>execute <code class="highlighter-rouge">wiki_login ("https://wiki.octave.space/api.php", yourUsername, yourPassword);</code></p>
</li>
</ul>
<p>If it is all successful, you should see a prompt <code class="highlighter-rouge">Success: logged in as yourUsername</code>.</p>
<p>Other than that, I’m really grateful to <a href="http://jweaton.org/">jwe</a> for moving the <code class="highlighter-rouge">version.h</code> file to <code class="highlighter-rouge">liboctave</code> from <code class="highlighter-rouge">libinterp</code> as this is needed to get the <code class="highlighter-rouge">OCTAVE_VERSION</code> for the user agent (see week 3 for this). Eariler, it was in <code class="highlighter-rouge">libinterp</code> and we must not use anything from <code class="highlighter-rouge">libinterp</code> in <code class="highlighter-rouge">liboctave</code> as it should be compiled irrespective of the former.</p>
<p><em>NOTE: You may need to re-run <code class="highlighter-rouge">./bootstrap</code> and <code class="highlighter-rouge">./configure</code> due to above changes.</em></p>
<p>Lastly, I would request to someone who uses Windows (My Windows ran into some problem and I’m now unable to connect to internet, I’ll reinstall it whenever I get some time) and/or Mac OS to test my implementation and report any errors.</p>
<p>Needless to say I’m always looking forward to constructive feedback/criticism.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 4https://me-ydv-5.github.io/gsoc2018/week42018-05-16T16:16:00+00:00<p>Continuing further, I added more options in <code class="highlighter-rouge">libcurl_wrapper.cc</code>. As described in earlier posts, the current implementation of <code class="highlighter-rouge">wiki_login.m</code> has the JAVA’s interface to Octave in use and I need to replace it with Octave’s own implementation. So, I’ve taken two steps in this direction. Now a user is able to retrieve token when he executes <code class="highlighter-rouge">wiki_login</code> AND use the cookies that are set in a temporary <code class="highlighter-rouge">.txt</code> file to login into the <code class="highlighter-rouge">api.php</code> wiki. Currently, there’s a problem in logging in, because the following cookies are unable to get added, <code class="highlighter-rouge">octave_org_session</code>, <code class="highlighter-rouge">octave_orgUserID</code>, <code class="highlighter-rouge">octave_orgUserName</code>, <code class="highlighter-rouge">octave_orgToken</code>. I got to know about these cookies when I tried to execute the curl-cli commands for logging in.</p>
<p>I also understood how the <code class="highlighter-rouge">HAVE_CURL</code> macro encapsulates the <code class="highlighter-rouge">curl_transfer</code> class, i.e, if curl is available in a machine, then this class exists, else not. <code class="highlighter-rouge">HAVE_CURL</code> is a macro that is set during <code class="highlighter-rouge">./configure</code> stage of building the software. I will be extending my work in this class essentially in the coming week.</p>
<p>I’ve also added the files in their appropriate directories.</p>
<p>A new <a href="https://wiki.octave.space">dummy wiki</a> has been created by Kai for testing purposes. I’ll be using this from now on.</p>
<hr />
<p><a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs">Link</a> to BitBucket repo.</p>Sahil Yadavhttps://me-ydv-5.github.io/Community Bonding Periodtag:eriveltongualter.github.io,2018-05-15:/GSoC2018/bonding.html2018-05-15T16:16:00+00:00<p>The community bonding period is over. The past 3 weeks were really busy because I was in my finals week, final projects and my doctoral research. However, I basically completed everything I wanted to before the “Coding officially begins!”:</p>
<ul class="simple">
<li>Finished Optimal Control and Intelligent Control System classes;</li>
<li>Submitted a conference …</li></ul>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Week 3https://me-ydv-5.github.io/gsoc2018/week32018-05-08T05:16:00+00:00<p><strong>UPDATE on 12-May-2018</strong>: You can now directly test my work by cloning my repo, updating the source tree to my bookmark by <code class="highlighter-rouge">hg up ocs</code>, making a build and calling <code class="highlighter-rouge">wiki_login</code> from octave-cli to get a login token in return. I’ve added the files and necessary changes in the codebase itself. There’s no directory <code class="highlighter-rouge">ocode</code> now.</p>
<p>If you happen to already have a build of octave, just do the following:</p>
<ul>
<li><code class="highlighter-rouge">cd path/to/your/source/tree</code></li>
<li><code class="highlighter-rouge">hg pull https://me_ydv_5@bitbucket.org/me_ydv_5/octave</code> in your source tree.</li>
<li><code class="highlighter-rouge">hg up ocs</code></li>
<li><code class="highlighter-rouge">hg up -r 8bbf393</code></li>
<li><code class="highlighter-rouge">make -jX</code> in your build tree.</li>
</ul>
<p>This will save your time of cloning the entire repo and compiliing.</p>
<hr />
<p>As mentioned in the previous post, I worked on <a href="https://bitbucket.org/me_ydv_5/octave/commits/8141dc41d467cc7c8d48990aefec92bd445f8663">__publish_wiki_output__.m</a> and <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/scripts/miscellaneous/publish.m">publish.m</a> code. The __publish_wiki_output__.m has been added as an internal function in scripts/miscellaneous/private. I skimmed through the parser in publish.m to get a gist of how it actually works. It has three levels of parsing:</p>
<ol>
<li>
<p>Extract the overall structure (paragraphs and code sections).</p>
</li>
<li>
<p>Parsing the content of a paragraph.</p>
</li>
<li>
<p>Generate the output of the script code and look after figures produced in code.</p>
</li>
</ol>
<hr />
<p>After that, I studied how the <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/liboctave/util/url-transfer.h">url-transfer.h</a> file is implemented which contains a base class named <code class="highlighter-rouge">base_url_transfer</code> which has a derived class named <code class="highlighter-rouge">url_transder</code>. One thing that puzzled me while doing so was, why there has to be a macro <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/liboctave/util/url-transfer.cc#l220"><code class="highlighter-rouge">HAVE_CURL</code></a> in order for <code class="highlighter-rouge">curl_transfer</code> to be defined and why we haven’t defined <code class="highlighter-rouge">url_transfer</code> class itself? I would try to get these doubts solved this week.</p>
<p>The problem of <a href="https://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client">user agent</a> was solved by selecting the following user agent:</p>
<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code> GNU Octave/OCTAVE_VERSION (https://www.gnu.org/software/octave/ ; help@octave.org) libcurl/LIBCURL_VERSION
</code></pre></div></div>
<p>where OCTAVE_VERSION and LIBCURL_VERSION correspond to the user’s octave and libcurl version respectively. <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#Locs/libcurl_wrapper.ccT184">This</a> code precisely does the same for us.</p>
<p>My inteded plan for the wrapper is, make a <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#chg-ocs/cookie_manager.m">cookie_manager.m</a> file that will process the various user options (like verbose output, timeout settings, api.php url, etc.) and pass the values to an internal <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#chg-ocs/__curl__.cc">__curl__.cc</a> function which will in turn, take help from <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#chg-ocs/libcurl_wrapper.cc">libcurl_wrapper.cc</a> to do various tasks (all the work related to cookies will be looked after by it, essentially).</p>
<p>Curently, all the code in <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#chg-ocs/wiki_login.m">wiki_login.m</a> has been commented out except the first step of login, i.e, <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#Locs/wiki_login.mT20">getting a login token</a> from the api.php, which it smoothly does, as of now. I am assuming that the file which would store the cookies, is <a href="https://bitbucket.org/me_ydv_5/octave/commits/0a2add3cc5e614ebf1e6d04391a197d85397f7bb?at=ocs#Locs/cookie_manager.mT9">temporary</a> and should be deleted once the session expires. This is one of the things I will be looking on in this week.</p>
<p>I’ve migrated all the developments from my forked <a href="https://github.com/me-ydv-5/octave-web/commits/master">git repo</a> to my mercurial bookmark <a href="https://bitbucket.org/me_ydv_5/octave/commits/branch/ocs"><code class="highlighter-rouge">ocs</code></a> recently and so I was not sure where should I put the files in my source tree. Thus, I’ve put all of them in a directory <code class="highlighter-rouge">ocode</code> for now.</p>
<p>To test this for yourself:</p>
<ul>
<li>Clone my build tree using <code class="highlighter-rouge">hg clone https://me_ydv_5@bitbucket.org/me_ydv_5/octave</code></li>
<li>Make yourself a build of octave (<code class="highlighter-rouge">make -j2</code>, etc.).</li>
<li><code class="highlighter-rouge">cd octave</code>.</li>
<li>Update to my bookmark using <code class="highlighter-rouge">hg up ocs</code>. (IMPORTANT!)</li>
<li><code class="highlighter-rouge">cd ocode</code>.</li>
<li>Execute <code class="highlighter-rouge">Makefile</code> in octave.</li>
<li>Execute <code class="highlighter-rouge">wiki_login</code> in octave to get a login token.</li>
</ul>
<hr />
<p>All other details of the wrapper’s implementation will be followed in the next post.</p>
<p>The next week would follow the following advances:</p>
<ul>
<li>Choosing the right location for the files (after I get a green light for the current developmental path).</li>
<li>Extending other options in the wrapper for wiki_login’s steps 2 and 3.</li>
<li>Implementing cookie_manager with other user options.</li>
<li>Writing help text and text cases, if any.</li>
<li>Correction of existing work/ changing the stategy as adviced by mentor, or anyone else.</li>
<li>Look into how can I use existing <code class="highlighter-rouge">base_url_transfer</code> class in the wrapper and resolve my query of the <code class="highlighter-rouge">HAVE_CURL</code> macro and shared pointers, etc.</li>
</ul>
<p>I am optimistic that I would be able to complete my first evaluation work by 25th May or so, as I will need to focus on my end term examinations after that which will start from 1 June. We don’t get holidays in between the exams!</p>
<p>Please let me know if I am doing it the right way or not, by either replying to <a href="http://lists.gnu.org/archive/html/octave-maintainers/2018-04/msg00173.html">this thread</a> or by simply dropping a message on <code class="highlighter-rouge">#octave</code> for <code class="highlighter-rouge"><batterylow></code>. All the suggestions are always welcomed.</p>
<p>Oh and not to forget, I got my <a href="https://batterylow.me">domain</a> an SSL certificate, now all the requests are served via HTTPS only!</p>
<p>Stay tuned for next update.</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 2https://me-ydv-5.github.io/gsoc2018/week22018-05-01T05:51:00+00:00<p>Week 1 included setting up all the work environment, <a href="https://bitbucket.org/me_ydv_5/octave">bitbucket repository</a> for tracking my project’s progess and review,setting up and getting this blog aggregated to planet.octave.org. It also included reading up various files that are of concern to the project.</p>
<p>Week 2 would focus on getting my hands dirty in refactoring the <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/__publish_wiki_output__.m">__publish_wiki_output__.m</a> and (possibly) <a href="https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/scripts/miscellaneous/publish.m">publish.m</a> code. This would also include looking up what exact methods/functions will be needed to implement the wrapper. Currently, a proof of concept is written as a <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/wikiLogin.sh">bash script</a>. My work of writing the wrapper will be highly inspired from this script.</p>
<p>The wrapper is written so that MediaWiki can be communicated directly using Octave and there won’t be any need to use Java’s interface to Octave or the bash script itself. To know how exactly MediaWiki API works, have a look at this nicely written <a href="https://siko1056.github.io/blog/2017/03/10/getting-to-know-the-mediawiki-api.html">post</a>. Note that the $wgEnableAPI written in the post is now deprecated from MediaWiki’s version 1.32.0. Another thing that needs looking upon is MediaWiki needs a <a href="https://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client">user agent</a> in order for the client to be identified. So we need to decide what would be it.</p>
<p>Stay tuned for next update!</p>Sahil Yadavhttps://me-ydv-5.github.io/Starting with GSoC 2018.tag:blogger.com,1999:blog-7386105313127736439.post-40354355411889230522018-04-26T12:57:05+00:00<div dir="ltr">So this year, I applied to the Google summer of code and got in. Google summer of code, or GSoC, as it is usually called, is a program funded by Google that has helped open source grow for over a decade. Under this program, Google awards stipends to university students for contributing code to open source organizations during their summer breaks from the university. The details of the program can be found here: <a href="https://summerofcode.withgoogle.com/">Starting with Google summer of code.</a><br /><br />Now this year, I have been selected to work with GNU Octave. It is a free and open source software/ high level programming language which is primarily focused on scientific computing. It is largely compatible with MATLAB and is a brilliant open source alternative to it. More details about GNU Octave can be found at <a href="https://www.gnu.org/software/octave/">Free your numbers! Introducing GNU Octave.</a><br /><br />My GSoC project is about adding a <b>Command line suggestion feature</b> to GNU Octave. Stay tuned, I will share the details of the project very soon.</div>Sudeepam Pandeynoreply@blogger.comhttps://sudeepam.blogspot.com/Week 1https://me-ydv-5.github.io/gsoc2018/week12018-04-25T07:16:00+00:00<p>I’ve been selected as a <a href="https://summerofcode.withgoogle.com">Google Summer of Code</a> , 2018 student developer at <a href="https://www.gnu.org/software/octave/">GNU Octave</a>. GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.</p>
<p>A very heartful thanks to Kai T. Ohlhus, Doug Stewart, Ankit Raj and others who saw my potential and chose me for GSoC 2018.</p>
<p>My project is <strong>Octave Code Sharing</strong>. Community bonding period would include reading up material that’d be essential for the project.</p>
<hr />
<h2 id="abstract">Abstract:</h2>
<p>This project aims to come up with a pan-octave implementation that could be used to connect to <a href="https://wiki.octave.org">wiki.octave.org</a> with appropriate credentials for publishing octave scripts that could be hosted for distribution using the MediaWiki API. Currently, no formal implementation is there but a proof of concept which was implemented as a <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/publishToWiki.m">bash script</a>, later refactored to use <a href="https://www.mathworks.com/help/matlab/internet-file-access.html">JAVA’s interface</a> to Octave. Since the network connection itself has latency and response time which are significantly large to get into notice, an Octave script, which will help in connection, will not be much of a performance killer. To maintain a stateless HTTP protocol, some information that would be needed will be stored as cookies with the help of <a href="https://curl.haxx.se/libcurl/">Libcurl</a> library. All this would lead to set up of RESTful services for GNU Octave which could be further extended to support the compatibility with MATLAB’s <a href="https://www.mathworks.com/help/matlab/internet-file-access.html">RESTful interface</a>.</p>
<hr />
<h2 id="timeline">Timeline:</h2>
<ol>
<li>
<p><strong>April 23 - May 14</strong></p>
<p><strong>Community Bonding Period</strong>: Get to know more about Libcurl and its implementation. Learn about HTTP request headers and stateless transfer protocol. Finalise location of files in the codebase that will be needed for code sharing. Study the existing work that has been done by the mentor. Study publish() and grabcode() functions. Study octave::url_transfer class. Learn about MediaWiki API and its implementation (backend working).</p>
</li>
<li>
<p><strong>May 15 - May 23</strong></p>
<p>Implement the Libcurl wrapper in libcurl_wrapper.cc/h. This would include the various <a href="https://ec.haxx.se/libcurl-http-cookies.html#cookie-store-commands">abstractions</a> such as ‘ALL’, ‘SESS’, ‘FLUSH’, ‘RELOAD’ that could be used by octave’s script to use Libcurl cookies to connect to the server. There’s a <a href="https://github.com/octave-de/octave-web/blob/master/web/libcurl_wrapper.cc">preliminary implementation</a> for the same which was implemented by the mentor sometime back, which can be extended in this phase for a more general design.</p>
</li>
<li>
<p><strong>May 23 - May 31</strong></p>
<p>Add ‘wiki’ as an ‘output_format’ in publish() function. This would extend the implementation of private function <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/__publish_wiki_output__.m">__ publish_wiki_output__.m</a>
that is to be used to format a wiki published code.</p>
</li>
<li>
<p><strong>June 1 - June 10</strong></p>
<p>Non Coding period due to University major examinations.</p>
</li>
<li>
<p><strong>June 11 - June 15</strong></p>
<p><strong>First Phase Evaluation</strong> - Write Documentation of the work done upto this point and other tests such as setting up the wiki installation environment for testing the script that would be implemented in point 6. Catch up with the work if anything is lagging behind.</p>
</li>
<li>
<p><strong>June 15 - June 30</strong></p>
<p>Implement wiki_login.m which would use the Libcurl wrapper implemented in point 2. Currently <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/wiki_login.m">this</a> file uses Java’s interface to octave. But with the wrapper, the file would become an implementation of RESTful services for ‘code sharing’ and will not be dependent on Java’s interface to octave.</p>
</li>
<li>
<p><strong>July 1 - July 9</strong></p>
<p>Implement ‘weboptions’ function. It will return a default ‘weboptions’ object to specify parameters for a request to web service. This function will use the libCURL wrapper that would’ve been already implemented. Current scope of the function would be restricted to four options, viz., ‘Username’, ‘password’, ‘Keyname’, ‘KeyValue’.</p>
</li>
<li>
<p><strong>July 10 - July 13</strong></p>
<p><strong>Second Evaluation Phase</strong> - Write documentation of the work done upto here and other tests required for ‘weboptions’ function. Catch up with any previous work if left.</p>
</li>
<li>
<p><strong>July 14 - July 20</strong></p>
<p>Implement ‘webread’ function. This will read content from a web service and return data formatted as text. Other output arguments (cmap, alpha) will not be supported currently.</p>
</li>
<li>
<p><strong>July 21 - July 27</strong></p>
<p>Implement ‘webwrite’ function. This will put data in the body of an HTTP POST request to the web service.</p>
</li>
<li>
<p><strong>July 28 - August 6</strong></p>
<p>Buffer period for anything that remains. Complete documentation and testing for ‘webwrite’ and ‘webread’.</p>
</li>
</ol>
<p>For now, libcurl_wrapper.cc/h would be placed in libinterp/corefcn but could be changed whether it should be used by default or on the user’s choice because someone might not want to connect themselves with <a href="https://wiki.octave.org">wiki.octave.org</a>. I’ve for now, merged two weeks, i.e from 15 June to 30 June because the main task would be include the wrapper implementation for connecting to the webserver. This may span upto two weeks and so is the reason for two weeks of the same task.</p>
<h3 id="additional-things-to-be-done-after-gsoc-is-over">Additional Things to be done after GSoC is over:</h3>
<p>The web function is already partially implemented. The task will be to finish the implementation with various arguments such as ‘-notoolbar’, ‘-noaddressbox’, ‘-new’ for MATLAB’s compatibility. ‘websave’, ‘ftp’ and ‘sendmail’ that are a part of RESTful services, will also be implemented. Any other part of GNU octave which currently might need RESTful services using cookies can be amended to use the implementation that would result from the project. The ‘webread’ function will be extended to read values into JSON format and images as well.</p>
<hr />
<p>Thanks for reading, will be posting next update soon. Looking forward to a bug-free and code-some summer. :-)</p>Sahil Yadavhttps://me-ydv-5.github.io/Week 1https://me-ydv-5.github.io/me-ydv-5.github.io/gsoc2018/week12018-04-25T07:16:00+00:00<p>I’ve been selected as a <a href="https://summerofcode.withgoogle.com">Google Summer of Code</a> , 2018 student developer at <a href="https://www.gnu.org/software/octave/">GNU Octave</a>. GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.</p>
<p>A very heartful thanks to Kai T. Ohlhus, Doug Stewart, Ankit Raj and others who saw my potential and chose me for GSoC 2018.</p>
<p>My project is <strong>Octave Code Sharing</strong>. Community bonding period would include reading up material that’d be essential for the project.</p>
<hr />
<h2 id="abstract">Abstract:</h2>
<p>This project aims to come up with a pan-octave implementation that could be used to connect to <a href="https://wiki.octave.org">wiki.octave.org</a> with appropriate credentials for publishing octave scripts that could be hosted for distribution using the MediaWiki API. Currently, no formal implementation is there but a proof of concept which was implemented as a <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/publishToWiki.m">bash script</a>, later refactored to use <a href="https://www.mathworks.com/help/matlab/internet-file-access.html">JAVA’s interface</a> to Octave. Since the network connection itself has latency and response time which are significantly large to get into notice, an Octave script, which will help in connection, will not be much of a performance killer. To maintain a stateless HTTP protocol, some information that would be needed will be stored as cookies with the help of <a href="https://curl.haxx.se/libcurl/">Libcurl</a> library. All this would lead to set up of RESTful services for GNU Octave which could be further extended to support the compatibility with MATLAB’s <a href="https://www.mathworks.com/help/matlab/internet-file-access.html">RESTful interface</a>.</p>
<hr />
<h2 id="timeline">Timeline:</h2>
<ol>
<li>
<p><strong>April 23 - May 14</strong></p>
<p><strong>Community Bonding Period</strong>: Get to know more about Libcurl and its implementation. Learn about HTTP request headers and stateless transfer protocol. Finalise location of files in the codebase that will be needed for code sharing. Study the existing work that has been done by the mentor. Study publish() and grabcode() functions. Study octave::url_transfer class. Learn about MediaWiki API and its implementation (backend working).</p>
</li>
<li>
<p><strong>May 15 - May 23</strong></p>
<p>Implement the Libcurl wrapper in libcurl_wrapper.cc/h. This would include the various <a href="https://ec.haxx.se/libcurl-http-cookies.html#cookie-store-commands">abstractions</a> such as ‘ALL’, ‘SESS’, ‘FLUSH’, ‘RELOAD’ that could be used by octave’s script to use Libcurl cookies to connect to the server. There’s a <a href="https://github.com/octave-de/octave-web/blob/master/web/libcurl_wrapper.cc">preliminary implementation</a> for the same which was implemented by the mentor sometime back, which can be extended in this phase for a more general design.</p>
</li>
<li>
<p><strong>May 23 - May 31</strong></p>
<p>Add ‘wiki’ as an ‘output_format’ in publish() function. This would extend the implementation of private function <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/__publish_wiki_output__.m">__ publish_wiki_output__.m</a>
that is to be used to format a wiki published code.</p>
</li>
<li>
<p><strong>June 1 - June 10</strong></p>
<p>Non Coding period due to University major examinations.</p>
</li>
<li>
<p><strong>June 11 - June 15</strong></p>
<p><strong>First Phase Evaluation</strong> - Write Documentation of the work done upto this point and other tests such as setting up the wiki installation environment for testing the script that would be implemented in point 6. Catch up with the work if anything is lagging behind.</p>
</li>
<li>
<p><strong>June 15 - June 30</strong></p>
<p>Implement wiki_login.m which would use the Libcurl wrapper implemented in point 2. Currently <a href="https://github.com/octave-de/OctConf2017/blob/master/demo2/wiki_login.m">this</a> file uses Java’s interface to octave. But with the wrapper, the file would become an implementation of RESTful services for ‘code sharing’ and will not be dependent on Java’s interface to octave.</p>
</li>
<li>
<p><strong>July 1 - July 9</strong></p>
<p>Implement ‘weboptions’ function. It will return a default ‘weboptions’ object to specify parameters for a request to web service. This function will use the libCURL wrapper that would’ve been already implemented. Current scope of the function would be restricted to four options, viz., ‘Username’, ‘password’, ‘Keyname’, ‘KeyValue’.</p>
</li>
<li>
<p><strong>July 10 - July 13</strong></p>
<p><strong>Second Evaluation Phase</strong> - Write documentation of the work done upto here and other tests required for ‘weboptions’ function. Catch up with any previous work if left.</p>
</li>
<li>
<p><strong>July 14 - July 20</strong></p>
<p>Implement ‘webread’ function. This will read content from a web service and return data formatted as text. Other output arguments (cmap, alpha) will not be supported currently.</p>
</li>
<li>
<p><strong>July 21 - July 27</strong></p>
<p>Implement ‘webwrite’ function. This will put data in the body of an HTTP POST request to the web service.</p>
</li>
<li>
<p><strong>July 28 - August 6</strong></p>
<p>Buffer period for anything that remains. Complete documentation and testing for ‘webwrite’ and ‘webread’.</p>
</li>
</ol>
<p>For now, libcurl_wrapper.cc/h would be placed in libinterp/corefcn but could be changed whether it should be used by default or on the user’s choice because someone might not want to connect themselves with <a href="https://wiki.octave.org">wiki.octave.org</a>. I’ve for now, merged two weeks, i.e from 15 June to 30 June because the main task would be include the wrapper implementation for connecting to the webserver. This may span upto two weeks and so is the reason for two weeks of the same task.</p>
<h3 id="additional-things-to-be-done-after-gsoc-is-over">Additional Things to be done after GSoC is over:</h3>
<p>The web function is already partially implemented. The task will be to finish the implementation with various arguments such as ‘-notoolbar’, ‘-noaddressbox’, ‘-new’ for MATLAB’s compatibility. ‘websave’, ‘ftp’ and ‘sendmail’ that are a part of RESTful services, will also be implemented. Any other part of GNU octave which currently might need RESTful services using cookies can be amended to use the implementation that would result from the project. The ‘webread’ function will be extended to read values into JSON format and images as well.</p>
<hr />
<p>Thanks for reading, will be posting next update soon. Looking forward to a bug-free and code-some summer. :-)</p>Sahil Yadavhttps://me-ydv-5.github.io/Welcome to Octave and Google Summer of Codetag:eriveltongualter.github.io,2018-04-23:/GSoC2018/welcome-gsoc.html2018-04-23T07:46:00+00:00<p>This summer I got accepeted to the Summer Google of Code under the GNU Octave. This program, admistred by Google, facilates the emergence of students to the Open Source Community. My primary goal to participate in the GSoC is to build a long term relationship with the open source community …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Welcome to Octave and Google Summer of Codetag:None,2018-04-23:/welcome-gsoc.html2018-04-23T07:46:00+00:00<p>This summer I got accepeted to the Summer Google of Code under the GNU Octave. This program, admistred by Google, facilates the emergence of students to the Open Source Community. My primary goal to participate in the GSoC is to build a long term relationship with the open source community …</p>Erivelton Gualterhttps://eriveltongualter.github.io/GSoC2018/Advent of Dhttp://jordi.inversethought.com/?p=4292018-03-06T04:24:45+00:00<p>I wrote my <a href="https://adventofcode.com/">Advent of Code</a> in D. The programming language. It was the first time I used D in earnest every day for something substantial. It was fun and I learned things along the way, such as easy metaprogramming, concurrency I could write correctly, and functional programming that doesn’t feel like I have one arm tied behind my back. I would do it all over again.</p>
<p>My main programming languages are C++ and Python. For me, D is the combination of the best of these two: the power of C++ with the ease of use of Python. Or to put it another way, D is the C++ I always wanted. This used to be D’s sales pitch, down to its name. There’s lots of evident C++ heritage in D. It is a C++ successor worthy of consideration.</p>
<h1>Why D?</h1>
<p>This is the question people always ask me. Whenever I bring up D, I am faced with the following set of standard rebuttals:</p>
<ul>
<li>Why not Rust?
</li>
<li>D? That’s still around?</li>
<li>D doesn’t bring anything new or interesting</li>
<li>But the GC…</li>
</ul>
<p>I’ll answer these briefly: D was easier for me to learn than Rust, yes, it’s still around and very lively, it has lots of interesting ideas, and what garbage collector? I guess there’s a GC, but I’ve never noticed and it’s never gotten in my way.</p>
<p>I will let D speak for itself further below. For now, I would like to address the “why D?” rebuttals in a different way. It seems to me that people would rather not have to learn another new thing. Right now, Rust has a lot of attention and some of the code, and right now it seems like Rust may be the solution we always wanted for safe, systems-level coding. It takes effort to work on a new programming language. So, I think the “why D?” people are mostly saying, “why should <em>I</em> have to care about a different programming language, can’t I just immediately dismiss D and spend time learning Rust instead?”</p>
<p>I posit that no, you shouldn’t immediately dismiss D. If nothing else, try to listen to its ideas, many of which are distilled into Alexandrescu’s <a href="https://www.worldcat.org/title/d-programming-language/oclc/456837504">The D Programming Language</a>. I recommend this book as good reading material for computer science, even if you never plan to write any D (as a language reference itself, it’s already dated in a number of ways, but I still recommend it for the ideas it discusses). Also browse the <a href="https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs">D Gems</a> section in the D tour. In the meantime, let me show you what I learned about D while using it.</p>
<h1>Writing D every day for over 25 days</h1>
<p>I took slightly longer than 25 days to write my advent of code solutions, partly because some stumped me a little and partly because around actual Christmas I wanted to spend time with family instead of writing code. When I was writing code, I would say that nearly every day of advent of code forced me to look into a new aspect of D. You can see my solutions <a href="http://inversethought.com/hg/aoc">in this Mercurial repository.</a></p>
<p>I am not going to go too much into details about the abstract theory concerning the solution of each problem. Perhaps another time. I will instead focus on the specific D techniques I learned about or found most useful for each.</p>
<h1>Contents</h1>
<ol>
<li>
<a href="http://jordi.inversethought.com/feed/#day01">Day 1: parsing arguments, type conversions, template constraints</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day02">Day 2: functional programming and uniform function call syntax</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day03">Day 3: let’s try some complex arithmetic!</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day04">Day 4: reusing familiar tools to find duplicates</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day05">Day 5: more practice with familiar tools</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day06">Day 6: ranges</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day07">Day 7: structs and compile-time regexes</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day08">Day 8: more compile-time fun with mixin</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day09">Day 9: a switch statement!</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day10">Day 10: learning what ranges <em>cannot</em> do</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day11">Day 11: offline hex coding</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day12">Day 12: for want of a set</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day13">Day 13: more offline coding</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day14">Day 14: reusing older code as a module</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day15">Day 15: generators, lambdas, functions, and delegates</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day16">Day 16: permutations with primitive tools</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day17">Day 17: avoiding all the work with a clever observation</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day18">Day 18: concurrency I can finally understand and write correctly</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day19">Day 19: string parsing with enums and final switches</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day20">Day 20: a physics problem with vector operations</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day21">Day 21: an indexable, hashable, comparable struct</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day22">Day 22: more enums, final switches, and complex numbers</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day23">Day 23: another opcode parsing problem</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day24">Day 24: a routine graph-search problem</a>
</li>
<li>
<a href="http://jordi.inversethought.com/feed/#day25">Day 25: formatted reads to finish off Advent of D</a>
</li>
</ol>
<p><a name="day01"></a></p>
<h2>Day 1: parsing arguments, type conversions, template constraints</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day01/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day01/app.d">my solution</a>)</p>
<p>For Day 1, I was planning to be a bit more careful about everything around the code. I was going to carefully parse CLI arguments, produce docstrings and error messages when anything went wrong, and carefully validate template arguments with constraints (comparable to <a href="https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)">concepts in C++</a>). While I could have done all of this, as days went by I tried to <abbr title="Minimise program length">golf</abbr> my solutions, so I abandoned most of this boilerplate. Instead, I lazily relied on getting D stack traces at runtime or compiler errors when I messed up.</p>
<p>As you can see from my solution, had I kept it up, the boilerplate isn’t <em>too</em> bad, though. Template constraints are achieved by adding <code class="codecolorer d solarized-light"><span class="d"><span>if</span><span>(</span>isNumeric<span>!</span>numType<span>)</span></span></code>, which checks at compile time that my template was given a template argument of the correct type, where <code class="codecolorer d solarized-light"><span class="d">isNumeric</span></code> comes from <code class="codecolorer d solarized-light"><span class="d"><span>import</span> std.<span>traits</span></span></code>. I also found that <code class="codecolorer d solarized-light"><span class="d">getopt</span></code> was a sufficiently mature standard library for handling command-line parsing. It’s not quite as rich as Python’s <code class="codecolorer d solarized-light"><span class="d">argparse</span></code>, merely sufficient. This about shows all it can do:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>string</span> input<span>;</span><br />
<span>auto</span> opts <span>=</span> getopt<span>(</span><br />
args<span>,</span><br />
<span>"input|i"</span><span>,</span> <span>"Input captcha to process"</span><span>,</span> <span>&</span>input<br />
<span>)</span><span>;</span><br />
<br />
<span>if</span> <span>(</span>opts.<span>helpWanted</span><span>)</span> <span>{</span><br />
defaultGetoptPrinter<span>(</span><span>"Day 1 of AoC"</span><span>,</span> opts.<span>options</span><span>)</span><span>;</span><br />
<span>}</span></div></div>
<p>Finally, a frequent workhorse that appeared from Day 1 was <code class="codecolorer d solarized-light"><span class="d">std.<span>conv</span></span></code> for parsing strings into numbers. A single function, <code class="codecolorer d solarized-light"><span class="d">to</span></code> is surprisingly versatile and does much more than that, by taking a single template argument for converting (not casting) one type into another. It knows not only how to parse strings into numbers and vice versa, but also how to convert numerical types keeping as much precision as possible or reading list or <abbr title="dict">associative array</abbr> literals from strings if they are in their standard string representation. It’s a good basic example of D’s power and flexibility in generic programming.</p>
<p><a name="day02"></a></p>
<h2>Day 2: functional programming and uniform function call syntax</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day02/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day02/app.d">my solution</a>)</p>
<p>For whatever reason, probably because I was kind of trying to golf my solutions, I ended up writing a lot of functionalish code, with lots of map, reduce, filter, and so forth. This started early on with Day 2. D is mostly unopinionated about which style of programming one should use and offers tools to do object-orientation, functional programming, or just plain procedural programming, presenting no obstacle to the mixing these styles. Lambdas are easily written inline with concise syntax, e.g. <code class="codecolorer d solarized-light"><span class="d">x <span>=></span> x<span>*</span>x</span></code>, and the basic standard functional tools like <code class="codecolorer d solarized-light"><span class="d">map<span>,</span> reduce<span>,</span> filter</span></code> and so on are available.</p>
<p>D’s approach to functional programming is quite pragmatic. While I rarely used it, because I wasn’t being too careful for these solutions, D functions can be labelled <a href="https://dlang.org/spec/function.html#pure-functions"><code class="codecolorer d solarized-light"><span class="d"><span>pure</span></span></code></a>, which means that they can have no side effects. However, this still lets them do local impure things such as reassigning a variable or having a for loop. The only restriction is that all of their impurity must be “on the stack”, and that they cannot call any impure functions themselves.</p>
<p>Another feature that I came to completely fall in love with was what they call <a href="https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs">uniform function call syntax</a> (UFCS). With some caveats, this basically means that</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> foo.<span>bar</span><span>(</span>baz<span>)</span></div></div>
<p>is just sugar for</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> bar<span>(</span>foo<span>,</span> baz<span>)</span></div></div>
<p>If the function only has one argument, the round brackets are optional and <code class="codecolorer text solarized-light"><span class="text">foo.bar</span></code> is sugar for <code class="codecolorer text solarized-light"><span class="text">bar(foo)</span></code>. This very basic syntactic convenience makes it so easy and pleasant to chain function calls together, lending itself to making it more inviting to write functional code. It also is a happy unification between OOP and FP, because syntactically it’s the same to give an object a new member function as it is to create a free-standing function whose first argument is the object.</p>
<p><a name="day03"></a></p>
<h2>Day 3: let’s try some complex arithmetic!</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day03/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day03/app.d">my solution</a>)</p>
<p>For me, 2-dimensional geometry is often very well described by complex numbers. The spiral in the problem here seemed easy to describe as an <abbr title="dict">associative array</abbr> from complex coordinates to integer values. So, I decided to give D’s <code class="codecolorer text solarized-light"><span class="text">std.complex</span></code> a try. It was easy to use and there were no big surprises here.</p>
<p><a name="day04"></a></p>
<h2>Day 4: reusing familiar tools to find duplicates</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day04/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day04/app.d">my solution</a>)</p>
<p>There weren’t any new D techniques here, but it was nice to see how easy it was to build a simple word counter from D builtins. Slightly disappointed that this data structure itself wasn’t builtin like Python’s own <code class="codecolorer text solarized-light"><span class="text">collections.Counter</span></code> but hardly an insurmountable problem.</p>
<p><a name="day05"></a></p>
<h2>Day 5: more practice with familiar tools</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day05/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day05/app.d">my solution</a>)</p>
<p>Again, not much new D here. I like the relative ease with which it’s possible to read integers into a list using <code class="codecolorer text solarized-light"><span class="text">map</span></code> and <code class="codecolorer text solarized-light"><span class="text">std.conv.to</span></code>.</p>
<p><a name="day06"></a></p>
<h2>Day 6: ranges</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day06/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day06/app.d">my solution</a>)</p>
<p>There’s usually a fundamental paradigm or structure in programming languages out of which everything else depends on. Haskell has functions and monads, C has pointers and arrays, C++ has classes and templates, Python has dicts and iterators, Javascript has callbacks and objects, Rust has borrowing and immutability. Ranges are one of D’s fundamental concepts. Roughly speaking, a range is anything that can be iterated over, like an array or a lazy generator. Thanks to D’s powerful metaprogramming, ranges can be defined to satisfy a kind of compile-time duck typing: if it has methods to check for emptiness, get the first element, and get the next element, then it’s an <code class="codecolorer text solarized-light"><span class="text">InputRange</span></code>. This duck typing is kind of reminiscent of type classes in Haskell. D’s general principle of having containers and algorithms on those containers is built upon the range concept. Ranges are intended to be simpler reformulation of iterators from the C++ standard libary.</p>
<p>I have been using ranges all along, as <code class="codecolorer text solarized-light"><span class="text">foreach</span></code> loops are kind of like sugar for invoking those methods on ranges. However, for day 6 I actually wanted to use a method that had to invoke an <code class="codecolorer text solarized-light"><span class="text">std.range</span></code> method, <code class="codecolorer text solarized-light"><span class="text">enumerate</span></code>. It simply iterates over a range while simultaneously producing a counter. This I used to write some brief code to obtain both the maximum of an array and the index in which it occurs.</p>
<p>Another range-related feature that appears for the first time here is slicing. Certain random-access ranges which allow integer indexing also allow slicing. The typical method to remove elements from an array is to use this slicing. For example, to remove the first five elements and the last two elements from an array:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> arr <span>=</span> arr<span>[</span><span>5</span><span>..</span>$<span>-</span><span>2</span><span>]</span><span>;</span></div></div>
<p>Here the dollar sign is sugar for <code class="codecolorer text solarized-light"><span class="text">arr.length</span></code> and this removal is simply done by moving some start and end pointers in memory — no other bytes are touched.</p>
<p>The D Tour has <a href="https://tour.dlang.org/tour/en/basics/ranges">a good taste of ranges</a> and <em>Programming in D</em> goes into <a href="http://ddili.org/ders/d.en/ranges.html">more depth</a>.</p>
<p><a name="day07"></a></p>
<h2>Day 7: structs and compile-time regexes</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day07/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day07/app.d">my solution</a>)</p>
<p>My solution for this problem was more complicated, and it forced me to break out an actual tree data structure. Because I wasn’t trying to be particularly parsimonious about memory usage or execution speed, I decided to create the tree by having a node struct with a global associative array indexing all of the nodes.</p>
<p>In D, structs have value semantics and classes have reference semantics. Roughly, this means that structs are on the stack, they get copied around when being passed into functions, while classes are always handled by reference instead and dynamically allocated and destroyed. Another difference between structs and classes is that classes have inheritance (and hence, polymorphic dispatch) but structs don’t. However, you can give structs methods, and they will have an implicit <code class="codecolorer d solarized-light"><span class="d"><span>this</span></span></code> parameter, although this is little more than sugar for free-standing functions.</p>
<p>Enough on OOP. Let’s talk about the really exciting stuff: compile-time regular expressions!</p>
<p>For this problem, there was some input parsing to do. Let’s look at what I wrote:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>void</span> parseLine<span>(</span><span>string</span> line<span>)</span> <span>{</span> <br />
<span>static</span> nodeRegex <span>=</span> regex<span>(</span><span>r"(?P<name>\w+) \((?P<weight>\d+)\)( -> (?P<children>[\w,]+))?"</span><span>)</span><span>;</span> <br />
<span>auto</span> row <span>=</span> matchFirst<span>(</span>line<span>,</span> nodeRegex<span>)</span><span>;</span> <br />
<span>// init the node struct here </span><br />
<span>}</span></div></div>
<p>The <code class="codecolorer text solarized-light"><span class="text">static</span></code> keyword instructs D that this variable has to be computed at compile-time. D’s compiler basically has its own interpreter that can execute arbitrary code as long as all of the inputs are available at compile time. In this case, this parses and compiles this regex into the binary. The next line, where I call <code class="codecolorer text solarized-light"><span class="text">matchFirst</span></code> on each line, is done at runtime, but if for whatever reason I had these strings available at compile time (say, defined as a big inline string a few lines above the same source file), I could also do the regex parsing at compile time if I wanted to.</p>
<p>This is really nice. This is one of my favourite D features. Add a <code class="codecolorer text solarized-light"><span class="text">static</span></code> and you can precompute into your binary just about anything. You often don’t even need any extra syntax. If the compiler realises that it has all of the information at compile time to do something, it might just do it. This is known as compile-time function execution, hereafter, CTFE. The D Tour has <a href="https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe">a good overview of the topic</a>.</p>
<p><a name="day08"></a></p>
<h2>Day 8: more compile-time fun with mixin</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day08/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day08/app.d">my solution</a>)</p>
<p>Day 8 was another problem where the most interesting part was parsing. As before, I used a compile-time regex. But the interesting part of this problem was the following bit of code for parsing strings into their corresponding D comparison operation, as I originally wrote it:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> comparisons <span>=</span> <span>[</span><br />
<span>"<"</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span><</span> b<span>,</span> <br />
<span>">"</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span>></span> b<span>,</span><br />
<span>"=="</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span>==</span> b<span>,</span><br />
<span>"<="</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span><=</span> b<span>,</span><br />
<span>">="</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span>>=</span> b<span>,</span><br />
<span>"!="</span><span>:</span> <span>function</span><span>(</span><span>int</span> a<span>,</span> <span>int</span> b<span>)</span> <span>=></span> a <span>!=</span> b<span>,</span> <br />
<span>]</span><span>;</span></div></div>
<p>Okay, this isn’t terrible. It’s just… not very pretty. I don’t like that it’s basically the same line repeated six times. I furthermore also don’t like that within each line, I have to repeat the operator in the string part and in the function body. Enter the <code class="codecolorer text solarized-light"><span class="text">mixin</span></code> keyword! Basically, string mixins allow you to evaluate any string at compile time. They’re kind of like the C preprocessor, but much safer. For example, string mixins only evaluate complete expressions, so no shenanigans like <code class="codecolorer cpp solarized-light"><span class="cpp"><span>#define private public</span></span></code> are allowed. My first attempt to shorten the above looked like this:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>bool</span> <span>function</span><span>(</span><span>int</span><span>,</span><span>int</span><span>)</span><span>[</span><span>string</span><span>]</span> comparisons<span>;</span> <br />
<span>static</span> <span>foreach</span><span>(</span>cmp<span>;</span> <span>[</span><span>"<"</span><span>,</span> <span>">"</span><span>,</span> <span>"=="</span><span>,</span> <span>"<="</span><span>,</span> <span>">="</span><span>,</span> <span>"!="</span><span>]</span><span>)</span> <span>{</span> <br />
comparisons<span>[</span>cmp<span>]</span> <span>=</span> <span>mixin</span><span>(</span><span>"function(int a, int b) => a "</span><span>~</span>cmp<span>~</span><span>" b"</span><span>)</span><span>;</span> <br />
<span>}</span></div></div>
<p>Since I decided to use a compile-time static loop to populate my array, I now needed a separate declaration of the variable which forced me to spell out its ungainly type: an associative array that takes a <code class="codecolorer d solarized-light"><span class="d"><span>string</span></span></code> and returns a function with that signature. The mixin here takes a concatenated string that evaluates to a function.</p>
<p>However, this didn’t work for two reasons!</p>
<p>The first one is that <code class="codecolorer d solarized-light"><span class="d"><span>static</span> <span>foreach</span></span></code> was <a href="https://dlang.org/blog/2017/09/01/dmd-2-076-0-released/">introduced on September 2017</a>. The D compilers packaged in Debian didn’t have it yet when I wrote that code! The second problem is more subtle: initialisation of associative arrays currently cannot be statically done because their internal data structures rely on runtime computations, according to my understanding of <a href="http://jordi.inversethought.com//forum.dlang.org/thread/CANHcaG=vACzvcD=xSXaiCNe4FiUJAW7VPRQaPoxEh2-su9+GNg@mail.gmail
.com">this discussion</a>. They might fix it some day?</p>
<p>So, next best thing is my final answer:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>bool</span> <span>function</span><span>(</span><span>int</span><span>,</span><span>int</span><span>)</span><span>[</span><span>string</span><span>]</span> comparisons<span>;</span><br />
<br />
<span>auto</span> getComparisons<span>(</span>Args<span>...</span><span>)</span><span>(</span><span>)</span> <span>{</span> <br />
<span>foreach</span><span>(</span>cmp<span>;</span> Args<span>)</span> <span>{</span> <br />
comparisons<span>[</span>cmp<span>]</span> <span>=</span> <span>mixin</span><span>(</span><span>"function(int a, int b) => a "</span><span>~</span>cmp<span>~</span><span>" b"</span><span>)</span><span>;</span> <br />
<span>}</span> <br />
<span>return</span> comparisons<span>;</span> <br />
<span>}</span><br />
<br />
<span>shared</span> <span>static</span> <span>this</span><span>(</span><span>)</span> <span>{</span> <br />
comparisons <span>=</span> getComparisons<span>!</span><span>(</span><span>"<"</span><span>,</span> <span>">"</span><span>,</span> <span>"=="</span><span>,</span> <span>"<="</span><span>,</span> <span>">="</span><span>,</span> <span>"!="</span><span>)</span><span>;</span> <br />
<span>}</span></div></div>
<p>Alright, by size this is hardly shorter than the repetitive original. But I still think it’s better! It has no dull repetition where bugs are most often introduced, and it’s using a variable-argument templated function so that the mixin can have its values available at compile time. It uses the next best thing to compile-time initialisation, which is a module initialiser <code class="codecolorer d solarized-light"><span class="d"><span>shared</span> <span>static</span> <span>this</span><span>(</span><span>)</span></span></code> that just calls the function to perform the init.</p>
<p><a name="day09"></a></p>
<h2>Day 9: a switch statement!</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day09/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day09/app.d">my solution</a>)</p>
<p>Day 9 was a simpler parsing problem, so simple that instead of using a regex I decided to just use a switch statement. There isn’t anything terribly fancy about switch statements, and they work almost exactly the same as they do in other languages. The only distinct features of switch statements in D is that they work on numeric, string, or bool types and that they have deprecated implicit fallthrough. Fallthrough instead must be explicitly done with <code class="codecolorer d solarized-light"><span class="d"><span>goto</span> <span>case</span><span>;</span></span></code> or will be once the deprecation is complete.</p>
<p>Oh, and you can also specify ranges for a case statement, e.g.</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>case</span> <span>'a'</span><span>:</span> <span>..</span> <span>case</span> <span>'z'</span><span>:</span><br />
<span>// do stuff with lowercase ASCII</span><br />
<span>break</span><span>;</span></div></div>
<p>It’s the small conveniences that make this pleasant. <em>Programming in D</em> has <a href="http://ddili.org/ders/d.en/switch_case.html">a good discussion</a> on switch statements.</p>
<p><a name="day10"></a></p>
<h2>Day 10: learning what ranges <em>cannot</em> do</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day10/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day10/app.d">my solution</a>)</p>
<p>So, superficially, you might think that expressions like <code class="codecolorer d solarized-light"><span class="d">arr<span>[</span><span>2</span><span>..</span>$<span>-</span><span>2</span><span>]</span></span></code>, which is valid, would also allow for things like <code class="codecolorer d solarized-light"><span class="d">arr<span>[</span>$<span>-</span><span>2</span><span>..</span><span>1</span><span>]</span></span></code> to traverse the array in reverse order or some other syntax for having a different step size than +1. At least I did. These kinds of array indexing are common in numeric-based arrays such as Octave, Julia, R, or Python’s numpy. So for day 10’s hash, which requires reversing an array, I thought I could just do that.</p>
<p>Turns out that the language doesn’t have syntax to allow this, but after <a href="https://dlang.org/phobos/std_algorithm_mutation.html#.reverse">a quick trip to the standard library</a> I found the necessary functions. What I thought could be written as</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer">arr<span>[</span>a<span>..</span><span>b</span><span>]</span> <span>=</span> arr<span>[</span>b<span>..</span><span>a</span><span>]</span><span>;</span></div></div>
<p>instead became</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer">reverse<span>(</span>arr<span>[</span>a<span>..</span><span>b</span><span>]</span><span>)</span><span>;</span></div></div>
<p>Other than this minor discovery about ranges, Day 10 was more about getting the algorithm right than using any specialised D utilities. Since real hashes typically allow several sizes, I templated the hash functions with the total size, rounds of hashing, and chunk size, with a template constraint that the chunk size must divide the total size:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> getHash<span>(</span><span>int</span> Size<span>=</span><span>256</span><span>,</span> <span>int</span> Rounds<span>=</span><span>64</span><span>,</span> <span>int</span> ChunkSize<span>=</span><span>16</span><span>)</span><span>(</span><span>string</span> input<span>)</span> <br />
<span>if</span><span>(</span> Size <span>%</span> ChunkSize <span>==</span> <span>0</span><span>)</span> <br />
<span>{</span> <br />
<span>// ... </span><br />
<span>}</span></div></div>
<p>Nothing new here. I just like that template constraints are so easy to write.</p>
<p><a name="day11"></a></p>
<h2>Day 11: offline hex coding</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day11/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day11/app.d">my solution</a>)</p>
<p>I did most of Day 11 on paper. It took me a while to figure out a proper hex coordinate system and what the distance function in that coordinate system should be. I had seen hex coordinates from playing <a href="http://wesnoth.org/">Battle for Wesnoth</a>, but took me a while to figure them out again. Once I had that, the actual D code is pretty simple and used no techniques I hadn’t seen before. I think this is the first time I used the <code class="codecolorer d solarized-light"><span class="d">cumulativeFold</span></code> function, but other than that, nothing to see here. An immutable global <abbr title="dict">associative array</abbr> populated at module init time like before,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>pure</span> <span>static</span> <span>this</span><span>(</span><span>)</span><span>{</span> <br />
directions <span>=</span> <span>[</span><br />
<span>"ne"</span><span>:</span> <span>[</span><span>1</span><span>,</span><span>1</span><span>]</span><span>,</span><br />
<span>"n"</span><span>:</span> <span>[</span><span>0</span><span>,</span><span>1</span><span>]</span><span>,</span><br />
<span>"nw"</span><span>:</span> <span>[</span><span>-</span><span>1</span><span>,</span><span>0</span><span>]</span><span>,</span><br />
<span>"sw"</span><span>:</span> <span>[</span><span>-</span><span>1</span><span>,-</span><span>1</span><span>]</span><span>,</span><br />
<span>"s"</span><span>:</span> <span>[</span><span>0</span><span>,-</span><span>1</span><span>]</span><span>,</span><br />
<span>"se"</span><span>:</span> <span>[</span><span>1</span><span>,</span><span>0</span><span>]</span><span>,</span><br />
<span>]</span><span>;</span> <br />
<span>}</span></div></div>
<p>and that’s it.</p>
<p><a name="day12"></a></p>
<h2>Day 12: for want of a set</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day12/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day12/app.d">my solution</a>)</p>
<p>The only new D technique for this problem was that I decided to use a set structure to keep track of which graph nodes had been visited. The only problem is that D doesn’t have a built-in set structure (yet?), but it does have a <code class="codecolorer d solarized-light"><span class="d">setDifference</span></code> function. It’s a bit clunky. It only works on ordered arrays, but that was sufficient for my purpose here, and probably not much worse than hashing with a traditional set structure would have been.</p>
<p>One further observation: D has an <code class="codecolorer d solarized-light"><span class="d"><span>in</span></span></code> keyword, which can be used to test membership, like in Python (it also has an unrelated use for defining input and output arguments to functions), but unlike Python, only for associative arrays. This makes sense, because the complexity of testing for membership for other data structures can vary widely depending on the structure and the chosen algorithm, and there isn’t a clear universal choice like there is for associative arrays.</p>
<p>If desired, however, it’s possible to define the <code class="codecolorer d solarized-light"><span class="d"><span>in</span></span></code> operator for any other class, like so:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>bool</span> opBinaryRight<span>!</span><span>(</span><span>"in"</span><span>)</span><span>(</span>T elt<span>)</span> <span>{</span> <br />
<span>// check that elt is in `this` </span><br />
<span>}</span></div></div>
<p>I would assume that’s what you could use to write a set class for D.</p>
<p><a name="day13"></a></p>
<h2>Day 13: more offline coding</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day13/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day13/app.d">my solution</a>)</p>
<p>This one is another where I did most of the solution on paper and thus managed to write a very short program. No new D techniques here, just the usual functionalish style that I seem to be developing.</p>
<p><a name="day14"></a></p>
<h2>Day 14: reusing older code as a module</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day14/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day14/app.d">my solution</a>)</p>
<p>The problem here is interesting because I’ve solved this labelling of connected components problem before in C++ for GNU Octave. I wrote <a href="https://sourceforge.net/p/octave/image/ci/1b8b83d01211/tree/src/bwlabeln.cc#l370">the initial bwlabeln implementation</a> using <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">union-find</a>. I was tempted to do the same here, but I couldn’t think of a quick way to do so, and talking to others in the #lobsters channel in IRC, I realised that a simpler recursive solution would work without overflowing the stack (because the problem is small enough, not because a stack-based algorithm is clever).</p>
<p>The interesting part is reusing an earlier solution, the hashing algorithm from <a href="http://jordi.inversethought.com/feed/#day10">Day 10</a>. At first blush, this is quite simple: every D file also creates its own module, namespaced if desired by directories. It’s very reminiscent of Python’s <code class="codecolorer python solarized-light"><span class="python"><span>import</span></span></code> statement and module namespacing. The only snag is that my other file has a <code class="codecolorer d solarized-light"><span class="d"><span>void</span> main<span>(</span><span>string</span><span>[</span><span>]</span> args<span>)</span></span></code> function and so does this one. The linker won’t like that duplicate definition of symbols. For this purpose, D offers conditional compilation, which in C and C++ is usually achieved via <a href="http://www.c4learn.com/c-programming/c-ifdef-conditional-compilation-directives/">a familiar C preprocessor macro idiom.</a></p>
<p>In D, this idiom is codified into the language proper via the <code class="codecolorer d solarized-light"><span class="d"><span>version</span></span></code> kewyord, like so</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>version</span><span>(</span>standalone<span>)</span> <span>{</span><br />
<span>void</span> main<span>(</span><span>string</span><span>[</span><span>]</span> args<span>)</span><span>{</span> <br />
<span>// do main things here </span><br />
<span>}</span> <br />
<span>}</span></div></div>
<p>This instructs the compiler to compile the inside of the <code class="codecolorer d solarized-light"><span class="d"><span>version</span></span></code> block only if an option called “standalone” is passed in,</p>
<div class="codecolorer-container bash solarized-light"><div class="bash codecolorer">gdc <span>-O2</span> <span>-fversion</span>=standalone app.d <span>-o</span> day10</div></div>
<p>or, with regrettably slightly different flags,</p>
<div class="codecolorer-container bash solarized-light"><div class="bash codecolorer">ldc2 <span>-O2</span> <span>-d-version</span>=standalone app.d <span>-of</span> day10</div></div>
<p>There are other built-in arguments for <code class="codecolorer d solarized-light"><span class="d"><span>version</span></span></code>, <a href="https://dlang.org/spec/version.html#predefined-versions">such as “linux” or “OSX” to conditionally compile for a particular operating system</a>. This keyword offers quite a bit of flexibility for conditional compilation, and it’s a big improvement over C preprocessor idioms.</p>
<p><a name="day15"></a></p>
<h2>Day 15: generators, lambdas, functions, and delegates</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day15/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day15/app.d">my solution</a>)</p>
<p>This problem was an opportunity to test out a new function, <code class="codecolorer d solarized-light"><span class="d">generate</span></code>, which takes a function and iterates it repeatedly on a range. Haskell calls this one <code class="codecolorer haskell solarized-light"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate"><span>iterate</span></a></span></code>, which I think is a better name. It’s also a lazy generator, so you need something like <code class="codecolorer d solarized-light"><span class="d">take</span></code> to say how much of the generator do you want to use. For example, the Haskell code</p>
<div class="codecolorer-container haskell solarized-light"><div class="haskell codecolorer">pows <span>=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take"><span>take</span></a> <span>11</span> <span>$</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate"><span>iterate</span></a> <span>(</span><span>\</span>x <span>-></span> x<span>*</span><span>2</span><span>)</span> <span>1</span></div></div>
<p>can be translated into D as</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> x <span>=</span> <span>1</span><span>;</span><br />
<span>auto</span> pows <span>=</span> generate<span>!</span><span>(</span>x <span>=></span> x<span>*</span><span>2</span><span>)</span>.<span>take</span><span>(</span><span>11</span><span>)</span><span>;</span></div></div>
<p>There are <a href="https://dlang.org/phobos/std_range.html#generate">other examples in the documentation</a>.</p>
<p>Let also take a moment here to talk about the different anonymous functions in D. The following both declare a function that squares its input:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>function</span><span>(</span><span>int</span> a<span>)</span> <span>{</span> <span>return</span> a<span>^^</span><span>2</span><span>;</span><span>}</span><br />
<span>delegate</span><span>(</span><span>int</span> a<span>)</span> <span>{</span> <span>return</span> a<span>^^</span><span>2</span><span>;</span><span>}</span></div></div>
<p>The difference is just a question of closure. The <code class="codecolorer d solarized-light"><span class="d"><span>delegate</span></span></code> version carries a hidden pointer to its enclosing scope, so it can dynamically close over the outer scope variables. If you can’t afford to pay this runtime penalty, the <code class="codecolorer d solarized-light"><span class="d"><span>function</span></span></code> version doesn’t reference the enclosing scope (no extra pointer). So, for a generator, you typically want to use a <code class="codecolorer d solarized-light"><span class="d"><span>delegate</span></span></code>, since you want the generator to remember its scoped variables across successive calls, like what I did:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> generator<span>(</span><span>ulong</span> val<span>,</span> <span>ulong</span> mult<span>)</span> <span>{</span><br />
<span>return</span> generate<span>!</span><span>(</span><span>delegate</span><span>(</span><span>)</span><span>{</span><br />
val <span>=</span> <span>(</span>val <span>*</span> mult<span>)</span> <span>%</span> <span>2147483647</span><span>;</span> <br />
<span>return</span> val<span>;</span><br />
<span>}</span><br />
<span>)</span><span>;</span><br />
<span>}</span></div></div>
<p>This function returns a generator range where each entry will result in a new entry of this <a href="https://en.wikipedia.org/wiki/Linear_congruential_generator">pseudorandom linear congruence generator</a>.</p>
<p>The <code class="codecolorer d solarized-light"><span class="d"><span>delegate</span></span></code>/<code class="codecolorer d solarized-light"><span class="d"><span>function</span></span></code> is part of the type, and can be omitted if it can be inferred by context (e.g. when passing a function into another function as an argument). Furthermore, there’s a lambda shorthand that I have been using all along, where the <code class="codecolorer d solarized-light"><span class="d"><span>{</span><span>return</span> foo<span>;</span><span>}</span></span></code> boilerplate can be shortened to just <code class="codecolorer d solarized-light"><span class="d"><span>=></span></span></code> like so:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>(</span>a<span>)</span> <span>=></span> a<span>^^</span><span>2</span></div></div>
<p>This form is only valid where there’s enough context to infer if it’s a <code class="codecolorer d solarized-light"><span class="d"><span>delegate</span></span></code> or a <code class="codecolorer d solarized-light"><span class="d"><span>function</span></span></code>, as well as the type of <code class="codecolorer d solarized-light"><span class="d">a</span></code> itself. More details in <a href="https://dlang.org/spec/expression.html#function_literals">the language spec</a>.</p>
<p><a name="day16"></a></p>
<h2>Day 16: permutations with primitive tools</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day16/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day16/app.d">my solution</a>)</p>
<p>This permutations problem made me reach for the <code class="codecolorer d solarized-light"><span class="d">std.<span>algorithm</span></span></code> function <code class="codecolorer d solarized-light"><span class="d">bringToFront</span></code> for cyclicly permuting an array in place like so,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> bringToFront<span>(</span>progs<span>[</span>rot<span>..</span>$<span>]</span><span>,</span> progs<span>[</span><span>0</span><span>..</span><span>rot</span><span>]</span><span>)</span><span>;</span></div></div>
<p>It’s a surprisingly versatile function that can be used to perform more tricks than cyclic permutations. Its documentation is <a href="https://dlang.org/phobos/std_algorithm_mutation.html#.bringToFront">worth a read</a>.</p>
<p>I also ran into <a href="https://issues.dlang.org/show_bug.cgi?id=16959">a D bug here</a>. I had to create a character array from an immutable input string, but due to Unicode-related reasons that D has for handling characters especially, I had to cast to <code class="codecolorer d solarized-light"><span class="d"><span>ubyte</span><span>[</span><span>]</span></span></code> instead of <code class="codecolorer d solarized-light"><span class="d"><span>char</span><span>[</span><span>]</span></span></code>.</p>
<p>Besides that, for the second part where you had to realise that permutations cannot have too big of an <a href="https://en.wikipedia.org/wiki/Group_action#Orbits_and_stabilizers">orbit</a>, I also ended up using a string array with the <code class="codecolorer d solarized-light"><span class="d">canFind</span></code> from <code class="codecolorer d solarized-light"><span class="d">std.<span>algorithm</span></span></code>. I would have preferred a string set with hashing instead of linear searching, but it didn’t make a huge difference for the size of this problem.</p>
<p>I really want sets in the D standard library. Maybe I should see what I can do to make them happen.</p>
<p><a name="day17"></a></p>
<h2>Day 17: avoiding all the work with a clever observation</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day17/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day17/app.d">my solution</a>)</p>
<p>This puzzle is a variation of the <a href="https://en.wikipedia.org/wiki/Josephus_problem">Josephus problem</a>. I needed some help from #lobsters in IRC to figure out how to solve it. There aren’t any new D techniques, just some dumb array concatenation with the tilde operator for inserting elements into an array:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer">circ <span>=</span> circ<span>[</span><span>0</span><span>..</span><span>pos</span><span>]</span> <span>~</span> y <span>~</span> circ<span>[</span>pos<span>..</span>$<span>]</span><span>;</span></div></div>
<p>The second part can be solved via the simple observation that you only<br />
need to track one position, the one immediately following zero.</p>
<p><a name="day18"></a></p>
<h2>Day 18: concurrency I can finally understand and write correctly</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day18/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day18/app.d">my solution</a>)</p>
<p>This day was an exciting one for me. Discussing this problem with others, it seems many people had much difficulty solving this problem in other programming languages. Most people seemed to have to emulate their own concurrency with no help from their programming language of choice. In contrast, D absolutely shined here, because it is based on <a href="https://en.wikipedia.org/wiki/Actor_model">the actor concurrency model (message passing)</a>, which precisely fits the problem statement. (There are other concurrency primitives in case the actor concurrency model isn’t sufficient, but it was sufficient for me.)</p>
<p>The basic idea of concurrency in D is that each thread of execution localises all of its state. By default, threads share no data. In order to communicate with each other, threads pass messages. A thread can indicate at any time when it’s ready to send or receive messages. Messages can be any type, and each thread says what type it’s expecting to receive. If a thread receives a type it’s not prepared to handle, it will throw an exception.</p>
<p>There are more details, such as what happens if a thread receives too many messages but doesn’t respond to any of them. Let’s not go into that now. Basic idea: threads get spawned, threads send and receive messages.</p>
<p>Let’s spend a little bit of time looking at the relevant functions and types I used, all defined in <code class="codecolorer d solarized-light"><span class="d">std.<span>concurrency</span></span></code></p>
<dl>
<dt>spawn</dt>
<dd>
Starts a thread of execution. The first argument is a reference to the function that this thread will execute, along with any arguments that function may take. Returns a <code class="codecolorer d solarized-light"><span class="d">Tid</span></code> type, a thread ID. This is used as the address to send messages to. A thread can refer to its parent thread via the special variable <code class="codecolorer d solarized-light"><span class="d">ownerTid</span></code>.</dd></dl>
<p> Unless explicitly declared <code class="codecolorer d solarized-light"><span class="d"><span>shared</span></span></code>, the arguments of a threaded function must be immutable. This is how the compiler guarantees no race conditions when manipulating those variables. Of course, with <code class="codecolorer d solarized-light"><span class="d"><span>shared</span></span></code> variables, the programmer is signalling that they are taking over synchronisation of that data, which may require using low-level mutexes.
</p>
<dt>send</dt>
<dd>Send a message to a particular thread. The first argument is the thread id. The other arguments can be anything. It’s up to the receiving thread to handle the arguments it receives.</dd>
<dt>receiveOnly</dt>
<dd>Indicate that this thread is ready to receive a single type, and returns the value of that type. The type must of course be specified as a compile-time argument.</dd>
<dt>receive</dt>
<dd>Indicates what to do with any of several possible types. The arguments to this function are a collection of functions, whose parameters types will be dynamically type-matched with received types. I didn’t need this function, but I wanted to mention it exists.</dd>
<dt>receiveTimeout</dt>
<dd>The problem statement is designed to deadlock. Although there probably is a more elegant solution, timing out on deadlock was the solution I wrote. This function does just that: listens for a message for a set of amount of time. If a message is received in the designated time, its handler function is executed and <code class="codecolorer d solarized-light"><span class="d">receiveTimeout</span></code> returns <code class="codecolorer d solarized-light"><span class="d"><span>true</span></span></code>. If the timeout happens, it returns <code class="codecolorer d solarized-light"><span class="d"><span>false</span></span></code> instead.</dd>
<p>Armed with these tools, the solution was a breeze to write. I first spawn two threads and save their thread ids,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>auto</span> tid1 <span>=</span> spawn<span>(</span><span>&</span>runProgram<span>,</span> opcodes<span>,</span> <span>0</span><span>)</span><span>;</span><br />
<span>auto</span> tid2 <span>=</span> spawn<span>(</span><span>&</span>runProgram<span>,</span> opcodes<span>,</span> <span>1</span><span>)</span><span>;</span></div></div>
<p>Each of the two threads defined by <code class="codecolorer d solarized-light"><span class="d">runProgram</span></code> then immediately stops, waiting for a thread id, to know whom to talk to,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>void</span> runProgram<span>(</span><span>immutable</span> <span>string</span><span>[</span><span>]</span> opcodes<span>,</span> <span>ulong</span> pid<span>)</span> <span>{</span><br />
<span>auto</span> otherProg <span>=</span> receiveOnly<span>!</span>Tid<span>(</span><span>)</span><span>;</span><br />
<span>// ...</span><br />
<span>}</span></div></div>
<p>The parent thread then connects the two worker threads to each other,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> send<span>(</span>tid1<span>,</span> tid2<span>)</span><span>;</span><br />
send<span>(</span>tid2<span>,</span> tid1<span>)</span><span>;</span></div></div>
<p>And off they go, the two threads run through the opcodes in the problem statement, and eventually they deadlock, which I decided to handle with a timeout like so,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>case</span> <span>"rcv"</span><span>:</span><br />
<span>if</span> <span>(</span><span>!</span>receiveTimeout<span>(</span><span>100</span>.<span>msecs</span><span>,</span> <span>(</span><span>long</span> val<span>)</span> <span>{</span>regs<span>[</span>reg<span>]</span> <span>=</span> val<span>;</span><span>}</span><span>)</span><span>)</span> <span>{</span><br />
<span>goto</span> done<span>;</span><br />
<span>}</span><br />
<span>// no timeout, handle next opcode</span><br />
<span>goto</span> <span>default</span><span>;</span></div></div>
<p>After the thread has timed out, it signals to the parent thread that it’s done,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> done<span>:</span><br />
send<span>(</span>ownerTid<span>,</span> thisTid<span>,</span> sent<span>)</span><span>;</span></div></div>
<p>The parent in turn receives two tuples with thread id and computed value from each thread, and based on that decides what to output, after figuring out which thread is which,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>// Wait for both children to let us know they're done.</span><br />
<span>auto</span> done1 <span>=</span> receiveOnly<span>!</span><span>(</span>Tid<span>,</span> <span>long</span><span>)</span><span>;</span><br />
<span>auto</span> done2 <span>=</span> receiveOnly<span>!</span><span>(</span>Tid<span>,</span> <span>long</span><span>)</span><span>;</span><br />
<span>if</span> <span>(</span>done1<span>[</span><span>0</span><span>]</span> <span>==</span> tid2<span>)</span> <span>{</span><br />
writeln<span>(</span>done1<span>[</span><span>1</span><span>]</span><span>)</span><span>;</span><br />
<span>}</span><br />
<span>else</span> <span>{</span><br />
writeln<span>(</span>done2<span>[</span><span>1</span><span>]</span><span>)</span><span>;</span><br />
<span>}</span></div></div>
<p>And voilà, concurrency easy-peasy.</p>
<p><a name="day19"></a></p>
<h2>Day 19: string parsing with enums and final switches</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day19/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day19/app.d">my solution</a>)</p>
<p>The only new D technique here are final switches. A <code class="codecolorer d solarized-light"><span class="d"><span>final</span> <span>switch</span></span></code> is for <code class="codecolorer d solarized-light"><span class="d"><span>enum</span></span></code> types, to make the compiler enforce you writing a <code class="codecolorer d solarized-light"><span class="d"><span>case</span></span></code> to match all of the possible values. That’s what I did here, where I wanted to make sure I matched the up, down, left, and right directions:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>final</span> <span>switch</span><span>(</span>dir<span>)</span><span>{</span><br />
<span>case</span> DIR.<span>d</span><span>:</span><br />
j<span>++;</span><br />
<span>break</span><span>;</span><br />
<span>case</span> DIR.<span>u</span><span>:</span><br />
j<span>--;</span><br />
<span>break</span><span>;</span><br />
<span>case</span> DIR.<span>l</span><span>:</span><br />
i<span>--;</span><br />
<span>break</span><span>;</span><br />
<span>case</span> DIR.<span>r</span><span>:</span><br />
i<span>++;</span><br />
<span>break</span><span>;</span><br />
<span>}</span></div></div>
<p>The rest of the problem is merely some string parsing.</p>
<p><a name="day20"></a></p>
<h2>Day 20: a physics problem with vector operations</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day20/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day20/app.d">my solution</a>)</p>
<p>I have not yet done serious numerical work with D, but I can see that it has all of the necessary ingredients for it. One of the most obvious amongst these is that it has <a href="https://dlang.org/spec/arrays.html#array-operations">built-in support for writing vector instructions</a>. Given <code class="codecolorer d solarized-light"><span class="d"><span>struct</span></span></code> to model a particle in motion,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>struct</span> particle <span>{</span><br />
<span>double</span><span>[</span><span>3</span><span>]</span> pos<span>;</span><br />
<span>double</span><span>[</span><span>3</span><span>]</span> vel<span>;</span><br />
<span>double</span><span>[</span><span>3</span><span>]</span> acc<span>;</span><br />
<span>}</span></div></div>
<p>the following function return another particle where all of the vectors have been divided by its norm (i.e. normalised to 1):</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> unit<span>(</span>particle p<span>)</span> <span>{</span><br />
<span>auto</span><br />
pos <span>=</span> p.<span>pos</span><span>,</span><br />
vel <span>=</span> p.<span>vel</span><span>,</span><br />
acc <span>=</span> p.<span>acc</span><span>;</span><br />
pos<span>[</span><span>]</span> <span>/=</span> pos.<span>norm</span><span>;</span><br />
vel<span>[</span><span>]</span> <span>/=</span> vel.<span>norm</span><span>;</span><br />
acc<span>[</span><span>]</span> <span>/=</span> acc.<span>norm</span><span>;</span><br />
particle u <span>=</span> <span>{</span>pos<span>,</span> vel<span>,</span> acc<span>}</span><span>;</span><br />
<span>return</span> u<span>;</span><br />
<span>}</span></div></div>
<p>This <code class="codecolorer d solarized-light"><span class="d">vec<span>[</span><span>]</span> <span>/=</span> scalar</span></code> notation divides all of the vector by the given scalar. But that’s not all. You can also add or multiply vectors elementwise with similar syntax,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> <span>double</span><span>[</span><span>3</span><span>]</span> diff1 <span>=</span> p.<span>pos</span><span>,</span> diff2 <span>=</span> p.<span>vel</span><span>;</span><br />
diff1<span>[</span><span>]</span> <span>-=</span> p.<span>vel</span><span>[</span><span>]</span><span>;</span><br />
diff2<span>[</span><span>]</span> <span>-=</span> p.<span>acc</span><span>[</span><span>]</span><span>;</span></div></div>
<p>Here <code class="codecolorer d solarized-light"><span class="d">diff1</span></code> and <code class="codecolorer d solarized-light"><span class="d">diff2</span></code> give the vector difference between the position and velocity, and respectively, velocity in acceleration (I use the criterion of all of three of these being mostly collinear to determine if all particles have escaped the system and thus can no longer interact with any other particle).</p>
<p>This is mostly syntactic sugar, however. Although the D compiler can sometimes turn instructions like these into native vector instructions like <a href="https://en.wikipedia.org/wiki/Advanced_Vector_Extensions">AVX</a>, real vectorisation has to be done via <a href="https://dlang.org/spec/simd.html">some standard library support</a></p>
<p><a name="day21"></a></p>
<h2>Day 21: an indexable, hashable, comparable struct</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day21/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day21/app.d">my solution</a>)</p>
<p>I was happy to recognise, via some string mixins, that I could solve this problem by considering the <a href="https://en.wikipedia.org/wiki/Dihedral_group">dihedral group</a> of the square:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>immutable</span> dihedralFun <span>=</span><br />
<span>"function(ref const Pattern p) {<br />
auto n = p.dim;<br />
auto output = new int[][](n, n);<br />
foreach(i; 0..n) {<br />
foreach(j; 0..n) {<br />
output[i][j] = p.grid[%s][%s];<br />
}<br />
}<br />
return output;<br />
}"</span><span>;</span><br />
<br />
<span>immutable</span> dihedralFourGroup <span>=</span> <span>[</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"i"</span><span>,</span> <span>"j"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"n-i-1"</span><span>,</span> <span>"j"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"i"</span><span>,</span> <span>"n-j-1"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"n-i-1"</span><span>,</span> <span>"n-j-1"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"j"</span><span>,</span> <span>"i"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"n-j-1"</span><span>,</span> <span>"i"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"j"</span><span>,</span> <span>"n-i-1"</span><span>)</span><span>)</span><span>,</span><br />
<span>mixin</span><span>(</span>format<span>(</span>dihedralFun<span>,</span> <span>"n-j-1"</span><span>,</span> <span>"n-i-1"</span><span>)</span><span>)</span><span>,</span><br />
<span>]</span><span>;</span></div></div>
<p>This isn’t a new technique, but I’m really happy how it turns out. Almost like lisp macros, but without devolving into the lawless chaos of Python or Javascript <code class="codecolorer python solarized-light"><span class="python"><span>eval</span></span></code> or C preprocessor macros. As an aside, the <code class="codecolorer d solarized-light"><span class="d">format</span></code> function accepts formatted strings with <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html">POSIX syntax for positional arguments</a>, but there isn’t anything built-in as nice as Perl string interpolation or Python format strings.</p>
<p>The real meat of this problem was to implement a grid structure that could be hashed, compared, and indexed. This is is all done with a number of utility functions. For indexing and slicing, the basic idea is that for a user-defined type <code class="codecolorer d solarized-light"><span class="d">foo</span></code></p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> foo<span>[</span>bar<span>..</span><span>baz</span><span>]</span></div></div>
<p>is sugar for</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"> foo.<span>opIndex</span><span>(</span>foo.<span>opSlice</span><span>(</span>bar<span>,</span> baz<span>)</span><span>)</span></div></div>
<p>so those are the two functions you need to implement for indexing and slicing. Similarly, for equality comparison and hashing, you implement <code class="codecolorer d solarized-light"><span class="d">opEquals</span></code> and <code class="codecolorer d solarized-light"><span class="d">toHash</span></code> respectively. I relied on the dihedral functions above for comparison for this problem.</p>
<p>After implementing these functions for a struct (recall: like a class, but with value semantics and no inheritance), the rest of the problem was string parsing and a bit of logic to implement the fractal-like growth rule.</p>
<p><a name="day22"></a></p>
<h2>Day 22: more enums, final switches, and complex numbers</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day22/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day22/app.d">my solution</a>)</p>
<p>Another rectangular grid problem, which I again decided to represent via complex numbers. The posssible infection states given in the problem I turned into an <code class="codecolorer d solarized-light"><span class="d"><span>enum</span></span></code> which I then checked with a <code class="codecolorer d solarized-light"><span class="d"><span>final</span> <span>switch</span></span></code> as before. The grid is then just an associative array from complex grid positions to infection states.</p>
<p>Nothing new here. By now these are all familiar tools and writing D code is becoming habitual for me.</p>
<p><a name="day23"></a></p>
<h2>Day 23: another opcode parsing problem</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day23/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day23/app.d">my solution</a>)</p>
<p>This problem wasn’t technically difficult from a D point of view. The usual switches and string parsing techniques of <a href="http://jordi.inversethought.com/feed/#day08">days 8</a> and <a href="http://jordi.inversethought.com/feed/#day18">18</a> work just as well. In fact, I started with the code of <a href="http://jordi.inversethought.com/feed/#day18">day 18</a> and modified it slightly to fit this problem.</p>
<p>The challenge was to statically analyse the opcode program to determine that it is implementing a very inefficient primality testing algorithm. I won’t go into an analysis of that program here because <a href="https://markheath.net/post/advent-of-code-2017-day-23">others have already done a remarkable job of explaining it</a>. Once this analysis was complete, the meat of the problem then becomes to write a faster primality testing algorithm, such as dumb (but not too dumb) trial division,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> isComposite<span>(</span><span>long</span> p<span>)</span> <span>{</span><br />
<span>return</span> iota<span>(</span><span>2</span><span>,</span> sqrt<span>(</span><span>cast</span><span>(</span><span>double</span><span>)</span> p<span>)</span><span>)</span>.<span>filter</span><span>!</span><span>(</span>x <span>=></span> p <span>%</span> x <span>==</span> <span>0</span><span>)</span>.<span>any</span><span>;</span><br />
<span>}</span></div></div>
<p>and use this test at the appropriate location.</p>
<p><a name="day24"></a></p>
<h2>Day 24: a routine graph-search problem</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day24/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day24/app.d">my solution</a>)</p>
<p>This problem required some sort of graph structure, which I implemented as an associative array from node ids to all edges incident to that node. The problem then reduces to some sort of graph traversal (I did <a href="https://en.wikipedia.org/wiki/Depth-first_search">depth-first search</a>), keeping track of edge weights.</p>
<p>No new D techniques here either, just more practice with my growing bag of tricks.</p>
<p><a name="day25"></a></p>
<h2>Day 25: formatted reads to finish off Advent of D</h2>
<p>(<a href="http://inversethought.com/hg/aoc/file/tip/2017/day25/problem">problem statement</a> / <a href="http://inversethought.com/hg/aoc/file/tip/2017/day25/app.d">my solution</a>)</p>
<p>The final problem involved parsing a slightly more verbose <abbr title="Domain-specific languge">DSL</abbr>. For this, I decided to use formatted strings for reading, like so,</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> branchFmt <span>=</span><br />
<span>" - Write the value %d.<br />
- Move one slot to the %s.<br />
- Continue with state %s.<br />
"</span><span>;</span><br />
<br />
<span>auto</span> parseBranch<span>(</span>File f<span>)</span> <span>{</span><br />
<span>int</span> writeval<span>;</span><br />
<span>string</span> movedir<span>;</span><br />
<span>char</span> newstate<span>;</span><br />
<br />
f.<span>readf</span><span>(</span>branchFmt<span>,</span> <span>&</span>writeval<span>,</span> <span>&</span>movedir<span>,</span> <span>&</span>newstate<span>)</span><span>;</span><br />
<span>return</span> Branch<span>(</span>writeval <span>?</span> <span>true</span> <span>:</span> <span>false</span><span>,</span> movedir <span>==</span> <span>"left"</span> <span>?</span> <span>-</span><span>1</span> <span>:</span> <span>1</span><span>,</span> newstate<span>)</span><span>;</span><br />
<span>}</span></div></div>
<p>This is admittedly a bit brittle. Even the type check between the formatted string and the expected types is done at runtime (but newer D versions have <a href="https://dlang.org/phobos/std_stdio.html#.readf">a compile-time version of readf</a> for type-checking the format string). An error here can cause exceptions at runtime.</p>
<p>Other than this, the only new technique here is that I actually wrote a loop to parse the program file:</p>
<div class="codecolorer-container d solarized-light"><div class="d codecolorer"><span>auto</span> parseInstructions<span>(</span>File f<span>)</span> <span>{</span><br />
Instruction<span>[</span><span>char</span><span>]</span> instructions<span>;</span><br />
<br />
<span>while</span><span>(</span><span>!</span>f.<span>eof</span><span>)</span> <span>{</span><br />
<span>char</span> state<span>;</span><br />
f.<span>readf</span><span>(</span><span>"In state %s:<span>\n</span>"</span><span>,</span> <span>&</span>state<span>)</span><span>;</span><br />
f.<span>readln</span><span>;</span> <span>// "If the current value is 0:"</span><br />
<span>auto</span> if0 <span>=</span> f.<span>parseBranch</span><span>;</span><br />
f.<span>readln</span><span>;</span> <span>// "If the current value is 1:"</span><br />
<span>auto</span> if1 <span>=</span> f.<span>parseBranch</span><span>;</span><br />
f.<span>readln</span><span>;</span> <span>// Blank line</span><br />
instructions<span>[</span>state<span>]</span> <span>=</span> Instruction<span>(</span>if0<span>,</span> if1<span>)</span><span>;</span><br />
<span>}</span><br />
<span>return</span> instructions<span>;</span><br />
<span>}</span></div></div>
<p>A small comfort here is that checking for eof in the loop condition actually works. This is always subtly wrong in C++ and I can never remember why.</p>
<p>What’s left of the problem is absolutely routine D by now: associative arrays, <abbr title="Uniform function-calling syntax">UFCS</abbr>, foreach loops, standard library utilities for summing and iterating and so forth. A few of my favourite things.</p>
<h1>Concluding remarks</h1>
<p>The best part is that my code was also <em>fast</em>! I was comparing my solutions above with someone else who was doing their Advent of Code in C. I could routinely match his execution speed on the problems where we bothered to compare, whenever we wrote similar algorithms. I’m eager to see what D can do when faced with some real number-crunching.</p>
<p>After all this, I have come to appreciate D more, as well as seeing some of its weak points. I think I have already raved enough about how much I like its functional style, its standard library, its type-checking, and its compile-time calculation. I also ran into a few bugs and deprecated features. I have also observed some language questionable design choices. Not once did I notice having a garbage collector. It was lots of fun.</p>
<p>Merry belated Christmas!</p>Jordi Gutiérrez Hermosohttp://jordi.inversethought.comSummary of work done during GSoCtag:blogger.com,1999:blog-3202743535685562583.post-17582287293702340702017-08-29T18:23:37+00:00<span><span>GSoC17 is at the end <span lang="en">and I want to thank </span></span><span><span lang="en"><span>my mentors and the Octave community </span> for giving me the opportunity to participate in this unique experience.</span></span></span><br /><br /><span><span>During this Google Summer of Code, my goal was to implement from scratch the Convolutional Neural Networks package for GNU Octave. It will be integrated with the already existing <i>nnet</i> package. </span></span><br /><span><span><br /></span></span><span><span> This was a very interesting project and a </span><span><span class="" id="result_box" lang="en" tabindex="-1"><span class="">stimulating experience for both the implemented code and the theoretical base behind the algorithms treated. A part has been implemented in Octave and an other part in Python using the Tensorflow API. </span></span></span></span><br /><span><span><br /></span></span><br /><h3><span><span><span>Code repository</span></span></span></h3><span><span>All the code implemented during these months can be found in </span><span>my public repository: </span></span><br /><br /><b><span><span> <a href="https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/all">https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/all</a> </span></span></b><br /><br /><span>(my username: <i>citti berto</i>, bookmark <i>enrico</i>)</span><br /><span><br /></span><span><span>Since I implemented a completely new part of the package, I pushed the entire project in three commits and I wait for the community approving for preparing a PR for the official package [1].</span></span><br /><span><br /></span><br /><h3><span><span><span>Summary</span></span></span></h3><span><b>The first commit</b> <b>(</b></span><span><b><span class="changeset-hash">ade115a, [2])</span></b> contains the layers. There is a class for each layer, with a corresponding function which calls the constructor. All the layers inherit from a Layer class which lets the user create a layers concatenation, that is the network architecture. Layers have several parameters, <span class="" id="result_box" lang="en" tabindex="-1">for which I have guaranteed the compatibility with Matlab [3].</span></span><br /><br /><span><br /></span><span><b>The second commit (</b></span><span><b><span class="changeset-hash">479ecc5 </span>[4]) </b>is about the Python part, including an init file checking the Tensorflow installation. I implemented a Python module, <i>TFintefrace</i>, which includes:</span><br /><span><br /></span><ul><li><span><i>layer.py:</i> an abstract class for layers inheritance</span></li><li><span><i>layers/layers.py:</i> the layer classes that are used to add the right layer to the TF graph</span></li><li><span><i>dataset.py:</i> a class for managing the datasets input</span></li><li><span><i>trainNetwork.py:</i> the core class, which initiates the graph and the session, performs the training and the predictions</span></li><li><span><i>deepdream.py:</i> a version of [5] for deepdream implementation (it has to be completed)</span></li></ul><br /><br /><span><b>The third commit (</b></span><span><b><span class="changeset-hash">e7201d8 [6])</span> </b>includes:</span><br /><ul><li><span><i>trainingOptions</i>: All the options for the training. Up to now, the only optimizer available is the stochastic gradient descent with momentum (sgdm) implemented in the class TrainingOptionsSGDM.</span></li><li><span><i>trainNetwork</i>: passing the data, the architecture and the options, this function performs the training and returns a SeriesNetwork object</span></li><li><span><i>SeriesNetwork</i>: class that contains the trained network, including the Tensorflow graph and session. This has three methods</span><ul><li><span><i><span><i>predict: </i></span></i><span>predicting </span><span>scores for regression problems</span><i><span><i></i></span></i></span></li><li><span><i>classify: </i>predicting labels for classification problems<i><br /></i></span></li><li><span><i>activations:</i></span> <span>getting the output of a specific layer of the architecture</span></li></ul><span><i> </i></span></li></ul><h3><span><span><span> </span></span></span></h3><h4><span><span><span>Goals not met</span></span></span></h4><span><span>I did not manage to implement some features because of the lack of time due to the bug fixing in the last period. </span><span class="short_text" id="result_box" lang="en" tabindex="-1">The problem was the </span><span class="short_text" id="result_box" lang="en" tabindex="-1"><span class="short_text" id="result_box" lang="en" tabindex="-1">conspicuous time</span> spent testing the algorithms (because of the different random generators between Matlab, Octave and Python/Tensorflow). I will work in the next weeks to implement the missing features and I plan to continue to contribute to maintaining this package to keep it up to date with both Tensorflow new versions and Matlab new feature. </span></span><br /><span><br /></span><br /><table> <tbody><tr> <th><span>Function</span></th> <th><span>Missing features</span></th> </tr><tr> <td><span><span><span><i>activations</i></span></span></span></td> <td><span><span><span><span><i>OutputAs</i> (for changing output format)</span></span></span></span></td> </tr><tr> <td><span><span><span><i>imageInputLayer</i></span></span></span></td> <td><span><span><span><span><i>DataAugmentation</i> and <i>Normalization</i></span></span></span></span></td> </tr><tr> <td><span><span><span><i>trainNetwork</i></span></span></span></td> <td><span><span>Accepted inputs: <i>imds</i> or <i>tbl</i></span></span></td> </tr><tr> <td><span><span><span><i>trainNetwork</i></span></span></span></td> <td><span><span><i>.mat Checkpoints</i></span></span></td> </tr><tr> <td><span><span><span><i>trainNetwork</i></span></span></span></td> <td><span><span><i>ExecutionEnvironment: 'multi-gpu' </i>and<i> 'parallel'</i></span></span></td> </tr><tr> <td><span><span><span><i>ClassificationOutputLayer </i></span></span></span></td> <td><span><span><i><span><span>classnames</span></span></i></span></span></td> </tr><tr> <td><span><span><span><i>TrainingOptions</i></span></span></span></td> <td><span><span><span><span><i>WorkerLoad</i> and <i>OutputFcn</i></span></span></span></span></td> </tr><tr> <td><span>DeepDreamImages</span></td> <td><span><span>Generalization to any network and AlexNet example</span></span></td> </tr></tbody></table><span><br /></span><h3><span><span><span> </span></span></span></h3><h3><span><span><span>Tutorial for testing the package</span></span></span></h3><ol><li><span><span>Install Python Tensorflow API (as explained in [4]) </span></span></li><li><span><span>Install Pytave (following these instructions [5])</span></span></li><li><span><span>Install nnet package (In Octave: install [6] and load [7])</span></span></li><li><span><span>Check the package with <i>make check PYTAVE="pytave/dir/"</i> </span></span></li><li><span><span>Open Octave, add the Pytave dir the the paths and run your first network:</span></span></li></ol><span><span><br />### TRAINING ###<br /># Load the training set<br />[XTrain,TTrain] = digitTrain4DArrayData();<br /> </span></span><br /><span><span><span># Define the layers<br />layers = [imageInputLayer([28 28 1]);<br /> convolution2dLayer(5,20);<br /> reluLayer();<br /> maxPooling2dLayer(2,'Stride',2);<br /> fullyConnectedLayer(10);<br /> softmaxLayer();<br /> classificationLayer()];<br /><br /># Define the training options<br />options = trainingOptions('sgdm', 'MaxEpochs', 15, 'InitialLearnRate', 0.04);<br /><br /># Train the network<br />net = trainNetwork(XTrain,TTrain,layers,options);<br /><br /><br />### TESTING ###<br /># Load the testing set<br />[XTest,TTest]= digitTest4DArrayData();<br /><br /># Predict the new labels<br />YTestPred = classify(net,XTest);</span></span></span><br /><br /><h4><span><span><span>Future improvements</span></span></span></h4><ul><li><span><span><span>Manage the session saving</span></span></span></li><li><span><span><span>Save the checkpoints as .mat files and not as TF checkpoints </span></span></span></li><li><span><span>Optimize array passage via Pytave </span></span></li><li><span><span>Categorical variables for classification problems </span></span></li></ul><br /><h4><span><span>Links</span></span></h4><span></span><br /><span>Repo link: <a href="https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/all">https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/all</a></span><br /><span><br /></span><span>[1] <a href="https://sourceforge.net/p/octave/nnet/ci/default/tree/">https://sourceforge.net/p/octave/nnet/ci/default/tree/</a></span><br /><span>[2] <a href="https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/ade115a0ce0c80eb2f617622d32bfe3b2a729b13">https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/ade115a0ce0c80eb2f617622d32bfe3b2a729b13</a></span><br /><span><span>[3] <a href="https://it.mathworks.com/help/nnet/classeslist.html?s_cid=doc_ftr">https://it.mathworks.com/help/nnet/classeslist.html?s_cid=doc_ftr</a></span> </span><br /><span>[4] <a href="https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/479ecc5c1b81dd44c626cc5276ebff5e9f509e84">https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/479ecc5c1b81dd44c626cc5276ebff5e9f509e84</a> </span><br /><span><span>[5] <a href="https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/deepdream">https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/deepdream</a></span> </span><br /><span>[6] <a href="https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/e7201d8081ca3c39f335067ca2a117e7971b5087">https://bitbucket.org/cittiberto/gsoc-octave-nnet/commits/e7201d8081ca3c39f335067ca2a117e7971b5087</a> </span><br /><span><span>[7] <a href="https://www.tensorflow.org/install/">https://www.tensorflow.org/install/</a></span></span><br /><span><span>[8] <a href="https://bitbucket.org/mtmiller/pytave">https://bitbucket.org/mtmiller/pytave</a></span></span><br /><span>[9] <a href="https://www.gnu.org/software/octave/doc/interpreter/Installing-and-Removing-Packages.html">https://www.gnu.org/software/octave/doc/interpreter/Installing-and-Removing-Packages.html</a></span><br /><span>[10] <a href="https://www.gnu.org/software/octave/doc/v4.0.1/Using-Packages.html">https://www.gnu.org/software/octave/doc/v4.0.1/Using-Packages.html</a></span>Enrico Bertinonoreply@blogger.comhttps://gsocnnet.blogspot.com/Final Reporttag:blogger.com,1999:blog-3508201418641099808.post-40861644101484431092017-08-28T15:50:59+00:00This is the final report on my work with the <a href="https://octave.sourceforge.io/interval/">interval package</a> during Google Summer of Code 2017. This whole blog have been dedicated to the project and by reading all posts you can follow my work from beginning to end.<br /><br />The work has been challenging and extremely fun! I have learned a lot about interval arithmetic, the <a href="https://www.gnu.org/software/octave/">Octave</a> and <a href="https://octave.sourceforge.io/">Octave-forge</a> project, and also how to contribute to open-source in general. I have found the whole Octave community to be very helpful and especially I want to thank my mentor, Oliver Heimlich, and co-mentor, Kai Torben Ohlhus, for helping me during the project.<br /><br />Here I will give a small introduction to Octave and the interval package for new readers and a summary of how the work has gone and how you can run the code I have actually contributed with.<br /><br /><h2>Octave and the Interval Package</h2><a href="https://www.gnu.org/software/octave/">Octave</a>, or GNU Octave, is a <a href="https://en.wikipedia.org/wiki/Free_software">free</a> program for scientific computing. It is very similar to <a href="https://en.wikipedia.org/wiki/MATLAB">Matlab</a> and its syntax is largely compatible with it. Octave comes with a large set of core functionality but can also be extended with packages from <a href="https://octave.sourceforge.io/">Octave forge</a>. These add new functionality, for example image processing, fuzzy logic or more statistic functions. One of those packages is the <a href="https://octave.sourceforge.io/interval/index.html">interval package</a> which allows you to compute with <a href="https://en.wikipedia.org/wiki/Interval_arithmetic">interval arithmetic</a> in Octave.<br /><br /><h2>Summary of the Work</h2>The goal with the project was to improve the Octave-forge interval package by implementing support for creating, and working with, N-dimensional arrays of intervals.<br /><br />The work has gone very well and we have just released version 3.0.0 of the interval package incorporating all the contributions I have made during the project.<br /><br />The package now has full support for working with N-dimensional arrays in the same way you do with ordinary floating point numbers in Octave. In addition I have also fixed some bugs not directly related to N-dimensional arrays, see for example bug <a href="https://savannah.gnu.org/bugs/?51783">#51783</a> and <a href="https://savannah.gnu.org/bugs/?51283">#51283</a>.<br /><br />During the project I have made a total of 108 commits. I have made changes to 332 of the packages 666 files. Some of these changes have only been changes in coding style or (mainly) automated adding of tests. Not counting these I have, manually, made changes to 110 files.<br /><br />If you want to take a look at all the commits I have contributed with the best way is to download the <a href="https://sourceforge.net/p/octave/interval/ci/default/tree/">repository</a> after which you can see all the commits from GSoC with<br /><span><br />hg log -u joeldahne@hotmail.com -d "2017-06-01 to 2017-08-29"<br /></span><br />Unfortunately I have not found a good way of isolating commits from a specific period and author on sourceforge where the package is hosted. Instead you can find a list of all commits at the end of this blog post.<br /><br />The <a href="https://octave.sourceforge.io/interval/NEWS.html">NEWS-file</a> from the release of version 3.0.0 is also a pretty good overview of what I have done. While not all of the changes are a result of GSoC quite a lot of them are.<br /><br /><h2>Running the Code</h2>As mentioned above we have just released version 3.0.0 of the interval package. With the new release it is very easy to test the newly added functionality. If you already have Octave installed the easiest way to install the package is with the command “pkg install -forge interval”. This will install the latest release of the package, at the time of writing this is 3.0.0 but that will of course change in the future. You can also download version 3.0.0 directly from <a href="https://octave.sourceforge.io/download.php?package=interval-3.0.0.tar.gz">Octave-forge</a>.<br /><br />If you want you can also download the source code from the official <a href="https://octave.sourceforge.io/pkg-repository/interval/">repository</a> and test it with "make run" or install it with "make install". To download the repository, update to version 3.0.0 an run Octave with the package on Linux use the following<br /><span><br /> hg clone http://hg.code.sf.net/p/octave/interval octave-interval<br /> cd octave-interval<br /> hg update release-3.0.0<br /> make run</span><br /><span><br /></span><br /><h2>A Package for Taylor Arithmetic</h2>The task took less time than planned so I had time to work upon a project depending on my previous work. I started to work on a project for Taylor arithmetic, you can read my <a href="https://gsocinterval.blogspot.se/2017/07/a-package-for-taylor-arithmetic.html">blog post</a> about it. I created a proof of concept implementation as part of my application for Google Summer of Code and I have now started to turn that into a real package. The repository can be found <a href="https://github.com/Urathai/octave-taylor-POC">here</a>.<br /><br />It is still far from complete but my goal is to eventually add it as a package at Octave-Forge. How that goes depends mainly on how much time I have to spend on it the following semesters.<br /><br />If you want to run the code as it is now you can pull the repository and then run it with "make run", this requires that Octave and version 3.0.0 (or higher) of the interval package is installed.<br /><br /><h2>List of Commits</h2>Here is a list of all 108 commits I have done to the interval package<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/68eb1b3281c9">https://sourceforge.net/p/octave/interval/ci/68eb1b3281c9</a><br />summary: Added Swedish translation for package metadata<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/52d6a2565ed2">https://sourceforge.net/p/octave/interval/ci/52d6a2565ed2</a><br />summary: @infsupdec/factorial.m: Fix decoration (bug #51783)<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/cf97d56a8e2a">https://sourceforge.net/p/octave/interval/ci/cf97d56a8e2a</a><br />summary: mpfr_function_d.cc: Cast int to octave_idx_type<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/5ed7880c917f">https://sourceforge.net/p/octave/interval/ci/5ed7880c917f</a><br />summary: maint: Fix input to source<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/637c532ea650">https://sourceforge.net/p/octave/interval/ci/637c532ea650</a><br />summary: @infsupdec/dot.m: Fix decoration on empty input<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/51425fd67692">https://sourceforge.net/p/octave/interval/ci/51425fd67692</a><br />summary: @infsup/postpad.m, @infsup/prepad.m, @infsupdec/postpad.m, @infsupdec/prepad.m: Corrected identification of dimension for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/b4e7f1546e36">https://sourceforge.net/p/octave/interval/ci/b4e7f1546e36</a><br />summary: mpfr_function_d.cc, mpfr_vector_dot_d.cc: Fixed bug when broadcasting with one size equal to zero<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/02db5199932c">https://sourceforge.net/p/octave/interval/ci/02db5199932c</a><br />summary: doc: NEWS.texinfo: Info about vectorization for nthroot and pownrev<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/197d033fc878">https://sourceforge.net/p/octave/interval/ci/197d033fc878</a><br />summary: @infsup/pownrev.m, @infsupdec/pownrev.m: Support for vectorization of p<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/7f8d1945264c">https://sourceforge.net/p/octave/interval/ci/7f8d1945264c</a><br />summary: @infsup/nthroot.m, @infsupdec/nthroot.m, mpfr_function_d.cc: Support for vectorization of n<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/e841c37383d6">https://sourceforge.net/p/octave/interval/ci/e841c37383d6</a><br />summary: doc: NEWS.texinfo: Summarized recent changes from GSoC<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/bc3e9523d42a">https://sourceforge.net/p/octave/interval/ci/bc3e9523d42a</a><br />summary: mpfr_vector_dot_d.cc: Fixed bug when broadcasting with one size equal to zero<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0160ff2c2134">https://sourceforge.net/p/octave/interval/ci/0160ff2c2134</a><br />summary: doc: examples.texinfo: Updated example for the latest Symbolic package version<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f37e1074c3ce">https://sourceforge.net/p/octave/interval/ci/f37e1074c3ce</a><br />summary: @infsup/*.m, @infsupdec/*.m: Added missing N-dimensional versions of tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/eb6b9c01bf6a">https://sourceforge.net/p/octave/interval/ci/eb6b9c01bf6a</a><br />summary: @infsup/*.m, @infsupdec/*.m: N-dimensional versions of all ternary tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/de71e0ad0a6e">https://sourceforge.net/p/octave/interval/ci/de71e0ad0a6e</a><br />summary: @infsup/*.m, @infsupdec/*.m: N-dimensional versions of all binary tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/9440d748b4aa">https://sourceforge.net/p/octave/interval/ci/9440d748b4aa</a><br />summary: @infsup/*.m, @infsupdec*.m: N-dimensional version of all unary tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/cb5b7b824400">https://sourceforge.net/p/octave/interval/ci/cb5b7b824400</a><br />summary: @infsup/powrev2.m: Fixed bug when called with vector arguments<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/a3e8fdb85b97">https://sourceforge.net/p/octave/interval/ci/a3e8fdb85b97</a><br />summary: @infsup/pownrev.m, @infsupdec/pownrev.m: Reworked vectorization test<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/aae143789b81">https://sourceforge.net/p/octave/interval/ci/aae143789b81</a><br />summary: @infsup/pown.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f05814665570">https://sourceforge.net/p/octave/interval/ci/f05814665570</a><br />summary: @infsup/nthroot.n: Reworked vectorization test<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c4c27574e331">https://sourceforge.net/p/octave/interval/ci/c4c27574e331</a><br />summary: @infsup/nthroot.m, @infsupdec/nthroot.m: Clarified that N must be scalar<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/3bfd4e7e6600">https://sourceforge.net/p/octave/interval/ci/3bfd4e7e6600</a><br />summary: @infup/pow.m, @infsupdec/pow.m: Fixed bug when called with vector arguments<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/e013c55a4e2a">https://sourceforge.net/p/octave/interval/ci/e013c55a4e2a</a><br />summary: @infsup/overlap.m, @infsupdec/overlap.m: Fixed formatting of vector test.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/d700a1ad4855">https://sourceforge.net/p/octave/interval/ci/d700a1ad4855</a><br />summary: doc: Modified a test so that it now passes<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/2404686b07fb">https://sourceforge.net/p/octave/interval/ci/2404686b07fb</a><br />summary: doc: Fixed formatting of example<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c078807c6b32">https://sourceforge.net/p/octave/interval/ci/c078807c6b32</a><br />summary: doc: SKIP an example that always fail in the doc-test<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/598dea2b8eb8">https://sourceforge.net/p/octave/interval/ci/598dea2b8eb8</a><br />summary: doc: Fixed missed ending of example in Getting Started<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/66ca24edfba4">https://sourceforge.net/p/octave/interval/ci/66ca24edfba4</a><br />summary: Updated coding style for all infsupdec-class functions<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/4c629b9000fc">https://sourceforge.net/p/octave/interval/ci/4c629b9000fc</a><br />summary: Updated coding style for all infsup-class functions<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/3898a7ad5d93">https://sourceforge.net/p/octave/interval/ci/3898a7ad5d93</a><br />summary: Updated coding style for all non-class functions<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f911db83df14">https://sourceforge.net/p/octave/interval/ci/f911db83df14</a><br />summary: @infsupdec/dot.m: Fixed wrong size of decoration when called with two empty matrices<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/8e6a9e37ee40">https://sourceforge.net/p/octave/interval/ci/8e6a9e37ee40</a><br />summary: Small updates to documentation and comments for a lot of function to account for the support of N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/a2ef249d54ae">https://sourceforge.net/p/octave/interval/ci/a2ef249d54ae</a><br />summary: doc: A small update to Examples, the interval Newton method can only find zeros inside the initial interval<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/76431745772f">https://sourceforge.net/p/octave/interval/ci/76431745772f</a><br />summary: doc: Updates to Getting Started, mainly how to create N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/9821eadca12b">https://sourceforge.net/p/octave/interval/ci/9821eadca12b</a><br />summary: doc: Small updates to Preface regarding N-dimensional arrays and fixed one link<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0f1aadf5f13a">https://sourceforge.net/p/octave/interval/ci/0f1aadf5f13a</a><br />summary: ctc_intersect.m, ctc_union.m: Fixed bugs when used for vectorization and when called with 0 or 1 output arguments<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0e01dc19dc75">https://sourceforge.net/p/octave/interval/ci/0e01dc19dc75</a><br />summary: @infsup/sumsq.m: Updated to use the new functionality of dot.m<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/2fba2056ed31">https://sourceforge.net/p/octave/interval/ci/2fba2056ed31</a><br />summary: @infsup/dot.m, @infsupdec/dot.m, mpfr_vector_dot_d.cc: Added support for N-dimensional vectors. Moved all vectorization to the oct-file. Small changes to functionality to mimic how the sum function works.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/05fc90112ea9">https://sourceforge.net/p/octave/interval/ci/05fc90112ea9</a><br />summary: ctc_intersect.m, ctc_union.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/2a4d1e9fa43e">https://sourceforge.net/p/octave/interval/ci/2a4d1e9fa43e</a><br />summary: @infsup/fsolve.m: Added support for N-dimensional arrays. Fixed problem with the function in the example. Improved performance when creating the cell used in vectorization.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/7a96c346225a">https://sourceforge.net/p/octave/interval/ci/7a96c346225a</a><br />summary: @infsup/disp.m: Fixed wrong enumeration of submatrices<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/933117890e45">https://sourceforge.net/p/octave/interval/ci/933117890e45</a><br />summary: Fixed typo in NEWS.texinfo<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/67734688adb1">https://sourceforge.net/p/octave/interval/ci/67734688adb1</a><br />summary: @infsup/diag.m: Added description of the previous bug fix in the NEWS file<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/07ebc81867cd">https://sourceforge.net/p/octave/interval/ci/07ebc81867cd</a><br />summary: @infsup/diag.m: Fixed error when called with more than 1 argument<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/554c34fb3246">https://sourceforge.net/p/octave/interval/ci/554c34fb3246</a><br />summary: @infsup/meshgrid.m, @infsupdec/meshgrid.m: Removed these functions, now falls back on standard implementation, also updated index<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/4bad2e7451f5">https://sourceforge.net/p/octave/interval/ci/4bad2e7451f5</a><br />summary: @infsup/plot.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/5e0100cdc25f">https://sourceforge.net/p/octave/interval/ci/5e0100cdc25f</a><br />summary: @infsup/plot3.m: Small change to allow for N-dimensional arrays as input<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/695a223ccbb7">https://sourceforge.net/p/octave/interval/ci/695a223ccbb7</a><br />summary: @infsupdec/prod.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/55f570c13f01">https://sourceforge.net/p/octave/interval/ci/55f570c13f01</a><br />summary: @infsup/prod.m: Added support for N-dimensional arrays. Removed short circuit in simple cases.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/445d7e5150aa">https://sourceforge.net/p/octave/interval/ci/445d7e5150aa</a><br />summary: @infsup/sum.m, @infsupdec/sum.m, mpfr_vector_sum_d.cc: Added support for N-dimensional vectors. Moved all vectorization to the oct-file. Small changes to functionality to mimic Octaves standard sum function.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f27ad212735e">https://sourceforge.net/p/octave/interval/ci/f27ad212735e</a><br />summary: @infsup/fminsearch.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/19724b3f581e">https://sourceforge.net/p/octave/interval/ci/19724b3f581e</a><br />summary: mpfr_function_d.cc: Finalized support for N-dimensional arrays with binary functions and added support for it with ternary functions.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/023e2788e445">https://sourceforge.net/p/octave/interval/ci/023e2788e445</a><br />summary: midrad.m: Added tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/617484113019">https://sourceforge.net/p/octave/interval/ci/617484113019</a><br />summary: @infsupdec/infsupdec.m: Added full support for creating N-dimensional arrays and added tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f1c28a3d48b7">https://sourceforge.net/p/octave/interval/ci/f1c28a3d48b7</a><br />summary: @infsup/subset.m, @infsupdec/subset.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/8e9990e3ed13">https://sourceforge.net/p/octave/interval/ci/8e9990e3ed13</a><br />summary: @infsup/strictsubset.m, @infsupdec/strictsubset.m: Fixed coding style and updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/5ca9b26b580d">https://sourceforge.net/p/octave/interval/ci/5ca9b26b580d</a><br />summary: @infsup/strictprecedes.m, @infsupdec/strictprecedes.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/1c76721003f1">https://sourceforge.net/p/octave/interval/ci/1c76721003f1</a><br />summary: @infsup/sdist.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/e044dea7c820">https://sourceforge.net/p/octave/interval/ci/e044dea7c820</a><br />summary: @infsup/precedes.m, @infsupdec/precedes.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/d48ca9206299">https://sourceforge.net/p/octave/interval/ci/d48ca9206299</a><br />summary: @infsup/overlap.m, @infsupdec/overlap.m: Fixed coding style and updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/1ad6b47f9335">https://sourceforge.net/p/octave/interval/ci/1ad6b47f9335</a><br />summary: @infsup/issingleton.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/11dccbcfd97e">https://sourceforge.net/p/octave/interval/ci/11dccbcfd97e</a><br />summary: @infsup/ismember.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/ade5da12cf88">https://sourceforge.net/p/octave/interval/ci/ade5da12cf88</a><br />summary: @infsup/isentire.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/9fc874b71533">https://sourceforge.net/p/octave/interval/ci/9fc874b71533</a><br />summary: @infsup/isempty.m, @infsupdec/isempty.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0c5c4eaf263b">https://sourceforge.net/p/octave/interval/ci/0c5c4eaf263b</a><br />summary: @infsup/iscommoninterval.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c980da83b634">https://sourceforge.net/p/octave/interval/ci/c980da83b634</a><br />summary: @infsup/interior.m, @infsupdec/interior.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/f67d78651aee">https://sourceforge.net/p/octave/interval/ci/f67d78651aee</a><br />summary: @infsup/idist.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/70a389bd59f5">https://sourceforge.net/p/octave/interval/ci/70a389bd59f5</a><br />summary: @infsup/hdist.m: Fixed coding style and updated documentation.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/e34401d2a6d6">https://sourceforge.net/p/octave/interval/ci/e34401d2a6d6</a><br />summary: @infsup/sin.m, @infsupdec/sin.m: Added workaround for bug #51283<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/fea4c6516101">https://sourceforge.net/p/octave/interval/ci/fea4c6516101</a><br />summary: @infsup/gt.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/ce973432d240">https://sourceforge.net/p/octave/interval/ci/ce973432d240</a><br />summary: @infsup/ge.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/a435986d4b0c">https://sourceforge.net/p/octave/interval/ci/a435986d4b0c</a><br />summary: @infsup/lt.m, @infsupdec/lt.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/23fa89e3c461">https://sourceforge.net/p/octave/interval/ci/23fa89e3c461</a><br />summary: @infsup/le.m, @infsupdec/le.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/730397b9e339">https://sourceforge.net/p/octave/interval/ci/730397b9e339</a><br />summary: @infsup/disjoint.m, @infsupdec/disjoint.m: Updated documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/682ad849fc98">https://sourceforge.net/p/octave/interval/ci/682ad849fc98</a><br />summary: mpfr_function_d.cc: Added support for N-dimensional arrays for unary functions. Also temporary support for binary functions.<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/9661e0256c51">https://sourceforge.net/p/octave/interval/ci/9661e0256c51</a><br />summary: crlibm_function.cc: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0ec3d29c0779">https://sourceforge.net/p/octave/interval/ci/0ec3d29c0779</a><br />summary: @infsup/infsup.m: Fixed documentation and added missing line continuation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/ebb00e763a46">https://sourceforge.net/p/octave/interval/ci/ebb00e763a46</a><br />summary: @infsup/disp.m: Fixed documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/1ab2749da374">https://sourceforge.net/p/octave/interval/ci/1ab2749da374</a><br />summary: @infsup/size.m: Fixed documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/507ca8478b72">https://sourceforge.net/p/octave/interval/ci/507ca8478b72</a><br />summary: @infsup/size.m: Fixes to the documentation<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/667da39afece">https://sourceforge.net/p/octave/interval/ci/667da39afece</a><br />summary: nai.m: Small fix to one of the tests<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/3eb13f91065a">https://sourceforge.net/p/octave/interval/ci/3eb13f91065a</a><br />summary: hull.m: Fixes according to Olivers review<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/20d784a6605d">https://sourceforge.net/p/octave/interval/ci/20d784a6605d</a><br />summary: @infsup/display.m: Vectorized loop<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/fab7aa26410f">https://sourceforge.net/p/octave/interval/ci/fab7aa26410f</a><br />summary: @infsup/disp.m: Fixes according to Olivers review, mainly details in the output<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/4959566545db">https://sourceforge.net/p/octave/interval/ci/4959566545db</a><br />summary: @infsup/infsup.m: Updated documentation and added test for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/b25fbf0ad324">https://sourceforge.net/p/octave/interval/ci/b25fbf0ad324</a><br />summary: @infsup/infsup.m: Fixed coding style<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/486a73046d5e">https://sourceforge.net/p/octave/interval/ci/486a73046d5e</a><br />summary: @infsup/disp.m: Updated documentation and added more tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/b6a0435da31f">https://sourceforge.net/p/octave/interval/ci/b6a0435da31f</a><br />summary: exacttointerval.m: Uppdated documentation and added tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/bdad0e6da132">https://sourceforge.net/p/octave/interval/ci/bdad0e6da132</a><br />summary: @infsup/intervaltotext.m, @infsupdec/intervaltotext.m: Updated documentation and added tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/55a3e708aef4">https://sourceforge.net/p/octave/interval/ci/55a3e708aef4</a><br />summary: @infsup/intervaltotext.m: Fixed coding style<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/536b0c5023ee">https://sourceforge.net/p/octave/interval/ci/536b0c5023ee</a><br />summary: @infsup/subsref.m, @infsupdec/subsref.m: Added tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c9654a4dcd8d">https://sourceforge.net/p/octave/interval/ci/c9654a4dcd8d</a><br />summary: @infsup/size.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/636709d06194">https://sourceforge.net/p/octave/interval/ci/636709d06194</a><br />summary: @infsup/end.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/e6451e037120">https://sourceforge.net/p/octave/interval/ci/e6451e037120</a><br />summary: nai.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/837cccf1d627">https://sourceforge.net/p/octave/interval/ci/837cccf1d627</a><br />summary: @infsup/resize.m, @infsupdec/resize.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/11cb9006b9ea">https://sourceforge.net/p/octave/interval/ci/11cb9006b9ea</a><br />summary: @infsup/reshape.m, @infsupdec/reshape.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c276af6c42ae">https://sourceforge.net/p/octave/interval/ci/c276af6c42ae</a><br />summary: @infsup/prepad.m, @infsupdec/prepad.m: Added small parts to the documentation and tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/c92c829dc946">https://sourceforge.net/p/octave/interval/ci/c92c829dc946</a><br />summary: @infsup/postpad.m, @infsupdec/postpad.m: Added small parts to the documentation and tests for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/0f8f6864123e">https://sourceforge.net/p/octave/interval/ci/0f8f6864123e</a><br />summary: @infsup/meshgrid.m, @infsupdec/meshgrid.m: Added support for outputting 3-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/b7faafef6030">https://sourceforge.net/p/octave/interval/ci/b7faafef6030</a><br />summary: @infsup/cat.m, @infsupdec/cat.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/d416a17a6d3d">https://sourceforge.net/p/octave/interval/ci/d416a17a6d3d</a><br />summary: hull.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/031831f6bdfd">https://sourceforge.net/p/octave/interval/ci/031831f6bdfd</a><br />summary: empty.m, entire.m: Added support for N-dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/2816b68b83c4">https://sourceforge.net/p/octave/interval/ci/2816b68b83c4</a><br />summary: @infsup/display.m: Added support for displaying high dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/79c8dfa8ac54">https://sourceforge.net/p/octave/interval/ci/79c8dfa8ac54</a><br />summary: @infsup/disp.m: Added support for displaying high dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/cc87924e52ac">https://sourceforge.net/p/octave/interval/ci/cc87924e52ac</a><br />summary: @infsup/disp.m: Fixed coding style<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/29a6b4ecda2a">https://sourceforge.net/p/octave/interval/ci/29a6b4ecda2a</a><br />summary: @infsupdec/infsupdec.m: Temporary fix for creating high dimensional arrays<br /><br /><a href="https://sourceforge.net/p/octave/interval/ci/039abcf7623d">https://sourceforge.net/p/octave/interval/ci/039abcf7623d</a><br />summary: @infsupdec/infsupdec.m: Fixed coding styleJoel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/Final Resumetag:blogger.com,1999:blog-8509760576772091576.post-35480845913895779712017-08-28T04:46:00+00:00SummaryDuring the GSoC I worked on different special functions that needed to be improved or implemented from scratch. Discussing with my mentors and the community, we decided that my work should be pushed on a copy of the scource code of Octave on <a href="https://bitbucket.org/M_Ginesi/octave">my repository</a> [1] and then I should have work with different bookmarks for each function I had to work on. When different functions happened to be related (e.g. gammainc and gammaincinv), I worked on these on the same bookmark. I present now a summary and the bookmarks related to the functions.<br /><br /> <h2>Incomplete gamma function</h2> <b>bookmark</b>: gammainc <br /><b>first commit</b>: d1e03faf080b<br /><b>last commit</b>: 107dc1d24c1b <br /><b>added files</b>: /libinterp/corefcn/__gammainc_lentz__.cc, /scripts/specfun/gammainc.m, /scripts/specfun/gammaincinv.m<br /> <b>removed files</b>:/libinterp/corefcn/gammainc.cc, /liboctave/external/slatec-fn/dgami.f, /liboctave/external/slatec-fn/dgamit.f, /liboctave/external/slatec-fn/gami.f, /liboctave/external/slatec-fn/gamit.f, /liboctave/external/slatec-fn/xdgami.f, /liboctave/external/slatec-fn/xdgamit.f, /liboctave/external/slatec-fn/xgmainc.f, /liboctave/external/slatec-fn/xsgmainc.f<br /> <b>modified files</b>: NEWS, /doc/interpreter/arith.txi, /libinterp/corefcn/module.mk, /liboctave/external/slatec-fn/module.mk, /liboctave/numeric/lo-specfun.cc, /scripts/specfun/module.mk<br /> <h3>Summary of the work</h3> On this bookmark I worked on the incomplete gamma function and its inverse.<br />The incomplete gamma function gammainc had both missing features (it were missed the "scaled" options) and some problem of inaccurate result type (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=47800">bug # 47800</a>). Part of the work was already been done by Marco and Nir, I had to finish it. We decided to implement it as a single .m file (<tt>gammainc.m</tt>) which call (for some inputs) a subfunction written in C++ (<tt>__gammainc_lentz__.cc</tt>).<br />The inverse of the incomplete gamma function was missing in Octave (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=48036">bug # 48036</a>). I implemented it as a single .m file (<tt>gammaincinv.m</tt>) which uses a Newton method. <h2>Bessel functions</h2><b>bookmark</b>: bessel <br /><b>first commit</b>: aef0656026cc <br /><b>last commit</b>: e9468092daf9 <br /> <b>modified files</b>: /liboctave/external/amos/README, /liboctave/external/amos/cbesh.f, /liboctave/external/amos/cbesi.f, /liboctave/external/amos/cbesj.f, /liboctave/external/amos/cbesk.f, /liboctave/external/amos/zbesh.f, /liboctave/external/amos/zbesi.f, /liboctave/external/amos/zbesj.f, /liboctave/external/amos/zbesk.f, /liboctave/numeric/lo-specfun.cc, /scripts/specfun/bessel.m<br /> <h3>Summary of the work</h3> On this bookmark I worked on Bessel functions. <br />There was a bug reporting NaN as output when the argument $x$ was too large in magnitude (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=48316">bug # 48316</a>). The problem was given by Amos library, which refuses to compute the output in such cases. I started "unlocking" this library, in such a way to compute the output even when the argument was over the limit setted by the library. Then I compared the results with other libraries (e.g. <a href="http://www.netlib.org/cephes/">Cephes</a> [2], <a href="https://www.gnu.org/software/gsl/">Gnu Scientific library</a> [3] and <a href="http://en.cppreference.com/w/cpp/numeric/special_math">C++ special function library</a> [4]) and some implementations I made. In the end, I discovered that the "unlocked" Amos were the best one to use, so we decided to maintain them (in the "unlocked" form), modifying the error variable to explain the loss of accuracy. <h2>Incomplete beta function</h2> <b>bookmark</b>: betainc <br /><b>first commit</b>: 712a069d2860<br /><b>last commit</b>: e0c0dd40f096 <br /> <b>added files</b>: /libinterp/corefcn/__betainc_lentz__.cc, /scripts/specfun/betainc.m, /scripts/specfun/betaincinv.m<br /> <b>removed files</b>: /libinterp/corefcn/betainc.cc, /liboctave/external/slatec-fn/betai.f, /liboctave/external/slatec-fn/dbetai.f, /liboctave/external/slatec-fn/xbetai.f, /liboctave/external/slatec-fn/xdbetai.f<br /> <b>modified files</b>: /libinterp/corefcn/module.mk, /liboctave/external/slatec-fn/module.mk, /liboctave/numeric/lo-specfun.cc, /liboctave/numeric/lo-specfun.h, /scripts/specfun/module.mk, /scripts/statistics/distributions/betainv.m, /scripts/statistics/distributions/binocdf.m<br /> <h3>Summary of the work</h3> On this bookmark I worked on the incomplete beta function and its inverse.<br />The incomplete beta function missed the "upper" version and had reported bugs on input validation (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=34405">bug # 34405</a>) and inaccurate result (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=51157">bug # 51157</a>). We decided to rewrite it from scratch. It is now implemented ad a single .m file (<tt>betainc.m</tt>) which make the input validation part, then the output is computed using a continued fraction evaluation, done by a C++ function (<tt>__betainc_lentz__.cc</tt>).<br />The inverse was present in Octave but missed the "upper" version (since it was missing also in betainc itself). The function is now written as a single .m file (<tt>betaincinv.m</tt>) which implement a Newton method where the initial guess is computed by few steps of bisection method. <h2>Integral functions</h2><b>bookmark</b>: expint<br /><b>first commit</b>: 61d533c7d2d8<br /><b>last commit</b>: d5222cffb1a5<br /> <b>added files</b>:/libinterp/corefcn/__expint_lentz__.cc, /scripts/specfun/cosint.m, /scripts/specfun/sinint.m<br /> <b>modified files</b>: /doc/interpreter/arith.txi, /libinterp/corefcn/module.mk, /scripts/specfun/expint.m, /scripts/specfun/module.mk<br /> <h3>Summary of the work</h3> On this bookmark I worked on exponential integral, sine integral and cosine integral. I already rewrote the exponential integral before the GSoC. Here I just moved the Lentz algorithm to an external C++ function (<tt>__expint_lentz__.cc</tt>), accordingly to gammainc and betainc. I've also modified the exit criterion for the asymptotic expansion using [5] (pages 1 -- 4) as reference.<br />The functions sinint and cosint were present only in the symbolic package of Octave but was missing a numerical implementation in the core. I wrote them as .m files (<tt>sinint.m</tt> and <tt>cosint.m</tt>). Both codes use the series expansion near the origin and relations with expint for the other values.<br /><br /> <h2>To do</h2>There is still room for improvement for some of the functions I wrote. In particular, gammainc can be improved in accuracy for certain couple of values, and I would like to make a template version for the various Lentz algorithms in C++ so to avoid code duplication in the functions.<br />In October I will start a PhD in Computer Science, still here in Verona. This will permit me to remain in contact with my mentor Marco Caliari, so that we will work on these aspects. <br /><br />[1] <a href="https://bitbucket.org/M_Ginesi/octave">https://bitbucket.org/M_Ginesi/octave</a> <br />[2] <a href="http://www.netlib.org/cephes/">http://www.netlib.org/cephes/</a> <br />[3] <a href="https://www.gnu.org/software/gsl/">https://www.gnu.org/software/gsl/</a> <br />[4] <a href="http://en.cppreference.com/w/cpp/numeric/special_math">http://en.cppreference.com/w/cpp/numeric/special_math</a> <br />[5] N. Bleistein and R.A. Handelsman, "Asymptotic Expansions of Integrals", Dover Publications, 1986.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/Geometry Package (Octave)https://piyush-jain1.github.io//GeometryPackage2017-08-28T00:00:00+00:00<h1 id="geometry-package-implement-boolean-operations-on-polygons">Geometry package: Implement boolean operations on polygons</h1>
<p>As part of GSoC 2017 , this project is intended to implement a set of boolean operations and supporting function for acting on polygons. These include the standard set of potential operations such as union/OR, intersection/AND, difference/subtraction, and exclusiveor/XOR. Other things to be implemented are the following functions: polybool, ispolycw, poly2ccw, poly2cw, poly2fv, polyjoin, and polysplit.<br /></p>
<p>This <a href="https://github.com/piyush-jain1/GSoC17OctaveGeometry">Repository</a> is a clone(fork) of the Geometry Package which is a part of the <a href="http://octave.sourceforge.net/">Octave Forge Project</a>.<br /></p>
<p>This fork adds new functions to the official Geometry Package as part of GSoC (Google Summer of Code) 2016.</p>
<p>The official Geometry Package can be found <a href="https://sourceforge.net/p/octave/geometry/ci/default/tree/">here</a></p>
<p>Link to commits on official repo :</p>
<ul>
<li><a href="https://sourceforge.net/p/octave/geometry/ci/19a35efc16dbe645e0bbfbffe9cfaa14e5ec9c96/">19a35e</a></li>
<li><a href="https://sourceforge.net/p/octave/geometry/ci/fc3710b6cce55502e6eff4dc4251d507bc5b4ff1/">fc3710</a></li>
</ul>
<h2 id="added-files-and-functions">Added files and functions</h2>
<ol>
<li>/inst/polygons2d/clipPolygon_mrf.m</li>
<li>/inst/polygons2d/private/_<em>poly2struct_</em>.m</li>
<li>/src/martinez.cpp</li>
<li>/src/polygon.cpp</li>
<li>/src/utilities.cpp</li>
<li>/src/polybool_mrf.cc</li>
<li>/inst/polygons2d/funcAliases</li>
</ol>
<h2 id="bonding-period">Bonding Period</h2>
<p>After discussion with my mentor and keeping my proposal in mind, I tried to understand and enlist the tasks more specifically and in detail. I first tried to understand the conventions and other things about the organisation. I tried to understand the first basic thing which I will need throughout this project, i.e. how we create an oct-interface for the C++ codes to be executable in Octave.
My first goal was to explore the possibility if the already implemented mex-interface geometry package can be improved in performance by replacing it with oct-interface. So, for understanding how these oct-files work and other things , I started to implement something in oct-files and getting familiar with it.</p>
<h2 id="first-coding-phase">First Coding Phase</h2>
<p>As stated, there is an already implemented <a href="https://github.com/piyush-jain1/GSoC17OctaveGeometry">Geometry3.0 package</a>. This has mex interface for its functions. I tried to compare its performance with the oct interface version. For benchmarking, I first implemented my own function for polyUnion (using clipper library) with oct-interface <a href="https://github.com/piyush-jain1/GSoC17OctaveGeometry/tree/master/devel/MyPolyUnion">(Find it here)</a>. Then, I compared its performance over a number of different sets of polgons (parametrized over number of vertices) and recorded the elapsed times with both the interfaces. On plotting a graph of Number of vertices V/s Elapsed time (for oct and mex), following obseravtions were made : </br></p>
<ul>
<li>The oct interface had a better performance over mex.</li>
<li>For 10000 vertices, the oct interface took about 0.008 seconds while the mex interface took about 0.014 seconds. This implies oct interface took 8X10e-3 seconds / 10e4 vertices i.e. 8X10e-7 seconds per vertex. For mex, it was 14X10e-7 seconds per vertex.</li>
<li>As it can be seen from the above data, the oct interface was not more than twice as good as mex interface.</br>
From these observations, it was concluded that it is not worth to change the interface from mex to oct since there was not much improvement in performance. Thus, our next goal is now to incorporate new algorithms.</li>
</ul>
<p>After spending decent time over checking out the new algorithm and understanding its implementation, I have now started to implement the polybool function. I also tried to compare its performance with the already implemented clipPolygon in the current geometry package. The new algorithm has a better performance than the old one.</p>
<p>The implementation of boolean operations on polygons, like <strong>DIFFERENCE</strong> , <strong>INTERSECTION</strong> , <strong>XOR</strong> and <strong>UNION</strong> with the new algorithm is almost done. A little work on tests, demos and documentation is still needed and I am working on that.</p>
<h4 id="more-about-the-algorithm-by-f-martínez-aj-rueda-fr-feito">More about the algorithm by F. Martínez, A.J. Rueda, F.R. Feito</h4>
<p>The algorithm is very easy to understand, among other things because it can be seen as an extension of the classical algorithm, based on the plane sweep, for computing the intersection points between a set of segments.When a new intersection between the edges of polygons is found, the algorithm subdivides the edges at the intersection point. This produces a plane sweep algorithm with only two kind of events: left and right endpoints, making the algorithm quite simple. Furthermore, the subdivision of edges provides a simple way of processing degeneracies. </br>
Overall sketch of the approach for computing Boolean operations on polygons:</p>
<ul>
<li>Subdivide the edges of the polygons at their intersection points.</li>
<li>Select those subdivided edges that lie inside the other polygon—or that do not lie depending on the operation.</li>
<li>Join the edges selected in step 2 to form the result polygon.</br>
<h4 id="complexity">Complexity</h4>
<p>Let n be the total number of edges of all the polygons involved in the Boolean operation and k be the number of intersections of all the polygon edges. The whole algorithm runs in time <strong>O(n+k)log(n)</strong>.</br></p>
</li>
</ul>
<p>After raw testing this new algorithm on several cases, I am now adding few tests in the m-script. Other than that, a demo has also been added in the m-script. The demo can be seen by the command <code class="highlighter-rouge">demo clipPolygon_mrf</code>. The tests have also been added. The test can be seen by <code class="highlighter-rouge">test clipPolygon_mrf</code>.</p>
<h2 id="second-coding-phase">Second Coding Phase</h2>
<p>After implementing the polybool function and checking it, we are planning to include it in the next release of geometry package.
Now , to move forward, I am first importing some functions from last year GSoC’s repo and ensuring their MATLAB compatibility.
Functions like poly2ccw, poly2cw, joinpolygons, splitPolygons have been created as aliases while ensuring their compatibility with their mathworks counterparts.
Then after, there was some time invested on understanding the CGAL library and its implementation.</p>
<p>The further plan is to sync the matgeom package with the geometry package.</p>
<h2 id="third-coding-phase">Third Coding Phase</h2>
<p>Proceeding towards the next goal, the idea is to devise some way to automate the process somewhat, of syncing the matgeom and geometry package. The issue is that when a new release of geometry package is planned, there are some things which ahve been updated in matgeom but not in their geometry counterparts (if it exists). So, every time before releasing, so much time has to be invested in manually checking each edit and syncing it into the geometry.</p>
<p>To achieve this, first a workaround is implemented on a dummy repository - <a href="https://github.com/piyush-jain1/dummyMatGeom/">dummyMatGeom</a>. Its master branch is matGeom (dummy) and there is another branch (named geometry) is created which contains geometry package (dummy). To test the entire procedure, go to the dummy repository dummyMatGeom , pull both branches in different folders, say “dummyMatGeom” for master branch and “dummyGeom” for geometry branch. Then follow the given steps as explained on the <a href="http://wiki.octave.org/Geometry_package:GSoC17">wiki page</a>.</p>
<h4 id="challenges">Challenges</h4>
<ul>
<li>
<p>Clearly, the above procedure will only sync the script of the function, not it’s tests and demo, which are in separate folders in a Matlab package structure. Even if we try to concatenate their corresponding test/demo scripts with the function scripts (as it is in an octave package structure), there will be discrepancies because the notion or writing tests for octave and matlab packages are quite different. The way octave allows tests to work is unique to octave as explained here. SO, we can’t simply concatenate the Matlab test scripts with the functions.</p>
</li>
<li>
<p>Git doesn’t preserves the original version of geometry scripts and overwrites the whole file completely.
For example :</p>
</li>
</ul>
<ol>
<li>Original file at matGeom (upstream)</li>
</ol>
<pre><code class="language-octave">
% Bla bla
% bla bla bla
function z = blabla (x,y)
% Help of function
for i=1:length(x)
z(i) = x(i)*y(i);
end
</code></pre>
<ol>
<li>Ported to geometry</li>
</ol>
<pre><code class="language-octave">
# Copyright - Somebody
# Bla bla
# bla bla bla
# texinfo
# Help of function
function z = blabla (x,y)
z = x .* y;
endfunction
</code></pre>
<ol>
<li>Updated in matGeom</li>
</ol>
<pre><code class="language-octave">
% Bla bla
% bla bla bla
function z = blabla (x,y)
% Help of function
% updated to be more clear
z = zeros (size(x));
for i=1:length(x)
z(i) = x(i)*y(i);
end
</code></pre>
<ol>
<li>After syncing , the expected result is something like this :</li>
</ol>
<pre><code class="language-octave">
# Copyright - Somebody
# Bla bla
# bla bla bla
# texinfo
# Help of function
# updated to be more clear
function z = blabla (x,y)
z = zeros (size(x));
z = x .* y;
endfunction
</code></pre>
<p>But, this doesn’t happen as expected. Git just finds the files which have been modified and overwrites those files completely.
Considering the possibilities of the solutions, there are ways like <code class="highlighter-rouge">git patch</code> or <code class="highlighter-rouge">git interactive</code> which allows us to select the lines specifically which we want to be committed, but that would not serve our purpose as it would not be better than syncing it manually, file by file. Looking for a better solution to handle this !</p>
<p>Now, the further idea is to release geometry and I am getting involved into it to get a feel of how things are done.</p>
<p>Thus, as the time concludes, it’s time to say Goodbye to GSoC’17. It was overall a great learning experience.</p>Piyush Jainhttps://piyush-jain1.github.io//Integral functionstag:blogger.com,1999:blog-8509760576772091576.post-57777785700050860242017-08-19T02:32:44+00:00Integral functionsDuring the last week I made few modifications to expint.m and wrote sinint.m and cosint.m from scratch. All the work done can be found on the bookmark expint of <a href="https://bitbucket.org/M_Ginesi/octave">my repository</a>. <h2>expint</h2>As I mentioned <a href="http://gsocspecfun.blogspot.it/2017/05/expint.html">here</a> I rewrote expint.m from scratch before the GSoC. During the last week I moved the Lentz algorithm to a .cc function (in order to remain coherent with the implementations of gammainc and betainc) and added few tests. <h2>sinint</h2>The sinint function is present in the symbolic package, but is not present a numerical implementation in the core.<br />The sine integral is defined as $$ \text{Si} (z) = \int_0^z \frac{\sin(t)}{t}\,dt. $$ To compute it we use the series expansion $$ \text{Si}(z) = \sum_{n=0}^\infty \frac{(-1)^n z^{2n+1}}{(2n+1)(2n+1)!} $$ when the module of the argument is smaller than 2. For bigger values we use the following relation with the exponential integral $$ \text{Si} = \frac{1}{2i} (E_1(iz)-E_1(-iz)) + \frac{\pi}{2},\quad |\text{arg}(z)| \frac{\pi}{2}$$ and the following simmetry relations $$ \text{Si}(-z) = -\text{Si}(z), $$ $$ \text{Si}(\bar{z}) = \overline {\text{Si}(z)}. $$ The function is write as a single .m file. <h2>cosint</h2>As the sinint function, also cosint is present in the symbolic package, but there is not a numerical implementation in the core.<br />The cosine integral is defined as $$ \text{Ci} (z) = -\int_z^\infty \frac{\cos(t)}{t}\,dt. $$ An equivalent definition is $$ \text{Ci} (z) = \gamma + \log z + \int_0^z \frac{\cos t - 1}{t}\,dt. $$ To compute it we use the series expansion $$ \text{Ci}(z) = \gamma + \log z + \sum_{n=1}^\infty \frac{(-1)^n z^{2n}}{(2n)(2n)!} $$ when the module of the argument is smaller than 2. For bigger values we use the following relation with the exponential integral $$ \text{Ci} = -\frac{1}{2} (E_1(iz)+E_1(-iz)),\quad |\text{arg}(z)| \frac{\pi}{2}$$ and the following simmetry relations $$ \text{Ci}(-z) = \text{Ci}(z) -i\pi,\quad 0\text{arg}(z)\pi, $$ $$ \text{Ci}(\bar{z}) = \overline{\text{Ci}(z)} .$$ As for sinint, also cosint is written as a single .m file.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/The Final Polishtag:blogger.com,1999:blog-3508201418641099808.post-1539978449745527602017-08-18T17:01:11+00:00We are preparing for releasing version 3.0.0 of the interval package and this last week have mainly been about fixing minor bugs related to the release. I mention two of the more interesting bugs here.<br /><h3>Compact Format </h3>We (Oliver) recently added support for "format compact" when printing intervals. It turns out that the way to determine if compact format is enabled differs very much between different version of Octave. There are at least three different ways to get the information.<br /><br />In the older releases (< 4.2.0 I believe) you use "get (0, "FormatSpacing")" but there appear to be a bug for version < 4.0.0 for which this always return "loose".<br /><br />For the current tip of the development branch you can use "[~, spacing] = format ()" to get the spacing.<br /><br />Finally in between these two version you use "__compactformat__ ()".<br /><br />In the end Oliver, probably, found a way to handle this mess and compact format should now be fully supported for intervals. The function to do this is available here <a href="https://sourceforge.net/p/octave/interval/ci/default/tree/inst/@infsup/private/__loosespacing__.m">https://sourceforge.net/p/octave/interval/ci/default/tree/inst/@infsup/private/__loosespacing__.m</a>.<br /><h3>Dot-product of Empty Matrices</h3>When updating "dot" to support N-dimensional arrays I also modified it so that it behaves similar to Octaves standard implementation. The difference is in how it handles empty input. Previously we had<br /><span><br />> x = infsupdec (ones (0, 2));<br />> dot (x, x)<br />ans = 0×2 interval matrix<br /></span><br />but with the new version we get<br /><span><br />> dot (x, x)<br />ans = 1×2 interval vector<br /> [0]_com [0]_com<br /></span><br />which is consistent with the standard implementation.<br /><br />In the function we use "min" to compute the decoration for the result. Normally "min (x)" and "dot (x, x)" returns results of the same size (the dimension along which it is computed is set to 1), but they handle empty input differently. We have<br /><span><br />> x = ones (0, 2);<br />> dot (x, x)<br />ans =<br /> 0 0<br />> min (x)<br />ans = [](0x2)<br /></span><br />This meant that the decoration would be incorrect since the implementation assumed they always had the same size. Fortunately the solution was very simple. If the dimension along which we are computing the dot-product is zero. the decoration should always be "com". So just adding a check for that was enough.<br /><br />You could argue that "min (ones (0, 2))" should return "[inf, inf]" similarly to how many of the other reductions, like "sum" or "prod", return their unit for empty input. But this would most likely be very confusing for a lot of people. And it is not compatible with how Matlab does it either.<br /><h2>Updates on the Taylor Package</h2>I have also had some time to work on the Taylor package this week. The basic utility functions are now completed and I have started to work on functions for actually computing with Taylor expansions. At the moment there are only a limited amount of functions implemented. For example we can calculate the Taylor expansion of order 4 for the functions $\frac{e^x + \log(x)}{1 + x}$ at $x = 5$.<br /><br /><span>## Create a variable of degree 4 and with value 5 </span><br /><span>> x = taylor (infsupdec (5), 4)<br />x = [5]_com + [1]_com X + [0]_com X^2 + [0]_com X^3 + [0]_com X^4</span><br /><span>## Calculate the function </span><br /><span>> (exp (x) + log (x))./(1 + x)<br />ans = [25.003, 25.004]_com + [20.601, 20.602]_com X + [8.9308, 8.9309]_com X^2 + [2.6345, 2.6346]_com X^3 + [0.59148, 0.59149]_com X^4<br /></span><br /><br /><br />Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/betaincinvtag:blogger.com,1999:blog-8509760576772091576.post-29003933909760764402017-08-12T08:52:18+00:00betaincinvThe inverse of the incomplete beta function was present in Octave, but without the "upper" option (since it was missing in betainc itself). We decided to rewrite it from scratch using Newton method, as for gammaincinv (see <a href="http://gsocspecfun.blogspot.it/2017/06/gammaincinv.html">my post on it</a> if you are interested).<br />To make the code numerically more accurate, we decide which version ("lower" or "upper") invert depending on the inputs.<br />At first we compute the trivial values (0 and 1). Then the remaining terms are divided in two sets: those that will be inverted with the "lower" version, and those that will be inverted with the "upper" one. For both cases, we perform 10 iterations of bisection method and then we perform a Newton method.<br />The implementation (together with the new implementation of betainc) can be found on <a href="https://bitbucket.org/M_Ginesi/octave">my repository</a>, bookmark "betainc".Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/Improving the Automatic Teststag:blogger.com,1999:blog-3508201418641099808.post-69913378885500340682017-08-11T15:04:54+00:00Oliver and I have been working on improving the test framework used for the interval package. The package shares a large number of tests with other interval packages through an interval test framework that Oliver created. Here is the <a href="https://github.com/oheim/ITF1788">repository</a>.<br /><br /><h2>Creating the Tests</h2>Previously these tests were separated from the rest of the package and you usually ran them with help of the Makefile. Now Oliver has moved them to the m-files and you can run them, together with the other tests for the function, with <i>test @infsup/function</i> in Octave. This makes it much easier to test the functions directly.<br /><br />In addition to making the tests easier to use we also wanted to extend them to not only test scalar evaluation but also vector evaluation. The test data, input ad expected output, is stored in a cell array and when performing the scalar testing we simply loop over that cell and run the function for each element. The actual code looks like this (in this case for <i>plus</i>)<br /><span><br />%!test<br />%! # Scalar evaluation<br />%! testcases = testdata.NoSignal.infsup.add;<br />%! for testcase = [testcases]'<br />%! assert (isequaln (...<br />%! plus (testcase.in{1}, testcase.in{2}), ...<br />%! testcase.out));<br />%! endfor<br /></span><br />For testing the vector evaluation we simply concatenate the cell array into a vector and give that to the function. Here is what that code looks like<br /><span><br />%! # Vector evaluation<br />%! testcases = testdata.NoSignal.infsup.add;<br />%! in1 = vertcat (vertcat (testcases.in){:, 1});<br />%! in2 = vertcat (vertcat (testcases.in){:, 2});<br />%! out = vertcat (testcases.out);<br />%! assert (isequaln (plus (in1, in2), out));<br /></span><br />Lastly we also wanted to test evaluation of N-dimensional arrays. This is done by concatenating the data into a vector and then reshape that vector into an N-dimensional array. But what size should we use for the array? Well, we want to have at least three dimensions because otherwise we are not really testing N-dimensional arrays. My solution was to completely factor the length of the vector and use that as size, <i>testsize = factor (length (in1))</i>, and if the length of the vector has two or fewer factors we add a few elements to the end until we get at least three factors. This is the code for that<br /><span><br />%!test<br />%! # N-dimensional array evaluation<br />%! testcases = testdata.NoSignal.infsup.add;<br />%! in1 = vertcat (vertcat (testcases.in){:, 1});<br />%! in2 = vertcat (vertcat (testcases.in){:, 2});<br />%! out = vertcat (testcases.out);<br />%! # Reshape data<br />%! i = -1;<br />%! do<br />%! i = i + 1;<br />%! testsize = factor (numel (in1) + i);<br />%! until (numel (testsize) > 2)<br />%! in1 = reshape ([in1; in1(1:i)], testsize);<br />%! in2 = reshape ([in2; in2(1:i)], testsize);<br />%! out = reshape ([out; out(1:i)], testsize);<br />%! assert (isequaln (plus (in1, in2), out));<br /></span><br />This works very well, except when the number of test cases is to small. If the number of test is less than four this will fail. But there are only a handful of functions with that few tests so I fixed those independently.<br /><h2>Running the tests</h2>Okay, so we have created a bunch of new tests for the package. Do we actually find any new bugs with them? Yes!<br /><br />The function <i>pow.m</i> failed on the vector test. The problem? In one place $\&\&$ was used instead of $\&$. For scalar input I believe these behave the same but they differ for vector input.<br /><br />Both the function <i>nthroot.m </i>and the function <i>pownrev.m</i> failed the vector test. Neither allowed vectorization of the integer parameter. For <i>nthroot.m</i> this is the same for standard Octave version so it should perhaps not be treated as a bug. The function <i>pownrev.m</i> uses <i>nthroot.m</i> internally so it also had the same limitation. This time I would however treat it as a bug because the function <i>pown.m</i> does allow vectorization of the integer parameter and if that supports it the reverse function should probably also do it. So I implemented support for vectorization of the integer parameter for both <i>nthroot.m and pownrev.m </i>and they now pass the test.<br /><br />No problems were found with the N-dimensional tests that the vector tests did not find. This is a good indication that the support for N-dimensional arrays is at least partly correct. Always good to know!Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/betainctag:blogger.com,1999:blog-8509760576772091576.post-5473042996657855272017-08-02T04:12:36+00:00table, th, td { border: 1px solid black; } betaincThe betainc function has two bugs reported: <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=34405">#34405</a> on the input validation and <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=51157">#51157</a> on inaccurate result. Moreover, it is missing the "upper" version, which is present in MATLAB. <h1>The function</h1>The <i>incomplete beta function ratio</i> is defined as $$I_x(a,b) = \dfrac{B_x(a,b)}{B(a,b)},\quad 0\le x \le 1,\,a>0,\,b>0,$$ where $B(a,b)$ is the classical beta function and $$B_x(a,b)=\int_0^x t^{a-1}(1-t)^{b-1}\,dt.$$ In the "upper" version the integral goes from $x$ to $1$. To compute this we will use the fact that $$\begin{array}{rcl} I_x(a,b) + I_x^U(a,b) &=& \dfrac{1}{B(a,b)}\left( \int_0^x t^{a-1}(1-t)^{b-1}\,dt + \int_x^1 t^{a-1}(1-t)^{b-1}\,dt\right)\\ &=&\dfrac{1}{B(a,b)}\int_0^1 t^{a-1}(1-t)^{b-1}\,dt\\ &=&\dfrac{B(a,b)}{B(a,b)}\\ &=&1 \end{array}$$ and the relation $$I_x(a,b) + I_{1-x}(b,a) = 1$$ so that $$I_x^U(a,b) = I_{1-x}(b,a).$$ <h1>The implementation</h1>Even if it is possible to obtain a Taylor series representation of the incomplete beta function, it seems to not be used. Indeed the MATLAB help cite only the continuous fraction representation present in "Handbook of Mathematical Functions" by Abramowitz and Stegun: $$I_x(a,b) = \dfrac{x^a(1-x)^b}{aB(a,b)}\left(\dfrac{1}{1+} \dfrac{d_1}{1+} \dfrac{d_2}{1+}\ldots\right)$$ with $$d_{2m+1} = -\dfrac{(a+m)(a+b+m)}{(a+2m)(a+2m+1)}x$$ and $$d_{2m} = \dfrac{m(b-m)}{(a+2m-1)(a+2m)}x$$ which seems to be the same strategy used by GSL. To be more precise, this continued fraction is computed directly when $$x\dfrac{a-1}{a+b-2}$$ otherwise, the computed fraction is used to compute $I_{1-x}(b,a)$ and then it is used the fact that $$I_x(a,b) = 1-I_{1-x}(b,a).$$ In my implementation I use a continued fraction present in "Handboob of Continued Fractions for Special Functions" by Cuyt, Petersen, Verdonk, Waadeland and Jones, which is more complicated but converges in fewer steps: $$\dfrac{B(a,b)I_x(a,b)}{x^a(1-x)^b} = \mathop{\huge{\text{K}}}_{m=1}^\infty \left(\dfrac{\alpha_m(x)}{\beta_m(x)}\right),$$ where $$\begin{array}{rcl} \alpha_1(x) &=&1,\\ \alpha_{m+1}(x) &=&\dfrac{(a+m-1)(a+b+m-1)(b-m)m}{(a+2m-1)^2}x^2,\quad m\geq 1,\\ \beta_{m+1}(x) &=&a + 2m + \left( \dfrac{m(b-m)}{a+2m-1} - \dfrac{(a+m)(a+b+m)}{a+2m+1} \right)x,\quad m\geq 0. \end{array}$$ This is most useful when $$x\leq\dfrac{a}{a+b},$$ thus, the continued fraction is computed directly when this condition is satisfied, while it is used to evaluate $I_{1-x}(b,a)$ otherwise.<br />The function is now written as a .m file, which check the validity of the inputs and divide the same in the values which need to be rescaled and in those wo doesn't need it. Then the continued fraction is computed by an external .c function. Finally, the .m file explicit $I_x(a,b)$. <h1>betaincinv</h1>Next step will be to write the inverse. It was already present in Octave, but is missing the upper version, so it has to be rewritten.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/Second period resumetag:blogger.com,1999:blog-8509760576772091576.post-67833649216937534862017-08-01T09:30:18+00:00Second period resumetd, th{ border: 1px solid } Here I present a brief resume of the work done in this second month. <h2>Bessel functions</h2>The topic of this month were Bessel functions. On the bug tracker it is reported only a problem regarding the "J" one (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=48316">bug 48316</a>), but the same problem is present in every type of Bessel function (they return <tt>NaN + NaNi</tt> when the argument is too big). <h3>Amos, Cephes, C++ and GSL</h3>Actually, Bessel functions in Octave are computed via the Amos library, written in Fortran. Studying the implementation I discovered that the reported bug follows from the fact that if the input is too large in module, the function <tt>zbesj.f</tt> set IERR to 4 (IERR is a variable which describe how the algorithm has terminate) and set the output to zero, then <tt>lo-specfunc.cc</tt> return NaN when IERR=4. Obviusly, the same happen for other Bessel functions.<br />What I initially did was to "unlock" these .f files in such a way to give still IERR=4, but computing anyway the output, and modify <tt>lo-specfun.cc</tt> in order to return the value even if IERR is 4. Then I tested the accuracy, together with other libraries.<br />On the bug report where suggested the <a href="http://www.netlib.org/cephes/">Cephes library</a>, so I tested also them, the <a href="http://en.cppreference.com/w/cpp/numeric/special_math">C++ Special mathematical functions library</a>, and the <a href="https://www.gnu.org/software/gsl/">GSL (Gnu Scientific library)</a>. Unfortunately, both these alternatives work worse than Amos. I also tried to study and implement some asymptotic expansions by myself to use in the cases which give inaccurate results, unfortunately without success.<br />For completeness, in the following table there are some results of the tests (ERR in Cephes refers to <tt>cos total loss of precision error</tt>): <table> <tr> <th></th> <th>1e09</th> <th>1e10</th> <th>1e11</th> <th>1e12</th> <th>1e13</th> </tr> <tr> <td>Amos</td> <td>1.6257e-16</td> <td>0.0000e+00</td> <td>0.0000e+00</td> <td>1.3379e-16</td> <td>1.1905e-16</td> </tr> <tr> <td>Cephes</td> <td>2.825060e-08</td> <td>ERR</td> <td>ERR</td> <td>ERR</td> <td>ERR</td> </tr> <tr> <td>GSL</td> <td>4.8770e-16</td> <td>2.2068e-16</td> <td>4.2553e-16</td> <td>1.3379e-16</td> <td>1.1905e-16</td> </tr> <tr> <td>C++</td> <td>2.82506e-08</td> <td>2.68591e-07</td> <td>1.55655e-05</td> <td>8.58396e-07</td> <td>0.000389545</td> </tr></table><br /><br /> <table> <tr> <th></th> <th>1e15</th> <th>1e20</th> <th>1e25</th> <th>1e30</th> </tr> <tr> <td>Amos</td> <td>1.3522e-16</td> <td>1.6256e-16</td> <td>15.22810</td> <td>2.04092</td> </tr> <tr> <td>GSL</td> <td>1.3522e-16</td> <td>0</td> <td>15.22810</td> <td>2.04092</td> </tr></table> <h3>The problem with the double precision</h3>As I explained in <a href="http://gsocspecfun.blogspot.it/2017/07/gnu-scientific-library.html">my last post</a> the problem seems to be that there are no efficient algorithms in double precision for arguments so large. In fact, the error done by Amos is quite small if compared with the value computed with SageMath (which I'm using as reference one), but only if we use the double precision also in Sage: using more digits, one can see that even the first digit change. Here the tests:<br /><tt>sage: bessel_J(1,10^40).n()<br />7.95201138653707e-21<br />sage: bessel_J(1,10^40).n(digits=16)<br />-7.522035532561300e-21<br />sage: bessel_J(1,10^40).n(digits=20)<br />-5.7641641376196142462e-21<br />sage: bessel_J(1,10^40).n(digits=25)<br />1.620655257788838811378880e-21<br />sage: bessel_J(1,10^40).n(digits=30)<br />1.42325562284462027823158499095e-21<br />sage: bessel_J(1,10^40).n(digits=35)<br />1.4232556228446202782315849909515310e-21<br />sage: bessel_J(1,10^40).n(digits=40)<br />1.423255622844620278231584990951531026335e-21<br /></tt>The value stabilizes only when we use more than 30 digits (two times the digits used in double precision). <h3>The decision</h3>Even if we are aware of the fact that the result is not always accurate, for MATLAB compatibility we decided to unlock the Amos, since they are still the most accurate and, even more important, the type of the inputs is the same as in MATLAB (while, for example, GSL doesn't accept complex $x$ value). Moreover, in Octave is possible to obtain in output the value of IERR, thing that is not possible in MATLAB.<br />You can find the work on the bookmark "bessel" of <a href="https://bitbucket.org/M_Ginesi/octave">my repository</a>. <h2>Betainc</h2>During these last days I also started to implement betainc from scratch, I think it will be ready for the first days of August. Then, it will be necessary to rewrite also betaincinv, since the actual version doesn't have the "upper" version. This should not be too difficult. I think we can use a simple Newton method (as for gammaincinv), the only problem will be, as for gammaincinv, to find good initial guesses.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/A Package for Taylor Arithmetictag:blogger.com,1999:blog-3508201418641099808.post-60010620882918607362017-07-28T21:56:13+00:00In the last blog post I wrote about what was left to do with implementing support for N-dimensional arrays in the interval package. There are still some things to do but I have had, and most likely will have, some time to work on other things. Before the summer I started to work on a proof of concept implementation of Taylor arithmetic in Octave and this week I have continued to work on that. This blog post will be about that.<br /><br /><h2>A Short Introduction to Taylor Arithmetic</h2>Taylor arithmetic is a way to calculate with truncated Taylor expansions of functions. The main benefit is that it can be used to calculate derivatives of arbitrary order.<br /><br />Taylor expansion or Taylor series (I will use these words interchangeably) are well known and from Wikipedia we have: The Taylor series of real or complex valued function $f(x)$ that is infinitely differentiable at a real or complex number $a$ is the power series<br />$$<br />f(a) + \frac{f'(a)}{1!}(x-a) + \frac{f''(a)}{2!}(x-a)^2 + \frac{f'''(a)}{3!}(x-a)^3 + ....<br />$$<br />From the definition it is clear that if we happen to know the coefficients of the Taylor series of $f$ at the point $a$ we can also calculate all derivatives of $f$ at that point by simply multiplying a coefficient with the corresponding factorial.<br /><br />The simplest example of Taylor arithmetic is addition of two Taylor series. If $f$ has the Taylor series $\sum_{n=0}^\infty (f)_n (x-a)^n$ and $g$ the Taylor series $\sum_{n=0}^\infty (g)_n (x-a)^n$ then $f + g$ will have the Taylor series<br />$$<br />\sum_{n=0}^\infty (f + g)_n (x-a)^n = \sum_{n=0}^\infty ((f)_n + (g)_n)(x-a)^n$<br />$$<br />If we instead consider the product, $fg$, we get<br />$$<br />\sum_{n=0}^\infty (fg)_n (x-a)^n = \sum_{n=0}^\infty \left(\sum_{i=0}^n (f)_n(g)_n\right)(x-a)^n.<br />$$<br /><br />With a bit of work you can find similar formulas for other standard functions. For example the coefficients, $(e^f)_n$, of the Taylor expansion of $\exp(f)$ is given by $(e^f)_0 = e^{(f)_0}$ and for $n > 0$<br />$$<br />(e^f)_n = \frac{1}{n}\sum_{i=0}^{n-1}(k-j)(e^f)_i(f)_{n-i}.<br />$$<br /><br />When doing the computations on a computer we consider truncated Taylor series, we choose an order and keep only coefficients up to that order. There is also nothing that stops us from using intervals as coefficients, this allows us to get rigorous enclosures of derivatives of functions.<br /><br />For a more complete introduction to Taylor arithmetic in conjunction with interval arithmetic see [1] which was my first encounter to it. For another implementation of it in code take a look at [2].<br /><h2>Current Implementation Status</h2>As mentioned in the last post my repository can be found <a href="https://github.com/Urathai/octave-taylor-POC">here</a><br /><br />When I started to write on the package, before summer, my main goal was to get something working quickly. Thus I implemented the basic functions needed to do some kind of Taylor arithmetic, a constructor, some help functions and a few functions like $\exp$ and $\sin$.<br /><br />This last week I have focused on implementing the basic utility functions, for example $size$, and rewriting the constructor. In the process I think I have broken the arithmetic functions, I will fix them later.<br /><br />You can at least create and display Taylor expansions now. For example creating a variable $x$ with value 5 of order 3<br /><span><br />> x = taylor (infsupdec (5), 3)<br />x = [5]_com + [1]_com X + [0]_com X^2 + [0]_com X^3<br /></span><br />or a matrix with 4 variables of order 2<br /><span><br />> X = taylor (infsupdec ([1, 2; 3, 4]), 2)<br />X = 2×2 Taylor matrix of order 2<br /><br />ans(:,1) =<br /><br /> [1]_com + [1]_com X + [0]_com X^2<br /> [3]_com + [1]_com X + [0]_com X^2<br /><br />ans(:,2) =<br /><br /> [2]_com + [1]_com X + [0]_com X^2<br /> [4]_com + [1]_com X + [0]_com X^2<br /></span><br />If you want you can create a Taylor expansion with explicitly given coefficients you can do that as well<br /><span><br />> f = taylor (infsupdec ([1; -2; 3, -4))<br />f = [1]_com + [-2]_com X + [3]_com X^2 + [-4]_com X^3<br /></span><br />This would represent a function $f$ with $f(a) = 1$, $f'(a) = -2$, $f''(a) = 3 \cdot 2! = 6$ and $f'''(a) = -4 \cdot 3! = -24$.<br /><h2>Creating a Package</h2>My goal is to create a full package for Taylor arithmetic along with some functions making use of it. The most important step is of course to create a working implementation but there are other things to consider as well. I have a few things I have not completely understood about it. Depending on how much time I have next week I will try to read a bit more about it probably ask some questions on the mailing list. Here are at least some of the things I have been thinking about<br /><h3>Mercurial vs Git?</h3>I have understood that most of the Octave forge packages uses Mercurial for version control. I was not familiar with Mercurial before so the natural choice for me was to use Git. Now I feel I could switch to Mercurial if needed but I would like to know the potential benefits better, I'm still new to Mercurial so I don't have the full picture. One benefit is of course that it is easier if most packages use the same system, but other than that?<br /><h3>How much work is it?</h3>If I were to manage a package for Taylor arithmetic how much work is it? This summer I have been working full time with Octave so I have had lots of time but this will of course not always be the case. I know it takes time if I want to continue to improve the package, but how much, and what, continuous work is there?<br /><h3>What is needed besides the implementation?</h3>From what I have understood there are a couple of things that should be included in a package besides the actual m-files. For example a Makefile for creating the release, an INDEX-file and a CITATION-file. I should probably also include some kind of documentation, especially since Taylor arithmetic is not that well known. Is there anything else I need to think about? <br /><h3>What is the process to get a package approved?</h3>If I were to apply (whatever that means) for the package to go to Octave forge what is the process for that? What is required before it can be approved and what is required after it is approved?<br /><br /><br />[1] W. Tucker, Validated Numerics, Princeton University Press, 2011.<br />[2] F. Blomquist, W. Hofschuster, W. Krämer, Real and complex taylor arithmetic in C-XSC, Preprint 2005/4, Bergische Universität Wuppertal.Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/Deep learning functionstag:blogger.com,1999:blog-3202743535685562583.post-42785794561545122712017-07-27T21:34:59+00:00<span>Hi there,</span><br /><span><br /></span><span>the second part of the project is finishing. This period was quite interesting because I had to dive into the theory behind Neural Networks In particular [1], [2], [3] and [4] were very useful and I will sum up some concepts here below. On the other hand,</span><span> coding became more challenging and t</span><span>he focus was on the python layer and in particular the way to structure the class in order to make everything scalable and generalizable. Summarizing the situation, in the first period I implemented all the Octave classes for the user interface. Those are Matlab compatible and they call some Python function in a seamless way. On the Python side, the TensorFlow API is used to build the graph of the Neural Network and perform training, evaluation and prediction.</span><br /><br /><span>I implemented the three core functions: trainNetwork, SeriesNetwork and trainingOptions. To do this, I used a Python class in which I initialize an object with the graph of the network and I store this object as attribute of SeriesNetwork. Doing that, I call the methods of this class from trainNetwork to perform the training and from predict/classify to perform the predictions. Since it was quite hard to have a clear vision of the situation, I used a Python wrapper (Keras) that allowed me to focus on the integration, "unpack" the problem and go forth "module" by "module". Now I am removing the dependency on the Keras library using directly the Tensorflow API. The code in my repo [5].</span><br /><br /><span>Since I have already explained in last posts how I structured the package, in this post I would like to focus on the theoretical basis of the deep learning functions used in the package. In particular I will present the available layers and the parameters that are available for the training. </span><br /><span><br /></span><br /><h2><span>Theoretical dive</span></h2><h3><span>I. Fundamentals</span></h3><div><span>I want to start with a brief explanation about the perceptron and the back propagation, two key concepts in the artificial neural networks world. </span></div><div><span><br /></span></div><b><span>Neurons</span></b><br /><span><br /></span><span>Let's start from the perceptron, that is the starting point for understanding neural networks and its components. A perceptron is simply a "node" that takes several binary inputs, <span> $ x_1, x_2, ... $, </span>and produces a single binary output:</span><br /><span><br /></span><span>The neuron's output, 0 or 1, is determined by whether the linear combination of the inputs $ \omega \cdot x = \sum_j \omega_j x_j $ is less than or greater than some threshold value. That is a simple mathematical model but is very versatile and powerful because we can combine many perceptrons and varying the weights and the threshold we can get different models. Moving the threshold to the other side of the inequality and replacing it by what's known as the perceptron's bias, b = −threshold, we can rewrite it as</span><br /><span><br /></span><br /><div><span> $ out = \bigg \{ \begin{array}{rl} 0 & \omega \cdot x + b \leq 0 \\ 1 & \omega \cdot x + b > 0 \\ \end{array} $ </span><br /><span><br /></span></div><span>Using the perceptrons like artificial neurons of a network, it turns out that we can devise learning algorithms which can automatically tune the weights and biases. This tuning happens in response to external stimuli, without direct intervention by a programmer and this enables us to have an "automatic" learning.</span><br /><span><br /></span><span>Speaking about learning algorithms, the proceedings are simple: we suppose we make a small change in some weight or bias and what see the corresponding change in the output from the network. If a small change in a weight or bias causes only a small change in output, then we could use this fact to modify the weights and biases to get our network to behave more in the manner we want. The problem is that this isn't what happens when our network contains perceptrons since a small change of any single perceptron can sometimes cause the output of that perceptron to completely flip, say from 0 to 1. We can overcome this problem by introducing an activation function. Instead of the binary output we use a function depending on weights and bias. The most common is the sigmoid function:</span><br /><div><span> $ \sigma (\omega \cdot x + b ) = \dfrac{1}{1 + e^{-(\omega \cdot x + b ) } } $ </span></div><div><span><br /></span><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://2.bp.blogspot.com/-V54xdcvuYKI/WXmj0GjP8pI/AAAAAAAAAoI/xDk4H26fiQ4M8bGkIET7g7vgJnLscb5fwCLcBGAs/s1600/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f382f38632f50657263657074726f6e5f6d6f6a2e706e67.png"><img border="0" height="150" src="https://2.bp.blogspot.com/-V54xdcvuYKI/WXmj0GjP8pI/AAAAAAAAAoI/xDk4H26fiQ4M8bGkIET7g7vgJnLscb5fwCLcBGAs/s400/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f382f38632f50657263657074726f6e5f6d6f6a2e706e67.png" width="400" /></a></td></tr><tr><td class="tr-caption">Figure 1. Single neuron </td></tr></tbody></table><br /></div><span>With the smoothness of the activation function $ \sigma $ we are able to analytically measure the output changes since $ \Delta out $ is a linear function of the changes $ \Delta \omega $ and $ \Delta b$ :</span><br /><div><span> $ \Delta out \approx \sum_j \dfrac{\partial out}{\partial \omega_j} \Delta \omega_j + \dfrac{\partial out}{\partial b} \Delta b $ </span></div><span><br /></span><b><span>Loss function</span></b><br /><span><br /></span><span>Let x be a training input and y(x) the desired output. What we'd like is an algorithm which lets us find weights and biases so that the output from the network approximates y(x) for all x. Most used loss function is mean squared error (MSE) :</span><br /><div><span> $ L( \omega, b) = \dfrac{1}{2n} \sum_x || Y(x) - out ||^2 $ ,</span></div><span>where n is the total number of training inputs, <i>out</i> is the vector of outputs from the network when x is input. </span><br /><span>To minimize the loss function, there are many optimizing algorithms. The one we will use is the gradient descend, of which every iteration of an epoch is defined as:</span><br /><div><span><br /></span></div><div><span> $ \omega_k \rightarrow \omega_k' = \omega_k - \dfrac{\eta}{m} \sum_j \dfrac{\partial L_{X_j}}{\partial \omega_k} $ </span></div><div><span> $ b_k \rightarrow b_k' = b_k - \dfrac{\eta}{m} \sum_j \dfrac{\partial L_{X_j}}{\partial b_k} $ </span></div><span><br /></span><span>where <i>m</i> is the size of the batch of inputs with which we feed the network and $ \eta $ is the learning rate.</span><br /><b><span><br /></span></b><b><span>Backpropagation</span></b><br /><span><br /></span><span>The last concept that I would like to emphasize is the backpropagation. Its goal is to compute the partial derivatives$ \partial L / \partial \omega $ and $ \partial L / \partial b} $ of the loss function L with respect to any weight or bias in the network. The reason is that those partial derivatives are computationally heavy and the network training would be excessively slow.</span><br /><span><br /></span><span>Let be $ z^l $ the <i>weighted input</i> to the neurons in layer <i>l</i>, that can be viewed as a linear function of the activations of the previous layer: $ z^l = \omega^l a^{l-1} + b^l $ .</span><br /><span>In the fundamental steps of backpropagation we compute:</span><br /><span><br /></span><span>1) the final error:</span><br /><div><span> $ \delta ^L = \Delta_a L \odot \sigma' (z^L) $ </span></div><div><span>The first term measures how fast the loss is changing as a function of every output activation and the second term measures how fast the activation function is changing at $ z_L $ </span></div><span><br /></span><span>2) the error of every layer <i>l:</i></span><br /><div><span> $ \delta^l = ((\omega^{l+1})^T \delta^{l+1} ) \odot \sigma' (z^l) $ </span></div><br /><div><span><br /></span></div><span>3) the partial derivative of the loss function with respect to any bias in the net</span><br /><div><span> $ \dfrac{\partial L}{\partial b^l_j} = \delta^l_j $ </span></div><br /><div><span><br /></span></div><span>4) the partial derivative of the loss function with respect to any weight in the net</span><br /><div><span> $ \dfrac{\partial L}{\partial \omega^l_{jk}} = a_k^{l-1} \delta^l_j $ </span></div><br /><div><span><br /></span></div><span>We can therefore update the weights and the biases with the gradient descent and train the network. Since inputs can be too numerous, we can use only a random sample of the inputs. Stochastic Gradient Descent (SGD) simply does away with the expectation in the update and computes the gradient of the parameters using only a single or a few training examples. In particular, we will use the SGD with momentum, that is a method that helps accelerate SGD in the relevant direction and damping oscillations. It does this by adding a fraction γ of the update vector of the past time step to the current update vector.</span><br /><span><br /></span><br /><h2><span>II. Layers</span></h2><span>Here a brief explanation of the functions that I am considering in the trainNetwork class</span><br /><span><br /></span><b><span>Convolution2DLayer</span></b><br /><span><br /></span><span>The convolution layer is the core building block of a convolutional neural network (CNN) and it does most of the computational heavy lifting. They derive their name from the “convolution” operator. The primary purpose of convolution is to extract features from the input image preserving the spatial relationship between pixels by learning image features using small squares of input data. </span><br /><span><br /></span><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://3.bp.blogspot.com/-1CRGQVGuooo/WXir66f-HTI/AAAAAAAAAnw/r_V3gB1hfsYsL8AhyZot9WgSB4eOfx-YgCLcBGAs/s1600/convolution_schematic.gif"><img border="0" src="https://3.bp.blogspot.com/-1CRGQVGuooo/WXir66f-HTI/AAAAAAAAAnw/r_V3gB1hfsYsL8AhyZot9WgSB4eOfx-YgCLcBGAs/s1600/convolution_schematic.gif" /></a></td></tr><tr><td class="tr-caption">Figure 2. Feature extraction with convolution (image taken form https://goo.gl/h7pXxf) </td></tr></tbody></table><span>In the example in Fig.2, the 3×3 matrix is called a 'filter' or 'kernel' and the matrix formed by sliding the filter over the image and computing the dot product is called the 'Convolved Feature' or 'Activation Map' (or the 'Feature Map'). In practice, a CNN learns the values of these filters on its own during the training process (although we still need to specify parameters such as number of filters, filter size, architecture of the network etc. before the training process). The more number of filters we have, the more image features get extracted and the better our network becomes at recognizing patterns in unseen images.</span><br /><span>The size of the Feature Map depends on three parameters: the depth (that corresponds to the number of filters we use for the convolution operation), the stride (that is the number of pixels by which we slide our filter matrix over the input matrix) and the padding (that consists in padding the input matrix with zeros around the border).</span><br /><span><b><br class="Apple-interchange-newline" />ReLULayer</b></span><br /><span><br /></span><span>ReLU stands for Rectified Linear Unit and is a non-linear operation: $ f(x)=max(0,x) $. Usually this is applied element-wise to the output of some other function, such as a matrix-vector product. It </span><span>replaces all negative pixel values in the feature map by zero with the purpose of introducing non-linearity in our network, since most of the real-world data we would want to learn would be non-linear.</span><br /><div><span><br /></span></div><span></span><span><b>FullyConnectedLayer</b></span><br /><br /><span>Neurons in a fully connected layer have full connections to all activations in the previous layer, as seen in regular Neural Networks. H</span><span>ence t</span><span>heir activations can be computed with a matrix multiplication followed by a bias offset. In our case, the purpose of the </span><span>fully-connected</span><span> layer is to use these features for classifying the input image into various classes based on the training dataset. Apart from classification, adding a fully-connected layer is also a cheap way of learning non-linear combinations of the features. </span><br /><br /><span><b>Pooling</b></span><br /><span><b><br /></b></span><span>It is common to periodically insert a pooling layer in-between successive convolution layers. </span><span>Spatial Pooling (also called subsampling or downsampling) reduces the dimensionality of each feature map but retains the most important information. </span><span>In particular, pooling</span><br /><span>- </span><span>makes the input representations (feature dimension) smaller and more manageable</span><br /><span>- </span><span>reduces the number of parameters and computations in the network, therefore, controlling overfitting</span><br /><span>- </span><span>makes the network invariant to small transformations, distortions and translations in the input image</span><br /><span>- </span><span>helps us arrive at an almost scale invariant representation of our image</span><span> </span><br /><span>Spatial Pooling can be of different types: Max, Average, Sum etc.</span><br /><span><br /></span><span><b>MaxPooling2DLayer</b></span><br /><span>In case of Max Pooling, we define a spatial neighborhood (for example, a 2×2 window) and take the largest element from the rectified feature map within that window. </span><br /><span><br /></span><span><b>AveragePooling2DLayer</b></span><br /><span>Instead of taking the largest element we could also take the average.</span><br /><br /><span><b>DropoutLayer</b></span><br /><span><br /></span><span>Dropout in deep learning works as follows: one or more neural network nodes is switched off once in a while so that it will not interact with the network. With dropout, the learned weights of the nodes become somewhat more insensitive to the weights of the other nodes and learn to decide somewhat more by their own. In general, dropout helps the network to generalize better and increase accuracy since the influence of a single node is decreased.</span><br /><div><br /></div><span><b>SoftmaxLayer</b></span><br /><span><b><br /></b></span><span>The purpose of the softmax classification layer is simply to transform all the net activations in your final output layer to a series of values that can be interpreted as probabilities. To do this, the softmax function is applied onto the net intputs.</span><br /><div><span> $ \phi_{softmax} (z^i) = \dfrac{e^{z^i}}{\sum_{j=0}^k e^{z_k^i}} $ </span></div><span><br /></span><span><br /></span> <span><b>CrossChannelNormalizationLayer</b></span><br /><span><b><br /></b></span><span>Local Response Normalization (LRN) layer implements the lateral inhibition that in neurobiology refers to the capacity of an excited neuron to subdue its neighbors.</span><span> This layer is useful when we are dealing with ReLU neurons because they have unbounded activations and we need LRN to normalize that. We want to detect high frequency features with a large response. If we normalize around the local neighborhood of the excited neuron, it becomes even more sensitive as compared to its neighbors. </span><span>At the same time, it will dampen the responses that are uniformly large in any given local neighborhood. If all the values are large, then normalizing those values will diminish all of them. So basically we want to encourage some kind of inhibition and boost the neurons with relatively larger activations.</span><br /><div><span><br /></span></div><h2>III. training options</h2><div><span>The training function takes as input a trainingOptions object that contains the parameters for the training. A brief explanation:</span><br /><span><br /></span><span><b>SolverName</b></span><br /><span>Optimizer chosen for minimize the loss function. To guarantee the Matlab compatibility, only the Stochastic Gradient Descent with Momentum ('sgdm') is allowed</span><br /><span><br /></span><span><b>Momentum</b></span><br /><span>Parameter for the <i>sgdm</i>: it corresponds to the contribution of the previous step to the current iteration</span><br /><span><br /></span><span><b>InitialLearnRate</b></span><br /><span>Initial learning rate <b>η </b>for the optimizer</span><br /><span><br /></span><span><b>LearnRateScheduleSettings</b></span><br /><span>These are the settings for regulating the learning rate. It is a struct containing three values:</span><br /><span><b>RateSchedule</b>: if it is set to '<i>piecewise', </i>the learning rate will drop of a </span><b>RateDropFactor </b><span>every a </span><b>RateDropPeriod </b><span>number of epochs.</span><i> </i><br /><br /><span><b>L2Regularization</b></span><br /><span>Regularizers allow to apply penalties on layer parameters or layer activity during optimization. This is the factor of the L2 regularization.</span><br /><span><br /></span><span><b>MaxEpochs</b></span><br /><span>Number of epochs for training</span><br /><span><br /></span><span><b>Verbose</b></span><br /><span>Display the information of the training every </span><span><b>VerboseFrequency </b>iterations</span><br /><br /><span><b>Shuffle</b></span><br /><span>Random shuffle of the data before training if set to <i>'once'</i></span><br /><span><br /></span><span><b>CheckpointPath</b></span><br />Path for saving the checkpoints<br /><br /><span><b>ExecutionEnvironment</b></span><br /><span>Chose of the hardware for the training: </span><span>'cpu', 'gpu', 'multi-gpu' or 'parallel'. the </span><span>load is divided between workers of GPUs or CPUs according to the r</span><span>elative division set by </span><b>WorkerLoad</b><br /><br /><span><b>OutputFcn</b></span><br />Custom output functions to call during training after each iteration passing a struct containing:<br />Current epoch number, Current iteration number, TimeSinceStart, TrainingLoss, BaseLearnRate, TrainingAccuracy (or TrainingRMSE for regression), State<br /><br />Peace,<br />Enrico<br /><br />[1] [Ian Goodfellow, Yoshua Bengio, Aaron Courville] Deep Learning, MIT Press, http://www.deeplearningbook.org, 2016<br />[2] http://ufldl.stanford.edu/tutorial/supervised/ConvolutionalNeuralNetwork/<br />[3] http://web.stanford.edu/class/cs20si/syllabus.html<br />[4] http://neuralnetworksanddeeplearning.com/<br />[5] https://bitbucket.org/cittiberto/octave-nnet/src</div>Enrico Bertinonoreply@blogger.comhttps://gsocnnet.blogspot.com/Ahead of the Timelinetag:blogger.com,1999:blog-3508201418641099808.post-77085844244945800762017-07-14T16:59:08+00:00One of my first posts on this blog was a <a href="https://gsocinterval.blogspot.se/2017/05/timeline.html">timeline</a> for my work during the project. Predicting the amount of time something takes is always hard to do. Often time you tend to underestimate the complexity of parts of the work. This time however I overestimated the time the work would take.<br /><br />If my timeline would have been correct I would have just started to work on Folding Functions (or reductions as they are often called). Instead I have completed the work on them and also for functions regarding plotting. In addition I have started to work on the documentation for the package as well as checking everything an extra time.<br /><br />In this blog post I will go through what I have done this week, what I think is left to do and a little bit about what I might do if I complete the work on N-dimensional arrays in good time. <br /><br /><h2>This Week</h2><h3>The Dot Function</h3>The $dot$-function was the last function left to implement support for N-dimensional arrays in. It is very similar to the $sum$-function so I already had an idea of how to do it. As with $sum$ I moved most of the handling of the vectorization from the m-files to the oct-file, the main reason being improved performance.<br /><br />The $dot$-functions for intervals is actually a bit different from the standard one. First of all it supports vectorization which the standard one does not<br /><span><br />> dot ([1, 2, 3; 4, 5, 6], 5)<br />error: dot: size of X and Y must match<br />> dot (infsupdec ([1, 2, 3; 4, 5, 6], 5)<br />ans = 1x3 interval vector<br /><br /> [25]_com [35]_com [45]_com<br /></span><br />It also treats empty arrays a little different, see <a href="https://savannah.gnu.org/bugs/?51333">bug #51333</a>,<br /><span><br />> dot ([], [])<br />ans = [](1x0)<br />> dot (infsupdec ([]), [])<br />ans = [0]_com</span><br /><span><br /></span><br /><h3>Package Documentation</h3>I have done the minimal required changes to the documentation. That is I moved support for N-dimensional arrays from Limitation to Features and added some simple examples on how to create N-dimensional arrays.<br /><br /><h3>Searching for Misses</h3>During the work I have tried to update the documentation for all functions to account for the support of N-dimensional arrays and I have also tried to update some of the comments for the code. But as always, especially when working with a lot of files, you miss things, both in the documentation and old comments.<br /><br />I did a quick grep for the words "matrix" and "matrices" since they are candidates for being changed to "array". Doing this I found 35 files where I missed things. It was mainly minor things, comments using the "matrix" which I now changed to "array", but also some documentation which I had forgotten to update.<br /><br /><h2>What is Left?</h2><h3>Package Documentation - Examples</h3>As mentioned above I have done the minimal required changes to the documentation. It would be very nice to add some more interesting example using N-dimensional arrays of intervals in a useful way. Ironically I have not been able to come up with an interesting example but I will continue to think about it. If you have an example that you think would be interesting and want to share, please let me know!<br /><br /><h3>Coding Style</h3>As I mentioned in one of the first blog posts, the coding style for the interval package was not following the standard for Octave. During my work I have adapted all files I have worked with to the coding standard for Octave. A lot of the files I have not needed to do any changes to, so they are still using the old style. It would probably be a good idea to update them as well.<br /><br /><h3>Testing - ITF1788</h3><a href="https://github.com/oheim/ITF1788">The interval framwork libary</a> developed by Oliver is used to test the correctness of many of the functions in the package. At the moment it tests evaluation of scalars but in principle it should be no problem to use it for testing vectorization or even broadcasting. Oliver has already started to work on <a href="https://github.com/oheim/ITF1788/issues/8">this</a>.<br /><br /><h2>After N-dimensional arrays?</h2>If I continue at this pace I will finish the work on N-dimensional arrays before the time of the project is over. Of course the things that are left might take longer than expected, they usually do, but there is a chance that I will have time left after everything is done. So what should I do then? There are more thing that can be done on the interval package, for example adding more examples to the documentation, however I think I would like to start working on a new package for Taylor arithmetics.<br /><br />Before GSoC I started to implement a proof of concept for Taylor arithmetics in Ocatve which can be found <a href="https://github.com/Urathai/octave-taylor-POC">here</a>. I would then start to work on implementing a proper version of it, where I would actually make use of N-dimensional interval arrays. If I want to create a package for this I would also need to learn a lot of other things, one of them being how to manage a package on octave forge.<br /><br />At the moment I will try to finish my work on N-dimensional arrays. Then I can discuss it with Oliver and see what he thinks about it.Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/Set inversion with fsolvetag:blogger.com,1999:blog-3508201418641099808.post-85256159847002398582017-07-13T10:24:52+00:00This week my work have mainly been focused on the interval version of <i>fsolve</i>. I was not sure if and how this could make use of N-dimensional arrays and to find that out I had to understand the function. In the end it turned out that the only generalization that could be done were trivial and required very few changes. However I did find some other problems with the functions that I have been able to fix. Connected to <i>fsolve</i> are the functions <i>ctc_intersect</i> and <i>ctc_union</i>. The also needed only minor changes to allow for N-dimensional input. I will start by giving an introduction to <i>fsolve, ctc_union</i> and <i>ctc_intersect</i> and then I will mention the changes I have done to them.<br /><br /><h2>Introduction to <i>fsolve</i></h2>The standard version of <i>fsolve </i>in Octave is used to solve systems of nonlinear equations<i>. </i>That is, given a functions $f$ and a starting point $x_0$ it returns a value $x$ such that $f(x)$ is close to zero. The interval version of <i>fsolve </i>does much more than this. It is used to enclose the preimage of a set $Y$ under $f$. Given a domain $X$, a set $Y$ and a function $f$ it returns an enclosure of the set<br />$$<br />f^{-1}(Y) = \{x \in X: f(x) \in Y\}.<br />$$ <br />By letting $Y = 0$ we get similar functionality as the standard <i>fsolve</i>, with the difference that the output is an enclosure of all zeros to the function (compared to one point for which $f$ returns close to zero).<br /><h3>Example: The Unit Circle</h3>Consider the function $f(x, y) = \sqrt{x^2 + y^2} - 1$ which is zero exactly on the unit circle. Plugging this in to the standard <i>fsolve</i> we get with $(0.5, 0.5)$ as a starting guess<br /><span><br />> x = fsolve (@(x) f(x(1), x(2)), [0.5, 0.5])<br />x = 0.70711 0.70711<br /></span><br />which indeed is close to a zero. But we get no information about other zeros.<br /><br />Using the interval version of <i>fsolve</i> with $X = [-3, 3] \times [-3, 3]$ as starting domain we get<br /><span><br />> [x paving] = fsolve (f, infsup ([-3, -3], [3, 3]));<br />> x<br />x ⊂ 2×1 interval vector<br /><br /> [-1.002, +1.002]<br /> [-1.0079, +1.0079]<br /></span><br />Plotting the paving we get the picture<br /><br /><div class="separator"><a href="https://4.bp.blogspot.com/-t7BsHJ-ac3M/WV9IULgbx3I/AAAAAAAAAXk/xtt9qHYkHrwjr3cGqxeuLXkgQkHIbDeZACLcBGAs/s1600/circle.png"><img border="0" height="300" src="https://4.bp.blogspot.com/-t7BsHJ-ac3M/WV9IULgbx3I/AAAAAAAAAXk/xtt9qHYkHrwjr3cGqxeuLXkgQkHIbDeZACLcBGAs/s400/circle.png" width="400" /></a></div>which indeed is a good enclosure of the unit circle.<br /><h3>How it works </h3>In its simplest form <i>fsolve</i> uses a simple bisection scheme to find the enclosure. Using interval methods we can find enclosure to images of sets. Given a set $X_0 \subset X$ we have three different possibilities<br /><ul><li>$f(X_0) \subset Y$ in which case we add $X_0$ to the paving</li><li>$f(X_0) \cap Y = \emptyset$ in which case we discard $X_0$</li><li>Otherwise we bisect $X_0$ and continue on the parts</li></ul>By setting a tolerance of when to stop bisecting boxes we get the algorithm to terminate in a finite number of steps.<br /><h3>Contractors</h3>Using bisection is not always very efficient, especially when the domain has many dimensions. One way to speed up the convergence is with what's called contractors. In short a contractor is a function that can take the set $X_0$ and returns a set $X_0' \subset X_0$ with the property that $f(X_0 \setminus X_0') \cap Y = \emptyset$. It's a way of making $X_0$ smaller without having to bisect it that many times.<br /><br />When you construct a contractor you use the reverse operations definer on intervals. I will not go into how this works, if you are interested you can find more information in the package documentation [1] and in these youtube videos about Set Inversion Via Interval Analysis (SIVIA) [2].<br /><br />The functions <i>ctc_union</i> and <i>ctc_intersect</i> are used to combine contractors on sets into contractors on unions or intersections of these sets.<br /><h2>Generalization to N-dimensional arrays</h2>How can <i>fsolve</i> be generalized to N-dimensional arrays? The only natural thing to do is to allow for the input and output of $f$ to be N-dimensional arrays. This also is no problem to do. While you mathematically probably would say that <i>fsolve</i> is used to do set inversion for functions $f: \mathbb{R}^n \to \mathbb{R}^m$ it can of course also be used for example on functions $f: \mathbb{R}^{n_1}\times \mathbb{R}^{n_2} \to \mathbb{R}^{m_1}\times \mathbb{R}^{m_2}$.<br /><br />This is however a bit different when using vectorization. When not using vectorization (and not using contractions) <i>fsolve</i> expects that the functions takes one argument which is an array with each element corresponding to a variable. If vectorization is used it instead assumes that the functions takes one argument for each variable. Every argument is then given as a vector with each element corresponding to one value of the variable for which to compute the function. Here we have no use of N-dimensional arrays.<br /><h2>Modifications</h2>The only change in functionality that I have done to the functions is to allow for N-dimensional arrays as input and output when vectorization is not used. This required only minor changes, essentially changing expressions like <span><br />max (max (wid (interval))) </span><br />to <span><br />max (wid (interval)(:)) </span><br />It was also enough to do these changes in <i>ctc_union</i> and <i>ctc_intersect</i> to have these support N-dimensional arrays.<br /><br />I have made no functional changes when vectorization is used. I have however made an optimization in the construction of the arguments to the function. The arguments are stored in an array but before being given to the function they need to be split up into the different variables. This is done by creating a cell array with each element being a vector with the values of one of the variables. Previously the construction of this cell array was very inefficient, it split the interval into its lower and upper part and then called the constructor to create an interval again. Now it copies the intervals into the cell without having to call the constructor. This actually seems have been quite a big improvement, using the old version the example with the unit circle from above took around 0.129 seconds and with the new version it takes about 0.092 seconds. This is of course only one benchmark, but a speed up of about 40% for this test is promising!<br /><br />Lastly I noticed a problem in the example used in the documentation of the function. The function used is<br /><span><br /># Solve x1 ^ 2 + x2 ^ 2 = 1 for -3 ≤ x1, x2 ≤ 3 again,<br /># but now contractions speed up the algorithm.<br />function [fval, cx1, cx2] = f (y, x1, x2)<br /> # Forward evaluation<br /> x1_sqr = x1 .^ 2;<br /> x2_sqr = x2 .^ 2;<br /> fval = hypot (x1, x2);<br /> # Reverse evaluation and contraction<br /> y = intersect (y, fval);<br /> # Contract the squares<br /> x1_sqr = intersect (x1_sqr, y - x2_sqr);<br /> x2_sqr = intersect (x2_sqr, y - x1_sqr);<br /> # Contract the parameters<br /> cx1 = sqrrev (x1_sqr, x1);<br /> cx2 = sqrrev (x2_sqr, x2);<br />endfunction<br /></span><br />Do you see the problem? I think it took me more than a day to realize that the problems I was having was not because of a bug in <i>fsolve</i> but because this function computes the wrong thing. The function is supposed to be $f(x_1, x_2) = x_1^2 + x_2^2$ but when calculating the value it calls hypot which is given by $hypot(x_1, x_2) = \sqrt{x_1^2 + x_2^2}$. For $f(x_1, x_2) = 1$, which is used in the example, it gives the same result, but otherwise it will of course not work.<br /><br />[1] <a href="https://octave.sourceforge.io/interval/package_doc/Parameter-Estimation.html#Parameter-Estimation">https://octave.sourceforge.io/interval/package_doc/Parameter-Estimation.html#Parameter-Estimation</a><br />[2] <a href="https://www.youtube.com/watch?v=kxYh2cXHdNQ">https://www.youtube.com/watch?v=kxYh2cXHdNQ</a><br /><br /><br />Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/Gnu Scientific Librarytag:blogger.com,1999:blog-8509760576772091576.post-84601096687673365162017-07-11T09:01:18+00:00<h2>Bessel functions</h2>During the second part of GSoC I have to work on Bessel functions. The bug is related in particular on the <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=48316">Bessel function of first kind</a> and regard the fact that the function is not computed if the $x$ argument is too big.<br />During this week I studied the actual implementation i.e. the Amos library, written in Fortran. The problem is related to the fact that zbesj.f simply refuse to compute the result when the input argument is too large (the same problem happens for all bessel functions, both for real and complex arguments). What I did was to "unlock" the amos in such a way to compute the value in any case (which seems to be the strategy used by MATLAB). Then I compared the relative errors with the relative errors done by the Gnu Scientific Library (GSL).<br />At first, I would precise some limitation present in GSL: <ul> <li> The parameter $x$ must be real, while it can be complex in Amos and MATLAB. The same holds for the parameter $\alpha$. </li> <li> The class of the output does not adapt to the class of the input, returning <b>always</b> a double. </li></ul>Doing some tests, it seems that the amos work better in terms of accuracy (even if we are talking about errors which do not differ in the order). I concentrated on values in which Amos usually refuse to compute, since in every other zone of the $x-\alpha$ plane it is known the error is in the order of 1e-15. For $\alpha\in\{-1,0,1\}$ they return the same result, while for other values of $\alpha$, Amos are in general more accurate.<br />Anyway, I would remark the fact that there are not, as far as I know, accurate algorithms in double precision for very large magnitude. In fact, for such arguments, both Amos and GSL make a relative error of the order of the unity. This problem is evident when using SageMath to compute accurate values, e.g.<br /><tt>sage: bessel_J(1,10^40).n()<br />7.95201138653707e-21<br />sage: bessel_J(1,10^40).n(digits=16)<br />-7.522035532561300e-21<br />sage: bessel_J(1,10^40).n(digits=20)<br />-5.7641641376196142462e-21<br />sage: bessel_J(1,10^40).n(digits=25)<br />1.620655257788838811378880e-21<br />sage: bessel_J(1,10^40).n(digits=30)<br />1.42325562284462027823158499095e-21<br />sage: bessel_J(1,10^40).n(digits=35)<br />1.4232556228446202782315849909515310e-21<br />sage: bessel_J(1,10^40).n(digits=40)<br />1.423255622844620278231584990951531026335e-21<br /></tt>The values "stabilize" only when the number of digits is bigger than 30, far away from double precision. <h2>Incomplete gamma function</h2>I also gave a look on how to use GSL to eventually improve the incomplete gamma function. It is not possible, however, to use only GSL functions gsl_sf_gamma_inc_P and gsl_sf_gamma_inc_Q due to their limitations: <ul> <li> There is no the "scaled" option. </li> <li> The value of $x$ must be real. This may be not a problem since even MATLAB does not accept complex value (while the version I worked on does). </li> <li> The parameter $x$ must be positive. This is actually a problem of MATLAB compatibility. </li> <li> The class of the output does not adapt to the class of the input, returning <b>always</b> a double. </li></ul>I tested the accuracy of the GSL and they work better when $a1$ and $x\ll1$ so I think I will fix gammainc.m using the algorithm of gsl_sf_gamma_inc_P and gsl_sf_gamma_inc_Q for that values of the input arguments. <h2>Incomplete Beta function</h2>The actual incomplete beta function needs to be replaced. I've already studied the continued fraction exapnsion (which seems to be the best one to use). In GSL is implemented in a good way but still present two limitations: <ul> <li> Does not adapt to the input class (it always work in double precision). </li> <li> There is no "upper" version. This is missing also in the current betainc function, but it is present in MATLAB. Unfortunately, it is not sufficient to compute it as $1 - I_x(a,b)$, it is necessary to find an accurate way to compute directly it. </li></ul>So I will take inspiration to GSL version of the function but I think I will write beatinc as a single .m file.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/First period resumetag:blogger.com,1999:blog-8509760576772091576.post-11161719239415148922017-07-01T07:58:38+00:00First period resume<h1>First period resume</h1>Here I present a brief resume of the work done in this first month. <h2>1st week: Gammainc</h2>The incomplete gamma function presented a series of inaccurate results in the previous implementation, so it has been decided to rewrite it from scratch. A big part of the work was done by Marco and Nir (<a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=47800">here</a> the discussion). I gave my contribution by fixing some problems in the implementation and by making a functioning commit (thanks to the suggestions given by Carne during the OctConf in Geneve). <h2>2nd and 3rd week: Gammaincinv</h2>The inverse of the incomplete gamma function was completely missing in Octave (and this was a problem of compatibility with Matlab). Now the function is present, written as a single .m file. The implementation consist in a simple, but efficient, Newton's method. <h2>Last week: Betainc</h2>When I first submitted my timeline, there was only a bug related to betainc, involving the input validation. During the first period of GSoC emerged a new bug of "incorrect result" type. From this, me and my mentor decided that it is necessary to rewrite the function, so I used this last week to study efficient algorithms to evaluate it. In particular, seems that the most efficient way is to use continude fractions, maybe after a rescale of the function. I'm already working on it, but I will complete it during the first week of august, after finishing the work on Bessel functions.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/One Month Intag:blogger.com,1999:blog-3508201418641099808.post-88968232709948897392017-06-30T16:38:02+00:00Now one month of GSoC has passed and so far everything has gone much better than I expected! According to my <a href="https://gsocinterval.blogspot.se/2017/05/timeline.html">timeline</a> this week would have been the first of two were I work on vectorization. Instead I have already mostly finished the vectorization and have started to work on other things. In this blog post I'll give a summary of what work I have completed and what I have left to do. I'll structure it according to where the functions are listed in the $INDEX$-file [1]. The number after the heading is the number of functions in that category.<br /><br />Since this will mainly be a list of which files have been modified and which are left to do this might not be very interesting if you are not familiar with the structure of the interval package.<br /><h3>Interval constant (3)</h3>All of these have been modified to support N-dimensional arrays.<br /><h3>Interval constructor (5)</h3>All of these have been modified to support N-dimensional arrays.<br /><h3>Interval function (most with tightest accuracy) (63)</h3>Almost all of these functions worked out of the box! At least after the API functions to the MPFR and crlibm libraries were fixed, they are further down in the list.<br /><br />The only function that did not work immediately were $linspace$. Even though this function could be generalized to N-dimensinal arrays the standard Octave function only works for matrices (I think the Matlab version only allows scalars). This means that adding support for N-dimensional vectors for the interval version is not a priority. I might do it later on but it is not necessary. <br /><h3>Interval matrix operation (16)</h3>Most of the matrix functions does not make sense for N-dimensional arrays. For example matrix multiplication and matrix inversion only makes sense for matrices. However all of the reduction functions are also here, they include $dot$, $prod$, $sum$, $sumabs$ and $sumsq$.<br /><br />At the moment I have implemented support for N-dimensional arrays for $sum$, $sumabs$ and $prod$. The functions $dot$ and $sumsq$ are not ready, I'm waiting to see what happens with bug #51333 [2] before I continue with that work. Depending on the bug I might also have to modify the behaviour of $sum$, $sumabs$ and $prod$ slightly.<br /><h3>Interval comparison (19)</h3>All of these have been modified to support N-dimensional arrays.<br /><h3>Set operation (7)</h3>All of these functions have been modified to support N-dimensional arrays except one, $mince$. The function $mince$ is an interval version of $linspace$ and reasoning here is the same as that for $linspace$ above.<br /><h3>Interval reverse operation (12)</h3>Like the interval functions above, all of the functions worked out of the box!<br /><h3>Interval numeric function (11)</h3>Also these functions worked out of the box, with some small modifications to the documentation for some of them.<br /><h3>Interval input and output (9)</h3>Here there are some functions which require some comments, the ones I do not comment about have all gotten support for N-dimensional arrays.<br /><br />$interval\_bitpack$<br />I think that this function does not make sense to generalize to N-dimensions. It could perhaps take an N-dimensional arrays as input, but it will always return a row vector. I have left it as it is for now at least.<br /><br />$disp$ and $display$<br />These are functions that might be subject to change later on. At the moment it prints N-dimensional arrays of intervals in the same way Octave does for normal arrays. It's however not clear how to handle the $\subset$ symbol and we might decide to change it.<br /><h3>Interval solver or optimizer (5)</h3>The functions $gauss$ and $polyval$ are not generalizable to N-dimensional vectors. I don't think that $fzero$ can be generalized either, for it to work the functions must be real-valued.<br /><br />The function $fsolve$ can perhaps be modified to support N-dimensional vectors. It uses the SIVIA algorithm [3] and I have to dive deeper into how it works to see if it can be done.<br /><br />For $fminsearch$ nothing needed to be done, it worked for N-dimensional arrays directly. <br /><h3>Interval contractor arithmetic (2)</h3>Both of these functions are used together with $fsolve$ so they also depend on if SIVIA can be generalized or not.<br /><h3>Verified solver or optimizer (6)</h3>All of these functions work on matrices and cannot be generalized.<br /><h3>Utility function (29)</h3>All of these for which it made sense have been modified to support N-dimensional arrays. Some of them only works for matrices, these are $ctranspose$, $diag$, $transpose$, $tril$ and $triu$. I have left them as they were, though I fixed a bug in $diag$.<br /><h3>API function to the MPFR and crlibm libraries (8)</h3>These are the functions that in general required most work. The ones I have added full support for N-dimensional arrays in are $crlibm\_function$, $mpfr\_function\_d$ and $mpfr\_vector\_sum\_d$. Some of them cannot be generalized, these are $mpfr\_matrix\_mul_d$, $mpfr\_matrix\_sqr\_d$ and $mpfr\_to\_string\_d$. The functions $mpfr\_linspace\_d$ and $mpfr\_vector\_dot\_d$ are related to what I mentioned above for $linspace$ and $dot$.<br /><h3>Summary</h3>So summing up the functions that still require some work are<br /><ul><li>Functions related to $fsolve$</li><li>The functions $dot$ and $sumsq$</li><li>The functions $linspace$ and $mince$</li></ul>Especially the functions related to $fsolve$ might take some time to handle. My goal is to dive deeper into this next week.<br /><br />Apart from this there are also some more things that needs to be considered. The documentation for the package will need to be updated. This includes adding some examples which make use of the new functionality.<br /><br />The interval package also did not follow the coding style for Octave. All the functions which I have made changes to have been updated with the correct coding style, but many of the functions that worked out of the box still use the old style. It might be that we want to unify the coding standard for all files before the next release. <br /><br />[1] The $INDEX$ file <a href="https://sourceforge.net/u/urathai/octave/ci/default/tree/INDEX">https://sourceforge.net/u/urathai/octave/ci/default/tree/INDEX</a><br />[2] Bug #51333 <a href="https://savannah.gnu.org/bugs/index.php?51333">https://savannah.gnu.org/bugs/index.php?51333</a><br />[3] The SIVIA algorithm <a href="https://www.youtube.com/watch?v=kxYh2cXHdNQ">https://www.youtube.com/watch?v=kxYh2cXHdNQ</a>Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/End of the first work periodtag:blogger.com,1999:blog-3202743535685562583.post-81741917703560258972017-06-30T09:32:16+00:00Hi all,<br /><br />this is the end of this first period of GSoC! It was a challenging but very interesting period and I am very excited about the next two months. It is the first time where I have the opportunity to make something that has a real value, albeit small, on someone else's work. Even when I have to do small tasks, like structure the package or learn Tensorflow APIs, I do it with enthusiasm because I have very clear in mind the ultimate goal and the value that my efforts would bring. It may seem trivial, but for a student this is not the daily bread :) I really have fun coding for this project and I hope this will last until the end!<br /><br />Speaking about the project, I've spent some time wondering which was the best solution to test the correct installation of the Tensorflow Python APIs on the machine. The last solution was putting the test in a function __nnet_init__ and calling it in the PKG_ADD (code in <i>inst/__nnet_init__ </i>in my repo [1]).<br /><br />Regarding the code, in this last days I tried to connect the dots, calling a Tensorflow network from Octave in a "Matlab compatible" way. In particular, I use the classes that I made two weeks ago in order to implement a basic version of trainNetwork, that is the core function of this package. As I explained in my post of June 12, trainNetwork takes as input the data and two objects: the layers and the options. I had some some difficulty during the implementation of the Layer class due to the inheritance and the overloading. Eventually, I decided to store the layers in a cell array as attribute of the Layer class. Overloading the subsref, I let the user call a specific layer with the '()' access, like a classic array. With this kind of overloading I managed to solve the main problem of this structure, that is the possibility to get a property of a layer doing for example <span>layers(1).Name</span><br /><br /><span>classdef Layer < handle</span><br /><span> properties (Access = private)</span><br /><span> layers = {};</span><br /><span> endproperties</span><br /><span><br /></span><span> methods (Hidden, Access = {?Layers})</span><br /><span> function this = Layer (varargin)</span><br /><span> nargin = numel(varargin);</span><br /><span> this.layers = cell(1, nargin);</span><br /><span> for i = 1:nargin</span><br /><span> this.layers{i} = varargin{i};</span><br /><span> end</span><br /><span> endfunction</span><br /><span> endmethods</span><br /><span><br /></span><span> methods (Hidden)</span><br /><span> function obj = subsref(this, idx)</span><br /><span> switch idx(1).type</span><br /><span> case '()'</span><br /><span> idx(1).type = '{}';</span><br /><span> obj = builtin('subsref',this.layers,idx);</span><br /><span> case '{}'</span><br /><span> error('{} indexing not supported');</span><br /><span> case '.'</span><br /><span> obj = builtin('subsref',this,idx);</span><br /><span> endswitch</span><br /><span> endfunction</span><br /><span><br /></span><span> function obj = numel(this)</span><br /><span> obj = builtin('numel',this.layers);</span><br /><span> endfunction</span><br /><span><br /></span><span> function obj = size(this)</span><br /><span> obj = builtin('size',this.layers);</span><br /><span> endfunction</span><br /><div><span>endmethods</span></div><div><div><span>endclassdef</span></div></div><div><br /></div><div><br /></div><div>Therefore, I implemented the same example of my last post in a proper way. In <i>tests/script </i>you can find the function <i>cnn_linear_model</i> which consists simply in:</div><div><br /></div>1. Loading the datasets<br /><span> load('tests/examples/cnn_linear_model/train_70.mat')<br /> load('tests/examples/cnn_linear_model/test_70.mat')</span><br /><br /><div>2. Defining layers and options<br /><span> layers = [ ...<br /> imageInputLayer([28 28 1])<br /> convolution2dLayer(12,25)<br /> reluLayer<br /> fullyConnectedLayer(1)<br /> regressionLayer];<br /> options = trainingOptions('sgdm', 'MaxEpochs', 1);</span><br /><br />3. Training<br /><span>net = trainNetwork(trainImages, trainAngles, layers, options);</span><br />4. Prediction <br /><span>acc = net.predict(testImages, testAngles) </span><br /><div><span><br /></span><div><i>TrainNetwork</i> is a draft and I have not yet implemented the classe <i>seriesNetwork</i>, but I think it's a good start :) In next weeks I will focus on the Tensorflow backend of the above mentioned functions, with the goal of having a working version at the end of the second period!</div><div><br /></div><div>Peace,</div><div>Enrico</div><div><br /></div><div>[1] https://bitbucket.org/cittiberto/octave-nnet/src</div></div></div>Enrico Bertinonoreply@blogger.comhttps://gsocnnet.blogspot.com/Package structuretag:blogger.com,1999:blog-3202743535685562583.post-57143538901010072032017-06-24T20:55:31+00:00Hello! During the first period of GSoC I have worked mostly on analyzing the Matlab structure of the net package in order to guarantee the compatibility throughout the whole project. The focus of the project is on the convolutional neural networks, about which I will write the next post.<br /><br />Regarding the package structure, the core will be composed by three parts: <br /><br /><ol><li><b>Layers</b>:<b> </b>there are 11 types of layers<b> </b>that I defined as Octave classes, using <i>classdef</i>. These layers can be concatenated in order to create a Layer object defining the architecture of the network. This will be the input for the training function. </li><li><b>Training</b>: the core of the project is the training function, which takes as input the data, the layers and some options and returns the network as output. </li><li><b>Network</b>:<b> </b>the network object has three methods (activations, classify and predict) that let the user compute the final classification and prediction. </li></ol><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="http://1.bp.blogspot.com/-ORu9UBCTgds/WT6aJVrPqMI/AAAAAAAAAYA/zv7BCHiKyFU9Z3qA_BtkjsInxBVzi8bRACK4B/s1600/structure.png"><img border="0" height="400" src="https://1.bp.blogspot.com/-ORu9UBCTgds/WT6aJVrPqMI/AAAAAAAAAYA/zv7BCHiKyFU9Z3qA_BtkjsInxBVzi8bRACK4B/s400/structure.png" width="380" /></a></td></tr><tr><td class="tr-caption">Figure 1: conv nnet flowchart </td></tr></tbody></table><div><br /></div><div><br /></div><div>I have already implemented a draft for the first point, the layers classes [1]. Every layer type inherits some attributes and methods from the parent class <i>Layers</i>. This is useful for creating the <i>Layer</i> object: the concatenation of different layers is always a Layer object that will be used as input for the training function. For this purpose, I overloaded the cat, horzcat and vertcat operators for <i>Layers</i> and subsref for <i>Layer</i>. I need to finalize some details for the disp methods of these classes.</div><div><br /></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="http://1.bp.blogspot.com/-NWlLJjVz9ZM/WT8E2gwlp9I/AAAAAAAAAYo/3dGVBOhyCHEsxChBkz_A-VafTHcPIt72gCK4B/s1600/Schermata%2B2017-06-12%2Balle%2B23.16.39.png"><img border="0" height="396" src="https://1.bp.blogspot.com/-NWlLJjVz9ZM/WT8E2gwlp9I/AAAAAAAAAYo/3dGVBOhyCHEsxChBkz_A-VafTHcPIt72gCK4B/s640/Schermata%2B2017-06-12%2Balle%2B23.16.39.png" width="640" /></a></td></tr><tr><td class="tr-caption"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td class="tr-caption">Figure 2: Layers classes definitions </td></tr></tbody></table></td></tr></tbody></table><i><br />InputParser</i> is used in every class for the parameter management and the attributes setting.</div><div><br /></div><div>The objects of these classes can be instantiated with a corresponding function, implemented in the directory <i>inst/</i>. Here an example for creating a Layer object </div><div><br /></div><div><span><br /></span></div><span>> # TEST LAYERS<br />> a = imageInputLayer([2,2,3]); # first layer<br />> b = convolution2dLayer(1,1); # second layer<br />> c = dropoutLayer(1); # third layer<br />> layers = [a b c]; # Layer object from layers concat <br />> drop = layers(3); # Layer element access<br />> drop.Probability # Access layer attribute<br />ans = 0.50000</span><br /><div><br /><div>All functions can be tested with the <i>make check</i> of the package.<br /><br /><br />The next step is a focus on the Tensorflow integration, with Pytave, writing a complete test for a regression of images angles and comparing the precision and the computational time with Matlab.<br /><br />[1] <a href="https://bitbucket.org/cittiberto/octave-nnet/src/35e4df2a6f887582216fc266fc75976a816f04fc/inst/classes/?at=default">https://bitbucket.org/cittiberto/octave-nnet/src/35e4df2a6f887582216fc266fc75976a816f04fc/inst/classes/?at=default</a></div><div><br /></div><div><br /></div>p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #f4f4f4; background-color: #000000; background-color: rgba(0, 0, 0, 0.95)} span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #f4f4f4; background-color: #000000; background-color: rgba(0, 0, 0, 0.95)} span.s1 {font-variant-ligatures: no-common-ligatures} </div>Enrico Bertinonoreply@blogger.comhttps://gsocnnet.blogspot.com/Train a Convolutional Neural Network for Regressiontag:blogger.com,1999:blog-3202743535685562583.post-71344106571759305752017-06-24T14:36:53+00:00Hello,<br /><br />I spent the last period working mostly on Tensorflow, studying the APIs and writing some examples in order to explore the possible implementations of neural networks. For this goal, I chose an interesting example proposed in the Matlab examples at [1]. The dataset is composed by 5000 images, rotated by an angle α, and a corresponding integer label (the rotation angle α). The goal is to make a regression to predict the angle of a rotated image and straighten it up.<br />All files can be found in <i>tests/examples/cnn_linear_model</i> in my repo [2].<br /><div><br /></div>I have kept the structure as in the Matlab example, but I generated a new dataset starting from LeCun's MNIST digits (datasets at [3]). Each image was rotated by a random angle between 0° and 70°, in order to keep the right orientation of the digits (code in <i>dataset_generation.m</i>). In Fig. 1 some rotated digits with the corresponding original digits.<br /><div class="separator"><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://4.bp.blogspot.com/-13SixYY4WOw/WU13nxmpXrI/AAAAAAAAAac/LaomtxB_lFAaD5MjxbIHDshqJFRy60ePgCEwYBhgL/s1600/test.png"><img border="0" height="301" src="https://4.bp.blogspot.com/-13SixYY4WOw/WU13nxmpXrI/AAAAAAAAAac/LaomtxB_lFAaD5MjxbIHDshqJFRy60ePgCEwYBhgL/s320/test.png" width="320" /></a></td></tr><tr><td class="tr-caption"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td class="tr-caption">Figure 1. rotated images in columns 1,3,5 and originals in columns 2,4,6</td></tr></tbody></table></td></tr></tbody></table><div><br />The implemented linear model is:</div><div>$ \hat{Y} = \omega X + b $,</div><div>where the weights $\omega$ and the bias $b$ will be optimized during the training minimizing a loss function. As loss function, I used the mean square error (MSE):</div><div><div>$ \dfrac{1}{n} \sum_{i=1}^n (\hat{Y_i} - Y_i)^2 $,</div><div>where the $Y_i$ are the training labels. </div></div><div><br /></div><div>In order to show the effective improvement given by a Neural Network, I started to make a simple regression feeding the X variable of the model directly with the 28x28 images. Even if for the MSE minimization a close form exists, I implemented an iterative method for discovering some Tensorflow features (code in <i>regression.py</i>). For evaluate the accuracy of the regression, I consider a correct regression if the difference between angles is less than 20°. After 20 epochs, the convergence was almost reached, giving an accuracy of $0.6146$.</div><div><br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://2.bp.blogspot.com/-knkDf4WvOP0/WU13zm1aBnI/AAAAAAAAAag/tr95xOU3ETsb2kxOCOMQehx77o1JcWFtgCLcBGAs/s1600/regr.png"><img border="0" height="310" src="https://2.bp.blogspot.com/-knkDf4WvOP0/WU13zm1aBnI/AAAAAAAAAag/tr95xOU3ETsb2kxOCOMQehx77o1JcWFtgCLcBGAs/s320/regr.png" width="320" /></a></td></tr><tr><td class="tr-caption">Figure 2. rotated images in columns 1,3,5 and after the regression in columns 2,4,6</td></tr></tbody></table><br />I want to analyze now the improvement given by a feature extraction performed with a convolutional neural network (CNN). As in the Matlab example, I used a basic CNN since the input images are quite simple (only numbers with monochromatic background) and consequently the features to extract are few.</div><div></div><div><ul><li>INPUT [28x28x1] will hold the raw pixel values of the image, in this case an image of width 28, height 28</li><li>CONV layer will compute the output of neurons that are connected to local regions in the input, each computing a dot product between their weights and a small region they are connected to in the input volume. This results in volume such as [12x12x25]: 25 filters of size 12x12</li><li>RELU layer will apply an element-wise activation function, such as the $max(0,x)$ thresholding at zero. This leaves the size of the volume unchanged ([12x12x25]).</li><li>FC (i.e. fully-connected) layer will compute the class scores, resulting in volume of size [1x1x1], which corresponds to the rotation angle. As with ordinary Neural Networks, each neuron in this layer will be connected to all the numbers in the previous volume.</li></ul></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://3.bp.blogspot.com/-d_-ilTzooso/WUrVnkRO9JI/AAAAAAAAAZ0/4-K68HW0imkQ_W-olPIKr9FKoi4zRBGgACLcBGAs/s1600/regression_scheme.png"><img border="0" height="320" src="https://3.bp.blogspot.com/-d_-ilTzooso/WUrVnkRO9JI/AAAAAAAAAZ0/4-K68HW0imkQ_W-olPIKr9FKoi4zRBGgACLcBGAs/s640/regression_scheme.png" width="640" /></a></td></tr><tr><td class="tr-caption">Figure 3. CNN linear model architecture</td></tr></tbody></table><div><br />We can visualize the architecture with Tensorboard where the graph of the model is represented.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://4.bp.blogspot.com/-H6ZcTGQpaP4/WU2JQX24gXI/AAAAAAAAAbA/ZWEh2_d4gxoO4-MWaV3Pflk67nBxhdrzACLcBGAs/s1600/tensorboard.png"><img border="0" height="510" src="https://4.bp.blogspot.com/-H6ZcTGQpaP4/WU2JQX24gXI/AAAAAAAAAbA/ZWEh2_d4gxoO4-MWaV3Pflk67nBxhdrzACLcBGAs/s640/tensorboard.png" width="640" /></a></td></tr><tr><td class="tr-caption">Figure 4. Model graph generated with Tensorboard</td></tr></tbody></table><br /></div><div>With the implementation in <i>regression_w_CNN.py</i>, the results are quite satisfying: after 15 epochs, it reached an accuracy of $0.75$ (205 seconds overall). One can see in Fig. 4 the marked improvement of the regression.</div><div><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td><a href="https://4.bp.blogspot.com/-MylUgRZvGQM/WU13_Rsm-VI/AAAAAAAAAak/jo6aUeXJ1ik3dYgpVvjn6E3iq0TdxmsxQCLcBGAs/s1600/conv.png"><img border="0" height="298" src="https://4.bp.blogspot.com/-MylUgRZvGQM/WU13_Rsm-VI/AAAAAAAAAak/jo6aUeXJ1ik3dYgpVvjn6E3iq0TdxmsxQCLcBGAs/s320/conv.png" width="320" /></a></td></tr><tr><td class="tr-caption"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container"><tbody><tr><td class="tr-caption">Figure 5. <span>rotated images in columns 1,3,5 and after the CNN regression in columns 2,4,6</span><br /><div><span><br /></span></div></td></tr></tbody></table></td></tr></tbody></table>With the same parameters, Matlab reached an accuracy of $0.76$ in 370 seconds (code in <i>regression_Matlab_nnet.m</i>), so the performances are quite promising<br /><br />In the next post (in few days), I will integrate the work done up to now, calling the Python class within Octave and making a function that simulates the behavior of Matlab. Leveraging the layers classes that I made 2 weeks ago, I will implement a draft of the functions <i>trainNetwork</i> and <i>predict</i> making the Matlab script callable also in Octave.<br /><br />I will also care about the dependencies of the package: I will add the dependency from Pytave in the package description and write a test as PKG_ADD in order to verify the version of Tensorflow during the installation of the package. <br /><br />Peace,<br />Enrico</div><div><br />[1] <a href="https://it.mathworks.com/help/nnet/examples/train-a-convolutional-neural-network-for-regression.html" target="_blank">https://it.mathworks.com/help/nnet/examples/train-a-convolutional-neural-network-for-regression.html</a><br />[2] <a href="https://bitbucket.org/cittiberto/octave-nnet/src/35e4df2a6f887582216fc266fc75976a816f04fc/tests/examples/cnn_linear_model/?at=default" target="_blank">https://bitbucket.org/cittiberto/octave-nnet/src/35e4df2a6f887582216fc266fc75976a816f04fc/tests/examples/cnn_linear_model/?at=default</a></div>[3] <a href="http://yann.lecun.com/exdb/mnist/" target="_blank">http://yann.lecun.com/exdb/mnist/</a><br /><br />Enrico Bertinonoreply@blogger.comhttps://gsocnnet.blogspot.com/Vectorization and broadcastingtag:blogger.com,1999:blog-3508201418641099808.post-30092066601123350102017-06-22T17:12:44+00:00At the moment I'm actually ahead of my <a href="https://gsocinterval.blogspot.se/2017/05/timeline.html">schedule</a> and this week I started to work on support for vectorization on N-dimensional arrays. The by far biggest challenge was to implement proper broadcasting and most of this post will be devoted to going through that. At the end I also mention some of the other things I have done during the week.<br /><br /><h2>Broadcasting arrays</h2>At the moment I have implement support for broadcasting on all binary functions. Since all binary functions behave similarly in respect to broadcasting I will use $+$ in all my example below, but this could in principle be any binary function working on intervals.<br /><br />When adding to arrays, $A, B$, of the same size the result is just an arrays of the same size with each entry containing the sum of the corresponding entries in $A$ and $B$. If $A$ and $B$ does not have the same size then we try to perform broadcasting. The simplest form of broadcasting is when $A$ is an arrays and $B$ is a scalar. Then we just take the value of $B$ and add to every element in $A$. For example<br /><span><br />> A = infsupdec ([1, 2; 3, 4])<br />A = 2×2 interval matrix<br /> [1]_com [2]_com<br /> [3]_com [4]_com<br />> B = infsupdec (5)<br />B = [5]_com<br />> A + B<br />ans = 2×2 interval matrix<br /> [6]_com [7]_com<br /> [8]_com [9]_com<br /></span><br />However it is not only when one of the inputs is a scalar that broadcasting can be performed. Broadcasting is performed separately for each dimension of the input. We require either that the dimensions are equal, and no broadcasting is performed, or that one of the inputs have that dimension equal to $1$, we then concatenate this input along that dimension until they are of equal size. If for example $A$ has dimension $4\times4\times4$ and $B$ dimension $4\times4\times1$ we concatenate $B$ with itself along the third dimension four times to get two arrays of the same size. Since a scalar has all dimensions equal to 1 we see that it can be broadcasted to any size. Both $A$ and $B$ can also be broadcasted at the same time, along different dimensions, for example<br /><span><br />> A = infsupdec (ones (1, 5, 2))<br />A = 1×5×2 interval array<br />ans(:,:,1) =<br /> [1]_com [1]_com [1]_com [1]_com [1]_com<br />ans(:,:,2) =<br /> [1]_com [1]_com [1]_com [1]_com [1]_com<br />> B = infsupdec ([1, 2, 3, 4, 5; 6, 7, 8, 9, 10])<br />B = 2×5 interval matrix<br /> [1]_com [2]_com [3]_com [4]_com [5]_com<br /> [6]_com [7]_com [8]_com [9]_com [10]_com<br />> A + B<br />ans = 2×5×2 interval array<br />ans(:,:,1) =<br /> [2]_com [3]_com [4]_com [5]_com [6]_com<br /> [7]_com [8]_com [9]_com [10]_com [11]_com<br />ans(:,:,2) =<br /> [2]_com [3]_com [4]_com [5]_com [6]_com<br /> [7]_com [8]_com [9]_com [10]_com [11]_com<br /></span><br /><h3>The implementation</h3>I'll go through a little bit about my implementation. I warn you that I'm not that familiar with the internals of Octave so some things I say might be wrong, or at least not totally correct.<br /><br />Internally all, numerical, arrays are stored as a linear vector and the dimensions are only metadata. This means that the most efficient way to walk through an array is with a linearly increasing index. When $A$ and $B$ have the same size the most efficient way to sum them is to linearly go through the arrays. In pseudo code<br /><span><br />// Calculate C = A + B<br />for (int i = 0; i < numel (A); i++) {<br /> C(i) = A(i) + B(i);<br />}<br /></span><br />This works fine, and apart from unrolling the loop or doing optimizations like that it is probably the most efficient way to do it.<br /><br />If $A$ and $B$ are not of the same size then one way to do it would be to simply extend $A$ or/and $B$ along the needed dimensions. This would however require coping a lot of data, something we want to avoid (memory access is expensive). Instead we try to be smart with our indexing to access the right data from both $A$ and $B$.<br /><br />After asking on the IRC-channel I got pointed to <a href="http://hg.savannah.gnu.org/hgweb/octave/file/be69ea3de7a3/liboctave/numeric/bsxfun-defs.cc#l37">this</a> Octave function which performs broadcasting. My implementation, which can be found <a href="https://sourceforge.net/u/urathai/octave/ci/default/tree/src/mpfr_function_d.cc#l63">here</a>, is heavily inspired by that function.<br /><h3>Performance</h3>Here I compare the performance of the new implementation with the old one. Since the old one could only handle matrices we are limited by that. We can measure the time it takes to add two matrices $A$, $B$ with the code<br /><span><br />tic; A + B; toc;<br /></span><br />We do 10 runs for each test and all times are in seconds. <br /><h4>Addition of large matrices</h4>Case 1: A = B = infsupdec (ones (1000, 1000));<br /> Old New<br /> 0.324722 0.277179<br /> 0.320914 0.276116<br /> 0.322018 0.276075<br /> 0.318713 0.279258<br /> 0.332041 0.279593<br /> 0.318429 0.279987<br /> 0.323752 0.279089<br /> 0.317823 0.276036<br /> 0.320509 0.280964<br /> 0.320610 0.281123<br />Mean: 0.32195 0.27854<br />Case 2: A = B = infsupdec (ones (10, 100000));<br /> Old New<br /> 0.299321 0.272691<br /> 0.297020 0.282591<br /> 0.296460 0.274298<br /> 0.294541 0.279661<br /> 0.298306 0.277274<br /> 0.301532 0.275531<br /> 0.298163 0.278576<br /> 0.298954 0.279868<br /> 0.302849 0.275991<br /> 0.297765 0.278806<br />Mean: 0.29849 0.27753<br /><br />Case 3: A = B = infsupdec (ones (100000, 10));<br /> Old New<br /> 0.286433 0.279107<br /> 0.289503 0.278251<br /> 0.297562 0.279579<br /> 0.292759 0.283311<br /> 0.292983 0.281306<br /> 0.290947 0.282310<br /> 0.293025 0.286172<br /> 0.294153 0.278886<br /> 0.293457 0.278625<br /> 0.296661 0.280804<br />Mean: 0.29275 0.28084 <br /><h4>Broadcasting scalars</h4>Case 4: A = infsupdec (ones (1000, 1000));<br /> B = infsupdec (1);<br /> Old New<br /> 0.298695 0.292419<br /> 0.298158 0.292274<br /> 0.305242 0.296036<br /> 0.295867 0.291311<br /> 0.296971 0.297255<br /> 0.304297 0.292871<br /> 0.298172 0.300329<br /> 0.297251 0.291668<br /> 0.299236 0.294128<br /> 0.300457 0.298005<br />Mean; 0.29943 0.29463 <br /><br />Case 5: A = infsupdec (1);<br /> B = infsupdec (ones (1000, 1000));<br /> Old New<br /> 0.317276 0.291100<br /> 0.316858 0.296519<br /> 0.316617 0.292958<br /> 0.316159 0.299662<br /> 0.317939 0.301558<br /> 0.322162 0.295338<br /> 0.321277 0.293561<br /> 0.314640 0.291500<br /> 0.317211 0.295487<br /> 0.317177 0.294376<br />Mean: 0.31773 0.29521<br /><h4>Broadcasting vectors</h4>Case 6: A = infsupdec (ones (1000, 1000));<br /> B = infsupdec (ones (1000, 1));<br /> Old New<br /> 0.299546 0.284229<br /> 0.301177 0.284458<br /> 0.300725 0.276269<br /> 0.299368 0.276957<br /> 0.303953 0.278034<br /> 0.300894 0.275058<br /> 0.301776 0.276692<br /> 0.302462 0.282946<br /> 0.304010 0.275573<br /> 0.301196 0.273109<br />Mean: 0.30151 0.27833 <br /><br />Case 7: A = infsupdec (ones (1000, 1000));<br /> B = infsupdec (ones (1, 1000)); <br /> Old New<br /> 0.300554 0.295892<br /> 0.301361 0.294287<br /> 0.302575 0.299116<br /> 0.304808 0.294184<br /> 0.306700 0.291606<br /> 0.301233 0.298059<br /> 0.301591 0.292777<br /> 0.302998 0.290288<br /> 0.300452 0.291975<br /> 0.305531 0.290178<br />Mean: 0.30278 0.29384<br /><br />We see that in all cases the new version is faster or at least equally fast as the old version. In the old version the order of the input made a slight difference in performance (case 4 vs case 5). In the new version both inputs are treated in exactly the same way so we no longer see that difference.<br /><h3>Possible improvements</h3>In theory the cases when we broadcast a scalar could be the fastest ones. If $B$ is a scalar we could, in pseudo code, do something similar to<br /><span>// Calculate C = A + B with B scalar<br />for (int i = 0; i < numel (A); i++) {<br /> C(i) = A(i) + B;<br />} </span><br />This is however not implemented at the moment. Instead we use the ordinary routine to calculate the index for $B$ (since it is a scalar it will always evaluate to $1$). If we would like to optimize more for this case we could add a check for if $A$ or $B$ are scalars and then optimize for that. Of course this would also make the code more complicated, something to watch out for. At the moment I leave it like this but if we later want to optimize for that case it could be done.<br /><h2>Other work</h2>Apart from the work to fix the broadcasting for binary functions there were very little to do for many of the functions. All binary functions that use this code, and all unary functions using an even simpler code, worked directly after fixing the oct-files. Some of them required small changes to the documentation but other than that the octave-scripts were fine. So mainly it has been a matter of actually going through all files and check that they actually did work.<br /><h3>Bug #51283</h3>When going through all the functions I noticed a bug in the interval version of $\sin$,<br /><span><br /> > sin (infsupdec (0))<br />ans = [0]_com<br />> sin (infsupdec ([0, 0]))<br />ans = 1×2 interval vector<br /> [0, -0]_com [0, -0]_com<br /></span><br />The second version here is wrong, $-0$ should never be allowed as a value for the supremum of an interval. I was able to track this down to how Octaves $\max$ function works, see <a href="https://savannah.gnu.org/bugs/index.php?51283">bug #51283</a>. As Oliver writes there the exact behaviour of the $\max$-function is not specified in <span id="hidsubpartcontentdiscussion">IEEE Std 754-2008 so we cannot rely on that. To solve this I have added a line to manually set all $-0$ to $+0$ in the supremum of the interval.</span><br /><h3> </h3>Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/Timetable: modificationtag:blogger.com,1999:blog-8509760576772091576.post-70864628587510616652017-06-20T07:01:37+00:00Timetable: modification <h1>Timetable: modification</h1>According to my timetable (that you can find <a href="http://gsocspecfun.blogspot.it/2017/05/mathjax.html">here</a>), during this last week of June, I should've work on <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=34405">the input validation of betainc</a>. Since a <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=51157">new bug</a> related to this function has been found and, moreover, the actual implementation doesn't accept the "lower" or "upper" tail (as MATLAB do), me and my mentor decided to use this week to start studying how to rewrite betainc (main references will be [1] and [2]) and to use the last part fo the GSoC to actually implement it. In this way, my timetable remain almost identical (I will use July to work on Bessel functions) and I will be able to fix also this problem. <br /><br />[1] Abramowitz, Stegun "Handbook of Mathematical Functions"<br />[2] Cuyt, Brevik Petersen, Vendonk, Waadeland "Handbook of Continued Fractions for Special Functions"Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/Construction and Printingtag:blogger.com,1999:blog-3508201418641099808.post-19310234811192722872017-06-16T14:07:55+00:00This week I have started to work on methods for constructing and printing N-dimensional arrays of intervals. In my timeline I estimated that this work would take 2 weeks. However in this first week I have managed to complete most of the work. I will give some comments on how I have worked with the Mercurial repository, how the work went and different things I encountered along the path.<br /><br /><h2>Working with Mercurial</h2>This is essentially the first time I'm using Mercurial for revision control, though I have used git before. However I quickly learned how to use it for the basic tasks that I need, committing, comparing files and checking the history. As mentioned in a previous post you can find my repository here [1].<br /><h3>Coding style</h3>When I started to work with the files I realized that they did not follow Octaves coding standard [2]. After a short discussion on the mailing list we decided that I will update the files I change to follow the standard coding style. Usually it is not a good idea to change coding style and add functionality in the same commit. However most of the changes to coding style are only white space changes so they can be ignored using the -w flag in Mercurial. Thus we decided that as long as the coding style changes are only such that it is ignored with -w I will do it in the same commit as the added functionality. If there are some coding style changes that's not only white space, the most common example is to long lines, I do a commit with only changes to the coding style first. So if you want to take a look at the functionality I have added you will probably want to use the -w flag. Note however that I have not updated the coding style for any files I have not changed otherwise.<br /><h3>Committing</h3>Normally I do one commit for each file, though in many cases the bare intervals and the decorated intervals have almost identical functions and in that case I commit changes to them both at the same time. Of course it also happens that I have to go back and do more changes to a files, in that case I just do another commit.<br /><h2>The actual work</h2>The work went much faster than I expected. The main reason for this is that Octave has very good support for indexing. For example expressions like<br /><br /><span>isnai(x.inf <= x.sup) = false; <br /></span><br />works just as well for matrices as for N-dimensional arrays. In fact the constructor for bare intervals even worked for N-dimensional arrays from the beginning, there I only had to do slight modification to the documentation and add some tests! <br /><br />Not all functions were that easy though. Some functions that have not been updated in a while clearly assumed the input was a matrix, for example in $hull$<br /><br /><span>sizes1 = cellfun ("size", l, 1);<br />sizes2 = cellfun ("size", l, 2); </span><br /><br />In most cases I only needed to add more general indexing, often times even making the code clearer.<br /><br />In some functions all I had to do was to remove the check on the input data so that it would accept N-dimensional arrays. This was true in for example $cat$ were all I had to do was to remove the check and do some minor modifications to the documentation.<br /><br />I can conclude with saying that Octave has great support for working with N-dimensional arrays. Since internally the data for intervals are stored only as arrays I could make good use of it!<br /><h2>Noteworthy things</h2>While most functions were straight forward to modify some required some thought. How should they even work for N-dimensional input?<br /><h3>Disp</h3>When modifying the $disp$-function I chose to mimic how Octave handles displaying N-dimensional arrays. I noticed that this is different from how Matlab handles it. In Matlab we have<br /><span><br />> x = zeros (2, 2, 2)<br /><br />x(:,:,1) =<br /><br /> 0 0<br /> 0 0<br /><br /><br />x(:,:,2) =<br /><br /> 0 0<br /> 0 0<br /><br /></span><br />while in Octave it's<br /><span><br />> x = zeros (2, 2, 2)<br />x =<br /><br />ans(:,:,1) =<br /><br /> 0 0<br /> 0 0<br /><br />ans(:,:,2) =<br /><br /> 0 0<br /> 0 0<br /><br /></span><br />I don't know the choice behind Octaves version. At least at first glance I think I prefer the way Matlab does it. But since I'm working in Octave I chose that style.<br /><br />The next question was how to handle the subset symbol, $\subset$. The interval package uses $=$ or $\subset$ depending on if the string representation is exact or not. For example<br /><span><br />> x = infsup (1/2048, 1 + 1/2048);<br />> format short; x<br />x ⊂ [0.00048828, 1.0005]<br />> format long; x<br />x = [0.00048828125, 1.00048828125]<br /></span><br />How should this be handled for N-dimensional arrays? One way would be to switch all $=$ to $\subset$ is the representation is not exact. Another to use $\subset$ on all submatrices that does not have an exact string representation. The third way, and how it is implemented now, is to only change the first $=$ to $\subset$, the one after the variable name. Like this<br /><span><br />> x(1,1,1:2) = infsup (1/2048, 1 + 1/2048)<br />x ⊂ 1×1×2 interval array<br /><br />ans(:,:,1) = [0.00048828, 1.0005]<br />ans(:,:,2) = [0.00048828, 1.0005]<br /><br /></span><br />This might be a bit odd when you first look at it, on some places we use $=$ and on some $\subset$. Though I think it somehow makes sense, we are saying that $x$ is a subset of the $1\times1\times2$ interval array given by<br /><span><br />ans(:,:,1) = [0.00048828, 1.0005]<br />ans(:,:,2) = [0.00048828, 1.0005]<br /></span><br />which actually is true. Anyway I will leave like this for now and then we might decide to switch it up later.<br /><h3>linspace and mince</h3>The standard implementation of $linspace$ only supports scalar or vector input. It could be generalized to N-dimensional arrays by for example returning a N+1-dimensional array were the last dimension corresponds to the linearly spaced elements. But since this has not been done in the standard implementation I will at least wait with adding for intervals.<br /><br />The function $mince$ can be seen as a interval generalization of $linspace$. It takes an interval and returns an array of intervals whose union cover it. This could similarly be expanded to N dimensions by creating the array along the N+1 dimension. But again we choose to at least wait with adding this.<br /><h3>meshgrid and ndgrid</h3>The interval package already has an implementation of $meshgrid$. But since it previously did not support 3-dimensional arrays it had to fit 3-d data in a 2-d matrix. Now that it supports 3-d data it can output that instead.<br /><br />Currently the interval package does not implement $ndgrid$. When I looked into it I realized that the standard implementation of $ndgrid$ actually works for interval arrays as well. I have not looked into the internals but in principle it should only need the $cat$ function, which is implemented for intervals. Further I noticed that the standard $meshgrid$ also works for intervals. However the interval implementation differs in that it converts all input to intervals, were as the standard implementation allows for non-uniform output. Using the interval implementation of $meshgrid$ we have<br /><br /><span>> [X Y] = meshgrid (infsup (1:3), 4:6)<br />X = 3×3 interval matrix<br /><br /> [1] [2] [3]<br /> [1] [2] [3]<br /> [1] [2] [3]<br /><br />Y = 3×3 interval matrix<br /><br /> [4] [4] [4]<br /> [5] [5] [5]<br /> [6] [6] [6]<br /></span><br />but if we fall back to the standard implementation (by removing the interval implementation) we get<br /><span><br />> [X Y] = meshgrid (infsup (1:3), 4:6)<br />X = 3×3 interval matrix<br /><br /> [1] [2] [3]<br /> [1] [2] [3]<br /> [1] [2] [3]<br /><br />Y =<br /><br /> 4 4 4<br /> 5 5 5<br /> 6 6 6<br /></span><br />Note that the last matrix is not an interval matrix.<br /><br />So the question is, should we implement a version of $ndgrid$ that converts everything to intervals or should we remove the implementation of $meshgrid$? It's at least most likely not a good idea that the functions are different. I think that removing the implementation of $meshgrid$ makes most sense. First of all it's less code to maintain, which is always nice. Secondly you can manually convert all input to the function to intervals if you want uniform output. If you do not want uniform output then the standard implementation works were as the interval implementation does not, so the standard implementation is more general in a sense.<br /><br />We have to choose what to do, but for now I leave it as it is.<br /><h3>Non-generalizable functions</h3>From what I have found there is no way to create a 3-dimensional array in Octave in the same way you can create a 2-dimensional one with for example<br /><span><br />M = [1, 2; 3, 4];<br /></span><br />Instead higher dimensional arrays have to be created using other functions, for example $reshape$ or $zeros$, or by specifying the submatrices directly<br /><span><br />M(:,:,1) = [1, 2; 3, 4];<br />M(:,:,2) = [5, 6; 7, 8];<br /></span><br />This means that the functions $\_\_split\_interval\_literals\_\_$, which is used to split a string like $"[1, 2; 3, 4]"$ into its separate components, cannot really be generalized to N dimensions.<br /><br /><br />[1] <a href="https://sourceforge.net/u/urathai/octave/ci/default/tree/">https://sourceforge.net/u/urathai/octave/ci/default/tree/</a> <br />[2] <a href="http://wiki.octave.org/Octave_style_guide">http://wiki.octave.org/Octave_style_guide</a>Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/gammaincinvtag:blogger.com,1999:blog-8509760576772091576.post-29500474263535409612017-06-16T10:39:13+00:00The gammaincinv function<h1>The <i> inverse of the incomplete gamma</i> function</h1><h2>Definition</h2>Given two real values $y\in[0,1]$ and $a>0$, gammaincinv(y,a,tail) gives the value $x$ such that \[P(x,a) = y,\quad\text{ or }\quad Q(x,a) = y\] depending on the value of tail ("lower" or "upper"). <h2>Computation</h2>The computation is carried out via standard Newton method, solving, for $x$ \[ y - P(x,a) = 0 \quad\text{ or }\quad y - Q(x,a) = 0. \] The Newton method uses as exit criterion a control on the residual and on the maximum number of iterations.<br />For numerical stability, the initial guess and which gammainc function is inverted depends on the values of $y$ and $a$. We use the following property: \[\text{gammaincinv}(y,a,\text{`upper'})=\text{gammaincinv}(1-y,a,\text{`lower'})\] which follows immediately by the property \[P(x,a) + Q(x,a) = 1.\] We start computing the trivial values, i.e. $a=1,y=0$ and $y=1$. Then we rename the variables as follows (in order to obtain a more classical notation): if tail = "lower", then $p=y,q=1-y$; while if tail = "upper", $q=y,p=1-y$. We will invert with $p$ as $y$ in the cases in which we invert the lower incomplete gamma function, and with $q$ as $y$ in the cases in which we invert the upper one.<br />Now, the initial guesses (and the relative version of the incomplete gamma function to invert) are the following: <ol><li>$p\dfrac{(0.2(1+a))^a}{\Gamma(1+a)}$. We define \[r = (p\,\Gamma(1+a))^{\frac{1}{a}}\] and the parameters \begin{align*} c_2 &= \dfrac{1}{a+1}\\ c_3 &= \dfrac{3a+5}{2(a+1)^2(a+2)}\\ c_4 &= \dfrac{8a^2+33a+31}{3(a+1)^3(a+2)(a+3)}\\ c_5 &= \dfrac{125a^4+1179a^3+3971a^2+5661a+2888}{24(1+a)^4(a+2)^2(a+3)(a+4)}\\ \end{align*} Then the initial guess is \[x_0 = r + c_2\,r^2+c_3\,r^3+c_4\,r^4+c_5\,r^5\] and we invert the lower incomplete gamma function. </li> <li>$q\dfrac{e^{-a/2}}{\Gamma(a+1)}, a\in(0,10)$. The initial guess is \[x_0 = - \log (q) - \log(\Gamma(a))\] and we invert the upper incomplete gamma function. </li> <li>$a\in(0,1)$. The initial guess is \[x_0 = (p\Gamma(a+1))^{\frac{1}{a}}\] and we invert the lower incomplete gamma function. </li> <li>Other cases. The initial guess is \[x_0 = a\,t^3\] where \[t = 1-d-\sqrt{d}\,\text{norminv}(q),\quad d=\dfrac{1}{9a}\] (where norminv is the inverse of the normal distribution function) and we invert the upper incomplete gamma function. </li></ol>The first three initial guesses can be found in the article "Efficient and accurate algorithms for the computation and inversion of the incomplete gamma function rations" by Gil, Segura ad Temme; while the last one can be found in the documentation of the function igami.c of the cephes library.<br />You can find my work on my repository on bitbucket <a href="https://bitbucket.org/M_Ginesi/octave">here</a>, bookmark "gammainc". <h2>Comments</h2>Working on this function I found some problems in gammainc. To solve them, I just changed the flag for the condition 5 and added separate functions to treat Inf cases.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/gammainctag:blogger.com,1999:blog-8509760576772091576.post-51420117513781639742017-06-06T09:09:56+00:00The gammainc function<h1>The <i>incomplete gamma</i> function</h1><h2>Definition</h2>There are actually four types of incomplete gamma functions: <ul><li><i>lower</i> (which is the default one) $$P(a,x) = \frac{1}{\Gamma(a)}\int_{0}^{x}e^{-t}t^{a-1}$$ </li><li><i>upper</i>$$Q(a,x) = \frac{1}{\Gamma(a)}\int_{x}^{+\infty}t^{a-1}e^{-t}dt$$ </li><li><i>scaled lower</i> and <i>scaled upper</i> which return the corresponding incomplete gamma function scaled by $$\Gamma(a+1)\frac{e^x}{x^a}$$ </li></ul><h2>Computation</h2>To compute the incomplete gamma function, there is a total of seven strategies depending on the position of the arguments in the $x-a$ plane. <ol> <li> $x=0,a=0$. In this case, the output is given as follows: <ul> <li> for "lower" and "scaledlower" is $1$ </li> <li> for "upper" and "scaledupper" is $0$ </li> </ul> </li> <li> $x=0,a\neq0$. In this case: <ul> <li> for "lower" is $0$ </li> <li> for "upper" and "scalelower" is $1$ </li> <li> for "scaledupper" is $+\infty$ </li> </ul> </li> <li> $x\neq0,a=0$. In this case: <ul> <li> for "lower" is $1$ </li> <li> for "scalelower" is $e^x$ </li> <li> for "upper" and "scaledupper" is $0$ </li> </ul> </li> <li> $x\neq0,a=1$. In this case: <ul> <li> for "lower" is $1-e^x$ </li> <li> for "upper" is $e^{-x}$ </li> <li> for "scaledlower" is $\frac{e^x-1}{x}$ </li> <li> for "scaledupper" is $\frac{1}{x}$ </li> </ul> </li> <li> $0 x\leq36,a\in\{2,3,4,\ldots,17,18\}$. In this case we used the following expansion (which holds true only for $n\in\mathbb{N}$): denote $\gamma(a,x):=\Gamma(a)P(a,x)$, then $$\gamma(n,x) = (n-1)!\left( 1-e^{-x}\sum_{k=0}^{n-1} \frac{x^k}{k!}\right).$$ </li> <li> $x+0.25 a $ or $x0$, or $x$ not real or $|x|1$ or $a5$. Here we used the expansion $$\gamma(a,x) = e^{-x} x^a \sum_{k=0}^{+\infty} \frac{\Gamma(a)}{\Gamma(a+1+k)}x^k.$$ </li> <li> for all the remaining cases was used a continued fraction expansion. Call $\Gamma(a,x) = \Gamma(a) Q(a,x)$, the expansion is $$\Gamma(a,x) = e^{-x}x^a\left( \frac{1}{x+1-a-}\frac{1(1-a)}{x+3-a-}\frac{2(2-a)}{x+5-a-}\cdots \right).$$ This case is handled by an external .cc function. </li></ol><h2>My contribution</h2>Actually I dind't participate from the start in the (re)making of this function: most of the work done on it is due to Nir and Marco (see <a href="https://savannah.gnu.org/bugs/?func=detailitem&item_id=47800">the discussion on the bug tracker</a>).<br />My contribution was, before the GSoC, to adapt the codes in such a way to make the type of the output (and the tolerances inside the algorithms) coherent on the type of the input (single, int or double). Then I corrected few small bugs and made a patch helped by Carne during the OctConf in Geneve.<br />During this first week of GSoC I fixed the input validation and added tests to this function, finding some problem in the implementation. Most of them depended on the fact that the continued fractions were used in cases in which they are not optimal. To solve these problems I changed the conditions to use the series expansion instead of the continued fractions.<br />Now gammainc works properly also with complex argument (for the $x$ variable), while Matlab doesn't accept non-real input.<br />You can find my work on my repository on bitbucket <a href="https://bitbucket.org/M_Ginesi/octave">here</a>, bookmark "gammainc". I will work on this same bookmark the next two weeks while I will work on gammaincinv.Michele Ginesinoreply@blogger.comhttp://gsocspecfun.blogspot.com/Function help text in scripts and command linehttp://users.ox.ac.uk/~bioc1289/post/2017/06/octave-help-text-in-scripts.html2017-06-06T00:00:00+00:00<div class="document">
<p>I guess I'm one of the few [[citation needed]] that writes Octave
programs instead of only things meant to run from the Octave console.
You end up with a few hundred lines of Octave and tens of functions in
a single file. For debugging you just source the whole thing.</p>
<p>I always thought that Octave's <tt class="docutils literal">help</tt> function only handled
functions in their own m files, the help text being the first or
second block of comments that does not start with Author or Copyright.
But I was wrong. You can document functions in the Octave command
line and you can also document functions in a script. Octave seems to
pick a block of comments before a function definition as that function
help text. Like so:</p>
<pre class="literal-block">
octave-cli:1> ## This is the help text of the foo function.
octave-cli:1> function x = foo (y, z), x = y+z; endfunction
octave-cli:2> ## This is the help text of the bar function.
octave-cli:2> ##
octave-cli:2> ## Multi-line help text are not really a problem,
octave-cli:2> ## just carry on.
octave-cli:2> function x = bar (y, z), x = y+z; endfunction
octave-cli:3> man foo
'foo' is a command-line function
This is the help text of the foo function.
octave-cli:4> man bar
'bar' is a command-line function
This is the help text of the bar function.
Multi-line help text are not really a problem,
just carry on.
</pre>
<p>It also works in script file with one exception: the first function of
the file which picks the first comment which will be the shebang
line:</p>
<pre class="literal-block">
$ cat > an-octave-script << END
> #!/usr/bin/env octave
>
> ## This is the help text of the foo function.
> function x = foo (y, z)
> x = y+z;
> endfunction
>
> ## This is the help text of the bar function.
> ##
> ## Multi-line help text are not really a problem,
> ## just carry on.
> function x = bar (y, z)
> x = y+z;
> endfunction
> END
$ octave-cli
octave-cli:1> source ("an-octave-script")
octave-cli:2> man bar
'bar' is a command-line function
This is the help text of the bar function.
Multi-line help text are not really a problem,
just carry on.
octave-cli:3> man foo
'foo' is a command-line function
!/usr/bin/env octave
</pre>
<p>I reported a bug about it
<a class="reference external" href="https://savannah.gnu.org/bugs/?51191">(bug #51191)
</a>
but I was
pleasantly surprised that functions help text were being identified at
all.</p>
</div>Carnë Draughttp://users.ox.ac.uk/~bioc1289/My Free software activities in May 2017http://users.ox.ac.uk/~bioc1289/post/2017/06/2017-05-foss-activities.html2017-06-01T00:00:00+00:00<div class="document">
<div class="section" id="octave">
<h1>Octave</h1>
<p>Bugs and patches:</p>
<ul class="simple">
<li>Fixed
<a class="reference external" href="https://savannah.gnu.org/bugs/?47115">(bug #47115)
</a>
where <tt class="docutils literal">label2rgb</tt> in the Image package
would not work with labelled images of type uint32 and uint64.
Reason was that behind the scenes <tt class="docutils literal">ind2rgb</tt> from the Octave core was
being used, and Octave imitates Matlab in that labelled images can
only be up to uint16. That sounds like a silly limitation so I
dropped it from core in <tt class="docutils literal">ind2rgb</tt> and <tt class="docutils literal">ind2gray</tt>. However, there's
other places where that limitation is in place and needs to be
lifted such as <tt class="docutils literal">cmunique</tt>, <tt class="docutils literal">cmpermute</tt>, and the display functions.</li>
<li>Fixed <tt class="docutils literal">hist3</tt> handling of special cases with NaN values on the
rows that define the histogram edges and centring values in an
histogram when there's only one value. Started as side effect of
fixing
<a class="reference external" href="https://savannah.gnu.org/bugs/?51059">(bug #51059)
</a>
.</li>
<li>
<a class="reference external" href="https://savannah.gnu.org/bugs/?44799">(bug #44799)
</a>
- document <tt class="docutils literal">imrotate</tt> does not work with the
spline method.</li>
<li>Fix input check for <tt class="docutils literal">fspecial</tt> <a class="reference external" href="http://hg.code.sf.net/p/octave/image/rev/e65762ee9414">e65762ee9414</a></li>
<li>Review <tt class="docutils literal">wiener2</tt>, function for adaptive noise reduction, from
Hartmut
and extended it for N dimensions.</li>
<li>Fixed <tt class="docutils literal">mean2</tt> Matlab incompatibilities for some weird cases
<a class="reference external" href="https://savannah.gnu.org/bugs/?51144">(bug #51144)
</a>
</li>
<li>Make <tt class="docutils literal">imcast</tt> support class logical.</li>
<li>Review <tt class="docutils literal">otsuthresh</tt>, function to compute threshold value of an
histogram via Otsu's method, by Avinoam Kalma
</li>
<li>Review affine2 and affine3, classes to represent 2d and 3d affine
transforms, submitted by Motherboard and Avinoam
. Started generalising code to N dimensions to avoid code
duplication. Only one method missing (maybe you wanna help?).</li>
</ul>
<p>As sysadmin:</p>
<ul class="simple">
<li>Finished updating the wiki for Octave, due to issues with pygments.</li>
</ul>
</div>
<div class="section" id="debian">
<h1>Debian</h1>
<ul class="simple">
<li>Started packaging libsystem-info-perl, got stuck due to a <a class="reference external" href="https://github.com/Tux/System-Info/issues/1">licensing
issue</a></li>
</ul>
</div>
</div>Carnë Draughttp://users.ox.ac.uk/~bioc1289/Blog Relaunch, More Pythonic With Pelicantag:blog.mtmxr.com,2017-05-31:/2017/05/31/blog-relaunch-more-pythonic-with-pelican2017-05-31T22:40:01+00:00<p>I just finished converting this blog to <a href="https://getpelican.com">Pelican</a>, a static
site generator built entirely on Python. The conversion process was so
easy, and Pelican is such a pleasure to work with, that I wish I had
done this much sooner instead of letting this blog languish for over a
year and a half.</p>
<p>To reboot the blog and kick things off again, I'll describe just how
easy it was to convert to Pelican, compare it to what I was working with
before, and lay out some ideas for the future.</p>
<h2>Starting with Pelican</h2>
<p>So Pelican is a static site generator, which means it builds a complete
website from a template and a bunch of files representing content. The
content is usually written in a plain text markup language like
<a href="https://daringfireball.net/projects/markdown">Markdown</a>. Pelican has explicit support for common blog
features like articles, pages, categories, comments, and syndication.
The blog was already written in Markdown and built using a static site
generator, and I wanted to try redoing it with Pelican.</p>
<p>Creating a Pelican site is simple, just run <code>pelican-quickstart</code>, fill
in some content, and run <code>pelican</code> to compile it. I only had to copy in
the existing Markdown content and massage the metadata headers into
Pelican's format. I also wanted to preserve the URL scheme I had already
been using, and found that it was trivial to tweak the default config
files to do exactly that. The result was a new site that mostly
replicated the content and URLs of the existing blog.</p>
<p>I noticed a small difference in the rules that Pelican uses for
transforming a page title into a URL. There is one post that contains a
"+" plus sign. My previous blog software converted this into the word
"plus", while Pelican drops it by default. Fortunately, it's simple to
add custom URL transformation rules, and I got this post where it needed
to be. If generic transformation rules don't work, Pelican also allows
an article to specify exactly what its URL should be in its metadata.</p>
<p>I also wanted to be able to add a delimiter in articles to mark the
beginning of the article as a summary. By default, Pelican pulls out the
first 50 words as the summary, probably cutting off in mid-sentence. I
wanted more control, without having to duplicate the summary in a header
field. Fortunately, there's a plugin for that. The
<a href="https://github.com/getpelican/pelican-plugins">pelican-plugins</a> repository has dozens of plugins,
including one that recognizes a delimiter comment marking the end of the
page summary.</p>
<p>At this point, setting aside appearance, the old blog was converted
perfectly, preserving all of the content, URLs, summaries, feeds, tags,
and comments. I had wanted to know if I could use Pelican to publish my
blog, and in about an hour I had answered that easily, and it was
basically ready to use. Of course the next question was, what else can I
do?</p>
<h2>Ditching Octopress</h2>
<p>I'll take a small detour here and try to explain why I did this
conversion in the first place.</p>
<p>When I started this blog three years ago, I wanted to use a static site
generator. I settled on <a href="http://octopress.org">Octopress</a> at the time because it
seemed simple and included all of the features I wanted. Octopress is
based on <a href="https://jekyllrb.com">Jekyll</a>, which was and is probably the most popular
static site generator. Octopress looked like Jekyll with a bunch of
integrated plugins and theming problems already solved. It looked like a
quick way to get started with Jekyll without having to learn Ruby and
mess around with CSS, just write and publish. And yes, it was pretty
easy.</p>
<p>One problem that was quickly apparent is that Octopress was basically a
preconfigured Jekyll project. To customize Octopress for your own blog,
you essentially have to fork Octopress and make changes to it. There was
no way to run upstream Octopress against your own configuration and
content.</p>
<p>Another problem that's a direct corollary, Octopress is based on a
now-ancient version of Jekyll. It's impossible to take advantage of any
Jekyll improvements over the years because Octopress used Jekyll 0.12
from 2012. If Octopress updates to a newer version, the only way to take
advantage would be to rebase your local fork onto Octopress upstream
changes and hope for the best.</p>
<p>Not only are configuration changes made in the Octopress repository
itself, but content is put there by default. So a typical Octopress blog
is a fork of the Octopress project, with local config files, <em>and</em> with
all articles and pages mixed in there too. I actually created a subrepo
for just the content, to try to maintain the content separately from the
Octopress repo. This seemed better at first, but I still had config
changes in one repo and content in the other, and they're not exactly
independent. Add in a customized fork of a theme, and that's three
separate repositories to maintain one simple blog.</p>
<p>I saw that Octopress had been working on a redesign to fix these
problems and work with the latest Jekyll, but it seems like development
has stalled. Maybe Jekyll is good enough now that Octopress isn't really
needed anymore? Maybe the authors moved on and the Octopress community
dissolved? Whatever the state of the project, I had a blog built with a
tool that was cumbersome to maintain, wasn't being updated, and I didn't
really understand. I stopped updating my blog due to a confluence of
many life events, and being difficult to maintain wasn't helping.</p>
<p>I was at PyCon 2017 last week, and one of the strongest messages that I
took with me was to try to find a Pythonic way to do everything. Since
Python can do so many things and make them so easy, why not do more
things in Python? I made a note to find and try out a best-practices
Pythonic static site generator to reboot my blog, and I found Pelican.
And here we are.</p>
<h2>Customizing Pelican</h2>
<p>Pelican feels extremely Pythonic to me, because it made it extremely
easy to build a replacement blog from scratch using sane defaults and
working with my existing Markdown. The default behavior is easy and
intuitive and makes something that just works. Now, beyond using the
defaults, customizing Pelican is done with Python modules, and there is
an active community building reusable plugins and themes for Pelican.</p>
<p>To add plugins and choose a theme, I only needed to point at the
<a href="https://github.com/getpelican/pelican-plugins">pelican-plugins</a> and <a href="https://github.com/getpelican/pelican-themes">pelican-themes</a>
repositories and add a little configuration. Most of the themes are
listed with screenshots and live demos at
<a href="http://www.pelicanthemes.com">pelicanthemes.com</a>. I was very happy to find the
<a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3">pelican-bootstrap3</a> theme because it is clean and
minimal, it's based on the widely used <a href="https://getbootstrap.com">Bootstrap</a> framework,
and it is extremely customizable. With just a little exploration of the
config space, I now have something that behaves and looks more like I
wanted the original blog to and less like the Pelican default theme.</p>
<p>I had to stop myself from customizing too much, write this post, and
update this blog after a long hiatus. It's functional and pretty much
does what I want for now, but there is still more customization to be
done. I would like to install a favicon. I want to get the built-in site
search feature working. I would like to look at the
<a href="https://bootswatch.com">Bootswatch</a> themes for Bootstrap that can easily be dropped
into this theme. I might add a tag cloud once I have some more content.</p>
<p>For now the most important thing is that the blog is up and running
again. I hope the switch to Pelican will lower the barrier to updating a
bit and help keep things moving. Sometimes the right tools make the
difference. I'm also curious what else Pelican can be used for. Are
there any downsides that I haven't run into yet? Other Pythonic tools
for compiling content into web sites, documents, presentations?</p>Mike Millerhttps://blog.mtmxr.com/The first day and my repositorytag:blogger.com,1999:blog-3508201418641099808.post-40374413123932790072017-05-31T17:38:01+00:00Yesterday I handed in the last exam for the semester(!) and today I started to work on the project full time. I begun by setting up a <a href="https://sourceforge.net/u/urathai/octave/ci/default/tree/">repository</a> for my code. At the moment I have only cloned the interval package, I'm still to make my own changes to it.<br /><br />I started to read the constructor for intervals to see how it works. While doing that I realized that there is much less to do than I thought. The constructor can actually handle creating intervals from numerical arrays of any dimension. The function displaying the interval can only handle up to two dimension but if you look at the internal state it actually has more dimensions. It is not perfect though, it does not work for decorated intervals and it cannot handle strings and cells as input. But still, it makes it much easier for me.<br /><br />I will continue to work and hopefully I have actually committed something by the end of the week. Next week I will be away but after that I will code for the rest of the summer.Joel Dahnenoreply@blogger.comhttps://gsocinterval.blogspot.com/