Table of Contents

Introduction
For whom is this editor?
For what platforms?
Download and install
A Ruby example
Line number, dirty mark, key bindings, and type size
A Java™ example
Other demo and Demo examples
Integration by Monte Carlo in Six Dimensions
Graph and the Escape key: Cauchy Camel
Graph and the Escape key: Bessel
Graph and the Escape key: Euler
Graph and the Escape key: Lissajous
Graph and the Escape key: Polar and no
Caught in a loop
License, revision date, and e-mail address

Introduction

ScaleDownEdit is an editor written in the Tcl/Tk language. It is tiny, and it is meant for use on tiny programs. The first claimed virtue is the SAVE AND RUN button, the big green button on the left on top. This button is able to save and run the program in just one click of the mouse. One may think of it as the instant gratification button.

The second claimed virtue is the GRAPH button, the big cyan button on the right on top. This button is able to save and run and graph the program in just one click of the mouse. Numbers and color names printed by the program are understood as plotting coordinates and colors for the graph. That is, the plotting is independent of language, so even computer languages without graphics can plot mathematical graphs.

The editor knows how to run the program by looking at the top line of the program file. That is, the editor is not specialized for a small set of languages. I hereby place ScaleDownEdit in the public domain. To top

For whom is this editor?

ScaleDownEdit is not for beginners in programming, because they need more features. Neither is it for professional programmers, because they need many more features. Rather, it is for amateur programmers who may wish to see a tiny editor so they can improve on it. Also, and mostly, it is for the authors of editors, so they can see my instant gratification button and see how it reads the first line of the file. Then they can easily add such a button to their editors. To top

For what platforms?

ScaleDownEdit ought to run on any platform having Tcl/Tk. However, the only platform I have now is Microsoft Windows XP, so I cannot be sure. To top

Download and install

All the files for ScaleDownEdit are in ScaleDownEdit.zip. The reader may just download that zip file into any desired folder or directory and then unzip it there. To top

A Ruby example

One of the program files is demo.rb. It looks like this:
# c:/ruby/bin/ruby.exe demo.rb

x=1.0

	15.times{ |j|
	x=x-( x**5 + x - 17.0 )/( 5.0 * x**4 + 1.0 - 0.0 )
	print j,"\t",x,"\n"
	}
In order to help load it into ScaleDownEdit, I have prepared a batch file called ScaleDownEdit.bat looking like this:
start c:\tcl\bin\wish85.exe ScaleDownEdit.tk %*
The way of using it is to type
ScaleDownEdit.bat demo.rb
at the Dos prompt and push return. (I speak of Microsoft Windows. The user will of course change all my batch files for other platforms and for other positions and names of wish.) The first line of the Ruby program shows the command which is going to be used to start the program after it is saved. If it is wrong for the user’s computer, I respectfully invite the user to make appropriate changes. Then the user may click the SAVE AND RUN button with the mouse. If everything is correct, the program area will close and the output area will open showing the answers.

To get back to the program area, the user may use the mouse to click on the SEE PROGRAM button. That will close the output area and re-open the program area, so that changes can be made. SAVE AND RUN and SEE PROGRAM are the important buttons. CLEAR OUTPUT will erase the answers in the output area. SEE OUTPUT will close the program area and open the output area. SAVE PROGRAM will save the program without running it.

Of course, I have stolen the idea of using the first line to say how to run the file. The Unix and Linux shells do this with a shebang. I am merely taking advantage of comments. In the case of Ruby (and many other computer languages) the # character begins a comment. The SAVE AND RUN button’s procedure breaks the first line into tokens, using white-space, and then the left-most token is changed into exec. Then the tokens are handed to eval to be evaluated. The reader sees that there must be white-space after the comment token. Also the reader sees that those are forward slashes in the first line of the program, not backslashes, even though the path is in Microsoft Windows.

The program implements the good old Newton-Raphson loop to solve the equation x5 + x = 17 for its real root. The loop goes around fifteen times. All my demo and Demo programs implement this same algorithm. To top

Line number, dirty mark, key bindings, and type size

The line number and character number follow the title. For example, 8.2 means the eighth line, the second character of that line. That is where the cursor is.

The dirty mark is the asterisk, *. It goes on the left of the title when the file is “dirty,” that is, when the file in the editor is (or may be) different from the file saved in the hard disk. SAVE AND RUN or SAVE PROGRAM will erase the dirty mark.

The four arrow keys or four triangle keys have their usual meanings. The Page Up and Page Down keys have their usual meanings. The control-Home and control-End keys have their usual meanings. Control-x means cut. Control-c means copy. Control-v means paste. All these are done by Tk without any work on my part.

I have bound control-minus to mean reduce the type size. I have bound control-plus to mean increase the type size, and I have bound control-equal to mean increase the type size. (The plus is the shifted equal sign on my keyboard.)

Also, I have bound the keys “F1” through “F6” to do the same work as my buttons. That is:
F1 is like SAVE AND RUN.
F2 is like SEE PROGRAM.
F3 is like CLEAR OUTPUT.
F4 is like SEE OUTPUT.
F5 is like SAVE PROGRAM.
F6 is like GRAPH.

I have bound these six keys to make a musical sound when they are pushed.

I warn everybody against pushing the “F10” key. Tk has bound it to toggle the focus, I think. Users who do push that key ought to push it a second time to come back into focus, or else the other “F” keys might not work. To top

A Java™ example

Another one of the program files is Demo.java. It looks like this:
// ScaleJava.bat Demo

import static java.lang.Math.*;

public class Demo
{

	public static void main( String[] args )
	{
	int n=15;
	double x=1.0;

		for( int j=0; j<n; j++ )
		{
		x=x-( pow(x,5)+x-17.0 )/( 5.0*pow(x,4)+1.0-0.0 );
		System.out.println( j+"\t"+x );
		}

	}

}
One may type
ScaleDownEdit.bat Demo.java
at the Dos prompt and push return. (Again, I speak of Windows.) The first line of the Java program shows the batch file which is going to be used to start the program after the program is saved. The batch file is named ScaleJava.bat, and it looks like
@if exist %1.class del %1.class
@"C:\Program Files\Java\jdk1.7.0\bin\javac.exe" %1.java
@if exist %1.class java %1
If it is wrong for the user’s computer, I respectfully invite the user to make appropriate changes. Then the user may click the SAVE AND RUN button with the mouse. If everything is correct, the program area will close and the output area will open showing the answers.

I respectfully point out that the last token of the first line of the program is merely Demo, without a suffix. To top

Other demo and Demo examples

I have also included demo or Demo files for C Sharp, PHP, Perl, and Python. As I said earlier in this file, all these programs implement the same algorithm, the Newton-Raphson loop. To top

Integration by Monte Carlo in Six Dimensions

Another common topic in numerical analysis is numerical integrals. I will take the integral of the square root of the sine of x*y*z*u*v*w over the unit hypercube in six dimensions. Here x, y, z, u, v, and w are the names of the six axes. I did try asking some freeware and proprietary calculus programs to do this symbolically, and they failed. Perhaps the reader knows of one that can succeed. Another thought is to use Simpson’s numerical method. The program will be complicated, because of the high number of dimensions.

No, the way to do this is to use Monte Carlo. That is, let us choose points at random in the hypercube and sum the values of the integrand. Then we can just divide the sum by how many values we used and print the quotient. That is all there is to it:

rem ScaleBas.bat sixDims

option explicit
defdbl a-z

dim n,s
dim x,y,z,u,v,w
dim j as integer

n=1e7
randomize timer
s=0

	for j=1 to n
	x=rnd: y=rnd: z=rnd: u=rnd: v=rnd: w=rnd
	s=s+sqr( sin( x*y*z*u*v*w ) )
	next j
 
print "The answer is ", s/n
That program, sixDims.bas, is written in FreeBasic. The rem token on the top line is the comment token of Basic. The ScaleBas.bat file is
@if exist %1.exe del %1.exe
@"c:\program files\freebasic\fbc.exe" -lang fblite -exx %1.bas
@if exist %1.exe %1.exe
As always, a user having her freebasic in a different place will change the file to suit her.

Now the question arises, “How good is the answer?” The way to find out is to click on the SAVE AND RUN button, wait for an answer, click again on the SAVE AND RUN button, wait for the second answer, and finally click a third time on the SAVE AND RUN button, and wait for the final answer. (Do not clear the output area between runs. It is not necessary to click back to the program each time.) If the three answers look somewhat alike, then maybe the true integral looks somewhat like them. This argument is feeble, but we have little choice.

I respectfully point out that the first line of the program file refers to sixDims without a suffix. To top

Graph and the Escape key: Cauchy Camel

In statistics class we are all told the virtues of Sir Ronald Fisher’s maximum likelihood estimator, that it is a panacea in every parametric problem. That is an overstatement of what Fisher actually said. To illustrate, here is a copy of graphCauchyCamel.lua, which is included in the zip file:
-- luaJIT graphCauchyCamel.lua

local width=800
local height=600
local edge=5
print( width,height,edge )

local cauchy=function( x,c )return 1/( 1+(x-c)^2 )/math.pi end

local k=100

	for t = -2*k, 2*k
	do
	local product=cauchy( -k,t )*cauchy( k,t )
	print( t,math.log( product ),"purple" )
	end
The double hyphen of the first line is the comment token of the Lua language. The LuaJIT interpreter is an improvement to the Lua interpreter, and users having either may use it here. I respectfully draw the reader’s attention to the print statements. The first print statement prints the numbers 800, 600, and 5. The print statement must make white space to separate the three, but no other punctuation marks. The three numbers are the desired numbers of pixels for graph width, graph height, and edge size. This last is the edge size of the solid squares of pixels of which the graph is made. (The squares may overlap, so things are more flexible than one may have feared.) My monitor is set to 800 pixels by 600 pixels, so that is how I set my graph. The user is respectfully invited to change these numbers to suit her monitor, and to change the edge size to suit her taste. This first print statement must execute exactly once.

The second print statement prints a number for x, a number for y, and a color name. The print statement must make white space to separate the three, but no other punctuation marks. The numbers will be rescaled and translated to fit the monitor, so the programmer need not scale them at all. The color name applies to the solid square of pixels to be drawn. To make the program happen, the user may click on the GRAPH button. (I am assuming that luaJIT.exe is in the path.) Then the program area will be covered up by a canvas with a white background and solid purple squares to show the logarithm of the likelihood. The statistical sample has two numbers, k and -k, where k is 100. The user is respectfully invited to change the color name and the value of k.

The graph turns out to look like a two-humped camel, and the humps are sharp and equal in height. Which of these humps is at the maximum likelihood? To ask the question is to answer it. When the user tires of looking at the graph, a push on the Escape key will close the canvas. The Escape key is commonly the leftmost key in the topmost row of the keyboard, and it is often labeled “Esc.”

Instead of clicking on GRAPH, the user may click on SAVE AND RUN, to see the numbers underlying the graph. When things go wrong, this is the right thing to do.

I nearly forgot to mention that Lua and LuaJIT as downloaded have no graphics of their own. That is the reason that I invented the GRAPH button. To top

Graph and the Escape key: Bessel

The preceding program printed in only one color. Here is a use of two, gray and blue. The gray is for the horizontal and vertical axes, and the blue is for Bessel’s J0 function. The function is calculated by a Maclaurin series. The language is the f77 dialect of Fortran. The “c” at the left of the top line is the comment token of Fortran. The reader will notice " gray" and " blue" with a blank inside the quotes. This is to force whitespace between the ordinate value and the color name.
c scaleF.bat graphBessel

	function besselJ0( x )
	term=1
	sum=term
	k=1

16	continue
	term=-term*x*x/2/2/k/k
	k=k+1
	oldSum=sum
	sum=sum+term
	if( oldSum.ne.sum )goto 16

	besselJ0=sum
	end



	print *,800,600,5
c	print *,1280,1024,15

	do 17 j=-10,250
	x=j/10.0
	y=besselJ0( x )
	print *,0,y," gray"
	print *,x,0," gray"
	print *,x,y," blue"
17	continue

	end
The reader will notice the commented “first” print line c print *,1280,1024,15. This can be uncommented and the line before it commented, if the monitor’s width and height are increased to 1280 and 1024. Of course, there are other width and height possibilities.

Here is scaleF.bat:

@path=c:\mingw\bin;%path%
@if exist z.exe del z.exe
@g77 -Wall -s %1.f -o z.exe -O3 -fomit-frame-pointer
@if exist z.exe call z.exe
@if exist z.exe del z.exe
The reader will perhaps change this to something more suitable. To top

Graph and the Escape key: Euler

Next is an example with three colors: gray, red, and blue. The program is written in Chipmunk Basic, and it uses the Euler algorithm to solve van der Pol’s second-order differential equation for the triode oscillator with initial conditions. The equation is x''+a*(x*x-1)*x'+x=0. We let y=x'. Here is the program:
rem c:/chipmunk/chipmunk/basic.exe graphEuler.bas
print 800,600,2
rem print 1280,1024,3

x=.01
y=0
t=0
h=.01
a=3

	for j=1 to 3e3
	t=t+h
	x=x+y*h
	y=y-( a*(x*x-1)*y+x )*h
	print t,0,"gray"
	print t,x,"red"
	print t,y,"blue"
	next j

quit
The “rem” at the left of the top line is the comment token for Basic. It may be that the reader put her copy of Chipmunk Basic in a different place from where I put mine, so I respectfully invite her to make the change. I respectfully point out that for Microsoft Windows the download of Chipmunk Basic comes with two interpreters: chipmunkbasic.exe cannot use the standard output, and basic.exe cannot do graphics. I use the latter.

I found out the hard way that the quit at the end of the program is really necessary. To top

Graph and the Escape key: Polar and no

To the actual colors I add a fictitious color called “no” which provides a cheap substitute for aspect ratio correction. The following program in FreePascal shows its use:
(* ScalePp.bat graphPolar
*)
program graphPolar;

var
theta,r,x,y: double;
n,j: longint;

begin
writeln( 600,' ',600,' ',2 );
n:=1000;

	for j:=1 to n do
	begin
	theta:=2.0*pi*j/n;
	r:=sin(theta+0.4321)-0.5;
	x:=r*cos(theta);
	y:=r*sin(theta);
	writeln( x,' ',0,' lightgray' );
	writeln( 0,' ',y,' lightgray' );
	writeln( x,' ',y,' red' );
	writeln( y,' ',x,' no' );
	end;

end.
The ScalePp.bat is
@if exist %1.exe del %1.exe
@if exist %1.o del %1.o
@c:\FPC\2.2.2\bin\i386-win32\fpc.exe %1.pp
@if exist %1.exe %1.exe
Allow me please to point out that the writeln of Pascal does not always put blanks between the fields, so ' ' must be inserted to make blanks. Also, I have changed my 800 to 600, so as to use a square canvas. Then I respectfully point to the two lines
	writeln( x,' ',y,' red' );
	writeln( y,' ',x,' no' );
to show my trick. For every genuine plotted point, I plot also the transpose of that point, but I plot it fictitiously using no color. This forces the horizontal and vertical scales to agree. No color is not the same as white, because white might white-out a point already plotted in a visible color, but no cannot do such a thing.

The curve is a slightly tilted limaçon, a familiar polar curve from the calculus book. Without some kind of correction, it would have wrong aspect ratio. Readers wishing to see the tranposed curve and how it works are respectfully invited to change the no to pink.

I nearly forgot to mention the (* and *) tokens for a Pascal comment. ScaleDownEdit is not expecting a right-hand token, so the *) must be dropped down to the next line. To top

Graph and the Escape key: Lissajous

My last example is somewhat more light-hearted. It is a Lissajous graph printed in six colors. I stole the appearance of this from an animated True Basic™ program called lissabox.tru. That was back in 1985. Those were the days. My drawing, however, does not move, and my algorithm is different and much simpler. True Basic™ can still be purchased. The present program is in the EcmaScript language, in this case Rhino.
// java -jar c:/rhino/rhino1_7R1/js.jar graphLissajous.js
print( 800,600,20 );
var n=5e2;
var kuller=["red","orange","yellow","green","blue","violet"];

	for( var j=0;j<n;j++ )
	{
	var x=Math.PI*2*j/n;
	print( Math.sin(3*x), Math.sin(4*x), kuller[j%6] );
	}
The // token is the comment token. Very possibly I put my js.jar in a different place from where the reader put hers. To top

Caught in a loop

If the program seems to be running much too long, it may be that it is caught in a loop, and the programmer needs to stop it and the editor. The only sure way to do this in Microsoft Windows is to hold down the control and alternate keys and push the delete key. This will start the Windows Task Manager. Select “wish” or “wish85” or whatever its name is, and stop its process. Then select the running program, “lua” or “java” or whatever it may be, and stop its process too. Then stop the Windows Task Manager. It will then be possible to restart ScaleDownEdit and make a correction to the program.

I respectfully suggest that this is the only method that is sure to work. Stopping just the editor or just the program will fail astonishingly. One must stop both on the same occasion of using Windows Task Manager. To top

License, revision date, and e-mail address

All the files in my zip file, and the zip file too, are in the public domain. On the other hand, the computer language compilers and interpreters that I mention are not mine at all, and they are protected by trade marks and copyrights. The date of my most recent revision is 6 November 2008. Please send criticism, both constructive and destructive, to me, Harold Kaplan,
       at        dot
smtw2gh  toadmail   com
To top

For links to some of Harold Kaplan’s programs, click on programming.htm.