Changes

Jump to navigation Jump to search

FAQ

422 bytes added, 09:22, 14 June 2012
m
use Codeline template
* Other optimisations in matrix operations
* bsxfun optimised for basic arithmetic functions
* Matlab-style ignoring of output arguments using <tt>{{Codeline|~</tt>}}
* Many optimisations of the accumarray function
* Sparse matrix indexing has been rewritten for speed
The Octave distribution includes a 650+ page manual that is also distributed under the terms of the GNU GPL. It is available on the web at http://www.octave.org/docs.html and you will also find there instructions on how to order a paper version.
The complete text of the Octave manual is also available using the GNU Info system via the GNU Emacs, info, or xinfo programs, or by using the <tt>{{Codeline|doc</tt> }} command to start the GNU info browser directly from the Octave prompt.
If you have problems using this documentation, or find that some topic is not adequately explained, indexed, or cross-referenced, please report it on http://bugs.octave.org.
ans = Hello Moon
As a natural extension of this, functions can also be defined in script files (m-files whose first non-comment line isn't <tt>{{Codeline|function out = &#61; foo (...)</tt>}})
===Comments with #===
The pound character, <tt>{{Codeline|#</tt>}}, may be used to start comments, in addition to <tt>{{Codeline|%</tt>}}. See the previous example. The major advantage of this is that as <tt>{{Codeline|#</tt> }} is also a comment character for unix script files, any file that starts with a string like <tt>{{Codeline|#! /usr/bin/octave -q</tt> }} will be treated as an octave script and be executed by octave.
===Strings delimited by double quotes "===
The double quote, <tt>{{Codeline|"</tt>}}, may be used to delimit strings, in addition to the single quote <tt>{{Codeline|'</tt>}}. See the previous example. Also, double-quoted strings include backslash interpretation (like C++, C, and Perl) while single quoted are uninterpreted (like Matlab and Perl).
===Line continuation by backslash===
Lines can be continued with a backslash, <tt>{{Codeline|\</tt>}}, in addition to three points <tt>{{Codeline|...</tt>}}. See the previous example.
===Informative block closing===
ans = 0.70711
In Matlab, it is for example necessary to assign the intermediate result <tt>{{Codeline|cos([0 pi pi/4 7])</tt> }} to a variable before it can be indexed again.
===Exclamation mark as not operator===
The exclamation mark <tt>{{Codeline|!</tt> }} (aka “Bang!”) is a negation operator, just like the tilde <tt>{{Codeline|~</tt>}}:
octave:1> if ! strcmp (program_name, "octave"),
> end
ans = It works!
Note however that Matlab uses the <tt>{{Codeline|!</tt> }} operator for shell escapes, for which Octave requires using the system command.
===Increment and decrement operators===
If you like the <tt>{{Codeline|++</tt>}}, <tt>{{Codeline|+=</tt> &#61;}} etc operators, rejoice! Octave includes the C-like increment and decrement operators <tt>{{Codeline|++</tt> }} and <tt>{{Codeline|--</tt> }} in both their prefix and postfix forms, in addition to <tt>{{Codeline|+=</tt>-}}, <tt>{{Codeline|-=</tt>&#61;}}, <tt>{{Codeline|*=</tt>&#61;}}, <tt>{{Codeline|/=</tt>&#61;}}, <tt>{{Codeline|^=</tt>&#61;}}, <tt>{{Codeline|.*=</tt>&#61;}}, <tt>{{Codeline|./=</tt>&#61;}}, and <tt>{{Codeline|.^=</tt>&#61;}}.
For example, to pre-increment the variable x, you would write {{Codeline|++x}}. This would add one to x and then return the new value of x as the result of the expression. It is exactly the same as the expression {{Codeline|x = &#61; x + 1}}.
To post-increment a variable x, you would write {{Codeline|x++}}. This adds one to the variable x, but returns the value that x had prior to incrementing it. For example, if x is equal to 2, the result of the expression x++ is 2, and the new value of x is 3.
For matrix and vector arguments, the increment and decrement operators work on each element of the operand.
The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.
Matlab can be made to do something similar with their <tt>{{Codeline|OnCleanUp</tt> }} function that was introduced in 2008a. Octave also has <tt>{{Codeline|onCleanup</tt> }} since version 3.4.0.
===Built-in ODE and DAE solvers===
==How does Octave solve linear systems?==
In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator <tt>{{Codeline|A\x</tt> }} will be interpreted. Sections [http://www.gnu.org/software/octave/doc/interpreter/Techniques-Used-for-Linear-Algebra.html#Techniques-Used-for-Linear-Algebra Techniques Used for Linear Algebra] and [http://www.gnu.org/software/octave/doc/interpreter/Sparse-Linear-Algebra.html Linear Algebra on Sparse Matrices] from the manual describe this procedure.
==How can I get involved in Octave development?==
If you are running an Octave script that includes a plotting command, the script and Octave may terminate immediately. So the plot window does show up, but immediately closes when Octave finishes execution.
A common solution is to put a <tt>{{Codeline|pause</tt> }} command at the end of your script.
==How do I get sound output in Windows?==
* Debian/Ubuntu<br/>
<tt>aptitude install octave-headers</tt>
* Fedora<br/>
<tt>yum install octave-devel</tt>
=Porting programs from Matlab to Octave=
===Block comments===
Block comments denoted by <tt>{{Codeline|#{</tt> }} and <tt>{{Codeline|#&#125;}}</tt> markers (or <tt>{{Codeline|%{</tt> }} and <tt>{{Codeline|%&#125;}}</tt>) are supported by Octave with some limitations. The major limitation is that block comments are not supported within [] or {}.
===Mat-File format===
Octave is a community project and so the toolboxes that exist are donated by those interested in them through [http://octave.sourceforge.net Octave Forge]. These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.
===Short-circuit <tt>{{Codeline|&</tt> }} and <tt>{{Codeline|</tt> &#124;}} operators===
The <tt>{{Codeline|&</tt> }} and <tt>{{Codeline|</tt> &#124;}} operators in Matlab short-circuit when included in a condition (e.g. an <tt>{{Codeline|if</tt> }} or <tt>{{Codeline|while</tt> }} statement) and not otherwise. In Octave only the <tt>{{Codeline|&&</tt> }} and <tt>{{Codeline||</tt> &#124;&#124;}} short circuit. Note that this means that
if (a | b)
end
have different semantics in Matlab. This is really a Matlab bug, but there is too much code out there that relies on this behaviour to change it. Prefer the <tt>{{Codeline|&#124;&#124;}} and {{Codeline|</tt> and <tt>&&</tt> }} operators in <tt>{{Codeline|if</tt> }} statements if possible. If you need to use code written for Matlab that depends on this buggy behaviour, you can enable it since Octave 3.4.0 with the following command:
do_braindead_shortcircuit_evaluation(1)
if ([1, 1] | [1, 2, 3]) 1, end ## OK
Also Matlab requires the operands of <tt>{{Codeline|&&</tt> }} and <tt>{{Codeline||</tt> &#124;&#124;}} to be scalar values but Octave does not (it just applies the rule that for an operand to be considered true, every element of the object must be nonzero or logically true).
Finally, note the inconsistence of thinking of the condition of an <tt>{{Codeline|if</tt> }} statement as being equivalent to <tt>{{Codeline|all(X(:))</tt> }} when <tt>{{Codeline|X</tt> }} is a matrix. This is true for all cases EXCEPT empty matrices:
if ([0, 1]) == if (all ([0, 1])) ==> i.e., condition is false.
if (all ([]))
because, despite the name, the <tt>{{Codeline|all</tt> }} is really returning true if none of the elements of the matrix are zero, and since there are no elements, well, none of them are zero. This is an example of [http://en.wikipedia.org/wiki/Vacuous_truth vacuous truth]. But, somewhere along the line, someone decided that <tt>{{Codeline|if ([])</tt> }} should be false. Mathworks probably thought it just looks wrong to have <tt>{{Codeline|[]</tt> }} be true in this context even if you can use logical gymnastics to convince yourself that "all" the elements of an empty matrix are nonzero. Octave however duplicates this behavior for <tt>{{Codeline|if</tt> }} statements containing empty matrices.
===Solvers for singular, under- and over-determined matrices===
The extensions in Octave over Matlab syntax are very useful, but might cause issues when sharing with Matlab users. A list of the major extensions that should be avoided to be compatible with Matlab are:
Comments in octave can be marked with <tt>{{Codeline|#</tt>}}. This allows POSIX systems to have the first line as <tt>{{Codeline|#! octave -q</tt> }} and mark the script itself executable. Matlab doesn't have this feature due to the absence of comments starting with <tt>{{Codeline|#</tt>}}".
Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. Matlab doesn't have this and all blocks must be terminated with end.
Octave has a lisp-like <tt>{{Codeline|unwind_protect</tt> }} block that allows blocks of code that terminate in an error to ensure that the variables that are touched are restored. You can do something similar with try/catch combined with <tt>{{Codeline|rethrow (lasterror ())</tt> }} in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. Matlab 2008a also introduced <tt>{{Codeline|OnCleanUp</tt> }} that is similar to <tt>{{Codeline|unwind_protect</tt>}}, except that the object created by this function has to be explicitly cleared in order for the cleanup code to run.
Note that using try/catch combined with <tt>{{Codeline|rethrow (lasterror ())</tt> }} can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example
global a
Typing Ctrl-C in the first case returns the user directly to the prompt, and the variable ''a'' is not reset to the saved value. In the second case the variable ''a'' is reset correctly. Therefore Matlab gives no safe way of temporarily changing global variables.
Indexing can be applied to all objects in Octave and not just variables. Therefore <tt>{{Codeline|sin(x)(1:10);</tt> }} for example is perfectly valid in Octave but not Matlab. To do the same in Matlab you must do <tt>{{Codeline|y = &#61; sin(x); y = &#61; y([1:10]);</tt>}}
Octave has the operators <tt>{{Codeline|++</tt>}}, <tt>{{Codeline|</tt>}}, <tt>{{Codeline|-=</tt>&#61;}}, <tt>{{Codeline|+=</tt>&#61;}}, <tt>{{Codeline|*=</tt>&#61;}}, etc. As Matlab doesn't, if you are sharing code these should be avoided.
Character strings in Octave can be denoted with double or single quotes. There is a subtle difference between the two in that escaped characters like <tt>{{Codeline|\n</tt> }} (newline), <tt>{{Codeline|\t</tt> }} (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the <tt>{{Codeline|\</tt> }} character is used in path names, and so single quoted strings should be used in paths. Matlab doesn't have double quoted strings and so they should be avoided if the code will be transferred to a Matlab user.
==GUI==

Navigation menu