iconEuler Reference

Basic Utilities


Basic utility functions.

This file collects some important utility functions.

Reset Euler

Resetting Euler is best done with the menu item, or with Ctrl-N. Then Maxima is restarted too. All variables are lost. You will be asked to save your notebook first. After reset, Euler calls the reset function below.

function Pi () := pi();
function overwrite setfont (size=none,height=1,name="",
  Set the font relative to the screen size
  Small prints of graphics require a larger font size relative to the
  maximal dimension of the graphics. This function sets the font
  size. The default is about 12 pixel for the HTML export with images
  of maximal dimensions of 400.
  The default name is blank. This sets the default font, which is
  "Arial" when Euler starts. But the user can set the default font
  permanently in the Menu.
  size : Size of font.
  height : Size of the print in the same unit.
  name : Font name. Default is "Arial".
  markersize : Fraction of the text height for the marker radius.
  Uses the internal function _setfont(lines,name).
  setfont(11pt,10cm) : 11 point font for a print of 10cm height.
  setfont(12,400) : This is a good font size for web pages.
  setfont() : Resets to the default 1/40 of the screen.
setfont (Euler Core)
function resetepsilon 
  Reset the default epsilon and return the current epsilon.
function overwrite exec (program:string, param:string="", dir:string="",
    print:integer=0, hidden:integer=0, wait:integer=1)
  Execute an external program
  This is a utility function for the built-in exec command. It runs
  an external program, and by default waits for it to finish. The
  result of the program can be returned in form of vector of strings.
  Communication is via standard named pipes. The pipes stdout and
  stderr are read as output from the program.
  By default, the builtin function exec will block and wait for user
  confirmation. This prevents security problems with downloaded Euler
  notebooks. The confirmation can be switched off in the options. It
  can be switched off in the first confirmation dialog for one
  If wait is false and print is true, the function starts the
  program, and generates pipes to and from the program. These pipes
  can later be written to with execwrite(string) or
  execwriteln(string) and read from with execread(), returning a
  vector of strings. Only one such program is available. Trying to
  start a second program with open pipes terminates the first one.
  program : the name of the program (plus path, if not in the system
  param : Parameters separated by blanks. If the parameters contain
  blanks, they must be enclosed by double quotes. Use strings as in
  param=''"first parameter" "second parameter"''
  dir : The working directory of the program. This can be used to
  locate the program instead of a full path.
  print : If true and wait is false, the function returns the output
  immediately in form of a vector of strings. If true and wait is
  true, the program will print the output of the program.
  hidden : If true, no window will be generated.
  wait : If true, the function will wait for the command to be
  finished, or for the escape key.
  >exec("cmd","/c dir /w *.en",home(),>print,>hidden,>wait)
  Volume in Laufwerk C: hat keine Bezeichnung.
  Volumeseriennummer: ECF3-71E6
  Verzeichnis von C:\euler\docs\Programs
  >filename=eulerhome()+"test.txt"; ...
  writematrix(random(5,5),filename,"%1.5f"); ...
  exec("cmd","/c start "+filename,eulerhome(),<wait);
  >execwriteln("dir /w");
  Microsoft Windows [Version 6.1.7601]
  Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.
exec (Euler Core),
execwrite (Euler Core),
execwriteln (Euler Core),
execread (Euler Core)

Vector and Matrix Functions

Functions to generate matrices or vectors with values spaced in various ways, as well as functions providing informations about vectors and matrices.

function matrix 
  matrix(v1,v2,v3,...) returns a matrix with rows v1,v2,v3,...
function equispace (a:real scalar, b:real scalar,
    n: positive integer scalar)
  Generates n+1 arcsin spaced values in [a,b].
  The arcsin distribution generates points which are the extremal
  points of the Chebyshev polynomial. These points are well suited
  for discrete approximation on an interval.
linspace (Euler Core),
chebzeros (Basic Utilities)
function logspace (a:positive real scalar,b:positive real scalar,
    n:positive integer scalar)
  logspace(a,b,n) generates n+1 log spaced points in [a,b]
equispace (Basic Utilities),
linspace (Euler Core)
function chebzeros (a: real scalar, b: real scalar,
    n: positive integer scalar)
  Generate the zeros of the n-th Chebyshev polynomial in [a,b]
linspace (Euler Core),
equispace (Basic Utilities)
function length (v)
  Returns the length of a row or column vector.
  In fact, the function returns the maximum of the number of rows,
  and the number of columns of a matrix.
function differences (s)
  Computes vector of the differences s[i+1]-s[i]
fold (Numerical Algorithms)
function tail (s, n:integer)
  The rest of the vector s starting from the n-th element.
  tail(s,-2) returns the last two elements of s.
head (Basic Utilities)
function head (s, n:integer)
  The vector, up to the n-th element
  The function works for negative n too. head(s,-2) returns all
  elements, but the last.
tail (Basic Utilities)
function args field (x:vector, y:vector)
  Returns {X,Y} such that the X+i*Y is a rectangular grid
  x and y must be 1xN and 1xM vectors.
  This is not necessary in most cases, since operators in the Euler
  matrix language combine x and y' automatically.

Text Menus

This is a simple text menu. It can be used for files with a user choice of options.

function menu ()
  Displays a menu and returns the number of the chosen item
  This is a simple menu in text form inside the text window of Euler.
  The menu items are the arguments to this function. The items should
  be labelled (a), (b), etc. The user can press the keys a, b, etc.,
  or the return key. In this case, the functin returns -1. Else the
  function returns the number of the chosen argument.

Number Bases

Functions for hexadecimal or binary input and conversion.

function baseinput (s:string, b:index=16)
  Read a number in base b.
function hex (s:string)
  Convert a hex string.
printhex (Basic Utilities),
baseinput (Basic Utilities)
function dual (s:string)
  Convert a hex string.
printdual (Basic Utilities),
baseinput (Basic Utilities)
function printbase (x:real scalar, base:index=16, digits=13,
  Prints the real number x with mantissa and exponent to base b.
  The output is of the form 1.xxxxx...xxx*2^n with mantissa of
  length digits. If integer is true, the value is assumed to
  be integer, and it prints in the form xxxxx. x is between 0 and
  base, where A=11, B=12, etc.
  >dual("10001001"), printdual(%,>integer)
  >baseinput("24256",7), printbase(%,base=7,>integer)
baseinput (Basic Utilities)
function printdual (x:real scalar, integer=false, length=1)
  Prints the real number x with dual mantissa.
  The output is of the form 1.xxxxx...xxx*2^n. If integer is true,
  then the output is of the form xxxxxx with x=0 or x=1.
  >dual("10001001"), printdual(%,>integer)
function printhex (x:real scalar, integer=false)
  Prints the real number x with hexadecimal mantissa.
  The output is of the form 1.xxxxx...xxx*16^n. If integer is true
  the output is of the form xxxxx where x is 0,...,9,A,B,C,D,E,F.
  >hex("DDFFD"), printhex(%,>integer)


function day (y,
    m:integer scalar=1, d:integer scalar=1,
    h:integer scalar=0, min:real scalar=0,
  Convert a date to a number of days.
  The computation is based on the Gregorian calendar.
  The date can be a format string in ISO8601 format (yyyy-mm-dd) with
  an optional time (hh:mm or hh:mm:sec) appended with a blank. The
  function will detect the alternative day format (dd.mm.yyyy), and
  an English month name instead of a numerical value.
date (Basic Utilities)
function daynow (utc=0)
  Date number for the current day.
  The computation is based on the Gregorian calendar.
  utc : Use UTC time.
day (Astronomical Functions),
date (Basic Utilities),
getnow (Basic Utilities),
printnow (Basic Utilities)
function comment getnow (utc)
  Current date
  utc : in UTC or local time.
daynow (Basic Utilities)
function overwrite printnow (utc=0)
  Current day and time as string.
  The functions uses the locale settings of the user.
getnow (Basic Utilities),
daynow (Basic Utilities)
function args date (d:number)
  Convers a day number to the date.
  >{y,m,d,h,min}=date(day(2012,11,1,23,12)); [y,m,d,h,min]
  [ 2012  11  1  23  12 ]
day (Astronomical Functions),
day (Basic Utilities)
function printdate (y:integer scalar, m:integer scalar,
    d:integer scalar, h:integer scalar=0, min:integer scalar=0,
    sec: integer scalar=none, time=true, timeonly=false, german=false)
  Prints a day
  The output is in ISO6801 standard format "yyyy-mm-dd hh:mm" by
  default. With <time, the output contains only the date, with
  >timeonly it contains only the time. With >german the format of
  the date is "dd.mm.yyyy".
  2014-06-07 12:00
day (Astronomical Functions),
day (Basic Utilities),
getnow (Basic Utilities)

Continued Fractions

function contfrac (x,n=10)
  Compute the continued fraction of x.
  returns [a,b,c,...] with
  x = a + 1 / (b + 1 / (c + 1/(...
function contfracval (r)
  Evaluate the continued fraction
  x = a + 1 / (b + 1 / (c + 1/(...
  with r = [a,b,c,...]
  Return an Interval {x1,x2}
function contfracbest (x,n=3)
  Return the best rational
  approximation to x
function isstring (x)
  Tests, if x is a string.
function expreval (expr:string, x)
  Evaluate the expression, using x and global variables
  Note that the variable x is given as a parameter. Other variables
  can be set using assigned variables, as in expreval("x*y",3,y=4).
  Moreover, global variables can be used. Local variables in the
  calling functions cannot be used, however.
  This works very much line expr(x), or "a*x*y"(3,4,a=5).
function map case (b:real, x, y)
  Returns x, if b!=0, else y.
  This function works like if, but evaluates both, x and y. It works
  for matrices b, x and y too.

Output Formats

Output formats, and conversions to radians etc.

In general, Euler either sets an output format for all numbers of a specific type, or uses functions to print a value in some format. E.g., one can select fracformat or use printfrac.

Note that the default format for Euler is longformat with 12 digits of precision. Zero rounding is on so that very small numbers print as 0. Dense output is on. This prints row vectors in a very dense form with brackets.

function rad ()
  rad(d[,min,sec]) converts to radians
  rad(x) transfers degree x to radians. If min and sec are present,
  these will be used to set minutes and seconds of the angle.
  x° works too instead of rad(x).
degprint (Basic Utilities)
function deg (x)
  deg(x) transfers radians x to degrees
rad (Basic Utilities),
degprint (Basic Utilities)
function degprint (x : real scalar, dms:integer=1)
  Print radians x in degrees.
  This function returns a string with a representation of the radial
  angle x in degrees. By default, the function will print minutes and
  seconds. Turn this off with <dms.
degformat (Basic Utilities)
function degformat (on:integer=1)
  Use degree format from now on.
  This calls _useformat with the degprint function.
userformat (Basic Utilities),
degprint (Basic Utilities)
function polarprint (x:complex scalar, format="%g")
  Print x in polar form, if it is complex.
  Prints the complex number in the form (r,phi°). The format for r
  can be set with format="...".
  >load format; polarprint(1+I,format="%0.12g")
polarformat (Basic Utilities),
polar (Mathematical Functions),
polar (Maxima Documentation)
function polarformat (on:integer=1)
  Sets the format to polar form.
userformat (Basic Utilities),
polarprint (Basic Utilities)
function ipmprint (x:interval scalar)
  Print an interval using plus-minus notation.
ipmformat (Basic Utilities)
function ipmformat (on:integer=1)
  Set the format to plus-minus notation
ipmprint (Basic Utilities)
function overwrite print (x:real scalar, digits:integer=2, length:integer=10,
    unit:string="", sep=none, dot=none)
  Print the real number x formatted.
  x : real value
  digits : number of digits after the decimal dot
  length : total length of output string
  unit : unit to append after the print
  sep : separator to group large numbers in groups of 3.
  dot : decimal dot
  sep and dot default to none, which means no separation and the
  usual decimal dot. If sep==1 (with >sep), then a hard space will be
  This function calls the built-in function _print() which takes the
  parameters _print(x,digits,length,sep,dot).
  >print(1234567890.23,2,20,>sep,dot=",",unit=" €")
  1 234 567 890,23 €
printf (Basic Utilities),
printf (Maxima Documentation),
hardspace (Euler Core)
function comment printstr (s, length, mode, char)
  String with x and n characters according to mode
  s : string
  length : length of output (at least length of string)
  mode : (optional)
  -1 = left justified
  0 = centered
  1 = right justified
  char : (optional)
  ascii code of fill character or string with one character
  >s=""; for i=1:5; s=s+"|"+printstr("Test",10,0); end; s+"|",
  |   Test   |   Test   |   Test   |   Test   |   Test   |
print (Maxima Documentation)
function prefix show (x$)
  Prints a variable with name
  This will print x=... if the provided argument is a variable.
  Otherwise, it will print the value only as usual.
showlarge (Basic Utilities)
function prefix showlarge (x$)
  Prints large matrices in full
  By default EMT eclipses lines and rows of large matrices. This can
  be used to see the full matrix.
largematrices (Euler Core),
show (Maxima Documentation)
function comment printf (format,x)
  Print a value with a C-format.
  The format can be decimal (%d, %x) or floating point (%g, %e, %f)
  if x is a number, or a string format (%s) if x is a string. Length
  modifiers (%20g), digits settings (%10.5f) or adjustments (%-20.5f)
  are allowed.
  The format string is checked for a valid format. It should not be
  possible to crash Euler via this function.
  x : real number
  format : a format string
  >printf("pi = %0.2f",pi)
  pi = 3.14
  >printf("left adjusted: --- %-20.10e ---",pi)
  left adjusted: --- 3.1415926536e+000    ---
  >printf("pi = %0.10g",pi)
  pi = 3.141592654
  >printf("%20s","Just a test")
  Just a test
  >printf(''String: "%20s"'',"Just a test")
  String: "         Just a test"
printf (Maxima Documentation),
Formats (Overview)
function overwrite format (n, digits=none, all=0)
  format(n,m) sets the output format to m digits and width n.
  format(n,m) : Turns zero rounding and dense output off. Then calls
  the builtin function _format, which sets the output width to n
  places, and the precision digits to m.
  format(n) : Works like goodformat with n digits of acccuracy. Dense
  output and zero rounding is on.
  all : If true, the set scalar format will be disabled.
  >format(6,2); (1:10)/3, longformat; // reset to default
  0.33  0.67  1.00  1.33  1.67  2.00  2.33  2.67  3.00  3.33
  >format(20,5); (1:4)'/3, longformat; // reset to default
  >format(3); pi, // like goodformat
goodformat (Basic Utilities),
Formats (Overview)
function comment zerorounding (f)
  Sets rounding to 0 of small numbers for output
Formats (Overview)
function comment denseoutput (f)
  Sets dense output for row vectors
Formats (Overview)
function comment scalarformat (f)
  Enables or disables the special output for scalars
setscalarformat (Basic Utilities),
Formats (Overview)
function comment setscalarformat (n)
  Sets the number of digits for the scalar format
Formats (Overview)
function comment iformat (n)
  Sets the number of digits for interval output
function overwrite goodformat (n,digits=none,all=0)
  goodformat(n,m) sets the output format to m digits and width n.
  Selects a nice format. For integers, the decimal dot will not be
  included. For very large or very small numbers, the exponential
  format will be used.
  This turn zero rounding on and dense output on. Then it calls the
  builtin function _goodformat, which sets the width to m digits and
  the precession to n digits. The width will only be used for matrix
  output with more than one row unless dense output is off.
  goodformat(n,m) : sets m digits of precission and a total length of
  n digits.
  goodformat(m) : total length is m+8, which is sufficient for
  exponential formats.
  all : if on, the scalar format is disabled.
  The difference to format is that the output of integers uses no
  decimal dot. Moreover, the dense output prints row vectors with
  all : If on this is applied to scalars too.
  >goodformat(6,2); (1:10)/3, longformat; // reset to default
  [ 0.33  0.67  1  1.3  1.7  2  2.3  2.7  3  3.3 ]
  >goodformat(6,2); (1:4)'/3, longformat; // reset to default
Formats (Overview)
function overwrite expformat 
  expformat(n,m) sets the output format to m digits and width n.
  Turns zero rounding off and dense output on. This works like
  format, but uses exponential output always. It calls the builtin
  function _expformat.
  >expformat(6,2); (1:10)/3, longformat; // reset to default
  [ 3.33e-001  6.67e-001  1.00e+000  1.33e+000  1.67e+000  2.00e+000
  2.33e+000  2.67e+000  3.00e+000  3.33e+000 ]
Formats (Overview)
function overwrite fixedformat 
  fixedformat(n,m) sets the output format to m digits and width n.
  This works like expformat, but always uses fixed point format. It
  calls the builtin function _fixedformat.
  Turns zero rounding on. Zero rounding is used to round very small
  numbers to 0.
Formats (Overview)
function longestformat 
  Sets a very long format for numbers.
  Turns zero rounding and dense output off. Uses a format, that
  allows to see the internal accuracy of IEEE double precision.
Formats (Overview)
function longformat (all=0)
  Sets a long format for numbers
  This is the default format for Euler with 12 digits precision.
  Turns zero rounding and dense output on, and uses a good format,
  which does not show a decimal dot if possible.
Formats (Overview)
function defformat 
  Sets the default format for Euler
  Turns zero rounding and dense output on, and uses a good format,
  which does not show a decimal dot if possible.
Formats (Overview)
function shortformat (all=0)
  Sets a short format for numbers.
Formats (Overview)
function shortestformat (all=0)
  Sets a very short format for numbers.
Formats (Overview)
function overwrite fracformat (n:integer=0, eps=1e-10)
  Sets the fractional format.
  Turns zero rounding on. If n>0 it turns dense output off. A
  continued fraction is used to approximate the numbers with
  fractions. The accuracy can be set with eps.
  >fracformat; (1:10)/3, longformat;
  [ 1/3  2/3  1  4/3  5/3  2  7/3  8/3  3  10/3 ]
  >fracformat(10); (1:4)'/3, longformat;
function fracprint (x:numerical, n:integer=10, eps=1e-10)
  Prints x in fractional format with n places.
  Uses a temporary format to print a value in fractional format. The
  format is then reset to the default longformat.
function overwrite frac (x:numerical, mixed:integer=0)
  Returns a string containing the fractional representation of x.
  This can be used to convert a number to a fraction contained in a
  string. If mixed, then the integer part is separated from the
  fractional part. The function uses the builtin function _frac to
  find a continued fraction, which approximates the number.
  The function can also be used for vectors and matrices to convert
  decimals into good fractions, e.g. for Maxima.
  >(1:4)/3; &%
  [0.33333333333333, 0.66666666666667, 1, 1.333333333333333]
  >frac((1:4)/3); &%
  1  2     4
  [-, -, 1, -]
  3  3     3
fracformat (Basic Utilities)
function prefix short (x)
  Print x in short format
  >short pi^2
function prefix shortest (x)
  Print x in shortest format
  >shortest pi^2
function prefix long (x)
  Print x in long format
  >long pi^2
function prefix longest (x)
  Print x in the longest format
  >longest pi^2
function cprint (x:real) := print(x,2,12,sep=char(hardspace()));
function prefix cformat (x)
  Print x in currency format
  >long pi^2
function prefix fraction (x)
  Print x in fractional format
  >longest pi^2
function printeuro (x,n=0)
  The value in Euro rounded to cent
function printdollar (x,n=0)
  The value in Dollar rounded to cent
function prefix uprint (x)
  Print in the format in userformat$.
  >userformat$="%10.2f Yen";
  >uprint 119569*119%
  142287.11 Yen
userformat (Basic Utilities)
function comment userformat (f$)
  Uses f$(x) for the output of the number x.
  f$ : A function to format a number x.
  The function should be able to detect real, complex or interval
  values. If f$=="" then the user format is reset to the default
  >function prefix money (x) := print(x,2,12,>sep,dot=",",unit="Euro");
  >money 12345.678
function texprint (x:real, d=5)
  Prints a real matrix for Latex as table.
  d : default number of places
tex (Plot Functions),
tex (Maxima Documentation)

Documentation Homepage