381 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			381 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | 
						|
 | 
						|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | 
						|
be copied by any method or incorporated into another program without
 | 
						|
the express written consent of Aerospace C.Power. This Information or any portion
 | 
						|
thereof remains the property of Aerospace C.Power. The Information contained herein
 | 
						|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | 
						|
liability for its use in any way and conveys no license or title under
 | 
						|
any patent or copyright and makes no representation or warranty that this
 | 
						|
Information is free from patent or copyright infringement.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
 | 
						|
#ifndef IOT_MATRIX_API_H
 | 
						|
#define IOT_MATRIX_API_H
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
extern "C" {
 | 
						|
#endif
 | 
						|
 | 
						|
/** \defgroup MATRIX_APIs MATRIX APIs
 | 
						|
 *  @brief MATRIX APIs
 | 
						|
 *
 | 
						|
 *
 | 
						|
 */
 | 
						|
 | 
						|
/** @addtogroup MATRIX_APIs
 | 
						|
 * @{
 | 
						|
 *
 | 
						|
 */
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Usage:
 | 
						|
 | 
						|
    // test case : 4x4 matrix, get matrix inverse , and count times is 14
 | 
						|
 | 
						|
    // count times
 | 
						|
    uint32_t count = 14;
 | 
						|
    // prepare source matrix data, data length is (4*4*2)*14 word
 | 
						|
    float *src = ...;
 | 
						|
 | 
						|
    // prepare destination matrix data, data length is (4*4*2)*14 word
 | 
						|
    float *dst = ...;
 | 
						|
 | 
						|
    iot_math_mtx_inverse(dst, src, CHOL_SIZE_4X4, count);
 | 
						|
 | 
						|
    // test case 4x4 matrix A, B, get the matrix of AxB, and count times is 14
 | 
						|
 | 
						|
    // uint32_t count = 14;
 | 
						|
    // prepare source matrix data, data length is (4*4*2)*14 word
 | 
						|
    float *mtx_a = ...;
 | 
						|
    float *mtx_b = ...;
 | 
						|
 | 
						|
    // prepare destination matrix data, data length is (4*4*2)*14 word
 | 
						|
    float *dst = ...;
 | 
						|
 | 
						|
    // input matrix
 | 
						|
    iot_math_cmm_cfg(mtx_a, 4, 4, 0, mtx_b, 4, 4, 0);
 | 
						|
 | 
						|
    // multiplication: AxB
 | 
						|
    iot_math_cmm_ab(dst, CMM_CONV_A, CMM_CONV_B, num);
 | 
						|
 | 
						|
    // multiplication: AxBxB.'
 | 
						|
    iot_math_cmm_abc(dst,
 | 
						|
        CMM_CONV_A, CMM_CONV_B, CMM_CONV_C, CMM_CONV_B_CONJ_TRAN, num);
 | 
						|
 | 
						|
    // multiplication: AxBxA.'xB.'
 | 
						|
    iot_math_cmm_abc(dst,
 | 
						|
        CMM_CONV_A, CMM_CONV_B, CMM_CONV_C, CMM_CONV_C_CONJ_TRAN, num);
 | 
						|
 | 
						|
 */
 | 
						|
 | 
						|
/**
 | 
						|
  * @brief matrix size.
 | 
						|
  */
 | 
						|
typedef enum {
 | 
						|
    CHOL_SIZE_3X3 = 0,
 | 
						|
    CHOL_SIZE_4X4 = 1,
 | 
						|
    CHOL_SIZE_5X5 = 2,
 | 
						|
    CHOL_SIZE_6X6 = 3,
 | 
						|
    CHOL_SIZE_7X7 = 4,
 | 
						|
    CHOL_SIZE_8X8 = 5,
 | 
						|
 | 
						|
    CHOL_SIZE_MAX,
 | 
						|
} chol_ctrl_size_t;
 | 
						|
 | 
						|
typedef enum {
 | 
						|
    CMM_CONV_A = 0,             // matrix A
 | 
						|
    CMM_CONV_A_CONJ = 1,        // matrix A conjugate
 | 
						|
    CMM_CONV_A_TRAN = 2,        // matrix B transpose
 | 
						|
    CMM_CONV_A_CONJ_TRAN = 3,   // matrix B conjugate transpose
 | 
						|
    CMM_CONV_B = 4,             // matrix B
 | 
						|
    CMM_CONV_B_CONJ = 5,        // matrix B conjugate
 | 
						|
    CMM_CONV_B_TRAN = 6,        // matrix B transpose
 | 
						|
    CMM_CONV_B_CONJ_TRAN = 7,   // matrix B conjugate transpose
 | 
						|
    CMM_CONV_C = 8,             // matrix C
 | 
						|
    CMM_CONV_C_CONJ = 9,        // matrix C conjugate
 | 
						|
    CMM_CONV_C_TRAN = 10,       // matrix C transpose
 | 
						|
    CMM_CONV_C_CONJ_TRAN = 11,  // matrix C conjugate transpose
 | 
						|
} cmm_op_conv_t;
 | 
						|
 | 
						|
typedef enum _iot_mtx_err {
 | 
						|
    MTX_OK = 0,                 // no error
 | 
						|
    MTX_OUT_INFINITY = 1,       // output is infinity
 | 
						|
    MTX_IN_NAN = 2,             // input is NaN
 | 
						|
    MTX_OUT_TINY = 3,           // output is tiny
 | 
						|
    MTX_OUT_OVER_MAX = 4,       // output is greater than max number
 | 
						|
} iot_mtx_err_t;
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_mtx_inverse() - get the inverse of the matrix,the matrix data
 | 
						|
                        type is float, and each element has real and imaginary
 | 
						|
                        part, the matrix input element(Ann) format in memory
 | 
						|
                        follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A01
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A02
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Aii
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Ann
 | 
						|
                        |-------|-----------|
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        Output format is same as input.
 | 
						|
 | 
						|
 * @param dst           the destination pointer of inverse matrix
 | 
						|
 * @param src           the source pointer of matrix
 | 
						|
 * @param size          the size of matrix, reference to chol_ctrl_size_t define
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 *
 | 
						|
 * @return              0 -- ok
 | 
						|
 * @return              others -- reference to iot_mtx_err_t
 | 
						|
 */
 | 
						|
uint8_t iot_math_mtx_inverse
 | 
						|
        (float *dst, const float *src, uint8_t size, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_mtx_det() - get the determinant of the matrix,the matrix data
 | 
						|
                        type is float, and each element has real and imaginary
 | 
						|
                        part, the matrix input element(Ann) format in memory
 | 
						|
                        follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A01
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A02
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Aii
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Ann
 | 
						|
                        |-------|-----------|
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        Output format is a 32-bit data.
 | 
						|
 | 
						|
 * @param dst           the destination pointer of determinant
 | 
						|
 * @param src           the source pointer of matrix
 | 
						|
 * @param size          the size of matrix, reference to chol_ctrl_size_t define
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 *
 | 
						|
 * @return              0 -- ok
 | 
						|
 * @return              others -- reference to iot_mtx_err_t
 | 
						|
 */
 | 
						|
uint8_t iot_math_mtx_det
 | 
						|
        (float *dst, const float *src, uint8_t size, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_mtx_inv_det() - get the determinant of the matrix,the matrix
 | 
						|
                        data type is float, and each element has real and
 | 
						|
                        imaginary part, the matrix input element(Ann) format in
 | 
						|
                        memory follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A01
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A02
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Aii
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Ann
 | 
						|
                        |-------|-----------|
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        Output format is same as input,and one more 32-bit data:
 | 
						|
 | 
						|
 * @param inv           the destination pointer of inverse matrix
 | 
						|
 * @param det           the destination pointer of determinant
 | 
						|
 * @param src           the source pointer of matrix
 | 
						|
 * @param size          the size of matrix, reference to chol_ctrl_size_t define
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 *
 | 
						|
 * @return              0 -- ok
 | 
						|
 * @return              others -- reference to iot_mtx_err_t
 | 
						|
 */
 | 
						|
uint8_t iot_math_mtx_inv_det
 | 
						|
        (float *inv, float *det, const float *src, uint8_t size, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_mtx_cholesky() - get the cholesky matrix,the matrix data
 | 
						|
                        type is float, and each element has real and imaginary
 | 
						|
                        part, the matrix input element(Ann) format in memory
 | 
						|
                        follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(01)
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(02)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(ii)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(nn)
 | 
						|
                        |-------|-----------|
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        Output format is a 32-bit data.
 | 
						|
 | 
						|
 * @param dst           the destination pointer of cholesky matrix
 | 
						|
 * @param src           the source pointer of matrix
 | 
						|
 * @param size          the size of matrix, reference to chol_ctrl_size_t define
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 *
 | 
						|
 * @return              0 -- ok
 | 
						|
 * @return              others -- reference to iot_mtx_err_t
 | 
						|
 */
 | 
						|
uint8_t iot_math_mtx_cholesky
 | 
						|
        (float *dst, const float *src, uint8_t size, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_mtx_eig() - get the max eigenvalue and eigenvectorof the
 | 
						|
                        matrix, the matrix data type is float, and each element
 | 
						|
                        has real and imaginary part, the matrix input element
 | 
						|
                        (Ann) format in memory follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(01)
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(02)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(ii)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   A(nn),
 | 
						|
                        |-------|-----------|
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        Output format follows as below:
 | 
						|
 | 
						|
                        0       31          63
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Vec(0)
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Vec(1)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Vec(i)
 | 
						|
                        |-------|-----------|
 | 
						|
                                ...
 | 
						|
                        |-------|-----------|
 | 
						|
                        | real  | imaginary |   Vec(n)
 | 
						|
                        |-------|-----------|
 | 
						|
                        | norm^2| eig       |
 | 
						|
                                .
 | 
						|
                                . repeat(num)
 | 
						|
                                .
 | 
						|
                        The number of repetition is (input param num).
 | 
						|
 | 
						|
                        The max eigenvector is not normalized, if need,
 | 
						|
                        eigenvector shoule be divided by the square root of norm
 | 
						|
 | 
						|
 * @param eigvec        the destination pointer of eigenvector
 | 
						|
 * @param norm          the destination pointer of norm
 | 
						|
 * @param eigval        the destination pointer of eigenvalue
 | 
						|
 * @param src           the source pointer of matrix
 | 
						|
 * @param size          the size of matrix, reference to chol_ctrl_size_t define
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 *
 | 
						|
 * @return              0 -- ok
 | 
						|
 * @return              others -- reference to iot_mtx_err_t
 | 
						|
 */
 | 
						|
uint8_t iot_math_mtx_eig(float *eigvec, float *norm, float *eigval,
 | 
						|
        const float *src, uint8_t size, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_cmm_cfg() - configure matrix multiplication input
 | 
						|
                        if only one operation matrix, set the param as NULL
 | 
						|
 * @param a             the destination pointer of the first operation matrix A
 | 
						|
 * @param ar            the row length of matrix A
 | 
						|
 * @param ac            the column length of matrix A
 | 
						|
 * @param adiag         whether the matrix A is diagonal or not
 | 
						|
 * @param b             the destination pointer of the first operation matrix B
 | 
						|
 * @param br            the row length of matrix B
 | 
						|
 * @param ac            the column length of matrix B
 | 
						|
 * @param bdiag         whether the matrix B is diagonal or not
 | 
						|
 */
 | 
						|
void iot_math_cmm_cfg(float *a, uint8_t ar, uint8_t ac, uint8_t adiag,
 | 
						|
        float *b, uint8_t br, uint8_t bc, uint8_t bdiag);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_cmm_ab() - get matrix multiplication , the format as AxB
 | 
						|
 | 
						|
 * @param dst           the destination pointer of multiplication result
 | 
						|
 * @param op0           the transform of matrix A, reference to cmm_op_conv_t
 | 
						|
 * @param op1           the transform of matrix B, reference to cmm_op_conv_t
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 */
 | 
						|
void iot_math_cmm_ab(float *dst, uint8_t op0, uint8_t op1, uint16_t num);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_math_cmm_abc() - get matrix multiplication , the format as AxBxC,
 | 
						|
                        the first operation is AxB, and then multiply with C,
 | 
						|
                        C can be matrix A, B or the result of the first
 | 
						|
                        operation , also transform matrix.
 | 
						|
                        Such as,
 | 
						|
                            (A)x(B)x(A.*) = (AxB)x(A.*)
 | 
						|
                        first operation
 | 
						|
                            op0: CMM_CONV_A, op1: CMM_CONV_B
 | 
						|
                        seconnd operation
 | 
						|
                            op2: CMM_CONV_C, op3: CMM_CONV_A_CONJ_TRAN
 | 
						|
 | 
						|
 * @param dst           the destination pointer of multiplication result
 | 
						|
 * @param op0           the transform of matrix, reference to cmm_op_conv_t
 | 
						|
 * @param op1           the transform of matrix, reference to cmm_op_conv_t
 | 
						|
 * @param op2           the transform of matrix, reference to cmm_op_conv_t
 | 
						|
 * @param op3           the transform of matrix, reference to cmm_op_conv_t
 | 
						|
 * @param num           the count of operation, minimum value is 1
 | 
						|
 */
 | 
						|
void iot_math_cmm_abc(float *dst,
 | 
						|
        uint8_t op0, uint8_t op1, uint8_t op2, uint8_t op3, uint16_t num);
 | 
						|
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
#endif //IOT_MATRIX_API_H
 |