光的世界︰派生科學計算七

送子由使契丹蘇軾

雲海相望寄此身,那因遠適更沾巾。
不辭驛騎淩風雪,要使天驕識鳳麟。
沙漠回看清禁月,湖山應夢武林春。
單於若問君家世,莫道中朝第一人。

 

既談派生科學計算,如果沒介紹『SciPy』套件集成,當真不道地也。無奈耶雲海無涯!不得不屢屢遠謫矣?

scipy-logo

Scientific Computing Tools for Python

 SciPy refers to several related but distinct entities:
  • The SciPy Stack, a collection of open source software for scientific computing in Python, and particularly a specified set of core packages.
  • The community of people who use and develop this stack.
  • Several conferences dedicated to scientific computing in Python – SciPy, EuroSciPy and SciPy.in.
  • The SciPy library, one component of the SciPy stack, providing many numerical routines.

The SciPy Stack

Core Packages

  • Python, a general purpose programming language. It is interpreted and dynamically typed and is very suited for interactive work and quick prototyping, while being powerful enough to write large applications in.
  • NumPy, the fundamental package for numerical computation. It defines the numerical array and matrix types and basic operations on them.
  • The SciPy library, a collection of numerical algorithms and domain-specific toolboxes, including signal processing, optimization, statistics and much more.
  • Matplotlib, a mature and popular plotting package, that provides publication-quality 2D plotting as well as rudimentary 3D plotting
  • pandas, providing high-performance, easy to use data structures.
  • SymPy, for symbolic mathematics and computer algebra.
  • IPython, a rich interactive interface, letting you quickly process data and test ideas. The IPython notebook works in your web browser, allowing you to document your computation in an easily reproducible form.
  • nose, a framework for testing Python code.

Other packages

There are many more packages built on this stack – too many to list here. This is a brief overview of a few major ones:

  • Chaco is another Python plotting toolkit designed from the ground up to be great for embedded, interactive plotting. It is built on Traits, both are part of the Enthought Tool Suite.
  • Mayavi is a powerful and user-friendly framework for 3D visualization, built on top of the award winning Visualization Toolkit, VTK.
  • Cython extends Python syntax so that you can conveniently build C extensions, either to speed up critical code, or to integrate with C/C++ libraries.
  • Scikits are extra packages for more specific functionality. scikit-image and scikit-learn are two of the most prominent.
  • h5py and PyTables can both access data stored in the HDF5 format.

See the Topical Software page for more.

 

未必時宜的講︰

幾乎每個核心軟件都需要一本書之份量才能說得清楚,於是也只能請讀者自己找書來看,或是參考線上文件︰

Documentation

Documentation for core SciPy Stack projects:

The Getting Started page contains links to several good tutorials dealing with the SciPy stack.

 

借著導引指南嘗試摸索探究︰

Getting Started

This page is intended to help the beginner get a handle on SciPy and be productive with it as fast as possible.

What are NumPy, SciPy, matplotlib, …?

SciPy and friends can be used for a variety of tasks:

  • NumPy‘s array type augments the Python language with an efficient data structure useful for numerical work, e.g., manipulating matrices. NumPy also provides basic numerical routines, such as tools for finding eigenvectors.
  • SciPy contains additional routines needed in scientific work: for example, routines for computing integrals numerically, solving differential equations, optimization, and sparse matrices.
  • The matplotlib module produces high quality plots. With it you can turn your data or your models into figures for presentations or articles. No need to do the numerical work in one program, save the data, and plot it with another program.
  • Using IPython makes interactive work easy. Data processing, exploration of numerical models, trying out operations on-the-fly allows to go quickly from an idea to a result. See the IPython site for many examples.
  • There is a sizeable collection of both generic and application-specific numerical and scientific code, written using Python, NumPy and SciPy. Don’t reinvent the wheel, there may already be a pre-made solution for your problem. See Topical Software for a partial list.
  • As Python is a popular general-purpose programming language, it has many advanced modules for building for example interactive applications (see e.g. wxPython and Traits) or web sites (see e.g. Django). Using SciPy with these is a quick way to build a fully-fledged scientific application.

How to work with SciPy

Python is a programming language, and there are several ways to approach it. There is no single program that you can start and that gives an integrated user experience. Instead, there are several possible ways to work with Python.

The most common is to use the advanced interactive Python shell IPython to enter commands and run scripts. Scripts can be written with any text editor, for instance Emacs, Vim or even Notepad. Some of the packages such as Python(x,y) mentioned in Installing the SciPy Stack also offer an integrated scientific development environment.

Neither SciPy nor NumPy provide plotting functions. There are several plotting packages available for Python, the most commonly used one being matplotlib.

Learning to work with SciPy

To learn more about the Python language, the official Python tutorial is an excellent way to become familiar with the Python syntax and objects.

One way of getting a handle on the scientific computation tools in Python is to take a look at the following online resources:

In addition, a number of books have been written on numerical computation in Python, see for example a Google search on books related to SciPy.

 

自有一番樂趣乎??!!

pi@raspberrypi:~ $ ipython3 --pylab
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: TkAgg

In [1]: from scipy import special, optimize

In [2]: f = lambda x: -special.jv(3, x)

In [3]: sol = optimize.minimize(f, 1.0)

In [4]: x = linspace(0, 10, 5000)

In [5]: x
Out[5]: 
array([  0.00000000e+00,   2.00040008e-03,   4.00080016e-03, ...,
         9.99599920e+00,   9.99799960e+00,   1.00000000e+01])

In [6]: plot(x, special.jv(3, x), '-', sol.x, -sol.fun, 'o')
Out[6]: 
[<matplotlib.lines.Line2D at 0x722c6470>,
 <matplotlib.lines.Line2D at 0x722c6650>]

In [7]: 

 

scipy-gs