views:

1138

answers:

6

As you may know, Fortran is a language for scientific computing. however, the kernel of the most famous high level language for scientific computing has been written in C instead of Fortran. Why?

A: 

What Fortran? Are you from the stone age?

hasen j
wow, too many downvotes! Are you telling me Fortran is still useful?
hasen j
Fortran is still heavily used in the sciences. Also, you don't seem to have understood the question: Matlab *is* from the stone age, (well, bronze age), so its not being written in Fortran is rather surprising, given its age.
David M.
+3  A: 

You can find some information on the origins of Matlab by viewing a video on their web site, specifically on the page of the Chief Scientist, Cleve Moler.

tvanfosson
+8  A: 

I want to qualify this answer with the following statement - I know nothing about Fortran or Matlab

However i think you almost answer your own question in your question.

Why would you not use a scientific language to write a kernel. You would use the most appropriate language for the task at hand. A kernel would be best written in C as it is better suited to working with low level tasks like kernels (working with memory management etc)

A Kernel is not a scientific application therefore you wouldn't use a scientific language.

Again, i may be completely off the mark but i think that is a highly logical answer.

A quick note: the kernel of a computational software package is not the same thing as the kernel of an operating system. In the first case, the kernel is the underlying computational engine, which the user interacts with through a higher-level user interface (e.g. the MATLAB command window or the Mathematica notebook interface).
las3rjock
+8  A: 

My understanding is that Matlab is written in a hodge-podge of languages, including but not limited to C, FORTRAN, Java, and Matlab itself.

I believe it employs best-of-breed libraries behind the scenes, including LAPACK (FORTRAN), FFTW (C) and probably many more.

I think the original version was pure FORTRAN, but as it became more polished and commercial they weren't shy about using the right language for each component. I don't blame them--I'd hate to write a parser or a GUI in FORTRAN!

Drew Hall
I wouldn't like to write a parser even in C. But yes, you're correct - early versions were written in fortran, then they switched to C (completely rewrote the thing) at some point.
ldigas
Of course, nowadays there's a bunch of stuff in there.
ldigas
Jason S
Someday I will learn how to use Java objects within Matlab... someday.
Trevor Boyd Smith
+8  A: 

Guessing on the basis of experience:

Memory management.

Fortran (well, the fortran that was around when matlab was written) has no support for dynamic memory management. Which makes it a pain for big work (see, for instance, CERNLIB).

CERNLIB solves the problem by allocating a whopping big array in a common block, and implementing a malloc like (de)allocator for the cells of the array. Clunky but working: array offsets are pointer equivalents, and away you go...

dmckee
So CERNLIB basically hacked in the same functionality of malloc()/free() into Fortran77? Why didn't they just use C?
Trevor Boyd Smith
Parts of CERNLIB date to the early 1960s. By the time c came around they already had a lot of code. I believe large parts were rewritten several times, but the deal breaker was: physicists. Even into the '80s most physicists only spoke fortran.
dmckee
*shakes head*. I hate dinosaurs ("dinosaur" is my euphemism for people who suceeded in some manner and are now stuck in their way and refuse to change). I guess a meteor came and wiped out all the Fortran Physicists in the 80s? What specifically was the "meteor" that forced the Physicists to change in the 80s?
Trevor Boyd Smith
"What specifically was the "meteor" that forced the Physicists to change in the 80s?" Incoming students who knew Pascal and c? Or the rise of unix? But the change was/is incremental and is still going on. CERNLIB has been largely superceded by ROOT, but there are millions of LOC out there, and it is still actively supported. Heck I wrote code against it just 18 months ago.
dmckee
@Trevor Boyd Smith - I wouldn't say there was a meteor at all. Nowadays, most engineers and physicists are familiar with fortran - at my college (nav. arch) we were taught fortran, some were taught py and matlab, but none learned C. C is the language of cs students - most engineers don't use it, unless they're programming inclined (but that's their own personal business). And let's be frank, when it comes to scientific purposes fortran is still much better suited than C, python, or most others (matlab is nice, but has some drawbacks when performance comes into the run) - just
ldigas
think "multidimensional arrays" (yes, try doing tensor calculus in C and in fortran).
ldigas
Pascal is another story - it is a nice language (I've noticed it is, and has always been, popular among mathematicians) - but nowadays its future is somewhat uncertain. Apart from Borland's Delphi (and Borland's future is also somewhat uncertain) it lacks support and its userbase is somewhat fading (these are just my observations).
ldigas
> I hate dinosaurs. You have a library with KLocs of Fortran written by experts over 40years - you are just going to convert it to C. Do you understand all those equations? All the rounding errors? All the edge cases? And you are going to do this in a language that doesn't have matrices or complex numbers as standard yet.
Martin Beckett
@ldigas: In my end of the business (particle physics), many student *don't* know or learn fortran anymore. C++ predominates, presumably because it's the language of ROOT. They also don't *write* the tensor manipulation code, they just bring in a robust library.
dmckee
@mgb - I really can't understand from your tone whether you're being sarcastic or not. Fortran does have matrices and complex as a standard.
ldigas
@dmckee - That probably varies from country to country. I know several chaps in your area of work here (Croatia); they still use it extensively. With the new standard, it is very easy to do that kind of calculations directly (indexing in fortran 95 and onwards is very nicely handled). In my area of work, unfortunatelly, since not many people do it, there is no such thing as robust libraries (or libraries of any kind) but there is a great need for intensive computations in several fields.
ldigas
But, just not to get this going in another flame war - I'd say that we can all agree - "use what you wish, and what you think is a good tool for the job" - those three, c, fortran and matlab - are very nicely interconnectible today, so ...
ldigas
+13  A: 

To answer your original question: Matlab was originally written in Fortran. But one of the first things that the creator of Matlab, Cleve Moler, and his partner did in 1983 was to rewrite the entire Matlab app in C:

Jack Little left his job at the consulting company and bought a new COMPAQ portable computer at Sears. The machine had only 256 KB of memory and no hard disc; Jack had to swap 5-1/4-inch floppies to compile programs. Jack and Steve took a year and a half to re- write MATLAB in C, adding new features they had envi- sioned. (The Growth of MATLAB and The MathWorks over Two Decades)

I think a more relevant question for today would be to ask: why did they switch to C back in 1983 ?

My guess is that C probably had certain features that Fortran did not have and thus the switch was more out of necessity and in the interest of code maintainability.

One killer feature missing in Fortran77 was dynamic memory allocation (Dynamic memory allocation was only added in Fortran90). Getting around that single limitation would be a horrible deal breaker in my opinion.

EDIT:

dmckee's answer has a great explanation of why using Fortran77 would be painful.

Trevor Boyd Smith
@Trevor Boyd Smith, Cleve Moler or Steve Moler? The first paragraph of your answer reads that Cleve Moler and his partner rewote MatLab, but in the quote it says Jack and Steve.
Geoffrey Van Wyk