File: ch05s03.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-- 8,398 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>Using Functions</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Manual do Genius"><link rel="up" href="ch05.html" title="Chapter 5. GEL Basics"><link rel="prev" href="ch05s02.html" title="Using Variables"><link rel="next" href="ch05s04.html" title="Separator"></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">Using Functions</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05s02.html">Prev</a> </td><th width="60%" align="center">Chapter 5. GEL Basics</th><td width="20%" align="right"> <a accesskey="n" href="ch05s04.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-functions"></a>Using Functions</h2></div></div></div><p lang="en">
Syntax:
</p><pre lang="en" class="programlisting">FunctionName(argument1, argument2, ...)
</pre><p lang="en">
Example:
</p><pre lang="en" class="programlisting">Factorial(5)
cos(2*pi)
gcd(921,317)
</pre><p lang="en">

To evaluate a function, enter the name of the function, followed by the arguments (if any) to the function in parentheses. This will return the result of applying the function to its arguments. The number of arguments to the function is, of course, different for each function.
      </p><p lang="en">
	      There are many built-in functions, such as <a class="link" href="ch11s06.html#gel-function-sin"><code class="function">sin</code></a>, <a class="link" href="ch11s06.html#gel-function-cos"><code class="function">cos</code></a> and <a class="link" href="ch11s06.html#gel-function-tan"><code class="function">tan</code></a>. You can use the <a class="link" href="ch11.html#gel-command-help"><code class="function">help</code></a> built-in command to get a list of available functions, or see <a class="xref" href="ch11.html" title="Chapter 11. List of GEL functions">Chapter 11, <i>List of GEL functions</i></a> for a full listing.
      </p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Using Tab completion</h3><p lang="en">
You can use Tab completion to get Genius to complete function names for you. Try typing the first few letters of the name and pressing <strong class="userinput"><code>Tab</code></strong>.
        </p></div><div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Function names are case sensitive</h3><p lang="en">
The names of functions are case sensitive. That means that functions named <code class="function">dosomething</code>, <code class="function">DOSOMETHING</code> and <code class="function">DoSomething</code> are all different functions.
        </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-functions-defining"></a>Defining Functions</h3></div></div></div><p lang="en">
Syntax:
</p><pre lang="en" class="programlisting">function &lt;identifier&gt;(&lt;comma separated arguments&gt;) = &lt;function body&gt;
&lt;identifier&gt; = (`() = &lt;function body&gt;)
</pre><p lang="en">
The <code class="literal">`</code> is the backquote character, and signifies an anonymous function. By setting it to a variable name you effectively define a function.
        </p><p lang="en">
A function takes zero or more comma separated arguments, and returns the result of the function body. Defining your own functions is primarily a matter of convenience; one possible use is to have sets of functions defined in GEL files that Genius can load in order to make them available.
Example:
</p><pre lang="en" class="programlisting">function addup(a,b,c) = a+b+c
</pre><p lang="en">
then <strong class="userinput"><code>addup(1,4,9)</code></strong> yields 14
        </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-functions-variable-argument-lists"></a>Variable Argument Lists</h3></div></div></div><p lang="en">
If you include <code class="literal">...</code> after the last argument name in the function declaration, then Genius will allow any number of arguments to be passed in place of that argument. If no arguments were passed then that argument will be set to <code class="constant">null</code>. Otherwise, it will be a horizontal vector containing all the arguments. For example:
</p><pre lang="en" class="programlisting">function f(a,b...) = b
</pre><p lang="en">
Then <strong class="userinput"><code>f(1,2,3)</code></strong> yields <code class="computeroutput">[2,3]</code>, while <strong class="userinput"><code>f(1)</code></strong> yields a <code class="constant">null</code>.
        </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-functions-passing-functions"></a>Passing Functions to Functions</h3></div></div></div><p lang="en">
In Genius, it is possible to pass a function as an argument to another function. This can be done using either ‘function nodes’ or anonymous functions.
        </p><p lang="en">
If you do not enter the parentheses after a function name, instead of being evaluated, the function will instead be returned as a ‘function node’. The function node can then be passed to another function.
Example:
</p><pre lang="en" class="programlisting">function f(a,b) = a(b)+1;
function b(x) = x*x;
f(b,2)
</pre><p lang="en">
        </p><p lang="en">
To pass functions that are not defined,
you can use an anonymous function (see <a class="xref" href="ch05s03.html#genius-gel-functions-defining" title="Defining Functions">the section called “Defining Functions”</a>).  That is, you want to pass a function without giving it a name.
Syntax:
</p><pre lang="en" class="programlisting">function(&lt;comma separated arguments&gt;) = &lt;function body&gt;
`(&lt;comma separated arguments&gt;) = &lt;function body&gt;
</pre><p lang="en">
Example:
</p><pre lang="en" class="programlisting">function f(a,b) = a(b)+1;
f(`(x) = x*x,2)
</pre><p lang="en">
This will return 5.
        </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-functions-operations"></a>Operations on Functions</h3></div></div></div><p lang="en">
	      Some functions allow arithmetic operations, and some single argument functions such as <a class="link" href="ch11s05.html#gel-function-exp"><code class="function">exp</code></a> or <a class="link" href="ch11s05.html#gel-function-ln"><code class="function">ln</code></a>, to operate on the function. For example,
</p><pre lang="en" class="programlisting">exp(sin*cos+4)
</pre><p lang="en">
will return a function that takes <code class="varname">x</code> and returns <strong class="userinput"><code>exp(sin(x)*cos(x)+4)</code></strong>.  It is functionally equivalent
to typing
</p><pre lang="en" class="programlisting">`(x) = exp(sin(x)*cos(x)+4)
</pre><p lang="en">

This operation can be useful when quickly defining functions. For example to create a function called <code class="varname">f</code>
to perform the above operation, you can just type:
</p><pre lang="en" class="programlisting">f = exp(sin*cos+4)
</pre><p lang="en">
It can also be used in plotting. For example, to plot sin squared you can enter:
</p><pre lang="en" class="programlisting">LinePlot(sin^2)
</pre><p lang="en">
      </p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Warning</h3><p lang="en">
Not all functions can be used in this way.  For example, when you use a binary operation the functions must take the same number of arguments. 
        </p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch05.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch05s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Using Variables </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Separator</td></tr></table></div></body></html>