Next: Fortran and C Up: Programming in Maple Previous: Calling External Programs

File Input/Output of Numerical Data

We wish now to discuss in more detail how to output data from Maple into a file suitable for reading by another program, and how the data should be formatted by the external program so that Maple can read it in.

Unfortunately, Maple's file I/O capabilities are almost non-existent so this is somewhat difficult. Maple version V also has no primitive reading capabilities for reading say a line of numbers separated by spaces, or even a line of text. The first difficulty then is that Maple will only read data in Maple syntax. The second difficulty is that Maple V cannot read floating point data which is printed in scientific ``E'' notation, e.g. the number 123.456 printed in scientific notation, in Fortran, would be


0.123456E3

Neither can Maple output floating point data in this standard notation. We will provide a utility routine for converting Maple floating point numbers to strings in the ``E'' notation below in order that numerical data can be output by Maple using the lprint function. The difficulty of reading data back into Maple is that it must be a valid Maple expression. E.g. suppose the program wants to return numerical data values . Then the output file should output something like


result := [x1, x2, ..., xn];

I.e. an external program must include the assignment statement, the commas, and yes, the semicolon. This is not difficult to program in C or Fortran. What is not so easy is outputting numerical data so that Maple can read it. There are two possibilities. Firstly to use decimal notation, i.e. 123.456 instead of scientific notation 0.123456E3 . The second possibility is to write floating point numbers using Maples Float notation, i.e. Float(123456,-3). Note this difficulty has been resolved in Maple V Release 2 which can read and write floating point data in ``E'' notation. Here is a program to convert a Maple floating point number into E notation. The program takes an optional suffix to allow you to specify D for Fortrans double precision. It outputs a string.


FFF := proc(f)
local mantissa,exponent,letter,prefix;

  if nargs = 1 then letter := 'E'
  elif type(args[2],string) then letter := args[2]
  else ERROR(`2nd argument must be a letter`)
  fi;

  if type(f,integer) then mantissa := f; exponent := 0
  elif type(f,float) then mantissa := op(1,f); exponent := op(2,f)
  else ERROR(`integer or float expected`,f)
  fi;

  if mantissa = 0 then prefix := '`0`'
  elif mantissa < 0 then prefix := '`-`'; mantissa := -mantissa
  else prefix := '``'
  fi;

  if mantissa <> 0 then
      exponent := exponent+length(mantissa)-1;
      mantissa := ``.mantissa; # convert to string
      prefix := cat(prefix, substring(mantissa,1..1));
      mantissa := substring(mantissa,2..length(mantissa));
  fi;

  cat(prefix,'`.`',mantissa,letter,exponent)

end:

For example,


> x := 1.234;

                                   x := 1.234

> lprint(x); # this prints in decimal notation okay
1.234

> x := 0.001234;

                                  x := .001234

> lprint(x); # but this prints in the Float notation
Float(1234,-6)

> y := FFF(x);

                                 y := 1.234E-3

> whattype(y);
                                     string

> lprint(y);
1.234E-3


bondaren@thsun1.jinr.ru