[sci.math] Algorithm Description

johnk@megaron.UUCP (10/30/86)

	I'm always amazed at the manner in which mathematicians describe
algorithms.  In a recent discussion on the generation of variates from a
normal distribution, one of the most interesting presentations of the
Box-Muller-Marsaglia algorithm was the following:
 
 	V1 = 2*RND - 1 <--------+
 	V2 = 2*RND - 1          |
 	S = V1*V1 + V2*V2       |
 	IF S >= 1 THEN GOTO ----+
 	X1 = V1 * SQRT (-2 * LOG(S) / S)
 	X2 = V2 * SQRT (-2 * LOG(S) / S)
 
While this is a clever improvement on the ghastly "step 1", "step 2", "go to
step i" style, why not simply

	repeat {
		v1 = 2*rand() - 1
		v2 = 2*rand() - 1
		s = v1^2 + v2^2
	} until ( s < 1 )
	x1 = v1 * sqrt(-2 * ln(s) / s)
	x2 = v2 * sqrt(-2 * ln(s) / s)

Admittedly, this algorithm is trivial; here the manner of presentation
makes little difference.  I think the computer scientist most
mathematicians know of is Knuth, and by presenting the algorithms of his
_The_Art_of_Computer_Programming_ in the numbered step form, he legitimized
a style which should, in our enlightened age, be obsolete.
-- 
John Kececioglu / arizona!johnk
"Chess, like love, like music, has the power to make men happy." -- S. Tarrasch

zdenek@heathcliff.columbia.edu (Zdenek Radouch) (10/31/86)

In article <1266@megaron.UUCP> johnk@megaron.UUCP writes:
>
>	I'm always amazed at the manner in which mathematicians describe
>algorithms.  In a recent discussion.. one of the presentations ..
>was the following:
> 
> 	V1 = 2*RND - 1 <--------+
> 	V2 = 2*RND - 1          |
> 	S = V1*V1 + V2*V2       |
> 	IF S >= 1 THEN GOTO ----+
> 	X1 = V1 * SQRT (-2 * LOG(S) / S)
> 	X2 = V2 * SQRT (-2 * LOG(S) / S)
> 
>While this is a clever improvement on the ghastly "step 1", "step 2", "go to
>step i" style, why not simply
>
>	repeat {
>		v1 = 2*rand() - 1
>		v2 = 2*rand() - 1
>		s = v1^2 + v2^2
>	} until ( s < 1 )
>	x1 = v1 * sqrt(-2 * ln(s) / s)
>	x2 = v2 * sqrt(-2 * ln(s) / s)

My friend's four year old daughter is always amazed when I speak to
her mother in Czech. No matter how hard I try, I am not able to explain
that some people just speak different languages. She is also convinced,
that the English is much simpler. Any suggestions?

zdenek

P.S.
Something tells me, that this might not be the best newsgroup for my
question. Sorry.

 -------------------------------------------------------------------------
 Men are four:
 He who knows and knows that he knows, he is wise - follow him;
 He who knows and knows not that he knows, he is asleep - wake him;
 He who knows not and knows that he knows not, he is simple - teach him;
 He who knows not and knows not that he knows not, he is a fool - shun him!

 zdenek@CS.COLUMBIA.EDU  or 	...!seismo!columbia!cs!zdenek
 Zdenek Radouch, 457 Computer Science, Columbia University,
 500 West 120th St., New York, NY 10027

debray@megaron.UUCP (11/03/86)

zdenek@CS.COLUMBIA.EDU, commenting on an article by johnk@megaron which
decried the use of GOTOs in algorithm descriptions:

> My friend's four year old daughter is always amazed when I speak to
> her mother in Czech. No matter how hard I try, I am not able to explain
> that some people just speak different languages. She is also convinced,
> that the English is much simpler. Any suggestions?

Well, some of us think that if you're describing an algorithm for others
to understand, obfuscating your description isn't terribly helpful.
Some of us also think that GOTOs get in the way of understandability.

No doubt some people would have no trouble deciphering algorithms described
in Vax-11 assembly language ... I'm not convinced that that'd make assembly
language an acceptable algorithm _description_ language.

---
Saumya Debray
University of Arizona, Tucson

  debray@arizona.edu
  {allegra, cmcl2, ihnp4}!arizona!debray

kenny@uiucdcsb.cs.uiuc.edu (11/13/86)

I'm always amazed at the way academic pedants flame people for couching
ideas in a language appropriate to their readership, when a more elegant if
less straightforward statement is available.  Witness the following:

/* Written 10:28 am  Oct 30, 1986 by johnk@megaron.UUCP in uiucdcsb:sci.math */
/* ---------- "Algorithm Description" ---------- */

>	I'm always amazed at the manner in which mathematicians describe
>algorithms.  In a recent discussion on the generation of variates from a
>normal distribution, one of the most interesting presentations of the
>Box-Muller-Marsaglia algorithm was the following:
> 
> 	V1 = 2*RND - 1 <--------+
> 	V2 = 2*RND - 1          |
> 	S = V1*V1 + V2*V2       |
> 	IF S >= 1 THEN GOTO ----+
> 	X1 = V1 * SQRT (-2 * LOG(S) / S)
> 	X2 = V2 * SQRT (-2 * LOG(S) / S)
> 
>While this is a clever improvement on the ghastly "step 1", "step 2", "go to
>step i" style, why not simply
>
>	repeat {
>		v1 = 2*rand() - 1
>		v2 = 2*rand() - 1
>		s = v1^2 + v2^2
>	} until ( s < 1 )
>	x1 = v1 * sqrt(-2 * ln(s) / s)
>	x2 = v2 * sqrt(-2 * ln(s) / s)
>
>Admittedly, this algorithm is trivial; here the manner of presentation
>makes little difference.  I think the computer scientist most
>mathematicians know of is Knuth, and by presenting the algorithms of his
>_The_Art_of_Computer_Programming_ in the numbered step form, he legitimized
>a style which should, in our enlightened age, be obsolete.
>-- 
>John Kececioglu / arizona!johnk
>"Chess, like love, like music, has the power to make men happy." -- S. Tarrasch
/* End of text from uiucdcsb:sci.math */

I am the original poster of that version of the polar method.  I think that
the flame is perhaps a bit misplaced.  First, you are flaming me incorrectly
for being a mathematician; I am, in fact, a computer scientist by trade.  I
responded in the ugly fashion because the original poster asked for a
solution expressed in BASIC.  The tone of theoriginal request suggested that
perhaps the poster was unfamiliar with structured languages and therefore
would find the ``repeat { ...  } until (condition)'' notation (which I used
in my working notes before composing the version that I posted) confusing.

You will probably argue that anyone who cannot understand that notation has
no business attempting to program.  I contend that it is very desirable to
learn that notation; that it is essential if one intends to be a serious
programmer; that ones programming is crippled without it.  It probably,
however, is not relevant to a non-programmer doing a BASIC program for
his own use.  Such people do not need to know even the fundamentals.
To carry the argument to absurdity, I suppose that if you were an exponent
of the applicative style of programming, you would favor something like:

------------------------------------------------------------------------
(defun deviate-pair () (make-deviate-pair (make-dotprod-and-xy)))
(defun make-deviate-pair (x)
	(let ((c (sqrt (quotient (times -2. (ln (car x)))
				 (car x) ))))
	  (mapcar (cdr x)
		  (closure (lambda (v) (times v c))))))
(defun make-dotprod-and-xy ()
	(check-dotprod-and-xy '(2. 1. 1.)))
(defun check-dotprod-and-xy (l)
	(cond ((lessp (car l) 1.) l)
	       (t (check-dotprod-and-xy (new-dotprod-and-xy)))))
(defun new-dotprod-and-xy ()
	(let ((x (new-coord)) (y (new-coord)))
	     (list (dotprod x y) x y)))
(defun new-coord () (sub1 (times 2 (random))))
(defun dotprod (x y) (plus (times x x) (times y y)))
------------------------------------------------------------------------

What then of the BASIC (or C or PASCAL) programmer who doesn't speak LISP?
Need he spend several months learning LISP, SCHEME, and the applicative style
of programming so that he can take the algorithm as presented and map it
back into his von-Neumann-model language?

In short, I was attempting to adapt my reply to my audience, not to
perpetuate a programming style that I, too, consider inferior.

Kevin Kenny			UUCP: {ihnp4,pur-ee,convex}!uiucdcs!kenny
Department of Computer Science	ARPA: kenny@B.CS.UIUC.EDU (kenny@UIUC.ARPA)
University of Illinois		CSNET: kenny@UIUC.CSNET
1304 W. Springfield Ave.
Urbana, Illinois, 61801		Voice: (217) 333-8740

ladkin@kestrel.ARPA (Peter Ladkin) (11/18/86)

In article <162000001@uiucdcsb>, kenny@uiucdcsb.cs.uiuc.edu writes:
> I'm always amazed at the way academic pedants flame people [...]
> [long apologia pro postanda sua] 
> Kevin Kenny			
> Department of Computer Science	

We are to assume you're not an academic pedant, I suppose.

Peter Ladkin
ladkin@kestrel.arpa

roz@l.cc.purdue.edu (Vu Qui Hao-Nhien) (11/19/86)

Expiration:

In article <1266@megaron.UUCP> johnk@megaron.UUCP writes:
Essentialy, that 'the ghastly "step 1", "step 2", "go to >step i" style' is,
well, ghastly.  And then gave the alternative for a goto:
>
>	repeat {
>		v1 = 2*rand() - 1
>		v2 = 2*rand() - 1
>		s = v1^2 + v2^2
>	} until ( s < 1 )
>	x1 = v1 * sqrt(-2 * ln(s) / s)
>	x2 = v2 * sqrt(-2 * ln(s) / s)

Well, goto looks a whole lot more formal.  I don't know why.  It just feels
that way.  My own style is instead of using brackets {}, I'd do

	step 1.
		v1 = ....
		v2 = ....
	step 2. if s >= 1, repeat Big step 1.
	step 3. x1 = ....
		x2 = ....

Brackets, especially when written your way, is intimidating (eg.  Why
is there a right bracket at the beginning of a line ? )

>I think the computer scientist most
>mathematicians know of is Knuth, and by presenting the algorithms of his
>_The_Art_of_Computer_Programming_ in the numbered step form, he legitimized
>a style which should, in our enlightened age, be obsolete.

Sad but true.  Most mathematicians present algorithms the Knuth way, mostly
because they learn basic mathematical algorithms from Knuth's book.

===========
Hao-Nhien Q. Vu (pur-ee!stat-l!vu)
                (vu@l.cc.purdue.edu)
[That's stat-"ell", not stat-"one"]

-- 
Hao-Nhien Q. Vu (pur-ee!stat-l!vu)
                (vu@l.cc.purdue.edu)
[That's stat-"ell", not stat-"one"]