clojure.core.matrix

Main namespace for the core.matrix API. 

Functions in this API may be supported by multiple matrix implementations, allowing code that uses 
this API to quickly switch between implementations without significant changes (if any).

abs

(abs m)
Computes the abs function on all elements of an array, using double precision values. Returns a new array.

abs!

(abs! m)
Computes the abs function on all elements of an array, using double precision values. Mutates the array in-place.

acos

(acos m)
Computes the acos function on all elements of an array, using double precision values. Returns a new array.

acos!

(acos! m)
Computes the acos function on all elements of an array, using double precision values. Mutates the array in-place.

add

(add)(add a)(add a b)(add a b & more)
Performs element-wise addition on one or more numerical arrays.

add!

(add! a)(add! a b)(add! a b & more)
Performs element-wise mutable addition on one or more numerical arrays. This is the mutable 
equivalent of `add`.

When adding many arrays, use of `add!` with a mutable array as the first argument is
usually faster than repreated use of `add` because it can avoid unnecessary copying.

Returns the first array after it has been mutated.

add-inner-product!

(add-inner-product! m a b)(add-inner-product! m a b factor)
Adds the inner product of two numerical arrays to the first array.
Returns the mutated array.
This is equivalent to (add! m (inner-product a b)) but may be optimised by the underlying implementation.

add-product

(add-product m a b)
Adds the element-wise product of two numerical arrays to the first array.
Arrays should be the same shape.

add-product!

(add-product! m a b)
Adds the product of two numerical arrays to the first array. Returns the mutated array.

add-row

(add-row m i j)(add-row m i j factor)
Add a row j (optionally multiplied by a scalar factor) to a row i
and replace row i with the result

add-scaled

(add-scaled m a factor)
Adds a numerical array scaled by a given factor to the first array

add-scaled!

(add-scaled! m a factor)
Adds a numerical array scaled by a given factor to the first array. Returns the mutated array.

add-scaled-product

(add-scaled-product m a b factor)
Adds the product of two numerical arrays scaled by a given factor to the first array.

This is equivalent to (add m (mul a b factor)) but may be optimised by the underlying implementation.

add-scaled-product!

(add-scaled-product! m a b factor)
Adds the product of two numerical arrays scaled by a given factor to the first array.
Returns the mutated array.
This is equivalent to (add! m (mul a b factor)) but may be optimised by the underlying implementation.

array

(array data)(array implementation data)
Constructs a new n-dimensional array from the given data.

This function will examine the data in order to construct an array of the appropriate shape.

The data may be in one of the following forms:
- A valid existing array (which will be converted to the implementation)
- Nested sequences of scalar values, e.g. Clojure vectors (must have regular shape)
- A sequence of slices, each of which must be valid array data
- A single scalar value, which will be wrapped or coerced as necessary for the implementation

If implementation is not specified, uses the current matrix library as specified
in *matrix-implementation*

If the implementation does not support the shape or type of data provided, may either
create an array using a different implementation on a best-efforts basis or
alternatively throw an error. This behaviour is implementation-specific.

array?

(array? m)
Returns true if the parameter is an N-dimensional array, for any N>=0.

as-vector

(as-vector m)
Creates a view of an array as a single flattened vector.

Returns nil if this is not supported by the implementation. You should use `to-vector` instead to
obtain a flattened vector without guaranteeing a view.

asin

(asin m)
Computes the asin function on all elements of an array, using double precision values. Returns a new array.

asin!

(asin! m)
Computes the asin function on all elements of an array, using double precision values. Mutates the array in-place.

assign

(assign m a)
Assigns array a element-wise, broadcasting to fill the whole shape of m.
Returns a new matrix, of the same shape as the original m.

assign!

(assign! m a)
Assigns a new value to an array. Sets the values of the target element-wise, broadcasting where necessary.
Returns the mutated array. The new value may be either a scalar or a array of compatible (maybe smaller) shape.

assign-array!

(assign-array! m a)
Assigns values to a core.matrix array from a Java array.
Returns the mutated core.matrix array

atan

(atan m)
Computes the atan function on all elements of an array, using double precision values. Returns a new array.

atan!

(atan! m)
Computes the atan function on all elements of an array, using double precision values. Mutates the array in-place.

block-diagonal-matrix

(block-diagonal-matrix blocks)(block-diagonal-matrix implementation blocks)
Constructs a block diagonal matrix for a given vector of 2D square matrices and arranges
the matrices along the main diagonal of the 2D matrix

broadcast

(broadcast m shape)
Broadcasts a matrix to a specified shape. Returns a new matrix with the shape specified.
The broadcasted matrix may be a view over the original matrix: attempting to modify the
broadcasted matrix therefore has undefined results.

Will throw an exception if broadcast to the target shape is not possible.

broadcast-coerce

(broadcast-coerce m a)
Broadcasts and coerces the second matrix to the shape and type of the first.
Equivalent to (coerce m (broadcast-like m a)).

broadcast-like

(broadcast-like m a)
Broadcasts the second matrix to the shape of the first. See 'broadcast'.

cbrt

(cbrt m)
Computes the cbrt function on all elements of an array, using double precision values. Returns a new array.

cbrt!

(cbrt! m)
Computes the cbrt function on all elements of an array, using double precision values. Mutates the array in-place.

ceil

(ceil m)
Computes the ceil function on all elements of an array, using double precision values. Returns a new array.

ceil!

(ceil! m)
Computes the ceil function on all elements of an array, using double precision values. Mutates the array in-place.

clamp

(clamp m a b)
Clamps each element in a numerical array between lower and upper bounds
specified by a and b, respectively.

Examples:
(clamp [[1 5 1] [4 10 2] [5 6 3]] 2 8) ;=> [[2 5 2] [4 8 2] [5 6 3]]

clone

(clone m)
Constructs a (shallow) clone of the array. This function is intended to
allow safe defensive usage of matrices / vectors. If the intent is to create a mutable clone of
some array data, it is recommended to use mutable instead.

Guarantees that:
1. Mutating the returned array will not modify any other array (defensive copy)
2. The returned array will be fully mutable, if the implementation supports mutable matrices.

The clone may or may not be of the same implementation: implementations are encouraged to do so but
this is not mandatory.

A core.matrix implementation which only provides immutable arrays may safely return the same array.

cmp

(cmp a b)
Element-wise of comparisons of two arrays. Returns the signum of the difference 
 between corresponding elements in two arrays.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(cmp 1 3) ;=> -1
(cmp 0 0) ;=> 0
(cmp 1 -1) ;=> 1
(cmp [[1 3] [5 5]] [[3 3] [5 3]]) ;=> [[-1 0] [0 1]]
(cmp [[1 4][1 5][1 8]] [[1 2][1 5][2 7]]) ;=> [[0 1][0 0][-1 1]]

coerce

(coerce param)(coerce matrix-or-implementation param)
Coerces param (which may be any array) into a format preferred by a specific matrix implementation.
If the matrix implementation is not specified, uses the current matrix implementation.
If param is already in a format deemed usable by the implementation, may return it unchanged.

coerce should never alter the shape of the array, but may convert element types where necessary
(e.g. turning real values into complex values when converting to a complex array type).

column-count

(column-count m)
Returns the number of columns in a matrix (array must be 2D or more)

column-matrix

(column-matrix data)(column-matrix implementation data)
Constructs a column matrix with the given data. The returned matrix is a 2D Nx1 column matrix.

The data must be either a valid existing vector or a sequence of scalar values.

column-matrix?

(column-matrix? m)
Returns true if a matrix is a column-matrix (i.e. is 2D and has has exactly one column)

columns

(columns m)
Gets the columns of a matrix, as a sequence of 1D vectors.

If the array has more than 2 dimensions, will return the columns from all slices in order.

compute-matrix

(compute-matrix shape f)(compute-matrix implementation shape f)
Creates a array with the specified shape, and each element specified by (f i j k...)
Where i, j, k... are the index positions of each element in the matrix

conforming?

(conforming? a)(conforming? a b)
Returns true if two arrays have a conforming shape. Two arrays are conforming if there
exists a common shape that both can broadcast to. This is a requirement for element-wise
operations to work correctly on two different-shaped arrays.

cos

(cos m)
Computes the cos function on all elements of an array, using double precision values. Returns a new array.

cos!

(cos! m)
Computes the cos function on all elements of an array, using double precision values. Mutates the array in-place.

cosh

(cosh m)
Computes the cosh function on all elements of an array, using double precision values. Returns a new array.

cosh!

(cosh! m)
Computes the cosh function on all elements of an array, using double precision values. Mutates the array in-place.

cross

(cross a b)
Computes the 3D cross-product of two numerical vectors.

Behavior on other types is undefined.

cross!

(cross! a b)(cross! dest a b)
Computes the cross-product of two numerical 3D vectors a and b, storing the result in the first vector.

Returns the (mutated) first vector.

current-implementation

(current-implementation)
Gets the currently active matrix implementation as a keyword, e.g. :vectorz

current-implementation-object

(current-implementation-object)
Gets a canonical object for the currently active matrix implementation. This object
can be used to pass as an implementation parameter, or to query implementation internals via core.matrix protocols.

dense

(dense data)(dense implementation data)
Coerces an array to a dense format if possible. Dense arrays are expected to
allocate contiguous storage space for all elements. Either row-major or column-major
storage may be alloacted, depending on the implementation.

'dense' should not be used with very large arrays, and may throw an OutOfMemoryError
 if the dense array is too large to fit in available memory.

Returns the array unchanged if such coercion is not possible, or if the array is already dense.

density

(density m)
Returns the density of the matrix, defined as the proportion of non-zero elements

det

(det a)
Calculates the determinant of a 2D square numerical matrix.

May throw an exception if the implementation does not support computation of determinants.

diagonal

(diagonal m)(diagonal m k)
Returns the specified diagonal of a 2D matrix as a vector.
If k>0, returns a diagonal above the main diagonal.
If k<0, returns a diagonal below the main diagonal.
Works on both square and rectangular matrices.
Returns empty vector if value of k is out of range (outside matrix)

diagonal-matrix

(diagonal-matrix diagonal-values)(diagonal-matrix implementation diagonal-values)
Constructs a 2D diagonal matrix with the given numerical values on the main diagonal.
 All off-diagonal elements will be zero, and diagonal-values may be a vector or any
Clojure sequence of values.

 Diagonal matrices constructed this way may use specialised storage formats, hence may not be fully mutable.
 Use (mutable (diagonal-matrix ...)) if you need to guarantee a mutable matrix.

diagonal?

(diagonal? m)
Returns true if the parameter is a diagonal matrix.

dimension-count

(dimension-count m dim)
Returns the size of the specified dimension in a matrix. Will throw an error if the matrix
does not have the specified dimension.

dimensionality

(dimensionality m)
Returns the dimensionality of an array. The dimensionality is equal to
the number of dimensions in the array's shape.

distance

(distance a b)
Calculates the euclidean distance between two numerical vectors.

This is equivalent to (norm 2 (sub a b)) but may be optimised by the underlying implementation.

div

(div a)(div a b)(div a b & more)
Performs element-wise matrix division for numerical arrays. 

Computes the reciprocal of each element when passed a single argument (similar to clojure.core//).

div!

(div! a)(div! a b)(div! a b & more)
Performs in-place element-wise matrix division for numerical arrays.

Computes the reciprocal of each element when passed a single argument (similar to clojure.core//).

dot

(dot a b)
Efficiently computes the scalar dot product (1Dx1D inner product) of two numerical vectors. Prefer this API 
function if you are performing a dot product on 1D vectors and want a scalar result.

If either argument is not a vector, will compute a higher dimensional inner product.

e*

(e*)(e* a)(e* a b)(e* a b & more)
An element-wise multiply operator equivalent to `emul`.

e=

(e= m1)(e= m1 m2)(e= m1 m2 & more)
Returns true if all corresponding array elements are equal (using the semantics of clojure.core/=).

WARNING: a java.lang.Long does not equal a java.lang.Double.
Use 'equals' or 'e==' instead if you want to test for numerical equality.

e==

(e== m1)(e== m1 m2)(e== m1 m2 & more)
Returns true if all corresponding array elements are numerically equal. 

Throws an error if any elements of the arrays being compared are not numerical values.

ecount

(ecount m)
Returns the total count of elements in an array, as an integer value. 

Equal to the product of the lengths of each dimension in the array's shape.

Result will usually be a Long, however callers should note that for very large sparse arrays 
the element count may be a BigInteger, i.e. equal to or larger than 2^63.

Returns 1 for a zero-dimensional array or scalar. 

eif

(eif m a b)
Element-wise if. Tranverses each element, x, of an array, m. If x > 0,
returns a (if a is a scalar) or the corresponding element from a (if a is an
array or matrix). If x <= 0, returns b (if b is a scalar) or the corresponding
element from array b (if b is an array or matrix).

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(eif (lt 1 3) 3 6) ;=> 3
(eif (lt 5 3) 3 6) ;=> 6
(eif (eq A B) 1 2) ;=> [[1 2] [2 1]]
(eif (eq A B) 1 D) ;=> [[1 1] [9 1]]
(eif (eq A B) C 2) ;=> [[2 2] [2 2]]
(eif [[1 0][0 1] [[2 3][4 5]] [[6 7][8 9]]) ;=> [[2 7][8 5]]
(eif (gt [[2 6][3 5]] 4) [[0 0][0 0]] [[1 1][1 1]] ;=> [[0 1][0 1]]

element-type

(element-type m)
Returns the class of elements that can be in the array. For example, a numerical array may return
the class java.lang.Double.

emap

(emap f m)(emap f m a)(emap f m a & more)
Element-wise map over all elements of one or more arrays.

f must return a result compatible with the element-type of the array m

Returns a new array of the same element-type and shape as the array m.

emap!

(emap! f m)(emap! f m a)(emap! f m a & more)
Element-wise map of a function f over all elements of one or more arrays.

f must return a result compatible with the element-type of the array m

Performs in-place modification of the first array argument.

emap-indexed

(emap-indexed f m)(emap-indexed f m a)(emap-indexed f m a & more)
Element-wise map-indexed over all elements of one or more arrays.

f must accept as first argument the index vector of the current element,
and return a result compatible with the element-type of the array m

Returns a new array of the same element-type and shape as the array m.

emap-indexed!

(emap-indexed! f m)(emap-indexed! f m a)(emap-indexed! f m a & more)
Element-wise map-indexed over all elements of one or more arrays.

f must accept as first argument the index vector of the current element,
and return a result compatible with the element-type of the array m

Performs in-place modification of the first array argument.

emax

(emax m)
Gets the maximum element value from a numerical array

emin

(emin m)
Gets the minimum element value from a numerical array

emul

(emul)(emul a)(emul a b)(emul a b & more)
Performs element-wise multiplication between arrays.

emul!

(emul! a)(emul! a b)(emul! a b & more)
Performs in-place element-wise multiplication of numerical arrays.

Returns the first argument after mutation.

ensure-mutable

(ensure-mutable m)
Checks if an array is mutable, and if not converts to a new mutable array. Guarantees
that the result will be mutable, but may not be the same type as the original array.

eq

(eq m a)
Element-wise equal comparison operation. Returns a binary array where 
elements equal to the argument are represented by 1 and elements not-equal to 
the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(eq 1 1) ;=> 1
(eq 5 1) ;=> 0
(eq [[1 5] [3 6]] 3) ;=> [[0 0] [1 0]]
(eq [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[0 0] [0 1]]

equals

(equals a)(equals a b)(equals a b epsilon)
Returns true if two arrays are numerically equal.

Will return false for arrays of different shapes.

May either return false or throw an error if the arrays are not numerical.

If epsilon is provided, performs an equality test
with the given maximum tolerance (default is 0.0, i.e. exact numerical equivalence)

ereduce

(ereduce f m)(ereduce f init m)
Element-wise reduce on all elements of an array.

eseq

(eseq m)
Returns all elements of an array as a sequence object in row-major order

esum

(esum m)
Calculates the sum of all the elements in a numerical array.

exp

(exp m)
Computes the exp function on all elements of an array, using double precision values. Returns a new array.

exp!

(exp! m)
Computes the exp function on all elements of an array, using double precision values. Mutates the array in-place.

fill

(fill m value)
Fills a matrix with a single scalar value. The scalar value must be compatible with the element-type
of the array. Returns a new array. 

Functionally similar to `assign!` except only intended for use with a scalar value.

fill!

(fill! m value)
Fills a matrix with a single scalar value. The scalar value must be compatible with the element-type
of the array.

Similar to assign!, but only supports scalar values (and may be more efficient).

floor

(floor m)
Computes the floor function on all elements of an array, using double precision values. Returns a new array.

floor!

(floor! m)
Computes the floor function on all elements of an array, using double precision values. Mutates the array in-place.

ge

(ge m a)(ge m a & more)
Element-wise greater-than-or-equal-to comparison operation. Returns a binary
array where elements greater-than or equal to the argument are represented by 1
and elements less-than to the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(ge 2 3) ;=> 0
(ge 3 3) ;=> 1
(ge [[1 5] [3 6]] 3) ;=> [[0 1] [1 1]]
(ge [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[0 1] [0 1]]

get-column

(get-column m y)
Gets a column of a matrix, as a 1D vector.

May return a mutable view if supported by the implementation.

get-row

(get-row m x)
Gets a row of a matrix, as a 1D vector.

May return a mutable view if supported by the implementation.

gt

(gt m a)(gt m a & more)
Element-wise greater-than comparison operation. Returns a binary array where 
elements greater-than the argument are represented by 1 and elements less-
than or equal to the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(gt 4 3) ;=> 1
(gt 3 3) ;=> 0
(gt [[1 5] [3 6]] 3) ;=> [[0 1] [0 1]]
(gt [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[0 1] [0 0]]

identity-matrix

(identity-matrix dims)(identity-matrix implementation dims)
Constructs a 2D identity matrix with the given number of rows.

Identity matrices constructed with this function may not be fully mutable because they may be
implemented with a specialised identity matrix type. Use (mutable (identity-matrix ...)) if you
need to guarantee a mutable matrix.

identity-matrix?

(identity-matrix? m)
Returns true if the parameter is an identity-matrix, i.e. a symmetric square matrix with element values
of 1 on the leading diagonal and 0 elsewhere.

immutable

(immutable data)(immutable data type)
Returns an immutable array containing the given array data.

May return the same array if it is already immutable.

If the implementation does not support immutable matrices, will return an immutable array
from another core.matrix implementation that supports either the same element type or a broader type.

index

(index data)(index implementation data)
Constructs a new 1-dimensional integer index from given data.

The data may be in one of the following forms:
- A valid existing index
- A 1D array of integer values
- A sequence of integer values

If implementation is not specified, uses the current matrix library as specified
in *matrix-implementation* to produce the index object.

If the implementation does not support its own native index types, will return a
valid index from a default implementation.

index-seq

(index-seq m)
Returns a sequence of all possible index vectors into a matrix, in row-major order

index-seq-for-shape

(index-seq-for-shape sh)
Returns a sequence of all possible index vectors for a given shape, in row-major order

index?

(index? m)
Returns true if the parameter is a valid array index type. An index is a seq-able 1D list
of integer values that can be used to index into arrays.

inner-product

(inner-product)(inner-product a)(inner-product a b)(inner-product a b & more)
Computes the inner product of numerical arrays.

For matrix/matrix and matrix/vector arguments, this is equivalent to matrix multiplication.

The inner product of two arrays with indexed dimensions {..i j} and {j k..} has dimensions {..i k..}. The inner-product of two vectors will be scalar.

inverse

(inverse m)
Calculates the inverse of a 2D numerical matrix. 

Returns nil if the matrix is singular. May throw an exception if the implementation does not support inverses.

join

(join & arrays)
Joins arrays together, along dimension 0. For 1D vectors, this behaves as simple concatenation. 

Other dimensions must be compatible. To join arrays along a different dimension, use 'join-along' instead.

join-along

(join-along dimension & arrays)
Joins arrays together, concatenating them along the specified dimension.

Other dimensions must be compatible.

label

(label m dim i)
Returns a label for the specified position along a given array dimension. Returns nil if the dimension is unlabelled.

labels

(labels m dim)
Return a vector of labels for a given array dimension. Return nil if the dimension is unlabelled.

le

(le m a)(le m a & more)
Element-wise less-than-or-equal-to comparison operation. Returns a binary
array where elements less-than or equal to the argument are represented by 1
and elements greater-than to the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(le 3 3) ;=> 1
(le 4 3) ;=> 0
(le [[1 5] [3 6]] 3) ;=> [[1 0] [1 0]]
(le [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[1 0] [1 1]]

length

(length m)
Calculates the euclidean length (magnitude) of a numerical vector

length-squared

(length-squared m)
Calculates the squared length (squared magnitude) of a numerical vector

log

(log m)
Computes the log function on all elements of an array, using double precision values. Returns a new array.

log!

(log! m)
Computes the log function on all elements of an array, using double precision values. Mutates the array in-place.

log10

(log10 m)
Computes the log10 function on all elements of an array, using double precision values. Returns a new array.

log10!

(log10! m)
Computes the log10 function on all elements of an array, using double precision values. Mutates the array in-place.

logistic

(logistic a)
Computes the sigmoid (logistic) function for every element of an array.

logistic!

(logistic! a)
Computes the sigmoid (logistic) function for every element of an array. Mutates the array.

lower-triangular?

(lower-triangular? m)
Returns true if the parameter is a lower triangular matrix.

lt

(lt m a)(lt m a & more)
Element-wise less-than comparison operation. Returns a binary array where 
elements less-than the argument are represented by 1 and elements greater-
than or equal to the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(lt 1 4) ;=> 1
(lt 3 3) ;=> 0
(lt [[1 5] [3 6]] 3) ;=> [[1 0] [0 0]]
(lt [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[1 0] [1 0]]

main-diagonal

(main-diagonal m)
Returns the main diagonal of a matrix or general array, as a vector.
The main diagonal of a general array is defined as those elements where the all the
indexes are equal, i.e. the index is of the form [i i ... i]
Works on both square and rectangular matrices.

matrix

(matrix data)(matrix implementation data)
Constructs a new 2-dimensional matrix from the given numerical data.

The data may be in one of the following forms:
- A valid existing numerical array
- Nested sequences of scalar values, e.g. Clojure vectors
- A sequence of slices, each of which must be valid matrix data

If implementation is not specified, uses the current matrix library as specified
in *matrix-implementation*

`matrix` works as a synonym for `array`

matrix?

(matrix? m)
Returns true if parameter is a valid matrix (i.e. an array with dimensionality == 2)

mget

(mget m)(mget m x)(mget m x y)(mget m x y & more)
Gets a scalar value from an array at the specified position. Supports any number of dimensions.

mmul

(mmul)(mmul a)(mmul a b)(mmul a b & more)
Performs matrix multiplication on matrices or vectors. Equivalent to
inner-product when applied to vectors.  Will treat a 1D vector roughly as a
1xN matrix (row vector) when it's the first argument, or as an Nx1 matrix
(column vector) when it's the second argument--except that the dimensionality
of the result will be different from what it would be with matrix arguments.

mset

(mset m v)(mset m x v)(mset m x y v)(mset m x y z & more)
Sets a scalar value in an array at the specified position. Supports any number of dimensions.

Returns a new matrix and leaves the original unchanged. 

WARNING: performance of this operation may be as high as O(N) where N is the number of elements in
the array. Consider using mutable arrays and `mset!` when setting large numbers of individual elements 
is required.

mset!

(mset! m v)(mset! m x v)(mset! m x y v)(mset! m x y z v)(mset! m x y z t & more)
Mutates a scalar value in an array at the specified position. Supports any number of dimensions.

Will throw an exception if the matrix is not mutable at the specified position. Note that it
is possible for some arrays to be mutable in places and immutable in others (e.g. sparse arrays)

Returns the modified matrix (it is guaranteed to return the same instance)

mul

(mul)(mul a)(mul a b)(mul a b & more)
Performs element-wise multiplication with scalars and numerical arrays.

Behaves like clojure.core/* for scalar values.

mul!

(mul! a)(mul! a b)(mul! a b & more)
Performs in-place element-wise multiplication of numerical arrays. 

Returns the first argument after mutation.

multiply-row

(multiply-row m i factor)
Multiply row i in a matrix by a constant factor

mutable

(mutable data)(mutable data type)
Constructs a fully mutable copy of the given array data.

If the implementation does not support mutable matrices, will return a mutable array
from another core.matrix implementation that supports either the same element type or a broader type.

mutable-matrix

deprecated

(mutable-matrix data)(mutable-matrix data type)
Constructs a mutable copy of the given matrix.

DEPRECATED: please use mutable instead

mutable?

(mutable? m)
Returns true if the matrix is mutable, i.e. supports setting of values.

It is possible for some matrix implementations to have constraints on mutability (e.g. mutable only in diagonal elements),
this method will still return true for such cases.

native

(native a)(native impl a)
Coerces an array into a native format array if possible. Native arrays may offer 
superior performance for some operations, depending on the implementation.
Returns nil if no appropriate native format exists.

native?

(native? a)
Returns true if the array is in a native format. 

Native formats are implementation defined, and may use non-Java resources (e.g. GPU memory).

ne

(ne m a)
Element-wise not-equal comparison operation. Returns a binary array where 
elements not-equal to the argument are represented by 1 and elements equal to 
the argument are 0.

Performs broadcasting of arguments if required to match the size of the largest array.

Examples:
(ne 1 1) ;=> 0
(ne 5 1) ;=> 1
(ne [[1 5] [3 6]] 3) ;=> [[1 1] [0 1]]
(ne [[1 5] [4 6]] [[2 3] [5 6]]) ;=> [[1 1] [1 0]]

negate

(negate m)
Calculates the negation of a numerical array. Generally equivalent to (scale m -1.0)

negate!

(negate! m)
Calculates the negation of a numerical array in place. Generally equivalent to (scale! m -1.0)

new-array

(new-array shape)(new-array implementation shape)
Creates a new array with the given shape.
New array will contain default values as defined by the implementation (usually null or zero).
If the implementation supports mutable matrices, then the new matrix will be fully mutable.

new-matrix

(new-matrix rows columns)(new-matrix implementation rows columns)
Constructs a new 2D array (matrix) with the given dimensions.
The new matrix will contain default values as defined by the implementation (usually null or zero).
If the implementation supports mutable matrices, then the new matrix will be fully mutable.

new-scalar-array

(new-scalar-array)(new-scalar-array implementation)
Returns a new mutable scalar array containing the scalar value zero.

new-sparse-array

(new-sparse-array shape)(new-sparse-array implementation shape)
Creates a new sparse array with the given shape.
New array will contain default values as defined by the implementation (usually zero).
If the implementation supports mutable sparse matrices, then the new matrix should be fully mutable.

new-vector

(new-vector length)(new-vector implementation length)
Constructs a new vector with the given length.
New matrix will contain default values as defined by the implementation (usually null or zero).
If the implementation supports mutable vectors, then the new vector will be fully mutable.

non-zero-count

(non-zero-count m)
Counts the number of non-zero values in a numerical array.
May perform a full array scan, but will often be quicker for specialised
sparse arrays - sometimes as fast as O(1)

non-zero-indices

(non-zero-indices m)
Gets the non-zero indices of an array.
- For a 1D vector, returns an ordered index list.
- For a higher dimensional array, returns the non-zero-indices for each slice in row-major order.

normalise

(normalise v)
Normalises a numerical vector (scales to unit length).
Returns a new normalised vector.

normalise!

(normalise! v)
Normalises a numerical vector in-place (scales to unit length).

Returns the modified vector.

numerical?

(numerical? m)
Returns true if the matrix is a valid numerical matrix (i.e. supports numerical core.matrix operations).

order

(order m indices)(order m dimension indices)
Reorders slices of an array along a specified dimension. Re-orders along major dimension
if no dimension is specified.

Indicies can be any seqable object containing the indices along the specified dimension to select.
An index can be selected multiple times (which created repreated slices), or not at all (which excludes
the slice from the result).

orthogonal?

(orthogonal? m eps)(orthogonal? m)
Returns true if the parameter is an orthogonal matrix.

outer-product

(outer-product)(outer-product a)(outer-product a b)(outer-product a b & more)
Computes the outer product of numerical arrays.

The outer product of two arrays with indexed dimensions {i..j} and {j..k} has dimensions {i..j j..k}, i.e. the dimensioanlity will be the
sum of the dimensionalities of the two arguments.

pack

(pack a)
Packs array data in the most efficient format as defined by the implementation. May return the
same array if no additional packing is required.

permutation-matrix

(permutation-matrix permutation)(permutation-matrix implementation permutation)
Constructs a permutation matrix for a given permutation vector. The permutation vector should
contain a distinct set of integers 0...n-1, representing the re-ordering performed by
the permutation matrix.

pm

(pm m)(pm m opts)
Pretty-prints a matrix.

opts is a map of optional parameters which may include:

   :formatter - a function to format each array element

pow

(pow m)(pow m exponent)(pow m exponent & more)
Raises every element of a numerical matrix by the given exponent.

Note that behaviour for large exponents may depend on the underlying implementation:
for example double-based matrices may overflow to Double/POSITIVE_INFINITY.

pow!

(pow! m a)
Mutable exponent function, see 'pow'

reshape

(reshape m shape)
Changes the shape of a matrix to the specified new shape. shape can be any sequence of dimension sizes.

Preserves the row-major order of matrix elements. Truncates the sequence of elements if the shape is smaller
than the original shape.

Pads with default values (dependent on implementation - normally zero) if the shape is larger.

rotate

(rotate m dimension shift-amount)(rotate m shifts)
Rotates an array along specified dimensions. 

Elements rotated off will re-appear at the other side. The shape of the array will not be modified.

round

(round m)
Computes the round function on all elements of an array, using double precision values. Returns a new array.

round!

(round! m)
Computes the round function on all elements of an array, using double precision values. Mutates the array in-place.

row-count

(row-count m)
Returns the number of rows in a matrix or vector (array must be 1D or more).

row-matrix

(row-matrix data)(row-matrix implementation data)
Constructs a row matrix with the given data. The returned matrix is a 2D 1xN row matrix.

The data must be either a valid existing vector or a sequence of scalar values.

row-matrix?

(row-matrix? m)
Returns true if a matrix is a row-matrix (i.e. is 2D and has exactly one row)

rows

(rows m)
Gets the rows of a matrix, as a sequence of 1D vectors.

If the array has more than 2 dimensions, will return the rows from all slices in order.

same-shape?

(same-shape?)(same-shape? m)(same-shape? m n)(same-shape? m n & more)
Returns true if the arrays have the same shape, false otherwise

scalar

(scalar m)
Coerces m to a scalar value. Result is guaranteed not to be an array.
Will throw an exception if m is not zero-dimensional.

scalar-array

(scalar-array value)(scalar-array implementation value)
Creates a new zero-dimensional array containing the specified scalar value.

scalar?

(scalar? v)
Returns true if the parameter is a scalar value (i.e. acceptable as matrix element value).
A 0-d array containing a scalar is *not* itself a scalar value.

scale

(scale m factor)(scale m factor & more-factors)
Scales a array by one or more scalar factors. The default implementation supports numerical arrays and
numbers as scalar values, however matrix implementations may extend this to support other scalar types.

Returns a new scaled matrix.

scale!

(scale! m factor)(scale! m factor & more-factors)
Scales a numerical array by one or more scalar factors (in place). The default implementation supports 
numerical arrays and numbers as scalar values, however matrix implementations may extend this to 
support other scalar types.

Returns the matrix after it has been mutated.

scale-add!

(scale-add! m1 a m2 b)(scale-add! m1 a m2 b constant)
Scales array m1 by factor a, then adds an array m2 scaled by factor b. May optionally add a constant.
Broadly equivalent to (add! (mul! m1 a) (mul m2 b) constant)

Returns the mutated array `m1`. The array `m2` will not be changed.

select

(select a & args)
Returns an array containing all elements in a which are at the positions
of the Cartesian product of args. An argument can be:
 - a number - slices at this dimension (eliminates the dimension),
 - a keyword which selects specific slices (:first :last)
 - a 1-dimensional array of numbers which selects the slices at these indices
 - a keyword which selects a range of slices (:all :butlast :rest)

The number of args must match the dimensionality of a.

Examples:
(select [[1 2][3 4]] 0 0) ;=> 1
(select [[1 2][3 4]] 0 :all) ;=> [1 2]
(select [[1 2][3 4]] [0 1] [0]) ;=> [[1] [3]]
(select [[1 2][3 4]] :all 0) ;=> [1 3]

select-indices

(select-indices a indices)
Returns a one-dimensional array of the elements which are at the specified
indices. An index is a one-dimensional array which element-count matches the
dimensionality of a. Examples:
(select-indices [[1 2] [3 4]] [[0 0][1 1]]) ;=> [1 4]

select-view

(select-view a & args)
Like `select`, but guarantees a view over the original data.

set-column

(set-column m i column)
Sets a column in a matrix using a specified vector.

set-column!

(set-column! m i column)
Sets a column in a matrix using a specified vector.

set-current-implementation

(set-current-implementation m)
Sets the currently active core.matrix implementation. 

Parameter may be 
 - A known keyword for the implementation e.g. :vectorz
 - An existing instance from the implementation

This is used primarily for functions that construct new matrices, i.e. it determines the
implementation used for expressions like: (matrix [[1 2] [3 4]])

set-indices

(set-indices a indices values)
like select-indices but sets the elements at the specified indices to values.
Leaves the original array (a) unchanged and returns a modified array

set-indices!

(set-indices! a indices values)
like set-indices but destructively modifies array in place

set-row

(set-row m i row)
Sets a row in a matrix using a specified vector.

set-row!

(set-row! m i row)
Sets a row in a matrix in-place using a specified vector.

set-selection

(set-selection a & args)
Like select but sets the elements in the selection to the value of the final argument. 
Leaves a unchanged and returns the modified array

set-selection!

(set-selection! a & args)
Like set-selection but mutates the array in place. Will throw an error if array is immutable.

shape

(shape m)
Returns the shape of an array, i.e. the dimension sizes for all dimensions.

The result will be a vector containing only integer index values, with a count
equal to the dimensionality of the array.

Returns nil the if object is not an array (i.e. is a scalar value)

shift

(shift m dimension shift-amount)(shift m shifts)
Shifts all elements of an array along specified dimensions, maintaining the shape of the array.
New spaces shifted into the array are filled with the appropriate zero value.

signum

(signum m)
Computes the signum function on all elements of an array, using double precision values. Returns a new array.

signum!

(signum! m)
Computes the signum function on all elements of an array, using double precision values. Mutates the array in-place.

sin

(sin m)
Computes the sin function on all elements of an array, using double precision values. Returns a new array.

sin!

(sin! m)
Computes the sin function on all elements of an array, using double precision values. Mutates the array in-place.

sinh

(sinh m)
Computes the sinh function on all elements of an array, using double precision values. Returns a new array.

sinh!

(sinh! m)
Computes the sinh function on all elements of an array, using double precision values. Mutates the array in-place.

slice

(slice m index)(slice m dimension index)
Gets a slice of an array along a specific dimension.
The returned array will have one less dimension.

Slicing a 1D vector will return a scalar.

Slicing on the first dimension (dimension 0) is likely to perform better
for many array implementations, and is therefore the default if no
dimension is specified.

slice-count

(slice-count m)
Returns the number of slices in an array (array must be 1D or more). The array is sliced
in row-major order, i.e. this is the dimension count of the first dimension.

slice-view

(slice-view m i)(slice-view m dimension i)
Gets a view of an array slice. Guaranteed to return a mutable view if the array is mutable.

slice-views

(slice-views m)(slice-views m dimension)
Gets a sequence of views of the slices of an array. If dimension is supplied, slices along a given dimension,
otherwise slices along the first dimension. If the matrix implementation supports mutable views, these views
can be used to mutate portions of the original array.

The key difference between 'slices' and 'slice-views' is that 'slice-views' must always return views. In order 
to ensure this behaviour on mutable 1-dimensioanal arrays, it must return a sequence of 0-dimensioanal arrays.

slices

(slices m)(slices m dimension)
Gets a sequence of slices of an array. If dimension is supplied, slices along a given dimension,
otherwise slices along the first dimension.

Returns a sequence of scalar values if the array is 1-dimensional.

sparse

(sparse data)(sparse implementation data)
Coerces an array to a sparse format if possible. Sparse arrays are expected to
minimise space usage for zero elements.

Returns the array unchanged if such coercion is not possible, or if the array is already sparse.

sparse-array

(sparse-array data)(sparse-array implementation data)
Creates a sparse array with the given data, using a specified implementation
or the current implementation if not specified. 

Throws an exception if creation of a sparse array is not possible

sparse-matrix

(sparse-matrix data)(sparse-matrix implementation data)
Creates a sparse matrix with the given data, using a specified implementation
or the current implementation if not specified. Sparse matrices are required to store
a M*N matrix with E non-zero elements in approx O(M+N+E) space or less.

Throws an exception if creation of a sparse matrix is not possible.

`sparse-matrix` wqorks as a synonym for `sparse-array`.

sparse?

(sparse? m)
Returns true if an array is sparse, i.e. the implementation supports storage of the entire
array in less memory than would normally be implied by the number of elements.

Sparse matrices may have memory requirements that scale with the number of non-zero elements
rather than the total number of elements, for example.

sqrt

(sqrt m)
Computes the sqrt function on all elements of an array, using double precision values. Returns a new array.

sqrt!

(sqrt! m)
Computes the sqrt function on all elements of an array, using double precision values. Mutates the array in-place.

square

(square m)
Squares every element of a numerical array. Returns a new array.

square?

(square? m)
Returns true if matrix is square (i.e. a 2D array with same number of rows and columns)

sub

(sub a)(sub a b)(sub a b & more)
Performs element-wise subtraction on one or more numerical arrays.

For a single argument, returns the negation.

Returns a new array.

sub!

(sub! a)(sub! a b)(sub! a b & more)
Performs element-wise mutable subtraction on one or more numerical arrays.

NOTE: For a single argument, returns the argument unchanged: use negate! instead if you wish to negate a mutable 
array in place. This is intentional, so that you can do (apply sub! m list-of-arrays) and get the expected 
result if the list of arrays is empty.

Returns the first array, after it has been mutated.

submatrix

(submatrix m index-ranges)(submatrix m dimension index-range)(submatrix m row-start row-length col-start col-length)
Gets a view of a submatrix, for a set of index ranges.
Index ranges should be [start, length] pairs.
Index range pairs can be nil (gets the whole range) 

subvector

(subvector m start length)
Gets a view of part of a vector. The view maintains a reference to the original,
so can be used to modify the original vector if it is mutable.

supports-dimensionality?

(supports-dimensionality? m dimension-count)
Returns true if the implementation for a given matrix supports a specific dimensionality, i.e.
can natively create and manipulate matrices with the given number of dimensions

supports-shape?

(supports-shape? m shape)
Returns true if the implementation supports creation of matrices with a specific shape.

swap-rows

(swap-rows m i j)
Swap row i with row j in a matrix, returning a new matrix

symmetric?

(symmetric? m)
Returns true if the parameter is a symmetric matrix, i.e. Aij = Aji for all i,j.

tan

(tan m)
Computes the tan function on all elements of an array, using double precision values. Returns a new array.

tan!

(tan! m)
Computes the tan function on all elements of an array, using double precision values. Mutates the array in-place.

tanh

(tanh m)
Computes the tanh function on all elements of an array, using double precision values. Returns a new array.

tanh!

(tanh! m)
Computes the tanh function on all elements of an array, using double precision values. Mutates the array in-place.

to-degrees

(to-degrees m)
Computes the to-degrees function on all elements of an array, using double precision values. Returns a new array.

to-degrees!

(to-degrees! m)
Computes the to-degrees function on all elements of an array, using double precision values. Mutates the array in-place.

to-double-array

(to-double-array m)(to-double-array m want-copy?)
Returns a Java double[] array containing the values of a numerical array m in row-major order. Will
throw an error if any of the array elements cannot be converted to doubles.

If want-copy? is true, will guarantee a new double array (defensive copy).
If want-copy? is false, will return the internal array used by m, or nil if not supported
by the implementation.
If want-copy? is not specified, will return either a copy or the internal array

to-nested-vectors

(to-nested-vectors m)
Converts an array to an idiomatic, immutable nested Clojure vector format. The bottom level of the
nested vectors will contain the element values. Higher levels will all implement IPersistentVector.

The depth of nesting will be equal to the dimensionality of the array.

to-object-array

(to-object-array m)(to-object-array m want-copy?)
Returns a Java Object[] array containing the values of an array m in row-major order.

If want-copy? is true, will guarantee a new Object array (defensive copy).
If want-copy? is false, will return the internal array used by m, or nil if not supported
by the implementation.
If want-copy? is not specified, will return either a copy or the internal array

to-radians

(to-radians m)
Computes the to-radians function on all elements of an array, using double precision values. Returns a new array.

to-radians!

(to-radians! m)
Computes the to-radians function on all elements of an array, using double precision values. Mutates the array in-place.

to-vector

(to-vector m)
Creates a new array representing the elements of array m as a single flattened vector.

This operation guarantees a new copy of the data.

trace

(trace a)
Calculates the trace of a 2D numerical matrix (sum of elements on main diagonal).

The matrix need not be square.

transform

(transform t v)
Transforms a given vector with a transformation, returning a new vector.

The transformation may be a 2D matrix, but other types of transformation may also be supported
e.g. affine transformations, unary operators.

transform!

(transform! t v)
Transforms a given vector in place. This is a mutable equivalent to `transform`.

Returns the transformed vector.

The transformation must map an n-dimensional vector to another n-dimensional vector, i.e.
if it is a 2D matrix then it must have shape [n x n].

transpose

(transpose m)(transpose m ordering)
Transposes a matrix, returning a new matrix. For 2D matrices, rows and columns are swapped.
More generally, the dimension indices are reversed for any shape of array. Note that 1D vectors
and scalars will be returned unchanged.

If ordering is provided, will re-order dimensions according to the provided order.

transpose!

(transpose! m)
Transposes a square 2D matrix in-place. 

Will throw an exception if not possible (e.g. if the matrix is not square or not mutable).

upper-triangular?

(upper-triangular? m)
Returns true if the parameter is a upper triangular matrix.

vec?

(vec? m)
Returns true if the parameter is a vector (1-dimensional array)

with-implementation

macro

(with-implementation impl & body)
Runs a set of expressions using a specified matrix implementation.

Example:
  (with-implementation :vectorz
    (new-matrix 10 10))

zero-array

(zero-array shape)(zero-array implementation shape)
Creates a new zero-filled numerical array with the given shape.

zero-count

(zero-count m)
Returns the number of zeros in an array.

Result will usually be a Long, however callers should note that for very large sparse arrays 
the zero count may be a BigInteger, i.e. equal to or larger than 2^63.

zero-dimensional?

(zero-dimensional? m)
Returns true if the parameter has zero dimensions. i.e. it is a 0-d array or a scalar value.

Behaviour is the same as `scalar?`, except that true is returned for 0-dimensional arrays.

zero-matrix

(zero-matrix rows columns)(zero-matrix implementation rows columns)
Constructs a new zero-filled numerical matrix with the given dimensions.

May produce a lightweight immutable zero matrix if supported by the implementation.

zero-matrix?

(zero-matrix? m)
Returns true if all the elements of the parameter are zero.

zero-vector

(zero-vector length)(zero-vector implementation length)
Constructs a new zero-filled numerical vector with the given length.

Implementations are encouraged to return immutable vectors or sparse vectors
for efficency whre available.