Pages

Showing posts with label technical. Show all posts
Showing posts with label technical. Show all posts

Tuesday, November 5, 2013

Bluetooth headphones on Arch: a new twist in the saga

As of a few days ago the official blueman build for Arch switched from depending on bluez4 to bluez (which is to say, bluez5). If you missed my previous post, I already had gotten my bluetooth headphones working pretty well with bluez4, ALSA and blueman. This new update broke my setup, and sent me on my way to new adventures in bluezland(TM).

I haven't gotten much to work yet. I'm sorry. But as I get things working I'll update this post. This is what I've done:

Tue Nov 5 11:51:07 CST 2013
I updated my system and everything was "broken". I sent a few bug reports, posted a few things on Arch forums, the usual. There are a few problems, and I'll describe them as best I can. First off, bluez5 dropped ALSA support, so I had to install pulseaudio-git from the AUR, since the official repo version of pulseaudio does not yet have bluez5 support. Then there's the issue that blueman-manager and blueman-applet won't work anymore, they just error out with a dbus error that seems to indicate (to my limited knowledge) that they were not properly updated to the new bluez5 API. So I turned to knowledge gleaned from recent edits of the Arch Wiki and tried to use the new command like tool that comes with bluez5, bluetoothctl. At first, this looks awesome. It has its own little shell, color output and everything. The main problem is that it doesn't really work. I can't use it to pair my headphones, and I can't use it to connect to them. The errors a terse, to say the least. So I looked for another alternative and tried using gnome-bluetooth, which is what most people use, I think. The problem with gnome-bluetooth is that, unless you're running gnome, it doesn't come with a system tray applet anymore, so there is no way to connect to your device. This is what I ended up doing: 1. install gnome-bluetooth; 2. run bluetoothctl and remove your device from the list, if it is there; 4. install and run pulseaudio; 3. run bluetooth-wizard and pair with your device, it will also autoconnect and it should now work (test with running speaker-test). The one issue I ran into is turning the headphones off and back on. They will not automatically reconnect they way they did under blueman. Instead, it seems to work to go into bluetoothctl and manually connect (see image).
The other problem is a lack of control over the bitpool rate (maybe there's a way, but I don't know it yet) so every once in a while the sound gets chopped up and dropped.

Mon Nov 18 13:26:28 CST 2013
I managed to figure out how to only use bluetoothctl for pairing and connecting, so that I don't have to rely on gnome-bluetooth any longer. The trick was to turn the agent on before trying to pair. Note that tab completion works inside bluetoothctl, so all these calls are pretty easy to make.
# bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:13:46:4D:6A:C6 Discovering: yes
[NEW] Device 00:1D:BA:29:50:D3 DR-BT50
[bluetooth]# pair 00:1D:BA:29:50:D3
Attempting to pair with 00:1D:BA:29:50:D3
[CHG] Device 00:1D:BA:29:50:D3 Connected: yes
[CHG] Device 00:1D:BA:29:50:D3 Paired: yes
Pairing successful
[CHG] Device 00:1D:BA:29:50:D3 Connected: no
[bluetooth]# connect 00:1D:BA:29:50:D3
Attempting to connect to 00:1D:BA:29:50:D3
[CHG] Device 00:1D:BA:29:50:D3 Connected: yes
Connection successful
[bluetooth]# scan off
Discovery stopped
[CHG] Controller 00:13:46:4D:6A:C6 Discovering: no
[bluetooth]# exit

Bluetooth headphones and Arch Linux: the endless battle

So a few years back I had the brilliant idea to get on the bluetooth bandwagon and get me a nice pair of headphones, because, you know, wires are for suckers. Pretty standard pair of Sony DR-BT50. And pretty much for as long as I've had the headphones I've had headaches. Not that they're physically uncomfortable... quite the opposite. The cushion is made of the softest skin, probably ripped straight off of a newborn baby's ass. No, it's the bluetooth that's been giving me the problems. Not so much in Windows, but who uses Windows anyway? They worked for a while on my MacBook, although with a bunch of caveats and weird behavior (I could lock my computer up if I accidentally closed the lid before turning the headphones off... WHAT?!) until the screen broke and while I was fixing it I accidentally nicked the bluetooth antenna that's weirdly placed behind the LCD.

But mostly I use Linux on a day to day basis, especially at work where I listen to most of my music. And I need headphones, because me blasting Cynic before noon would get the rats all agitated and whatnot. And let me tell you, blutooth on Linux is about as developed as cuisine on an airplane. Except where airplane food has stayed the same over, I dunno, the past 50 years or so (has there been any time when they did not offer peanuts and soda?) bluetooth on Linux changes about every six months. The main stack is called bluez and the developers are churning the versions and modifying the API something fierce. Not so much with the documentation. In fact it seems like the documentation has been dwindling in size. For example, there used to be a wiki. Was it amazing? No, but it gave people a starting point. Now the only way to get to the wiki, which is mostly about bluez4 (the version most people are still using) is to use the way back machine. Anyway, this guy here does a much better job than me at describing all the ridiculousness surrounding the topic.

What I meant to say before I got off the rails is that, every time some update to bluez or pulseaudio rolls in, my poor headphones stop working. And even if they do work, there's a lot of crackling and just general instability. So the other day I said "enough is enough!" and started looking at the issue like a researcher. I did not get as far as James Bond (see above) but I'll tell you what I did find. This is how my setup works now, it's relatively simple, and I might simplify it even further later, as things start breaking more.

For one, I am running Arch, and somehow Arch decided that bluez5 is the default version of the bluetooth stack even though nothing really seems to work with it yet. Whatever, first step is to install bluez4, which is bluez 4.101 or something
along those lines. Next you need something to help you pair and connect to your headphones. I've heard you can use the command line for this, but the actual commands seem to change every few months so I haven't been able to get it to work. Instead I installed blueman. Now, I paired my headphones, it asked for a PIN, had to look it up and it turns most headphones use 0000 a their PIN. Next, in order to get them to connect I had to add
Enable=Sink,Source,Socket
in /etc/bluetooth/audio.conf. Connected like magic!

One big step done, we move on to getting sound through. For the longest time I used pulseaudio, which is supposed to work
out of the box with bluez. Blueman even has a nice plugin to switch the output to your headphones when you connect them and then switch it back when you disconnect. The problem with this setup is that everything is magic. I have no idea how any of this works, it's code that talks to other code on dbus in ways I'd have to spend a month or more to figure out. So, after having some issues with this and being able to figure out nothing, I completely ditched pulse and switched to good old ALSA. What this means is I added this file in my ~/.asoundrc:
pcm.btheadset {
   type plug
   slave {
       pcm {
           type bluetooth
           device 00:1D:BA:29:50:D3
           profile "auto"
           bitpool "31"
       }  
   }  
   hint {
       show on
       description "BT Headset"
   }  
}

ctl.btheadset {
  type bluetooth
}


You'll want to change the device to match the mac address that blueman gives you for your particular headphones. Everything else is pretty self explanatory except for profile (I have no idea what it does) and bitpool, which I will explain momentarily.

Having done the above you can check your also read it by connecting your headphones and running aplay -L. Your new bluetooth sink should come up. You can even get sound by running speaker-test -D btheadset. Not very pleasant, but it tests that things are working. Now you should be able to select the btheadset output in your media player of choice.

After all this hassle there is one thing left: sound quality. I was getting interruptions and hangs every few seconds with the default config. This is where the bitpool value comes in. I don't actually know what it means and what it does, but reading a bunch of forums I realized it might be helpful. It's not even documented very well... I had to read the source file in C to see that it was in fact set to some magic numbers at compile time by default. Anyway, I played around with it, and it seems to be some sort of quality vs. skips control. The higher the number the higher the chance of a skip, but the better the sound quality between skips. Make the number too low and you'll get phone quality on your headset. Since I listen to my music over ssh, and have it downsampled to 128bits anyway, I care not about high quality, so I probably made that number slightly smaller than I needed to, but I get absolutely no skips now. And that does it. This should hold me for a while, until bluez4 goes completely out the window and bluez5 forces us to use pulseaudio with its magic touch and cryptic error messages that nobody understands... Anyway, I leave you off with a track from the new Cynic album. Enjoy!

Tuesday, October 22, 2013

Installing ROS on Arch (part 1)

Call me masochistic, but today I try and install ROS on Arch Linux. If you guys don't know Arch is this really cool distro that's pretty much always on the bleeding edge of things and has a huge and supportive community behind it. There are plenty of pre-compiled packages for the core and a huge user repository where everyone can make their own package to be downloaded and compiled by others. It is quite peachy. ROS on the other hand is this monster of software that's basically only supported on Ubuntu and has a dependency tree larger that xorg or gnome combined (citation needed).

Now, why would I want to run ROS on Arch? It's simple: Arch is my current system and I have it set up the way I like it, but I need to use ROS to write code for this new piece of awesome technology we bought, the Allegro Hand. It's currently a secondary project, but I plan on getting the hand running on my machine so I don't have to ssh (or, even worse, physically log in) to a Ubuntu machine every time I want to test out code. But, back to the problem at hand (hehe).

Along with this huge dependency tree there's another problem: the default python on Arch is python3 while ROS assumes that everyone in their right minds would use python2 as their default, so it simply hardcodes the string "python" in every script and path because... well... why would anyone have /usr/bin/python point to anything else than python2? Does a different version of python even exist? (deep sarcasm). Anyway this is a problem I'll get to later, because we don't encounter it until we download the sources, which is itself sort of a pain. 

First off, the best thing you can do for yourself if you are running Arch is to install yaourt, from the french repositories. The french word for yogurt, this is an awesome little tool that lets you install AUR packages with more or less the same amount of effort as regular packages. It's also a transparent wrapper for pacman, so all the commands you know and love work exactly the same way.

Now that you have yaourt it's time to install some dependencies. These are the things I had to install:
aur/wstool-git
aur/python2-rospkg
aur/python2-catkin_pkg
aur/python2-rosinstall_generator-git
aur/ros-arch-deps
That last one took a long time and maybe the first four should be listed as dependencies to it, I don't know. Maybe one doesn't even need it. Let me know if you know. Anyway, I installed it. Then, I followed the steps in the ROS installation document:
$ mkdir ~/ros_catkin_ws
$ cd ~/ros_catkin_ws
$ rosinstall_generator desktop --rosdistro hydro \
> --deps --wet-only > hydro-desktop-wet.rosinstall
$ wstool init -j8 src hydro-desktop-wet.rosinstall
 What this did was pull the source for hydro with the desktop variant in ~/ros_catkin_ws/src

Ok now that we're here we're gonna skip the call to rosdep because that's going to just give us errors saying that we don't have the required dependencies because they don't exist on arch. I am NOT patching rosdep to take care of that. Instead I go right ahead and try and build the damned thing:
$ ./src/catkin/bin/catkin_make_isolated --install
But guess what! This errors out, because catkin_make is a python script and it calls python expecting it to be python2, but instead it gets python3 on my system. None of the things we just installed are present for python3, since they only have python2 versions. Ugly ugly.  I still don't know how to fix this, other than a sed on all the files in the src folder. And for those asking, hell no I'm not gonna change my default python system wide. It's not a matter of ease, it's a matter of principle dammit.

Sunday, February 24, 2013

How to choose the correct format for your academic figure

If you're using jpeg for all of your graphics, you probably want to read this. If you are using BMP for all your graphics you either really know what you're doing or you MUST read this.

I often get asked about figure generation and converting figures from one format to another, usually for use in a presentation or academic paper. Of course, a lot of this is a matter of taste, but sometimes I see what people do and it makes me want to start a huge lecture about the ubiquitous abuse of jpegs and the distinction between vector graphics and bitmap graphics. But I'm usually a generous person, and often I just give them a few quick hints. So here it is, for your pleasure, the basics of graphics formats and their intended uses. Caveat: I am by no means an expert at this stuff. Most of what I know is from the wikipedia articles.

Vector or Bitmap?

Before I go into the nitty-gritty, there's a few things we need to discuss. The main one is the distinction between vector graphics and bitmap graphics. These are the two main paradigms of image storage and understanding the strengths and limitations of both is crucial.

Instead of a long diatribe, however, I will try and present this in as few images and words as possible. The image below, which I pulled from Wikipedia, summarizes this section:

To put it in words, there are two main ways of representing images inside a computer: raster (or bitmap) images are stored as arrays of numbers, where each number represents the color and transparency of one tiny block of the image, a pixel; a vector image is instead stored as a collection of lines and circles and other similar geometric primitives, generally known as paths. In a bitmap image two points cannot be closest than one pixel apart, which is a function of the resolution of your image. So, when you zoom in, eventually you hit that pixel limit and your image starts looking like an old Atari game. In a vector image the position of each end of a line (or primitive node) is stored as a high precision number, which means that if the distance between two points is greater than 1e-12 you will eventually be able to tell them apart by zooming in enough (although I would recommend spending your time on more valuable endeavors, like writing blog posts for the benefit of the technologically hungry).

If the above didn't make sense, or it did but the implications are not clear, or if you simply don't care about the details, here is what it boils down to: use raster formats for things you do not know the geometry of, like scanned in pictures of your dog or screenshots of your sweet desktop arrangement; use vector graphics for images where geometry is known or obvious, like computer generated graphs, schematics, or cartoons.

Common image formats and what to use when

The above stuff is all well and good, but it doesn't really tell you why using jpegs indiscriminately is bad, and that's what you're here for, right? Since nothing is better explained than by example, I'll give a few scenarios and my personal preference (and reasons) for doing the voodoo that I do.

Pictures of my dog

Ok, I don't actually have a dog, but if I were to have one I would name it Jake and take a million photos of him, and stick that stuff in GIMP in order to add party hats to its cute head. If you have an amateur camera like mine, it will likely not give you much of an option as to the type of image it saves. It will probably be JPEG, and with good reason: JPEG is a compression algorithm for raster images that is specifically designed to work well on standard photographic material. For example, JPEG does really well (high compression ratio with very small loss in perceived quality) for pictures of sand, or dogs, i.e. where there is already a lot of information we perceive as nothing more than just noise. It does and adequate job for pictures of faces, but it does a not so good job for certain Joan Miro paintings, where it often fails to preserve edge sharpness. Unfortunately your crappy camera does not give you an option, so this is the best you're going to get, so just turn the compression quality to High and live with it. Or else buy a decent camera that allows you to store images in raw format (large files, no compression) or tiff (lossless compression) but at that point you're turning into a professional photographer and you're only reading my post for the jokes.

Screenshots and scanned images

Because JPEG compression does not do well with edges, and because computer generated images, like screenshots, often involve sharp edges, some clever people got together and invented a different compression algorithm: PNG (portable network graphics). This is in many ways similar to JPEG, but is in no way a replacement. Rather, it complements it. While JPEG is good at encoding originally noisy raster images and bad at edges, PNG is good at edges but bad at preserving the noisiness of sand on a beach. That is why, when you take a screenshot, save it as a PNG. And, most importantly, don't save it as a JPEG and then change it to a PNG: every time you re-encode you lose quality and suffer from the downsides of your algorithm. I made the images below to show the differences between the two formats. First up is a picture of tux, the beloved linux penguin:

I encoded the image as both PNG and JPEG to have the same file size. They look pretty similar, but if you zoom in you start to notice nasty artifacts in the JPEG version. Here are the original files for comparison: tux.jpg and tux.png. On the other hand, this next image shows the downside of PNG.

Both images look (at least to me) to be the same quality, but the size of each file is very different. And that's with a low level of JPEG compression. I could have dialed the compression up quite a bit with very little changes to the observed quality. Again, original images are gravel.jpg and gravel.png.

Other uses for PNGs are creating a raster image of an icon or cartoon or sketch. All the twitter icons in all your apps: they are likely pngs. Most webcomics publish their art as PNG. If you are going to scan a sweet doodle you made, or save that drawing you made using your wacom tablet, PNG is probably your best choice. You get the gist, I hope.

One small note here: if you're scanning documents that are mostly text, and your scanning software is not something last modified in the previous century, you likely have the option to use OCR (Optical Character Recognition) while scanning, and obtain a PDF. If you have this option, I highly recommend you use it. It will try and do character recognition for your document, which will result in your being able to select text from your scanned document and copy-pasta it anywhere you want it. Not to mention that, unless you mess with the options, the original image of the scanned document will still be what is visible, so regardless of the quality of OCR, your document is still be readable by eye.

Graphs and Sketches

And here we are at my favorite section, and that is creating vector graphics from scratch. This is your best option whenever you create digital images outside of the restrictions in the previous sections. So, when do you create images on a computer? A lot of people I know use MATLAB to generate plots of their data. Others use R or python or whathave you. Sure, you can save your graphs as a high resolution JPEG and lug around 50 files one megabyte each, digital storage is cheap and getting cheaper, right? But what if you really want to zoom in on one of those plots later on? What if you want to change some text on it, but you lost all the data and regenerating the plot is out of the question? You use JPEG and you're hosed. That's why you use a vector format instead: SVG, PDF, EPS. The advantages are plenty: your file will be smaller in size and also editable outside of the software that created it; not to mention you can zoom in up to infinity. You can use Adobe Illustrator (costs $) or Inkscape (open source) to edit any of the text or even change the colors of your curves or thickness of your axes. Everything is easy! Now that we're at vector graphics software, let's say you want to make a diagram, or sketch of a mechanism. Are you going to use Photoshop? Boo! Photoshop (and its free cousin, gimp) are great programs but they are made for editing photos and other image manipulation tasks (as their names imply). Sure, they both have *some* vector graphics capabilities built in, but if you're going to make a clean sketch it's like using a bread knife to mow the lawn: it will work, but do you really want to do it when there's a brand new lawn mower in the garage? So go ahead and download Inkscape or ipe (they are free) and play around for a while. Save your stuff as SVG if you're going to post it on the web, or PDF if you're going to use it in your dissertation ($\LaTeX$ ftw).

When Vector Graphics Goes Bad

I have encountered only one significant case where all the above information would have had me using vector format for an image but instead the correct choice was to encode it as PNG: a vector image with a very large number of primitives. Imagine you are doing a scatter plot of something like ten thousand data points, and you save it as PDF. There will be points all over each other, you won't be able to actually see more than a few hundred of them, but yet the image will be a few megabytes in size and it will take seconds to load (I know, I could just become a patient person, but I'd rather spend my time figuring things out rather than waiting on needless computation). So, all of a sudden, the love affair with vector format has come to bite you in the proverbial ass. Maybe your paper is even too big to submit to the conference! You have a few options, depending on how crafty you want to be. First, just give up, save that image as PNG, go home and have a cold one. If you want to be fancy, you can do the following. Save two images, one just the axes and the text, saved in SVG format. The other, just the troublesome datapoints, in PNG format. You can then go into your favorite vector image program and merge them together, save as a PDF. PDFs are awesome because they allow you to mix vector and raster images. You will still be able to select the text, modify later and everything else, except for the data points. But your image will be small and manageable again.

Conclusion

There is one format suitable for every purpose. When in doubt, read the wikipedia entry for it. You will learn a ton. Then use that information to your advantage to make your stuff look consistently good, have consistently small file sizes (and load times), and finally help yourself out by keeping as much of an image editable as you can. I could not count the times my adviser wanted a slightly larger font on my figure and because of poor planning it meant I had to re-run an overnight simulation to do it. I've been there and done that, and you don't have to repeat my mistakes!

Tuesday, November 13, 2012

Adaptive Time Step Integrators in SciPy

For one of my projects at school I have to numericaly integrate a bunch of ODEs, something I would normally do using ode45 in Matlab or NDSolve in Mathematica. However, since the rest of my code is written in Python, and I really don't want to install the bloatware that is Matlab on my clean linux machine, I've decided I was going to try out the ODE libraries that come with numpy and scipy. As it turns out, they're not too bad, but their documentation is. So, it took me a while to figure out how to do this, and I'm guessing someone else will probably benefit from this post (I know it would have saved me about a day's work, at least).

So, it turns out that scipy doesn't really implement its own ode solver, but instead wraps a bunch of FORTRAN code, and just silently drops like 80% of the functionality of that code without documenting much of what it kept and what it ignored. Anyway, after sifting through a bunch of forums and a whole bunch of source code, I came to the conclusion that the only function that can actually do adaptive time stepping and supply dense output is the wrapper around the vode integrator. The other thing that I needed was to detect when my trajectory crosses a certain boundary defined by $\phi(y) = 0$ and trigger an event at that point. In my case I simply needed to restart integration after adding the crossing point to the solution array. The following code does what I needed:
def f(t, y):
    # define your \dot{y}=f(y,t) here

def phi(y):
    # this function defines the boundary that is crossed


from scipy.integrate import ode 

# set up the solver, with a maximum time step of 1e-1
# an initial condition of y0 = init and t0 = 0
solver = ode(f)
solver.set_integrator('vode', max_step=1e-1)
solver.set_initial_value(init, 0)
tfinal = 30  # run this for 30 seconds
t = [0] 
y = [init]

# call the solver iteratively with the 'step' option
# what this does is it returns the result of the integration
# after the next (variable) time step, and the length of that step
while solver.successful() and solver.t < tfinal :
    solver.integrate(tfinal, step=True)
    y.append(solver.y);
    t.append(solver.t);

    # check for zero crossing
    gg1 = phi(y[-1])
    gg0 = phi(y[-2]) 
    if gg0*gg1 < 0 : 
        # find zero crossing
        # simple linear interpolation will do for now
        tcross = t[-2] - gg0*(t[-1]-t[-2])/(gg1-gg0)
        ycross = y[-2] - gg0*(y[-1]-y[-2])/(gg1-gg0)
        
        # replace wrong y and t
        y[-1] = ycross
        t[-1] = tcross

        # reset integration and start again
        solver.set_initial_value(ycross, tcross)
        print("crossed the boundary at time ", tcross)

Friday, October 26, 2012

Exporting Videos in Mathematica (the hard but nice way)

Have you ever tried to export your sweet looking Mathematica animation only to end up with some crappy quality video that doesn't seem to play anywhere? Well, you're about to leave that behind for good, because I'm going to teach you how to obtain videos of perfect quality out of that accursed IDE. The way we'll do it is we'll export high quality pdf/png frames of your movie, and then stitch them all together into a custom video. First off you'll need some tools. I used ffmpeg and imagemagick, command line utilities, in order to convert pdfs to pngs and stitch frames together. On linux they are easily available from your repositories. For Debian, for example, you just do
apt-get install ffmpeg imagemagick
On OS X it's also relatively simple, provided you're already using macports (or fink, or homebrew). For macports, the command is
sudo port install ffmpeg imagemagick
For Windows you're on your own, since I haven't ran that platform in years and I don't know how you get software for it anymore. I'm sure you can find a tool for the purpose somewhere on Google. Ok, I'll also assume you have Mathematica installed (otherwise you wouldn't be reading this) and a good understanding of how to use it. Then you want some code very similar to what I pasted below, as well as an implementation of the Draw[] function.

(* make sure that the default export path is in the same place as your .nb file *)
(* I actually have this as the first line in pretty much all Mathematica files *)
SetDirectory[DirectoryName[ToFileName[
      "FileName"/.NotebookInformation[SelectedNotebook[]]
]]]

(* export slides *)
Do[
     (* here I export every 5th slide, but they have to be named
     sequentially, hence the Ceiling[] call. *)
     oFile = StringForm["./frames/frame`1`.pdf", Ceiling[i/5]];
     oFile = ToString[oFile];
     
     (* this call exports a picture to oFile *)
     (* Draw[i] takes the frame number and returns a graphics object *)
     Export[oFile, Graphics[Draw[i], (* extra options for Graphics[] *) ]],     
  
  (* the do loop counter, picking every 5th frame*)
  {i, 1, Length[tk], 5}
];
Ok, you need to change the code to suit your needs, but most of the heavy lifting gets done in the Draw[] function, which you already know how to write, right? We're exporting to pdf, which makes for some really good quality. If you do it all right you should have a subfolder called frames filled with pdf files named frame1.pdf, frame2.pdf, and so on. This is awesome, we're halfway done! Next you need to stitch those frames together. If you're in Windows, you best be using cygwin or something equivalent, otherwise you're on your own. In *nix, you open a terminal (don't worry, it won't bite), cd to the frames directory and run ffmpeg :
cd ~/path/to/math/code/file/frames
for f in frame*.pdf do convert -density 300 "$f" "{$f/.pdf}".png done
ffmpeg -q:v 1 -r 25 -i frame%d.png video.mp4
What this does is convert all the pdfs to png, with a 300dpi density and then it merges all the frames into a video, showing 25 frames per second (-r 25) and keeping the video quality of the original images unchanged (-q:v 1). That's it, you're done!

Wednesday, October 17, 2012

Chain Rule under Einstein Notation

OK, so the other day I was trying to do all this symbolic math in order to get expressions for the derivatives of certain mappings from one vector space into another. Ultimately, all this would go into a Python function. When dealing with the first derivative things are pretty easy, because all you have is matrix-vector multiplication, and you can easily do that using NumPy's dot() function. The problem was that second and higher derivatives started showing in my formulas. These are higher dimensional tensors, and all of a sudden it is not clear what row multiplies what column when (for example, there are two ways to right multiply a 3-tensor to a vector) and what dimensions get added where when derivatives are taken. In particular, I was having a lot of trouble with applying chain rule. After reading the wikipedia article on Ricci calculus I wrote down a simple little rule for chain rule in index notation. Here it is:

Suppose we have a vector valued mapping $A^i(\mathbf{x})=B^i\left(\mathbf{C}\left(\mathbf{x}\right)\right)$ and we want to find its derivative with respect to $x^i$. We do this by applying chain rule, but first a little bit about the notation. The superscript denotes that $\mathbf{A}$ is a column vector, and the little equality above simply says that the $i$th element of $\mathbf{A}$ is equal to the $i$th element of $\mathbf{B}$. Now, we want to take the derivative with respect to $x$, which is also a column vector. The standard notation for this is
\[ \partial_{x^j}A^i(\mathbf{x}) = A^i_{,\,j}. \]
Now, on to chain rule. We drop the arguments of the functions, since they are unambiguous. Then, we write:
\[ A^i_{,\,j} = B^i_{,\,k}C^k_{,\,l}x^l_{,\,j} = B^i_{,\,k}C^k_{,\,l}\delta^l_j = B^i_{,\,k}C^k_{,\,j} .\]
And that's it! Now you know how to do chain rule, so taking derivatives and calculating them in [insert favorite programming language here] should be a piece of cake!

p.s. I might have been a little bit sloppy in my terminology and notation, but I am open to suggestions.
p.p.s. I have enabled $\LaTeX$ in my posts and comments. Feel free to give it a try!

Tuesday, October 9, 2012

vim trick #2

Yet another very useful vim trick: do you ever open a system config file but forget to open it with sudo? You make the changes but then, when it's time to save it, you're hosed. You have to save as a temp file, then move it to the right place, a whole lot of ugly. Well, not anymore! Put this in your .vimrc file and save with :w!! instead of :w. That's it!
cmap :w!! %!sudo tee > /dev/null %
What this does is sends the current file to 'sudo tee'. Now, tee here is used as a hack, as its regular function is to both redirect input to a file and to standard output. We are not interested in the standard output so we pipe that to /dev/null (otherwise it shows up in the vim window), and tell tee to overwrite the file we have open, with sudo power. One last thing is, vim will ask you to reload the file, because it was modified outside of the editor (duh!), so go ahead and do that. Nifty, eh?

Wednesday, August 1, 2012

Make ntfs-3g automount your drive in write mode under OS X

I just found a great blog post on how to get your NTFS write on in OS X (Lion). I won't go into details, since the other post does, but for the impatient, here is the route I chose to take, since I already had most of the things installed and up to date: using macports I installed fuse4x and ntfs-3g, and the I replaced the /sbin/mount_ntfs binary with the one provided on the site (it is basically a script that calls the ntfs-3g utility with the proper arguments). That's it!

Edit:
Turns out the method above, while it does work, makes for very slow write (and read) speeds, especially over USB. So I switched to a commercial version made by Paragon which boasts (and so far seems to deliver) speeds comparable to native file systems. Sorry open source, I love you, but I don't want to wait an eternity to copy my files to my usb drive.

Wednesday, May 9, 2012

vi tricks #1

Been working with vim lately and every day I discover new tricks. I figured I'd post things here as I they might be of help not only to me, but also to the internet at large.

First up, suppose you want to insert a large number of the same character or string, for example in order to write a comment box, or something similar. Instead of typing "*" 30 or so times, thus shortening the life of the "8" key on your keyboard, you can do this, in normal mode:
 <number>i<string><ESC>  
For example, want to make a lot of $$$? Just type
300000i$<ESC>  
and you'll be set for a life(ish)!