Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Tuesday, March 18, 2014

Fortran and Octave/Matlab: Variable File Names

Suppose you want to write to files with names that are not known before compile time, or loop over a set of related filenames. For example, you may want to write to files file0001.txt, file0002.txt, ... so on.

Doing this in Octave/Matlab is quite easy: For example:
In Fortran 90, you do something similar

It is easy to extend this idea to create files using loops.

Thursday, September 22, 2011

Speeding up Matlab code

Here is a nice video from Mathworks on different ways to speed up Matlab programs. It covers a bunch of techniques including  preallocation and vectorization, which get you a great deal of bang for the buck.

It is nearly an hour long.

Saturday, September 17, 2011

How to write gzipped files from a C++ program?

Every so often, you write a C++ program that generates a ton of output. There are cases when you want to write continuously to a compressed gzipped file from the C++ program (rather than using gzip to compress a large file after the program has finished running).

One solution is to use the gzstream library. It "is a small C++ library, basically just a wrapper, that provides the functionality of the zlib C-library in a C++ iostream. It is freely available under the LGPL license."

How do you actually do it? It is actually quite simple in a standard *nix system.
  1. Download and unarchive the tarball into a folder gzstream.
  2. Type "cd gzstream" and then "make" at the command prompt. It should make a library called "libgzstream.a"
  3. Move the folder to an appropriate location if needed.
  4. In the C++ program file: #include the headers gzstream.h, iostream, and fstream.
  5. In your C++ program, say "ogzstream rpout("sigma.gz");", where rpout is the handle and sigma.gz is the filename that you want to write to.
  6. Write to the file using somthing that may look like "rpout << setprecision(4) << sigma << endl;"
  7. Close the file with "rpout.close();"
  8. Finally to compile the C++ program say something like: "g++ program.cpp -I./gzstream -L./gzstream -lz -lgzstream". Here I assume that the gzstream directory is located in the same directory as the C++ program. If this is not true, then you have to change the -I and -L location tags.
  9. You are now good to go, with "a.out".

Friday, June 10, 2011

Links

1. In defense of inefficient code: Mike Croucher makes a case for working-but-not-particularly-efficient code written by practitioners in high-level languages (Matlab, Mathematica, python). As he puts it:
It comes down to this. CPU time is cheap. Very cheap. Human time, particularly specialised human time, is expensive.
and also:
In my opinion, high level programming languages such as Mathematica, MATLAB and Python have democratised scientific programming. Now, almost anyone who can think logically can turn their scientific ideas into working code. I’ve seen people who have had no formal programming training at all whip up models, get results and move on with their research. Let’s be clear here – It’s results that matter not how you coded them.
I often use these high-level languages to rapidly prototype new ideas. If and when required, it can always be translated into C++ or Fortran. Also from a practical perspective inefficient code that runs 10 times slower than highly optimized code is acceptable, if it still takes only 10 seconds to run.

2. Geeky jokes on Tanya Khovanova's Blog:

I like this one:
I just learned that 4,416,237 people got married in the US in 2010. Not to nitpick, but shouldn’t it be an even number?
Obviously, you can understand the audience she caters to from this comment.

Wednesday, October 20, 2010

Computing the eigenvectors of a 3x3 symmetric matrix in C++

Every once in a while Google makes me wonder how people ever managed to do research 15 years ago.

Just today, I had to find a quick C++ routine to compute the eigenvalues and eigenvectors of a well-behaved symmetric matrix (principal components of the gyration tensor of a 3D random walk, this time). I found it here.

It took me less than 5 minutes to google it down, download it, and test it.

Amazing.

Friday, September 3, 2010

"Publish"ing in Matlab

I use GNU Octave as the glue to post-process and make preliminary visualizations most of the time. Every once in a while, however, I need to use its commercial cousin, Matlab.

This Fall, I am teaching an undergrad numerical analysis class, and realized that Matlab has this interesting documenting feature called "Publish", which lets you use a double percent sign %% in addition to the regular % sign used to begin a comment. Using these %% signs, you can divide your code into "cells" which is helpful for selectively testing individual cells -- an added benefit.

Check out a video on how to use this feature at Mathworks.

By proper use of %% and % comments, one can actually produce a nice document (in html, LaTeX or some other options), which is ideal, since now students can submit only a single "M-file", and I can check whether the program works, and the accompanying documentation in one shot.

Wednesday, February 17, 2010

Can't engineers program anymore?

I taught junior-level thermodynamics three times in a row as a fresh assistant professor at Florida State. There was a small Matlab (actually Octave, but doesn't really matter) component to the class, so that we could solve some realistic problems without sweating the algebra or arithmetic, and let the computer do the dirty work.

But instead of being wildly popular, it was easily the most-unloved portion of an otherwise well-received course. I have talked to colleagues in other engineering departments here and elsewhere, and the lack of programming skills seems to be a pervasive phenomenon, independent of the rank of the university.

It has indeed become a national epidemic, or maybe perhaps, even a global pandemic.

To me, this seemed unexplainable, at first. Kids these days are exposed to computers and other programmable gadgets at such an early age. They are comfortable with them. How then, could their ability to squeeze performance out of these devices be so poor?

And we are talking Matlab - which if you ask me - is not even a real programming language. I mean there are no classes, structures, libraries, templates, or any of the other hallmarks of a real programming language. Spreadsheets like Excel are all they can handle, and even there, they aren't power-users who exploit the underlying programmability.

One thing I realized gradually, was that electronic equipment and computers have become extraordinarily complex. When I was first exposed to computers, it was easy and fascinating to poke "under the hood", and tweak things. This was true of other electronic equipment like radios, TVs and VCRs

As Feynman puts it (via this amazing blog):
Radio circuits were much easier to understand in those days because everything was out in the open. After you took the set apart (it was a big problem to find the right screws), you could see this was a resistor, that’s a condenser, here’s a this, there’s a that; they were all labeled. And if wax had been dripping from the condenser, it was too hot and you could tell that the condenser was burned out. If there was charcoal on one of the resistors you knew where the trouble was. Or, if you couldn’t tell what was the matter by looking at it, you’d test it with your voltmeter and see whether voltage was coming through. The sets were simple, the circuits were not complicated. The voltage on the grids was always about one and a half or two volts and the voltages on the plates were one hundred or two hundred, DC. So it wasn’t hard for me to fix a radio by understanding what was going on inside, noticing that something wasn’t working right, and fixing it.
And as a commentator on the same posting wrote:
The problem, I think, is much wider and deeper than just for physics. Philosopher Stephen Clarke has argued that we are currently in a unique transition point in the history of technology: From a time when most people could understand (or could quickly learn) how most technologies they encountered in their everday life worked, to a time when almost no ordinary person can understand how any everday technology works.
I will blog more about this topic in the near future, but if you have any ideas and suggestions to make programming "cool" do let me know.