开发者

Is Python appropriate for algorithms focused on scientific computing? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. 开发者_开发技巧 Closed 11 years ago.

My interests in programming lie mainly in algorithms, and lately I have seen many reputable researchers write a lot of their code in python. How easy and convenient is python for scientific computing? Does it have a library of algorithms that compares to matlab's? Is Python a scripting language or does it compile? Is it a great language for prototyping an algorithm? How long would it take me to learn enough of it to be productive provided I know C well and OO programming somewhat? Is it OO based?

Sorry for the condensed format of questions, but I'm very curious and was hoping a more experienced programmer could help me out.


How easy and convenient is python for scientific computing?

Scipy/NumPy.

Does it have a library of algorithms that compares to matlab's?

Yes.

Is Python a scripting language or does it compile?

Interpreted.

Is it a great language for prototyping an algorithm?

Yes.

How long would it take me to learn enough of it to be productive provided I know C well and OO programming somewhat?

Depends.

Is it OO based?

Yes.


How easy and convenient is python for scientific computing?

Very! You should try attending the SciPy conferences (every year there's one in the US and one in Europe) to get a real feeling for that, but even just the rest of the scipy.org site should give you some impression.

Does it have a library of algorithms that compares to matlab's?

I don't know matlab, but the amount of stuff available in/for Python is staggering.

Is Python a scripting language or does it compile?

Python is a language, and it offers many implementations (all open-source).

The most popular one, CPython, compiles sources to its own bytecode which its virtual machine then executes (the compilation is very fast and happens transparently when needed, but compiled files are typically stored on disk and recompiled only when needed). That's very similar to Java/JVM or C#/.Net, except the compilation step can be subsumed with the execution step (but of course you can have a build system which compiles ahead-of-time, if you want).

Jython compiles to JVM bytecode, which a JVM then executes; Microsoft's IronPython (their first fully open source project, I believe) compiles to CRL (".Net bytecode") which .Net and Mono can then execute. They both support both just-in-time and ahead-of-time compilation to their respective bytecodes.

PyPy can compile Python sources to many things, including (for a subset of Python) directly (ahead-of-time) to native machine language or (for all of Python) to an intermediate code which is then compiled to machine language in just-in-time fashion. PyPy is incredibly flexible in terms of the kind of build systems you can set up. (Its name comes from the fact that it's coded in Python itself, and that's surely still a plus in many terms, but the speed of the code it makes, and its flexibility, are its biggest strengths today).

These four implementations are all production quality at this time (historically, they became so in the order I've listed -- PyPy most recently, and actually pretty recently indeed, but I like what I see there very much these days).

Is it a great language for prototyping an algorithm?

I can't think of a better one; see chapter 18 of the Python Cookbook, especially the introduction by Tim Peters, for more. That intro is entirely readable in the Google Books link I just gave, and I really can't do it justice in what's already going to be a long SO answer; please click on the link and read that intro!

How long would it take me to learn enough of it to be productive provided I know C well and OO programming somewhat?

When I first met Python, after browsing through the tutorial, I decided to give it a try when I had a free weekend with my family away: I'd devote one weekend (Friday 6pm to Sunday midnight, or, well, wee hours on Monday perhaps) to learning the language by doing in it a CGI web app to compute and show various kinds of bridge probabilities (as a bridge enthusiast, but known in the field mostly through my probability and computer work about it, it's a problem I've long loved: I learned Fortran back in my freshman year, though at that time as an EE major I wasn't supposed to do programming until junior year, by punching cards to solve that kind of problem;-).

Of course I didn't expect to finish the task from scratch in 54 hours or so (minus sleep time;-) while teaching myself the language and its library (CGI and the needed algorithms I already knew well), but I wanted to see how far I would get (evaluating Python vs the other languages I was a guru in at the time, mostly perl and C++).

Less than 24 hours later (admittedly having slept little that night, I was just too excited), I stepped back and had to admit that I was finished -- not only did my little CGI web app have all the functionality I had had in mind, but I had also made it able to give output in different natural languages by building from scratch a little templating system (I knew there were plenty -- that's why I named mine yaptu, "Yet Another Python Templating Utility" -- but I just didn't have time to learn anything outside of the language and standard library... rolling my own was faster;-).

That's when I irretrievably fell in love with Python. Not long after, I ended up leaving my existing high-flying career for a spell writing books and freelancing with Python, and a few years later I moved across an ocean and two continents to join one of the largest companies extensively using Python (my current employer, Google) -- in the meantime having re-married (to my current wife, Anna -- she was also co-author in one of my books and the first woman Member of the Python Software Foundation). Our "vanity" license plate reads P♥THON...;-). So, OK, I'm biased. But it all started with those <24 hours in which I accomplished more than I had hoped to do in >54 hours (despite being, like all SW developers, an incurable optimist whenever it comes to "how long will it take me to do X" for any SW-centered X;-).

Is it OO based?

Yes, but multi-paradigm (like C++... but even more than C++) -- you don't have to use classes when you don't need them, and it has reasonable support for functional programming too (definitely not as deep as "true" FP languages like Haskell, but still very useful for many tasks).


It bytecompiles, and then sends the bytecode through an interpreter.

Official Tutorial

NumPy

You are now set.


Answer your question one by one:

How easy and convenient is python for scientific computing?

One great point of python is that it provides very intuitive way of writing code. The powerful embedded data structure such as dictionary and list would help you a lot in scientific computing. Besides, as a dynamic language, you do not need to deal with many low level detail which you have to do in C.

Does it have a library of algorithms that compares to matlab's?

Indeed, python has a great number of library of algorithms. For example, you can use NumPy and SciPy to support large, multi-dimensional arrays and matrices compuation. And you can find more detail in those links:

  1. official site of numpy: http://numpy.scipy.org/
  2. wikipedia of numpy: http://en.wikipedia.org/wiki/NumPy

Besides, python also has library to support network analysis. For example, networkx library is a great tool for graph analysis.

Is Python a scripting language or does it compile?

Generally, python is designed as a scripting language. But there is also tools for compile, for example, py2exe. I recommend you to use python as a scripting language. I think you may have performance concern about python. And a usually applied solution is to write those performance critic module in c/c++, and glue them by SWIG.

Is it a great language for prototyping an algorithm?

Sure it is. With the rich support of embedded data structure, you can quick implement some complicated algorithm with shorter code compared to C/C++. Typical example is as following:

 //C++ loop a one dimensional array and print value
  const int N = 100; 
  int A[N];
  for (int i = 0; i < N; ++i)
      cout << i;

#python loop a one dimensional array and print value
for i in range(100):
   print i

And as a scripting language, you do not need compile, run, recompile, run, it would save you a lot of time.

How long would it take me to learn enough of it to be productive provided I know C well and OO programming somewhat? Is it OO based?

Python is different from C. You may find it is not very convinent to write c-style code in python. But python is easy to start, most syntaxes are just plain English. Moreover, there are some great tutorials for python. For example, dive into python is very nice for beginners.

Python provides mechanism for OO based programming.


I've used Python for 6 years for scientific computing. Having come from matlab/IDL, it was very easy to do the switch since it's also interpreted.

There are 3rd party tools such as SciPy and Matplotlib to help specifically with data analysis /visualization. Also, if you look in Amazon there are books targeting this audience.

Python is also very often used to teach programming because of it's simple yet powerful syntax.


If you know C and some object oriented programming you'll learn python very quickly - most of the key things in just a few hours of reading / tinkering with it. Here are the main differentiators: - Designed to be easy to learn quickly and to encourage writing readable code. - Has the fewest warts of any object oriented programming language. - Doesn't force you to write object oriented code however. - Makes it easy to write scripts that can be executed stand alone or imported into others.

Object Oriented features: - True polymorphism. Unlike C++ and derivatives such as Java you don't have to stand on your head to make your code polymorphic, generic and reusable - even in ways you didn't think about beforehand. This is because although it's strongly typed it's not statically typed. So as long as your objects have the expected methods or attributes that some piece of code wants, it'll work. This is known as duck typing.
- Introspection - so you can easily check if a method or attribute is present before accessing it. Also is great for debugging. - You can add attributes and even methods to objects at run time. Very malleable code. - Supports multiple inheritance.

Problems: - Typically faster than Ruby but sometimes slower than Java. - You have to get used to seeing the word self all over the place. - Hard for C style developers to let go of having to type curly braces and semi-colons. Seeing uncluttered code like that can make you feel like there's just something missing.

There's a programming style guide that all python developers are meant to follow: http://www.python.org/dev/peps/pep-0008/

There's a variant of it that's faster than the main version and supports an easy to use concurrency mechanism. It's known as Stackless Python because it does away with using the C stack. EVE Online is written in this language.

Here's an example of something I suppose could be considered scientific programming - adjusting sound waves - it's simply cool - scroll down to the bottom for source and see how relatively simple it is. http://musicmachinery.com/2010/05/21/the-swinger/


Is Python appropriate for algorithms focused on scientific computing?

Yes


You asked about compiled/interpreted. If your concern here was execution speed, there is an aspect of python that doesn't seem to have been covered explicitly---you can use tools like SWIG and boost.python to make your lightning fast C/C++ packages appear in your python as modules. Once you get to the module, it will run at the speed/efficiency of the underlying C/C++ implementation. Many modules are available that take advantage of this.

So, you get to do all the organizational stuff in clear, flexible, easy to learn Python, and then when you get to your heavy number crunching, you get to hand off the problem to a fast, efficient routine. You get the best of both worlds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜