File: stack3.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (412 lines) | stat: -rw-r--r-- 12,279 bytes parent folder | download
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
/*------------------------------------------------------------------------
 *    Graphic library
 *    Copyright (C) 1998-2000 Enpc/Inria 
 *    jpc@cereve.enpc.fr 
 --------------------------------------------------------------------------*/
/*------------------------------------------------------
 * Read and Write inside the Scilab stack 
 *------------------------------------------------------*/

#include "../stack-c.h"

extern int C2F(dmcopy)  __PARAMS((double *a, integer *na, double *b, integer *nb, integer *m, integer *n));
extern int C2F(stackg)  __PARAMS((integer *id));
extern int C2F(stackp)  __PARAMS((integer *id, integer *macmod));

/* Table of constant values */

static integer cx0 = 0;
static integer cx1 = 1;

/*------------------------------------------------------
 * read a matrix 
 *------------------------------------------------------*/

int C2F(readmat)(namex, m, n, scimat, name_len)
     char *namex;
     integer *m, *n;
     double *scimat;
     unsigned long name_len;
{
    int j;
    j = C2F(creadmat)(namex, m, n, scimat, name_len);
    return 0;
}

/*----------------------------------------------------------------
 * readmat reads vector/matrix in scilab's internal stack 
 * calling sequence 
 *     logic=creadmat('matrixname',m,n,scimat) 
 *  matrixname: character string; name of the scilab variable. 
 *  m: number of rows (output of readmat) 
 *  n: number of columns (output of readmat) 
 *  scimat: matrix entries stored columnwise (output of readmat) 
 *    Example of use: 
 *    Amat is a real 2 x 3 scilab matrix 
 *    your subroutine should be as follows: 
 *    subroutine mysubr(...) 
 *    ... 
 *    call readmat('Amat',m,n,scimat) 
 *    => m=3 , n=2, and scimat(1)=Amat(1,1) 
 *                      scimat(2)=Amat(2,1) 
 *                      scimat(3)=Amat(3,1) 
 *                      scimat(4)=Amat(1,2) ... 
 *                      scimat(5)=Amat(3,2) 
 *----------------------------------------------------------------*/

int C2F(creadmat)(namex, m, n, scimat, name_len)
     char *namex;
     integer *m, *n;
     double *scimat;
     unsigned long name_len;
{
    integer l;
    integer id[nsiz];

    C2F(str2name)(namex, id, name_len);
    /* read   : from scilab stack -> fortran variable */
    Fin = -1;
    C2F(stackg)(id);
    if (Err > 0) return FALSE_ ; 
    if (Fin == 0) {
      Scierror(4,"Undefined variable %s\r\n",get_fname(namex,name_len));
      return FALSE_;
    }
    if ( *infstk(Fin ) == 2)  Fin = *istk(iadr(*lstk(Fin )) + 1 +1);
    /* get matrix data pointer */
    if (! C2F(getrmat)("creadmat", &Fin, &Fin, m, n, &l, 8L)) 	return FALSE_;

    C2F(dmcopy)(stk(l ), m, scimat, m, m, n);

    return TRUE_;
}

/*----------------------------------------------------------------
 * cwritemat writes vector/matrix in scilab's internal stack
 * logic=cwritemat('matrixname'//char(0),m,n,mat)
 * name: character string; name of the scilab variable ( null terMinated) 
 * m: number of rows 
 *  n: number of columns 
 *  mat: matrix entries stored columnwise in Scilab object 
 *----------------------------------------------------------------*/

int C2F(cwritemat)(namex, m, n, mat, name_len)
     char *namex;
     integer *m, *n;
     double *mat;
     unsigned long name_len;
{
  integer   ix1 = *m * *n;
  integer Rhs_k = Rhs , Top_k = Top ;
  integer l4, id[nsiz], lc, lr;
  
  C2F(str2name)(namex, id, name_len);
  ++Top;
  if (! C2F(cremat)("cwritemat", &Top, &cx0, m, n, &lr, &lc, 9L)) return  FALSE_;
  C2F(dcopy)(&ix1, mat, &cx1, stk(lr ), &cx1);
  Rhs = 0;
  l4 = C2F(iop).lct[3];
  C2F(iop).lct[3] = -1;
  C2F(stackp)(id, &cx0);
  C2F(iop).lct[3] = l4;
  Top = Top_k;
  Rhs = Rhs_k;
  if (Err > 0)  return FALSE_;
  return TRUE_;
} 

/*------------------------------------------------------
 *     see creadchain 
 *------------------------------------------------------*/

int C2F(readchain)(namex, itslen, chai, name_len, chai_len)
     char *namex;
     integer *itslen;
     char *chai;
     unsigned long name_len, chai_len;
{
    int j;
    j = C2F(creadchain)(namex, itslen, chai, name_len, chai_len);
    return 0;
} 

/*------------------------------------------------------
 *     this routine reads a string in scilab's  memory 
 *     and store it into chai 
 * !calling sequence 
 *     integer       itslen 
 *     character*(*) chai,name 
 *     name    : character string = name of scilab variable (input) 
 *     chai    : chain to be read (output) 
 *               null terMinated 
 *     itslen  : (input) Maximum number of character that can ne stored 
 *               in chain 
 *               (output) number of copied characters into chai 
 *     if Scilab variable x='qwert' exists 
 *     character ch*(10) 
 *     l=10 
 *     logic= creadchain('x',l,ch) returns l=5 and ch='qwert' 
 *------------------------------------------------------*/

int C2F(creadchain)(namex, itslen, chai, name_len, chai_len)
     char *namex;
     integer *itslen;
     char *chai;
     unsigned long name_len;
     unsigned long chai_len;
{
    integer ix1;
    integer m1, n1;
    integer id[nsiz];
    integer lr1;
    integer nlr1;

    Err = 0;
    C2F(str2name)(namex, id, name_len);
    Fin = -1;
    C2F(stackg)(id);
    if (Err > 0) return FALSE_ ;
    if (Fin == 0) {
      Scierror(4,"Undefined variable %s\r\n",get_fname(namex,name_len));
      return FALSE_ ;
    }
    if (*infstk(Fin ) == 2) {
	Fin = *istk(iadr(*lstk(Fin )) + 1 +1);
    }
    if (! C2F(getsmat)("creadchain", &Fin, &Fin, &m1, &n1, &cx1, &cx1, &lr1, &nlr1, 10L)) {
	return FALSE_;
    }
    if (m1 * n1 != 1) {
      Scierror(999,"creadchain: argument must be a string\r\n");
      return FALSE_ ;
    }

    ix1 = *itslen - 1;
    *itslen = Min(ix1,nlr1);
    C2F(cvstr)(itslen, istk(lr1 ), chai, &cx1, chai_len);
    chai[*itslen] = '\0';
    return TRUE_ ;
}

/*----------------------------------------------------------------------
 *     this routine reads name(ir,ic) in scilab's  memory 
 *     and store it into chai 
 *     if ir=ic=-1 on entry then the routines returns in ir,ic 
 *     the size of the matrix 
 * !calling sequence 
 *     integer       itslen 
 *     character*(*) chai,name 
 *     name    : character string = name of scilab variable (input) 
 *     chai    : chain to be read (output) 
 *               null terMinated 
 *     itslen  : (input) Maximum number of character that can be stored 
 *               in chain 
 *               (output) number of copied characters into chai 
 *     if Scilab variable x='qwert' exists 
 *     character ch*(10) 
 *     l=10 
 *     logic= creadchain('x',l,ch) returns l=5 and ch='qwert' 
 *----------------------------------------------------------------------*/


int C2F(creadchains)(namex, ir, ic, itslen, chai, name_len, chai_len)
     char *namex;
     integer *ir, *ic, *itslen;
     char *chai;
     unsigned long name_len;
     unsigned long chai_len;
{
    integer ix1;
    integer m1, n1;
    integer id[nsiz];
    integer lr1;
    integer nlr1;

    Err = 0;
    C2F(str2name)(namex, id, name_len);
    Fin = -1;
    C2F(stackg)(id);
    if (Err > 0) return FALSE_ ;

    if (Fin == 0) {
      Scierror(4,"Undefined variable %s\r\n",get_fname(namex,name_len));
      return FALSE_ ;
    }

    if (*infstk(Fin ) == 2) {
	Fin = *istk(iadr(*lstk(Fin )) + 1 +1);
    }
    if (*ir == -1 && *ic == -1) {
	if (! C2F(getsmat)("creadchain", &Fin, &Fin, ir, ic, &cx1, &cx1, &lr1, &nlr1, 10L)) 
	  return FALSE_;
	else 
	  return TRUE_ ; 
    } else {
	if (! C2F(getsmat)("creadchain", &Fin, &Fin, &m1, &n1, ir, ic, &lr1, &nlr1, 10L)) {
	  return FALSE_;
	}
    }
    ix1 = *itslen - 1;
    *itslen = Min(ix1,nlr1);
    C2F(cvstr)(itslen, istk(lr1 ), chai, &cx1, chai_len);
    chai[*itslen]='\0';
    return TRUE_;
}

/*----------------------------------------------------------------
 *     cwritemat writes vector/matrix in scilab's internal stack 
 *     logic=cwritemat('matrixname'//char(0),m,n,mat) 
 *  name: character string; name of the scilab variable ( null terMinated) 
 *  m: number of rows 
 *  n: number of columns 
 *  mat: matrix entries stored columnwise in Scilab object 
 *----------------------------------------------------------------*/

int C2F(cwritechain)(namex, m, chai, name_len, chai_len)
     char *namex;
     integer *m;
     char *chai;
     unsigned long name_len,  chai_len;
{
    integer Rhs_k, Top_k;
    integer l4;
    integer id[nsiz], lr;
    C2F(str2name)(namex, id, name_len);
    Top_k = Top;
    ++Top;
    if (! C2F(cresmat2)("cwritechain", &Top, m, &lr, 11L)) {
	return FALSE_;
    }
    C2F(cvstr)(m, istk(lr ), chai, &cx0, chai_len);
    Rhs_k = Rhs;
    Rhs = 0;
    l4 = C2F(iop).lct[3];
    C2F(iop).lct[3] = -1;
    C2F(stackp)(id, &cx0);
    C2F(iop).lct[3] = l4;
    Top = Top_k ;
    Rhs = Rhs_k ;
    if (Err > 0)  return FALSE_;
    return TRUE_ ;
}

/*----------------------------------------------------------------
 *     see cmatptr 
 *----------------------------------------------------------------*/

int C2F(matptr)(namex, m, n, lp, name_len)
     char *namex;
     integer *m, *n, *lp;
     unsigned long name_len;
{
    int ix;
    ix = C2F(cmatptr)(namex, m, n, lp, name_len);
    return 0;
} 

/*----------------------------------------------------------------
 * !purpose 
 *     matptr returns the adress of real matrix "name" 
 *     in scilab's internal stack 
 *     m=number of rows 
 *     n=number of columns 
 *     stk(lp),stk(lp+1),...,stk(lp+m*n-1)= entries (columnwise) 
 *     If matrix "name" not in Scilab stack, returns m=n=-1. 
 *    Example of use: 
 *    Amat is a real 2 x 3 scilab matrix 
 *    your subroutine should be as follows: 
 *    subroutine mysubr(...) 
 *    ... 
 *    logic= cmatptr('Amat',m,n,lp) 
 *    => m=3 , n=2, and stk(lp)=Amat(1,1) 
 *                      stk(lp+1)=Amat(2,1) 
 *                      stk(lp+2)=Amat(3,1) 
 *                      stk(lp+3)=Amat(1,2) ... 
 *                      stk(lp+5)=Amat(3,2) 
 *   see example in fydot.f file 
 *   see also  readmat.f, matz.f 
 *----------------------------------------------------------------*/

int C2F(cmatptr)(namex, m, n, lp, name_len)
     char *namex;
     integer *m, *n, *lp;
     unsigned long name_len;
{
    integer id[nsiz];
    C2F(str2name)(namex, id, name_len);
    /* get the position in fin */
    Fin = -1;
    C2F(stackg)(id);
    if (Fin == 0) {
      Scierror(4,"Undefined variable %s\r\n",get_fname(namex,name_len));
      *m = -1;
      *n = -1;
      return FALSE_;
    }
    /* get data */
    if (*infstk(Fin ) == 2) {
	Fin = *istk(iadr(*lstk(Fin )) + 1 +1);
    }
    if (! C2F(getrmat)("creadmat", &Fin, &Fin, m, n, lp, 8L)) {
	return FALSE_;
    }
    return TRUE_ ;
}

/*----------------------------------------------------------------
 *     string conversion to Scilab ID 
 *     Warning : the character name is null terMinated 
 *             and len(name) is not used 
 *             since it can be wrong (ex when name is transmited 
 *             by fort (intfort : function )
 *----------------------------------------------------------------*/

int C2F(str2name)(namex, id, name_len)
     char *namex;
     integer *id;
     unsigned long name_len;
{
    integer ix;
    integer lon;
    lon = 0;
    for (ix = 0 ; ix < name_len ; ix++ ) {
      if ( namex[ix] == '\0') break;
      ++lon;
    }
    C2F(cvname)(id, namex, &cx0, lon);
    return 0;
} 

/*----------------------------------------------------------------
 *     objptr returns the adress of "name" 
 *     in scilab's internal stack 
 *----------------------------------------------------------------*/

int C2F(objptr)(namex, lp, name_len)
     char *namex;
     integer *lp;
     unsigned long name_len;
{
    integer id[nsiz];
    *lp = 0;
    /*     ---- get the id */
    C2F(str2name)(namex, id, name_len);
    /*     ---- get the position in fin */
    Fin = -1;
    C2F(stackg)(id);
    if (Fin == 0) {
      C2F(putid)(&C2F(recu).ids[(C2F(recu).pt + 1) * nsiz - nsiz], id);
      /*         we juste return false and lp is set to zero */
      /*         call error(4) */
      return FALSE_;
    }
    *lp = *lstk(Fin );
    if (*infstk(Fin ) == 2) {
	*lp = *lstk(*istk(iadr(*lp) + 1 +1) );
    }
    return  TRUE_;
}