File: examples.dia.ref

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (677 lines) | stat: -rw-r--r-- 11,271 bytes parent folder | download | duplicates (2)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
 
shared archive loaded
 
a=1:10;b=a+1;c=ones(2,3)+2;
 
 
[x,y,z,t]=ex1c('mul',a,b,c);
 
 
// Check the result
 
if norm(t-(a*2)) > %eps then bugmes();quit;end
 
if norm(z-(b*2) ) > %eps then bugmes();quit;end
 
if norm(y-(c*2)) > %eps then bugmes();quit;end
 
deff('[y]=f(i,j)','y=i+j');
 
if norm(x- ( y.* feval(0:1,0:2,f))) > %eps then bugmes();quit;end
 
 
[x,y,z,t]=ex1c('add',a,b,c);
 
 
// Check the result
 
if norm(t-(a+2)) > %eps then bugmes();quit;end
 
if norm(z-(b+2) ) > %eps then bugmes();quit;end
 
if norm(y-(c+2)) > %eps then bugmes();quit;end
 
deff('[y]=f(i,j)','y=i+j');
 
if norm(x- ( c +2 + feval(0:1,0:2,f))) > %eps then bugmes();quit;end
 
 
 
 
// Example with optional argument specified with the
 
// arg=value syntax
 
// [a,b,c] = ex2c(x1,{ v1= arg1, v2=arg2}) , arg1 default value 99
 
//					       arg2 default value 3
 
// only v1 and v2 are recognized as optional argument names
 
// the return value are a<--x1, b = 2*v2 , c = 3*v2
 
//
 
 
[a,b,c]=ex2c('test');
 
 
if norm([99*2,3*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2c('test',v1=[10,20]);
 
 
if norm([[10,20]*2,3*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2c('test',v1=[10,20],v2=8);
 
 
if norm([[10,20]*2,8*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2c('test',v2=8,v1=[10]);
 
 
if norm([10*2,8*3]-[b,c]) > %eps then bugmes();quit;end
 
 
 
 
 
 
 
 
 
//matrix (double) created by C function
 
x1=ex3c();
 
if norm(x1-matrix((1:5*3),3,5)) > %eps then bugmes();quit;end
 
 
//matrix (int) created by C function
 
x2=ex4c();
 
if norm(x2-matrix((1:5*3),3,5)) > %eps then bugmes();quit;end
 
 
//Character string created by C function
 
x3=ex5c();
 
if x3<>"Scilab is0..." then bugmes();quit;end
 
 
// all together
 
 
[y1,y2,y3]=ex6c();
 
if y1<>x3 then bugmes();quit;end
 
if norm(y2-x2) > %eps then bugmes();quit;end
 
if norm(y3-x1) > %eps then bugmes();quit;end
 
 
 
A=[1,2,3,4];
 
B=[10,20,30;40,50,60];
 
//Just display A and B
 
ex7c(A,B)
 
    10.    20.    30.  
    40.    50.    60.  
 
    1.    2.    3.    4.  
 
 
a_chain='hello';s=poly(0,'s');
 
deff('[y1,y2,y3]=myfunction(x1,x2)','y1=x1+x2,y2=1+s,y3=a_chain')
 
 
x1=1;x2=2;
 
[y1,y2,y3]=myfunction(x1,x2);
 
 
[u,v,w]=ex8c(x1,x2,myfunction);
 
 
if u-y1 > %eps then bugmes();quit;end
 
if v-y2<>0 then bugmes();quit;end
 
if w<>y3 then bugmes();quit;end
 
 
 
//a's to o's
 
x=ex9c('gaad');
character string length 4
 
if x<>'good' then bugmes();quit;end
 
//variable read in Scilab stack
 
param=1:10;
 
z=ex10c();
 
if norm(z-param) > %eps then bugmes();quit;end
 
deff('[u,v,w]=myfct(x,y)','u=7+x,v=8+y,w=9+y')
 
//ex11c executes myfct with input variables x=5 and y=6
 
x=5;y=6;[u,v,w]=myfct(x,y);
 
[p,q,r]=ex11c(1,2,3,4,5,6);
 
if p<>u | q <> v | r <> w then bugmes();quit;end
 
 
deff('p=mypoly(x)','p=poly(x,''z'',''coeffs'')')
 
c=[1,2,3];
 
P1=ex12c(2,c);
 
if P1<>poly(c*2,'z','coeffs') then bugmes();quit;end
 
a=ex13c([1,2,3]+%i*[4,5,5]);
 
if norm(a-2*[1,2,3]-%i*[4,5,5]*3) > %eps then bugmes();quit;end
 
 
a=ex13c([1,2,3]);
 
if norm(a-2*[1,2,3]) > %eps then bugmes();quit;end
 
 
 
 
 
 
// Creating the variable C
 
ex14c();
 
if C<>[10,20,30] then bugmes();quit;end
 
 
 
// Accessing a Scilab string inside an interface
 
Mystr='My string';
 
ex15c()
Mystr= "My string", length 9 
 
 
ex16c();
 
if Str<> "Pilgrim said he, where can it be this land of Eldorado" then pause;end
 
 
 
//We build a function ex17c with 3 input parameters X,Y,f and
 
//1 output parameter Z: Z=ex17c(X,Y,f).
 
//The third input parameter of ex17c (f) is a function.
 
//This function, which is passed to ex17c as an argument,
 
//is called a "argument function".
 
//This function can be a Scilab function or a C function.
 
//In both cases, this function must have a specific calling
 
//sequence which is imposed by ex17c.
 
// (A typical example is the case of an ode solver:
 
//  if we want to solve an ode xdot=f(x), we want to build a function
 
//  ode with a syntax like y=ode(x0,t,f) where x0 is a real vector,
 
//  t a real number and f the "argument function" f: x->f(x). )
 
 
//There are two ways of calling ex17c:
 
//Z=ex17c(X,Y,f) for computing Z from X and Y using the Scilab function f
 
 
//Z=ex17c(X,Y,"f") for computing Z from X and Y using the C function "f"
 
//The C function f can be dynamically linked to Scilab, using the
 
//link command (e.g. if f=f3c, link("f3c.o","f3c","C") ) or it can be put in
 
//a table of functions (see ex17c.c).
 
 
//In the example proposed here, we want to build a function ex17c with
 
//syntax Z=ex17c(X,Y,f) which returns a real matrix Z obtained from
 
//the two real input vectors X and Y by a calculation performed by
 
//f. To be specific, we want Z(i,j)=f(X(i),Y(j)). In this example,
 
//the argument function f is a function from R x R to R.
 
//The Scilab built-in function function feval does this: ex17c emulates
 
//feval.
 
 
//If f is a Scilab function, its syntax is z=f(x,y), defined for instance
 
//by:
 
//deff('z=f(x,y)','z=cos(x+y)');
 
//
 
//If f is a C function, its definition will be:
 
//int f(x,y,z)
 
//     double x,y,*z;
 
//{
 
//  *z= cos(x+y);
 
//}
 
//Note that the argument function f must be defined as above, defining f as e.g.
 
//int f(z,x,y)
 
//is not allowed.
 
 
//If X is a vector with m components and Y is a vector with n components,
 
//the resulting matrix Z=ext17(X,Y) is m x n.
 
 
//The interface function is intex17c.
 
 
//Test inputs for ex17c:
 
X=1:3;Y=4:6;
 
//...and 3 examples of Scilab argument functions:
 
deff('z=f1(x,y)','z=x+y');
 
deff('z=f2(x,y)','z=x*x+y*y');
 
deff('z=f3(x,y)','z=cos(x+y)');
 
 
//reference values for Z calculated by feval
 
Zref1=feval(X,Y,f1);
 
Zref2=feval(X,Y,f2);
 
Zref3=feval(X,Y,f3);
 
 
// Calling ex17c with a argument function which is a Scilab function:
 
Z1=ex17c(X,Y,f1);
 
if norm(Z1-Zref1) > %eps then bugmes();quit;end
 
 
Z2=ex17c(X,Y,f2);
 
if norm(Z2-Zref2) > %eps then bugmes();quit;end
 
 
Z3=ex17c(X,Y,f3);
 
if norm(Z3-Zref3) > %eps then bugmes();quit;end
 
 
// Calling ex17c with a argument function which is a C function:
 
Z1=ex17c(X,Y,'f1c');     // f1c defined in ex17c.c
 
if norm(Z1 - Zref1) > %eps then bugmes();quit;end
 
 
Z2=ex17c(X,Y,'f2c');   // f2c defined in ex17c.c
 
if norm(Z2 - Zref2) > %eps then bugmes();quit;end
 
 
// making f3c.o and linking f3c dynamically with Scilab
 
if ~c_link('f3c') then
  files=G_make(['f3c.o'],'f3c.dll');
  link(files,'f3c','C') ;
shared archive loaded
Link done
end
 
 
// Calling ex17c with the C argument function fp3
 
Z3=ex17c(X,Y,'f3c');
 
if norm(Z3 - Zref3) > %eps then bugmes();quit;end
 
 
 
 
 
 
a=1:10;b=a+1;c=ones(2,3)+2;
 
[x,y,z,t]=ex1f('mul',a,b,c);
 
 
// Check the result
 
if norm(t-(a*2)) > %eps then bugmes();quit;end
 
if norm(z-(b*2) ) > %eps then bugmes();quit;end
 
if norm(y-(c*2)) > %eps then bugmes();quit;end
 
deff('[y]=f(i,j)','y=i+j');
 
if norm(x- ( y.* feval(1:2,1:3,f))) > %eps then bugmes();quit;end
 
 
[x,y,z,t]=ex1f('add',a,b,c);
 
 
// Check the result
 
if norm(t-(a+2)) > %eps then bugmes();quit;end
 
if norm(z-(b+2) ) > %eps then bugmes();quit;end
 
if norm(y-(c+2)) > %eps then bugmes();quit;end
 
deff('[y]=f(i,j)','y=i+j');
 
if norm(x- ( c +2 + feval(1:2,1:3,f))) > %eps then bugmes();quit;end
 
 
 
 
// Example with optional argument specified with the
 
// arg=value syntax
 
// [a,b,c] = ex2f(x1,{ v1= arg1, v2=arg2}) , arg1 default value 99
 
//					       arg2 default value 3
 
// only v1 and v2 are recognized as optional argument names
 
// the return value are a<--x1, b = 2*v2 , c = 3*v2
 
//
 
 
[a,b,c]=ex2f('test');
 
 
if norm([99*2,3*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2f('test',v1=[10,20]);
 
 
if norm([[10,20]*2,3*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2f('test',v1=[10,20],v2=8);
 
 
if norm([[10,20]*2,8*3]-[b,c]) > %eps then bugmes();quit;end
 
 
[a,b,c]=ex2f('test',v2=8,v1=[10]);
 
 
if norm([10*2,8*3]-[b,c]) > %eps then bugmes();quit;end
 
 
 
 
 
 
 
 
 
 
//matrix (double) created by C function
 
x1=ex3f();
 
if norm(x1-matrix((1:5*3),3,5)) > %eps then bugmes();quit;end
 
 
//matrix (int) created by C function
 
x2=ex4f();
 
if norm(x2-matrix((1:5*3),3,5)) > %eps then bugmes();quit;end
 
 
//Character string created by C function
 
x3=ex5f();
 
if x3<>"Scilab is0..." then bugmes();quit;end
 
 
 
// all together
 
 
[y1,y2,y3]=ex6f();
 
if y1<>x3 then bugmes();quit;end
 
if norm(y2-x2) > %eps then bugmes();quit;end
 
if norm(y3-x1) > %eps then bugmes();quit;end
 
A=[1,2,3,4];
 
B=[10,20,30;40,50,60];
 
//Just display A and B
 
ex7f(A,B)
 
    10.    20.    30.  
    40.    50.    60.  
 
    1.    2.    3.    4.  
 
 
 
a_chain='hello';s=poly(0,'s');
 
deff('[y1,y2,y3]=myfunction(x1,x2)','y1=x1+x2,y2=1+s,y3=a_chain')
 
 
x1=1;x2=2;
 
[y1,y2,y3]=myfunction(x1,x2);
 
 
[u,v,w]=ex8f(x1,x2,myfunction);
 
 
if u-y1 > %eps then bugmes();quit;end
 
if v-y2<>0 then bugmes();quit;end
 
if w<>y3 then bugmes();quit;end
 
//a's to o's
 
x=ex9f('gaad');
 
if x<>'good' then bugmes();quit;end
 
//variable read in Scilab stack
 
param=1:10;
 
z=ex10f();
 
if norm(z-param) > %eps then bugmes();quit;end
 
deff('[u,v,w]=myfct(x,y)','u=7+x,v=8+y,w=9+y')
 
//ex11f executes myfct with input variables x=5 and y=6
 
x=5;y=6;[u,v,w]=myfct(x,y);
 
[p,q,r]=ex11f(1,2,3,4,5,6);
 
if p<>u | q <> v | r <> w then bugmes();quit;end
 
deff('p=mypoly(x)','p=poly(x,''z'',''coeffs'')')
Warning :redefining function: mypoly                  
 
 
c=[1,2,3];
 
P1=ex12f(2,c);
 
if P1<>poly(c*2,'z','coeffs') then bugmes();quit;end
 
 
 
 
[a]=ex13f([1,2,3]+%i*[4,5,5]);
 
if norm(a-2*[1,2,3]-%i*[4,5,5]*3) > %eps then bugmes();quit;end
 
 
[a]=ex13f([1,2,3]);
 
if norm(a-2*[1,2,3]) > %eps then bugmes();quit;end
 
 
 
 
 
 
// Creating the variable C
 
ex14f();
 
if C<>[10,20,30] then bugmes();quit;end
 
 
 
// Accessing a Scilab string inside an interface
 
Mystr='My string';
 
ex15f()
Mystr=My string  Its length is0...      
 
 
ex16f();
 
if Str<> "Pilgrim said he, where can it be this land of Eldorado" then pause;end
 
 
//Test inputs for ex17f:
 
X=1:3;Y=4:6;
 
//...and 3 examples of Scilab argument functions:
 
deff('z=f1(x,y)','z=x+y');
 
deff('z=f2(x,y)','z=x*x+y*y');
 
deff('z=f3(x,y)','z=cos(x+y)');
 
 
//reference values for Z calculated by feval
 
Zref1=feval(X,Y,f1);
 
Zref2=feval(X,Y,f2);
 
Zref3=feval(X,Y,f3);
 
 
// Calling ex17f with a argument function which is a Scilab function:
 
Z1=ex17f(X,Y,f1);
 
if norm(Z1-Zref1) > %eps then bugmes();quit;end
 
 
Z2=ex17f(X,Y,f2);
 
if norm(Z2-Zref2) > %eps then bugmes();quit;end
 
 
Z3=ex17f(X,Y,f3);
 
if norm(Z3-Zref3) > %eps then bugmes();quit;end
 
 
// Calling ex17f with a argument function which is a Fortran function:
 
Z1=ex17f(X,Y,'f1f');     // f1f defined in ex17f.f
 
if norm(Z1 - Zref1) > %eps then bugmes();quit;end
 
 
Z2=ex17f(X,Y,'f2f');   // f2f defined in ex17f.f
 
if norm(Z2 - Zref2) > %eps then bugmes();quit;end
 
 
// making f3f.o and linking f3f dynamically with Scilab
 
if ~c_link('f3f') then
  files=G_make(['f3f.o'],'f3f.dll');
  link(files,'f3f','f') ;
shared archive loaded
Link done
end
 
 
// Calling ex17f with the Fortran argument function fp3
 
Z3=ex17f(X,Y,'f3f');
 
if norm(Z3 - Zref3) > %eps then bugmes();quit;end