• Keine Ergebnisse gefunden

Introduction to Mathematical Software 5

N/A
N/A
Protected

Academic year: 2022

Aktie "Introduction to Mathematical Software 5"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Introduction

to Mathematical Software 5 th Exercise Sheet

Department of Mathematics winter term 2009/2010

PD Dr. Ulf Lorenz 30/11/2009

Christian Brandenburg

Important Notice

Before starting this exercise sheet, you should at least have completed exercises2.1,2.2,3.1,3.2, and4.1.

Exercise 5.1Decimal Expansion of Rational Numbers

Let two integersa, bbe given with b>0. Write a program that prints the rational number ab to the screen indecimal expansion.

Browse the internet for a suitable algorithm to accomplish this task.

The output might be finite or periodic and should be of the form0.75fora=3,b=4in the finite case and0.1p6for a=1,b=6in the periodic case, where thepdenotes the beginning of the period.

Exercise 5.2Matrix-Vector Multiplication

Write a function that computes matrix-vector products. Your program should dynamically allocate the memory needed for the matrices and vectors.

You have two possibilities to represent matrices:

• a two-dimensional array of typedouble**(i.e. an array of pointers to arrays of typedouble) with the first index being the row index and the second index being the column index. Your function for computing the matrix vector productA·x=yshould be of the form

void MatMult(double** A, double* x, double* y, int rows, int cols);

wheremdenotes the number of rows ofA(and y) andndenotes the number of columns ofA(and rows ofx).

• a one-dimensional array of typedouble*that contains all matrix rows one after the other in a single long array.

The different rows can then be accessed with offsets. Your function for computing the matrix vector product A·x=yshould be of the form

void MatMult(double* A, double* x, double* y, int rows, int cols);

with the variables defined as above.

You can proceed in the following way:

• Ask the user for the matrix dimensions (number of rows, columns).

• dynamically allocate the required memory (you need to#include <stdlib.h>).

• fill the matrixAand the vectorxwith random values or ask the user for the data. The statement (double)rand()/(double)RAND_MAXproduces “random” numbers between0and1.

• compute the matrix-vector product

• free the memory that you have allocated.

• you might want to add functions like

void print_matrix(double **A, unsigned int rows, unsigned int cols);

(orvoid print_matrix(double *A, unsigned int rows, unsigned int cols);) and

1

(2)

void print_vector(double *x, unsigned int rows);

to visualize your results.

2

Referenzen

ÄHNLICHE DOKUMENTE

Munich Personal RePEc Archive.. Yet, two more revisions to the Human

The Economist Intelligence Unit’s democracy index is based on fi ve categories: electoral process and plu- ralism; civil liberties; the functioning of government;..

In the first one we define Banach bundles as fibre bundles (in the sense of [St51]) having a Banach space as typical fibre and clarify how a fibrewise norm can be obtained on them

For example, over the period 1986-2012, a test of a linear factor model with the S&amp;P 500 Index as the sole factor on the cross-section of daily- rebalanced index option

As a measure of relevance for the loss of statistical information that goes together with score construction, it is therefore expedient to relate the explanatory

The number of times each country appears in tables and graphs of the different “The Economist” issues for year 1995 confirms the evolution of the index between 1990 and 2000.. Data

• a two-dimensional array of type double** (i.e. an array of pointers to arrays of type double) with the first index being the row index and the second index being the column

$1,460 per year. Another problem is that there is no adjustment for differences in publicly provided and subsidized goods and services and taxation, climate differences