Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Matrix

Create a matrix (i.e., a two-dimensional ndarray).

Usage

var matrix = require( '@stdlib/ndarray/matrix/ctor' );

matrix( [dtype][, options] )

Returns a two-dimensional ndarray having a specified data type.

var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );

var arr = matrix();
// returns <ndarray>

var sh = getShape( arr );
// returns [ 0, 0 ]

var dt = String( getDType( arr ) );
// returns 'float64'

By default, the function returns an ndarray having a float64 data type. To specify an alternative data type, provide a dtype argument.

var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );

var arr = matrix( 'int32' );
// returns <ndarray>

var sh = getShape( arr );
// returns [ 0, 0 ]

var dt = String( getDType( arr ) );
// returns 'int32'

The function accepts the following arguments:

  • dtype: data type.
  • options: function options.

The function accepts the following options:

  • order: specifies whether an ndarray is 'row-major' (C-style) or 'column-major' (Fortran-style). Default: 'row-major'.
  • mode: specifies how to handle indices which exceed array dimensions (see ndarray). Default: 'throw'.
  • submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see ndarray). If provided fewer modes than dimensions, an ndarray) instance recycles modes using modulo arithmetic. Default: [ options.mode ].
  • readonly: boolean indicating whether an array should be read-only. Default: false.

matrix( M, N[, dtype][, options] )

Returns a two-dimensional ndarray having a specified shape.

var getShape = require( '@stdlib/ndarray/shape' );

var arr1 = matrix( 5, 5 );
// returns <ndarray>

var sh1 = getShape( arr1 );
// returns [ 5, 5 ]

var arr2 = matrix( 3, 3, 'uint8' );
// returns <ndarray>

var sh2 = getShape( arr2 );
// returns [ 3, 3 ]

The function accepts the following arguments:

  • M: number of rows.
  • N: number of columns.
  • dtype: data type.
  • options: function options. See above.

matrix( shape[, dtype][, options] )

Returns a two-dimensional ndarray having a specified shape.

var getShape = require( '@stdlib/ndarray/shape' );

var arr1 = matrix( [ 5, 5 ] );
// returns <ndarray>

var sh1 = getShape( arr1 );
// returns [ 5, 5 ]

var arr2 = matrix( [ 3, 3 ], 'uint8' );
// returns <ndarray>

var sh2 = getShape( arr2 );
// returns [ 3, 3 ]

The function accepts the following arguments:

  • shape: array shape. Must contain exactly two elements.
  • dtype: data type.
  • options: function options. See above.

matrix( obj[, dtype][, options] )

Creates a two-dimensional ndarray from an array-like object or iterable.

var getShape = require( '@stdlib/ndarray/shape' );

var arr1 = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>

var sh1 = getShape( arr1 );
// returns [ 2, 2 ]

var arr2 = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], 'float32' );
// returns <ndarray>

var sh2 = getShape( arr2 );
// returns [ 2, 2 ]

The function accepts the following arguments:

  • obj: array-like object or iterable from which to generate an ndarray. If an array-like object, the value must be a nested array (i.e., an array-like object of array-like objects), where each nested array must have the same number of elements. If an iterable, the iterable must return array-like objects, each of which must have the same number of elements.
  • dtype: data type.
  • options: function options. See above.

matrix( buffer[, byteOffset[, M, N]][, dtype][, options] )

Returns a two-dimensional ndarray view of an ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array/buffer' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );

var buf = new ArrayBuffer( 32 );

var arr1 = matrix( buf );
// returns <ndarray>

var sh1 = getShape( arr1 );
// returns [ 1, 4 ]

var dt1 = String( getDType( arr1 ) );
// returns 'float64'

var arr2 = matrix( buf, 'float32' );
// returns <ndarray>

var sh2 = getShape( arr2 );
// returns [ 1, 8 ]

var dt2 = String( getDType( arr2 ) );
// returns 'float32'

var arr3 = matrix( buf, 16 );
// returns <ndarray>

var sh3 = getShape( arr3 );
// returns [ 1, 2 ]

var dt3 = String( getDType( arr3 ) );
// returns 'float64'

var arr4 = matrix( buf, 16, 'float32' );
// returns <ndarray>

var sh4 = getShape( arr4 );
// returns [ 1, 4 ]

var dt4 = String( getDType( arr4 ) );
// returns 'float32'

var arr5 = matrix( buf, 8, 2, 1 );
// returns <ndarray>

var sh5 = getShape( arr5 );
// returns [ 2, 1 ]

var dt5 = String( getDType( arr5 ) );
// returns 'float64'

var arr6 = matrix( buf, 10, 2, 2, 'int16' );
// returns <ndarray>

var sh6 = getShape( arr6 );
// returns [ 2, 2 ]

var dt6 = String( getDType( arr6 ) );
// returns 'int16'

The function accepts the following arguments:

  • buffer: underlying ArrayBuffer.
  • byteOffset: integer byte offset specifying the location of the first indexed element. Default: 0.
  • shape: array shape. Must contain exactly two elements.
  • dtype: data type.
  • options: function options. See above.

matrix( buffer[, byteOffset[, shape]][, dtype][, options] )

Returns a two-dimensional ndarray view of an ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array/buffer' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );

var buf = new ArrayBuffer( 32 );

var arr1 = matrix( buf, 8, [ 2, 1 ] );
// returns <ndarray>

var sh1 = getShape( arr1 );
// returns [ 2, 1 ]

var dt1 = String( getDType( arr1 ) );
// returns 'float64'

var arr2 = matrix( buf, 10, [ 2, 2 ], 'int16' );
// returns <ndarray>

var sh2 = getShape( arr2 );
// returns [ 2, 2 ]

var dt2 = String( getDType( arr2 ) );
// returns 'int16'

The function accepts the following arguments:

  • buffer: underlying ArrayBuffer.
  • byteOffset: integer byte offset specifying the location of the first indexed element. Default: 0.
  • M: number of rows.
  • N: number of columns.
  • dtype: data type.
  • options: function options. See above.

matrix.factory( dtype[, options] )

Returns a function for creating a two-dimensional ndarray.

var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );

var Float32Matrix = matrix.factory( 'float32' );

var arr = new Float32Matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>

var dt = String( getDType( arr ) );
// returns 'float32'

var len = getShape( arr );
// returns [ 2, 2 ]

The function accepts the following arguments:

  • dtype: data type.
  • options: function options (optional).

The function accepts the following options:

  • order: specifies whether the default memory layout for a returned ndarray should be 'row-major' (C-style) or 'column-major' (Fortran-style). Default: 'row-major'.
  • mode: specifies the default behavior when handling indices which exceed array dimensions (see ndarray). Default: 'throw'.
  • submode: specifies the default behavior for each dimension when handling subscripts which exceed array dimensions (see ndarray). If provided fewer modes than dimensions, an ndarray) instance recycles modes using modulo arithmetic. Default: [ options.mode ].
  • readonly: boolean indicating whether to return a read-only ndarray by default. Default: false.

The function returned by the factory method supports the same arguments and options as matrix above, except for the dtype argument, as the returned function always returns a two-dimensional ndarray having the same data type.

When providing options to the returned function, the provided option values override the defaults established during function creation.

Notes

  • Nested arrays are flattened in lexicographic order, such that, for an input nested array obj, obj[i][j] corresponds to the element mat.get(i, j) in the returned two-dimensional ndarray. In other words, an input nested array is assumed to be comprised of rows. Similarly, if obj is an iterable, each array returned by the iterable is assumed to correspond to a matrix row.
  • If provided an ArrayBuffer without corresponding shape arguments, the leading dimension of the returned ndarray always has a size of one.

Examples

var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var cartesianProduct = require( '@stdlib/array/cartesian-product' );
var unzip = require( '@stdlib/utils/unzip' );
var dtypes = require( '@stdlib/ndarray/dtypes' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var sum = require( '@stdlib/blas/ext/sum' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var matrix = require( '@stdlib/ndarray/matrix/ctor' );

// Create an array of random shapes:
var shapes = discreteUniform( [ 10, 2 ], 2, 8, {
    'dtype': 'int32'
});

// Resolve a list of supported ndarray integer-valued data types:
var dts = dtypes( 'integer_and_generic' );

// Create shape-dtype pairs:
var pairs = cartesianProduct( ndarray2array( shapes ), dts );

// Split the pairs into individual arguments:
var args = unzip( pairs );

// Define a callback to create a random matrix and return the sum of all matrix elements:
function clbk( shape, dtype ) {
    var buf = ndarray2array( discreteUniform( shape, 0, 100 ) );
    var x = matrix( buf, dtype );
    return sum( x ).get();
}

// Apply the callback and print the results:
logEachMap( 'shape: [%3s]. dtype: %7s. sum: %d.', args[ 0 ], args[ 1 ], clbk );