-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsci_foo.c
More file actions
130 lines (102 loc) · 3.8 KB
/
sci_foo.c
File metadata and controls
130 lines (102 loc) · 3.8 KB
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
/* ==================================================================== */
/* Template toolbox_skeleton */
/* Example detail in "API_scilab getting started" help page */
/* This file is released under the 3-clause BSD license. See COPYING-BSD. */
/* ==================================================================== */
#include "api_scilab.h"
#include "BOOL.h"
#include <localization.h>
/* ==================================================================== */
int sci_foo(char *fname, unsigned long fname_len)
{
// Error management variable
SciErr sciErr;
////////// Variables declaration //////////
int m1 = 0, n1 = 0;
int *piAddressVarOne = NULL;
double *matrixOfDouble1 = NULL;
double *newMatrixOfDouble = NULL;
int m2 = 0, n2 = 0;
int *piAddressVarTwo = NULL;
double *matrixOfDouble2 = NULL;
//int *newMatrixOfBoolean = NULL;
int i = 0,j,k;
////////// Check the number of input and output arguments //////////
/* --> [c, d] = foo(a, b) */
/* check that we have only 2 input arguments */
/* check that we have only 2 output argument */
CheckInputArgument(pvApiCtx, 2, 2) ;
CheckOutputArgument(pvApiCtx, 1, 1) ;
////////// Manage the first input argument (double) //////////
/* get Address of inputs */
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
/* Check that the first input argument is a real matrix (and not complex) */
if ( !isDoubleType(pvApiCtx, piAddressVarOne) || isVarComplex(pvApiCtx, piAddressVarOne) )
{
Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 1);
return 0;
}
/* get matrix */
sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &matrixOfDouble1);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
////////// Manage the second input argument//////////
/* get Address of inputs */
sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if ( !isDoubleType(pvApiCtx, piAddressVarTwo) || isVarComplex(pvApiCtx, piAddressVarTwo) )
{
Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 2);
return 0;
}
/* get matrix */
sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &matrixOfDouble2);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
////////// Check the consistency of the two input arguments //////////
if (n1!=m2)
{
Scierror(999, _("%s: Wrong size for input arguments: The no. of columns of first matrix should be equal to no. of rows of second matrix.\n"), fname, 1);
return 0;
}
newMatrixOfDouble = (double*)malloc(sizeof(double) * m1 * n2);
////////// Application code //////////
// Could be replaced by a call to a library
for (i = 0; i < m1 ; i++)
{
for(j=0;j<n2;j++){
newMatrixOfDouble[i+m1*j]=0.0;
for(k=0;k<n1;k++){
newMatrixOfDouble[i+m1*j]=newMatrixOfDouble[i+m1*j]+matrixOfDouble1[i+m1*k]*matrixOfDouble2[k+j*m2];
}
}
}
////////// Create the output argument //////////
/* Create the matrix as return of the function */
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n2, newMatrixOfDouble);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
////////// Return the output argument to the Scilab engine //////////
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */