FAQ: Difference between revisions

146 bytes added ,  16 November 2011
Line 546: Line 546:
Octave is a community project and so the toolboxes that exist are donated by those interested in them through the Octave Forge website (http://octave.sourceforge.net). These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.
Octave is a community project and so the toolboxes that exist are donated by those interested in them through the Octave Forge website (http://octave.sourceforge.net). These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.


===Short-circuit & and | operators===
===Short-circuit <tt>&</tt> and <tt>|</tt> operators===


The & and | operators in Matlab short-circuit when included in an if statement and not otherwise. In Octave only the && and || short circuit. Note that this means that
The <tt>&</tt> and <tt>|</tt> operators in Matlab short-circuit when included in an if statement and not otherwise. In Octave only the <tt>&&</tt> and <tt>||</tt> short circuit. Note that this means that


             if (a | b)
             if (a | b)
Line 561: Line 561:
             end
             end


are different 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 || and && operators in if 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:
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>||</tt> and <tt>&&</tt> operators in <tt>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)
             do_braindead_shortcircuit_evaluation(1)
Line 572: Line 572:
and
and


             if (1 | []) 1, end  ## short circuits so condition is
             if (1 | []) 1, end  ## short circuits so condition is true.


true.
Another case that is documented in the Matlab manuals is that
Another case that is documented in the Matlab manuals is that


Line 580: Line 579:
             if ([1, 1] | [1, 2, 3]) 1, end  ## OK
             if ([1, 1] | [1, 2, 3]) 1, end  ## OK


Also Matlab requires the operands of && and || 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).
Also Matlab requires the operands of <tt>&&</tt> and <tt>||</tt> 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 if statement as being equivalent to all(X(:)) when X is a matrix. This is true for all cases EXCEPT empty matrices:
Finally, note the inconsistence of thinking of the condition of an <tt>if</tt> statement as being equivalent to <tt>all(X(:))</tt> when <tt>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 ([0, 1]) == if (all ([0, 1]))  ==>  i.e., condition is false.
Line 595: Line 594:
             if (all ([]))
             if (all ([]))


because, despite the name, the <tt>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>if ([])</tt> should be false. Mathworks probably thought it just looks wrong to have <tt>[]</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 if statements containing empty matrices.
because, despite the name, the <tt>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>if ([])</tt> should be false. Mathworks probably thought it just looks wrong to have <tt>[]</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>if</tt> statements containing empty matrices.


===Solvers for singular, under- and over-determined matrices===
===Solvers for singular, under- and over-determined matrices===