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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2010 - DIGITEO - Sylvestre LEDRU
*
* 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.1-en.txt
*
*/
#include <stdlib.h>
#include <string.h>
#define ENABLE_HELPERS
#include "javasci2_helper.h"
#include "api_scilab.h"
#include "lasterror.h"
BOOL isComplexVar(char *variableName)
{
/* 0 = not complex */
return isNamedVarComplex(pvApiCtx, variableName) != 0;
}
sci_int_types getIntegerPrecision(char *variableName)
{
SciErr sciErr;
int iPrec;
sciErr = getNamedMatrixOfIntegerPrecision(pvApiCtx, variableName, &iPrec);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return sciErr.iErr;
}
return iPrec;
}
double *getDouble(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
double *matrixOfDouble = NULL;
sciErr = readNamedMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfDouble = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double));
/* Load the matrix */
sciErr = readNamedMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, matrixOfDouble);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfDouble;
}
int putDouble(char *variableName, double *variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
double *getDoubleComplexReal(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
double *matrixOfDoubleComplexReal = NULL;
double *matrixOfDoubleComplexImg = NULL;
sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, NULL, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfDoubleComplexReal = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double));
matrixOfDoubleComplexImg = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double));
/* Load the matrix */
sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, matrixOfDoubleComplexReal, matrixOfDoubleComplexImg);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfDoubleComplexReal;
}
double *getDoubleComplexImg(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
double *matrixOfDoubleComplexReal = NULL;
double *matrixOfDoubleComplexImg = NULL;
sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, NULL, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfDoubleComplexReal = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double));
matrixOfDoubleComplexImg = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double));
/* Load the matrix */
sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, matrixOfDoubleComplexReal, matrixOfDoubleComplexImg);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfDoubleComplexImg;
}
int putDoubleComplex(char* variableName, double *variable, int nbRow, int nbCol, double * imag, int nbRowI, int nbColI)
{
SciErr sciErr;
sciErr = createNamedComplexMatrixOfDouble(pvApiCtx, variableName, nbRow, nbCol, variable, imag);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
BOOL *getBoolean(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
BOOL *matrixOfBoolean = NULL;
sciErr = readNamedMatrixOfBoolean(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfBoolean = (BOOL *) malloc(((*nbRow) * (*nbCol)) * sizeof(BOOL));
/* Load the matrix */
sciErr = readNamedMatrixOfBoolean(pvApiCtx, variableName, nbRow, nbCol, matrixOfBoolean);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfBoolean;
}
int putBoolean(char *variableName, BOOL * variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfBoolean(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
///////////////////// byte / int8
byte *getByte(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
byte *matrixOfByte = NULL;
sciErr = readNamedMatrixOfInteger8(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfByte = (byte *) malloc(((*nbRow) * (*nbCol)) * sizeof(byte));
/* Load the matrix */
sciErr = readNamedMatrixOfInteger8(pvApiCtx, variableName, nbRow, nbCol, (char *)matrixOfByte);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfByte;
}
int putByte(char *variableName, byte * variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfInteger8(pvApiCtx, variableName, nbRow, nbCol, (const char *)variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
byte *getUnsignedByte(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
byte *matrixOfByte = NULL;
sciErr = readNamedMatrixOfUnsignedInteger8(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfByte = (byte *) malloc(((*nbRow) * (*nbCol)) * sizeof(byte));
/* Load the matrix */
sciErr = readNamedMatrixOfUnsignedInteger8(pvApiCtx, variableName, nbRow, nbCol, matrixOfByte);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfByte;
}
int putUnsignedByte(char *variableName, byte * variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfUnsignedInteger8(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
//////////////////////////// short / int16
short *getShort(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
short *matrixOfShort = NULL;
sciErr = readNamedMatrixOfInteger16(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfShort = (short *)malloc(((*nbRow) * (*nbCol)) * sizeof(short));
/* Load the matrix */
sciErr = readNamedMatrixOfInteger16(pvApiCtx, variableName, nbRow, nbCol, matrixOfShort);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfShort;
}
int putShort(char *variableName, short *variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfInteger16(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
unsigned short *getUnsignedShort(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
unsigned short *matrixOfShort = NULL;
sciErr = readNamedMatrixOfUnsignedInteger16(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfShort = (unsigned short *)malloc(((*nbRow) * (*nbCol)) * sizeof(unsigned short));
/* Load the matrix */
sciErr = readNamedMatrixOfUnsignedInteger16(pvApiCtx, variableName, nbRow, nbCol, matrixOfShort);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfShort;
}
int putUnsignedShort(char *variableName, unsigned short *variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfUnsignedInteger16(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
////////////////////// int / int32
int *getInt(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
int *matrixOfInt = NULL;
sciErr = readNamedMatrixOfInteger32(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfInt = (int *)malloc(((*nbRow) * (*nbCol)) * sizeof(int));
/* Load the matrix */
sciErr = readNamedMatrixOfInteger32(pvApiCtx, variableName, nbRow, nbCol, matrixOfInt);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfInt;
}
int putInt(char *variableName, int *variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfInteger32(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
unsigned int *getUnsignedInt(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
int *matrixOfInt = NULL;
sciErr = readNamedMatrixOfUnsignedInteger32(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfInt = (int *)malloc(((*nbRow) * (*nbCol)) * sizeof(int));
/* Load the matrix */
sciErr = readNamedMatrixOfUnsignedInteger32(pvApiCtx, variableName, nbRow, nbCol, (unsigned int *)matrixOfInt);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return (unsigned int *)matrixOfInt;
}
int putUnsignedInt(char *variableName, unsigned int *variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfUnsignedInteger32(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
////////////////////// long / int64
#ifdef __SCILAB_INT64__
long *getLong(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
long *matrixOfLong = NULL;
sciErr = readNamedMatrixOfInteger64(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfLong = (long *)malloc(((*nbRow) * (*nbCol)) * sizeof(long));
/* Load the matrix */
sciErr = readNamedMatrixOfInteger64(pvApiCtx, variableName, nbRow, nbCol, matrixOfLong);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfLong;
}
int putLong(char *variableName, long *variable, int *nbRow, int *nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfInteger64(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
unsigned long *getUnsignedLong(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
long *matrixOfLong = NULL;
sciErr = readNamedMatrixOfUnsignedInteger64(pvApiCtx, variableName, nbRow, nbCol, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
/* Alloc the memory */
matrixOfLong = (long *)malloc(((*nbRow) * (*nbCol)) * sizeof(long));
/* Load the matrix */
sciErr = readNamedMatrixOfUnsignedInteger64(pvApiCtx, variableName, nbRow, nbCol, matrixOfLong);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return matrixOfLong;
}
int putUnsignedLong(char *variableName, unsigned long *variable, int *nbRow, int *nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfUnsignedInteger64(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
#endif
char **getString(char *variableName, int *nbRow, int *nbCol)
{
SciErr sciErr;
int i = 0;
int *piLen = NULL;
char **pstData = NULL;
//first call to retrieve dimensions
sciErr = readNamedMatrixOfString(pvApiCtx, variableName, nbRow, nbCol, NULL, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
piLen = (int *)malloc(sizeof(int) * (*nbRow) * (*nbCol));
//second call to retrieve length of each string
sciErr = readNamedMatrixOfString(pvApiCtx, variableName, nbRow, nbCol, piLen, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
pstData = (char **)malloc(sizeof(char *) * (*nbRow) * (*nbCol));
for (i = 0; i < (*nbRow) * (*nbCol); i++)
{
pstData[i] = (char *)malloc(sizeof(char) * (piLen[i] + 1)); //+ 1 for null termination
}
//third call to retrieve data
sciErr = readNamedMatrixOfString(pvApiCtx, variableName, nbRow, nbCol, piLen, pstData);
if (sciErr.iErr)
{
printError(&sciErr, 0);
}
return pstData;
}
int putString(char *variableName, char **variable, int nbRow, int nbCol)
{
SciErr sciErr;
sciErr = createNamedMatrixOfString(pvApiCtx, variableName, nbRow, nbCol, variable);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
int putSparse(char * variableName, int nbRow, int nbCol, int * nbRowItem, int nbRowItemL, int * colPos, int colPosL, double * data, int dataL)
{
SciErr sciErr;
sciErr = createNamedSparseMatrix(pvApiCtx, variableName, nbRow, nbCol, colPosL, nbRowItem, colPos, data);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
int putComplexSparse(char * variableName, int nbRow, int nbCol, int * nbRowItem, int nbRowItemL, int * colPos, int colPosL, double * data, int dataL, double * imag, int imagL)
{
SciErr sciErr;
sciErr = createNamedComplexSparseMatrix(pvApiCtx, variableName, nbRow, nbCol, colPosL, nbRowItem, colPos, data, imag);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
int putBooleanSparse(char * variableName, int nbRow, int nbCol, int * nbRowItem, int nbRowItemL, int * colPos, int colPosL)
{
SciErr sciErr;
sciErr = createNamedBooleanSparseMatrix(pvApiCtx, variableName, nbRow, nbCol, colPosL, nbRowItem, colPos);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
int putPolynomial(char * variableName, char * polyVarName, double ** data, int nbRow, int nbCol, int * nbCoef)
{
SciErr sciErr;
sciErr = createNamedMatrixOfPoly(pvApiCtx, variableName, polyVarName, nbRow, nbCol, nbCoef, data);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
int putComplexPolynomial(char * variableName, char * polyVarName, double ** data, int nbRow, int nbCol, int * nbCoef, double ** imag, int nbRowI, int nbColI, int * nbCoefI)
{
SciErr sciErr;
sciErr = createNamedComplexMatrixOfPoly(pvApiCtx, variableName, polyVarName, nbRow, nbCol, nbCoef, data, imag);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return -1;
}
return 0;
}
BOOL isExistingVariable(char *variableName)
{
int iExisting = isNamedVarExist(pvApiCtx, variableName);
return iExisting != 0; /* 0 = not existing variable */
}
|