Posts Tagged ‘Mathematics’

Season’s End

During this summer, I participated in the Season of KDE, working on a Octave backend for Cantor with my mentor and Cantor author Alexander Rieder. I’m proud to say I have achieved my goal. In addition to running Octave commands directly from Cantor’s worksheet, all convenience features supported by both Cantor and Octave support are available:

  • Syntax highlighting for functions, variables, operators and strings
  • Tab-completion
  • Easy access to function documentation / syntax help
  • Easy 2D and 3D plotting of functions, with plots integrated in the worksheet
  • Linear algebra extension for defining matrices and computing their inverse, eigenvectors and eigenvalues
  • Integrated script editor and runner
  • Possibility to interrupt or restart Octave at any time

I haven’t been really regular with blogging, so I would like to apologize and give you something better in return: a screencast. Please excuse my bad english and strange voice, I’m really not a good speaker in any language.

Also, it was only after I finished that I remembered it would be better to make a presentation with untranslated Cantor. I hope I explained everything I did well enough.

Hi-quality version

Low-quality version

I would really like to thank the KDE team (especially Lydia) for organizing this, and my mentor Alexander for help and guidance (and finding me more things to do 🙂 ). I will certainly continue working on (and with) KDE.


Highlighting in Cantor

Disclaimer: another post about 4.6 when 4.5 is barely out. Please don’t hate me. If you do anyway, scroll to the end of the post.

When I started working on Cantor, most of its highlighters worked like this:

There was a struct HiglightingRule with two members: a QRegExp and a QTextCharFormat. The highlighter would make a QList of these rules, iterate over them every time the cursor moved, and formatted all matches of each regex with the associated format. As there could be a lot of these rules (~1600 for Octave), things could get slow. The other backends didn’t have that many rules, that’s why I was most bothered with it. Thousands of iterations and especially regex-matchings for every key pressed is really too much for my poor laptop. It was enough to make scrolling non-smooth. Because QSyntaxHighlighter can’t easily be made asynchronous, holding down the right arrow key caused the cursor to hang and jump randomly.

Fortunately, Alexander (Cantor’s author) and Oleksiy (who works on the R backend) tried a different implementation: The text was broken down into words, and each word checked against a database. We then decided that a database is not needed and I replaced it with a QHash<QString, QTextCharFormat>. Iterating over 1600 regexes was replaced with iterating over less than ten words and searching for them in a hash.

I preserved the QRegExp-based API for things like strings (‘.*’ and/or ".*"), but most of the rules are now converted to the word-based highlighting. Even without a proper benchmark, it’s obvious that scrolling is much faster now. It’s also not something screenshot-worthy, so I have to disappoint you again. Please don’t hate me again.

Final note for spoiler-haters: Even though I’m talking like I’m already skipping it, and I don’t have it because it’s not in Arch yet, there is something special for me about the 4.5 release. It’s the very first release of KDE with a piece of my code. Quite literally a piece, because in total it’s two new lines, but it makes me prowd. I added auto-prefetching to the Comic plasmoid.

Octave-backend improvements

I’m having a bit of a pause between the exams, so I’m doing some hacking on the new Octave backend for Cantor. Thing are going very well, probably better (especially faster) that I expected.

The basic input-output sending between Cantor and the command-line process was working some time ago, and I recently added support for embedding graphics into the worksheet.

This week I was working on cosmetics: Autocompletion, syntax highlighting and pop-up help.

Completion for functions and variables

Completion for functions and variables

Completion was easy, octave provides a completion_matches() function which takes a string argument and return all functions and variables starting with that string.

Syntax highlighting and help

Highlighter - there can be only one.

Syntax help was also quite straightforward, through the use of Octave’s built-in help() command. Highlighting was where I spent most of the time, because as a command-line program Octave has no native support for it.

There’s a handy command called ‘who’ that lists all currently visible variables, so I retrieved the variable name list with it and used it in the highlighter. I then hardcoded a list of all recognised operators and keywords.

The toughest nut was getting a list of all functions from Octave. There’s no (at least known to me) equivalent to ‘who’ for functions, so I had to improvise. I didn’t want to hardcode all the functions, as is done for the Maxima backend, because, as we all know, hardcoding is bad, and there’s always the possibility of future changes to Octave language. The closest I could get is using completion_matches() with an empty string, but that returns all the internal constants, environment variables, and files in the current directory. I filtered them out the best I could, and the end result work well, despite there being more that 1600 functions.

Embedded plots in Cantor

Embedded plots in Cantor

Another think I worked on this week is getting as much plotting code as possible from C++ to an octave script. This not only unclutters the worksheet (there used to be a long line of commands instead of cantor_plot#d()), but makes simple changes to plotting faster by not requiring a recompile. Tweaking the size of the plot to prevent it from filling the whole page didn’t even need a new session, just an update to the script.