Python anyone?

Status
Not open for further replies.
I'm trying to port some Pascal code from Pascal to Python. Much of it is vector and math based. I'm not sure if I should use Numeric or build my own class like in the following link:

vector: A list based vector class supporting elementwise operations

Here's the Pascal module in question:

code:<HR>
Code:
 Unit SGP_Math;
{           Author:  Dr TS Kelso }
{ Original Version:  1991 Oct 30 }
{ Current Revision:  1998 Mar 17 }
{          Version:  3.00 }
{        Copyright:  1991-1998, All Rights Reserved }
{$N+}

interface

type
  vector = array [1..4] of double;

const
  twopi = 2 * pi;
  zero : vector = (0,0,0,0);

Function Sign(arg : double) : shortint;
Function Cube(arg : double) : double;
Function Power(arg,pwr : double) : double;
Function Radians(arg : double) : double;
Function Degrees(arg : double) : double;
Function Tan(arg : double) : double;
Function ArcSin(arg : double) : double;
Function ArcCos(arg : double) : double;
Function Modulus(arg1,arg2 : double) : double;
Function Fmod2p(arg : double) : double;
Function AcTan(sinx,cosx : double) : double;
Procedure Magnitude(var v : vector);
Procedure Vec_Add(v1,v2 : vector; var v3 : vector);
Procedure Vec_Sub(v1,v2 : vector; var v3 : vector);
Procedure Scalar_Multiply(k : double; v1 : vector; var v2 : vector);
Function Dot(v1,v2 : vector) : double;
Function Angle(var v1,v2 : vector) : double;
Procedure Cross(v1,v2 : vector; var v3 : vector);
Procedure Normalize(var v : vector);

implementation

uses
  Dialogs,
  Support;

type
  matrix = array [1..3,1..3] of double;

Function Sign(arg : double) : shortint;
  begin
  if arg > 0 then
    Sign := 1
  else if arg < 0 then
    Sign := -1
  else
    Sign := 0;
  end; {Function Sign}

Function Cube(arg : double) : double;
  begin
  Cube := arg*Sqr(arg);
  end; {Function Cube}

Function Power(arg,pwr : double) : double;
  begin
  if arg > 0 then
    Power := Exp(pwr*Ln(arg))
  else
    Writeln(output,'Invalid argument in Function Power!');
  end; {Function Power}

Function Radians(arg : double) : double;
  begin
  Radians := arg*pi/180;
  end; {Function Radians}

Function Degrees(arg : double) : double;
  begin
  Degrees := arg*180/pi;
  end; {Function Degrees}

Function Tan(arg : double) : double;
  begin
  Tan := Sin(arg)/Cos(arg);
  end; {Function Tan}

Function ArcSin(arg : double) : double;
  begin
  if Abs(arg) >= 1 then
    ArcSin := Sign(arg)*pi/2
  else
    ArcSin := ArcTan(arg/Sqrt(1-Sqr(arg)));
  end; {Function ArcSin}

Function ArcCos(arg : double) : double;
  begin
  ArcCos := pi/2 - ArcSin(arg);
  end; {Function ArcCos}

Function Modulus(arg1,arg2 : double) : double;
  var
    modu : double;
  begin
  modu := arg1 - Trunc(arg1/arg2) * arg2;
  if modu >= 0 then
    Modulus := modu
  else
    Modulus := modu + arg2;
  end; {Function Modulus}

Function Fmod2p(arg : double) : double;
  begin
  Fmod2p := Modulus(arg,twopi);
  end; {Function Fmod2p}

Function AcTan(sinx,cosx : double) : double;
  begin
  if cosx = 0 then
    if sinx > 0 then
      Actan := pi/2
    else
      Actan := 3*pi/2
  else if cosx > 0 then
    Actan := ArcTan(sinx/cosx)
  else
    Actan := pi + ArcTan(sinx/cosx);
  end; {Function Actan}

Procedure Magnitude(var v : vector);
  begin
  v[4] := Sqrt(Sqr(v[1]) + Sqr(v[2]) + Sqr(v[3]));
  end; {Procedure Magnitude}

Procedure Vec_Add(v1,v2 : vector; var v3 : vector);
{ Adds vectors v1 and v2 together to produce v3 }
  var
    i : byte;
  begin
  for i := 1 to 3 do
    v3[i] := v1[i] + v2[i];
  Magnitude(v3);
  end; {Procedure Vec_Add}

Procedure Vec_Sub(v1,v2 : vector; var v3 : vector);
{ Subtracts vector v2 from v1 to produce v3 }
  var
    i : byte;
  begin
  for i := 1 to 3 do
    v3[i] := v1[i] - v2[i];
  Magnitude(v3);
  end; {Procedure Vec_Sub}

Procedure Scalar_Multiply(k : double;
                         v1 : vector;
                     var v2 : vector);
{ Multiplies the vector v1 by the scalar k to produce the vector v2 }
  var
    i : byte;
  begin
  for i := 1 to 3 do
    v2[i] := k*v1[i];
  v2[4] := Abs(k)*v1[4];
  end; {Procedure Scalar_Multiply}

Function Dot(v1,v2 : vector) : double;
  begin
  Dot := v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3];
  end;  {Function Dot}

Function Angle(var v1,v2 : vector) : double;
{ Calculates the angle between vectors v1 and v2 }
  begin
  Magnitude(v1);
  Magnitude(v2);
  Angle := ArcCos(Dot(v1,v2)/(v1[4]*v2[4]));
  end; {Function Angle}

Procedure Cross(v1,v2 : vector; var v3 : vector);
  begin
  v3[1] := v1[2]*v2[3] - v1[3]*v2[2];
  v3[2] := v1[3]*v2[1] - v1[1]*v2[3]; 
 v3[3] := v1[1]*v2[2] - v1[2]*v2[1];
  Magnitude(v3);
  end; {Procedure Cross}

Procedure Normalize(var v : vector);
  var
    i : byte;
  begin
  for i := 1 to 3 do
    v[i] := v[i]/v[4];
  end; {Procedure Normalize}

end.

q
 

petrov

Ars Tribunus Militum
2,714
Originally posted by Ursus Maximus:
Ok, now I'm really curious, how much would the Indians have today at 5% interest on the $26 sale of Manhattan some-what-300-400 years ago????

I believe it was made in 1626 by Peter Minuit, and the amount of money was actually $24 (that would make it 378 years). And the answer to your question varies greatly upon the interest rate expected:
<table>
<tr><th>Interest</th><th>Present Value</th></tr>
<tr><td>1%</td><td>$1,032.03</td></tr>
<tr><td>2%</td><td>$42,763.79</td></tr>
<tr><td>3%</td><td>$1,708,764.37</td></tr>
<tr><td>4%</td><td>$65,889,088.05</td></tr>
<tr><td>5%</td><td>$2,453,388,301.80</td></tr>
<tr><td>6%</td><td>$88,273,086,435.01</td></tr>
<tr><td>7%</td><td>$3,070,995,688,511.61</td></tr>
<tr><td>8%</td><td>$103,369,119,218,531.00</td></tr>
<tr><td>9%</td><td>$3,368,429,508,597,830.00</td></tr>
<tr><td>10%</td><td>$106,327,653,940,462,000.00</td></tr>
</table>

Warren Buffet has managed to pull off a 20+% growth rate for 30 years (and is now one of the richest men in the world). Most stocks do about 7%. A bank will give you 3-5%. I have never seen a sum total for the current value of all Manhattan real estate. I'm not sure if it's worth $3 trillion though, that's a lotta berries.

--sam
 
https://secure.linuxports.com/opendocs/ is a link to a place to buy a brand new book called GUI Programming with Qt: Python Edition. I am probably going to order it and also order Black Adder from theKomapny. Since PyQt documentation is hard to find, and I am not fluent in C++, I think this new book will be of great help to me. I think it costs $49.00.
 
Well, I have just ordered both Black Adder, Personal edition plus one year of updates, as well as the new book, GUI Programming with Qt: Python edition.

I want to thank all of you who responded to my enquiry on this: you have helped to make my decision My reasons are:

1. I want to use my gui programs primarily on Linux (KDE) and also, eventually, the Sharp Zaurus which uses the Qt toolkit.

2. The new GUI programming book using PyQt will be very helpful to me, it includes chapters specifically on Black Adder.

3. I feel it is time for me to get off the fence and support Black Adder, which appears to be what I am looking for.

This will not be a quick learn for me, but I am in it for the long term now.

Thanks to all who offered opinions, and I look forward to reading the book as soon as I get home from Asia! ;-)))

Ron Stephens
gui programming neophyte

I will keep a log of my experiences with Black Adder on my website Python City at http://www.awaretek.com/plf.html :cool: :cool:
 

dockeen

Ars Tribunus Militum
1,600
Yeah, I just hated ending up with odd numbers.
Later, when renumber appeared, it made things
so pretty, It was a blessing from God...

I also was lucky a few years later, like 1979,
when a nice Astronomy professor at Vassar showed
me that it really was faster to use this new
fangled for-loop than an if and a goto.

:)

Of coarse, those were also the days when you
learned things like use x * x rather than x**2
for effeciency too.

Wayne the old
 

dockeen

Ars Tribunus Militum
1,600
Sometimes the hardest thing to do is overcome an attachment
to the first way we learned to do something.

I worked for an older physicist who, I swear, could not write
10 lines of code without a goto (or 2, or 3).

One other BASIC memory just popped in. I was learning about
graphics on the Tektronix. For some reason, it took me the
better part of one afternoon sitting in a lab at Vassar to
get straight the concept of window and viewport. Was working
on a program for that same Astronomy professor that taught
me about for-loops. The floppy drive on that thing was about
twice as big as a big tower case is these days.

My 20th reunion at Vassar is coming up this summer. I may yet
go...

Wayne

sitting in my house in Florida, about a mile from the beach,
where its still pretty cold!
 

dockeen

Ars Tribunus Militum
1,600
Wayne requires a tactical nuclear weapon....

Kidding.

I still like to play with new languages, many of
which have not particular application to anything
I am doing. Sometimes, like was the case with
Python, they do.

I'm not sure about Vassar though, its a long way,
and I was a town student, so I didn't really know
too many people beyond the Physics department.
We'll see, I was hot about doing it last year,
wavering now.

Oh yeah, wife doesn't want to go...

Wayne
 

_Dent_

Ars Scholae Palatinae
607
My 20th reunion at Vassar is coming up this summer


code:<HR>
Code:
 A Day at the Races (from the '30s)

Whitmore is questioning Dr. Hackenbush (Groucho) (who is really a
 vet) on his medical ability, trying to find out whether he is 
qualified to lead Mrs. Upjohn's Sanitarium 

Whitmore: Tell me, Dr. Hackenbush, just what was your medical background? 
Hackenbush: Medically? 
Whitmore: Yes. 
Hackenbush: Well, uh, at the age of fifteen I got a job in a drugstore filling prescriptions. 
Whitmore: Don't you have to be twenty-one to fill prescriptions? 
Hackenbush: Well, ah, that's for grown ups. I just filled them for children. 
Whitmore: No, no, Doctor. I meant where did you get your training as a physician? 
Hackenbush: Oh, well to begin with I took four years at Vassar. 
Mrs. Upjohn: Vassar? But that's a girls' college. 
Hackenbush: I found that out the third year. I'd've been there yet but I went out for the swimming team. 
Whitmore: The Docotor seems reluctant to discuss his medical experiences. 
Hackenbush: Well, medically, my experiences have been most unexciting except during the flu epidemic. 
Whitmore: Ah, and what happened? 
Hackenbush: I got the flu.

:confused:
 

dockeen

Ars Tribunus Militum
1,600
I get some weird looks when I tell people I got my BA
from Vassar. I have to tell them that no major surgery
was involved, Vassar went Co-Ed in the 70's.

Actually, I have an interesting combination of degrees,
BA from Vassar, MS and Ph.D. from the University of Alabama.
Sometimes, in my line of work, that dumb ol' country
boy accent can come in handy.

Wayne
 
D

Deleted member 5324

Guest
Originally posted by Ursus Maximus:
Hey, Jimfinch, do you live in Cincinnati? I was just looking at the html source of this trhead (in order to try to figure out how come others can get Python City type of links to show up correctly as links) when I noticed you listed as Tribus Cincinantus??

I sure do :) Lived here since 1986 :)

Both my wife an I were born and reaised in Cincinnati Ohio. I miss it!!! How are the Bearcats doing? What I really , really miss, is Skyline Chili. I can't get three ways or five ways since I left town in 1988 and that hurts! I used to eat three ways for breakfast, lunch, and dinner, all in the same day sometimes!!

I worked at one of the best Skylines in the system for almost 4 years :)

Jeremy
 
Status
Not open for further replies.