TWiki . Octave . OctaveToC

The Octave to C++ Backend is a C++ code generator. It performs the following translations,

The latest sources of the Octave Optimizer can be found at

Example (factorial)

Consider the following Octave function,

function x = factorial(n)
     x = 1;
     while(n>1)
      x = x * n;
      n = n -1;
     end
endfunction

Using Octave to .oct? compilation, it results in (without the includes),

DEFUN_DLD (factorial, args, nargout, "")
{
  octave_value_list c_0;
  octave_value x;
  octave_value n;
  n = args(0);
  x = 1;
  while ( do_binary_op(octave_value::op_gt, n, 1).all().all().bool_array_value()(0) )
    {
      x = do_binary_op(octave_value::op_mul, x, n);
      n = do_binary_op(octave_value::op_sub, n, 1);
    }
  c_0(0) = x;
  return(c_0);
}

Using Octave to C++ (standalone)? compilation, the following script

for n = 1:1000
   b = factorial(n);
end
b

results in,

void a_0_c_0 ()
{
  double n;
  double b;
  int d_0;
  for ( d_0 = 0 ; (d_0 < floor((((1000 - 1) + 1) / 1))) ; d_0++ )
    {
      n = (1 + (d_0 * 1));
      b = factorial_d_0(n);
    }
  set_global_value("ans", b);
  get_global_value("ans").print_name_tag(std::cout, "ans");
  get_global_value("ans").print(std::cout);
}
double factorial_d_0 (double n)
{
  double x;
  x = 1;
  while ( (n > 1) )
    {
      x = (x * n);
      n = (n - 1);
    }
  return(x);
}
int main ()
{
  init_octave_internals();
  a_0_c_0();
}

----- Revision r1.3 - 19 Nov 2004 - 15:21 - RobVermaas
Copyright © 1999-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback