2-dimensional arrays used in Mathematics

  • In mathematics, a matrix is a rectangular array (or a table of numbers).

  • Example: a 3×3 matrix:

  • Matrices are used in many mathematical applications:

      • Matrices are used in Linear Algebra (to represent linear maps)

      • Matrices are used in Graph Theory to represent graphs (= networks)

        (You will study these applications in college)

2-dimensional arrays used in Mathematics

  • When writing a computer program that works with matrices, each matrix will be represented by a 2-dimensional array

  • Example:   this 3×3 matrix

    is represented in a Java program as follows:

          double[][] matrix1 = { {1, 2, 3},
                                 {3, 6, 7},
      			   {2, 5, 3}  };

Matrix operations

  • There are many operations defined on matrices (see Wikipedia)

    • I will explain a few of the matrix operations because we will write Java methods to implement them.

  • Matrix addition:   adds the corresponding elements in the matrices

       Example:   

  • Scalar multiplication:   multiply each elements by a number

       Example:   

  • Transposition:   turn rows into columns and vice versa ("flip" a matrix)

       Example:   

Matrix scalar multiplication

Write a scalarMultiply( ) method that multiplies a matrix m with a number a

    public static double[][] scalarMultiply(double[][] m, double a) 
    {
        double[][] result = new double[m.length][m[0].length];
        


        for(int i = 0; i < m.length; i++) 
        {
            for(int j = 0; j < m[0].length; j++) 
            {
                result[i][j] = m[i][j] * a;
            }
        }

        return result;
    }

         

Matrix scalar multiplication

(1) Define a 2-dimensional array to store the result

    public static double[][] scalarMultiply(double[][] m, double a) 
    {
        double[][] result = new double[m.length][m[0].length];
                            // result matrix has
			    // same # rows and # columns as m
      
        for(int i = 0; i < m.length; i++) 
        {
            for(int j = 0; j < m[0].length; j++) 
            {
                result[i][j] = m[i][j] * a;
            }
        }

        return result;
    } 

         

Matrix scalar multiplication

(2) Compute the result

    public static double[][] scalarMultiply(double[][] m, double a) 
    {
        double[][] result = new double[m.length][m[0].length];
                            // result matrix has
			    // same # rows and # columns as m
    
        for (int i = 0; i < m.length; i++) 
        {
            for (int j = 0; j < m[0].length; j++) 
            {
                result[i][j] = m[i][j] * a;
            }
        }
  
        return result;
    } 

         

Matrix scalar multiplication

(3) Return the result

    public static double[][] scalarMultiply(double[][] m, double a) 
    {
        double[][] result = new double[m.length][m[0].length];
                            // result matrix has
			    // same # rows and # columns as m
    
        for (int i = 0; i < m.length; i++) 
        {
            for (int j = 0; j < m[0].length; j++) 
            {
                result[i][j] = m[i][j] * a;
            }
        }

        return result;
    } 

         

How to use the matrix scalar multiplication method   Demo program

 

    public static void main()
    {
        double [][] a =  { {1, 8, -3},
                           {4, -2, 5} };
                         
        double[][] b;
        
        b = scalarMultiply(a, 2);
        
        System.out.println( Arrays.toString(a[0]) );
        System.out.println( Arrays.toString(a[1]) );
        System.out.println( " * " + 2 + " = " );
        System.out.println( Arrays.toString(b[0]) );
        System.out.println( Arrays.toString(b[1]) );
        
    }

  

DEMO: demo/09-multi-dim-array/05-matrix/ScalarMult.java

Transpose a matrix

What happens when you transposes a matrix:

   Original matrix:           Transposed matrix:

      +-             -+             +-       -+
      | 1     2     3 |		    | 1     4 |
      | 4     5     6 |		    | 2     5 |
      +-             -+		    | 3     6 |
                                    +-       -+

    Change 1: Number of rows: 2         Number of rows: 3      (exchanged !)
              Number of columns: 3      Number of columns: 2

    Change 2: Row and column indexes: (exchanged !)

      +-             -+             +-        -+
      | 0,0  0,1  0,2 |		    | 0,0  0,1 |
      | 1,0  1,1  1,2 |		    | 1,0  1,1 |
      +-             -+		    | 2,0  2,1 |
                                    +-        -+
 

Transpose a matrix

Write a matrixTranspose( ) method that transposes a matrix M

    public static double[][] matrixTranspose(double[][] m) 
    {
        double[][] result = new double[m[0].length][m.length];
	                    // result matrix has:
			    //   # rows    = # columns in m
			    //   # columns = # rows in m
    
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                result[j][i] = m[i][j];
		            // Copy m's element in row i and column j
			    // to row j and column i in result
                
        return result;
    } 

               

Transpose a matrix

(1) Define a 2-dimensional matrix to store the result

    public static double[][] matrixTranspose(double[][] m) 
    {
        double[][] result = new double[m[0].length][m.length];
	                    // result matrix has:
			    //   # rows    = # columns in m (= m[0].length)
			    //   # columns = # rows in m (= m.length)
    
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                result[j][i] = m[i][j];
		            // Copy m's element in row i and column j
			    // to row j and column i in result
                
        return result;
    }

                

Transpose a matrix

(2) Compute the transpose

    public static double[][] matrixTranspose(double[][] m) 
    {
        double[][] result = new double[m[0].length][m.length];
	                    // result matrix has:
			    //   # rows    = # columns in m (= m[0].length)
			    //   # columns = # rows in m (= m.length)
 
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                result[j][i] = m[i][j];
		            // Copy m's element in row i and column j
			    // to row j and column i in result       
                
        return result;
    }

                

Transpose a matrix

(3) Return the result

    public static double[][] matrixTranspose(double[][] m) 
    {
        double[][] result = new double[m[0].length][m.length];
	                    // result matrix has:
			    //   # rows    = # columns in m (= m[0].length)
			    //   # columns = # rows in m (= m.length)
 
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                result[j][i] = m[i][j];
		            // Copy m's element in row i and column j
			    // to row j and column i in result       
              
        return result;
    }

                

How to use the matrix transpose method   Demo program

 

    public static void main()
    {
        double [][] a =  { {1, 8, -3},
                           {4, -2, 5} };
                         
        double[][] b;
        
        b = matrixTranspose(a);
        
        System.out.println( Arrays.toString(a[0]) );
        System.out.println( Arrays.toString(a[1]) );
        System.out.println( "Transpose:" );
        System.out.println( Arrays.toString(b[0]) );
        System.out.println( Arrays.toString(b[1]) );
        System.out.println( Arrays.toString(b[2]) );
    }
 

DEMO: demo/09-multi-dim-array/05-matrix/TransposeMatrix.java