12.26Statistical functions. 4tH 4tH Forth compiler
Oglas
Oglas
CHAPTER 12. STANDARD LIBRARIES 200
Note that like ansfloat.4th, fsl-util.4th isn’t included automatically by other library members, so you have to include it explicitly. If not, the compiler will simply abort.
Since the FSL uses special datatypes, you have to do some work in order to get them to work with 4tH. If you want to declare an fsl-array of e.g. ten floats:
10 FLOAT MARRAY MyFSL
You have to declare it like this:
10 FLOATS 1+ ARRAY MyFSL
FLOAT LATEST FSL-ARRAY
DOES> (FSL-ARRAY) ;
( allocation)
( initialization)
( runtime behavior)
If you want to declare an fsl-matrix of e.g. 16 by 8 floats:
16 8 FLOAT MMATRIX MyFSL
You have to declare it like this:
16 8 * FLOATS 2 + ARRAY MyFSL
16 8 FLOAT LATEST FSL-MATRIX
DOES> (FSL-MATRIX) ;
( allocation)
( initialization)
( runtime behavior)
Surely, this is a bit awkward, but it is required to be as compatible as possible with sources that use the FSL. Note that all restrictions and reservations concerning floating point support also apply to the FSL words.
12.26
Statistical functions
4tH features a small array of statistical functions that can be used in conjunction with both the ANS and the Zen floating point libraries. First you have to declare the floating point stack of your choice:
\ select ANS- or ZEN float
\ include the ZEN float library false constant shared-fp shared-fp
[IF] include lib/fp2.4th
[ELSE] include lib/fp4.4th
[THEN] \ include the ANS float library
After you’ve included the statistical library itself you have to allocate the statistical structure required: include lib/statist.4th
/st_var array stats
\ include the statistical library
\ allocate a statistics structure
That was the hardest part. Now, initialize the floating point library and clear the structure so it is ready for use: fclear 100 set-precision stats st.clear
\ initialize FP library
\ clear the structure
CHAPTER 12. STANDARD LIBRARIES
You can now add your floating point numbers to the structure by issuing:
2 s>f stats st.add
4 s>f stats st.add
4 s>f stats st.add
4 s>f stats st.add
5 s>f stats st.add
5 s>f stats st.add
7 s>f stats st.add
9 s>f stats st.add
201
If you have forgotten how many numbers you added, simply query the structure as if it were a normal variable: stats @
A number of statistical functions are now at your disposal, e.g. variance, standard deviation, and several means. You can call them at any time by issuing, e.g.: stats st.stddev f. cr
Note you can have several statistical structures at the same time and manipulate or query them independently. You can also clear them individually at any time without any side effects.
F
UNCTION
W
ORD
Standard deviation st.stddev
Variance
Arithmetic mean st.variance
st.amean
Geometric mean
Harmonic mean st.gmean
st.hmean
Root mean square st.rms
Table 12.12: Statistical functions
Chapter 13
Special libraries
13.1
Infix formula translation
1
Part of the elegance and simplicity of Forth comes from the use of Reverse Polish or postfix notation. As such, there are no requirements for precedence rules, parentheses, or complicated parsers. However, complicated mathematical and scientific equations that are often written in infix notation can be tedious to convert to Forth code. To simplify such a conversion, Julian V. Noble (one of the founders of the Forth Scientific Library, see section
12.25) wrote a FORmula TRANslator
2 to convert infix code to Forth code. Later Wil
Baden, using Noble’s concepts implemented a different version. For an implementation in 4tH, Wil Baden’s version was chosen as it seemed simpler. The 4tH version of this
(opgftran.4th) consists of three main words:
FTRAN
4TH-MATH
( a1 n1 – a2 n2) converts a text string from infix to postfix notation
( – ) sets FTRAN to produce 4tH code as the output text
STANDARD-MATH ( – ) sets FTRAN to produce standard postfix notation for the output text
To see how these work, consider the following code segment: include lib/opgftran.4th
s" (12 - 2)/(6 - 3)" 2dup forth-math ftran type cr cr standard-math ftran type cr
Which results in the following output: s" 12E0" s>float s" 2E0" s>float Fs" 6E0" s>float s" 3E0" s>float F- F/
12 2 - 6 3 - /
1
Article written and contributed by David Johnson.
2
Also known as ”Operator Precedence Grammar”.
202
Preuzimanje
Oglas