Polar rose

It’s fascinating to see how simple mathematical transformations look like in different coordinate systems.  ”Polar rose” is the name of a mathematical curve – described by the polar equation:

r(\Phi) = a \cos(k\Phi + \phi_0)

Below is a small code to draw it in R using the ggplot2 package:

require(ggplot2)

#data points
p <- data.frame(t=seq(-2*pi, 2*pi, 0.1))

#1.. drawing polar rose
c <- ggplot(p, aes(x=t, y=cos(3*t)))
c + geom_line(colour="red", size=1.5) + coord_polar()

#2.. another one with sine
c <- ggplot(p, aes(x=t, y=2*sin(4*t)))
c + geom_line(colour="red", size=1.5) + coord_polar()

…and the output

Posted in math, notes | Tagged | Leave a comment

Search in multiple files with Vim

Vim has a built-in feature to search over multiple files in a directory (even recursively across the sub-directories).

Vim can use external applications (e.g. grep) to do the search, but as I’m using currently a portable version of Gvim, I’m more-or-less forced to use the built in search functionality. The general command look like:

:vim[grep][!] /{pattern}/[g][j] {file}

The [g] option tells vim to count all matches in a line, not just the first one. If the [j] option is given vim won’t jump to the first match in the file.

For a recursive search use **.

:vimgrep /solve/j **/*.gms

Vim puts all matches to the “quickfix list” that can be opened with the command

:cw

This command splits the buffer to two windows (viewports). Below you will find the quickfix list with file names and the lines where a match was found. Just hit RET and the selected file will be loaded in the upper window.

Posted in geeky, notes | Tagged | Leave a comment

In gams: refer to set elements as values

In gams you have a set with numbers you would think that you can use them as “real” numbers, e.g. you can do calculations with them. But if you try to do so you’ll got error messages. Set elements can not be referred to directly as numbers.

But there is a special function i.val that you can use in these cases. Here comes an example to make it clear:

set i /1, 20, 300, 4000/;

parameter p_tmp(i)  'to store i as values';


$ontext
*** this would generate error
*** 148  Dimension different - The symbol is referenced with more/less
***        indices as declared

p_tmp(i) = 3*i;
display  p_tmp;
$offtext

p_tmp(i) = i.val/100;
display  p_tmp;


Posted in Uncategorized | Leave a comment

Find text in multiple files

A grep alternative in Windows: findstr

An example:

findstr /i/n “DCOW” *.gms

Posted in Uncategorized | Leave a comment

View/create LXI files without the GAMS IDE

Shiro Takeda went one step further in making a better alternative to GAMS IDE. He added a new functionality to his gams emacs mode. From now on, you can view and create LXI files within emacs.

To invoke the LXI mode press C-c C-x (it’s strange that there’s no menu item for it, so it’s good to keep in mind this combinationupdate: now it’s added to the GAMS menu). The LXI file helps you to navigate through the gams list file. It contains the information where to find the most interesting/important parts (solution reports, variables, equations etc.). When started the LXI mode shows a tree structure on the left-hand side. This gives you an overview and an easy way to walk through the list file by clicking on those parts of the tree that you’re interested in.

Together with the outline mode (which is in my opinion better in investigating data and results) you have flexibile tools to understand what your model tells you.

Screenshots here.

Posted in notes | Tagged | Leave a comment

Text editors for GAMS coding

Recently I’m working in a very restricted Windows environment, and so I needed a portable text editor that requires no installation (as I don’t have the permissions to add softwares to the system).

My favourite so far was the portable edition of Gvim (linux users know what I’m talking about). But today I found PSPad - a really handy editor with tons of features. The best in these editors is that you can add syntax highlighting rules, and then you can use them to write code in less known languages (e.g GAMS).

Update 4: Many thanks to Henry who sent me his own gams.vim and so helped me to inprove the syntax file.  Based on his version, I was able to add some new features. Follow the development on github.

Update 3: I’ve transferred the project to github:

http://github.com/trialsolution/gamsvim

Update 2: The code was reviewed and uploaded to vim.org. Please find it here:
http://www.vim.org/scripts/script.php?script_id=3127

Update 1: I found it in an old post but I post it because I was looking for a good line wrap setting for gvim for quite some time. To set a nice word wrapping in gvim:

:set wrap linebreak textwidth=0

You can find my GAMS syntax files for these two applications below. Feel free to modify it if you like.

1. Syntax file for gvim. Instructions: simply save the code as ‘gams.vim’ and place it into the

\GVimPortable\App\vim\vim71\syntax

folder

2. Syntax file for PSPad. Save the code as ‘GAMS.ini’ in the ‘\pspad\syntax’ folder, then you can configure the colors in the ‘Settings\Highlighters settings’ menu.

;PSPad user HighLighter definition file
[Settings]
Name=GAMS
HTMLGroup=0
Label=1
FileType=.gms,.lst
CommentString=*
PocoComment=1
2StarsComment=1
IndentChar={
UnIndentChar=}
TabWidth=4
SingleQuote=1
DoubleQuote=1
KeyWordChars=-_$
CodeExplorer=ftUnknown
[KeyWords]
ABS=
acos=
acosh=
Alias=
asin=
asinh=
atan=
atan2=
atanh=
ceil=
cos=
cosh=
ctime=
display=
div=
Equations=
equations=
exp=
floor=
log=
log10=
max=
min=
Model=
option=
Parameter=
Parameters=
precision=
round=
Scalar=
Set=
sets=
sin=
sinh=
Solve=
sqrt=
Table=
tables=
tan=
tanh=
time=
trunc=
uniform=
Variable=
variables=
[ReservedWords]
[KeyWords2]
abort=
By=
Downto=
else=
Elseif=
file=
for=
if=
loop=
put=
putclose=
repeat=
to=
until=
while=
xxpto=
[KeyWords3]
$call=
$eval=
$if not=
$include=
$option=

Posted in geeky, notes | Tagged | 10 Comments

Debugging GAMS code

It’s good to feed Erwin Kalvelagen’s blog into your rss reader, because you can always find some useful GAMS tricks there. Or if you have some free time, read the longer posts dealing with interesting topics in optimization.

Erwin has recently published a post about debugging GAMS codes.

An advice of him is to use the $stop statement to stop the execution in a certain point and then save the current state of the model into a gdx file. He also talks about the difficulties of writing loops in GAMS.

Here you can find some additional info about gdx files and creating loops.

Update: Another gams insider’s blog is here.

Posted in notes | Tagged | Leave a comment

My new project is released!

This post is really an announcement. In a recent post I talked about a project I was working on, and later I was asked in a comment whether the code would be published. This question was the final incentive to register for a sourceforge site and let the project go.

The project aims to develop a small utility that converts gdx data files into open office spreadsheets. It’s meant to be an alternative of the existing gdx2xls utility for Linux users. Now, we Linux users are able to convert our data files into human readable spreadsheets.

So, check out my new project on sourceforge.net if you’re interested!

Comments, suggestions, remarks are welcome, as always.

Posted in geeky | Tagged , | Leave a comment

DVD+RW doesn’t need to be erased

I accidentally ran into this issue when I wanted to reuse a DVD+RW disk: DVD+RW disks do not support blanking (erasing). You only have to overwrite them without erasing.

When I inserted a DVD+RW in the drive and wanted to erase it with brasero, the application sent me a pop-up window with an error message and ejected the disk. To find out what happened I opened a terminal and typed:

cdrecord dev=/dev/dvd blank=all

then I got the error message:

cdrecord: OPC failed.
Error: this media does not support blanking, ignoring.
This drive or media does not support the ‘BLANK media’ command
cdrecord: Cannot blank disk, aborting.

After googling it a bit I found this post that calmed me down. DVD+RW’s are only need to be overwritten. So I simply started brasero again and burned a new project onto the disk.

Posted in geeky, notes | Tagged | Leave a comment

Convert avi to dvd with command line tools

Assume you have a divx movie (e.g. sneakers.avi) and two subtitle files (sneakersHUN.srt, sneakersENG.srt) that you want to convert into dvd format. In this blog entry I’ll only use command line tools. This is because there’s no need to write another “How to convert avi to dvd with Devede” article, and because most GUI tools available in Ubuntu are simple graphical frontends of ffmpeg, mencoder and dvdauthor (the command line tools described bellow).

Let’s get down to business:

1. Convert your .avi file with ffmpeg or mencoder to a suitable .mpg format

Using ffmpeg is less complicated because it needs much less command line options. As an example (converting an avi file into a pal dvd format):

ffmpeg -i film.avi -aspect 4:3 -target pal-dvd dvd.mpg

If using mencoder, a lot of command line options have to be added. To make my life easier, I use a bash script in that I can simply change the file name, aspect ratio and what else is needed. Here’s the script:

#! /bin/bash
# bash script for creating a pal dvd
# movie is sneakers!

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
-vf scale=720:576,harddup -srate 48000 -af lavcresample=48000 \
-lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:\
keyint=15:vstrict=0:acodec=ac3:abitrate=192:aspect=16/9 -ofps 25 \
-o sneakers.mpg sneakers.avi

Notes: the DVD format is heavily constrained, i.e. bitrates and screen sizes are pre-defined. Take a look at this table.

2. Add the subtitles with spumux

  • Create two xml files with the contents below, and save them as “spumuxHUN.xml” and “spumuxENG.xml” (one for each language) .
    <subpictures>
        <stream>
           <textsub filename="sneakersHUN.srt" characterset="ISO8859-2"
                        fontsize="28.0" font="devedesans.ttf" horizontal-alignment="left"
                        vertical-alignment="bottom" left-margin="60" right-margin="60"
                        top-margin="20" bottom-margin="30" subtitle-fps="25"
                        movie-fps="25" movie-width="720" movie-height="574"
                        force="yes"
            />
         </stream>
    </subpictures>
    

    Notes: the xml files contain the necessary information for spumux. Option names speak for themselves. One remark to “font=”: you can use whatever fonts you like, only copy the .ttf file into the ~/.spumux directory (devedesans.ttf is the only font in Ubuntu that’s available by default).

  • Create an mpg file with the subtitles embedded:

    spumux -s0 spumuxHUN.xml sneakersHUN.mpg
    rm sneakers.mpg
    spumux -s1 spumuxENG.xml sneakersHUNENG.mpg
    rm sneakersHUN.mpg

3. Create the dvd file system with dvdauthor in two steps:

  • Create a simple text file with the following content and save it as auth.xml
    <dvdauthor>
        <vmgm />
        <titleset>
            <titles>
                <subpicture lang="hu"/>
                <subpicture lang="en" />
                <pgc>
                    <vob file="sneakersHUNENG.mpg" />
                </pgc>
            </titles>
        </titleset>
    </dvdauthor>
    
  • type the following into a terminal:

    dvdauthor -o mydvd -x auth.xml

    Note: this will create the dvd filesystem in the mydvd folder.

      4. Burn the dvd! A simple command line tool to do this:

      growisofs -Z /dev/dvd -dvd-video mydvd/

      Be aware that the -Z option will erase the disk if it’s not empty!

Posted in geeky, notes | Tagged , , , , | 1 Comment