Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio |PyJEM| Search

 

Example: Matrix Processing
Function
An example script which shows how to use the matrix processing functions available in DM.
Version
20131110, v1.0
Author
D. R. G. Mitchell and J. A. van den Berg
Acknowledgements
-
Comments

Example code which creates two matrices (images) and then carries out matrix multiplication, matrix inversion and matrix determinant calculations.

System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
In GMS 1.x the matrix determinant does not return a zero value if there is no inverse matrix. Instead it returns a very small value eg 1e-20. Testing the determinant to see if is zero will fail in this case - better to check for it being less than a really small value.
Supported
Yes
Included Files
Main script file.
Source Code

// Example script to demonstrate the use of the matrix functions

// available in DM

// D. R. G. Mitchell and J. A. van den Berg, adminsnospam@dmscripting.com (remove the no spam to make this work)


// version:20131110, v1.0, Nov. 2013, www.dmscripting.com

 

 

// function to carry out matrix multiplication with checks

 

image mtxmultiplication(image matrix1, image matrix2)

{

number m1xsize, m2xsize, m1ysize, m2ysize

getsize(matrix1, m1xsize, m1ysize)

getsize(matrix2, m2xsize, m2ysize)

 

 

if(m1xsize!=m2ysize)

{

showalert("The x and y dimensions of the two matrices must be the same.",2)

exit(0)

}

image mtxoutput=matrixmultiply(matrix1, matrix2)

return mtxoutput

}

 

 

 

// Function to carry out matrix inversion with checks

 

image mtxinversion(image matrix1)

{

number m1xsize, m1ysize

getsize(matrix1, m1xsize, m1ysize)

 

 

// Matrix inversion requires that the x and y dimensions be the same

 

if(m1xsize!=m1ysize)

{

showalert("Matrix Inversion: The x and y dimensions of the matrix must be the same",2)

exit(0)

}

 

image mtxinversion=matrixinverse(matrix1)

return mtxinversion

}

 

 

 

// Function to compute the matrix determinant with checks

// Note: the matrix inverse exists only for matrices whose determinant is not zero

 

number mtxdeterminant(image matrix1)

{

number m1xsize, m1ysize

getsize(matrix1, m1xsize, m1ysize)

 

image zerotest=tert(matrix1==0, 0, matrix1)

if(sum(zerotest)==0)

{

showalert("Matrix Determinant: All elements in the matrix can not be zero.",2)

exit(0)

}

 

// The determinant can only be obtained from square matrices

if(m1xsize!=m1ysize)

{

showalert("Matrix Determinant: The x and y dimensions of the matrix must be the same",2)

exit(0)

}

number mtxdet=matrixdeterminant(matrix1)

return mtxdet

}

 

 

 

// Main program

 

// Note matrix multiplication requires the x dimension of Matrix 1 to be the same as the y dimension of Matrix 2

// The function checks this and bails out if not

 

 

// define a 3 x 3 matrix 1

 

image matrix1=realimage("",4,3,3)

matrix1=(icol*random())+(irow*random()) // put in some arbitrary numerical values

 

// To see the effect of setting all the matrix elements to 1 remove the // in front of the following line

//matrix1=1 - this matrix will have a determinant of zere - see below.

 

showimage(matrix1)

setname(matrix1, "M1")

showimage(matrix1)

 

 

// define a 3 x 3 matrix 2

 

image matrix2=realimage("",4,3,3)

matrix2=(icol*random())+(irow*random())

showimage(matrix2)

setname(matrix2, "M2")

showimage(matrix2)

 

 

// Carry out the matrix multiplication

 

image mtxmultiply=mtxmultiplication(matrix1, matrix2)

showimage(mtxmultiply)

setname(mtxmultiply, "M1 x M2")

 

 

// Transpose the matrix so that rows become columns and vice versa

 

image mtxtranspose=matrixtranspose(matrix1)

showimage(mtxtranspose)

setname(mtxtranspose, "Transpose of M1")

 

 

// Compute the matrix determinant. If this has a value of 0 there is no inverse matrix

// Note there appears to be a bug in my version of DM 1.83, wherein the determinant of

// 2x2 matrix of all 1s returns of value of -1e-20. Obviously a very small value

// but not zero as it should be - so checking for mtxdet==0 does not work - hence the cludge below

 

number mtxdet=mtxdeterminant(matrix1)

if(abs(mtxdet)<1e-9) mtxdet=0 // cludge to deal with DM bug : 1e-9 arbitrarily chosen.

okdialog("Determinant of matrix1 = "+mtxdet)

 

 

// Compute the inverse matrix

 

if(mtxdet!=0) // non-zero determinant means the inverse matrix exists

{

image mtxinverse=mtxinversion(matrix1)

showimage(mtxinverse)

setname(mtxinverse, "Inverse of M1")

}

else

{

Showalert("Determinant of matrix1 is zero - no inverse matrix exists",2)

}