Thank you - there are quite a few languages which support the complex type more or less well.
The reason why I was asking this question was linked to determining the derivative of functions with a higher degree of accuracy than using common difference schemes (centered, forward, higher order, ...). In the real world you often have a numerical function which has to be evaluated with REAL parameters, but you also sometimes need its derivative which is generally not available in analytical form.
Using complex variables is a very elegant way to achieve an very accurate result close to machine accuracy.
Ideally, this would only require replacing type definitions like REAL by COMPLEX and no further change (or even no modification, as in RPL).
In some languages like HP BASIC or Fortran, I can simply change the type from REAL to COMPLEX and no additional code changes are required. In some of the other languages more code changes are required. Languages like RPL, which detect which type of object is passed to a function, automatically call the proper function e.g. sin(REAL) or sin(COMPLEX).
Example
Determine numerically the derivative of the function 'sin(3*x)*log(x)' at x=0.7.
A finite difference approach like
(f(x+h)-f(x-h))/(2*h) is not very accurate on a machine with, say, 15 relevant digits.
Example in RPL (e.g. HP 49G)
(note that there is no need to change the function 'F', it accepts its parameter either REAL or COMPLEX type)
Function f(x) to be evaluated, can be called with either a real or a complex parameter. No modification required.
Code:
<< 3 * SIN SWAP LOG * >> 'F' STO
General differentiation program calls 'F' with complex parameter, step size 1E-15. Of course, 'F' could also be supplied as a parameter on the stack.
Code:
<< (0.,1E-15) + F IM 1E-15 / >> 'DIFF' STO
Execute DIFF, leaves derivative of f(0.7):
Example in Python
(less elegant than RPL)
Code:
# actually replaces all occurrences of sin and log by their COMPLEX counterpart.
# It is not possible to use the REAL sin and log functions in a simple notation.
from cmath import sin, log
# here sin and log are (not visible) replaced by the corresponding cmath functions,
# even if this function is called with REAL parameter x it returns a COMPLEX type.
def f(x):
return sin(3*x)*log(x)
# determining the derivative at x=0.7
# step size
h = 1E-15
z = complex(0.7,h)
diff = f(z) / h
print('f\'(%.6f) = %.15f' % (z.real, diff.imag))
# prints
# f'(0.700000) = 1.773354106237344
Martin