ccode

C code representation of symbolic expression

Syntax

ccode(s)
ccode(s,'file',fileName)

Description

ccode(s) returns a fragment of C that evaluates the symbolic expression s.

ccode(s,'file',fileName) writes an "optimized" C code fragment that evaluates the symbolic expression s to the file named fileName. "Optimized" means intermediate variables are automatically generated in order to simplify the code. MATLAB® generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32.

Examples

The statements

syms x
f = taylor(log(1+x));
ccode(f)

return

t0 = x-(x*x)*(1.0/2.0)+(x*x*x)*(1.0/3.0)-(x*x*x*x)*(1.0/4.0)+...
(x*x*x*x*x)*(1.0/5.0);

The statements

H = sym(hilb(3));
ccode(H)

return

  H[0][0] = 1.0;
  H[0][1] = 1.0/2.0;
  H[0][2] = 1.0/3.0;
  H[1][0] = 1.0/2.0;
  H[1][1] = 1.0/3.0;
  H[1][2] = 1.0/4.0;
  H[2][0] = 1.0/3.0;
  H[2][1] = 1.0/4.0;
  H[2][2] = 1.0/5.0;

The statements

syms x
z = exp(-exp(-x));
ccode(diff(z,3),'file','ccodetest')

return a file named ccodetest containing the following:

  t2 = exp(-x);
  t3 = exp(-t2);
  t0 = t3*exp(x*(-2.0))*(-3.0)+t3*exp(x*(-3.0))+t2*t3;
Was this topic helpful?