beginner oo programming

Steven Taschuk staschuk at telusplanet.net
Tue Feb 25 12:47:57 EST 2003


Quoth Multiscan:
> Hi all,
> 	may be it is not the right place to post this question but I an
> approaching oo programming using python and therefore...
> 
> Suppose I want to define a new vector class. This is simple: a vector has
> x, y, z components.
> But suppose I want to be able to use/write/store the vectors in two (or
> more) different coordinate systems as, e.g. cartesian and oblique, and 
> that the transformation from one system to another is variable.
> For example. Suppose you have a 
> cartesian coordinate system {(1, 0), (0,1)} 
> and an oblique one {(1, 0), (a, b)} with b != 0
> The way to transform a vector from one system to the other depends on the
> ration a/b. Right ?
> Now what is the best way to add to the vectors the knowledge of the
> transformation relation ?

An excellent question.

Since I don't know your application, I'm not sure my notions will
be appropriate.  But here ya go:

The same question might be asked with "integer" instead of
"vector", and "base" instead of "coordinate system".  The usual
answer for the integer/base question is this: The base is a
property of a particular representation of an integer, not a
property of the integer itself.  So, the integer object should not
be bound to any particular base, and the user should have to
specify the desired base when asking for a string representation
of the integer or when creating an integer from a string
representation.  (For convenience, there should be a default base,
namely base 10.)

Similarly, the coordinate system is a property of a particular
representation of a vector, not a property of the vector itself. 
So, the vector object should not be bound to any particular
coordinate system, and the user should have to specify the desired
coordinate system:
	>>> v = vector((1, 1), cartesian)
	>>> print v.components(cartesian)
	(1, 1)
	>>> print v.components(polar) # (sqrt(2), pi/4)
	(1.4142135623730951, 0.78539816339744828)
	>>> import math
	>>> u = vector((1, math.pi/6), polar)
	>>> u.components(polar)
	(1, 0.52359877559829882)
	>>> u.components(cartesian) # (sqrt(3)/2, 1/2)
	(0.8660254037844386, 0.5)
For convenience, there should be a default coordinate system,
probably the Cartesian.

And, just as integers have a particular internal representation --
namely base 2 -- so vectors will have a particular internal
representation -- perhaps Cartesian.  The vector addition etc. 
routines work on that representation.  (But note that the user
doesn't care what the internal representation is -- you might
choose to use some other representation later.)

If you use Cartesian coordinates internally, the Cartesian
coordinate system object can access them directly.  Other
coordinate system objects ask for the Cartesian representation and
convert.  (This assumes that
	v.components(polar)
delegates to, say,
	polar.components(v)
And the reverse: vector(tuple, coordsystem) delegates to
coordsystem.makevector(tuple) or some such.  The nice thing about
this scheme is that the knowledge of how to convert to/from a
given coordinate system is stored in the object which represents
that coordinate system.  Good abstraction.)

(Note, incidentally, that oblique (and rotated) coordinate systems
can be implemented with a matrix and a bit of linear algebra.)

How does that sound?

-- 
Steven Taschuk                Aral: "Confusion to the enemy, boy."
staschuk at telusplanet.net      Mark: "Turn-about is fair play, sir."
                              (_Mirror Dance_, Lois McMaster Bujold)





More information about the Python-list mailing list