Playing with Basic Linear Algebra Algorithms from H++   2 comments

Last week I was visited by a friend of mine, whose is studying Informatics at UASD. He was assigned a homework to be made in C/C++, about Linear Algebra.

I decided to help him, by showing how to implement the basic algorithms from H++. By the way, I found a bug in my compiler, so I was excited I had work to do with H++ this week. So I implemented the code using basic features of H++, until I would fix the bug in the H++ compiler. Then, I showed him my code that solved the homework.

When I fixed the bug, I re-wrote the code, and this is what I fixed in the code.

Two-dimensional Arrays in H++

The problem with two-dimensional arrays in H++, was caused because before the fix, you could only create functions with unknown fixed-length dimensions; now you must specify the array dimensions for two-dimensional arrays before doing any matrix computations; and example:

before the fix:

public static void CalcTrace(double [][] M, int rows, int cols);

after the fix:

public static void CalcTrace(double [length-of-array][length-of-array] M, int rows, int cols);

where length-of-array could be any integer (for now).

I did not realize that I had that bug, until I was exposed with these basic matrices’s algorithms; this is the fix:

Fix to the H++ Compiler

Basic Matrices Algorithms

The following algorithms are very easy to implement in any computer language, because they just iterate the matrix:

Computing the Trace

Show Matrices and Calc Trace

Summing Two Matrices

The SumMatrices function can sum or substract them; as you can depict, it’s very easy to implement it:

Sum of Two Matrices

Multiplying Two Matrices: (mxn) (nxp) == mxp 

The matrix multiplication algorithm, is implemented this way in H++ (very similar to C/C++ or Java):

Multiply Two Matrices

Computing the Traspose

An easy algorithm is the Traspose of a matrix:

Traspose of Matrix

The H++ Program that calls these basic functions

The program first ask for the two matrices dimensions, before asking you what algorithm to apply to one isolated matrix, or both of them:

Main - Part I

Main - Part II

Main - Part III

Main - Part IV

Compiling the H++ Program for this homework

Now, for this fix, we have the beta build 2072 of H++ (a long time has passed since the last fix I found):

Compiling TestMatrices in H++

Testing the compiled H++ Program

Program Init

Main Menu

Computing The Matrix Trace

Computing Traspose of A

Multiply Matrices - Running

Multiplying Square Matrices

Summing Square Matrices

Summing Matrices 4x4 - Result

A book recommendation on Matrix Computations

If you wish to implement more algorithms on matrices, I recommend the book Matrix Computations by Gene H. Golub and Charles F. Van Loan:

Matrix Computations by Golub & Gene

See you in the next post!

Advertisements

Posted November 14, 2011 by hmarzan in Uncategorized

2 responses to “Playing with Basic Linear Algebra Algorithms from H++

Subscribe to comments with RSS.

  1. Bélico manke!, mándame el compilador actualizado para probarlo. 😀

    Mirel Galán Blanco
  2. The compiler and the source code can be cloned from https://github.com/hmarzan/HCC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: