File: obrien.html

package info (click to toggle)
lg-issue26 5-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,456 kB
  • ctags: 232
  • sloc: makefile: 36; sh: 4
file content (384 lines) | stat: -rw-r--r-- 12,377 bytes parent folder | download | duplicates (4)
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
<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<title>Yorick LG Issue 26</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#A000A0"
ALINK="#FF0000">
<!--endcut ============================================================-->

<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>

<P> <HR> <P> 
<!--===================================================================-->

<center>
<H2>The Yorick Programming Language</H2>
<H4>By <a href="mailto:cobrien@optimsys.com">Cary O'Brien</a></H4>
</center>
<P> <HR> <P>  
<H3>Contents:</H3>
<ul>
<li><a HREF="./obrien.html#intro">1.  Introduction</A> 
<li><a HREF="./obrien.html#basic">2. Basic operations</A> 
<li><a HREF="./obrien.html#prog">3. Programming</A> 
<li><a HREF="./obrien.html#array">4. Advanced Array Operations</A> 
<li><a HREF="./obrien.html#graphics">5. Graphics Operations</A> 
<li><a HREF="./obrien.html#final">6. Closing Remarks</A> 
<li><a HREF="./obrien.html#resource">7. Additional Resources</A> 
</ul>
<P> <HR> <P> 

<A name="intro"></A> 
<H3>1. Introduction</H3> 
<P>
Linux leverages a vast amount of academic software, either easy ports
of existing Unix packages or, increasingly in recent years, software
that is already ready to run under Linux.
One example is Yorick, and this article is an attempt to 
provide a brief overview of the nature and capabilities
of this system.
<P>
Yorick is not just another calculator -- readable syntax, array
notation, and powerful IO and graphics capabilities make Yorick a
favorite tool for scientific numerical analysis.  Machine-independent
IO, using the standard NetCDF file formats, simplifies moving
applications between hardware architectures.
<P>
Yorick is an interpreted language for numerical analysis, developed
at Livermore Labs by David H. Munro.  Implemented in C, it is freely 
distributed under a liberal copyright.  Yorick runs on a vast
range of machines, from 486SX Linux Laptops (in my case) to Cray YMP
supercomputers.  
<P> 
Who uses Yorick? The majority of users are physicists, 
many with access to the most powerful computers in the world.
Specific applications include Astrophysics, Astronomy, Neurosciences, 
Medical Image Processing and Fusion Research.  
<P>
In this article I will discuss the basics of running Yorick, describe
the key array operations, and briefly discuss array operations,  programming,
and graphics.  I hope that this quick look is enough to get the more
mathematically inclined of you to give Yorick a try.

<P> <HR> <P> 
<A name="basic"></A> 
<H3>2. Basic operations.</H3> 
<P>
	When invoked without arguments, Yorick presents a typical
	command line interface.  Expressions are evaluated
	immediately, and the result is displayed.  Primitive
	types include integers, floating point values, and strings, and
	all the built-in functions and constants you would expect are 
	present. 
<P>
	Variable names are unadorned -- no leading $, and need not
	be pre-declared, and C style comments are supported.  
<P>
<H4>Arrays</H4>
<P>
	One might not expect an interpreted language to be suitable for
	numerical analysis -- and indeed this would be the case if arrays
	were not built into the language.  Arrays are first-class objects
	that can be operated on with a single operation.  Since the
	virtual machine understands arrays, it can apply optimized 
	compiled subroutines to array operations eliminating the speed
	penalty of the interpreter.
<P>
	Arrays can be created explicitly:
<pre>
	> a1 = [1.1, 1.2, 1.3, 1.4, 1,5]
</pre>
	And elements can be accessed singly or as a subset, with 1 being
	the origin.  Parenthesis indicate the indexing operation, and
	a single index or a range of indexes can be specified.
<pre>
> a1
[1.1,1.2,1.3,1.4,1,5]
> a1(2)
1.2
> a1(1:3)
[1.1,1.2,1.3]
</pre>
	Since array operations are built into the language, functions
	applied to the array are automatically applied to all elements
	at once.  
<pre>
> sqrt(a1)
[1.04881,1.09545,1.14018,1.18322,1,2.23607]
</pre>
	Arrays are not limited in dimension.  The rank (number of indices) of
    an array is not limited to one (a vector) or two (a matrix), but can
    be as large as desired.  Arrays of rank 3 can be used to represent
    the distribution of a parameter across a volume, and an array of
    rank 4 could model this over time.  
<P>
<H4>On-line Help</H4> 
<P>
    Yorick also provides a simple but effective help system.  Executing
    the help command describes the help system.  Executing it with
    a command name as an argument provides information on that command.

<P> <HR> <P> 
<A name="prog"></A> 
<H3>3. Programming</H3> 
<P>
Yorick provides a complete programming language that closely matches C
in terms of control flow, expressions, and variable usage. For
example,
<pre>
> for(i=1; i&lt;10; i++) { print,1&lt;&lt;i; }
</pre>
print the powers of two just as you would expect, and function
declarations, introduced with func, work as expected.
<pre>
> func csc(x) {
>    return 1/sin(x);
> }
</pre>
There are differences -- variables need not be declared, and arrays
are much more powerful than C.  The major difference is in function
invocation.  Passing arguments to a function in parenthesis causes an
evaluation and printing of the result, however passing arguments
separated by commas simply executes the function and does not return
the result.  Since in most cases intermediate results are not
required, many scripts contain function calls of the form f,x,y rather
than the more familiar f(x,y).
<P>
Having a programming language close to C allows easy migration between
Yorick for prototyping and C for final implementation.  However, as
several Yorick users have indicated, moving to C was often unnecessary
-- the Yorick program proved to be fast enough to get the job done,
and with a minimum of programming effort.
<P>
If C extensions are required, a straightforward framework allows
extending the Yorick command language with whatever new operations
are necessary.

<P> <HR> <P> 
<A name="array"></A> 
<H3>4. Advanced Array Operations</H3> 
<P>
Now things get really interesting.  Yorick has a compact
and sophisticated mechanism for describing array indexing
and operations, which are used to precisely specify the
desired operation to the interpreter.
<P>
As you recall, applying an operation to an array 
causes the operation to be applied to each element
of the array.  For example
<pre>
  a = [1,2,3,4,5]
  sqrt(a)
[1,1.41421,1.73205,2,2.23607]
</PRE>  
<P>
But what about multiplying two vectors?  The default
is to perform an element by element by multiplication.
<pre>
  b = [2,4,6,8,10]
  a*b
[2,8,18,32,50]
</PRE>  
<P>
But those of you that remember physics or linear algebra 
will recall the inner and outer products.  The inner
product is defined as the sum of the pairwise products:
<pre>
  a(+)*b(+)
110
</PRE> 
And the outer product creates a matrix out of each possible
multiplication
<pre>
  a(-,)*b(,-)

[[2,4,6,8,10],
 [4,8,12,16,20],
 [6,12,18,24,30],
 [8,16,24,32,40],
 [10,20,30,40,50]]
</PRE> 
<P>
The + and - symbols used where an index would be are
called special-subscripts, and provide precise control
over how array operations are executed.
<P>
The + is the matrix multiplication pseudo-index, which indicates
to Yorick along which dimension the addition part of a 
matrix multiply should be performed.
<P>
The - is a pseudo-index, creating an index where one
did not exist before.
<P>
<H4>Rank-Reducing Operators:</H4> 
<P>
The operators sum, min, max, and avg can be used
in place of indexes.
<pre>
  a(max)
5
  b(avg)
6
</PRE> 
<P>
One might wonder why this is necessary, when the equivalent
functional operators (i.e. min() or avg()) exist?  The reason
is that for matrices of rank more than one, the rank-reducing
index operators allow you to specify exactly how to perform
the operation.  For example, given a 3x3 array, to you want
to average across rows, columns, or the entire array?
<pre>
  c = [[1,2,3],[4,5,6],[7,8,9]]
  dimsof(c)
[2,3,3]
  avg(c)
5
  c(avg,avg)
5
  c(avg,)
[2,5,8]
  c(,avg)
[4,5,6]
</PRE> 
<P>
Here we have also introduced the dimsof() operator, which
reports the dimensions of the argument.  In this case the
result tells us that c is an array of rank 2, with 3 elements
in each direction.

<P> <HR> <P> 
<A name="graphics"></A> 
<H3>5. Graphics Operations</H3> 
<P>
Under Linux, Yorick is linked with the GIST graphics
subsystem, allowing immediate display of plots and
diagrams.  Plots are interactive, allowing the user
to zoom in and out, stretch axes, and crop the
displays using the mouse.  Yorick is capable of
displaying sequences of plots over time as a move, 
and because of this the command to prepare for
a new image is fma, or frame advance.  Onward.
<P>
To plot the value of a function at evenly spaced
points we need to first create the x values.
<pre>
  x = span(0,10,256)
  dimsof(x)
[1,256]
</PRE> 
<P>
X is now a 256 element long array with values that range
from 0 to 10.
<P>
The plg function, given vectors for the x and y
values, plots x-y graphs.  
<pre>
 plg, sin(x^2), x
</PRE> 
<img src="./gx/obrien/2184p1.gif">
<P> 
<center><H4>Plot 1</H4> </center>
<P>
The result is shown in Plot 1.  Note that the arguments
are supplied y,x (not x,y).  This allows Yorick to supply
a default x vector (ranging from 1 to the number of y points)
if desired.
<P>
Parametric plots are also supported.  Consider the
following code which produces the spiral in Plot 2:
<P>
<pre>
 window, style="vgbox.gs"
 a = span(0,20,256)
 x = a * sin(a)
 y = a * cos(a)
 plg, y, x
</PRE> 
<img src="./gx/obrien/2184p2.gif">
<P> 
<center><H4>Plot 2</H4> </center>
<P>
Surface plots are also available, either as a wire
frame as in Plot 3.
<pre>
 #include "plwf.i"
 orient3
 x = span(-pi,pi,32)(,-:1:32)
 y = transpose(x)
 fma
 plwf, sin(x)*cos(y)
</PRE> 
<img src="./gx/obrien/2184p3.gif">
<P> 
<center><H4>Plot 3</H4> </center>
<P>
Or a shaded surface rendition as in Plot 4:
<PRE> 
 fma
 plwf, sin(x)*cos(y), shade=1, edges=0
</PRE> 
<img src="./gx/obrien/2184p4.gif">
<P> 
<center><H4>Plot 4</H4> </center>
<P>
A host of advanced graphics options are used in the 
demonstration programs distributed with Yorick, and
the latest copy of the documentation has an extensive
description of graphics options.  In addition, libraries
to read, write, and display PNM format images is provided.

<P> <HR> <P> 
<A name="final"></A> 
<H3>6. Closing Remarks</H3> 
<P>
Yorick is an exceptionally rich environment for numerical
analysis.   Many capabilities such as file IO, debugging,
animation, and distributed operation using MPY have been
omitted.  If you are at all interested please take the
time to read through the documentation and the example
programs.  You will not be disappointed.

<P> <HR> <P> 
<A name="resource"></A> 
<H3>7. Additional Resources</H3> 
<P>
Yorick Home Page:<BR> 
<A HREF="ftp://ftp-icf.llnl.gov/pub/Yorick/yorick-ad.html">
ftp://ftp-icf.llnl.gov/pub/Yorick/yorick-ad.html</A> 
<P>
Yorick Archive Sites:<BR>      
<A
HREF="ftp://sunsite.unc.edu/pub/Linux/apps/math/matrix/yorick-1.4.src.tar.gz">
ftp://sunsite.unc.edu/pub/Linux/apps/math/matrix/yorick-1.4.src.tar.gz</A> 
<P>
Linux Software for Scientists (and Curious Layfolk):<BR> 
<A HREF="http://www-ocean.tamu.edu/~baum/linuxlist.html">
http://www-ocean.tamu.edu/~baum/linuxlist.html</A> 
<P>
Scientific Applications under Linux:<BR> 
<A HREF="http://SAL.KachineTech.COM/">
http://SAL.KachineTech.COM/</A> 

<!--===================================================================-->
<P> <hr> <P> 
<center><H5>Copyright &copy; 1998, Cary O'Brien <BR> 
Published in Issue 26 of <i>Linux Gazette</i>, March 1998</H5></center>

<!--===================================================================-->
<P> <hr> <P> 
<A HREF="./lg_toc26.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif" 
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../lg_frontpage.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="./kunz.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="./lg_backpage26.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P> 
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->