| 1 |
<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"> |
| 2 |
Genius implements modular arithmetic. |
| 3 |
To use it you just add "mod <integer>" after |
| 4 |
the expression. Example: |
| 5 |
<strong class="userinput"><code>2^(5!) * 3^(6!) mod 5</code></strong> |
| 6 |
It could be possible to do modular arithmetic by computing with integers and then modding in the end with |
| 7 |
the <code class="literal">%</code> operator, which simply gives the remainder, but |
| 8 |
that may be time consuming if not impossible when working with larger numbers. |
| 9 |
For example, <strong class="userinput"><code>10^(10^10) % 6</code></strong> will simply not work (the exponent |
| 10 |
will be too large), while |
| 11 |
<strong class="userinput"><code>10^(10^10) mod 6</code></strong> is instantaneous. The first expression first tries to compute the integer |
| 12 |
<strong class="userinput"><code>10^(10^10)</code></strong> and then find remainder after division by 6, while the second expression evaluates |
| 13 |
everything modulo 6 to begin with. |
| 14 |
</p><p lang="en"> |
| 15 |
The inverses of numbers mod some integer are computed by writing them as |
| 16 |
rational numbers (as long as the desired inverse exists, of course). |
| 17 |
Examples: |
| 18 |
</p><pre lang="en" class="programlisting">10^-1 mod 101 |
| 19 |
1/10 mod 101</pre><p lang="en"> |
| 20 |
Modular evaluation also works with matrices including taking inverses, |
| 21 |
powers, and dividing. |
| 22 |
Example: |
| 23 |
</p><pre lang="en" class="programlisting">A = [1,2;3,4] |
| 24 |
B = A^-1 mod 5 |
| 25 |
A*B mod 5</pre><p lang="en"> |
| 26 |
This should yield the identity matrix as B will be the inverse of A mod 5. |
| 27 |
</p><p lang="en"> |
| 28 |
Some functions such as |
| 29 |
<a class="link" href="ch11s05.html#gel-function-sqrt"><code class="function">sqrt</code></a> or |
| 30 |
<a class="link" href="ch11s05.html#gel-function-log"><code class="function">log</code></a> |
| 31 |
work in a different way when in modulo mode. These will then work like their |
| 32 |
discrete versions working within the ring of integers you selected. For |
| 33 |
example: |
| 34 |
</p><pre lang="en" class="programlisting">genius> sqrt(4) mod 7 |
| 35 |
= |
| 36 |
[2, 5] |
| 37 |
genius> 2*2 mod 7 |
| 38 |
= 4</pre><p lang="en"> |
| 39 |
<code class="function">sqrt</code> will actually return all the possible square |
| 40 |
roots. |
| 41 |
</p><p lang="en"> |
| 42 |
Do not chain mod operators, simply place it at the end of the computation, all computations in the expression on the left |
| 43 |
will be carried out in mod arithmetic. If you place a mod inside |
| 44 |
a mod, you will get unexpected results. If you simply want to |
| 45 |
mod a single number and control exactly when remainders are |
| 46 |
taken, best to use the <code class="literal">%</code> operator. When you |
| 47 |
need to chain several expressions in modular arithmetic with |
| 48 |
different divisors, it may be best to just split up the expression into several and use |
| 49 |
temporary variables to avoid a mod inside a mod. |
| 50 |
</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> |