Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4694

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>making new classes</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_new.html">next</a> | <a href="_inheritance.html">previous</a> | <a href="_new.html">forward</a> | <a href="_inheritance.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_making_spnew_spclasses.html" title="">making new classes</a></div>
<hr/>
<div><h1>making new classes</h1>
<div>All new classes are made with the operator <a href="_new.html" title="new objects and new types">new</a>.  You may choose to implement the instances of your new class either as basic lists or as hash tables, or you may even base it on a subclass of <a href="___Basic__List.html" title="the class of all basic lists">BasicList</a> or a subclass of <a href="___Hash__Table.html" title="the class of all hash tables">HashTable</a>, if you find a class that has some of the methods you need already implemented.<p/>
As an example, we may wish to implement quaternions as lists of four real numbers.  We know that lists already have a method for addition that treats them as vectors, and we could use the same code for addition of quaternions.<table class="examples"><tr><td><pre>i1 : Qu = new Type of List

o1 = Qu

o1 : Type</pre>
</td></tr>
<tr><td><pre>i2 : w = new Qu from {1,2,3,4}

o2 = {1, 2, 3, 4}

o2 : Qu</pre>
</td></tr>
<tr><td><pre>i3 : w+w

o3 = {2, 4, 6, 8}

o3 : Qu</pre>
</td></tr>
</table>
Now all we have to do is to install a method for multiplying quaternions.<table class="examples"><tr><td><pre>i4 : Qu * Qu := (x,y) -> new Qu from { 
               x#0*y#0 - x#1*y#1 - x#2*y#2 - x#3*y#3,
               x#0*y#1 + x#1*y#0 + x#2*y#3 - x#3*y#2,
               x#0*y#2 + x#2*y#0 + x#3*y#1 - x#1*y#3,
               x#0*y#3 + x#3*y#0 + x#1*y#2 - x#2*y#1
               };</pre>
</td></tr>
<tr><td><pre>i5 : w*w

o5 = {-28, 4, 6, 8}

o5 : Qu</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>