summaryrefslogtreecommitdiff
path: root/2.3-1/src/c/matrixOperations/expm/sexpma.c
blob: cd96998186f7b2912118323edc5360111270c42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2008-2008 - INRIA - Allan SIMON
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include "matrixExponential.h"

void sexpma (float* in, float* out, int _iLeadDim){

    int iIndex1     = 0;
	int iMax		= 0;
	int iFlag		= 0;
	int iLoop1		= 0;
	int iSquare		= 0;


	float fltExp	= 0;
	float fltS		= 0;
	float fltS2	        = 0;
	float fltCst	= 0.5f;
    double dblCst  = 0.5;

	float *pfltMatrixA		= NULL;/*A'*/
	float *pfltMatrixX		= NULL;/*X*/
	float *pfltMatrixD		= NULL;/*D*/
	float *pfltMatrixcX	= NULL;/*cX*/
	float *pfltMatrixcA	= NULL;/*cX*/
	float *pfltMatrixEye	= NULL;/*Eye*/
	float *pfltMatrixTemp	= NULL;/*Temp*/
	float *pfltMatrixTemp2	= NULL;/*Temp2*/



	iSquare = _iLeadDim * _iLeadDim;

	pfltMatrixA			= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixX			= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixD			= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixcX		= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixcA		= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixEye		= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixTemp		= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);
	pfltMatrixTemp2		= (float*)malloc(sizeof(float) *(unsigned int)  iSquare);



	/* Scale A by power of 2 so that its norm is < 1/2 .*/
	sfrexps( sinfnorma( in, _iLeadDim, _iLeadDim), &fltExp);
	fltS	= max(0, fltExp + 1);
	fltS2	= spows(2, fltS);

	/*A = A./2^s*/

    for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
        pfltMatrixA[iIndex1] =  in[iIndex1] / fltS2;

	/* Pade approximation for exp(A)*/
	/*X = A */
	/*C2F(dcopy)(&iSquare, pfltMatrixA, &iOne, pfltMatrixX, &iOne );*/

    for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
        pfltMatrixX[iIndex1] = pfltMatrixA[iIndex1] ;

	seyea(pfltMatrixEye, _iLeadDim, _iLeadDim);


	/*cA = A * c*/


    for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
        pfltMatrixcA[iIndex1] = pfltMatrixA[iIndex1] * fltCst ;


	/*E = Eye + cA*/
    sadda (pfltMatrixEye , iSquare, pfltMatrixcA ,iSquare, out ) ;

	/*D = Eye - cA*/
    sdiffa (pfltMatrixEye , iSquare, pfltMatrixcA ,iSquare,pfltMatrixD ) ;

	iMax	= 6;
	iFlag	= 1;

	for(iLoop1 = 2 ; iLoop1 <= iMax ; iLoop1++)
	{


		dblCst	= dblCst * (iMax - iLoop1 + 1 ) / (iLoop1 * (2 * iMax - iLoop1 + 1));

        dblCst += 0 ;
		/*Temp = X*/
		/*C2F(dcopy)(&iSquare, pfltMatrixX, &iOne, pfltMatrixTemp, &iOne);*/
        for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
                pfltMatrixTemp[iIndex1] = pfltMatrixX[iIndex1] ;
		/*X = A * Temp;*/

            smulma (  pfltMatrixA , _iLeadDim , _iLeadDim,
                    pfltMatrixTemp , _iLeadDim , _iLeadDim,
                    pfltMatrixX );

		/*cX = c * X*/

    for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
        pfltMatrixcX[iIndex1] = pfltMatrixX[iIndex1] * (float) dblCst ;

		/*E = E + cX*/
        sadda ( out, iSquare , pfltMatrixcX , iSquare , out ) ;

        if(iFlag == 1) /*D = D + cX*/
		    {
            sadda ( pfltMatrixD, iSquare , pfltMatrixcX , iSquare , pfltMatrixD ) ;
		    }
		else /*D = D - cX*/
		    {
            sdiffa ( pfltMatrixD, iSquare , pfltMatrixcX , iSquare , pfltMatrixD );
            }

		/*Toggle iFlag*/
		iFlag = !iFlag;
	}

	/*Temp = E*/
	/*C2F(dcopy)(&iSquare, out, &iOne, pfltMatrixTemp, &iOne);*/
    for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
        pfltMatrixTemp[iIndex1] = out[iIndex1] ;

	/*E = D\E*/
    sldivma (  pfltMatrixD , _iLeadDim , _iLeadDim , pfltMatrixTemp , _iLeadDim , _iLeadDim , out );

	/* Undo scaling by repeated squaring*/
	for(iLoop1 = 0 ; iLoop1 < fltS ; iLoop1++)
	{
		/*Temp = E*/
		/*Temp2 = E*/
        /*C2F(dcopy)(&iSquare, out, &iOne, pfltMatrixTemp, &iOne);
		C2F(dcopy)(&iSquare, out, &iOne, pfltMatrixTemp2, &iOne);*/

        for ( iIndex1 = 0 ; iIndex1 < iSquare ; iIndex1++ )
            {
            pfltMatrixTemp [iIndex1] = out[iIndex1] ;
            pfltMatrixTemp2[iIndex1] = out[iIndex1] ;
            }
		/* E = E*E*/
        smulma (  pfltMatrixTemp , _iLeadDim , _iLeadDim,
                    pfltMatrixTemp2 , _iLeadDim , _iLeadDim,
                    out );
	}

	free(pfltMatrixA);
	free(pfltMatrixX);
	free(pfltMatrixD);
	free(pfltMatrixcX);
	free(pfltMatrixcA);
	free(pfltMatrixEye);
	free(pfltMatrixTemp);
	free(pfltMatrixTemp2);



	return ;
}