Top Banner
Computational analysis with scientific Computing Part I This portfolio is by Sifiso Samson Thwala
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computational Analysis With Scientific Computing(v1)

Computational analysis with scientific Computing

Part I

This portfolio is by Sifiso Samson Thwala

Page 2: Computational Analysis With Scientific Computing(v1)

ForewordIT is a vast field that overwhelms me and my fellow acquaintances hence we are actually studying, researching and trying our level best to develop IT as much as possible. IT ranges from (fundamentally) bitwise operations used in machine level to number manipulation used in computational applications to combinatorial problems leading to the ever so vast study of AI (Artificial intelligence) to matrix calculus which is the basis of graphics and graphical user interfaced applications, etc... Scientific computing - which was highly used in this portfolio and most, if not all, of my projects – is a collection of tools, techniques and theories required to develop and solve mathematical models in science and engineering on a computer, bridging the gap of the theoretical side of academics and the work experience of professionals to produce a well thought after solution to common and even critically rare and unconsidered problems.

It as been my life’s work to ponder ideas and research in this industry, so I hope the few examples I have compiled here for illustration purposes offer more insight on IT itself than praise any persona, for the discipline itself is the future and let man give his all to it, in order of it to grow.

The basis of the code provided will be written in this documentation, for background knowledge into the problems solved in the dot CPP of dot JAVA files accompanying thin document. All implementations and testing of the C++ programs have been done of gcc 3.3, DevC++ and Microsoft Visual Studio.NET, while for java I used version 1.6.0 (jdk1.6.0), May you not have any problems running the executable file given. Should you need to recompile, you may, provided that the original file are not modified directly.

Author’s email address: [email protected]

© All rights are reserved by the University of Johannesburg, meaning that this documentation and/or the underlying code may not be reproduced in any form or by any means, electronic or mechanical, including photocopying, recording or any information storage and retrieval system now known or to be inverted, without written permission from the author

Page 3: Computational Analysis With Scientific Computing(v1)

Contents

SecurityChecksumCyclic Redundancy ChecksFunctionalityExclusive OR functionBaeza-Yates-GonnetLet’s playThe MazeSudokuPracticalFinite State MachinesDictionary Word ProcessorChatRoomMathematicsEigenvalues and EigenvectorsMatrix class in C#Graphics Grey PictureGIMPBézier curveHarmonic interpolation

Bibliography

Page 4: Computational Analysis With Scientific Computing(v1)

Checksumchecksum.cpp

A checksum can be used to determine if errors occurred in transmission of data. It is widely used in the internet by several protocols. The checksum works as a liner and cyclic code does yet it is based on the concept of redundancy, several protocols within the internet model still primarily use it for error detect.The checksum is based on a 32-bit checksum algorithm that simply adds all data in the transmission as if it were 32-bit quantities, i.e. we read 4 bytes each and the resulting 32-but quantities, when the checksum overflows, and the overflow bit is added to the checksum.

i) Find the checksum for ABCD For the String ABCD we have: 65 + 66*256+66*256 2 +68*256 3 = 11455258561 Where the used the ASCII table, in which ‘A’ => 65 , ‘B’ => 66, ‘C‘ => 67 and ‘D’ => 68

• The underlying Checksum.cpp code will illustrate: Checksum computation of some data and provide options for comparing the checksum to a given value.

ii) Pitfall: If we look at the checksum it is clear that any difference in two sequences of 32-bit values that is not a multiple of 2 32 in total will result in a different checksum. This allows us to detect all errors that do not produce a difference in the sum of precisely 2 32 = 4294967296. The checksum in this example is further improved by the addition of the carry bit. The addition of the carry bit will prevent multiples of 4294967296 from being detected as a no error condition. So now all errors must result in a net increase of zero in the sum if they are to go undetected, i.e., some values must decrease and others increase to get a net result of zero difference over the sum.

Page 5: Computational Analysis With Scientific Computing(v1)

Cyclic Redundancy

ChecksCRC32.cpp

Cyclic redundancy checks provide a better means of checking data for errors (well it’s the closest one before to brute force).Cyclic Codes are special linear block codes with one extra property, in cyclic code, if a code word is cyclically shifted (rotated), the result is another code word and the a Cyclic redundancy check is an instance of cyclic code.

The cyclic redundancy 32 polynomial

1)( 2457811121622232632 +++++++++++++= xxxxxxxxxxxxxxG is a commonly used polynomial for error detection. This polynomial is resented as a bit string where each coefficient (1 or 0) is a binary digit in the string. To check for errors in transmitted data we do the following:

• compute the CRC of the to be sent • send the data and the CRC• Recompile the CRC and check whether it matches the sent CRC

To compute the CRC we do the following: we let B(x) be the data to be sent plus 32 0’s appended(i.e., multiply by 32x ). Now we calculate R(x) = B(x) mod G(x), and set T(x) = B(x) – R(x). This sets the low bits of T to R. We transmit T to get T’. If T’(x) mod G(x) = 0 then there was no error. We can perform the remainder calculation a feedback shift register:

• Create a register with 32 bits, all set to zero• For each G(x) where the coefficient is 1, build an XOR gate so the output is ‘xoorred’ – Allow me

coil that word – with the bit that is shifted into that register.The data bits are shifted in one at time all the bits shift one position on, and if there is an XOR gate then XOR the position with the value in the last register. The solutions to questions passed in this discussion are answer in the cpp file named CRC32.cpp

Page 6: Computational Analysis With Scientific Computing(v1)

Exclusive OR functionXOR

XOR.java

Now the origins of this function dates far back from the evolutions of logic from mathematics and has been a tool ever since. Although the precise date of the XOR function’s origins is undetermined, the modern use of it fascinated me because it’s an essential tool to modern Structures such as Graphics.

In the Graphics class of Java the method:

public abstract void setVORMode( Color c1)

sets the paint mode of this Graphics context to alternate between this Graphics context’s current colour and the new specified colour.

The big question is how?This specifies that the logical pixel operations are performed in the XOR mode, which alternate pixels between the current colour and a specified XOR colour. When drawing operations are performed, pixels which are the current colour are changed to the specified colour, and vice versa. In other words, we XOR the colour c of the pixel being written with current ⊕ c1, i.e.,

current = (current ⊕ c1) ⊕ c1, c1 = (current ⊕ c1) ⊕ current

The java illustration shows this…

Page 7: Computational Analysis With Scientific Computing(v1)

Baeza-Yates-GonnetAlgorithm

Byg.cppThe Baeza-Yates-Gonnet algorithm (BYG algorithm,) for searching for a pattern in a string runs 40-50 per-cent faster than KMP.The technique works as follows:

• Build a table of bitmasks for each letter of the alphabet, the mask has the length of the search pat-tern. Each bit is 1 (if that letter is not in that position).

• Create a working bitmask, initialized to all1’s.• For each letter in the search string, shift the working mask 1 bit to the left and perform the logical

or with the bitmask for the letter in that position of the string.• Let the length of the search pattern be x. If the bit in position x of the working bitmask is 0, then

the String matches.• If we reach the end of the string without finding a match, then there is no match.

The c++ program to match strings using the BYG algorithm

Page 8: Computational Analysis With Scientific Computing(v1)

The MazeMaze.java

The maze is an illustration of recursion; a blood hound implementation is given here which find the possible solution to the maze a maze which could be randomly generated but for illustration purses hard coding will do the trick. The aim of the maze is to move from position (0, 0) – top left corner – to the position (m – 1, n – 1) – bottom right corner following the path’s of 1’s. We can only move to the right, left, down and up but not diagonal. Write a java program using recursion that find out whether or not there’s a solution and if so provide a solution which is denoted as a path of 5’s.

This common recursive problem that occurs in many disciplines but the most exciting one is within infinite state machine especially severs and other none terminating applications and machinery.

Page 9: Computational Analysis With Scientific Computing(v1)

SudokuSudoku.cpp

Sudoku is a numerical puzzle. The puzzle consists of 9x9 squares arranged in a grid. This grid is further subdivided into 3x3 squares of size 3x3. Some numbers are initially placed on the grid. To solve the puzzle, a number between 1 and 9 (inclusive) must be selected for each square that is empty so that:

1. No number is duplicated in any row.

2. No number is duplicated in any column.

3. No number is duplicated in any of the 3x3 subgrids.

This a program allows a user to specify and solve a sudoku puzzle. Initially the 9x9 grid is empty, the user types in the numbers that are given for the puzzle. Thereafter, the user types in the numbers that he/she has computed to solve the puzzle at each stage.

The program displays the puzzle on the screen, with the entered numbers after each new number is entered. Note: The user can choose to enter a new number at any position he/she chooses, as long as there is no number there already.

After all squares have been filled with a number, I check that the puzzle has a solution by making sure that the above constraints are satisfied. Note: there may be many, or perhaps no solutions to the puzzle.

Page 10: Computational Analysis With Scientific Computing(v1)

FiniteState

Machinesmachine.cpp

This is a skeleton design of a Mealy machine to represent a vending machine which provides a carbonated drink for R2,50. The machine accepts 50c, R1 and R2 coins. The symbol number output by the machine is interpreted a change (in cents) for the customer. The symbol ‘c’ output by the machine is interpreted as the output of the carbonated drink. We allow null output, i.e., output which does not actually appears (empty string). Thus the output ‘symbols’ we could expect are the strings:

Output is: “”,”C”,”C50”, “C100”, “C150”.

For the illustration I use the states to indicate how many 50c the vending machine has received. In other words S0 represents 0c, S50 represents 50c and so on. The following transition table describes the machine.

State Input Output Next StateS0- 50c S50S0- R1 S100S0- R2 S200

S50 50c S100S50 R1 S150S50 R2 C S0

S100 50c S150S100 R1 S200S100 R2 C50 S0

S150 50c S200S150 R1 C S0S150 R2 C100 S0

S200 50c C S0S200 R1 C50 S0S200 R2 C150 S0

Page 11: Computational Analysis With Scientific Computing(v1)

Dictionary Word ProcessorSpellChecker.cpp

The program is a spellchecker program. The program takes parameters on the command line to specify the file to be checked and optionally the dictionary file. The dictionary file is read in and stored in memory. The program reads the file to be spellchecked word for word, and sees if each word is in the dictionary file. If the file has a spelling error an option to change the word (in which case the file is modified). The modi-fied file is written to a new file.

The program adheres to the following requirements:

• You must use a binary search algorithm to check if the word is in the dictionary. • Make suggestions when a spelling mistake has been made. • Ignore the case of words. (Hint: use to upper) • Ignore punctuation.

The dictionary is made available as words.txt in the same folder.

The spell checker Challenge is a special case that is continuously and new commonly used way of alternat-ive words. Instead of lexicon order of the alphabet it also detects meaning, so it detects a couple of letters of cases. Like ‘impottant’ word be important

Page 12: Computational Analysis With Scientific Computing(v1)

ChatRoom

ChatClient.javaChatRoomServer.java

etcThis program implement a basic electronic chatroom application that employs a multi threaded server.

Client:

The client is implemented as a GUI that can send and receive messages until it sends the string 'Bye'.

A separate thread receives messages from the server and adds them cumulatively to a text area. The thread:

• Accept the user's chatroom nickname via an input dialogue box

• Send this name to the server.

• All other messages are sent via a text area with an associated button.

As a simplification, it is assumed that no two clients will use the same nickname

Server:

The chat server is multi threaded and must be able to:

• Broadcasts each message it receives to all the connected clients.

• Maintain a dynamic list of Socket references associated with those clients. Though you could use an array to hold the list, the use of a Vector object would be much more realistic.

Page 13: Computational Analysis With Scientific Computing(v1)

Eigenvalues and

EigenvectorsEigenvalues.cpp

The German adjective eigen means “own” or “characteristic of”, eigenvalues and eigenvectors are characteristic of a matrix in the sense that they contain important information about the nature of the matrix, To find eigenvalues and eigenvectors of a symmetrical matrix over R one often uses the Jacobi method. It consists of a sequence of orthogonal similarity transformation of the form

...211

11

211

1 →→→ −−− QAQQQAQQA Let .1 nrp ≤<≤ the basic Jacobi rotation matrix Q is a matrix with

.,0

,0,sin,1,cos

prjrpiqrpiqqqqqq

rpiqqq

ij

riirpiiprppr

iirrpp

≠∩≠→=

≠→====−=−=

≠→===

θθ

The idea of the Jacobi method is to try to zero off diagonals elements by a series of plane rotations.

Page 14: Computational Analysis With Scientific Computing(v1)

MatrixThis is a class that was created purely to be generic. IT is one of my ventures of studying C# and its syntax. The Matrix class is simple textbook work – in fact I just took my old text book from the shelf - and merely spans in the regular and generic cases of a typical matrix.

The UML diagram of the class:

Matrix

- Mat : double [ , ]

+ <<constructor>> Matrix()+ Clone() : Object+ Equals( in b : Object) : boolean+ <<operator +>> Matrix ( a : Matrix, b : Matrix )+ <<operator ->> Matrix ( a : Matrix, b : Matrix )+ <<operator *>> Matrix ( a : Matrix, b : Matrix )+ <<operator *>> Matrix ( a : Matrix, b : Vector )+ Transpose() : Matrix+ Translate( v : Vector ) : Matrix+ Scale ( s : double ) : Matrix+ Scale ( sx : double, sy : double, sz :double ) : Matrix+ Rotate ( axis : Vector, angle : double ) : Matrix+ Projection ( n : double, f : double , t : double, b : double, l : double, r : double ) : Matrix

+ ToString() : string

Page 15: Computational Analysis With Scientific Computing(v1)

Grey Picture

Computer graphics program seldom take the structure and functioning of the eye into account. If they do, the results usually take a very long time to produce.

One of the peculiarities of the eye (or camera) is focal length. A certain distance in the scene remains in fo-cus, whereas closer (or further) objects appear to be blurred. A typical ray tracer program produces crystal clear images for all distances. To make the result realistic, we would like to simulate the focal length or depth of field. We thus need to be able to read images stored in binary format. The images provided for this practical are gray files. The format of the gray file is as follows:

• One integer specifying the width (binary format).• One integer specifying the height (binary format).• The intensity of the each pixel in the grid that is width × height values of type unsigned char.

The values in the image indicate the intensity with 0 being black and 255 white. The values in between specify shades of gray. You are also given a depth image of exactly the same size and format.However, the values are interpreted as:

• 0 - very, very near.• 255 – very, very far.• Any value in between - somewhere between near and far!

Load both an image and its depth map into separate two dimensional dynamic arrays. Now, apply a Gaus-sian filter:

))2/())()((2

222

21),( rrowycolxer

yxG −+−−=π

The 2D Gaussian function reaches a maximum in the centre:

For each pixel, we calculate the value r (given below) and then use a nested for loop to iterate from 3r rows before to 3r rows afterward, and likewise for the columns. Multiply the Gaussian function by the intensity at that position in the image and add the results together. Be sure to check whether the pixels are in bounds, x and y refers to the pixel under consideration and G can be used to calculate the relative importance of the pixel. We thus compute

∑ ∑+

−=

+

−==

rrow

rrowy

rcol

rcolxxyimageyxGcolrownewimage

3

3

3

3]][[),(]][[

I used floating points to calculate the value and typecast back to an unsigned char afterward. The radius r is determined by the focal length f,

50/|| fdr −=Where d = depth[row][col] is the depth value stored at the pixel. Make sure that r is at least 0.5.

Page 16: Computational Analysis With Scientific Computing(v1)

For example, consider the image and depth map of a castle:

If a focal length of 128.0 is used results are:

And with a focal length of 240.0, results are:

The name of the image and depth map, as well as focal length must be passed through on the command line. The resulting image must be written to file in gray format. A program will be included to view gray files, and example data will be provided.Images and scene files are provided by Gilles Tran.

Page 17: Computational Analysis With Scientific Computing(v1)

GIMPA computer image is made up of tiny elements called pixels (picture elements). Each element is composed of differing values of the colours red, green and blue called RGB values. These are often represented as a set of three numbers ranging from 0-255. For example 0 means that none of that particular colour is includ-ed and 255 means that all of that colour component is included. The colour Red for example would be rep-resented as 255 0 0, Blue as 0 0 255 and Green as 0 255 0 Christian Faur[1] is an artist who has devised a mapping between colours and characters in the Latin alpha-bet where the most striking colours (as measured by human perception tests) are mapped onto the most fre-quently used letters in the English language. Mr. Faur’s mapping is as follows (used with the permission of the artist):

The portable pixel map[2] (.ppm) file format is a very simple text-based file format for representing graphi-cal images. We will be using a simplified* version of the format as follows: P3#generated by <executable name>Image-width image-heightMax-value-for-rgbR G B R G B R G B…R G B R G B R G B…R G B R G B R G B….*our simplified version will ignore the 70 character line limit so that one line of our image is equal to one line of RGB values in the PPM file.In order to view one of our PPM files you will need to use an imaging tool a little more sophisticated than MS-Paint. I highly recommend using The GIMP[3] (GNU Image Manipulation Program) which is both open source and extremely capable. An example PPM file encoding the string “Hello World” is available under the additional files section of this practical. The example file would appear visually as follows when loaded into the GIMP.

[1] http://www.christianfaur.com/conceptual/colorAlphabet/index.html[2] http://netpbm.sourceforge.net/doc/ppm.html[3] http://www.gimp.org/

Page 18: Computational Analysis With Scientific Computing(v1)

Bézier curveBezier.java

A Bézier curve B(t) of degree n is defined by a set of control points ip for ni ,...,1,0= as follows:

∑=

=n

inii tBptB

0, )()(

Where niB , are the Bernstein polynomials:ini

in

ni ttCtB −−= )1(?)(,

The sum of the Bernstein polynomials is a partition of unity

∑=

=n

ini tB

0, 1)(

We have 0)(, ≥tB ni to every 10 ≤≤ t . The properties imply that the Bézier curves are affine invariant and that the curve lies entity in the convex hull of the control points defining the curve. We find that

0)0( pB = and that npB =)1( . Thus, the Bézier curve interpolates the end points, we find that

)()0( 01 ppndtdB −= , )()1( 1−−= nn ppn

dtdB

Thus the tangents at the end points of the curve are easily computed. This allows us to construct piecewise smooth 11orGC curve out of Bézier curves by setting )()( 101 −−=− nnqp qqcnppn for a curve

)()(0

, tBqtQqn

inii∑

=

=

Followed by a curve

∑=

=pn

inii tBptP

0, )()(

The Java Applet displays the Bézier curve

Page 19: Computational Analysis With Scientific Computing(v1)

Harmonic interpolationHarmonicinterpolation.java

The nn × primary permutation matrix U is given by

=

00011000

01000010

U

The eigenvalues of U are given by )/2exp(:,...,,1 1210 niwithn πλλλλλ == − . The spectral decomposition of U is:

∑−

=

=1

0

.n

jj

jPU λ

The projection matrix jp can be expressed using kU . Since 0=kj PP for kj ≠ and jj PP =2 we find:

∑−

=

=1

0

n

jj

jk PU λ .,...,2,1 nk =

Thus, we calculated the Fourier transform of the projection matrices. For k = n we have nn IU = and the

completeness relation:

∑−

=

=1

0

n

jjn PI

The inverse of the matrix )( jkλ is given by )(1 jkn

−λ . Thus

∑−

=

−=1

0

,1n

k

kjkj UnP λ .1,...,1,0 −= nj

We apply the permutation in such a way that we can continuously interpolate between the points of the polygon. We embed the matrices kU into a real Lie group. Thus we have to consider the cases n = 2m + 1 and n = 2m, where m is a positive integer. In the second case we have 10 =λ and 1−=mλ . Furthermore,

we define .: 1−− = nj PP The projection matrices are therefore real. We consider first case n = 2m +1. we

obtain by replacing k by [ ]1,0, ∈tnt

∑=

−− ℜ∈++=

m

jj

ijtj

ijt tPePePtU1

220 ),()(~ ππ

The unitary matrices )(~ tU are 1-periodic, i.e. )1(~ +tU = )(~ tU . Thus )1(~ +tU = )(~ tU and

1,...,1,0)/(~ −== nforkUnkU k . Owing to the previous, we can write )(~ tU as:

∑ ∑−

= =

−−− ++=1

1 1

)/(2)/(2 ))(1(1)(~ n

k

m

j

knktijnktij UeentU ππ

We define: ))(1(1:)/()(1

)/(2)/(2∑=

−− ++=−≡m

j

nktijnktijk ee

nnktt ππδδ

Using ,sincos)exp( ααα ii +≡ : 1,...,1,0,))/(sin())/(sin()( −=

−−= nk

nktnnktntk π

πδ

Page 20: Computational Analysis With Scientific Computing(v1)

Thus we can write: ∑−

=

=1

0

)()(~ n

k

Kk UttU δ

We have the properties: ∑−

=

=1

0

1)(n

kk tδ ∑

=

=1

0

2 .1)(n

kk tδ

thus the functions )(tkδ and )(2 tkδ provide a partition of unity and the harmonic interpolation is affine invariant. Now let T

nxxxx ),...,,( 110 −= be the vector which describes the polygon. Then

∑−

=

−= ==1

0

)/()(~)(n

k

k XUnktXtUtX δ

The curve which describes our closed smooth curve is given by:

∑ ∑−

=

=++ +=+−+=−=

1

0

1

0.0 )/()/)(/()/()(

n

k

n

klklkl nltXXnlknltXnkttX δδ

Where ( k + l ) is calculated mod n. Hence, for all l it represents the same curve and we can write:

∑−

=

∈−=1

0

]1,0[,)/()(n

kk tXnkttX δ

Thus this curve interpolates the points of the given polygon smoothly. We consider now the case n = 2m. for this case we may write:

∑−

=−

−++−+=1

10 ).()1(

m

jj

jkj

jkm

kk PPPPU λλ

If we replace the discrete variable k by the real variable t, we find the factor itk eπ≡− )1( The other terms are real. Thus, t the Lie group we could find is not real and therefore cannot be used directly for the harmonic interpolation in computer graphics. Since the function U(t) will be continuous in the complex domain and go through the points of the polygon. A similar calculation as described for the case n = 2m +1 given above yields ])1,0[( ∈t

∑ ∑ ∑−

=

=

=

+−+−=1

0

1

0

1

0

)())(sin(1))(cos(1)(~ n

k

n

k

n

k

kk

kk UtUkntnUkntntU ξππ

Where: ,)sin(

))(sin(:)(tn

knttπ

πξ −= )/(:)( nktt −= ξξ

Thus, our smooth curve in the even case which goes through all the points of the polygon is:

∑−

=

+−=1

0

))())(cos(1()(n

kkk XtkntntX ξπ

`Most calculations and deductions were guided by Prof. A hardy and Prof.WH Steeb

The java applet and application draw the harmonic interpolation of a polygon.

Page 21: Computational Analysis With Scientific Computing(v1)

BibliographyDavid PooleLinear Algebra a modern introduction second editionThomson BROOKS/COLE (2006)

Akenine-Möller, T. and Haines E.Real-Time Rendering, second editionAK Peter, Natick, Massachusetts (2002)

Séroul R.Programming for mathematiciansSpringer Verlag, Belin (1991)

Wili-Hans Steeb, Yorick Hardy, Alexandre Hardy and Ruedi StoopProblem solving & solutions in scientific computingWorld Scientific (2004)

Neapolitan R. and Naimipour Kfoundations of AlgorithmsD. C. Heath and company, Lexington (1996)

Bäck T.Evolutionary Algorithms in Theory and PracticeOxford University Press, Oxford (1996)

Baase S.Computer algorithms, Second EditionAddison-Wesley, Reading, Massechusetts(1988)