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 (64 lines) | stat: -rw-r--r-- 8,413 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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Använda funktioner</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Handbok för Genius"><link rel="up" href="ch05.html" title="Chapter 5. Grunderna i GEL"><link rel="prev" href="ch05s02.html" title="Använda variabler"><link rel="next" href="ch05s04.html" title="Avskiljare"></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">Använda funktioner</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05s02.html">Prev</a> </td><th width="60%" align="center">Chapter 5. Grunderna i GEL</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>Använda funktioner</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>Det finns många inbyggda funktioner, som <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> och <a class="link" href="ch11s06.html#gel-function-tan"><code class="function">tan</code></a>. Du kan använda det inbyggda kommandot <a class="link" href="ch11.html#gel-command-help"><code class="function">help</code></a> för att få en lista över tillgängliga funktioner, eller se <a class="xref" href="ch11.html" title="Chapter 11. Lista över GEL-funktioner">Chapter 11, <i>Lista över GEL-funktioner</i></a> för en fullständig lista.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Använda tabbkomplettering</h3><p>Du kan använda tabbkomplettering för att få Genius att komplettera funktionsnamn åt dig. Skriv in de första bokstäverna i namnet och tryck <strong class="userinput"><code>Tabb</code></strong>.</p></div><div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Funktionsnamn är skiftlägeskänsliga</h3><p>Namnen på funktioner är skiftlägeskänsliga. Detta betyder att funktionerna med namnen <code class="function">arbeta</code>, <code class="function">ARBETA</code> och <code class="function">Arbeta</code> alla är olika funktioner.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-functions-defining"></a>Definiera funktioner</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>Variabla argumentlistor</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>Skicka funktioner till funktioner</h3></div></div></div><p>I Genius är det möjligt att skicka en funktion som ett argument till en annan funktion. Detta kan antingen göras med ”funktionsnoder” eller anonyma funktioner.</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="Definiera funktioner">the section called “Definiera funktioner”</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>Operationer på funktioner</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>Alla funktioner kan inte användas på detta sätt. Då du till exempel använder en binär operation måste funktionerna ta samma antal argument.</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">Använda variabler </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Avskiljare</td></tr></table></div></body></html>