# svd/app.cpp

/*******************************************************************************
    Copyright (c) 2021-2022 Qualcomm Technologies, Inc.
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted (subject to the limitations in the disclaimer
    below) provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of Qualcomm Technologies, Inc. nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.
    
    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
    THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
    NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    @brief
    Program to run the FastADAS Singular Value Decomposition.
    *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <vector>
    #include <iostream>
    #include <iomanip>
    
    //#define USE_EIGEN
    
    #ifdef USE_EIGEN
    #include <Eigen/Eigen>
    #endif
    
    #ifdef USE_OPENCV
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/calib3d.hpp>
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/mat.hpp>
    #endif
    
    #include <fadas.h>
    #include "../util/util.h"

    int main( int argc, char** argv )
    {
        if( FADAS_ERROR_NONE != FadasInit( nullptr ) )
        {
            UTIL_ERROR( "FadasInit failed" );
            return -1;
        }
    
        const uint32_t r = 3;
        const uint32_t c = 4;
        float32_t FADAS_ALIGN128( A[r][c] ) = {
            { 1.0, 1.0, 0.0, 1.0 },
            { 0.0, 0.0, 0.0, 1.0 },
            { 1.0, 1.0, 0.0, 0.0 } };
    
        float32_t FADAS_ALIGN128( svd_s[c] ) = { 0 };
        float32_t FADAS_ALIGN128( svd_U[r][c] ) = { 0 };
        float32_t FADAS_ALIGN128( svd_Vt[c][c] ) = { 0 };
        float32_t FADAS_ALIGN128( tmp_U[r][c] ) = { 0 };
        float32_t FADAS_ALIGN128( tmp_Vt[c][c] ) = { 0 };
    
        FadasRegBuf( FADAS_BUF_TYPE_IN, A, sizeof(A) );
        FadasRegBuf( FADAS_BUF_TYPE_OUT, svd_s, sizeof(svd_s) );
        FadasRegBuf( FADAS_BUF_TYPE_OUT, &svd_U[0][0], sizeof(svd_U) );
        FadasRegBuf( FADAS_BUF_TYPE_OUT, &svd_Vt[0][0], sizeof(svd_Vt) );
        FadasRegBuf( FADAS_BUF_TYPE_OUT, &tmp_U[0][0], sizeof(tmp_U) );
        FadasRegBuf( FADAS_BUF_TYPE_OUT, &tmp_Vt[0][0], sizeof(tmp_Vt) );
    
        if( FadasSVD_SVDf32( &A[0][0], r, c, svd_s, &svd_U[0][0], &svd_Vt[0][0], &tmp_U[0][0],
                            &tmp_Vt[0][0] ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "FadasSVD_SVDf32 error" );
    
        uint32_t mrc = std::min( c, r );
        UTIL_PrintMatrix( r, c, &A[0][0], "A" );
        UTIL_PrintMatrix( mrc, 1, svd_s, "svd_s" );
        UTIL_PrintMatrix( r, mrc, &svd_U[0][0], "svd_U" );
        UTIL_PrintMatrix( mrc, c, &svd_Vt[0][0], "svd_Vt" );

    #ifdef USE_OPENCV
        std::cout << "\nOpenCV" << std::setprecision( 4 ) << std::endl;
        cv::Mat o_A( r, c, CV_32FC1, A );
        cv::Mat o_s, o_U, o_Vt;
        cv::SVD::compute( o_A, o_s, o_U, o_Vt );
        std::cout << "o_A:\n" << o_A << std::endl;
        std::cout << "\no_s:\n" << o_s << std::endl;
        std::cout << "\no_U:\n" << o_U << std::endl;
        std::cout << "\no_Vt:\n" << o_Vt << std::endl;
    #endif

    #ifdef USE_EIGEN
        std::cout << "\nEigen" << std::setprecision( 4 ) << std::endl;
        Eigen::MatrixXf e_A = Eigen::Map<Eigen::Matrix<float, r, c, Eigen::RowMajor>>( (float*)A );
        std::cout << "e_A:\n" << e_A << std::endl;
        Eigen::JacobiSVD<Eigen::MatrixXf> svdA( e_A, Eigen::ComputeThinU | Eigen::ComputeThinV );
        std::cout << "\ne_s:\n" << svdA.singularValues() << std::endl;
        std::cout << "\ne_U:\n" << svdA.matrixU() << std::endl;
        std::cout << "\ne_Vt:\n" << svdA.matrixV().transpose() << std::endl;
    #endif
    
        FadasDeregBuf( &A[0][0] );
        FadasDeregBuf( &svd_s[0] );
        FadasDeregBuf( &svd_U[0][0] );
        FadasDeregBuf( &svd_Vt[0][0] );
        FadasDeregBuf( &tmp_U[0][0] );
        FadasDeregBuf( &tmp_Vt[0][0] );
    
        FadasDeInit();
    
        return 0;
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
vyuy\_remap/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/vyuy-remap.md)