# N-Dimensional Array

It can store real as well as complex numbers in n-dimensions It can be used to store Vectors (1D) or Matrices (2D). This class stores the data internally in flat typed arrays

NDArray is the central class of Bluemath library. It's used to input and output data to/from most of the APIs of this library.

## Construction

You can create an NDArray

• With shape and/or data type

// 3-dimensional array with 32-bit integer storage
new NDArray({shape:[3,4,3],datatype:'i32'});

• Initializing it with array data

// 2x3 Matrix with 64-bit floating point (double) storage
new NDArray([[1,1,1],[4,4,4]],{datatype:'f64'});

• Using standard functions

zeros([2,2,2]); // Returns 2x2x2 NDArray of zeros
eye([4,4]); // Creates 4x4 Identity matrix


## Basic math operations

Bluemath provides functions that allow basic math operations on NDArrays

sub

mul

div

• NDArray

## Properties

### datatype

datatype: NumberType

Data type of each number, specified by a string code

### shape

shape: number[]

Array of array dimensions. First being the outermost dimension.

### size

size: number

Size of the data (i.e. number of real/complex numbers stored in this array)

data:

## Methods

• all(): void

• any(): void

### argsort

• argsort(): void

### clone

• Create deep copy of the array

### fill

• fill(value: number): void
• Set all members of this array to given value

### flatten

• Return 1D copy of this array

### forEach

• forEach(callback: function): void
• Iterate over each element, invoke a callback with each index and value

#### Parameters

• ##### callback: function
• (value: number | Complex, ...index: number[]): void

### get

• get(...index: number[]): number | Complex
• Access member at given index

### isEqual

• isEqual(other: NDArray, tolerance?: number): boolean
• Does equality test for each element of the array as well as the shape of the arrays

#### Parameters

• ##### other: NDArray

Other NDArray to compare with

### isShapeEqual

• isShapeEqual(other: NDArray): boolean
• Checks if the shape of this ndarray matches the shape of other

### max

• max(axis?: number | number[]): number | NDArray

• mean(): void

• min(): void

### reshape

• reshape(shape: number[]): void
• Set new shape for the data stored in the array The old data remains intact. If the total size with the new shape is larger than the old size, then excess elements of the data are fill with zero.

New shape

### set

• set(...args: (number | Complex)[]): void
• Set member at given index All but the last argument should specify the index. The last argument is the value to set.

### slice

• slice(...slices: (undefined | null | string | number)[]): NDArray
• Bluemath supports extracting of NDArray slices using a syntax similar to numpy. Slicing is supported by NDArray.slice function.

The function accepts number of arguments not greater than the dimensions of the NDArray. Each argument could be a number, a string in format <start>:<stop> or undefined or null.

If the argument is a number then it represents a single slice, i.e. all the elements in the lower dimension are returned for this index in given dimension.

let A = new NDArray([
[2,4,6],
[1,0,9],
[0,2,3]
]);
A.slice(0); // [[2,4,6]]


If the argument is undefined or null, then that's interpreted as all items in the given dimension.

A.slice(null); // [[2,4,6],[1,0,9],[0,2,3]]
A.slice(1,null); // [[1,0,9]]


A string argument of format <start>:<stop> is used to specify range of slices in the given dimension. Both <start> and <stop> are optional.

A.slice('1:2'); // [[1,0,9]]
A.slice(':1'); // [[2,4,6]]
A.slice(':'); // [[2,4,6],[1,0,9],[0,2,3]]
A.slice(':',2); // [[6],[9],[3]]


The argument order is interpreted as going from outermost dimension to innermost.

## Caveats

• Negative indices not supported yet
• No support for <start>:<stop>:<step> format yet

• sort(): void

### swapOrder

• swapOrder(): void
• Change between Row-major and Column-major layout

### swaprows

• swaprows(i: number, j: number): void
• Swaps matrix rows (this must be a 2D array)

### take

• take(indices: number[], axis: number): NDArray

### toArray

• toArray(): any

### toHTML

• toHTML(precision?: number): any

### toString

• toString(precision?: number): any