Anda di halaman 1dari 3

Introductory Course in Scientific Programming

Lecture 6

(1)

Introductory Course in Scientific Programming

Lecture 6

(2)

One-dimensional arrays (cont.)


Example, [1.0 , 0.5 , 0.1]T R3 is dened by

One-Dimensional Arrays
Some concepts in mathematics can not be
represented in a natural way using the types
weve seen so far. One such example is vectors.

double
v[0] =
v[1] =
v[2] =

All basic types can be extended to be


vector-valued (in the terminology of computer
science aggregates of basic types or arrays).

v[3];
1.0;
0.5;
0.1;

/* x-component */
/* y-component */
/* z-component */

Example, computing x2 , where x R100 and


xi = i (numbered from zero):

A one-dimensional array consisting of n


elements of the same type can be declared by
type name[n];
and each element in the array accessed by
name[i], where i goes from 0 to n 1.

double x[100];
double l2norm = 0;
int
i;
for (i=0 ; i < 100 ; i++)
x[i] = i;

Note that the numbering of the components is


dierent from the one normally used in
mathematics (starting from 0 instead of 1).

for (i=0 ; i < 100 ; i++)


l2norm += x[i] * x[i];

NB! Compilers dont catch indexing mistakes


(i<0 || i>=n), but the program will compute
the wrong result or crash (segmentation fault).

l2norm = sqrt(l2norm);
(math.h: sqrt(a) computes

a.)

Marco Kupiainen
marcok@nada.kth.se
NADA

Marco Kupiainen
marcok@nada.kth.se
NADA

Introductory Course in Scientific Programming

Lecture 6

(3)

Introductory Course in Scientific Programming

(4)

Multi-d arrays & eciency

Multi-dimensional arrays

Although it is convenient to store matrices as


two-dimensional arrays it is not advisable for
computation due to ineciency (Intro. to
High Performance Computing).

Arrays can be extended to 2 (matrices), 3 or


even more dimensions,
type name[n1 ][n2 ]...[nr ]
and the elements accessed by
name[i1 ][i2 ]...[ir ] where 0 ij nj .

A matrix

Multi-dimensional arrays can be thought of as


arrays of arrays.

Example, the array double x[10][3][5]; can


be thought of as belonging to R1035 , and its
elements accessed by x[i1 ][i2 ][i3 ], where

x0,0
..
.

...
..
.

x0,n1
..
.

xm1,0

...

xm1,n1

Rmn

is normally stored in a one-dimensional array


double x[lda*n], with the mapping from
matrix to array dened by xi,j x[i+j*lda].

0 i1 < 10

lda is the leading dimension of the matrix,


satisfying lda m (remnant from Fortran).

0 i2 < 3
0 i3 < 5

This format is used in most numerical libraries


working with dense matrices.

Marco Kupiainen
marcok@nada.kth.se
NADA

Lecture 6

Marco Kupiainen
marcok@nada.kth.se
NADA

Introductory Course in Scientific Programming

Lecture 6

(5)

Introductory Course in Scientific Programming

Lecture 6

(6)

Arrays as function arguments


Arrays can be used as function arguments but
a function can not return an array.

Arrays as function arguments

Arrays are always passed by reference, that is,


all changes to the parameter will also aect the
argument.

Example, computing x


double maxnorm(int n, double *x) {
/* Compute max | x[i] |, 0 <= i < n */
int
i;
double nrm = -1;

The length of an array must be passed to the


function.
For one dimensional arrays, a function
declaration may look like

for (i=0 ; i<n ; i++)


if (fabs(x[i]) > nrm)
nrm = fabs(x[i]);

return-type function-name(int length,


type parameter[]);
or equivalently

return nrm;
}

return-type function-name(int length,

(math.h: fabs(a) computes |a|.)

type *parameter);
(Other parameters declared as before.)

Marco Kupiainen
marcok@nada.kth.se
NADA

Marco Kupiainen
marcok@nada.kth.se
NADA

Introductory Course in Scientific Programming

Lecture 6

(7)

Introductory Course in Scientific Programming

Lecture 6

(8)

Arrays as function arguments


Example, computing y y + x (saxpy)

Plotting

void saxpy(int n, float alpha,


float x[], float *y) {
int i;
for (i=0 ; i<n ; i++)
y[i] += alpha*x[i];
}

Files plot.h and plot.c on course home page


may be useful for this afternoons exercises.
C interfaces to (parts of) the GNU graph
utility.
Allow simple plots to screen or postscript les
in C programs.

Passing multi-dimensional arrays as arguments


is more complicated,
return- type function-name(int n1 ,

Other options:

type parameter[][n2 ]...[nr ]);

Matlab many & good plotting routines


but require temporary les. Commercial.

Only the rst dimension may be omitted!

Gnuplot simple plotting (mainly


interactive). Free.

If we change the size of the array in the


program we must also change the function
denition.

Graphics libraries programmer has large


control. Additional programming.

Using preprocessor directives (Friday) all


necessary changes can be done by changing a
single line.
Marco Kupiainen
marcok@nada.kth.se
NADA

Marco Kupiainen
marcok@nada.kth.se
NADA

Introductory Course in Scientific Programming

Lecture 6

(9)

Introductory Course in Scientific Programming

Plotting an example

Lecture 6

(10)

Plotting example output

#include "plot.h"
#include <math.h>
int main() {
/* Initialize plotter */
Plotter p = pl_alloc();
double x[30], y[30];
int i;
for (i=0 ; i<30 ; i++) {
x[i]=(6.28*i)/29; y[i]=sin(x[i]);
}
/* Add arrays x,y for plotting */
pl_add(p, 30, x, y, "rx-");

(image generated using pl print().)

/* Set labels */
pl_xlabel(p, "x"); pl_ylabel(p, "sin(x)");
/* Plot */
pl_plot(p);

Plot displayed in separate window.


Window closed by clicking it with the mouse.

Marco Kupiainen
marcok@nada.kth.se

Marco Kupiainen
marcok@nada.kth.se
NADA

NADA

Anda mungkin juga menyukai