Package com.stata.sfi
Class Matrix
java.lang.Object
com.stata.sfi.Matrix
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 Summary
Modifier and TypeMethodDescriptionstatic intconvertSymmetricToStd(String name) Convert a symmetric matrix to a standard matrix.static intcreateMatrix(String name, int rows, int cols, double initialValue) Create a Stata matrix.static intcreateMatrix(String name, int rows, int cols, double initialValue, boolean isSymmetric) Create a Stata matrix.static doublegetAt(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.static double[]Get the data in a Stata matrix.static intgetMatrixCol(String name) Get the number of columns in a Stata matrix.static String[]getMatrixColNames(String name) Get the column names of a Stata matrix.static intgetMatrixRow(String name) Get the number of rows in a Stata matrix.static String[]getMatrixRowNames(String name) Get the row names of a Stata matrix.static intsetMatrixColNames(String name, String[] colNames) Set the column names of a Stata matrix.static intsetMatrixRowNames(String name, String[] rowNames) Set the row names of a Stata matrix.static intstoreMatrixAt(String name, int row, int col, double val) Store an element in an existing Stata matrix.
-
Method Details
-
convertSymmetricToStd
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
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 ofstoreMatrixAt(). When the matrix is marked as symmetric,storeMatrixAt()will always maintain symmetry.- Returns:
- Return code from Stata; 0 if successful.
-
getAt
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 callinggetMatrixCol().row- Zero-based row number.col- Zero-based column number.- Returns:
- The element.
-
getMatrix
Get the data in a Stata matrix. UsegetAt()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
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
Get the column names of a Stata matrix.- Parameters:
name- Name of the Stata matrix.- Returns:
- A
Stringarray containing the column names of the matrix. Returns null if an error occurs.
-
getMatrixRow
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
Get the row names of a Stata matrix.- Parameters:
name- Name of the Stata matrix.- Returns:
- A
Stringarray containing the row names of the matrix. Returns null if an error occurs.
-
setMatrixColNames
Set the column names of a Stata matrix.- Parameters:
name- Name of the Stata matrix.colNames- AStringarray 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
Set the row names of a Stata matrix.- Parameters:
name- Name of the Stata matrix.rowNames- AStringarray 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
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.
-