Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/dmult.m @ 8692:54227442f7ed
add missing BLAS sources
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 06 Feb 2009 07:30:55 +0100 |
parents | e07e93c04080 |
children |
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 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3191 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3191 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3191 | 18 |
3449 | 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 | 24 ## If @var{a} is a vector of length @code{rows (@var{b})}, return |
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 | 41 ## @end deftypefn |
3426 | 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 | 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 | 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 | 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 | 75 print_usage (); |
3191 | 76 endif |
7649
1eac99a280a2
extend dmult to allow scaling arbitrary dimension
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
77 |
3191 | 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 | 81 %! assert (dmult ([1,2,3], ones(3)), [1,1,1;2,2,2;3,3,3]) |
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 | 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 | 86 %! assert (dmult (ones(3), [1,2,3]), [1,2,3;1,2,3;1,2,3]) |
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 | 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 | 91 %! assert (dmult (ones(3,4,2), [1 2 3 4], 2),... |
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])) |