summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/sci_sym_remove.cpp
blob: c09cbc762f428c1e7fa0f88cdfe1177666264fc7 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (C) 2015 - 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: Iswarya
// Organization: FOSSEE, IIT Bombay
// Email: toolbox@scilab.in

#include <symphony.h>

extern sym_environment* global_sym_env;//defined in globals.cpp

extern "C" {
#include <api_scilab.h>
#include <Scierror.h>
#include <BOOL.h>
#include <stdlib.h>
#include <malloc.h>
#include <localization.h>
#include <sciprint.h>
//function to remove specified columns
int sci_sym_delete_cols(char *fname, unsigned long fname_len){
	
	// Error management variables
	SciErr sciErr1,sciErr2;
	double status=1.0;//assume error status
	double num;//variable to store the number of columns to be deleted obtained from user in scilab
	int count=0;//iterator variable
	int num_cols;//stores the number of columns in the loaded problem
	int iType= 0;//stores the datatype of matrix 
	int rows=0,columns=0;//integer variables to denote the number of rows and columns in the array denoting the column numbers to be deleted
	int *value=NULL;//pointer to integer array allocated dynamically having the indices to be deleted
	double *array_ptr=NULL;//double array pointer to the array denoting the column numbers to be deleted
	int *piAddressVarOne = NULL;//pointer used to access first and second arguments of the function
	int output=0;//output parameter for the symphony sym_delete_cols function
	CheckInputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument as input or not
	CheckOutputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument on output side or not

	//load address of 1st argument into piAddressVarOne
	sciErr2=getVarAddressFromPosition(pvApiCtx,1,&piAddressVarOne);

	
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//check if it is double type
	sciErr2 = getVarType(pvApiCtx, piAddressVarOne, &iType);
	if(sciErr2.iErr || iType != sci_matrix)
	{
		printError(&sciErr2, 0);
		return 0;
	}

		
	//getting the first argument as a double array
	sciErr2=getMatrixOfDouble(pvApiCtx,piAddressVarOne,&rows,&columns,&array_ptr);
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//dynamically allocate the integer array 
	value=(int *)malloc(sizeof(int)*columns);
	//store double values in the integer array by typecasting
	while(count<columns)
	{
		value[count]=(int)array_ptr[count];
		count++;
	}	
	sciprint("\n");

	//ensure that environment is active

	if(global_sym_env==NULL){
		sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
		}
	else {
		int flag=0;//flag used for finding if the indices to be deleted are valid
		output=sym_get_num_cols(global_sym_env,&num_cols);//function to find the number of columns in the loaded problem
		if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999, "An error occured. Has a problem been loaded?\n");
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		for(count=0;count<columns;count++)//loop used to check if all the indices mentioned to be deleted are valid
		{
			if(value[count]<0   ||  value[count]>=num_cols){
				flag=1;
				break;
		}	
				
		}
		if(flag==1)
		{
			Scierror(999,"Not valid indices..\n");
			sciprint("valid indices are from 0 to %d",num_cols-1);
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		//only when the number of columns to be deleted is lesser than the actual number of columns ,execution is proceeded with
		if(columns<=num_cols){
		output=sym_delete_cols(global_sym_env,(int)columns,value);//symphony function to delete the columns specified
		if(output==FUNCTION_TERMINATED_NORMALLY)
		{
			sciprint("Execution is successfull\n");
			status=0.0;
		}
		else if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999,"Problem occured while deleting the columns,Are the column numbers correct?\n");
			sciprint("Function terminated abnormally\n");
			status=1.0;
		}
	}
		else{
			sciprint("These many number of variables dont exist in the problem\n");
			status=1.0;
		}
				
	}
	
	int e=createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,status);
	if (e){
		AssignOutputVariable(pvApiCtx, 1) = 0;
		free(value);//freeing the memory of the allocated pointer
		return 1;
		}

	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	//ReturnArguments(pvApiCtx);
	free(value);//freeing the memory of the allocated pointer
	return 0;
	}


//function to remove specified rows
int sci_sym_delete_rows(char *fname, unsigned long fname_len){
	
	// Error management variable
	SciErr sciErr1,sciErr2;
	double status=1.0;//assume error status
	int count=0;//iterator variable
	int num_rows;//stores the number of columns in the loaded problem
	int iType= 0;//stores the datatype of matrix 
	int rows=0,columns=0;//integer variables to denote the number of rows and columns in the array denoting the row numbers to be deleted
	int *value=NULL;//pointer to integer array allocated dynamically having the indices to be deleted
	double *array_ptr=NULL;//double array pointer to the array denoting the rows numbers to be deleted
	int *piAddressVarTwo = NULL;//pointer used to access first and second arguments of the function
	int output=0;//output parameter for the symphony sym_delete_rows function
	CheckInputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument as input or not
	CheckOutputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument on output side or not

	//load address of 2nd argument into piAddressVarTwo
	sciErr2=getVarAddressFromPosition(pvApiCtx,1,&piAddressVarTwo);

	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//check if it is double type
	sciErr2 = getVarType(pvApiCtx, piAddressVarTwo, &iType);
	if(sciErr2.iErr || iType != sci_matrix)
	{
		printError(&sciErr2, 0);
		return 0;
	}

		
	//getting the second argument as a double array
	sciErr2=getMatrixOfDouble(pvApiCtx,piAddressVarTwo,&rows,&columns,&array_ptr);
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//dynamically allocate the integer array 
	value=(int *)malloc(sizeof(int)*columns);
	//store double values in the integer array by typecasting
	while(count<columns)
	{
		value[count]=(int)array_ptr[count];
		count++;
	}	
	sciprint("\n");

	//ensure that environment is active

	if(global_sym_env==NULL){
		sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
		}
	else {
		output=sym_get_num_rows(global_sym_env,&num_rows);//function to find the number of rows in the loaded problem
		if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999, "An error occured. Has a problem been loaded?\n");
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		int flag=0;//variable to check if the entered indices are valid.
		for(count=0;count<columns;count++)//loop used to check if all the indices mentioned to be deleted are valid
		{
			if(value[count]<0   ||  value[count]>=num_rows){
				flag=1;
				break;
		}	
				
		}
		if(flag==1)
		{
			Scierror(999,"Not valid indices..\n");
			sciprint("valid constraint indices are from 0 to %d",num_rows-1);
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		//only when the number of rows to be deleted is lesser than the actual number of rows ,execution is proceeded with
		if(columns<=num_rows){
		output=sym_delete_rows(global_sym_env,(int)columns,value);//symphony function to delete the rows specified
		if(output==FUNCTION_TERMINATED_NORMALLY)
		{
			sciprint("Execution is successfull\n");
			status=0.0;
		}
		else if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999,"Problem occured while deleting the columns,Are the column numbers correct?\n");
			sciprint("Function terminated abnormally\n");
			status=1.0;
		}
	}
		else{
			sciprint("These many number of constraints dont exist in the problem\n");
			status=1.0;
		}
				
	}
	
	int e=createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,status);
	if (e){
		AssignOutputVariable(pvApiCtx, 1) = 0;
		free(value);//freeing the memory of the allocated pointer
		return 1;
		}

	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	//ReturnArguments(pvApiCtx);
	free(value);//freeing the memory of the allocated pointer
	return 0;
	}

}