annotate scripts/linear-algebra/dmult.m @ 8664:e07e93c04080

style fixes
author John W. Eaton <jwe@octave.org>
date Wed, 04 Feb 2009 10:56:23 -0500
parents cadc73247d65
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
1 ## Copyright (C) 2008 VZLU Prague, a.s., Czech Republic
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
14 ##
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
18
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
19 ## -*- texinfo -*-
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
20 ## @deftypefn {Function File} {@var{c} =} dmult (@var{a}, @var{b})
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
21 ## @deftypefnx {Function File} {@var{c} =} dmult (@var{a}, @var{b}, @var{ind})
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
22 ## Scale a matrix by rows or columns, or a multidimensional tensor along
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
23 ## a specified dimension.
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
24 ## If @var{a} is a vector of length @code{rows (@var{b})}, return
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
25 ## @code{diag (@var{a}) * @var{b}} (but computed much more efficiently).
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
26 ## Similarly, if @var{b} is a vector of length @code{columns(@var{a})},
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
27 ## return @code{@var{a} * diag(@var{b})}.
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
28 ##
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
29 ## If @var{b} is a multidimensional array and @var{a} a vector,
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
30 ## @var{c} will have the same shape as @var{b}, with
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
31 ## @code{@var{C}(i,:,@dots{}) = @var{a}(i)*@var{b}(i,:,@dots{})}.
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
32 ##
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
33 ## If @var{a} is a multidimensional array and @var{b} a vector,
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
34 ## @var{c} will have the same shape as @var{a}, with
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
35 ## @code{@var{C}(:,@dots{},i) = @var{a}(:,@dots{},i)*@var{b}(i)}.
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
36 ##
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
37 ## If @var{ind} is supplied, @var{a} should be an array and @var{b}
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
38 ## a vector of length @code{size (@var{a},index)}. The result is then
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
39 ## @code{@var{C}(:,@dots{},i,:,@dots{}) = @var{a}(:,@dots{},i,:,@dots{})*@var{b}(i)}
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
40 ## where i indexes the @var{ind}-th dimension.
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
41 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
42
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
43 ## Author: Jaroslav Hajek <highegg@gmail.com>
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
44 ## Description: Scale a tensor along a dimension
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
45
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
46 ### Original Author: KH <Kurt.Hornik@wu-wien.ac.at>
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
47 ### Original Description: Rescale the rows of a matrix
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
48
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
49 function m = dmult (a, b, ind)
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
50 if (nargin == 2)
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
51 sa = size (a);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
52 sb = size (b);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
53 if (isvector (a) && length (a) == sb(1))
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
54 a = a(:);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
55 m = reshape (kron (ones (prod (sb(2:end)), 1), a), sb) .* b;
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
56 elseif (isvector (b) && length (b) == sa(end))
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
57 b = b(:);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
58 m = reshape (kron (b, ones (prod (sa (1:end-1)), 1)), sa) .* a;
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
59 else
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
60 error ("dmult: dimensions mismatch");
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
61 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
62
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
63 elseif (nargin == 3 && isscalar (ind))
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
64 if (isvector (b) && ind > 0 && ind <= ndims (a)
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
65 && length (b) == size (a, ind))
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
66 b = b(:);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
67 sa = size (a);
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
68 sal = prod (sa(1:ind-1)); sat = prod (sa(ind+1:end));
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
69 s = kron (ones (sat, 1), kron (b, ones (sal, 1)));
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
70 m = reshape (s, sa) .* a;
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
71 else
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
72 error ("dmult: dimensions mismatch or index out of range");
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
73 endif
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
74 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
75 print_usage ();
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
76 endif
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
77
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
78 endfunction
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
79
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
80 %!test
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
81 %! assert (dmult ([1,2,3], ones(3)), [1,1,1;2,2,2;3,3,3])
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
82 %! assert (dmult ([1,2,3]', ones(3)), [1,1,1;2,2,2;3,3,3])
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
83 %!test
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
84 %! assert (dmult ([1,2,3], ones(3,2,2)), reshape ([1,1,1,1;2,2,2,2;3,3,3,3], [3,2,2]))
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
85 %!test
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
86 %! assert (dmult (ones(3), [1,2,3]), [1,2,3;1,2,3;1,2,3])
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
87 %! assert (dmult (ones(3), [1,2,3]'), [1,2,3;1,2,3;1,2,3])
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
88 %!test
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
89 %! assert (dmult (ones(2,2,3), [1,2,3]), reshape ([1,2,3;1,2,3;1,2,3;1,2,3], [2,2,3]))
7649
1eac99a280a2 extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
90 %!test
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
91 %! assert (dmult (ones(3,4,2), [1 2 3 4], 2),...
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7649
diff changeset
92 %! reshape ([1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4], [3,4,2]))