annotate libinterp/corefcn/kron.cc @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
1 /*
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
2
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 Copyright (C) 2002-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
5 See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
6 or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
7
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
8
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
9 This file is part of Octave.
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
10
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23583
diff changeset
11 Octave is free software: you can redistribute it and/or modify it
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
12 under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23583
diff changeset
13 the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
14 (at your option) any later version.
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
15
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
16 Octave is distributed in the hope that it will be useful, but
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
17 WITHOUT ANY WARRANTY; without even the implied warranty of
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22737
diff changeset
19 GNU General Public License for more details.
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
20
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
21 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: 6678
diff changeset
22 along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23583
diff changeset
23 <https://www.gnu.org/licenses/>.
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
24
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
25 */
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
26
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3910
diff changeset
27 // Author: Paul Kienzle <pkienzle@users.sf.net>
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3910
diff changeset
28
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21580
diff changeset
29 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
30 # include "config.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
31 #endif
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
32
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
33 #include "dMatrix.h"
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
34 #include "fMatrix.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
35 #include "CMatrix.h"
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
36 #include "fCMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
37
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
38 #include "dSparse.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
39 #include "CSparse.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
40
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
41 #include "dDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
42 #include "fDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
43 #include "CDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
44 #include "fCDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
45
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
46 #include "PermMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
47
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
48 #include "mx-inlines.cc"
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3911
diff changeset
49 #include "quit.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
50
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
51 #include "defun.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
52 #include "error.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
53 #include "ovl.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
54
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
55 template <typename R, typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
56 static MArray<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
57 kron (const MArray<R>& a, const MArray<T>& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
58 {
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
59 assert (a.ndims () == 2);
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
60 assert (b.ndims () == 2);
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
61
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
62 octave_idx_type nra = a.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
63 octave_idx_type nrb = b.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
64 octave_idx_type nca = a.cols ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
65 octave_idx_type ncb = b.cols ();
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
66
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
67 MArray<T> c (dim_vector (nra*nrb, nca*ncb));
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
68 T *cv = c.fortran_vec ();
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
69
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
70 for (octave_idx_type ja = 0; ja < nca; ja++)
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
71 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
72 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
73 for (octave_idx_type jb = 0; jb < ncb; jb++)
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
74 {
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
75 for (octave_idx_type ia = 0; ia < nra; ia++)
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
76 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
77 mx_inline_mul (nrb, cv, a(ia, ja), b.data () + nrb*jb);
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
78 cv += nrb;
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
79 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
80 }
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
81 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7455
diff changeset
82
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
83 return c;
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
84 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
85
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
86 template <typename R, typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
87 static MArray<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
88 kron (const MDiagArray2<R>& a, const MArray<T>& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
89 {
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
90 assert (b.ndims () == 2);
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
91
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
92 octave_idx_type nra = a.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
93 octave_idx_type nrb = b.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
94 octave_idx_type dla = a.diag_length ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
95 octave_idx_type nca = a.cols ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
96 octave_idx_type ncb = b.cols ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7455
diff changeset
97
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14556
diff changeset
98 MArray<T> c (dim_vector (nra*nrb, nca*ncb), T ());
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
99
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
100 for (octave_idx_type ja = 0; ja < dla; ja++)
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
101 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
102 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
103 for (octave_idx_type jb = 0; jb < ncb; jb++)
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
104 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
105 mx_inline_mul (nrb, &c.xelem (ja*nrb, ja*ncb + jb), a.dgelem (ja),
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
106 b.data () + nrb*jb);
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
107 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
108 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
109
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
110 return c;
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
111 }
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
112
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
113 template <typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
114 static MSparse<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
115 kron (const MSparse<T>& A, const MSparse<T>& B)
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
116 {
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
117 octave_idx_type idx = 0;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
118 MSparse<T> C (A.rows () * B.rows (), A.columns () * B.columns (),
10527
b4d2080b6df7 Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents: 10461
diff changeset
119 A.nnz () * B.nnz ());
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
120
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
121 C.cidx (0) = 0;
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
122
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
123 for (octave_idx_type Aj = 0; Aj < A.columns (); Aj++)
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
124 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
125 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
126 for (octave_idx_type Bj = 0; Bj < B.columns (); Bj++)
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
127 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
128 for (octave_idx_type Ai = A.cidx (Aj); Ai < A.cidx (Aj+1); Ai++)
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
129 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
130 octave_idx_type Ci = A.ridx (Ai) * B.rows ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
131 const T v = A.data (Ai);
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
132
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
133 for (octave_idx_type Bi = B.cidx (Bj); Bi < B.cidx (Bj+1); Bi++)
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
134 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
135 C.data (idx) = v * B.data (Bi);
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
136 C.ridx (idx++) = Ci + B.ridx (Bi);
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
137 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
138 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
139 C.cidx (Aj * B.columns () + Bj + 1) = idx;
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
140 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
141 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
142
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
143 return C;
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
144 }
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
145
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
146 static PermMatrix
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
147 kron (const PermMatrix& a, const PermMatrix& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
148 {
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
149 octave_idx_type na = a.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
150 octave_idx_type nb = b.rows ();
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
151 const Array<octave_idx_type>& pa = a.col_perm_vec ();
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
152 const Array<octave_idx_type>& pb = b.col_perm_vec ();
19341
718aaf1e84f7 kron.cc: Use column vector in undeprecated Array constructor (cset 1f4455ff2329).
Rik <rik@octave.org>
parents: 19330
diff changeset
153 Array<octave_idx_type> res_perm (dim_vector (na * nb, 1));
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
154 octave_idx_type rescol = 0;
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
155 for (octave_idx_type i = 0; i < na; i++)
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
156 {
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
157 octave_idx_type a_add = pa(i) * nb;
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
158 for (octave_idx_type j = 0; j < nb; j++)
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
159 res_perm.xelem (rescol++) = a_add + pb(j);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
160 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
161
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
162 return PermMatrix (res_perm, true);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
163 }
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
164
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
165 template <typename MTA, typename MTB>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
166 octave_value
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
167 do_kron (const octave_value& a, const octave_value& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
168 {
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
169 MTA am = octave_value_extract<MTA> (a);
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
170 MTB bm = octave_value_extract<MTB> (b);
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 20940
diff changeset
171
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
172 return octave_value (kron (am, bm));
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
173 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
174
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
175 octave_value
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
176 dispatch_kron (const octave_value& a, const octave_value& b)
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
177 {
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
178 octave_value retval;
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
179 if (a.is_perm_matrix () && b.is_perm_matrix ())
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
180 retval = do_kron<PermMatrix, PermMatrix> (a, b);
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23581
diff changeset
181 else if (a.issparse () || b.issparse ())
15942
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
182 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
183 if (a.iscomplex () || b.iscomplex ())
15942
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
184 retval = do_kron<SparseComplexMatrix, SparseComplexMatrix> (a, b);
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
185 else
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
186 retval = do_kron<SparseMatrix, SparseMatrix> (a, b);
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
187 }
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
188 else if (a.is_diag_matrix ())
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
189 {
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
190 if (b.is_diag_matrix () && a.rows () == a.columns ()
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
191 && b.rows () == b.columns ())
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
192 {
14555
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
193 // We have two diagonal matrices, the product of those will be
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
194 // another diagonal matrix. To do that efficiently, extract
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
195 // the diagonals as vectors and compute the product. That
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
196 // will be another vector, which we then use to construct a
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
197 // diagonal matrix object. Note that this will fail if our
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18100
diff changeset
198 // digaonal matrix object is modified to allow the nonzero
14555
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
199 // values to be stored off of the principal diagonal (i.e., if
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
200 // diag ([1,2], 3) is modified to return a diagonal matrix
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
201 // object instead of a full matrix object).
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
202
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
203 octave_value tmp = dispatch_kron (a.diag (), b.diag ());
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
204 retval = tmp.diag ();
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
205 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
206 else if (a.is_single_type () || b.is_single_type ())
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
207 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
208 if (a.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
209 retval = do_kron<FloatComplexDiagMatrix, FloatComplexMatrix> (a, b);
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
210 else if (b.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
211 retval = do_kron<FloatDiagMatrix, FloatComplexMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
212 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
213 retval = do_kron<FloatDiagMatrix, FloatMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
214 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
215 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
216 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
217 if (a.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
218 retval = do_kron<ComplexDiagMatrix, ComplexMatrix> (a, b);
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
219 else if (b.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
220 retval = do_kron<DiagMatrix, ComplexMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
221 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
222 retval = do_kron<DiagMatrix, Matrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
223 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
224 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
225 else if (a.is_single_type () || b.is_single_type ())
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
226 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
227 if (a.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
228 retval = do_kron<FloatComplexMatrix, FloatComplexMatrix> (a, b);
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
229 else if (b.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
230 retval = do_kron<FloatMatrix, FloatComplexMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
231 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
232 retval = do_kron<FloatMatrix, FloatMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
233 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
234 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
235 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
236 if (a.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
237 retval = do_kron<ComplexMatrix, ComplexMatrix> (a, b);
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
238 else if (b.iscomplex ())
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
239 retval = do_kron<Matrix, ComplexMatrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
240 else
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
241 retval = do_kron<Matrix, Matrix> (a, b);
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
242 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
243 return retval;
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
244 }
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
245
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
246
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
247 DEFUN (kron, args, ,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
248 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
249 @deftypefn {} {} kron (@var{A}, @var{B})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
250 @deftypefnx {} {} kron (@var{A1}, @var{A2}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
251 Form the Kronecker product of two or more matrices.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
252
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
253 This is defined block by block as
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
254
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
255 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
256 x = [ a(i,j)*b ]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
257 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
258
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
259 For example:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
260
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
261 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
262 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
263 kron (1:4, ones (3, 1))
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
264 @result{} 1 2 3 4
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
265 1 2 3 4
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
266 1 2 3 4
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
267 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
268 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
269
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
270 If there are more than two input arguments @var{A1}, @var{A2}, @dots{},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
271 @var{An} the Kronecker product is computed as
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
272
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
273 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
274 kron (kron (@var{A1}, @var{A2}), @dots{}, @var{An})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
275 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
276
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
277 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
278 Since the Kronecker product is associative, this is well-defined.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
279 @end deftypefn */)
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
280 {
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
281 int nargin = args.length ();
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
282
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
283 if (nargin < 2)
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
284 print_usage ();
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
285
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
286 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
287
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
288 octave_value a = args(0);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
289 octave_value b = args(1);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
290
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
291 retval = dispatch_kron (a, b);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
292
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
293 for (octave_idx_type i = 2; i < nargin; i++)
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
294 retval = dispatch_kron (retval, args(i));
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
295
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
296 return retval;
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
297 }
12790
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
298
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
299 /*
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
300 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
301 %! x = ones (2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
302 %! assert (kron (x, x), ones (4));
12790
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
303
19134
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
304 %!shared x, y, z, p1, p2, d1, d2
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
305 %! x = [1, 2];
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
306 %! y = [-1, -2];
12790
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
307 %! z = [1, 2, 3, 4; 1, 2, 3, 4; 1, 2, 3, 4];
19134
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
308 %! p1 = eye (3)([2, 3, 1], :); ## Permutation matrix
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
309 %! p2 = [0 1 0; 0 0 1; 1 0 0]; ## Non-permutation equivalent
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
310 %! d1 = diag ([1 2 3]); ## Diag type matrix
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
311 %! d2 = [1 0 0; 0 2 0; 0 0 3]; ## Non-diag equivalent
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
312 %!assert (kron (1:4, ones (3, 1)), z)
19134
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
313 %!assert (kron (single (1:4), ones (3, 1)), single (z))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
314 %!assert (kron (sparse (1:4), ones (3, 1)), sparse (z))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
315 %!assert (kron (complex (1:4), ones (3, 1)), z)
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
316 %!assert (kron (complex (single(1:4)), ones (3, 1)), single(z))
13748
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
317 %!assert (kron (x, y, z), kron (kron (x, y), z))
77857d6fe074 Allow more than two input arguments for the kron function, plus some cleanup.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12790
diff changeset
318 %!assert (kron (x, y, z), kron (x, kron (y, z)))
19134
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
319 %!assert (kron (p1, p1), kron (p2, p2))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
320 %!assert (kron (p1, p2), kron (p2, p1))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
321 %!assert (kron (d1, d1), kron (d2, d2))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
322 %!assert (kron (d1, d2), kron (d2, d1))
38c54d45d05f kron.cc: codesprint: add more kron tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18849
diff changeset
323
14555
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
324 %!assert (kron (diag ([1, 2]), diag ([3, 4])), diag ([3, 4, 6, 8]))
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
325
22737
7abc25e6206a maint: Clean up code base to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 22407
diff changeset
326 ## Test for two diag matrices.
7abc25e6206a maint: Clean up code base to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 22407
diff changeset
327 ## See the comments above in dispatch_kron for this case.
14555
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
328 %!test
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
329 %! expected = zeros (16, 16);
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
330 %! expected (1, 11) = 3;
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
331 %! expected (2, 12) = 4;
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
332 %! expected (5, 15) = 6;
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
333 %! expected (6, 16) = 8;
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
334 %! assert (kron (diag ([1, 2], 2), diag ([3, 4], 2)), expected);
12790
5ecdb60ddf0f codesprint: Write two tests for kron.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
335 */