File: ch12.html

package info (click to toggle)
genius 1.0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 25,308 kB
  • sloc: ansic: 75,620; xml: 71,565; sh: 4,445; makefile: 1,927; lex: 523; yacc: 298; perl: 54
file content (74 lines) | stat: -rw-r--r-- 4,187 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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 12. Example Programs in GEL</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Manual do Genius"><link rel="up" href="index.html" title="Manual do Genius"><link rel="prev" href="ch11s20.html" title="Plotagem"><link rel="next" href="ch13.html" title="Chapter 13. Preferências"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 12. Example Programs in GEL</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch11s20.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch13.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="genius-gel-example-programs"></a>Chapter 12. Example Programs in GEL</h1></div></div></div><p lang="en">
Here is a function that calculates factorials:
</p><pre lang="en" class="programlisting">function f(x) = if x &lt;= 1 then 1 else (f(x-1)*x)
</pre><p lang="en">
    </p><p lang="en">
With indentation it becomes:
</p><pre lang="en" class="programlisting">function f(x) = (
  if x &lt;= 1 then
    1
  else
    (f(x-1)*x)
)
</pre><p lang="en">
    </p><p lang="en">
This is a direct port of the factorial function from the <span class="application">bc</span> manpage. The syntax seems similar to <span class="application">bc</span>, but different in that in GEL, the last expression is the one that is returned. Using the <code class="literal">return</code> function instead, it would be:
</p><pre lang="en" class="programlisting">function f(x) = (
  if (x &lt;= 1) then return (1);
  return (f(x-1) * x)
)
</pre><p lang="en">
    </p><p lang="en">
By far the easiest way to define a factorial function would be using
the product loop as follows.  This is not only the shortest and fastest,
but also probably the most readable version.
</p><pre lang="en" class="programlisting">function f(x) = prod k=1 to x do k
</pre><p lang="en">
    </p><p lang="en">
Here is a larger example, this basically redefines the internal
<a class="link" href="ch11s09.html#gel-function-ref"><code class="function">ref</code></a> function to calculate the row echelon form of a
matrix.  The function <code class="function">ref</code> is built in and much faster,
but this example demonstrates some of the more complex features of GEL.
</p><pre lang="en" class="programlisting"># Calculate the row-echelon form of a matrix
function MyOwnREF(m) = (
  if not IsMatrix(m) or not IsValueOnly(m) then
    (error("MyOwnREF: argument not a value only matrix");bailout);
  s := min(rows(m), columns(m));
  i := 1;
  d := 1;
  while d &lt;= s and i &lt;= columns(m) do (

    # This just makes the anchor element non-zero if at
    # all possible
    if m@(d,i) == 0 then (
      j := d+1;
      while j &lt;= rows(m) do (
        if m@(j,i) == 0 then
          (j=j+1;continue);
        a := m@(j,);
        m@(j,) := m@(d,);
        m@(d,) := a;
        j := j+1;
        break
      )
    );
    if m@(d,i) == 0 then
      (i:=i+1;continue);
    
    # Here comes the actual zeroing of all but the anchor
    # element rows
    j := d+1;
    while j &lt;= rows(m)) do (
      if m@(j,i) != 0 then (
        m@(j,) := m@(j,)-(m@(j,i)/m@(d,i))*m@(d,)
      );
      j := j+1
    );
    m@(d,) := m@(d,) * (1/m@(d,i));
    d := d+1;
    i := i+1
  );
  m
)
</pre><p lang="en">
    </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch11s20.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch13.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Plotagem </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 13. Preferências</td></tr></table></div></body></html>