File: ch05s06.html

package info (click to toggle)
genius 1.0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • 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 (50 lines) | stat: -rw-r--r-- 4,397 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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Modular Evaluation</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="ch05s05.html" title="Comments"><link rel="next" href="ch05s07.html" title="List of GEL Operators"></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">Modular Evaluation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05s05.html">Prev</a> </td><th width="60%" align="center">Chapter 5. GEL Basics</th><td width="20%" align="right"> <a accesskey="n" href="ch05s07.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-modular-evaluation"></a>Modular Evaluation</h2></div></div></div><p lang="en">
	      Genius implements modular arithmetic.
To use it you just add "mod &lt;integer&gt;" after
the expression.  Example:
<strong class="userinput"><code>2^(5!) * 3^(6!) mod 5</code></strong>
It could be possible to do modular arithmetic by computing with integers and then modding in the end with
the <code class="literal">%</code> operator, which simply gives the remainder, but
that may be time consuming if not impossible when working with larger numbers.
For example, <strong class="userinput"><code>10^(10^10) % 6</code></strong> will simply not work (the exponent
will be too large), while
<strong class="userinput"><code>10^(10^10) mod 6</code></strong> is instantaneous.  The first expression first tries to compute the integer
<strong class="userinput"><code>10^(10^10)</code></strong> and then find remainder after division by 6, while the second expression evaluates
everything modulo 6 to begin with.
      </p><p lang="en">
	      The inverses of numbers mod some integer are computed by writing them as
rational numbers (as long as the desired inverse exists, of course).
Examples:
</p><pre lang="en" class="programlisting">10^-1 mod 101
1/10 mod 101</pre><p lang="en">
Modular evaluation also works with matrices including taking inverses,
powers, and dividing.
Example:
</p><pre lang="en" class="programlisting">A = [1,2;3,4]
B = A^-1 mod 5
A*B mod 5</pre><p lang="en">
This should yield the identity matrix as B will be the inverse of A mod 5.
      </p><p lang="en">
Some functions such as
<a class="link" href="ch11s05.html#gel-function-sqrt"><code class="function">sqrt</code></a> or
<a class="link" href="ch11s05.html#gel-function-log"><code class="function">log</code></a>
work in a different way when in modulo mode.  These will then work like their
discrete versions working within the ring of integers you selected.  For
example:
</p><pre lang="en" class="programlisting">genius&gt; sqrt(4) mod 7
=
[2, 5]
genius&gt; 2*2 mod 7
= 4</pre><p lang="en">
	<code class="function">sqrt</code> will actually return all the possible square
	roots.
      </p><p lang="en">
	      Do not chain mod operators, simply place it at the end of the computation, all computations in the expression on the left
	      will be carried out in mod arithmetic.  If you place a mod inside
	      a mod, you will get unexpected results.  If you simply want to
	      mod a single number and control exactly when remainders are
	      taken, best to use the <code class="literal">%</code> operator.  When you
	      need to chain several expressions in modular arithmetic with
	      different divisors, it may be best to just split up the expression into several and use
	      temporary variables to avoid a mod inside a mod.
      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05s05.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="ch05s07.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Comments </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> List of GEL Operators</td></tr></table></div></body></html>