Snippets
Simple
Basics
fdm language
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Basics
# Single line comment
/# Multi
line
comment #/
/* Alternative
multi
line
comment */
# 5 basic types
character = 'c';
string = "abc";
print( character );
print( string );
number1 = 123;
number2 = 123.456;
number3 = 123.456E789;
number4 = 123.456E-789;
print( number1, number2,
number3, number4 );
# Some expression
# examples
# Numbers are stored
# accurately and
# calculations by
# default are performed
# to 50 digits accuracy
print( number3 * number4,
number3 / number4,
number3 ^ 2000,
number4 ^ 2000 );
# Next basic type is
# a data series
# Observation dates depend
# on calculation frequency
# and can also simply be an
# index
# Empty series
dataSeries1 = empty();
# Starting date
# followed by
# observations
dataSeries2 =
observe(
dateIndex( 2015, 12, 31 ),
1, 2, 3 );
# Date given as a number
# Interpretation depends on
# frequency
dataSeries3 =
observe( 1000, 1, 2, 3, 4 );
print( dataSeries1,
dataSeries2,
dataSeries3 );
# Matrix
# 2x3 filled with 0
A = matrix( 2, 3 );
# 2x3 with 1 then 0s
B = matrix( 2, 3, 1 );
# 2x3 with 1 in first
# row and 2 in second
C = matrix( 2, 3,
1, 1, 1, 2, 2, 2 );
print( "Matrices",
A, B, C );
# end basic types
# More expressions
a = 1 + 2 + 3;
b = 5 + 6 / 2;
# b is 8 not 5.5
pow = a ^ b;
cmp = a == b;
notEq = a != b;
altNotEq = a <> b;
print( "Expressions", a,
b, pow, cmp,
notEq, altNotEq );
# Case sensitive!!!
a = 1;
A = 2;
print( "Case!", a, A, a == A );
# Indexing
s = "abcdef";
print( s[1] ); # "a"
print( s[1,3,5] ); # "ace"
print( s[2:4] ); # "bcd"
print( s[6,1,2] ); # "fab"
# The same indexing ideas apply
# to matrices, but then with
# rows and columns separated
# by a semi colon e.g.
# A[ 1,2 ; 3,4 ]
# or
# A[ 1:2, 3:4 ]
# to get a submatrix
cpi = observe( today(),
100, 101, 102 );
l = lastDate( cpi );
print( "Series", cpi[ l ] );
print( cpi[ l ] / cpi[ l - 1 ] );
mat = matrix( 2, 3,
1, 2, 3, 4, 5, 6 );
i = 1;
j = 1;
n = rows( mat );
m = cols( mat );
aij = mat[ i; j ];
rowi = mat[ i; 1:m ];
colj = mat[ 1:n; j ];
sub = mat[ 1,2;1,2 ];
rev = mat[ 2,1;3,2,1 ];
cat0 = matrixCatColumns( mat,
matrix0( 2 ) );
catI = matrixCatRows( mat,
matrixI( 3 ) );
print( "Index", aij, rowi,
colj, sub, rev,
cat0, catI );
# Leads and lags
lag1 = cpi(-1);
lag2 = lag( cpi, 2 );
lead1 = cpi(+1);
lead2 = lag( cpi, -2 );
print( "L&L", cpi,
lag1, lag2,
lead1, lead2 );
# The language is a
# functional language
# Note that if and loop
# statements are nothing
# more than functions
# with arguments
# A single argument can
# be replaced by a bracketed
# group of statements, e.g.
# ( a, b, ( c, d ), e ) or
# ( a, b, { c; d }, e )
# If
print( "Ifs" );
# Prints TRUE
if( 1, print( "TRUE" ) );
# Prints FALSE
if( 0,
print( "TRUE" ),
print( "FALSE" ) );
# Body of statements
if( 1,
{ print( 1 ), print( 2 ) },
{ print( 3 ); print( 4 ) } );
# Loops
print( "Loops" );
i = 1;
while( i < 10,
print( i ),
i = i + 1 );
Lots of pi
10,000 digits of pi
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Lots of digits of pi
# We will loop this a couple of times
# and each time calculate more digits
# of pi
# Create a 7x1 matrix
pMat = matrix( 7, 1,
50,
100,
500,
1000,
2500,
5000,
10000 );
# It has only one column with
# entries the number of digits
# we want to calculate
# Loop the rows
r = 0;
while( r < 4, {
r = r + 1;
n = pMat[ r; 1 ];
# Note the accuracy is a bit more
# as we add a few guard digits
accuracy ( n * 1.05 );
# Use capital P, small p means percentage change
format( 0 );
calcStatus( n + " digits" );
P = pi();
# Print a header
print( "Accurate to " + n + " digits" );
print( "---------------------------------------" );
# Now print the digits
format( n );
pStr = "" + P;
# Print in blocks of 50 digits
i0 = 0;
i1 = 53; # First block has 3 extra for +3.
while( i0 < length( pStr ), {
# Align
if( i0 > 0, a = " ", a = "" );
# Correct
if( i1 > length( pStr ), i1 = length( pStr ) );
# Print
print( a + pStr[ i0 + 1:i1 ] );
# Prepare for next round
i0 = i1;
i1 = i1 + 50;
} );
} );
Lots of e
Print 10,000 digits of e
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Lots of digits of e
# We will loop this a couple of times
# and each time calculate more digits
# of e
# Create a 7x1 matrix
eMat = matrix( 7, 1,
50,
100,
500,
1000,
2500,
5000,
10000 );
# It has only one column with
# entries the number of digits
# we want to calculate
# Loop the rows
r = 0;
while( r < rows( eMat ), {
r = r + 1;
n = eMat[ r; 1 ];
# Note the accuracy is a bit more
# as we add a few guard digits
accuracy ( n * 1.05 );
# Use capital E to store e
format( 0 );
calcStatus( n + " digits" );
E = e();
# Print a header
print( "Accurate to " + n + " digits" );
print( "---------------------------------------" );
# Now print the digits
format( n );
eStr = "" + E;
# Print in blocks of 50 digits
i0 = 0;
i1 = 53; # First block has 3 extra for +2.
while( i0 < length( eStr ), {
# Align
if( i0 > 0, a = " ", a = "" );
# Correct
if( i1 > length( eStr ), i1 = length( eStr ) );
# Print
print( a + eStr[ i0 + 1:i1 ] );
# Prepare for next round
i0 = i1;
i1 = i1 + 50;
} );
} );
Loop
Calculate sums inside a loop
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Loop
# Calculate the sum of x,
# x square and x cubed from
# 1 to n
start = 1; # Loop start
stop = 100; # Loop end
# x1 is sum of x
# x2 is sum of squares
# x3 is sum of cubes
x1 = x2 = x3 = 0;
# Now we do the loop
index = start;
# The loop is a while loop
# while ( condition, ... );
# This is the exit condition
while( index <= stop,
# The loop body is in here
# Normal expressions
# Separated by commas
# Set x
x = index,
# Update sums
x1 = x1 + x,
x2 = x2 + x * x,
x3 = x3 + x * x * x,
# Do not forget this, or the
# loop will never terminate
# You can force it to stop
# by touching the rotating
# progress indicator
index = index + 1 );
# Print results
# See log for details
format( 0 );
print( "Sum from ", start );
print( " to ", stop );
print( " Of x ", x1 );
print( " Of x squared", x2 );
print( " Of x cubed ", x3 );
# The loop again,
# without comments
x1 = x2 = x3 = 0;
index = start;
while( index <= stop,
x = index,
x1 = x1 + x,
x2 = x2 + x * x,
x3 = x3 + x * x * x,
index = index + 1 );
String
Swap two words
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Strings
# Swap the first two
# words in a string
s = "This is a string";
print( "[" + s + "] becomes" );
# Find position of first
# space
# (First character is at
# index = 1 or s[1] which
# returns a string of length 1)
i1 = 0;
f = 0; # 1 when found
while( and( not( f ),
i1 < length( s ) ),
i1 = i1 + 1,
s1 = s[ i1 ],
if ( s1 == " ", f = 1 ) );
# Now find position of
# second space
i2 = i1;
f = 0;
while( and( not( f ),
i2 < length( s ) ),
i2 = i2 + 1,
s2 = s[ i2 ],
if ( s2 == " ", f = 1 ) );
# Proceed only if found
if ( f,
{
# Get the two words
# Could also use mid here
w1 = s[ 1 :i1-1 ],
w2 = s[ i1+1:i2-1 ],
# Fix capitalisation
# Could also use left here
w1 = lower( w1[ 1 ] ) +
right( w1,
length( w1 ) - 1 ),
w2 = upper( w2[ 1 ] ) +
right( w2,
length( w2 ) - 1 ),
# Concatenate and bracket
s = "[" + w2 + " " +
w1 + " " +
right( s,
length( s ) - i2 ) +
"]",
# Done
print( s )
},
# Else
print( "-- Unable to swap" ) );