Science makes it known,
Engineering makes it work,
Art makes it beautiful.
|
|
Lazarus (Delphi Compatible) Calling Numerical Recipes:
The Art of
Scientific Computing1
Fortran 95 subroutines (Statistics Analysis)
Lazarus
is a Delphi compatible cross-platform IDE (think of it as Visual Object
Pascal). This page looks at interfacing a Lazarus main program2
(ST95DLPH) with
FORTRAN statistical analysis (avg, std dev, etc.) subprograms
using single/REAL*4 arrays. ST95DLPH was
developed on an AMD Athlon II running WinXP Pro SP3 using Free Pascal
Lazarus Project Version# 1.2.6 and Free Pascal 2.6.4.
Its purpose is to show how to further leverage Fortran engineering/mathematical/scientific
libraries with a Lazarus front-end.
Files:
-
ST95DLPH.lpr
- Project Main Source; launches main window and starts the event
processing loop
ST95DLPH.lpi
- Project Information - XML format
ST95DLPH.lps
- XML format of list of unit files ever used in project (even if they have
been later removed)
ST95DLPH.res
- Compiled Resource Script
stunit1.pas
- Pascal Main Unit
stunit1.lfm
- Form (shown below)
NumRecGlbl.pas
- Numerical Recipes
global constants/variables/procedures unit, reused in other Lazarus projects
& Object Pascal Librarys
-
nrlazrs.pas
- user written FORTRAN Numerical Recipes - Lazarus Interface2
Library of Free Pascal procedures (LIBRARY nrlazrs);
Compile with batch file
fpcdll.bat.
ST95DLPH unit stunit1 event procedure
TDataForm.menuCalculateClick(...) calls LIBRARY nrlazrs
procedure rMoment (...), which then calls the appropriate
subroutines in sttstcs.dll to perform the statistical analysis.
LIBRARY nrlazrs procedures can also be called
by non-Lazarus Free Pascal programs. Pascal calling FORTRAN
subprograms
is straight-forward; see Free Pascal Programmer's Guide Chapter 12,
Programming shared libraries.
nrlazrs.dll - nrlazrs.pas dynamic link library
-
genetyp.inc3
- include file developed from major rewrite of Dawkins8 Biomorphs
(Free Pascal dawkins8.dll callable from both Pascal
and D programs); contains useful CONSTant and TYPE
declarations
nrtyp.inc
- Numerical Recipes include file; CONSTant and TYPE
declarations, including unit dimension constants (engineering/scientific applications;
as an example, length is a dimension, but it is measured in units of feet (ft)
or meters (m); unit dimension constants are single char data,
some of which are 8-bit ASCII; these are meant
as indicators (or program control values); can not output these direct
in Windows without ASCII to UTF-8 conversion)
Both include files are used in NumRecGlbl.pas, nrlazrs.pas,
and other Lazarus / Object Pascal source files
-
sttstcs.for
- Numerical Recipes FORTRAN statistical analysis and support
subprograms;
user written subroutine D12R3MINMAX (...),
Numerical Recipes subroutines AVEVAR (...), and
MDIAN2 (...) are called by LIBRARY nrlazrs
procedure rMoment (...); to prevent a possible intellectual
property violation, limited sttstcs.for source code available for
download, since the original FORTRAN source code is
published; the unmodified
Numerical Recipes Statistical Analysis FORTRAN source code is
available at
Older Numerical Recipes book editions.
sttstcs.dll - sttstcs.for dynamic link library; built using
SilverFrost FTN95 Plato IDE
sttstcs.lib
- not used in this application;
this can be either MicroSoft linker compatible (SLINK generated)
OR Digital Mars D compatible (implib generated).
-
mathproc.dll
- not called in this application;
LIBRARY nrlazrs function fRandom (...) calls Numerical Recipes'
IRBIT1 (...) and RAN3 (...) in mathproc.dll
stunit1
-
Has comments explaining both Lazarus and program logic, including http links
for documentation
-
Uses unit NumRecGlbl
-
Uses floating point (single) Data Points (calls different
subroutines in sttstcs.dll than
winstat - D Windows Statistical Analysis)
-
Stores the Data Points as a 1 dimensional array (vector) global variable
(rData[ ]). The array is passed to the FORTRAN subprograms.
-
Displays the Data Points array in a TStringGrid matrix
Properties Options in Object Inspector:
[goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor,
goSmoothScroll]
-
Uses Hints
-
Right justify the TStringGrid cells with:
procedure TDataForm.PointMatrixPrepareCanvas(sender: TObject;
aCol, aRow: Integer; aState: TGridDrawState);
var
MyTextStyle: TTextStyle;
begin
MyTextStyle := PointMatrix.Canvas.TextStyle;
MyTextStyle.Alignment := taRightJustify;
PointMatrix.Canvas.TextStyle := MyTextStyle
end;
(event OnPrepareCanvas in Object Inspector)
|
|
-
Uses OnSetEditText event
[TDataForm.PointMatrixSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string)]
to edit grid cells and save data points in rData[ ] array - some may consider
this inefficient, however, it did what was needed - allow the editing of
each cell as it was entered.
-
Uses TStringList for read/write Data Points files in
procedure TDataForm.menuOpenClick(...) and
procedure TDataForm.menuSaveAsClick(...)
(can read Integer Data Points files produced by
winstat - D Windows Statistical Analysis;
winstat can read stunit1 written files but rounds to the nearest
Integer)
-
LIBRARY nrlazrs procedure rMoment (...) external declaration is
in the interface section, following the var declaration
-
stunit1 is still a little rough, will be refined and enhanced. As it
is enhanced, form stunit1 may change.
NumRecGlbl
-
Secondary Unit
-
Includes genetyp.inc, nrtyp.inc
-
Defines Numerical Recipes global variables used in primary unit stunit1
-
Procedures to convert indices between one dimensional rData[ ]
and two dimensional TStringGrid
-
Has rudimentary command line parameter processing - may not be the
most optimized method but it works; successfully called indirectly from a
D module
-
Unit NumRecGlbl procedures can be called by
non-Lazarus Free Pascal programs - calling unit/library/program
will need to
include unit Interfaces in the uses clause - see
D .dll Calling Free Pascal .dll - Pascal Files
for an example
(Pascal Librarys using Interfaces or LCLIntf
will be large)
Compiling/Linking: 4
-
Create, compile, and link sttstcs.for.
-
Compile and link nrlazrs.pas into nrlazrs.dll:
Open a MS-DOS Command Prompt
fpcdll nrlazrs
-
Compile and link ST95DLPH
Run Lazarus, Open and compile (Run, Compile)
ST95DLPH
1. Press, William H., Brian P. Flannery, Saul A Teukolsky,
and William T. Vetterling (1986). Numerical Recipes: The Art of Scientific
Computing.
New York:Press Syndicate of the University of Cambridge.
2. Called via a stand-alone Free Pascal .dll. Possibly related to a
known Windows problem,
calling direct may produce an Access Violation Exception when
TDataForm.menuCalculateClick(...) (which performs statistical analysis
using Numerical Recipes FORTRAN subroutines) exits. However,
when called via a Free Pascal Library procedure, no exceptions are
produced. In either case, no data corruption occurred, results
were correct.
Have also seen this in Free Pascal Library missle13.pas procedure
BGEPCD (...), which directly/indirectly calls multiple FORTRAN
subprograms. Again, no data corruption occurred, results
were correct. try . . . except block in calling procedure/function
allowed program to continue. Library has several procedures which call
FORTRAN subprograms, only BGEPCD (...) has problem.
3. Rietman, Edward (1994). Genesis Redux: Experiments Creating Artificial
Life. Windcrest (McGraw-Hill).
The original Dawkins8 source consisted of one unit and one MS-DOS main program
(without any modularity whatsoever). Broke that down into two Windows
callable .dll's (genecode.dll and dawkins8.dll) and one
include file. The main .dll is now very modular.
4. For compatibility with other programs, using 32 bit compiler.
ATTENTION:
When doing a Google Search for "Lazarus Numerical Recipes"
or "Lazarus calling FORTRAN" you may find several references for
Google Docs (or perhaps web pages with a TLD in Eastern Europe - no
derogatory intended) with the same terms and even graphics used in my Lazarus
web pages. THE Google Docs ARE NOT MINE. They
may have links for downloadable executable files which may or may not contain
malware, adware, or other undesirable features.
This may be a new trend in distributing malware - have seen this with my
COBOL web pages.
Exercise Caution
|