# ZGEMM

In this article, we have covered ZGEMM (Double Precision Complex General Matrix Multiplication) which is a standard library function for Matrix Multiplication and a variant of GEMM.

1. Introduction to ZGEMM
2. Use of ZGEMM
3. Difference between ZGEMM and other GEMM functions

## Introduction to ZGEMM

ZGEMM stands for "Double Precision Complex General Matrix Multiplication".

It is a standard gemm routine in BLAS and BLIS libraries like OpenBLAS and is used to do Matrix Multiplication. It performs the standard GEMM operation that is Matrix Matrix multiplication with the matrices being of datatype Complex Double 64 bits. Complex number means it has both real and imaginary part.

The API of CGEMM is as follows:

``````status dgemm(
char transa,
char transb,
dim_t M,
dim_t N,
dim_t K,
Complex_64 alpha,
const Complex_64* A,
dim_t lda,
const Complex_64* B,
dim_t ldb,
Complex_64 beta,
Complex_64* C,
dim_t ldc
)
``````

Note:

• The 3 matrices A, B and C are in Complex Double datatype (64 bits).

CGEMM operation is defined as follows:

``````C = alpha * op(A) * op(B) + beta * C
``````

where

• op(X) = X or XT depending on transa and related values. X is a matrix.
• alpha and beta are scalars
• A, B, and C are matrices
• op(A) is an MxK matrix
• op(B) is an KxN matrix
• C is an MxN matrix (output)

The parameters are as follows:

• transa: Transposition flag for matrix A. If it is set to 0, op(A) = A and if it is set to 1, op(A) = AT.
• transb: Transposition flag for matrix B. If it is set to 0, op(B) = B and if it is set to 1, op(B) = BT.
• M, N, K: dimensions
• alpha: parameter that is used to scale the product of matrices A and B.
• A: Input matrix of size MxK
• lda: Leading dimension for matrix A
• B: Input matrix of size KxN
• ldb: Leading dimension for matrix B
• beta: Beta parameter that is used to scale matrix C
• C: Output matrix
• ldc: Leading dimension for matrix C

The different combinations in CGEMM will be:

• C = alpha * A * B + beta * C
• C = alpha * AT * B + beta * C
• C = alpha * A * BT + beta * C
• C = alpha * AT * BT + beta * C
• C = alpha * A * BH + beta * C
• C = alpha * AH * B + beta * C
• C = alpha * AH * BT + beta * C
• C = alpha * AT * BH + beta * C
• C = alpha * AH * BH + beta * C

where:

• T is transpose
• H is conjugate transpose

The GEMM operations like ZGEMM from any library are highly optimized for specific applications and platforms.

## Use of ZGEMM

ZGEMM is a relatively less used GEMM function compared to SGEMM and DGEMM.

ZGEMM is used in Scientific mathematical applications that require Complex numbers.

ZGEMM functions are not used in Machine Learning models as it deals with real numbers. Note that ZGEMM can be used with only the real component set in which case, it will act as DGEMM.

ZGEMM calls are available in different libraries like:

• BLAS like OpenBLAS
• BLIS like FLAME BLIS
• FBGEMM
• OneDNN

and others.

## Difference between zgemm and other gemm functions

• ZGEMM vs SGEMM + DGEMM"

ZGEMM deals with complex numbers where there is a real and imaginary part.

On the other hand, SGEMM and DGEMM deal with real numbers only. Hence, ZGEMM can be viewed as a generalization of DGEMM as the precision (Double 64 bits) is same.

• ZGEMM vs GEMM

The main difference is that GEMM is the generalized group of functions. ZGEMM is a specific implementation of GEMM.

The general GEMM functions have different variations with different datatypes for the 3 matrices involved like:

• gemm_u8s8s32: GEMM with A of datatype unsigned INT8, B of datatype signed INT8 and output as signed INT32.
• gemm_s8s8s32: GEMM with A of datatype signed INT8, B of datatype signed INT8 and output as signed INT32.

and much more.

The datatype is ZGEMM is fixed that is Double 64 bits Complex numbers.

• CGEMM vd ZGEMM

CGEMM deal with Single 32 bits while ZGEMM deal with Double 64 bits. Both deal with Complex numbers.

With this article at OpenGenus, you must have the complete idea of ZGEMM.