summaryrefslogtreecommitdiff
path: root/src/c/CACSD/syslin/dsyslina.c
blob: 3c20c65cc376896aa509ad2a4a89a7b9286cca35 (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
/* Copyright (C) 2017 - IIT Bombay - FOSSEE

 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
 Author: Siddhesh Wani
 Organization: FOSSEE, IIT Bombay
 Email: toolbox@scilab.in
*/

/*Function for declaring state space system*/
/*output structure is as follows :
 | A B X0 |
 | C D dom|
 where dom is 1 for 'c' and 2 for 'd'
 Another column is appended with no_of_states and no_inputs as its elements */



#include <stdlib.h>

void dsyslina(char* dom, double* A, int no_of_states, double* B, int no_of_inputs, \
	double* C, int no_of_outputs, double* D, double* X0, double* out)
{
	int row = 0,col = 0;
    int no_of_cols = no_of_states + no_of_inputs + 2;
    int no_of_rows = no_of_states + no_of_outputs;
    /*Copy matrix A into out matrix*/
    for(col = 0; col < no_of_states; col++)
    {
    	for(row = 0; row < no_of_states; row++)
    	{
    		out[col*no_of_rows + row] = A[col*no_of_states + row];
    	}
    	
    }

    /*Copy matrix B in out matrix*/
    for(col=0; col < no_of_inputs; col++)
    {
    	for(row = 0; row < no_of_states; row++)
    	{
    		out[col * no_of_rows + no_of_states*no_of_rows + row]  \
    				= B[col * no_of_states + row];
    	}
    }

    /*Copy matrix C in out matrix*/
    for(col=0; col < no_of_states; col++)
    {
    	for(row = 0; row < no_of_outputs; row++)
    	{
    		out[no_of_states + (col*no_of_rows) + row] = C[col * no_of_outputs + row];
    	}
    }

	/*Copy matrix D in out matrix*/
	if( D != NULL)
	{
	    for(col=0; col < no_of_inputs; col++)
		{
	    	for(row = 0; row < no_of_outputs; row++)
	    	{
	    		out[(no_of_states+col)*no_of_rows + no_of_states +  row] = \
	    			D[col * no_of_outputs + row];
	    	}
	    }
	}
	else
	{
	    for(col=0; col < no_of_inputs; col++)
		{
	    	for(row = 0; row < no_of_outputs; row++)
	    	{
	    		out[(no_of_states+col)*no_of_rows + no_of_states +  row] = 0;
	    	}
	    }
	}	

	/*Copy matrix X0 in out matrix*/
	if( X0 != NULL)
	{
	    for(row = 0; row < no_of_states; row++)
		{
	    	out[(no_of_rows*(no_of_cols-1))+row] = X0[row];
	    }
	}
	else
	{
	    for(row = 0; row < no_of_states; row++)
		{
	    	out[(no_of_rows*(no_of_cols-1))+row] = 0;
	    }
	}	

	if(*dom == 'c')
		out[(no_of_rows*(no_of_cols-2))	+ no_of_states] = 1; 
	else if(*dom == 'd')
		out[(no_of_rows*(no_of_cols-2)) + no_of_states] = 2;

	/*Insert no of states and inputs in last column*/
	out[(no_of_rows*(no_of_cols-1))] = no_of_states;
	out[(no_of_rows*(no_of_cols-1))+1] = no_of_inputs; 

}