A high-performance general-purpose compute library

- Main Page
- Tutorials
- Release Notes
- Examples

Functions
solve
ArrayFire Functions | ArrayFire Functions by Category » Linear Algebra » Linear solve and least squares

## Functions
AFAPI array | solve (const array &a, const array &b, const matProp options=AF_MAT_NONE)
| C++ Interface to solve a system of equations.
AFAPI af_err | af_solve (af_array *x, const af_array a, const af_array b, const af_mat_prop options)
| C Interface to solve a system of equations.

## Detailed Description

Solve a system of equations.

This function takes a co-efficient matrix \(A\) and an output matrix \(B\) as inputs to solve the following equation for \(X\), \(A * X = B\).

This operation can be done in ArrayFire using the following code snippet.

af::array X1 = af::solve(A, B0);

The results can be verified by reconstructing the output matrix using af::matmul in the following manner,

af::array B1 = af::matmul(A, X1);

The sample output can be seen below.

A [3 3 1 1]
0.1000 3.1000 6.1000
1.1000 4.1000 7.0000
2.0000 5.0000 8.0000
B0 [3 1 1 1]
21.9000
30.7000
39.0000
X1 [3 1 1 1]
4.0000
3.0000
2.0000
B1 [3 1 1 1]
21.9000
30.7000
39.0000

If the coefficient matrix is known to be a triangular matrix, AF_MAT_LOWER or AF_MAT_UPPER can be passed to make solve faster.

The sample code snippets for solving a lower triangular matrix can be seen below.

af::array X = af::solve(AT, B0, AF_MAT_LOWER);

Similarily, the code snippet for solving an upper triangular matrix can be seen below.

af::array X = af::solve(AT, B0, AF_MAT_UPPER);

See also: af::solveLU

## Function Documentation

## ◆ af_solve()

AFAPI af_err af_solve | ( | af_array * | x,
|  | const af_array | a,
|  | const af_array | b,
|  | const af_mat_prop | options )

C Interface to solve a system of equations.

The `options` parameter must be one of AF_MAT_NONE, AF_MAT_LOWER or AF_MAT_UPPER.

This function is not supported in GFOR.

Parameters

  [out] | x | matrix of unknown variables
  [in] | a | coefficient matrix
  [in] | b | measured values
  [in] | options | determines various properties of matrix `a`

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

## ◆ solve()

AFAPI array solve | ( | const array & | a,
|  | const array & | b,
|  | const matProp | options = AF_MAT_NONE )

C++ Interface to solve a system of equations.

The `options` parameter must be one of AF_MAT_NONE, AF_MAT_LOWER or AF_MAT_UPPER.

This function is not supported in GFOR.

Parameters

  [in] | a | coefficient matrix
  [in] | b | measured values
  [in] | options | determines various properties of matrix `a`

Returns
  `x`, the matrix of unknown variables