Recent site activity

A Simple Triple Class

Developing a Triple Class

Now we are going to begin developing a class based system for our computational triples. The two references which I will be using are Chapter 4 of Astronomical Applications of Vedic Mathematics by Kenneth Williams, and Chapter 12 Python Visual Quick Start by Chris Fehily.

The first thing we need to do is to define the class, set up instance variables and define an initialisation method. In the example file simple_triple_class_1.py we have a very basic implementation of a class. Run the script and see the output. Then run it under the debugger and pause it at the first print statement, and examine tthe variable t1. You will see that t1 is like a structure with three values contained within it. These are the instance variables.

We will now extend the initialisation method so that we can pass arguments to it, simple_triple_class_2.py. This allows us to have a basic initialisation, but also to input different values. Note that in the previous exposure to triples I used a tuple, however instance variables are simpler to use here. Again run the script, and then examine the variable t1, and it's instance variables.

Next we use the new method to create the triple which is orthogonal to (3,4,5) namely (4,3,5), simple_triple_class_3.py. Again run the script, and run under the debugger, to the first print sstatement after t2 is created. You will note that the instance variables of t2 are different from the instance variables of t1, so that they actually vary from one instance of a Triple class to another.

So now we have the ability to create various instances of a Triple. We don't explicitly need to create a destructor, so we will continue on with creating string representations, for printing and re-evaluation. In the first case, simple_triple_class_3.py you will see how it is very easy to print an instance of a Triple, in a neatly formatted way. The second is used to create a string which is used for debugging.

The next thing we should do is to set the truth value of an instance, and define the comparison operators. I will skip these for the moment and jump ahead to defining addition and subtraction methods instead. You might consider what is the best way to compare triples, I have my own ideas, but it would be interesting to hear others' opinions first.

The basic formula for addition is given on page 41 of Kenneth's book, and the way of building addition into a class is given on p 376 of Fehily's book. The script, simple_triple_class_5.py, illustrates implementing triple addition. Subtraction is implemented in, simple_triple_class_6.py, based on the formula on page 44 of Kenneth's book. I know that there seems to be a repetition of code in the subtraction method, but this can be refactored when we define a negate method, we will deal with this at a later stage.

Before finishing I will add one other method, based on extracting information on the angle in a triple. We dealt with this already when we were developing a procedural approach to triples, using the built in data type of a tuple. We need to import the atan2 function and the constant pi from the math module, and then the extraction of the anlgle is very simple. Note that since the atan2 function is used in the class definition, we must import before we define the class. The script is, simple_triple_class_7.py.

So now you have the basics of defining a computational class for triples. It would be tempting to go on at this stage and implement all the other methods, but I will adopt a different approach, and start to build a hirearchical system, first by defining an abstract base class, then by implementing triples for two and three dimensions, and then considering the most general case, triples in an n dimensional Euclidean space.

With this under our belt we will really have begun to develop the computational tools we require to explore the construction of our hyper-circle in Real 4-space.

Attachments (7)

  • simple_triple_class_1.py - on 1 Oct 2008 17:11 by Unknown user (version 1)
    1k Download
  • simple_triple_class_2.py - on 1 Oct 2008 17:11 by Unknown user (version 1)
    1k Download
  • simple_triple_class_3.py - on 1 Oct 2008 17:11 by Unknown user (version 1)
    1k Download
  • simple_triple_class_4.py - on 1 Oct 2008 17:12 by Unknown user (version 1)
    1k Download
  • simple_triple_class_5.py - on 1 Oct 2008 17:12 by Unknown user (version 1)
    1k Download
  • simple_triple_class_6.py - on 1 Oct 2008 17:12 by Unknown user (version 1)
    1k Download
  • simple_triple_class_7.py - on 1 Oct 2008 17:13 by Unknown user (version 1)
    1k Download