12.07.2015 Views

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68 <strong>GNU</strong> <strong>Octave</strong>x = rand (1000);f (x);does not actually force two 1000 by 1000 element matrices to exist unless the function fmodifies the value of its argument. Then <strong>Octave</strong> must create a copy to avoid changing thevalue outside the scope of the function f, or attempting (and probably failing!) to modifythe value of a constant or the value of a temporary result.10.2.2 RecursionWith some restrictions 1 , recursive function calls are allowed. A recursive function is onewhich calls itself, either directly or indirectly. For example, here is an inefficient 2 way tocompute the factorial of a given integer:function retval = fact (n)if (n > 0)retval = n * fact (n-1);elseretval = 1;endifendfunctionThis function is recursive because it calls itself directly. It eventually terminates becauseeach time it calls itself, it uses an argument that is one less than was used for the previouscall. Once the argument is no longer greater than zero, it does not call itself, and therecursion ends.The built-in variable max_recursion_depth specifies a limit to the recursion depth andprevents <strong>Octave</strong> from recursing infinitely.max recursion depthBuilt-in VariableLimit the number of times a function may be called recursively. If the limit is exceeded,an error message is printed and control returns to the top level.The default value is 256.10.3 Arithmetic Operatorsx + yThe following arithmetic operators are available, and work on scalars and matrices.Addition. If both operands are matrices, the number of rows and columns mustboth agree. If one operand is a scalar, its value is added to all the elements ofthe other operand.x .+ y Element by element addition. This operator is equivalent to +.1 Some of <strong>Octave</strong>’s function are implemented in terms of functions that cannot be called recursively. Forexample, the ODE solver lsode is ultimately implemented in a Fortran subroutine that cannot be calledrecursively, so lsode should not be called either directly or indirectly from within the user-suppliedfunction that lsode requires. Doing so will result in undefined behavior.2 It would be much better to use prod (1:n), or gamma (n+1) instead, after first checking to ensure thatthe value n is actually a positive integer.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!