Array Creation
Methods
================================ ============ Method Description ================================ ============ empty_ 3 methods Empty vector, matrix or tensor zeros_ 3 methods Zero vector, matrix or tensor vector_ 3 methods Create a vector matrix_ 3 methods Create a matrix tensor_ 2 methods Create a tensor eye_ (size) Identity matrix diag_ (a) Diagonal matrix arange_ (start, stop, step) Evenly spaced values in an interval linspace_ (start, stop[, num]) Evenly spaced numbers logspace_ (start, stop[, num]) Evenly spaced numbers on a log scale geomspace_ (start, stop[, num]) Evenly spaced numbers on a log scale meshgrid_ (x, y[, f]) Coordinate matrices from coordinate vectors tri_ (rows, cols[, k]) Matrix with ones below a given diagonal tril_ (a[, k]) Lower triangle of a matrix triu_ (a[, k]) Upper triangle of a matrix ================================ ============
Description
.. _empty:
.. function:: empty(size)
:param T: type parameter, implements ``Number``
:param size: Int64, size of the vector
:return: Vector<T>
Create an empty vector with the given size. Note the vector is *not*
initialized, hence may contain arbitrary content.
.. function:: empty(rows, cols)
:param T: type parameter, implements ``Number``
:param rows: Int64, number of rows of the matrix
:param cols: Int64, number of columns of the matrix
:return: Matrix<T>
Create an empty matrix with the given number of rows and columns. Note
the matrix is *not* initialized, hence may contain arbitrary content.
.. function:: empty(dims)
:param T: type parameter, implements ``Number``
:param dims: Array<Int64>, list of dimensions of the tensor
:return: Tensor<T>
Create an empty tensor with the given dimensions. Note the tensor is
*not* initialized, hence may contain arbitrary content.
.. _zeros:
.. function:: zeros(size)
:param T: type parameter, implements ``Number``
:param size: Int64, size of the vector
:return: Vector<T>
Create a vector with the given size, initialized to zeros.
.. function:: zeros(rows, cols)
:param T: type parameter, implements ``Number``
:param rows: Int64, number of rows of the matrix
:param cols: Int64, number of columns of the matrix
:return: Matrix<T>
Create a matrix with the given number of rows and columns,
initialized to zeros.
.. function:: zeros(dims)
:param T: type parameter, implements ``Number``
:param dims: Array<Int64>, list of dimensions of the tensor
:return: Tensor<T>
Create a tensor with the given dimensions, initialized to zeros.
.. _vector:
.. function:: vector(size, x)
:param T: type parameter, implements ``Number``
:param size: Int64, size of the vector
:param x: T, initial value
Create a vector with the given size, with each element initialized
to the given value.
.. function:: vector(size, f)
:param T: type parameter, implements ``Number``
:param size: Int64, size of the vector
:param f: (Int64) -> T, initialization function (mapping index to value).
Create a vector with the given size, with each element initialized
according to the provided function.
.. function:: vector(a)
:param T: type parameter, implements ``Number``
:param a: Array<T>, initialization array.
Create a vector with the same content as the input array.
.. _matrix:
.. function:: matrix(rows, cols, x)
:param T: type paramter, implements ``Number``
:param rows: Int64, number of rows of the matrix
:param cols: Int64, number of columns of the matrix
:param x: T, initial value
Create a matrix with the given number of rows and columns, with each
element initialized to the given value.
.. function:: matrix(rows, cols, f)
:param T: type parameter, implements ``Number``
:param rows: Int64, number of rows of the matrix
:param cols: Int64, number of columns of the matrix
:param f: (Int64, Int64) -> T, initialization function (mapping a pair
of indices to value).
Create a matrix with the given number of rows and columns, with each
element initialized according to the provided function.
.. function:: matrix(a)
:param T: type parameter, implements ``Number``
:param a: Array<Array<T>>, initialization array
Create a matrix with the same content as the input two-dimensional array.
.. _tensor:
.. function:: tensor(dims, x)
:param T: type parameter, implements ``Number``
:param dims: Array<Int64>, list of dimensions of the tensor
:param x: T, initial value
Create a tensor with the given size, with each element initialized
to the given value.
.. function:: tensor(dims, f)
:param T: type parameter, implements ``Number``
:param dims: Array<Int64>, list of dimensions of the tensor
:param f: (Array<Int64>) -> T, initialization function, (mapping a list
of indices to value).
Create a tensor with the given size, with each element initialized
according to the provided function.
.. _eye:
.. function:: eye(size)
:param T: type parameter, implements ``Number``
:param size: Int64, number of rows (and columns) of the matrix
Create the identity matrix with the given size.
.. _diag:
.. function:: diag(a)
:param T: type parameter, implements ``Number``
:param a: Vector<T>, values on the diagonal
:return: Matrix<T>
Return a diagonal matrix with given values on the diagonal.
.. _arange:
.. function:: arange(start, stop, step)
:param T: type parameter, implements ``Real``
:param start: T, starting value
:param stop: T, ending value
:param step: T, step size
:return: Vector<T>
Creates a range of values from ``start`` to ``stop``, with the given step size.
The function will continue to enumerate values until reaching a value greater
than or equal to ``stop`` (not including this value in the returned vector).
**Note**: be careful of round-off errors when ``T`` has floating point type.
In such cases using :ref:`linspace <linspace>` is suggested.
.. _linspace:
.. function:: linspace(start, stop[, num])
:param T: type parameter, implements ``Float``
:param start: T, starting value
:param stop: T, ending value
:param num: Int64, number of values in the returned vector. Optional, defaulting
to 100.
:return: Vector<T>
Create evenly-spaced values from ``start`` to ``stop``, with ``num`` giving the
number of values to be returned.
.. _logspace:
.. function:: logspace(start, stop[, num])
:param T: type parameter, implements ``Float``
:param start: T, base-10 logarithm of the starting value
:param stop: T, base-10 logarithm of the ending value
:param num: Int64, number of values in the returned vector. Optional, defaulting
to 50.
:return: Vector<T>
Create evenly-spaced values on the log scale, from base-10 logarithm of ``start``
to base-10 logarithm of ``stop``, with ``num`` giving the number of values to be
returned.
**Note**: use :ref:`geomspace <geomspace>` if it is more convenient to provide
starting and ending values directly.
.. _geomspace:
.. function:: geomspace(start, stop[, num])
:param T: type parameter, implements ``Float``
:param start: T, starting value
:param stop: T, ending value
:param num: Int64, number of values in the returned vector. Optional, defaulting
to 50.
:return: Vector<T>
Create evenly-spaced values on the log scale, from ``start`` to ``stop``, with
``num`` giving the number of values to be returned.
**Note**: use :ref:`logspace <logspace>` if it is more convenient to provide
logarithms of starting and ending values.
.. _meshgrid:
.. function:: meshgrid(x, y)
:param T: type parameter, implements ``Number``
:param x: Vector<T>, values on the :math:`x`-axis
:param y: Vector<T>, values on the :math:`y`-axis
:return: (Matrix<T>, Matrix<T>)
Return two matrices :math:`X` and :math:`Y` used for plotting two-dimensional
functions. Let :math:`c` be the size of ``x``, and :math:`r` be the size of ``y``,
then both :math:`X` and :math:`Y` are :math:`r`-by-:math:`c` matrices. Each column
in :math:`X` is constant, corresponding to a value in ``x``. Each row in :math:`Y`
is constant, corresponding to a value in ``y``.
.. function:: meshgrid(x, y, f)
:param T: type parameter, implements ``Number``
:param x: Vector<T>, values on the :math:`x`-axis
:param y: Vector<T>, values on the :math:`y`-axis
:param f: (T, T) -> T, function to apply to values in ``x`` and ``y``.
:return: (Matrix<T>, Matrix<T>, Matrix<T>)
Similar to ``meshgrid<T>(x, y)``, except also apply the provided function to
the values in ``x`` and ``y``. The returned triple :math:`X, Y, Z` can be used
directly for plotting the function ``f``.
.. _tri:
.. function:: tri(rows, cols[, k])
:param T: type parameter, implements ``Number``
:param rows: Int64, number of rows of the matrix
:param cols: Int64, number of columns of the matrix
:param k: Int64, index of the sub-diagonal. Optional, default to 0.
:return: Matrix<T>
Return a matrix with ones at and below the given diagonal and zeros elsewhere.
.. _tril:
.. function:: tril(a[, k])
:param T: type parameter, implements ``Number``
:param a: Matrix<T>, the original matrix
:param k: Int64, index of the sub-diagonal. Optional, default to 0.
:return: Matrix<T>
Return a copy of the given matrix, with elements above the diagonal ``k`` set to zero.
.. _triu:
.. function:: triu(a[, k])
:param T: type parameter, implements ``Number``
:param a: Matrix<T>, the original matrix
:param k: Int64, index of the sub-diagonal. Optional, default to 0.
:return: Matrix<T>
Return a copy of the given matrix, with elements below the diagonal ``k`` set to zero.