AleX's Blog

Random stuff on informatics, computer science, physics, etc.

Implementing mod function in C

Something some people don’t realize is that C and several other programming languages don’t offer you the standard modulo operation but simply a remainder operation (this issue has been in any case discussed countless times, e.g. see, although most of the times without offering a solution).

The problem arises when people try to use the remainder operator, % or \text{rem} , for the modulo without realizing the implications of doing so.
I recall when a programmer I knew tried to write C code for a steering angle sensor system using the % operator for the mod operation (which was needed by the algorithm), leading to erratic and difficult to debug results (in this case, a function that was supposed to be smooth exhibited erratic jumps), leaving me the job to understand the algorithm and then find the bug.
Note that the C standard defines the remainder of an integer division via the % operator for positive integers. For negative integers the operation is implementation dependent and only fixed by the C99 standard or newer. Note that the remainder can be negative but the modulo can’t, and that \mod(a,b) is contained in [0,b[ :

Modulo vs remainder operation for fixed divisor.

The question is then, if the remainder operation, % here also denoted as rem is available, how to leverage this to calculate the modulo operation? And here’s one way of doing it:

\mod (a,b) = \begin{cases} 0, & a = 0 \vee b = 0\\ b - \text{rem}(-a -1, b) -1, &a<0 \\ \text{rem} (a,b), &a>0\end{cases}.
Note that this is only valid for a non-negative divisor b, however the generalization is rather trivial and as is common practice to say, left as an exercise for the reader :).

Video game collisions – pixel perfect

Collisions abound in video games. If not, how would you know whether you’ve hit a wall, your enemy, or if that bullet shot you or not? Here we will look at collisions in 2D games, which tend to use sprites (alternatively you can have 3D models projected onto a 2D plane, which is becoming increasingly common).

By virtue of how graphics are stored in computers, the full sprites are rectangular (and some pixels are either alpha pixels or have a special color defined to be transparent – alternatively one can use masks).

Let us then look at collision between rectangles.

Box collision

Consider a rectangle A defined by its vertices \lbrace (x_{1}^{\text{A}}, y_{1}^{\text{A}}), (x_{1}^{\text{A}}, y_{2}^{\text{A}}), (x_{2}^{\text{A}}, y_{1}^{\text{A}}), (x_{2}^{\text{A}}, y_{2}^{\text{A}}) \rbrace and a second rectangle B, also defined by its vertices \lbrace (x_{1}^{\text{B}}, y_{1}^{\text{B}}), (x_{1}^{\text{B}}, y_{2}^{\text{B}}), (x_{2}^{\text{B}}, y_{1}^{\text{B}}), (x_{2}^{\text{B}}, y_{2}^{\text{B}}) \rbrace :

Box collision

Box collision

It is a simple exercise to calculate the overlap in the x and y directions between the two rectangles to be given by:

x_\text{overlap} = \max(0, \min(x_{2}^{\text{A}}, x_{2}^{\text{B}}) - \max(x_{1}^{\text{A}}, x_{1}^{\text{B}}) )
y_\text{overlap} = \max(0, \min(y_{2}^{\text{A}}, y_{2}^{\text{B}}) - \max(y_{1}^{\text{A}}, y_{1}^{\text{B}}) )

where \max(a,b), \min(a,b) are the function that return the largest/smallest of the two values, a or b.

Then for our purposes, there is a collision between the rectangles whenever both x_\text{overlap} \leq 0 and y_\text{overlap} \leq 0.

Pixel perfect

So what about pixel perfect collisions, i.e. when we detect exactly whether or not two sprites have collided?


Boxes with objects

Sprites and bounding boxes – in this case the two objects do not intersect, although looking only at the bounding box they appear to intersect.

Well, here’s a possible algorithm:

    • Detect collisions as in the box collision algorithm above
    • If a collision is detected, extract coordinates of collision rectangle
    • Iterate over pixels in the collision rectangle: look if there’s any pixel where the sprites have both a non-alpha pixel (or where both masks have the same value, if using masking)
    • If any such pixel is found there’s a collision, if none is found, there’s no collision

At most we would have to iterate over the size of one sprite. Let’s estimate that as 300 x 300 = 90.000 pixel, which is still quite a lot, specially if we have to detect a lot of such collisions. But if we look only, for example, at every 10 pixels, in every direction, that would mean iterating at most through 900 pixels, which is 2 orders of magnitude better, considerably faster and nobody can detect that! Although not a pixel perfect collision, it’s much better than box collision and much faster than pixel perfect collisions.

Fixing Trek Stor 3.0’s blank screen

My wife’s eBook reader TrekStor 3.0 suddenly started displaying a blank screen after a fall. If you’re having the same problem and the screen is intact, chances are that the problem is identical, so I will describe how I proceeded to fix the issue.

Blank screen

Blank screen

Since there are no moveable parts and the screen was obviously undamaged, I decided to open it to search for the source of the failure. I discovered the problem is that the eBook reader is so slim that the case presses down on the inductors even when a minimal amount of strength is applied. In this case it was easy to pin-point the problem to a broken SMD (surface mount device) inductor, in this case a 10 uH inductor (you can read “100” printed on the inductor which means the inductance is 10\times 10^0 = 10 uF ).

Old, broken, inductor

Old, broken, inductor

Having no SMD components at hand, I decided to try a through-hole inductor and see whether or not that fixed the screen.

Test inductor (through-hole)

Test inductor (through-hole)

It worked, so I bought a SMD inductor and soldered it into place.

New inductor

New inductor

New inductor

New inductor

And voilà! Problem solved!

Working TrekStor

Working TrekStor

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Fixing an Asus K53’s power jack

I fancy Asus laptops. They are not expensive and come well equipped. Plus, Asus is a well established company and their products tend to be, in my opinion, of good quality.

Unfortunately, the power jack of my wife’s Asus K53S started having problems after a couple of years. The connector from the charger started to feel loose when connected to the jack and the laptop would suddenly alternate between charging and discharging the battery.
After trying a different charger, it became clear that the problem was the power jack. I have changed it and since then the laptop has been working flawlessly. In what follows I describe the steps necessary to access the power jack, desolder it and solder a new one. This guide was constructed for an Asus K53S but it should work, with minor modifications on most, if not all, laptops of the K53 series.

Here’s the material you will need:

  • New power jack (I ordered mine from ebay)
  • Precision Philips screwdriver (#0 or #00)
  • Soldering iron
  • Desoldering pump
  • Desoldering braid
  • Solder
  • Pair of diagonal pliers (potentially)

You will need to disassemble the laptop in order to access the power jack. Begin by laying the laptop down with its bottom side up. Remove the battery and remove the screws indicated in the following figure:

Back side

Fig. 1: Back side

The red arrow points to 2×6 screws while the blue one points to 3×6 ones.
Now remove the memory and hard drive compartment and remove the following screws (all 2×6):

Memory and HDD compartment

Fig. 2: Memory and HDD compartment

Remove now the hard drive by sliding it. There is another screw beneath it that needs to be removed (2×3):

Screw under HDD

Fig. 3: Screw under HDD

Remove now the keyboard by using a flat screwdriver to push the three pins on top. You need now to remove three ribbon cables: one from the keyboard, another one from the power button and yet another from the touchpad. To release the one from the keyboard, simply pull the black restrainer. To remove the ones from the power button and touchpad you will need to rotate the retainer 90 degrees (see Fig.8 and Fig. 9). You can now remove the screws under the keyboard (again, all 2×6):

Screws underneath the keyboard

Fig. 4: Screws underneath the keyboard

Now turn the laptop around and remove the optical drive. You need now to remove three screws that are sitting at the optical drive bay (these are 2×3 screws)

Screws under the optical drive

Fig. 5: Screws under the optical drive

Turn the laptop around again and remove the top part (where the keyboard used to sit). You have now access to the motherboard. You will need to remove several cables (blue arrows) and screws (red arrows):

Asus MB

Fig. 6: Asus MB

Again, all these screws are 2×6.

Locked retainer

Fig. 7: Locked retainer

Unlocked retainer

Fig. 8: Unlocked retainer

Now that you have access to the jack you will need to desolder it. I recommend that you use, at least, a desoldering pump (these are very cheap anyway) and if possible desoldering braid. You should begin by applying flux to the old joints and the new solder on top (yes, you read well. You should apply new solder to desolder. ). Ideally you would desolder the piece and remove it intact. Honestly I found this hard to do. The old solder didn’t seem to melt and I was out of flux (except for the one contained in the core of the solder I was using). Therefore, what I did was to destroy the old jack and cut its contacts using a pair of diagonal pliers.
That made desoldering much easier.

Old power jack

Fig. 9: Old power jack

Soldered jack with destroyed one next to it

Fig. 10: Soldered jack with destroyed one next to it

Soldering the new jack should now be easy. After that you just need to put everything back in reverse order.
And that’s it!

P.S. I am not responsible for any damage your laptop incurs in following this guide. If you have absolutely no idea of what you’re doing, you’re better off contacting a repair service.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Bulding a DC power supply and a current amplifier

And here goes something in electronics. This was something very simple, but one of my professors in the university needed a 15/30V DC power supply AND a current amplifier in order to feed a system with very small impedance. Since he required both things simultaneously, I decided to put everything into the same case.

This is partly a guide on how to build these things and partly a brief description of how I did it. I could try to make this more general, but I guess that would only complicate things. Suffices to say, a lot of these things can be done in a different way, different components can be used and the end result will still be the same. The input and output voltages can, obviously, be different with a small change of components.

I will split it into two parts: part I is concerned with the power supply itself and part II with the current amplification and buffer in order to feed this low impedance circuit.

Part I: 15V/30V Power supply

For this part you will be needing the following components (you will also need some tools, which depend on how you plan to assemble things):

  • 15V*2 transformer
  • 1x diode bridge
  • 2x 1000 uF electrolytic capacitors
  • 1x LM7815 positive voltage regulator
  • 1x LM7915 negative voltage regulator
  • 2x 220 uF ceramic capacitors

Optionally you will also need an adequate fuse, switch, LED and appropriate resistance for the LED.

First transform the mains AC current (220V @ 50 Hz in my case) to 15V (@ 50 Hz in my case) by using a transformer with two secondaries (careful when handling transformers if they are connected to the main socket!). Transformers are very simple devices which consist of two coils and a metallic core (usually iron) and functions via magnetic induction.
For security reasons you should connect a power switch and an adequate fuse between the transformer and the main power. In the output of the transformer you can connect an LED (using an appropriate resistance in series with it, to avoid burning it) to indicate whether the device is on or off (true, the LED will blink, but at 50 or 60 Hz you will not see it!).

Power supply schematic

Power supply schematic

Next we need to rectify the signal. What happens is that the signal from the mains is a sinusoidal signal which, with respect to the ground, varies between 230V and -230V (I live in Europe). We want the signal to be always positive or only negative. To do this we use a diode bridge (Br in the schematic). Diodes are solid state devices that only allow current to pass in one direction. You can either build your own diode bridge, or buy a pre-assembled diode bridge (which I recommended).

We now need to filter the signal, that means converting the sinusoidal circuit from the mains into something more constant (the aim at the end is to have a completely DC signal).

For this use two high capacity electrolytic capacitors, 1000 uF (C1 and C2 in the schematic). Be very careful how you connect electrolytic capacitors. Polarity is important with these guys. Assemble them the wrong way and they may explode! (yes, it does make a lot of noise, but it’s usually harmless) You should now feed the resulting signal into voltage regulators. In my case I used the LM7815 (positive terminal voltage regulator) and the LM7915 (negative terminal voltage regulator). Note that if you need a different output voltage, you should use different voltage regulators. This may be all that you need, but in my case I was not satisfied with the end result, since there was still high frequency noise, so I added two low capacity, 220 uF, ceramic transistors (C3 and C4 in the schematic)

And that’s it! You should now have a clean DC signal and three outputs, +15 V, 0 V and -15 V, so you can either have an absolute voltage of either so 15V or 30V.

In case you want to build the circuit on a PBC, it’s better to use an adequate software and send it to a company specializing in doing PBCs. I projected mine in Eagle.

Power supply PCB (Eagle)

Power supply PCB (Eagle)

Power supply schematic (Eagle)

Power supply schematic (Eagle)

Part II: Current amplifier

For this part you will be needing the following components (you will also need tools, which depend on how you plan to assemble things):

  • 1x BC547C
  • 1x 2N3055

For the current amplification I decided to use the so called Darlington pair. It basically consists in an arrangement of two transistors and has a very high input impedance, a very low output impedance (which was necessary in my case), so you can use it to feed circuits with very low impedance (speakers, for example) and amplifies current instead of amplifying voltage (also what I wanted). One can also obviously have voltage amplifiers. A small note here is that such circuit MUST be powered (it’s an active circuit, which is obvious. Think about it this way, power is V.I. If the output voltage is the same as the input and you increased the current, energy must be coming from somewhere, right?). In fact, the power supply previously built can be used to power this circuit!

Now, I won’t delve into transistors since that would make this too long (it’s probably already too long), so I’ll assume you know some stuff about transistors (if you don’t, read Wikipedia). We need the transistors to be always in the active zone. This means V_{BE} must always be above 0.7 V (for normal npn transistors).
In our case we connect the BC547C (Tr1 in the schematic) and the 2N3055 (Tr2 in the schematic) as in the schematic.

Darlington pair

Darlington pair

In this case, since we have two transistors, we need that V_{B^\prime E^\prime } exceeds 1.4V.
In case you are feeding an AC signal, you may need to sum a constant DC component to allow this (this is easily done anyway). In my case, since I wanted to use the output signal of a signal generator that allowed me to have a DC offset, I simply didn’t care about it,

Again, you can print everything into a PCB.

Current amplifier schematic (Eagle)

Current amplifier schematic (Eagle)

Current amplifier PCB (Eagle)

Current amplifier PCB (Eagle)

My Eagle files are available here.
And well, that’s all of it! In my case I bundled everything into a metallic case not only for safety reasons, but also to allow the components to dissipate properly (I attached some of them, like the power transistor, to the case, to serve as a heatsink) and also because it looks much nicer. Here’s the drawing I glued to the front of the case (the small hole at the top left corner is for the LED).

Case front

Case front

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Replacing missing Skoda Fabia estate trunk cover lock piece

I love my Skoda Fabia estate 1.6 TDI (5J, 2010 facelift): it has plenty of room, was not expensive, it has a nice VW engine, all the extras I need and good fuel consumption.
Unfortunately one of the pieces holding/locking the trunk cover in place when it’s extended fell of and this seems to be a fairly common problem with the Fabia estate. It’s really annoying because it involves removing some of the car’s panels. It is, however, not a difficult procedure and you can do it at home and avoid going to a repair shop.

Trunk cover before replacing piece

Trunk cover before replacing piece

Lock piece missing

Lock piece missing

The retractable trunk cover is held into position by two plastic pieces, one at each side. Unfortunately these pieces have a tendency to fall from their place. In this case you can either take the car to a garage, or fix the problem yourself. You probably won’t be needing a new piece (it should be somewhere between the panel and the body work) but if you do decide to buy a new piece (which I did), the parts number is 1Z9 867 925 D 47H (the old parts number seems to be 1Z9 867 925 C 47H – this is the SAME piece).

The piece

The trunk cover lock piece

The piece

The trunk cover lock piece

You will need the following tools:

  • T10 bit (torx)
  • T15 bit
  • T25 bit
  • H5 bit (hexagonal), a big flat screwdriver or simply a coin
  • 10 mm wrench
  • Panel removal tools (not really necessary, but useful)
  • Flat screwdriver or pin removal tool

You will need to remove 3 panels, or 2 panels and pull a bit the panel where the piece is supposed to go, if you’re lucky.

Although you won’t probably take long enough time to drain the car’s battery, I recommend you start by removing fuse number 23 (15A) in order to turn off the trunk light.

Start by removing the bottom panel by the trunk lock.

Bottom panel

Bottom panel

You will have to remove 2 pins and 2 nuts (10 mm). The pins consist of two parts: an inner and an outer one. The inner piece needs to be pulled out before you can remove the entire pin. You can achieve this by easing a flat screwdriver under the pin head or by using a pin removal tool (the screwdriver works really well, so there’s no need to buy the tool, in my opinion).



Then remove the nuts near the bottom of the panel using, for example, a ratchet wrench. The panel is still attached to the bodywork by metallic clips, but just needs to be pulled out (if you have those panel removal tools, it will make the job much easier).

You need now to remove the grey side panel by the window. It is attached to the bodywork by some metal clips and nothing else. You just need to pull it out (don’t be afraid to use some strength, these panels are not that fragile). Again those panel removal tools may come in handy. It may happen that one of those metal clips will remain attached to the bodywork. If that’s the case, simple pull them out and re-insert them into the back of the panel.

Small side panel

Small side panel

Metal clip

Metal clip

You are now in position to remove the screws from the big side panel where the piece is supposed to fit. You will need to remove the following objects (note necessarily in that order):

  • The nut holding the lights, using either a big flat screwdriver, a H5 bit or a coin
  • 1xT10 screw (holding some sort of hook into position)
  • 3xT15 screws (one where the trunk cover fits, another beneath the side panel you removed and the other beneath the bottom panel you removed)
  • 1xT25 screw (beneath the hook)
T15 in the trunk cover insertion

T15 in the trunk cover insertion

T15 at the top

T15 at the top

T15 at the bottom

T15 at the bottom

T10 and lights' nut

T10 and lights’ nut


Hook and T25

There will still be a pin near the seats that you should remove in case you want to remove the entire panel. This may be necessary in case you want to look for the missing piece. In case you have a new one which just needs to be clipped into position, you don’t really need to remove the entire panel but simply to pull it away from the bodywork. In this case you don’t need to remove the pin.

Now, simply pull the panel away from the bodywork (again, it is be held into place by some metallic clips) and attach the trunk cover lock piece.
That’s basically it! Put everything back in reverse order.

Trunk cover after replacing piece

Trunk cover after replacing piece

P.S. I am not responsible for any damage your car incurs in following this guide. If you have absolutely no idea of what you’re doing, you’re better off taking the car to a repair shop.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Configuring Unison

Unison is a file syncronization software, using the rsync algorithm.
Unlike rsync, Unison makes two-way file synchronization very simple.
Unison also allows you to syncronize files across different platforms and filesystems.
There are official Unison releases for several platforms, including Linux and Windows, although in my Windows machine I use Linux’s version through Cygwin.
You can actually also install a GUI for Unison (which probably come by default with some pre-compiled binaries, such as the Windows version).

It allows you to perform local syncronizations, for example between your main hard drive and an external hard drive, or with a remote host by using ssh.

If you want to sync two roots regularly, you should create a prefences file (.prf) file. Here’s an example of the file I use for mirroring my working directory in my SD card (FAT32) with my external hard drive (NTSF) in Windows (using Cygwin). In Linux this file should go in your .unison directory located in your home folder.

# Unison preferences file

# roots

# for mirroring

# to sync fat32

# prevent unison from prompting the user

Here I am mirroring my /cygdrive/f/Documents directory.
Note how I name the directories in the two roots in a different way. In this case, if by any chance the drive name changes, I am not mirroring things in the opposite direction! (an alternative is to completely disable mirroring)
Note that since I store my data in an SD card using the FAT32 filesystem which does not support the same set of permissions as NTFS or EXT , I set “perms=0” (otherwise Unison will complain about permissions).

And here’s the .prf file I use for synching my working directory with my server (via ssh):

# Unison preferences file

# roots

# for mirroring

# to sync fat32

# prevent unison from prompting the user

# to prevent the ssh connection from hanging
sshargs = -o ServerAliveInterval=120

One more thing that is wort mentioning, is that, even with ServerAliveInterval set to a large value, the synchronization may fail whenever there are many files or these a very large due to connection reset by peer.
In this case, what works for me is to synchronize smaller subsets of files before synchronizing the full roots.
This can be achieved via the path variable. Imagine you have a folder /cygdrive/f/Documents/foo/bar that you want to synchronize, using the roots above.
Then, by setting,

path = foo/bar

Unison will synchronize only that folder.

Please note that this aims to be a small introduction to Unison and not a comprehensive one. In case you need more information, you should always consult the user manual.

You can obviously schedule automatic synchronizations in Linux using crontab or identical tools in Windows, and using public key authentication but that is material for another post…

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Amitochondrial Eukaryotes

A defining characteristic of eukaryotes is the fact that they posses organelles and a differentiated nucleus.
A very important type of those organelles are the mitochondria.
It is an accepted hypothesis that mitochondria used to be prokaryotic organisms that established some sort of symbiosis with eukaryotes, and eventually lost their ability to live on their own (for example, mitochondria usually import cytoplasmic tRNA for transcription of mitochondrial DNA), this is know as the endosymbiotic theory.[1]

Now, something crossed my mind the other day: Do all known eukaryotes have mitochondria?
It seems obvious that there must have been a time when eukaryotes lacked mythocondria.
We also know that eukaryotes are able of obtaining energy in other ways than not respiration. Even human cells are cable of anaerobic (lactic acid fermentation) processes.
We can, however, understand that having mitochondria provides Eukaryotes with a huge advantage.
In Biology energy is usually measured in terms of the maximum theoretical number of units of ATP (adenosine triphospate) produced per unit of glucose. Well, lactic fermentation in our muscles is capable of producing only 2 units of ATP per unit of glucose while aerobic respiration is capable of producing 36-38, that’s around 18-19 times more![2] (bear in mind that these are estimations for the ideal case!)

So the question remains: do all known eukaryotes have mitochondria?
Well we know some eukaryotic organisms which seem to be amitochondrial, i.e. they lack mitochondria, such as members of the genus Giardia.[3] One can, however, detect remnants of mitochondrial DNA in their nuclear DNA. [3] Also, it seems these organisms, have some organelles that appear to be derived from mitochondria such as hydrogenosomes and mitosomes. [4,5] So it seems that even though they lack the organelle, they are descendant from some eurakyote that used to contain mitochondria. It is also quite plausible, that some of the proteins coded in mitochondria are still produced and that somehow they retain some of the functions (note that the it would make perfect sense for mitochondria’s proteins to be coded in our nuclear DNA. The fact that it is not, seems to simply imply that after the endosymbiosis took place there was either no evolutionary pressure for that or it was advantageous to keep both genomes apart).

Anyway, after this long dissertation it seems that there is agreement in the scientific community that no known eukaryote has a totally amitochondrial lineage, even though some seem to have lost mitochondria during evolution.[6]
As it is stated in [6]: “Since Trimastix retains genetic evidence of a mitochondriate ancestry, we can now say definitively that all known living eukaryote lineages descend from a common ancestor that had mitochondria.”

So while the simple answer to my question is “Yes, there are eukaryotes that lack mitochondria”, the full scenario is a tad more complex and more interesting than that…

[1] William F. Martin and Miklós Müller, Origin of Mitochondria and Hydrogenosomes (Springer, 2010).
[2] Eldon Enger, Frederick Ross and David Bailey, Concepts in Biology (McGraw-Hill, 2011).
[3] Jorge Tovar et al., Mitochondrial remnant organelles of Giardia function in iron-sulphur protein maturation. Nature, 426(6963), Nov 2003.
[4] M. van der Giezen, Hydrogenosomes and mitosomes: conservation and evolution of functions. J Eukaryot Microbiol. , 56(3), May-Jun 2009.
[5] R. E. Schneider et al., The Trichomonas vaginalis hydrogenosome proteome is highly reduced relative to mitochondria, yet complex compared with mitosomes. Int J Parasitol., 41(13-14), Nov 2011.
[6] Hampl V, Silberman JD, Stechmann A, Diaz-Triviño S, Johnson PJ, et al., Genetic Evidence for a Mitochondriate Ancestry in the ‘Amitochondriate’ Flagellate Trimastix pyriformis. PLoS ONE 3(1): e1383.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Making PyQuante work with Libint

Pyquante is a very nice “open-source suite of programs for developing quantum chemistry methods” developed by Richard Muller. Although not as fast as comercial software, I can atest that the code is very simple to understand and to modify. And in case you need to implement your own Quantum Chemistry routines, PyQuante takes you off the burden of having to write everything from scratch.

Libint is a software stack for computing integrals found in Quantum Chemistry created by Edward Valeev. I certainly cannot say much about libint as I haven’t had enough time to fiddle around with it. What I can say is that Gabriele Lanaro wrote some bindings that allow PyQuante to use libint and that make the calculation of the integrals faster (I cannot give you exact figures, since I never did any rigorous runtime tests, but I can attest that for the situations I tested, it did indeed run faster – also the ground state energy was indeed the same).

My problem was that I did not find any instructions on how to make PyQuante work with libint.
So here are my own (they works for PyQuante 1.6.4 and libint-1.1.4)

Download libint 1.1.4 and then compile it by doing:

./configure --enable-shared

If using Ubuntu (and possibly other Linux distros) you may get an error similar to:

/bin/sh ./libtool --mode=compile g++ -DHAVE_CONFIG_H -D_ISOC99_SOURCE=1 
-I./include -I/data/development/libint-2.0.3-stable/./include -O2 -c src/ -o src/HRRPart1bra0ket0gf.lo
./libtool: 1: eval: base_compile+= g++: not found
./libtool: 1: eval: base_compile+= -DHAVE_CONFIG_H: not found
./libtool: 1: eval: base_compile+= -D_ISOC99_SOURCE=1: not found
./libtool: 1: eval: base_compile+= -I./include: not found
./libtool: 1: eval: base_compile+= -I/data/development/libint-2.0.3-stable/./include: not found
./libtool: 1: eval: base_compile+= -O2: not found
./libtool: 1: eval: base_compile+= -c: not found
./libtool: 1: eval: CC_quoted+= gcc: not found
./libtool: 1: eval: CC_quoted+= g++: not found
libtool: compile: you must specify a compilation command
libtool: compile: Try `libtool --help --mode=compile' for more information.
make: *** [src/HRRPart1bra0ket0gf.lo] Error 1

This is due to the fact that in Ubuntu 6.10, the default system shell, /bin/sh, was changed to dash.
Changing the shell to bash by adding

SHELL = /bin/bash

to the Makefile should solve the problem.
Now download PyQuante 1.6.4 and copy the folder libint-1.1-4 to PyQuante’s folder and install PyQuante with libint support by running

sudo python install --enable-libint


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Everything should be working now, and the integrations should now be now faster than before!

Installing and properly configuring Cygwin

Cygwin is a Linux-like interface for Microsoft Windows. It’s very neat. In my case I have a Lenovo X230 tablet and it’s just less of a nuisance and more functional to use MS Windows. Also, even though Wine works very well, it’s still not perfect. That way I have Windows installed and simply use Cygwin to easily access to my Linux server and have that Linux feel in Windows.

Cygwin is very easy to install and can be downloaded from here. There is a 64-bit version, but at the moment it is lacking some useful packages like rxvt. During install you will be prompted to choose a mirror (most work well) and which packages to install. Be sure to install wget, because we will need it for customizing cygwin later.

Now, if you need to install some packages on Cygwin, the usual procedure is to re-run the setup executable. This is not very convenient. Fortunately there’s a better way. Stephen Jungels has written a command line installer for Cygwin which works just like apt-get. You should visit apt-cyg webpage here and install it.

Now, in Cygwin to access VI Improved, which comes selected for install by default, you should type vi and not vim (as you may be used to) .

The problem is that if you try to run vim, it enters in compatibility mode, and some stuff simply won’t work. You may notice that you cannot see in the bottom in which mode you are, backspace won’t probably work, etc… This is because it is lacking a .virc file in your home folder.
Simply creating an empty .virc file using touch .virc in your home folder should be enough.

Now, something I strongly recommend, is to install rxvt. It is, in my opinion, a better shell than the standard Cygwin one.
If you have installed apt-cyg, simply issuing apt-cyg rxvt should be enough to install it (mind you, that the 64-bits version of Cygwin is, at the moment I am writing this post, lacking rxvt!)

You may notice that once you start rxvt the background, foreground, and font are quite different from the usual shell. To configure this, as well as the appearance of the window, you need to create in your home directory a file named .Xdefaults.
Then add the following to this file:

Rxvt*geometry: 100x30        ! WxH
Rxvt*background: #000000
Rxvt*foreground: #ffffbf
Rxvt*scrollBar: True
Rxvt*scrollBar_right: True
Rxvt*font: Lucida Console-12
Rxvt*SaveLines: 2000
Rxvt*loginShell: True
! VIM-like colors
Rxvt*color0:    #000000      ! pure black
Rxvt*color1:    #FFFFFF      ! pure white
Rxvt*color2:    #00A800      
Rxvt*color3:    #FFFF00
Rxvt*color4:    #6495ED      ! cornflower blue
Rxvt*color5:    #A800A8
Rxvt*color6:    #00A8A8
Rxvt*color7:    #D8D8D8
Rxvt*color8:    #000000
Rxvt*color9:    #FFFFFF      ! pure white
Rxvt*color10:   #00A800
Rxvt*color11:   #FFFF00
Rxvt*color12:   #6495ED      ! cornflower blue
Rxvt*color13:   #A800A8
Rxvt*color14:   #00A8A8
Rxvt*color15:   #D8D8D8

We are still not done. You it may happen that if you SSH into a Linux machine and try to use some programs such as less, man or top they will fail with errors such as “unknown terminal type”. The problem is the TERM variable which rxvt sets to rxvt-cygwin which is unrecognized by your remote machine.
Simply add

case "$TERM" in

to the .bashrc file in the home folder of your remote machine and things should work flawlessly.
An alternative, in case you are using a shortcut to run rxvt, is to add the flag -tn xterm.

If you are planing on using X, you should install the package xinit in Cygwin.
You can then run the Xserver and open an X terminal by running:




For more information on this, you should consult the Cygwin/X page.

Note: If you don’t see any X window and find yourself unable to start the X server, it may already be running. You may need to kill the process


using the task manager.

There are many other things that you can customize in Cygwin, but this should be enough to get you going.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.