/** @file  psUnary.c
 *
 *  @brief Provides unary functions for simple matrix and vector element operations. Functions
 *  include:
 *
 *      Addition (+)
 *      Subtraction (-)
 *      Multiplication (*)
 *      Division (/)
 *      Power (^)
 *      Minimum (min)
 *      Maximum (max)
 *      Absolute value (abs)
 *      Exponent (exp)
 *      Natural Log (ln)
 *      Power of 10 (ten)
 *      Log (log)
 *      Sine (sin or dsin)
 *      Cosine (cos or dcos)
 *      Tangent (tan or dtan)
 *      Arcsine (asin or dasin)
 *      Arccosine (acos or dacos)
 *      Arctan (atan or datan)
 *
 *  Currently only vector-vector and image-image binary operations are supported.
 *
 *  @ingroup MatrixArithmetic
 *
 *  @author Ross Harman, MHPCC
 *  @author Robert DeSonia, MHPCC
 *
 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2005-05-12 00:54:49 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

/******************************************************************************
 *  INCLUDE FILES                                                             *
 ******************************************************************************/
#include <string.h>
#include <complex.h>
#include <math.h>
#include <stdint.h>

#include "psMemory.h"
#include "psError.h"
#include "psImage.h"
#include "psVector.h"
#include "psScalar.h"
#include "psLogMsg.h"
#include "psConstants.h"
#include "psDataManipErrors.h"

/*****************************************************************************
 *  FUNCTION IMPLEMENTATION - LOCAL                                          *
 *****************************************************************************/

// Conversion for degrees to radians
#define D2R 0.01745329252111111  /* PI/180 */

// Conversion for radians to degrees
#define R2D 57.29577950924861   /* 180.0/PI */


// Unary SCALAR operations
#define SCALAR(OUT,IN,OP,TYPE)                                                                               \
{                                                                                                            \
    ps##TYPE *o = NULL;                                                                                      \
    ps##TYPE *i1 = NULL;                                                                                     \
    o  = &((psScalar* )OUT)->data.TYPE;                                                                      \
    i1 = &((psScalar* )IN)->data.TYPE;                                                                       \
    *o = OP;                                                                                                 \
}

// Unary IMAGE operations
#define VECTOR(OUT,IN,OP,TYPE)                                                                               \
{                                                                                                            \
    psS32 i = 0;                                                                                             \
    psS32 nIn = 0;                                                                                           \
    psS32 nOut = 0;                                                                                          \
    ps##TYPE *o = NULL;                                                                                      \
    ps##TYPE *i1 = NULL;                                                                                     \
    nIn = ((psVector* )IN)->n;                                                                               \
    nOut = ((psVector* )OUT)->n;                                                                             \
    if(nIn != nOut) {                                                                                        \
        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
                PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                         \
                nIn, nOut);                                                                                  \
        if (OUT != IN) {                                                                                     \
            psFree(OUT);                                                                                     \
        }                                                                                                    \
        return NULL;                                                                                         \
    }                                                                                                        \
    o  = ((psVector* )OUT)->data.TYPE;                                                                       \
    i1 = ((psVector* )IN)->data.TYPE;                                                                        \
    for(i = 0; i < nIn; i++, o++, i1++) {                                                                    \
        *o = OP;                                                                                             \
    }                                                                                                        \
}

// Unary IMAGE operations
#define IMAGE(OUT,IN,OP,TYPE)                                                                                \
{                                                                                                            \
    psS32 i = 0;                                                                                             \
    psS32 j = 0;                                                                                             \
    psS32 numRowsIn = 0;                                                                                     \
    psS32 numColsIn = 0;                                                                                     \
    psS32 numRowsOut = 0;                                                                                    \
    psS32 numColsOut = 0;                                                                                    \
    ps##TYPE *o = NULL;                                                                                      \
    ps##TYPE *i1 = NULL;                                                                                     \
    numRowsIn = ((psImage* )IN)->numRows;                                                                    \
    numColsIn = ((psImage* )IN)->numCols;                                                                    \
    numRowsOut = ((psImage* )OUT)->numRows;                                                                  \
    numColsOut = ((psImage* )OUT)->numCols;                                                                  \
    if(numRowsIn!=numRowsOut || numColsIn!=numColsOut) {                                                     \
        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
                PS_ERRORTEXT_psMatrix_IMAGE_SIZE_DIFFERS,                                                    \
                numColsIn, numRowsIn, numColsOut, numRowsOut);                                               \
        if (OUT != IN) {                                                                                     \
            psFree(OUT);                                                                                     \
        }                                                                                                    \
        return NULL;                                                                                         \
    }                                                                                                        \
    for(j = 0; j < numRowsIn; j++) {                                                                         \
        o  = ((psImage* )OUT)->data.TYPE[j];                                                                 \
        i1 = ((psImage* )IN)->data.TYPE[j];                                                                  \
        for(i = 0; i < numColsIn; i++, o++, i1++) {                                                          \
            *o = OP;                                                                                         \
        }                                                                                                    \
    }                                                                                                        \
}

// Preprocessor macro function to create arithmetic function based on input type
#define UNARY_TYPE(DIM,OUT,IN,OP)                                                                            \
switch (IN->type) {                                                                                          \
case PS_TYPE_S32:                                                                                            \
    DIM(OUT,IN,OP,S32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_F32:                                                                                            \
    DIM(OUT,IN,OP,F32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_F64:                                                                                            \
    DIM(OUT,IN,OP,F64);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_C32:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_S8:                                                                                             \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_U8:                                                                                             \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_S16:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_U16:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_U32:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_S64:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_U64:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
case PS_TYPE_C64:                                                                                            \
    DIM(OUT,IN,OP,C32);                                                                                      \
    break;                                                                                                   \
default: {                                                                                                     \
        char* strType;                                                                                           \
        PS_TYPE_NAME(strType, IN->type);                                                                         \
        psError(PS_ERR_BAD_PARAMETER_TYPE, true,                                                                 \
                PS_ERRORTEXT_psMatrix_TYPE_MISMATCH,                                                             \
                strType);                                                                                        \
        if (OUT != IN) {                                                                                         \
            psFree(OUT);                                                                                         \
        }                                                                                                        \
        return NULL;                                                                                             \
    } \
}

// Preprocessor macro function to create arithmetic function operation name. Functions below that add
// FLT_EPSILON are done so to align results with a 64 bit computing architecture
#define UNARY_OP(DIM,OUT,IN,OP)                                                                              \
if(!strncmp(OP, "abs", 3)) {                                                                                 \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cabs(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,fabs(*i1));                                                                    \
    }                                                                                                        \
} else if(!strncmp(OP, "exp", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cexp(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,exp(*i1));                                                                     \
    }                                                                                                        \
} else if(!strncmp(OP, "ln", 2)) {                                                                           \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,clog(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,log(*i1));                                                                     \
    }                                                                                                        \
} else if(!strncmp(OP, "ten", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cpow(10.0,*i1));                                                               \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,pow(10.0,*i1));                                                                \
    }                                                                                                        \
} else if(!strncmp(OP, "log", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,clog(*i1)/log(10.0));                                                          \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,log10(*i1));                                                                   \
    }                                                                                                        \
} else if(!strncmp(OP, "sin", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,csin(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,sin(*i1));                                                                     \
    }                                                                                                        \
} else if(!strncmp(OP, "dsin", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,csin(*i1*D2R));                                                                \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,sin(*i1*D2R));                                                                 \
    }                                                                                                        \
} else if(!strncmp(OP, "cos", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,ccos(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cos(*i1));                                                                     \
    }                                                                                                        \
} else if(!strncmp(OP, "dcos", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,ccos(*i1*D2R));                                                                \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cos(*i1*D2R));                                                                 \
    }                                                                                                        \
} else if(!strncmp(OP, "tan", 3)) {                                                                          \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,ctan(*i1));                                                                    \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,tan(*i1));                                                                     \
    }                                                                                                        \
} else if(!strncmp(OP, "dtan", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,ctan(*i1*D2R));                                                                \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,tan(*i1*D2R));                                                                 \
    }                                                                                                        \
} else if(!strncmp(OP, "asin", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,casin(*i1));                                                                   \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,asin(*i1));                                                                    \
    }                                                                                                        \
} else if(!strncmp(OP, "dasin", 5)) {                                                                        \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,R2D*casin(*i1));                                                               \
    } else if(PS_IS_PSELEMTYPE_INT(IN->type)) {                                                              \
        UNARY_TYPE(DIM,OUT,IN,(R2D*asin(*i1)));                                                              \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,(R2D*asin(*i1)));                                                              \
    }                                                                                                        \
} else if(!strncmp(OP, "acos", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,cacos(*i1));                                                                   \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,acos(*i1));                                                                    \
    }                                                                                                        \
} else if(!strncmp(OP, "dacos", 5)) {                                                                        \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,R2D*cacos(*i1));                                                               \
    } else if(PS_IS_PSELEMTYPE_INT(IN->type)) {                                                              \
        UNARY_TYPE(DIM,OUT,IN,(R2D*acos(*i1)));                                                              \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,R2D*acos(*i1));                                                                \
    }                                                                                                        \
} else if(!strncmp(OP, "atan", 4)) {                                                                         \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,catan(*i1));                                                                   \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,atan(*i1));                                                                    \
    }                                                                                                        \
} else if(!strncmp(OP, "datan", 5)) {                                                                        \
    if(PS_IS_PSELEMTYPE_COMPLEX(IN->type)) {                                                                 \
        UNARY_TYPE(DIM,OUT,IN,R2D*catan(*i1));                                                               \
    } else {                                                                                                 \
        UNARY_TYPE(DIM,OUT,IN,R2D*atan(*i1));                                                                \
    }                                                                                                        \
} else {                                                                                                     \
    psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                                \
            PS_ERRORTEXT_psMatrix_OPERATION_UNSUPPORTED,                                                     \
            OP);                                                                                             \
    if (OUT != IN) {                                                                                         \
        psFree(OUT);                                                                                         \
    }                                                                                                        \
    return NULL;                                                                                             \
}

psPtr psUnaryOp(psPtr out, psPtr in, char *op)
{
    #define psUnaryOp_EXIT { \
                             if (out != in) { \
                             psFree(out); \
                             } \
                             return NULL; \
                           }

    psType* psTypeIn = (psType* ) in;

    PS_PTR_CHECK_NULL_GENERAL(in, psUnaryOp_EXIT);
    PS_PTR_CHECK_NULL_GENERAL(op, psUnaryOp_EXIT);

    psDimen dimIn = psTypeIn->dimen;
    psElemType elTypeIn = psTypeIn->type;

    switch (dimIn) {
    case PS_DIMEN_SCALAR:
        if (out == NULL ||
                ((psType*)out)->dimen != PS_DIMEN_SCALAR ||
                ((psScalar*)out)->type.type != elTypeIn) {
            psFree(out);
            out = psScalarAlloc(0.0,elTypeIn);
        }
        UNARY_OP(SCALAR, out, psTypeIn, op);    // scalar
        break;
    case PS_DIMEN_VECTOR:
    case PS_DIMEN_TRANSV:
        if (((psVector*)in)->n == 0) {
            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
                    PS_ERRORTEXT_psMatrix_VECTOR_EMPTY);
            psUnaryOp_EXIT;
        }

        out = psVectorRecycle(out,
                              ((psVector*)in)->n,
                              elTypeIn);
        if (out == NULL) {
            psError(PS_ERR_UNKNOWN, false,
                    PS_ERRORTEXT_psMatrix_OUTPUT_VECTOR_NOT_CREATED);
            psUnaryOp_EXIT;
        }

        UNARY_OP(VECTOR, out, psTypeIn, op);    // vector
        break;
    case PS_DIMEN_IMAGE:
        if (((psImage* ) in)->numCols == 0 || ((psImage* ) in)->numRows == 0) {
            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
                    PS_ERRORTEXT_psMatrix_IMAGE_EMPTY);
            psUnaryOp_EXIT;
        }

        out = psImageRecycle(out,
                             ((psImage*)in)->numCols,
                             ((psImage*)in)->numRows,
                             elTypeIn);
        if (out == NULL) {
            psError(PS_ERR_UNKNOWN, false,
                    PS_ERRORTEXT_psMatrix_OUTPUT_IMAGE_NOT_CREATED);
            psUnaryOp_EXIT;
        }

        UNARY_OP(IMAGE, out, psTypeIn, op);     // image
        break;
    default:
        if (out != in) {
            psFree(out);
        }
        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
                PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
                "in", dimIn);
        psUnaryOp_EXIT;
    }

    // Automtically free psScalar types, since they are usually allocated in the argument list when this
    // function is called, provided that the input is not the output.
    if(psTypeIn->dimen==PS_DIMEN_SCALAR && in!=out) {
        psFree(in);
    }

    return out;
}
