Mercurial > hg > octave-nkf
annotate scripts/general/tril.m @ 7671:4fbaba9abec1
implement compiled binary lookup
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 28 Mar 2008 15:53:09 -0400 |
parents | 3209a584e1ac |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, |
2 ## 2006, 2007 John W. Eaton | |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
245 | 19 |
3369 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} tril (@var{a}, @var{k}) | |
22 ## @deftypefnx {Function File} {} triu (@var{a}, @var{k}) | |
5668 | 23 ## Return a new matrix formed by extracting the lower (@code{tril}) |
3369 | 24 ## or upper (@code{triu}) triangular part of the matrix @var{a}, and |
25 ## setting all other elements to zero. The second argument is optional, | |
26 ## and specifies how many diagonals above or below the main diagonal should | |
27 ## also be set to zero. | |
3426 | 28 ## |
3369 | 29 ## The default value of @var{k} is zero, so that @code{triu} and |
30 ## @code{tril} normally include the main diagonal as part of the result | |
31 ## matrix. | |
3426 | 32 ## |
3369 | 33 ## If the value of @var{k} is negative, additional elements above (for |
34 ## @code{tril}) or below (for @code{triu}) the main diagonal are also | |
35 ## selected. | |
3426 | 36 ## |
3369 | 37 ## The absolute value of @var{k} must not be greater than the number of |
38 ## sub- or super-diagonals. | |
3426 | 39 ## |
3369 | 40 ## For example, |
3426 | 41 ## |
3369 | 42 ## @example |
43 ## @group | |
44 ## tril (ones (3), -1) | |
45 ## @result{} 0 0 0 | |
46 ## 1 0 0 | |
47 ## 1 1 0 | |
48 ## @end group | |
49 ## @end example | |
3426 | 50 ## |
3369 | 51 ## @noindent |
52 ## and | |
3426 | 53 ## |
3369 | 54 ## @example |
55 ## @group | |
56 ## tril (ones (3), 1) | |
57 ## @result{} 1 1 0 | |
58 ## 1 1 1 | |
59 ## 1 1 1 | |
60 ## @end group | |
61 ## @end example | |
5642 | 62 ## @seealso{triu, diag} |
3369 | 63 ## @end deftypefn |
4 | 64 |
2314 | 65 ## Author: jwe |
66 | |
2311 | 67 function retval = tril (x, k) |
4 | 68 |
69 if (nargin > 0) | |
7618
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7411
diff
changeset
|
70 if (isstruct (x)) |
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7411
diff
changeset
|
71 error ("tril: structure arrays not supported"); |
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7411
diff
changeset
|
72 endif |
4 | 73 [nr, nc] = size (x); |
74 endif | |
75 | |
76 if (nargin == 1) | |
77 k = 0; | |
78 elseif (nargin == 2) | |
1083 | 79 if ((k > 0 && k > nc) || (k < 0 && k < -nr)) |
1518 | 80 error ("tril: requested diagonal out of range"); |
4 | 81 endif |
82 else | |
6046 | 83 print_usage (); |
4 | 84 endif |
85 | |
7618
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7411
diff
changeset
|
86 retval = resize (resize (x, 0), nr, nc); |
2015 | 87 for j = 1 : min (nc, nr+k) |
1083 | 88 nr_limit = max (1, j-k); |
89 retval (nr_limit:nr, j) = x (nr_limit:nr, j); | |
4 | 90 endfor |
91 | |
92 endfunction | |
7411 | 93 |
94 %!test | |
95 %! a = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; | |
96 %! | |
97 %! l0 = [1, 0, 0; 4, 5, 0; 7, 8, 9; 10, 11, 12]; | |
98 %! l1 = [1, 2, 0; 4, 5, 6; 7, 8, 9; 10, 11, 12]; | |
99 %! l2 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; | |
100 %! lm1 = [0, 0, 0; 4, 0, 0; 7, 8, 0; 10, 11, 12]; | |
101 %! lm2 = [0, 0, 0; 0, 0, 0; 7, 0, 0; 10, 11, 0]; | |
102 %! lm3 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 10, 0, 0]; | |
103 %! lm4 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 0, 0, 0]; | |
104 %! | |
105 %! assert((tril (a, -4) == lm4 && tril (a, -3) == lm3 | |
106 %! && tril (a, -2) == lm2 && tril (a, -1) == lm1 | |
107 %! && tril (a) == l0 && tril (a, 1) == l1 && tril (a, 2) == l2)); | |
108 | |
109 %!error tril (); | |
110 | |
111 %!error tril (1, 2, 3); | |
112 |