Python Lambda Functions


The lambda keyword creates a small anonymous function or procedure.

It creates a function object the same way that the def keyword does, but with the following rules:

1. The lambda construct does not have a return statement.
2. The body can contain only a single expression.
3. The expression may yield a value.
4. The expression may be a conditional statement which returns a value.

Lambda may also be used to create anonymous procedures for use in GUI callbacks.

Lambda Examples

Here is a simple example of a lambda function.

g = lambda x: x**2
print g(8)

The result is: 64

Here is an example using two arguments:

g=lambda x,y: x+y
print g(2,3)

The result is: 5

A lambda function may be used inside a regular function.

def transform(n):
return lambda x: x + n
f = transform(3)
print f(4)

The result is: 7

Map function

A lambda function may be used with a map function to process a sequence of values.

g = lambda x: x**2
temp=(1, 2, 3, 4)
f = map(g, temp)
print f

The result is: [1, 4, 9, 16]

Reduce function

The function reduce(func, seq) continually applies the function func() to the sequence seq. It returns a single value.
The following sum a list of numbers.

reduce(lambda x,y: x+y, [47,11,42,13])

Result: 113

These statements find the maximum in a list.

f = lambda a,b: a if (a > b) else b
reduce(f, [47,11,42,102,13])

Result: 102

Calculate the sum of the numbers from 1 to 100:

reduce(lambda x, y: x+y, range(1,101))

Result: 5050

* * *

Tom Irvine

Mixed Fortran & Python


The scripts on this page require the utility module

* * *


f2py is a Fortran to Python interface generator.

* * *

Sample Project

The following project was performed on Ubuntu.

Here is a project that solves the generalized eigenvalue problem.

The mass & stiffness matrices are read in by a Python script.

The Python script then passes the arrays to a Fortran program for the number-crunching.

The Fortran program then returns the eigenvalues & vectors to the Python script.

The project includes an option for the standard eigenvalue problem.

Note that this project requires Lapack.

Also the python-dev header & static library must be installed.

* * *

Source Code

Here are the two Fortran scripts and their compilation commands:  eigen_general.f

f2py -c -m eigen_general eigen_general.f –fcompiler=gnu95 -llapack

f2py -c -m eigen_general eigen_general.f –fcompiler=gnu95 -llapack
Here is the Python script:

python -i

Note that the Python script imports the Fortran subrountines via:

import eigen_standard import eigen_general
Related Posts

Generalized Eigenvalue Problem

Vibrationdata Blog Entry – Instructions for the generalized eigenvalue problem including Lapack installation.

* * *

– Tom Irvine

Python Rainflow Fatigue


The scripts on this page require the utility modules

* * *


A script for the ASTM E 1049-85 (2005) Rainflow Counting Method is given at

A GUI version using Tkinter is given at:

* * *

Dirlik Method

A number of methods have been derived for performing rainflow cycle counting for a response PSD. The Dirlik method is an example of a semi-empirical method for cycle identification. A script for performing this method is given at:

The following script calculates the response of a single-degree-of-freedom system to an acceleration PSD base input, with an option for Dirlik rainflow cycle counting:

This script calculates the Dirlik Fatigue Damage Spectra for an acceleration PSD base input:

* * *

Matlab Script

A Matlab script is posted at the Vibrationdata Blog: Matlab Mex

* * *

Fatigue Damage Spectra from Time History

A script for fatigue damage spectra is given at:

* * *

See also:

Rainflow Cycle Blog Entry

Miner’s Cumulative Fatigue via Rainflow Cycle Counting

Dirlik Blog Entry

Fatigue Damage Spectra

* * *

– Tom Irvine


This is a privately-hosted, non-commercial blog operated in partnership with the NASA Engineering & Safety Center (NESC). Public participation is encouraged.

The purpose of this site is to share knowledge and tips for using Python to solve engineering problems, with an emphasis on structural dynamics, signal processing, and vibroacoustics. Python is an interpreted, interactive, object-oriented, open-source programming language. Add-on packages such as NumPy, SciPy, and Matplotlib make Python an attractive alternative to Matlab, and there is an emerging trend for some Matlab users to switch to Python. My colleagues at the Aerospace Corporation and at NASA are among those learning Python.

Python works on almost all computer platforms including Windows, Mac OS and Linux, and there are a number of different installation options.

Due to its elegance and generality, Python is gaining traction as a language for teaching computer programming. MIT, among others have gone to Python for their introductory computer science classes.

Also, I have posted a Python GUI package for vibration analysis at: vibrationdata Python GUI page.

– Tom Irvine