GSL Shell main data types are real and complex matrices and many of the GSL routines works on this kind of data. GSL Shell does not have a separate type to rapresent a vector but we use column matrix for this purpose.
With matrix you can do basic algebraic operation just by using the ‘+’ and ‘*’ operators to perform element by element operations with the matrix operands. In order to perform real matrix multiplication you can use the functions mul() and prod(). The first one take an arbitrary number of arguments and perform the matrix products of its arguments. The prod() function instead takes exactly two arguments and perform the matrix product of the transpose of the first with the second argument. So, for example, if x and y are two column matrices the expression prod(x, y) gives their scalar product.
In order to create a new matrix you should use the new() function for a real matrix and cnew() for a complex matrix. Most of GSL Shell function comes in two variants for complex and real number. Generally the complex variant has the same name of the real one but with ‘c’ prefix.
The new() function takes two mandatory arguments, the number of rows and of columns and an optional third argument. If the third argument is not given all the elements of the matrix are initialised to zero. Otherwise, if you provide a function of two variables, lets say i and j, it will be used to initialise the element of the matrix. The provided function will be called for each element of the matrix with the index of row and column as an argument. This provides a very useful way to initialise matrices. As an example, let us suppose that we want to define a matrix m of dimension whose elements are given by
.
In GSL Shell we can define the matrix with the following command:
-- we assume that n is a positive integer number
m = cnew(n, n, |j,k| exp(2i*pi*(j-1)*(k-1)/n))
where you have previously defined n to be a small integer number.
Return the Frobenius norm of the matrix. It is defined as:
where aij are the elements of the matrix.
Return an iterator that gives all the rows of the matrix as a submatrix.
Example to calculate the norm of each row of a matrix m:
for r in m:rows() do
print(r:norm())
end
All the functions described in this section have an equivalent function for complex valued matrix. The functions for complex matrix are obtained by adding the ‘c’ prefix to the name. So for example the function mul() has an equivalent for complex matrix whose name is cmul().
Set the matrix a to be equal to the matrix b. It raise an error if the dimensions of the matrices are different. Please note that it is different than the statement:
a = b
because this latter simple make the variable a refer to the same matrix of b. With the set() function you set each element of an existing matrix a to the same value of the corresponding element of b.