comparison doc/faq/OctaveFAQ.texi @ 14856:c3fd61c59e9c

maint: Use Octave coding conventions for cuddling parentheses in doc directory * OctaveFAQ.texi, basics.txi, container.txi, contrib.txi, diagperm.txi, diffeq.txi, dynamic.txi, errors.txi, eval.txi, expr.txi, func.txi, geometry.txi, interp.txi, intro.txi, numbers.txi, oop.txi, plot.txi, poly.txi, quad.txi, set.txi, sparse.txi, stmt.txi, testfun.txi, vectorize.txi, refcard.tex: Use Octave coding conventions for cuddling parentheses.
author Rik <octave@nomad.inbox5.com>
date Mon, 09 Jul 2012 17:00:46 -0700
parents 72c96de7a403
children 648dabbb4c6b
comparison
equal deleted inserted replaced
14855:1b48b209a8d6 14856:c3fd61c59e9c
452 Indexing other things than variables is possible, as in: 452 Indexing other things than variables is possible, as in:
453 @example 453 @example
454 @group 454 @group
455 octave:1> [3 1 4 1 5 9](3) 455 octave:1> [3 1 4 1 5 9](3)
456 ans = 4 456 ans = 4
457 octave:2> cos([0 pi pi/4 7])(3) 457 octave:2> cos ([0 pi pi/4 7])(3)
458 ans = 0.70711 458 ans = 0.70711
459 @end group 459 @end group
460 @end example 460 @end example
461 461
462 @node Exclamation mark as not operator 462 @node Exclamation mark as not operator
869 Octave has limited support for nested functions. That is 869 Octave has limited support for nested functions. That is
870 870
871 @example 871 @example
872 @group 872 @group
873 function y = foo (x) 873 function y = foo (x)
874 y = bar(x) 874 y = bar (x)
875 function y = bar (x) 875 function y = bar (x)
876 y = @dots{}; 876 y = @dots{};
877 end 877 end
878 end 878 end
879 @end group 879 @end group
882 is equivalent to 882 is equivalent to
883 883
884 @example 884 @example
885 @group 885 @group
886 function y = foo (x) 886 function y = foo (x)
887 y = bar(x) 887 y = bar (x)
888 end 888 end
889 function y = bar (x) 889 function y = bar (x)
890 y = @dots{}; 890 y = @dots{};
891 end 891 end
892 @end group 892 @end group
1063 behaviour, you can enable it since Octave 3.4.0 with the following 1063 behaviour, you can enable it since Octave 3.4.0 with the following
1064 command: 1064 command:
1065 1065
1066 @example 1066 @example
1067 @group 1067 @group
1068 do_braindead_shortcircuit_evaluation(1) 1068 do_braindead_shortcircuit_evaluation (1)
1069 @end group 1069 @end group
1070 @end example 1070 @end example
1071 1071
1072 Note that the difference with @sc{Matlab} is also significant when 1072 Note that the difference with @sc{Matlab} is also significant when
1073 either argument is a function with side effects or if the first argument 1073 either argument is a function with side effects or if the first argument
1100 but Octave does not (it just applies the rule that for an operand to be 1100 but Octave does not (it just applies the rule that for an operand to be
1101 considered true, every element of the object must be nonzero or 1101 considered true, every element of the object must be nonzero or
1102 logically true). 1102 logically true).
1103 1103
1104 Finally, note the inconsistence of thinking of the condition of an if 1104 Finally, note the inconsistence of thinking of the condition of an if
1105 statement as being equivalent to @code{all(X(:))} when @var{X} is a 1105 statement as being equivalent to @code{all (X(:))} when @var{X} is a
1106 matrix. This is true for all cases EXCEPT empty matrices: 1106 matrix. This is true for all cases EXCEPT empty matrices:
1107 1107
1108 @example 1108 @example
1109 @group 1109 @group
1110 if ([0, 1]) == if (all ([0, 1])) ==> i.e., condition is false. 1110 if ([0, 1]) == if (all ([0, 1])) ==> i.e., condition is false.
1145 is equivalent to 1145 is equivalent to
1146 1146
1147 @example 1147 @example
1148 @group 1148 @group
1149 function x = mldivide (A, b) 1149 function x = mldivide (A, b)
1150 [Q, R, E] = qr(A); 1150 [Q, R, E] = qr (A);
1151 x = [A \ b, E(:, 1:m) * (R(:, 1:m) \ (Q' * b))] 1151 x = [A \ b, E(:, 1:m) * (R(:, 1:m) \ (Q' * b))]
1152 end 1152 end
1153 @end group 1153 @end group
1154 @end example 1154 @end example
1155 1155
1159 other ways. 1159 other ways.
1160 1160
1161 A numerical question arises: how big can the null space component 1161 A numerical question arises: how big can the null space component
1162 become, relative to the minimum-norm solution? Can it be nicely bounded, 1162 become, relative to the minimum-norm solution? Can it be nicely bounded,
1163 or can it be arbitrarily big? Consider this example: 1163 or can it be arbitrarily big? Consider this example:
1164 1164 OctaveFAQ.texi
1165 @example 1165 @example
1166 @group 1166 @group
1167 m = 10; 1167 m = 10;
1168 n = 10000; 1168 n = 10000;
1169 A = ones(m, n) + 1e-6 * randn(m,n); 1169 A = ones (m, n) + 1e-6 * randn (m,n);
1170 b = ones(m, 1) + 1e-6 * randn(m,1); 1170 b = ones (m, 1) + 1e-6 * randn (m,1);
1171 norm(A \ b) 1171 norm (A \ b)
1172 @end group 1172 @end group
1173 @end example 1173 @end example
1174 1174
1175 @noindent 1175 @noindent
1176 while Octave's minimum-norm values are around 3e-2, @sc{Matlab}'s 1176 while Octave's minimum-norm values are around 3e-2, @sc{Matlab}'s
1178 1178
1179 @example 1179 @example
1180 @group 1180 @group
1181 m = 5; 1181 m = 5;
1182 n = 100; 1182 n = 100;
1183 j = floor(m * rand(1, n)) + 1; 1183 j = floor (m * rand (1, n)) + 1;
1184 b = ones(m, 1); 1184 b = ones (m, 1);
1185 A = zeros(m, n); 1185 A = zeros (m, n);
1186 A(sub2ind(size(A),j,1:n)) = 1; 1186 A(sub2ind (size (A),j,1:n)) = 1;
1187 x = A \ b; 1187 x = A \ b;
1188 [dummy,p] = sort(rand(1,n)); 1188 [dummy,p] = sort (rand (1,n));
1189 y = A(:,p)\b; 1189 y = A(:,p) \ b;
1190 norm(x(p)-y) 1190 norm (x(p)-y)
1191 @end group 1191 @end group
1192 @end example 1192 @end example
1193 1193
1194 @noindent 1194 @noindent
1195 It shows that unlike in Octave, mldivide in @sc{Matlab} is not invariant 1195 It shows that unlike in Octave, mldivide in @sc{Matlab} is not invariant
1280 prompt, and the variable "a" is not reset to the saved value. In the 1280 prompt, and the variable "a" is not reset to the saved value. In the
1281 second case the variable "a" is reset correctly. Therefore @sc{Matlab} 1281 second case the variable "a" is reset correctly. Therefore @sc{Matlab}
1282 gives no safe way of temporarily changing global variables. 1282 gives no safe way of temporarily changing global variables.
1283 1283
1284 @item 1284 @item
1285 Indexing can be applied to all objects in Octave and not just 1285 Indexing can be applied to all objects in Octave and not just a
1286 variable. Therefore @code{sin(x)(1:10);} for example is perfectly valid 1286 variable. Therefore @code{sin(x)(1:10);} for example is perfectly valid
1287 in Octave but not @sc{Matlab}. To do the same in @sc{Matlab} you must do 1287 in Octave but not @sc{Matlab}. To do the same in @sc{Matlab} you must do
1288 @code{y = sin(x); y = y([1:10]);} 1288 @code{y = sin (x); y = y([1:10]);}
1289 1289
1290 @item 1290 @item
1291 Octave has the operators "++", "--", "-=", "+=", "*=", etc. As 1291 Octave has the operators "++", "--", "-=", "+=", "*=", etc. As
1292 @sc{Matlab} doesn't, if you are sharing code these should be avoided. 1292 @sc{Matlab} doesn't, if you are sharing code these should be avoided.
1293 1293