# BLAS Code Examples

## Example. Using BLAS Level 1 Function

The following example illustrates a call to the BLAS Level 1 function sdot. This function performs a vector-vector operation of computing a scalar product of two single-precision real vectors x and y.

Parameters |
n | Specifies the number of elements in vectors x and y.
incx | Specifies the increment for the elements of x.
incy | Specifies the increment for the elements of y.

`program dot_main`
`real x(10), y(10), sdot, res`
`integer n, incx, incy, i`
`external sdot`
`n = 5`
`incx = 2`
`incy = 1`
`do i = 1, 10`
`x(i) = 2.0e0`
`y(i) = 1.0e0`
`end do`
`res = sdot (n, x, incx, y, incy)`
`print*, `SDOT = `, res`
`end`

As a result of this program execution, the following line is printed:

SDOT = 10.000

## Example. Using BLAS Level 1 Routine

The following example illustrates a call to the BLAS Level 1 routine scopy. This routine performs a vector-vector operation of copying a single-precision real vector x to a vector y.

Parameters |
n | Specifies the number of elements in vectors x and y.
incx | Specifies the increment for the elements of x.
incy | Specifies the increment for the elements of y.

``
`program copy_main`
`real x(10), y(10)`
`integer n, incx, incy, i`
`n = 3`
`incx = 3`
`incy = 1`
`do i = 1, 10`
`x(i) = i`
`end do`
`call scopy (n, x, incx, y, incy)`
`print*, `Y = `, (y(i), i = 1, n)`
`end`

As a result of this program execution, the following line is printed:

Y = 1.00000 4.00000 7.00000

## Example. Using BLAS Level 2 Routine

The following example illustrates a call to the BLAS Level 2 routine sger. This routine performs a matrix-vector operation
`a := alpha*x*y' + a.`

Parameters |
alpha | Specifies a scalar alpha.
x | m-element vector.
y | n-element vector.
a | m-by-n matrix.

`program ger_main`
`real a(5,3), x(10), y(10), alpha`
`integer m, n, incx, incy, i, j, lda`
`m = 2`
`n = 3`
`lda = 5`
`incx = 2`
`incy = 1`
`alpha = 0.5`
`do i = 1, 10`
`x(i) = 1.0`
`y(i) = 1.0`
`end do`
`do i = 1, m`
`do j = 1, n`
`a(i,j) = j`
`end do`
`end do`
`call sger (m, n, alpha, x, incx, y, incy, a, lda)`
`print*, `Matrix A: ``
`do i = 1, m`
`print*, (a(i,j), j = 1, n)`
`end do`
`end`

As a result of this program execution, matrix a is printed as follows:

Matrix A:

1.50000 2.50000 3.50000

1.50000 2.50000 3.50000

## Example. Using BLAS Level 3 Routine

The following example illustrates a call to the BLAS Level 3 routine ssymm. This routine performs a matrix-matrix operation
`c := alpha*a*b' + beta*c.`

Parameters |
alpha | Specifies a scalar alpha.
beta | Specifies a scalar beta.
a | Symmetric matrix
b | m-by-n matrix
c | m-by-n matrix

`program symm_main`
`real a(3,3), b(3,2), c(3,3), alpha, beta`
`integer m, n, lda, ldb, ldc, i, j`
`character uplo, side`
`uplo = 'u'`
`side = 'l'`
`m = 3`
`n = 2`
`lda = 3`
`ldb = 3`
`ldc = 3`
`alpha = 0.5`
`beta = 2.0`
`do i = 1, m`
`do j = 1, m`
`a(i,j) = 1.0`
`end do`
`end do`
`do i = 1, m`
`do j = 1, n`
`c(i,j) = 1.0`
`b(i,j) = 2.0`
`end do`
`end do`
`call ssymm (side, uplo, m, n, alpha,`
`a, lda, b, ldb, beta, c, ldc)`
`print*, `Matrix C: ``
`do i = 1, m`
`print*, (c(i,j), j = 1, n)`
`end do`
`end`

As a result of this program execution, matrix c is printed as follows:

Matrix C:

5.00000 5.00000

5.00000 5.00000

5.00000 5.00000

The following example illustrates a call from a C program to the complex BLAS Level 1 function zdotc(). This function computes the dot product of two double-precision complex vectors.

## Example. Calling a Complex BLAS Level 1 Function from C

In this example, the complex dot product is returned in the structure c.

`#define N 5`
`void main()`
`{`
``
`int n, inca = 1, incb = 1, i;`
`typedef struct{ double re; double im; } complex16;`
`complex16 a[N], b[N], c;`
`void zdotc();`
`n = N;`
`for( i = 0; i < n; i++ ){`
`a[i].re = (double)i; a[i].im = (double)i * 2.0;`
`b[i].re = (double)(n - i); b[i].im = (double)i * 2.0;`
`}`
`zdotc( &c, &n, a, &inca, b, &incb );`
`printf( "The complex dot product is: ( %6.2f, %6.2f )\n", c.re, c.im );`
`}`

### Note

Instead of calling BLAS directly from C programs, you might wish to use the CBLAS interface; this is the supported way of calling BLAS from C. For more information about CBLAS, see Appendix “CBLAS Interface to the BLAS”, which presents CBLAS, the C interface to the Basic Linear Algebra Subprograms (BLAS) implemented in Intel® MKL.

Parent topic: Code Examples