If you already have Cygwin installed on your machine, you can still follow the steps
below. The setup program, which is used to manage the Cygwin installation, "knows"
which packages of Cygwin you have in your previous installation. That is, information
on installed packages is kept in the /etc/setup/ directory of your Cygwin installation.
So do not worry about starting from scratch.
$ ls -l
(list files in current directory)$ cygcheck -c
(list installed packages in alphabetic order)$ cd c:
(change to C-directory)
$ gcc --version
(compiler version)
$ gcc --help
(for a list of options)
$ g++ --version
(compiler version)
$ g++ --help
(for a list of options)
$ g77 --version
(compiler version)
$ g77 --help
(for a list of options)
$ python setup.py install
>>>from Numeric import sin
>>>print "sin(1) = ", sin(1)
$ python setup.py install
$ f2py -ver
(f2py -h
for options and help):
$ ./configure
$ make
$ make install
$ swig -version
(swig -help
for options and help):
hello.f
:
C file: hello.f:
C Simple fortran example
C Program name:
program hello
C Variable declaration:
integer x,n
real y
C Print Hello from Fortran!, sin(number):
write(*,*) 'Enter an integer'
read(*,*) x
y = sin(float(x))
write(*,*) 'Hello from Fortran! The sin(',x ,') = ', y
C End of program:
end
hello.exe
in current working directory)
$ g77 -o hello hello.f
$ ./hello
Enter an integer:
2
Hello from Fortran! The sin( 2) = 0.909297407
factorial.f
:
$ ./factorial
Enter an integer:
5
5!= 120
array.f
:
stringfunction.f
,
fortranimport.py
:
str
(type string) to call a specified function:
C file: stringfunction.f
C subroutine myadd:
SUBROUTINE func_str(a,b,str,sum)
real*8 a,b,sum
character*(*) str
Cf2py intent(in) a
Cf2py intent(in) b
Cf2py intent(in) str
Cf2py intent(out) sum
external myadd, mysub_r, mysub_l, mymul
if (str .eq. 'myadd')then
call myadd(a,b,sum)
else if (str. eq. 'mysub_r')then
call mysub_r(a,b,sum)
else if (str. eq. 'mysub_l')then
call mysub_l(a,b,sum)
else if (str. eq. 'mymul')then
call mymul(a,b,sum)
end if
return
end
C subroutine myadd:
SUBROUTINE myadd(a,b,sum)
real*8 a,b,sum
Cf2py intent(in) a
Cf2py intent(in) b
sum = a+b
return
end
SUBROUTINE mysub_r(a,b,sum)
... substraction from right: b - a ...
SUBROUTINE mysub_l(a,b,sum)
... substraction from left: a - b ...
SUBROUTINE mymul(a,b,sum)
... multiplication of a and b ...
stringfunction.dll
in current working directory)
$ f2py -c -m stringfunction stringfunction.f
fortranimport.py
):
>>> import stringfunction
>>> a=1; b=2
>>> print stringfunction.func_str(a,b,"myadd")
3.0
>>> print stringfunction.func_str(a,b,"mysub_r")
1.0
>>> print stringfunction.func_str(a,b,"mymul")
2.0
fortranmath.f
,
fortranmathimport.py
:
C file: fortranmath.f
SUBROUTINE myreverse(list,n,listout)
... array in reversed order ...
SUBROUTINE mysort_inc(list,n)
... sorts array in increasing order ...
SUBROUTINE mysort_dec(list,n)
... sorts array in decreasing order ...
SUBROUTINE mysum(list,n,sum)
... computes the sum of elements in array ...
SUBROUTINE myaverage(list,n,average)
... computes the average of elements in array ...
SUBROUTINE mymax(list,n,max)
... find the largest value in array ...
SUBROUTINE mymin(list,n,min)
... find the smallest value in array ...
fortranmathimport.py
):
>>> import fortranmath
>>> import Numeric
>>> a = Numeric.array([10,2,3,9,4,6])
>>> print "Array: ", a
Array: [10 2 3 9 4 6]
>>> print "Average: ", fortranmath.myaverage(a)
Average: 5.66666666667
>>> print "Min value: ", fortranmath.mymin(a)
Min value: 2.0
>>> print "Sorted increasing: ", fortranmath.mysort_inc(a)
Sorted increasing: [ 2. 3. 4. 6. 9. 10.]
hello.c
:
hello.app
)
gcc -o hello.app -O hello.c -lm
$ ./hello.app 2
Hello from C!
The sin(2) = 0.909297
array.c
,
array.h
:
$ ./array.app 4.5 2.3 7 9 -1.2
You entered 5 elements: 4.5 2.3 7 9 -1.2
Sum of elements: 21.6
Average: 4.32
Sorted array: -1.2 2.3 4.5 7 9
array.cpp
:
array.app
)
$ g++ -o array.app array.cpp -lm
$ ./array.app
Enter length of array: 4
Enter 4 values:
1.2 5.6 2.3 7.8
Enter value to search for: 2.3
2.3 is index 2 in the array.
array2D.cpp
:
setup.py
which
uses Python's tool "Distutils" for compiling and linking the extension modules.
example.c
,
example.i
,
setup.py
,
cimport.py
:
/* file: example.c */
/* Global variables */
double MyDouble = 3.0;
char *MyString = "Hello from C!";
/* compute the sum of two numbers */
double cadd(double n, double m) {
return (n + m);
}
/* compute the average */
double caverage(double n, double m) {
double sum = cadd(n,m);
return (sum/2);
}
/* compute factorial of integer */
int cfact(int n) {
int i = 1;
while(n > 1){
i = i * n;
n = n - 1;
}
return i;
}
/* file: example.i */
%module example
%{
/*include header files here*/
%}
extern double cadd(double n, double m);
extern double caverage(double n, double m);
extern int cfact(int n);
extern double MyDouble;
extern char *MyString;
$ python setup.py build_ext
$ python setup.py install-platlib=.
>>> import example
>>> mystring = example.cvar.MyString
>>> print mystring
Hello from C!
>>> cadd = example.cadd
>>> x = 4.0; y = 2.0;
>>> print cadd(x,y)
6.0
>>>
cimport.py
contains
tests for computing the cpu-time of factorials in Python vs factorials in C.
Point.cpp
,
Point.h
,
Point.i
,
setup.py
,
classimport.py
:
>>> from point import *
>>> p1 = Point()
>>> p1.printpoint()
x=0, y=0
>>> p1.set(3.2, 4.5)
>>> p1.printpoint()
x=3.2, y=4.5
>>> p2 = p1;
>>> p1.isequal(p2)
true
>>> p3 = p1.add(p2)
>>> p3.isequal(p2)
false
>>>
cnumpy.c
,
cnumpyimport.py
,
make_module.sh
:
cnumpy
contains a function "sumarray" which takes a numpy array as argument and
returns the sum of elements in the array.
#include ‹Python.h›
#include ‹Numeric/arrayobject.h›
#include ‹math.h›
static PyObject *sumarray(PyObject *self, PyObject* args)
{
PyArrayObject *array; /* C representation of Numeric array */
int i,n; /* number of array entries */
double result = 0.0;
double* a; /* ptr to the NumPy array */
/* parse the arguments */
/* check that we have a one-dimensional array */
/* check that the datatype is NumPy-float, (C double ) */
/* compute the sum of elements in array */
return Py_BuildValue("d", result);
}
/* Doc strings: */
/* Method table */
/* init */
#file: make_module.sh
#!/bin/sh -x
root=`python -c 'import sys; print sys.prefix'`
ver=`python -c 'import sys; print sys.version[:3]'`
gcc -shared -o cnumpy.dll cnumpy.c -I$root/include/python$ver \
/lib/python$ver/config/libpython$ver.dll.a
cnumpy.dll
in current working directory)
$ ./make_module.sh
>>> from Numeric import *
>>> import cnumpy
>>> print dir(cnumpy)
['__doc__', '__file__', '__name__', 'sumarray']
>>> a = array([1,2,3,4,5,6,7,8], Float)
>>> print a
[ 1. 2. 3. 4. 5. 6. 7. 8.]
>>> sum = cnumpy.sumarray(a)
>>> print sum
36.0
>>>