LAME Linear Algebra Made Easy David Golub Carmine Elvezio Ariel Deitcher Muhammad Ali Akbar December 20 2010 1 Project Proposal We propose to develop a language with built in support for linear algebra and matrix operations The language will provide functionality similar to MATLAB from The MathWorks Inc However the syntax will be similar to C C or Java Our language will provide four primitive data types scalar matrix string and Boolean These data types will do as their names suggest A scalar will hold a double precision floating point number A matrix will store a two dimensional array of double precision floating point values Matrices will be declared using commas to separate columns and semicolons to separate rows For example the code matrix 1 4 7 A 2 3 5 6 8 9 will correspond to the matrix 1 A 3 7 2 5 8 3 6 9 in the standard notation from linear algebra Individual elements of a matrix variable will be able to be access and modified using the syntax A i j for the element in the ith row and the jth column Keywords will be provided to obtain the dimensions of a matrix Operators will be provided for addition subtraction multiplication division and exponentiation The operators will be interpreted appropriately for various 1 combinations of operand types as long as they are mathematically meaningful Combinations of operand types that are not mathematically meaningful such as division of two matrices will yield a compiler error When matrix operations are performed a check will be done at runtime to ensure that the dimensions are compatible If the check fails a runtime error will be thrown The language will follow the imperative paradigm and will provide constructs for variable assignment decisions loops and basic I O Programs will be translated to an intermediate code and then to C code using a custom library The C code can then be compiled to native code 2 Tutorial This is a beginner s tutorial to LAME LAME is a C like programming language for linear algebra It allows a mathematician to implement algorithms involving matrix operations with a very short learning curve This tutorial is divided in to three parts First we describe the steps involved in compiling and running a simple Hello World program Then we describe how to perform basic matrix operations Finally we explain the construction of a program that implements the algorithm to solve a system of linear equations 2 1 Getting Started We start by writing a very basic program that prints Hello World 2 1 1 Hello World Program Type the following program in a file names hello lam print Hello World The print keyword takes a string as an argument and prints that string to the output console Each statement in LAME ends with a semicolon 2 1 2 Compiling and Running the Program You should have a C compiler on your machine The lame exe executable the compiler bat script the lame h and matrix h header files should be present in the working directory for the compilation Windows First we give instructions for a Windows machine with Visual C First set up the environment by running vcvars32 bat file from Visual Studio s common folder in the installation path Alternatively you can use the Visual Studio Command Prompt which automatically sets the environment variables for the compiler Now run the following commands to compile and run the program 2 lame exe hello lam program cpp cl exe EHsc program cpp program exe Alternatively you can use the provided batch script by using following command compiler bat hello lam program exe Linux Run the following commands to compile and run the program lame hello lam program cpp g program cpp o program program If running the program prints Hello World on a separate line on the screen and you are ready to go to next part of the tutorial 2 2 Basic Matrix Operations Let s write a program that declares a matrix with initial values and prints the matrix matrix A 1 2 3 4 print A n A Let s declare another matrix B and add it to the matrix A matrix B 4 3 2 1 matrix C A B print A B n C Subtraction and multiplication of matrices are done in similar manner Let s multiply the matrix by a scalar value matrix D 2 A print 2 A n D Let s change the value of an element in A and print the value of an element in B A 0 1 20 print B 1 1 B 1 1 3 The following if statement prints True and the while loop prints all elements of matrix A if A 0 0 1 print True else print False scalar i 0 scalar j 0 while i 2 while j 2 print A i j n j j 1 i i 1 2 3 Case Study Solving Linear System of Equations Let us implement an algorithm using LAME to solve a system of simultaneous linear equations The equations are 3x1 x2 3 and 9x1 4x2 6 We use the following algorithm to solve this problem 3 1 A 9 4 B X A 1 x1 x2 3 6 A 1 B 1 1 Adj A A A0 0 A1 1 A0 1 A1 0 A1 1 A1 0 A0 1 A0 0 This algorithm has been implemented in LAME as follows matrix A 3 1 9 4 matrix B 3 6 matrix X print nSolving system of simultaneous linear equations n 4 print A 0 0 x1 A 0 1 x2 B 0 n print A 1 0 x1 A 1 1 x2 B 1 n print nA n A n print nB n B n scalar det of A A 0 0 A 1 1 A 0 1 A 1 0 print nDeterminant A det of A n if det of A 0 matrix inv of A inv of A 0 0 A 1 1 inv of A 0 1 1 A 0 1 inv of A 1 0 1 A 1 0 inv of A 1 1 A 0 0 inv of A inv of A det of A X inv of A B print nInverse A n inv of A n print X Inverse A B n X n print Solution n print x1 X 0 n print x2 X 1 n else print A is singular and its inverse doesn t exist n The program prints out the following output Solving system of simultaneous linear equations 3 x1 1 x2 3 9 x1 4 x2 6 A 3 1 9 4 B 3 6 Determinant A 3 Inverse A 1 33333 0 333333 3 1 X Inverse A B 2 3 5 Solution x1 2 x2 3 3 Language Reference Manual 3 1 Definition of a Program A program is a sequence of variable declarations and statements 3 2 3 2 1 Variable Declarations Data Types The following data types represent the complete listing of primitives available to a programmer of LAME Scalar This is the equivalent of a double precision floating point number in …
View Full Document
Unlocking...