License Header change: Removed the LICENSE_END before beta
[scilab.git] / scilab / modules / api_scilab / help / en_US / legacy / polynomial / J_polyExample.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4  * Copyright (C) 2009  - DIGITEO - Antoine ELIAS
5  * 
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  -->
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="polyExample" xml:lang="en">
17     <refnamediv>
18         <refname>polynomial example</refname>
19         <refpurpose>
20             Polynom use case.
21         </refpurpose>
22     </refnamediv>
23     <refsection>
24         <title>Description</title>
25         <para>This example shows how to get a polynomial variable and return it to Scilab.</para>
26     </refsection>
27     <refsection>
28         <!--File_gateway: SCI/modules/api_scilab/tests/unit_tests/polyExample.c-->
29         <!--File_scilab: SCI/modules/api_scilab/tests/unit_tests/polyExample.tst-->
30         <!--Lib_name: polyExample-->
31         <!--Func_list: polyExample-->
32         <title>Gateway Source</title>
33         <programlisting role="code_gateway"><![CDATA[ 
34 int polyExample(char *fname,void* pvApiCtx)
35 {
36         SciErr sciErr;
37         int* piAddr = NULL;
38         int iType   = 0;
39         int iRet    = 0;
40
41     CheckInputArgument(pvApiCtx, 1, 1);
42     CheckOutputArgument(pvApiCtx, 0, 1);
43
44         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
45         if(sciErr.iErr)
46         {
47                 printError(&sciErr, 0);
48                 return 0;
49         }
50
51         if(isPolyType(pvApiCtx, piAddr))
52         {
53                 char pstVarName[64];
54                 int iLen = 0;
55
56                 sciErr = getPolyVariableName(pvApiCtx, piAddr, pstVarName, &iLen);
57                 if(sciErr.iErr)
58                 {
59                         printError(&sciErr, 0);
60                         return sciErr.iErr;
61                 }
62
63                 if(isScalar(pvApiCtx, piAddr))
64                 {
65                         int iNbCoef                             = 0;
66                         double* pdblReal        = NULL;
67                         double* pdblImg         = NULL;
68
69                         if(isVarComplex(pvApiCtx, piAddr))
70                         {
71                                 iRet = getAllocatedSingleComplexPoly(pvApiCtx, piAddr, &iNbCoef, &pdblReal, &pdblImg);
72                                 if(iRet)
73                                 {
74                                         freeAllocatedSingleComplexPoly(pdblReal, pdblImg);
75                                         return iRet;
76                                 }
77
78                                 sciErr = createComplexMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, 1, 1, &iNbCoef, &pdblReal, &pdblImg);
79                                 if(sciErr.iErr)
80                                 {
81                                         freeAllocatedSingleComplexPoly(pdblReal, pdblImg);
82                                         printError(&sciErr, 0);
83                                         return sciErr.iErr;
84                                 }
85
86                                 freeAllocatedSingleComplexPoly(pdblReal, pdblImg);
87                         }
88                         else
89                         {
90                                 iRet = getAllocatedSinglePoly(pvApiCtx, piAddr, &iNbCoef, &pdblReal);
91                                 if(iRet)
92                                 {
93                                         freeAllocatedSinglePoly(pdblReal);
94                                         return iRet;
95                                 }
96
97                                 sciErr = createMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, 1, 1, &iNbCoef, &pdblReal);
98                                 if(sciErr.iErr)
99                                 {
100                                         freeAllocatedSinglePoly(pdblReal);
101                                         printError(&sciErr, 0);
102                                         return sciErr.iErr;
103                                 }
104
105                                 freeAllocatedSinglePoly(pdblReal);
106                         }
107                 }
108                 else
109                 {
110                         int iRows           = 0;
111                         int iCols           = 0;
112                         int* piNbCoef       = NULL;
113                         double** pdblReal   = NULL;
114                         double** pdblImg    = NULL;
115
116                         if(isVarComplex(pvApiCtx, piAddr))
117                         {
118                                 iRet = getAllocatedMatrixOfComplexPoly(pvApiCtx, piAddr, &iRows, &iCols, &piNbCoef, &pdblReal, &pdblImg);
119                                 if(iRet)
120                                 {
121                                         freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg);
122                                         return iRet;
123                                 }
124
125                                 sciErr = createComplexMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
126                                 if(sciErr.iErr)
127                                 {
128                                         freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg);
129                                         printError(&sciErr, 0);
130                                         return sciErr.iErr;
131                                 }
132
133                                 freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg);
134                         }
135                         else
136                         {
137                                 iRet = getAllocatedMatrixOfPoly(pvApiCtx, piAddr, &iRows, &iCols, &piNbCoef, &pdblReal);
138                                 if(iRet)
139                                 {
140                                         freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal);
141                                         return iRet;
142                                 }
143
144                                 sciErr = createMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, iRows, iCols, piNbCoef, pdblReal);
145                                 if(sciErr.iErr)
146                                 {
147                                         freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal);
148                                         printError(&sciErr, 0);
149                                         return sciErr.iErr;
150                                 }
151
152                                 freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal);
153                         }
154                 }
155
156         AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
157         }
158         else
159         {
160         AssignOutputVariable(pvApiCtx, 1) = 0;
161         }
162         return 0;
163 }
164  ]]></programlisting>
165     </refsection>
166     <refsection>
167         <title>Scilab test script</title>
168         <programlisting role="code_scilab"><![CDATA[ 
169 a = %s;
170 b = (2 + 3*%i) * %s;
171 c = [a, 2*a, 3*a;4*a, 5*a, 6*a];
172 d = [c + c * %i];
173 if polyExample(a) <> a then pause, end
174 if polyExample(b) <> b then pause, end
175 if polyExample(c) <> c then pause, end
176 if polyExample(d) <> d then pause, end
177  ]]></programlisting>
178     </refsection>
179 </refentry>