fact - factorial

Syntax:

fact(float x)

Description:

Returns the factorial for each member of x. The factorial of n (n!) is equal to n*(n-1)*(n-2)* ... * 3 * 2 * 1

  • fact() will return undef for any non-integer or negative numbers

Examples:

 x

fact(x)

0
3
4
5

1
6
24
120

fault - bilinear interpolation

Syntax:

float fault(float x, float y, float z, float xgrid, float ygrid, float xreg, float yreg, float xbar, float ybar , float estimation, float sorting, float radius, float smoothing, float hitTolerance, float radiusFactor, float feedbackFactor)

Description:

Interpolate the irregular points (x,y) with value z into a rectangular grid.

The method:

  1. The value at each node is calculated from all the input points that are within radius from the node using the specified estimation method. If a node is within hitTolerance of one or more input points, then the value of the node is taken to be the closest input point.
  2. The grid is smoothed using quadratic interpolation. Quadratic interpolation is a nonlinear filtering operation which tends to amplify local extrema. See smoothing, radiusFactor and feedbackFactor
  3. Finally the values are refined using a weighted average of the surrounding nodes. This step can be repeated a number of times. See smoothing.

The new gridded surface will smooth the original data points, but will not necessarily pass through them.

  • The x location of the grid is defined like so:

    • If xgrid is an array then xgrid is used.

    • If xgrid is a single value, range(x,xgrid) is used

    • If xgrid is not specified, range(x,33) is used.

  • The y location is determined in a similar way

  • x and y must not contain any undef points. If they do you can use the following code to get rid of them:

undefpoints = (x=undef) or (y=undef);
x = mask(x, not undefpoints);
y = mask(y, not undefpoints);
z = mask(z, not undefpoints);

  • xreg and yreg can be used to specify a region to interpolate within.

    • Each region must be specified as a closed polygon

    • Each region must be separated by an (undef,undef) point

    • It is also possible to interpolate over the entire grid and then apply the region what the grid is plotted. This has the advantage that the plot will contour right up to the edges of the region, rather than leaving a jagged edge.

  • xbar and ybar can be used to specify a barrier in the interpolation. This ensures that the interpolation on one side of the barrier is unaffected by the values of points on the other side of the barrier.

  • estimation is used to specify the estimation method used in step 2:

     2   Bilinear Interpolation (default) 
     1  Distance Weighted average interpolation

  • sorting is ignored. hitTolerance is specified as a multiple of the distance between two grid nodes. The default is 0.5.

  • radius can be used to specify a search radius. During step 1 a value is calculated for each grid node. The search radius can be used to limit the number of input points that are included in that approximation. Use as small a radius as possible to reduce interpolation time.

  • The smoothing level is used to control whether steps 2 and 3 are used. Step 2 is used if the smoothing is >= -1. Step 3 is applied (smoothing+1) times. The default value of smoothing is 0, which means that step 2 is applied and then step 3 is applied once.

  • It is possible to control the quadratic interpolation in step 2 using radiusFactor and feedbackFactor. radiusFactor must be > 1 (default is 1.25) and feedbackFactor must be > 0 (default is 0.15).

See also:

bivariate, bilinear, polynomial, range, nicerange, unique

fclose - close file

Syntax:

fclose(float fptr)

Description:

Close the file. fptr is the value returned by fopen

  • All open files are automatically closed by the GSL command reset all and if Gsharp is exited.

Code sample:

f = fopen("index.htm");
fprintf(f,"<HTML><HEAD><TITLE>My Home Page</TITLE></HEAD>");
fprintf(f,"<BODY>My home page<A HREF=http://www.a"+
          "vs.com>has moved</A></BODY>");
fclose(f);

See also:

fopen, fprintf, fread, fseek, ftell, fflush, fwrite

fflush - flush the file

Syntax:

fflush(float fptr)

Description:

Flush any buffered output to the file without closing the file.

  •  fptr is the value returned by fopen

Code sample:

f = fopen("log.dat");
while true
  fprintf(f, "The time is "+time);
  fflush(f);
endwhile

See also:

fopen, fprintf, fread, fseek, ftell, fclose, fwrite

fftabs - absolute value of Fourier transform

Syntax:

fftabs(float y)

Description:

Returns the absolute value of a Fourier transformation

  •  fftabs(y) is equivalent to sqrt(fftsin(y)**2+fftcos(y)**2)

Code sample:

#taken from $UNIDIR\example\Gsharp\filter.gsl
Time = range(0//1.5,500);
Car = cos(2*pi*16*Time);
Mod = cos(2*pi*2*Time);
OSignal = 3*(1+.6*Mod)*Car;

Noise = normrnd(500)*.7;
DSignal = OSignal+Noise;

S = fftsin(DSignal);
C = fftcos(DSignal);

Spec = fftabs(DSignal);

See also:

fftcos, fftsin

fftcos - cosine Fourier transform

Syntax:

fftcos(float x)

Description:

Returns the cosine Fourier transformation of x

  •  x is equivalent to invfftreal(fftcos(x),fftsin(x))

Code sample:
#taken from $UNIDIR\example\Gsharp\filter.gsl
Time = range(0//1.5,500);
Car = cos(2*pi*16*Time);
Mod = cos(2*pi*2*Time);
OSignal = 3*(1+.6*Mod)*Car;

Noise = normrnd(500)*.7;
DSignal = OSignal+Noise;

S = fftsin(DSignal);
C = fftcos(DSignal);

Filter = 0//repeatx(1,50)//repeatx(0,400)//repeatx(1,49);
S = S*Filter;  C=C*Filter;
FilteredSignal = invfftreal(C,S);

See also:

fftabs, fftsin, invfftreal

fftimag - imaginary element of complex Fourier transform

Syntax:

fftimag(float r, float i)

Description:

Returns the imaginary element of a complex Fourier transformation

See also:

fftreal, invfftimag, invfftreal

fftreal - real element of complex Fourier transform

Syntax:

fftreal(float r, float i)

Description:

Returns the real element of a complex Fourier transformation

See also:

fftimag, invfftimag, invfftreal

fftsin - sine Fourier transform

Syntax:

fftsin(float x)

Description:

Returns the sine Fourier transformation of x

  •  x is equivalent to invfftreal(fftcos(x),fftsin(x))

See also:

fftabs, fftsin, invfftreal

filelist - return list of files matching pattern

Syntax:

string filelist(string fileSpec)

Description:

filelist returns a list of files matching the given fileSpec

  • If no files match the fileSpec, then undef is returned

  • Remember that the backslash has special meaning. Use "\\" or "/" e.g.
  • docs = filelist("c:\\my documents\\*.doc"))
    
    docs = filelist("c:/my documents/*.doc");
    
  • The filenames returned by filelist() do not include the directory - you must add this back in if you need it. See example below:

Code sample:

function ReadAllFolders(string dir)
  string file;

  for file in filelist(dir +"/"+"*.fold")
    if (file=undef)  continue;
    import_folder(dir+"/"+file, file-".fold");
  endfor
endfunction

See also:

rename, remove, system, getdir, dirlist

fopen - open file

Syntax:

float fopen(float filename, string mode)

Description:

Open filename with the specified mode. 

  • The filename can include environment variables, e.g. $UNIDIR

  • mode can be any of the values taken by the C function fopen. These include:

    • "r" - read
    • "w" - write
    • "a" - append, create if necessary
    • "r+" - read or write
    • "w+" - create new, then read or write
    • "a+" - read or write, open at end
  • fopen returns a float identifier to the file which can be used with fprintf, fread, fwrite, fclose, etc.

  • The following identifiers are also available within Gsharp: stdin (0), stdout (1) and stderr (2).

See also:

fclose, fprintf, fread, fseek, ftell, fflush, fwrite

fprintf - write to open file

Syntax:

float fprintf(float fptr, string format, any var1, any var2, ...)

Description:

Write formatted I/O to the file specified by fptr. The format is a C language format string - see examples.

  • fprintf returns the number of characters output

  • fptr is returned from fopen or is either stdout or stderr

Examples:

fprintf(2, "Mayday Mayday");
fprintf(stdout, "X = %d and Y = %g\n",X,Y);
fprintf(fptr,"%02d/%02d/%4d",daynumber(today), monthnumber(today),
                   yearnumber(today));

See also:

fclose, fopen, fread, fseek, ftell, fflush, fwrite, printf

fprob - probability density function of an F distribution

Syntax:

float fprob(float x, float f1, float f2)

Description:

Return the probability that a F distributed variable with f1 and f2 degrees of freedom will have a value less than or equal to x

  • The result is accurate to approximately 2 significant figures

  • An F distribution is a continuous distribution obtained from the ratio of two chi-square distributions and used esp. to test the equality of the variances of two normally distributed variances.

See also:

normprob, tprob, x2prob

fread - read from a file

Syntax:

float fread(float fptr, var1, var2, ...)

Description:

Read data from the file specified by fptr. The read data is stored in var1, var2, etc.

  • fptr is returned from fopen or could be stdin

  • Each variable is read in turn

  • A value is read from the file for each element of the input variable.

  • The dimensions of var1, var2, etc control how many elements are read. e.g.

  • float g[10,10];
    fread(fptr, g);
  • If the input variable is a string, date or time then each elemnt is terminated by a line feed.

  • If the input variable is a date or time and the string cannot be converted then an undef value is stored

  • If the input variable is a float then a binary double is read from the file. In Gsharp 3.1 and before a binary float was read from the file.

  • The function returns the total number of bytes that were read.

Code sample:

function WriteData(string filename, string Titles,
                      string Descriptions, float Scores)
  float f;
  f = fopen(filename, "w");
  fwrite(f,size(Titles));
  fwrite(f,Titles, Description, Scores);
  fclose(f);
endfunction
function ReadData(string filename)
  float f, numrec;
  f = fopen(filename,"r");
  fread(f,numrec);
  NewTitle = repeatx("",numrec);
  NewDescription = NewTitle;
  NewScores = repeatx((0,0,0),numrec);
  fread(f, NewTitle, NewDescription, NewScores);
  fclose(f);
endfunction
Titles = "Andy"//"Dave"//"Sarah";
Description = "England"//"Scotland"//"Wales";
Scores = (95,92,93)//(94,94,94)//(99,87,99);
WriteData("test.fld",Titles, Description, Scores);
reset data;
ReadData("test.fld");
for i=1 to size(NewTitle)
  printf("%s (%s) - %d:%d:%d\n",NewTitle[i],NewDescription[i],
               NewScores[i,1],NewScores[i,2],NewScores[i,3]);
endfor

See also:

fclose, fopen, fprintf, fseek, ftell, fflush, fwrite, printf

fread_old - read from a file

Syntax:

float fread_old(float fptr, var1, var2, ...)

Description:

Exactly the same as fread, except numbers are assumed to have been stored as floats rather than doubles.

  • Use this function to read files written with fwrite from Gsharp 3.1 or earlier. See example.

  • If you wish, you can continue to store your data as floats, by changing all your calls from fread() and fwrite() to fread_old() and fwrite_old().

Code sample:

function ConvertFileFormat()

  for filename in filelist("*.fld")
    ReadOldData(filename);
    WriteNewData("new/"+filename);
  endfor
endfunction
function ReadOldData(string filename)
  float f, numrec;
  f = fopen(filename,"r");
  fread_old(f,numrec);
  Titles = repeatx("",numrec);
  Description = repeatx("",numrec);
  Scores = repeatx((0,0,0),numrec);
  fread_old(f, Titles, Description, Scores);
  fclose(f);
endfunction
function WriteNewData(string filename)
  float f;
  f = fopen(filename, "w");
  fwrite(f,size(Titles));
  fwrite(f,Titles, Description, Scores);
  fclose(f);
endfunction

See also:

fread, fwrite_old

fseek - set position of file pointer

Syntax:

float fseek(float fptr, float offset, float origin)

Description:

Position the file pointer of the file specified by fptr

  • fptr is the value returned by fopen

  • offset is the number of bytes to move

  • origin is the point at which to start counting the offset. Possible values are: 0 (beginning), 1 (current position) and 2 (end of file).

  • fseek returns zero on success

Examples:

function float FileSize(string filename)
  f = fopen(filename, "r");
  if f<0 return undef;
  fseek(f,0,2);
  fsize = ftell(f);
  fclose(f);
  return f;
endfunction

See also:

fclose, fopen, fprintf, fread, ftell, fflush, fwrite, printf

ftell - get position of file pointer

Syntax:

float ftell(float fptr)

Description:

Returns the position of the file specified by fptr as the number of bytes from the beginning of the file. 

  • fptr is the value returned by fopen

  • fseek returns -1 on failure

Examples:

function ReadRecord(filename, n);
  float f, fp, fp2;
  f = fopen(filename);
  ReadHeader(f);
  fp = ftell(f);
  ReadDummyRecord(f);
  fp2 = ftell(f);
  RecordSize = fp2-fp;
  fseek(f,1,RecordSize*(n-1));
  ReadRecord_();
endfunction

See also:

fclose, fopen, fprintf, fread, fseek, fflush, fwrite, printf

fwrite - write to a file

Syntax:

float fwrite(float fptr, var1, var2, ...)

Description:

Write data to the file specified by fptr.

  • fptr is returned from fopen or is either stdout or stderr

  • Each variable is written in turn

  • Floats are written in binary double format (Gsharp 3.1 and earlier wrote binary floats).

  • Strings, dates and times are written as ASCII strings.

  • If you want to save disk space you could use daysince(mydate) and secsince(mytime) when you wrote days and times and invdaysince(d) and invsecsince(t) when you read them back.

  • The written file is most easily read by fread.

  • The function returns the total number of bytes written. e.g.
    RecordSize = fwrite(f, Title, Name, Occupation, Address, Age, Sex, Salary);

Code sample:

See fread

See also:

fread, fclose, fopen, fprintf, fseek, ftell, fflush, printf

fwrite_old - write to a file

Syntax:

float fwrite_old(float fptr, var1, var2, ...)

Description:

Exactly the same as fwrite, except that numbers are stored as floats rather than doubles.

  • If you have been using fread() and fwrite() from Gsharp 3.1 or earlier and want to carry on as before with the minimum of effort then just change all of your calls from fread() and fwrite() to fread_old() and fwrite_old().

See also:

fread_old, fwrite