3. Manipulating values in Python#

One gives here some examples of manipulating values obtained previously in the form of lists or Numeric tables.

Numeric is an optional Python module (i.e. not included in the Python distribution provided on www.python.org) but essential for using*Code_Aster*, so you can import Numeric on any Code_Aster installations.

3.1. With Python lists#

Python lists are easily manipulated using loops. Let’s take the example of the [U2.51.01] notice for \(–10.<x<10.\) in \(100\) steps:

\(y=-1.5\)

if \(\mathrm{sin}(x)<0\)

\(y=-5.\)

otherwise

So we’re trying to build two real lists, \(\mathrm{lx}\) and \(\mathrm{ly}\).

As always in Python, there are several ways to do it, more or less simply, more or less elegantly!

x0=-10.

step=20./100

lx= []

or

lx= [x0+i*pas for i in range (101)]

For in range (101):

lx.append (x0+i*not)

def f (x):

if sin (x) <0. :

return -1.5

Else:

Return -5.

ly=map (f, lx)

which applies function \(f\) to all elements in \(\mathrm{lx}\)

We can draw this curve using the keywords ABSCISSE and ORDONNEE from IMPR_FONCTION (cf. [§ 4]).

3.2. With Numeric boards#

The manipulation of data in the form of Numeric tables is simplified by the use of very powerful operations (called ufunc) on the entire array (in the following example we use sin ()). Numeric also manages multi-dimensional arrays.

Continuing with the previous example:

from Numeric import*

lx = arrayrange (-10., 10.+0.2, 0.2, Float)

ly = array (map (f, lx))

or without using f:

ly = -1.5*less (sin (lx), 0.) + (-5.)* (1.-less (sin (lx), 0.))

Note that map () returns a list and not a Numeric array. The second expression is between 10 and 20 times faster on very large arrays (\({10}^{5}\)\({10}^{6}\) terms), which is however not often the case with functions or tables from Aster.

We can draw this curve using the keywords ABSCISSE and ORDONNEE of IMPR_FONCTION, taking care to convert Numeric tables into lists, for example (cf. [§ 4]):

ABSCISSE = lx.toList ()