annotate libinterp/corefcn/kron.cc @ 33623:4517f929c59d bytecode-interpreter tip

maint: Merge default to bytecode-interpreter
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 May 2024 16:22:41 -0400
parents a258493e726a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31707
diff changeset
3 // Copyright (C) 2002-2024 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21580
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
28 #endif
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
29
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
30 #include "dMatrix.h"
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
31 #include "fMatrix.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
32 #include "CMatrix.h"
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
33 #include "fCMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
34
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
35 #include "dSparse.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
36 #include "CSparse.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 "dDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
39 #include "fDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
40 #include "CDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
41 #include "fCDiagMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
42
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
43 #include "PermMatrix.h"
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
44
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
45 #include "mx-inlines.cc"
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3911
diff changeset
46 #include "quit.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
47
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
48 #include "defun.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
49 #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
50 #include "ovl.h"
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
51
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30888
diff changeset
52 OCTAVE_BEGIN_NAMESPACE(octave)
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
53
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
54 template <typename R, typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
55 static MArray<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
56 kron (const MArray<R>& a, const MArray<T>& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
57 {
33395
a258493e726a Backed out changeset c714266d9f0d
Arun Giridhar <arungiridhar@gmail.com>
parents: 33389
diff changeset
58 panic_unless (a.ndims () == 2);
a258493e726a Backed out changeset c714266d9f0d
Arun Giridhar <arungiridhar@gmail.com>
parents: 33389
diff changeset
59 panic_unless (b.ndims () == 2);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
60
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
61 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
62 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
63 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
64 octave_idx_type ncb = b.cols ();
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
65
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
66 MArray<T> c (dim_vector (nra*nrb, nca*ncb));
32660
f53ac65ffba6 maint: New method rwdata() as clearer alternative to fortran_vec().
Rik <rik@octave.org>
parents: 32632
diff changeset
67 T *cv = c.rwdata ();
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
68
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
69 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
70 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
71 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
72 for (octave_idx_type jb = 0; jb < ncb; jb++)
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
73 {
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
74 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
75 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
76 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
77 cv += nrb;
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
78 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
79 }
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
80 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7455
diff changeset
81
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
82 return c;
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
83 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
84
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
85 template <typename R, typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
86 static MArray<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
87 kron (const MDiagArray2<R>& a, const MArray<T>& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
88 {
33395
a258493e726a Backed out changeset c714266d9f0d
Arun Giridhar <arungiridhar@gmail.com>
parents: 33389
diff changeset
89 panic_unless (b.ndims () == 2);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
90
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
91 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
92 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
93 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
94 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
95 octave_idx_type ncb = b.cols ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7455
diff changeset
96
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
97 MArray<T> c (dim_vector (nra*nrb, nca*ncb), T ());
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
98
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
99 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
100 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
101 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
102 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
103 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
104 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
105 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
106 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
107 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
108
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
109 return c;
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
110 }
3910
79a90a0f0eff [project @ 2002-04-25 05:36:52 by jwe]
jwe
parents:
diff changeset
111
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
112 template <typename T>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
113 static MSparse<T>
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
114 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
115 {
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
116 octave_idx_type idx = 0;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
117 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
118 A.nnz () * B.nnz ());
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
119
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
120 C.cidx (0) = 0;
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
121
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
122 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
123 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
124 octave_quit ();
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
125 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
126 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
127 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
128 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
129 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
130 const T v = A.data (Ai);
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
131
25485
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
132 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
133 {
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
134 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
135 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
136 }
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 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
139 }
38a07d930ccd Improve kron performance by 17% by fewer calls to octave_quit() (bug #54005).
Rik <rik@octave.org>
parents: 25054
diff changeset
140 }
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
141
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
142 return C;
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
143 }
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
144
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
145 static PermMatrix
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
146 kron (const PermMatrix& a, const PermMatrix& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
147 {
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
148 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
149 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
150 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
151 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
152 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
153 octave_idx_type rescol = 0;
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
154 for (octave_idx_type i = 0; i < na; i++)
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
155 {
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
156 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
157 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
158 res_perm.xelem (rescol++) = a_add + pb(j);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
159 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
160
18849
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18812
diff changeset
161 return PermMatrix (res_perm, true);
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
162 }
7455
fe332ce262b5 eliminate spkron.cc; dispatch in kron
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
163
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21127
diff changeset
164 template <typename MTA, typename MTB>
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
165 octave_value
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
166 do_kron (const octave_value& a, const octave_value& b)
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
167 {
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
168 MTA am = octave_value_extract<MTA> (a);
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
169 MTB bm = octave_value_extract<MTB> (b);
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 20940
diff changeset
170
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
171 return octave_value (kron (am, bm));
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
172 }
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
173
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
174 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
175 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
176 {
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 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
178 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
179 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
180 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
181 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
182 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
183 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
184 else
75cea615ade4 Check for sparse before diagonal class in kron (bug #38082)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15195
diff changeset
185 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
186 }
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
187 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
188 {
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 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
190 && 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
191 {
14555
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
192 // 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
193 // 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
194 // 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
195 // 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
196 // 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
197 // 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
198 // 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
199 // 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
200 // 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
201
6eabd81604b5 allow kron to work for two diag matrix arguments (bug #35647)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
202 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
203 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
204 }
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 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
206 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
207 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
208 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
209 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
210 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
211 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
212 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
213 }
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 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
215 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
216 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
217 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
218 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
219 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
220 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
221 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
222 }
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 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
225 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
226 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
227 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
228 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
229 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
230 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
231 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
232 }
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 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
234 {
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23220
diff changeset
235 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
236 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
237 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
238 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
239 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
240 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
241 }
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 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
243 }
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
10461
81067c72361f optimize kron
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
245
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
246 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
247 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
248 @deftypefn {} {@var{C} =} kron (@var{A}, @var{B})
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
249 @deftypefnx {} {@var{C} =} kron (@var{A1}, @var{A2}, @dots{})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
250 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
251
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
252 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
253
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
254 @example
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
255 c = [ a(i,j)*b ]
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
256 @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
257
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
258 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
259
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
260 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
261 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
262 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
263 @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
264 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 @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
267 @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
268
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
269 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
270 @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
271
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
272 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
273 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
274 @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
275
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
276 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
277 Since the Kronecker product is associative, this is well-defined.
31547
212cc32100f5 Add new function 'tensorprod' (patch #10288)
Kasper H. Filtenborg <kasper.filtenborg@gmail.com>
parents: 30888
diff changeset
278 @seealso{tensorprod}
21966
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)
28915
c40a367a84c0 maint: Use Octave convention of space after function name in libinterp/.
Rik <rik@octave.org>
parents: 27978
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 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
336
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30888
diff changeset
337 OCTAVE_END_NAMESPACE(octave)