A high-performance general-purpose compute library

- Main Page
- Tutorials
- Release Notes
- Examples

Functions
diag
ArrayFire Functions | ArrayFire Functions by Category » Functions to create and modify Arrays » Functions to create arrays.

## Functions
AFAPI array | diag (const array &in, const int num=0, const bool extract=true)
| C++ Interface to extract the diagonal from an array.
AFAPI af_err | af_diag_create (af_array *out, const af_array in, const int num)
| C Interface to create a diagonal matrix from an extracted diagonal array.
AFAPI af_err | af_diag_extract (af_array *out, const af_array in, const int num)
| C Interface to extract the diagonal from an array.

## Detailed Description

Extract the diagonal from an array.

If `extract` is true, an array is extracted containing diagonal of the matrix, while a false condition returns a diagonal matrix.

// Extraction
array a = randu(dim4(4, 3));
// a [4 3 1 1]
// 0.0000 0.5328 0.6793
// 0.1315 0.2190 0.9347
// 0.7556 0.0470 0.3835
// 0.4587 0.6789 0.5194
array b = diag(a);
// b [3 1 1 1]
// 0.0000
// 0.2190
// 0.3835
array c = diag(a, 1);
// c [2 1 1 1]
// 0.5328
// 0.9347
// Creation
array a = randu(4);
// a [4 1 1 1]
// 0.0000
// 0.1315
// 0.7556
// 0.4587
array b = diag(a, 0, false);
// b [4 4 1 1]
// 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.1315 0.0000 0.0000
// 0.0000 0.0000 0.7556 0.0000
// 0.0000 0.0000 0.0000 0.4587
array b = diag(a, -1, false);
// c [5 5 1 1]
// 0.0000 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.1315 0.0000 0.0000 0.0000
// 0.0000 0.0000 0.7556 0.0000 0.0000
// 0.0000 0.0000 0.0000 0.4587 0.0000

## Function Documentation

## ◆ af_diag_create()

AFAPI af_err af_diag_create | ( | af_array * | out,
|  | const af_array | in,
|  | const int | num )

C Interface to create a diagonal matrix from an extracted diagonal array.

See also, af_diag_extract.

Parameters

  [out] | out | diagonal matrix
  [in] | in | diagonal array
  [in] | num | diagonal index

Returns
  AF_SUCCESS, if function returns successfully, else an af_err code is given

## ◆ af_diag_extract()

AFAPI af_err af_diag_extract | ( | af_array * | out,
|  | const af_array | in,
|  | const int | num )

C Interface to extract the diagonal from an array.

See also, af_diag_create.

Parameters

  [out] | out | `num`-th diagonal array
  [in] | in | input array
  [in] | num | diagonal index

Returns
  AF_SUCCESS, if function returns successfully, else an af_err code is given

## ◆ diag()

AFAPI array diag | ( | const array & | in,
|  | const int | num = 0,
|  | const bool | extract = true )

C++ Interface to extract the diagonal from an array.

Parameters

  [in] | in | input array
  [in] | num | diagonal index
  [in] | extract | if true, returns an array containing diagonal of the matrix; if false, returns a diagonal matrix

Returns
  diagonal array (or matrix)