# Overview

SystemML enables flexible, scalable machine learning. This flexibility is achieved through the specification of a high-level declarative machine learning language that comes in two flavors, one with an R-like syntax (DML) and one with a Python-like syntax (PyDML).

Algorithm scripts written in DML and PyDML can be run on Spark, on Hadoop, or in Standalone mode. SystemML also features an MLContext API that allows SystemML to be accessed via Scala or Python from a Spark Shell, a Jupyter Notebook, or a Zeppelin Notebook.

This Beginner’s Guide serves as a starting point for writing DML and PyDML scripts.

# Script Invocation

DML and PyDML scripts can be invoked in a variety of ways. Suppose that we have hello.dml and hello.pydml scripts containing the following:

print('hello ' + \$1)


One way to begin working with SystemML is to download a binary distribution of SystemML and use the runStandaloneSystemML.sh and runStandaloneSystemML.bat scripts to run SystemML in standalone mode. The name of the DML or PyDML script is passed as the first argument to these scripts, along with a variety of arguments. Note that PyDML invocation can be forced with the addition of a -python flag.

./runStandaloneSystemML.sh hello.dml -args world
./runStandaloneSystemML.sh hello.pydml -args world


# Data Types

SystemML has four value data types. In DML, these are: double, integer, string, and boolean. In PyDML, these are: float, int, str, and bool. In normal usage, the data type of a variable is implicit based on its value. Mathematical operations typically operate on doubles/floats, whereas integers/ints are typically useful for tasks such as iteration and accessing elements in a matrix.

aDouble = 3.0
bInteger = 2
bInteger ^ 3 = 8.0
cBoolean = TRUE
(2 < 1) = FALSE
dString = Hello
eString = Hello World

aFloat = 3.0
bInt = 2
aFloat + bInt = 5.0
bInt ** 3 = 8.0
aFloat ** 2 = 9.0
cBool = TRUE
(2 < 1) = FALSE
dStr = Hello
eStr = Hello World


# Matrix Basics

## Creating a Matrix

A matrix can be created in DML using the matrix() function and in PyDML using the full() function. In the example below, a matrix element is still considered to be of the matrix data type, so the value is cast to a scalar in order to print it. Matrix element values are of type double/float.

[1,1]:1.0
[1,2]:2.0
[1,3]:3.0
[2,1]:4.0
[2,2]:5.0
[2,3]:6.0
[3,1]:7.0
[3,2]:8.0
[3,3]:9.0
[4,1]:10.0
[4,2]:11.0
[4,3]:12.0

[0,0]:1.0
[0,1]:2.0
[0,2]:3.0
[1,0]:4.0
[1,1]:5.0
[1,2]:6.0
[2,0]:7.0
[2,1]:8.0
[2,2]:9.0
[3,0]:10.0
[3,1]:11.0
[3,2]:12.0


We can also output the matrix element values using the toString function:

1.0 | 2.0 | 3.0
4.0 | 5.0 | 6.0
7.0 | 8.0 | 9.0
10.0 | 11.0 | 12.0


For additional information about the matrix() and full() functions, please see the Matrix Construction section of the Language Reference. For information about the toString() function, see the Other Built-In Functions section of the Language Reference.

## Saving a Matrix

A matrix can be saved using the write() function in DML and the save() function in PyDML. SystemML supports four different formats: text (i,j,v), mm (Matrix Market), csv (delimiter-separated values), and binary.

Saving a matrix automatically creates a metadata file for each format except for Matrix Market, since Matrix Market contains metadata within the *.mm file. All formats are text-based except binary. The contents of the resulting files are shown here. Note that the text (i,j,v) and mm (Matrix Market) formats index from 1, even when working with PyDML, which is 0-based.

1 1 1.0
1 2 2.0
1 3 3.0
3 1 7.0
3 2 8.0
3 3 9.0

{
"data_type": "matrix",
"value_type": "double",
"rows": 4,
"cols": 3,
"nnz": 6,
"format": "text",
"author": "SystemML",
"created": "2017-01-01 00:00:01 PST"
}

%%MatrixMarket matrix coordinate real general
4 3 6
1 1 1.0
1 2 2.0
1 3 3.0
3 1 7.0
3 2 8.0
3 3 9.0

1.0,2.0,3.0
0,0,0
7.0,8.0,9.0
0,0,0

{
"data_type": "matrix",
"value_type": "double",
"rows": 4,
"cols": 3,
"nnz": 6,
"format": "csv",
"sep": ",",
"author": "SystemML",
"created": "2017-01-01 00:00:01 PST"
}

Not text-based

{
"data_type": "matrix",
"value_type": "double",
"rows": 4,
"cols": 3,
"rows_in_block": 1000,
"cols_in_block": 1000,
"nnz": 6,
"format": "binary",
"author": "SystemML",
"created": "2017-01-01 00:00:01 PST"
}


A matrix can be loaded using the read() function in DML and the load() function in PyDML. As with saving, SystemML supports four formats: text (i,j,v), mm (Matrix Market), csv (delimiter-separated values), and binary. To read a file, a corresponding metadata file is required, except for the Matrix Market format. A metadata file is not required if a format parameter is specified to the read() or load() functions.

min:0.0
max:9.0
sum:30.0
row 1 sum:6.0
row 2 sum:0.0
row 3 sum:24.0
row 4 sum:0.0
col 1 sum:8.0
col 2 sum:10.0
col 3 sum:12.0

min:0.0
max:9.0
sum:30.0
row 0 sum:6.0
row 1 sum:0.0
row 2 sum:24.0
row 3 sum:0.0
col 0 sum:8.0
col 1 sum:10.0
col 2 sum:12.0


## Matrix Operations

DML and PyDML offer a rich set of operators and built-in functions to perform various operations on matrices and scalars. Operators and built-in functions are described in great detail in the Language Reference (Expressions, Built-In Functions).

In this example, we create a matrix A. Next, we create another matrix B by adding 4 to each element in A. Next, we flip B by taking its transpose. We then multiply A and B, represented by matrix C. We create a matrix D with the same number of rows and columns as C, and initialize its elements to 5. We then subtract D from C and divide the values of its elements by 2 and assign the resulting matrix to D.

1.000 2.000
3.000 4.000
5.000 6.000

5.000 7.000 9.000
6.000 8.000 10.000

17.000 23.000 29.000
39.000 53.000 67.000
61.000 83.000 105.000

6.000 9.000 12.000
17.000 24.000 31.000
28.000 39.000 50.000


## Matrix Indexing

The elements in a matrix can be accessed by their row and column indices. In the example below, we have 3x3 matrix A. First, we access the element at the third row and third column. Next, we obtain a row slice (vector) of the matrix by specifying the row and leaving the column blank. We obtain a column slice (vector) by leaving the row blank and specifying the column. After that, we obtain a submatrix via range indexing, where we specify rows, separated by a colon, and columns, separated by a colon.

1.000 2.000 3.000
4.000 5.000 6.000
7.000 8.000 9.000

9.000

4.000 5.000 6.000

3.000
6.000
9.000

4.000 5.000
7.000 8.000


# Control Statements

DML and PyDML both feature if, if-else, and if-else-if conditional statements.

DML and PyDML feature 3 loop statements: while, for, and parfor (parallel for). In the example, note that the print statements within the parfor loop can occur in any order since the iterations occur in parallel rather than sequentially as in a regular for loop. The parfor statement can include several optional parameters, as described in the Language Reference (ParFor Statement).

hello
world
!!!
for A[1,1]:1.0
for A[2,1]:3.0
for A[3,1]:5.0
parfor A[2,1]:3.0
parfor A[1,1]:1.0
parfor A[3,1]:5.0

hello
world
!!!
for A[0,0]:1.0
for A[1,0]:3.0
for A[2,0]:5.0
parfor A[0,0]:1.0
parfor A[2,0]:5.0
parfor A[1,0]:3.0


# User-Defined Functions

Functions encapsulate useful functionality in SystemML. In addition to built-in functions, users can define their own functions. Functions take 0 or more parameters and return 0 or more values.

In the above example, a 3x2 matrix of random doubles between 0 and 2 is created using the rand() function. Additional parameters can be passed to rand() to control sparsity and other matrix characteristics.

Matrix A is passed to the doSomething function. A column of 1 values is concatenated to the matrix. A column consisting of the values (0, 1, 2) is concatenated to the matrix. Next, a column consisting of the maximum row values is concatenated to the matrix. A column consisting of the row sums is concatenated to the matrix, and this resulting matrix is returned to variable B. Matrix A is output to the A.csv file and matrix B is saved as the B.csv file.

1.6091961493071,0.7088614208099939
0.5984862383600267,1.5732118950764993
0.2947607068519842,1.9081406573366781

1.6091961493071,0.7088614208099939,1.0,0,1.6091961493071,4.927253719424194
0.5984862383600267,1.5732118950764993,1.0,1.0,1.5732118950764993,5.744910028513026
0.2947607068519842,1.9081406573366781,1.0,2.0,2.0,7.202901364188662


# Command-Line Arguments and Default Values

Command-line arguments can be passed to DML and PyDML scripts either as named arguments or as positional arguments. Named arguments are the preferred technique. Named arguments can be passed utilizing the -nvargs switch, and positional arguments can be passed using the -args switch.

Default values can be set using the ifdef() function.

In the example below, a matrix is read from the file system using named argument M. The number of rows to print is specified using the rowsToPrint argument, which defaults to 2 if no argument is supplied. Likewise, the number of columns is specified using colsToPrint with a default value of 2.

Example #1 Arguments:
-f ex.dml -nvargs M=m.csv rowsToPrint=1 colsToPrint=3

Example #1 Results:
[1,1]:1.0
[1,2]:2.0
[1,3]:3.0

Example #2 Arguments:
-f ex.dml -nvargs M=m.csv

Example #2 Results:
[1,1]:1.0
[1,2]:2.0
[2,1]:0.0
[2,2]:0.0

Example #1 Arguments:
-f ex.pydml -nvargs M=m.csv rowsToPrint=1 colsToPrint=3

Example #1 Results:
[0,0]:1.0
[0,1]:2.0
[0,2]:3.0

Example #2 Arguments:
-f ex.pydml -nvargs M=m.csv

Example #2 Results:
[0,0]:1.0
[0,1]:2.0
[1,0]:0.0
[1,1]:0.0


Here, we see identical functionality but with positional arguments.

Example #1 Arguments:
-f ex.dml -args m.csv 1 3

Example #1 Results:
[1,1]:1.0
[1,2]:2.0
[1,3]:3.0

Example #2 Arguments:
-f ex.dml -args m.csv

Example #2 Results:
[1,1]:1.0
[1,2]:2.0
[2,1]:0.0
[2,2]:0.0

Example #1 Arguments:
-f ex.pydml -args m.csv 1 3

Example #1 Results:
[0,0]:1.0
[0,1]:2.0
[0,2]:3.0

Example #2 Arguments:
-f ex.pydml -args m.csv

Example #2 Results:
[0,0]:1.0
[0,1]:2.0
[1,0]:0.0
[1,1]:0.0


In addition, many excellent examples can be found in the scripts directory.