Method on @sym: function_handle (f)
Method on @sym: function_handle (f1, …, fn)
Method on @sym: function_handle (…, param, value)
Method on @sym: function_handle (…, ’vars’, [xz])
Method on @sym: function_handle (…, ’file’, filename)
Method on @sym: function_handle (…, ’outputs’, [o1on])

Convert symbolic expression into a standard function.

This can make anonymous functions from symbolic expressions:

syms x y
f = x^2 + sin(y)
  ⇒ f = (sym)
       2
      x  + sin(y)
h = function_handle(f)
  ⇒ h = @(x, y) x .^ 2 + sin (y)
h(2, pi/2)
  ⇒ ans =  5

Multiple arguments correspond to multiple outputs of the function. For example, the final x in this example specifies the third output (rather than the input):

h = function_handle(x^2, 5*x, x);
[a, b, c] = h(2)
  ⇒ a = 4
  ⇒ b = 10
  ⇒ c = 2

The order and number of inputs can be specified:

syms x y z
h = function_handle(f, 'vars', [z y x])
  ⇒ h = @(z, y, x) x .^ 2 + sin (y)

For compatibility with the Symbolic Math Toolbox in Matlab, we provide a synonym: see ‘@sym/matlabFunction’

OctSymPy can also generate an .m file from a symbolic expression by passing the keyword file with a string argument for filename. A handle to the function in the file will be returned. Passing an empty filename creates an anonymous function:

h = function_handle(f, 'file', '')
  ⇒ h = @(x, y) x .^ 2 + sin (y)

FIXME: naming outputs with PARAM as outputs not implemented.

FIXME: does not “optimize” code, for example, using common subexpression elimination.

See also: @sym/ccode, @sym/fortran, @sym/latex, @sym/matlabFunction.

Package: symbolic