Package com.stata.sfi

Class Matrix

java.lang.Object
com.stata.sfi.Matrix

public final class Matrix extends Object
This class provides access to Stata matrices.

Example:

	public static int printMatrix(String args[]) {
		String name = args[0];
		double[] matrix = Matrix.getMatrix(name);
		int cols = Matrix.getMatrixCol(name);
		int rows = Matrix.getMatrixRow(name);

		if (cols == 0 || rows == 0) {
			SFIToolkit.errorln("Empty matrix");
			return 503;
		}
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				double element = Matrix.getAt(matrix, cols, i, j);
				SFIToolkit.displayln("[" + (i + 1) + ", " + (j + 1) + "] = "
						+ element);
			}
		}
		return 0;
	}

  • Method Details

    • convertSymmetricToStd

      @Synchronized public static int convertSymmetricToStd(String name)
      Convert a symmetric matrix to a standard matrix. If the matrix is not symmetric, then nothing is done.
      Parameters:
      name - Name of the matrix.
      Returns:
      Return code from Stata; 0 if successful.
    • createMatrix

      @Synchronized public static int createMatrix(String name, int rows, int cols, double initialValue)
      Create a Stata matrix.
      Parameters:
      name - Name of the matrix to create.
      rows - Number of rows.
      cols - Number of columns.
      initialValue - An initialization value for each element.
      Returns:
      Return code from Stata; 0 if successful.
    • createMatrix

      @Synchronized public static int createMatrix(String name, int rows, int cols, double initialValue, boolean isSymmetric)
      Create a Stata matrix.
      Parameters:
      name - Name of the matrix to create.
      rows - Number of rows.
      cols - Number of columns.
      initialValue - An initialization value for each element.
      isSymmetric - Mark the matrix as symmetric. If the number of rows and columns are not equal, this parameter will be ignored. This parameter affects the behavior of storeMatrixAt(). When the matrix is marked as symmetric, storeMatrixAt() will always maintain symmetry.
      Returns:
      Return code from Stata; 0 if successful.
    • getAt

      @Synchronized public static double getAt(double[] matrix, int colCount, int row, int col)
      Access an element of a previously obtained Stata matrix in the form of a one-dimensional array.
      Parameters:
      matrix - The matrix in the form of a one-dimensional array.
      colCount - The number of columns. This value can be obtained by calling getMatrixCol().
      row - Zero-based row number.
      col - Zero-based column number.
      Returns:
      The element.
    • getMatrix

      @Synchronized public static double[] getMatrix(String name)
      Get the data in a Stata matrix. Use getAt() to obtain elements of the returned array.
      Parameters:
      name - Name of the Stata matrix.
      Returns:
      A double array containing the matrix values. Returns null if an error occurs.
    • getMatrixCol

      @Synchronized public static int getMatrixCol(String name)
      Get the number of columns in a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      Returns:
      The number of columns. Returns -1 if an error occurs.
    • getMatrixColNames

      @Synchronized public static String[] getMatrixColNames(String name)
      Get the column names of a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      Returns:
      A String array containing the column names of the matrix. Returns null if an error occurs.
    • getMatrixRow

      @Synchronized public static int getMatrixRow(String name)
      Get the number of rows in a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      Returns:
      The number of rows. Returns -1 if an error occurs.
    • getMatrixRowNames

      @Synchronized public static String[] getMatrixRowNames(String name)
      Get the row names of a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      Returns:
      A String array containing the row names of the matrix. Returns null if an error occurs.
    • setMatrixColNames

      @Synchronized public static int setMatrixColNames(String name, String[] colNames)
      Set the column names of a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      colNames - A String array containing the column names for the matrix. The array length must match the number of columns in the matrix.
      Returns:
      Return code from Stata; 0 if successful.
    • setMatrixRowNames

      @Synchronized public static int setMatrixRowNames(String name, String[] rowNames)
      Set the row names of a Stata matrix.
      Parameters:
      name - Name of the Stata matrix.
      rowNames - A String array containing the row names for the matrix. The array length must match the number of rows in the matrix.
      Returns:
      Return code from Stata; 0 if successful.
    • storeMatrixAt

      @Synchronized public static int storeMatrixAt(String name, int row, int col, double val)
      Store an element in an existing Stata matrix.
      Parameters:
      name - Name of the matrix.
      row - Zero-based row number.
      col - Zero-based column number.
      val - Value.
      Returns:
      Return code from Stata; 0 if successful.