annotate libinterp/corefcn/svd.cc @ 31605:e88a07dec498 stable

maint: Use macros to begin/end C++ namespaces. * oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE, OCTAVE_END_NAMESPACE) that can be used to start/end a namespace. * mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc, __dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc, auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc, display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc, help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc, ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h, mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc, pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h, __delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, op-b-b.cc, op-b-bm.cc, op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc, op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc, op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc, op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc, op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-mi.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 14:23:45 -0800
parents ed17822e7662
children aac27ad79be6
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 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30390
diff changeset
3 // Copyright (C) 1996-2022 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 ////////////////////////////////////////////////////////////////////////
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21273
diff changeset
27 # include "config.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
29
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
30 #include "svd.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
32 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
33 #include "error.h"
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20940
diff changeset
34 #include "errwarn.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
35 #include "ovl.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
36 #include "pr-output.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
37 #include "utils.h"
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
38 #include "variables.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
39
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30588
diff changeset
40 OCTAVE_BEGIN_NAMESPACE(octave)
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29418
diff changeset
41
26630
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
42 static std::string Vsvd_driver = "gesvd";
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
43
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
44 template <typename T>
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
45 static typename math::svd<T>::Type
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30001
diff changeset
46 svd_type (int nargin, int nargout, const octave_value_list& args, const T& A)
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
47 {
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
48 if (nargout == 0 || nargout == 1)
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
49 return math::svd<T>::Type::sigma_only;
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
50 else if (nargin == 1)
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
51 return math::svd<T>::Type::std;
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
52 else if (! args(1).is_real_scalar ())
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
53 return math::svd<T>::Type::economy;
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
54 else
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
55 {
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
56 if (A.rows () > A.columns ())
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
57 return math::svd<T>::Type::economy;
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
58 else
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
59 return math::svd<T>::Type::std;
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
60 }
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
61 }
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
62
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
63 template <typename T>
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
64 static typename math::svd<T>::Driver
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
65 svd_driver (void)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
66 {
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
67 if (Vsvd_driver == "gejsv")
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30001
diff changeset
68 return math::svd<T>::Driver::GEJSV;
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
69 else if (Vsvd_driver == "gesdd")
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30001
diff changeset
70 return math::svd<T>::Driver::GESDD;
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
71 else
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30001
diff changeset
72 return math::svd<T>::Driver::GESVD; // default
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
73 }
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
74
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
75 DEFUN (svd, args, nargout,
22893
5ff6716cf157 allow dispatch types to be declared for built-in functions
John W. Eaton <jwe@octave.org>
parents: 22755
diff changeset
76 classes: double single
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
77 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
78 @deftypefn {} {@var{s} =} svd (@var{A})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
79 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A})
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
80 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, "econ")
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
81 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, 0)
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
82 @cindex singular value decomposition
25106
d7ad543255c5 doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents: 25103
diff changeset
83 Compute the singular value decomposition of @var{A}.
d7ad543255c5 doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents: 25103
diff changeset
84
d7ad543255c5 doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents: 25103
diff changeset
85 The singular value decomposition is defined by the relation
d7ad543255c5 doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents: 25103
diff changeset
86
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
87 @tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
88 $$
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
89 A = U S V^{\dagger}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
90 $$
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
91 @end tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
92 @ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
93
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
94 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
95 A = U*S*V'
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
96 @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
97
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
98 @end ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
99
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
100 The function @code{svd} normally returns only the vector of singular values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
101 When called with three return values, it computes
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
102 @tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
103 $U$, $S$, and $V$.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
104 @end tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
105 @ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
106 @var{U}, @var{S}, and @var{V}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
107 @end ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
108 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
109
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
110 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
111 svd (hilb (3))
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
112 @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
113
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
114 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
115 returns
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
116
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
117 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
118 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
119 ans =
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
120
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
121 1.4083189
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
122 0.1223271
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
123 0.0026873
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
124 @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
125 @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
126
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
127 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
128 and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
129
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
130 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
131 [u, s, v] = svd (hilb (3))
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
132 @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
133
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
134 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
135 returns
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
136
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
137 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
138 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
139 u =
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
140
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
141 -0.82704 0.54745 0.12766
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
142 -0.45986 -0.52829 -0.71375
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
143 -0.32330 -0.64901 0.68867
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
144
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
145 s =
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
146
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
147 1.40832 0.00000 0.00000
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
148 0.00000 0.12233 0.00000
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
149 0.00000 0.00000 0.00269
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
150
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
151 v =
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
152
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
153 -0.82704 0.54745 0.12766
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
154 -0.45986 -0.52829 -0.71375
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
155 -0.32330 -0.64901 0.68867
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
156 @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
157 @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
158
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
159 When given a second argument that is not 0, @code{svd} returns an economy-sized
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
160 decomposition, eliminating the unnecessary rows or columns of @var{U} or
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
161 @var{V}.
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
162
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
163 If the second argument is exactly 0, then the choice of decomposition is based
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
164 on the matrix @var{A}. If @var{A} has more rows than columns then an
22709
5c04055aa767 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 22615
diff changeset
165 economy-sized decomposition is returned, otherwise a regular decomposition
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
166 is calculated.
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
167
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
168 Algorithm Notes: When calculating the full decomposition (left and right
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
169 singular matrices in addition to singular values) there is a choice of two
26733
da8ebed6d56e doc: Update docstring for svd to state that gesvd is default driver.
Rik <rik@octave.org>
parents: 26630
diff changeset
170 routines in @sc{lapack}. The default routine used by Octave is @code{gesvd}.
da8ebed6d56e doc: Update docstring for svd to state that gesvd is default driver.
Rik <rik@octave.org>
parents: 26630
diff changeset
171 The alternative is @code{gesdd} which is 5X faster, but may use more memory
29418
9245ae55b6bd Use Octave coding conventions in patch for GEJSV SVD driver (bug #55727).
Rik <rik@octave.org>
parents: 29412
diff changeset
172 and may be inaccurate for some input matrices. There is a third routine
9245ae55b6bd Use Octave coding conventions in patch for GEJSV SVD driver (bug #55727).
Rik <rik@octave.org>
parents: 29412
diff changeset
173 @code{gejsv}, suitable for better accuracy at extreme scale. See the
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
174 documentation for @code{svd_driver} for more information on choosing a driver.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
175 @seealso{svd_driver, svds, eig, lu, chol, hess, qr, qz}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
176 @end deftypefn */)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
177 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
178 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
179
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
180 if (nargin < 1 || nargin > 2 || nargout > 3)
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
181 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
182
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
183 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
184
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
185 if (arg.ndims () != 2)
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
186 error ("svd: A must be a 2-D matrix");
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
187
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20873
diff changeset
188 octave_value_list retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20873
diff changeset
189
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
190 bool isfloat = arg.is_single_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
191
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
192 if (isfloat)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
193 {
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
194 if (arg.isreal ())
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
195 {
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
196 FloatMatrix tmp = arg.float_matrix_value ();
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
197
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
198 if (tmp.any_element_is_inf_or_nan ())
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
199 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
200
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
201 math::svd<FloatMatrix> result
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
202 (tmp,
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
203 svd_type<FloatMatrix> (nargin, nargout, args, tmp),
22317
6ca3acf5fad8 move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents: 22204
diff changeset
204 svd_driver<FloatMatrix> ());
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
205
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
206 FloatDiagMatrix sigma = result.singular_values ();
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
207
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
208 if (nargout == 0 || nargout == 1)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
209 retval(0) = sigma.extract_diag ();
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
210 else if (nargout == 2)
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
211 retval = ovl (result.left_singular_matrix (),
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
212 sigma);
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
213 else
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
214 retval = ovl (result.left_singular_matrix (),
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
215 sigma,
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
216 result.right_singular_matrix ());
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
217 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23573
diff changeset
218 else if (arg.iscomplex ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
219 {
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
220 FloatComplexMatrix ctmp = arg.float_complex_matrix_value ();
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
221
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
222 if (ctmp.any_element_is_inf_or_nan ())
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
223 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
224
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
225 math::svd<FloatComplexMatrix> result
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
226 (ctmp,
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
227 svd_type<FloatComplexMatrix> (nargin, nargout, args, ctmp),
22317
6ca3acf5fad8 move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents: 22204
diff changeset
228 svd_driver<FloatComplexMatrix> ());
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
229
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
230 FloatDiagMatrix sigma = result.singular_values ();
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
231
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
232 if (nargout == 0 || nargout == 1)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
233 retval(0) = sigma.extract_diag ();
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
234 else if (nargout == 2)
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
235 retval = ovl (result.left_singular_matrix (),
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
236 sigma);
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
237 else
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
238 retval = ovl (result.left_singular_matrix (),
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
239 sigma,
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
240 result.right_singular_matrix ());
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
241 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
242 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
243 else
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
244 {
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
245 if (arg.isreal ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
246 {
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
247 Matrix tmp = arg.matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
248
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
249 if (tmp.any_element_is_inf_or_nan ())
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
250 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
251
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
252 math::svd<Matrix> result
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
253 (tmp,
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
254 svd_type<Matrix> (nargin, nargout, args, tmp),
22317
6ca3acf5fad8 move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents: 22204
diff changeset
255 svd_driver<Matrix> ());
20535
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
256
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
257 DiagMatrix sigma = result.singular_values ();
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
258
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
259 if (nargout == 0 || nargout == 1)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
260 retval(0) = sigma.extract_diag ();
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
261 else if (nargout == 2)
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
262 retval = ovl (result.left_singular_matrix (),
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
263 sigma);
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
264 else
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
265 retval = ovl (result.left_singular_matrix (),
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
266 sigma,
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
267 result.right_singular_matrix ());
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
268 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23573
diff changeset
269 else if (arg.iscomplex ())
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
270 {
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
271 ComplexMatrix ctmp = arg.complex_matrix_value ();
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
272
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
273 if (ctmp.any_element_is_inf_or_nan ())
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
274 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
275
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
276 math::svd<ComplexMatrix> result
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
277 (ctmp,
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
278 svd_type<ComplexMatrix> (nargin, nargout, args, ctmp),
22317
6ca3acf5fad8 move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents: 22204
diff changeset
279 svd_driver<ComplexMatrix> ());
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
280
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
281 DiagMatrix sigma = result.singular_values ();
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
282
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
283 if (nargout == 0 || nargout == 1)
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
284 retval(0) = sigma.extract_diag ();
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
285 else if (nargout == 2)
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
286 retval = ovl (result.left_singular_matrix (),
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
287 sigma);
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
288 else
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
289 retval = ovl (result.left_singular_matrix (),
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
290 sigma,
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
291 result.right_singular_matrix ());
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
292 }
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
293 else
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
294 err_wrong_type_arg ("svd", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
295 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
296
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
297 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
298 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
299
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
300 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
301 %!assert (svd ([1, 2; 2, 1]), [3; 1], sqrt (eps))
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
302
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
303 %!test
25699
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
304 %! a = [1, 2; 3, 4] + [5, 6; 7, 8]*i;
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
305 %! [u,s,v] = svd (a);
825e1d0dfa8e Allow svd to return two output arguments (bug #53356).
Marco Caliari <marco.caliari@univr.it>
parents: 25106
diff changeset
306 %! assert (a, u * s * v', 128 * eps);
22592
ee0df00e12d6 fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents: 22407
diff changeset
307
ee0df00e12d6 fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents: 22407
diff changeset
308 %!test
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
309 %! [u, s, v] = svd ([1, 2; 2, 1]);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
310 %! x = 1 / sqrt (2);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
311 %! assert (u, [-x, -x; -x, x], sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
312 %! assert (s, [3, 0; 0, 1], sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
313 %! assert (v, [-x, x; -x, -x], sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
314
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
315 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
316 %! a = [1, 2, 3; 4, 5, 6];
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
317 %! [u, s, v] = svd (a);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
318 %! assert (u * s * v', a, sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
319
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
320 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
321 %! a = [1, 2; 3, 4; 5, 6];
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
322 %! [u, s, v] = svd (a);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
323 %! assert (u * s * v', a, sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
324
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
325 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
326 %! a = [1, 2, 3; 4, 5, 6];
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
327 %! [u, s, v] = svd (a, 1);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
328 %! assert (u * s * v', a, sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
329
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
330 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
331 %! a = [1, 2; 3, 4; 5, 6];
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
332 %! [u, s, v] = svd (a, 1);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
333 %! assert (u * s * v', a, sqrt (eps));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
334
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
335 %!assert (svd (single ([1, 2; 2, 1])), single ([3; 1]), sqrt (eps ("single")))
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
336
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
337 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
338 %! [u, s, v] = svd (single ([1, 2; 2, 1]));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
339 %! x = single (1 / sqrt (2));
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
340 %! assert (u, [-x, -x; -x, x], sqrt (eps ("single")));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
341 %! assert (s, single ([3, 0; 0, 1]), sqrt (eps ("single")));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
342 %! assert (v, [-x, x; -x, -x], sqrt (eps ("single")));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
343
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
344 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
345 %! a = single ([1, 2, 3; 4, 5, 6]);
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
346 %! [u, s, v] = svd (a);
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
347 %! assert (u * s * v', a, sqrt (eps ("single")));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
348
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
349 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
350 %! a = single ([1, 2; 3, 4; 5, 6]);
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
351 %! [u, s, v] = svd (a);
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
352 %! assert (u * s * v', a, sqrt (eps ("single")));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
353
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
354 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
355 %! a = single ([1, 2, 3; 4, 5, 6]);
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
356 %! [u, s, v] = svd (a, 1);
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
357 %! assert (u * s * v', a, sqrt (eps ("single")));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
358
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
359 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
360 %! a = single ([1, 2; 3, 4; 5, 6]);
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
361 %! [u, s, v] = svd (a, 1);
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
362 %! assert (u * s * v', a, sqrt (eps ("single")));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
363
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
364 %!test
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
365 %! a = zeros (0, 5);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
366 %! [u, s, v] = svd (a);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
367 %! assert (size (u), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
368 %! assert (size (s), [0, 5]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
369 %! assert (size (v), [5, 5]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
370
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
371 %!test
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
372 %! a = zeros (5, 0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
373 %! [u, s, v] = svd (a, 1);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
374 %! assert (size (u), [5, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
375 %! assert (size (s), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
376 %! assert (size (v), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
377
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
378 %!test <*49309>
22615
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
379 %! [~,~,v] = svd ([1, 1, 1], 0);
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
380 %! assert (size (v), [3 3]);
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
381 %! [~,~,v] = svd ([1, 1, 1], "econ");
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
382 %! assert (size (v), [3 1]);
68e9bdb7cde3 Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents: 22592
diff changeset
383
26731
75d79c39ac92 Replace '-0' in sigma matrix from svd() with '+0' (bug #55710).
Rik <rik@octave.org>
parents: 26630
diff changeset
384 %!assert <*55710> (1 / svd (-0), Inf)
75d79c39ac92 Replace '-0' in sigma matrix from svd() with '+0' (bug #55710).
Rik <rik@octave.org>
parents: 26630
diff changeset
385
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
386 %!test
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
387 %! old_driver = svd_driver ("gejsv");
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
388 %! s0 = [1e-20; 1e-10; 1]; # only gejsv can pass
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
389 %! q = sqrt (0.5);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
390 %! a = s0 .* [q, 0, -q; -0.5, q, -0.5; 0.5, q, 0.5];
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
391 %! s1 = svd (a);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
392 %! svd_driver (old_driver);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
393 %! assert (sort (s1), s0, -10 * eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
394
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
395 %!error svd ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
396 %!error svd ([1, 2; 4, 5], 2, 3)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
397 */
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
398
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
399 DEFUN (svd_driver, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
400 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
401 @deftypefn {} {@var{val} =} svd_driver ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
402 @deftypefnx {} {@var{old_val} =} svd_driver (@var{new_val})
30588
ed17822e7662 doc: Always have a return value for functions which set internal variables (bug #61681)
Rik <rik@octave.org>
parents: 30564
diff changeset
403 @deftypefnx {} {@var{old_val} =} svd_driver (@var{new_val}, "local")
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
404 Query or set the underlying @sc{lapack} driver used by @code{svd}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
405
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
406 Currently recognized values are @qcode{"gesdd"}, @qcode{"gesvd"}, and
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
407 @qcode{"gejsv"}. The default is @qcode{"gesvd"}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
408
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
409 When called from inside a function with the @qcode{"local"} option, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
410 variable is changed locally for the function and any subroutines it calls.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
411 The original variable value is restored when exiting the function.
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
412
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
413 Algorithm Notes: The @sc{lapack} library routines @code{gesvd} and @code{gesdd}
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
414 are different only when calculating the full singular value decomposition (left
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
415 and right singular matrices as well as singular values). When calculating just
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
416 the singular values the following discussion is not relevant.
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
417
26630
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
418 The newer @code{gesdd} routine is based on a Divide-and-Conquer algorithm that
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
419 is 5X faster than the alternative @code{gesvd}, which is based on QR
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
420 factorization. However, the new algorithm can use significantly more memory.
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
421 For an @nospell{MxN} input matrix the memory usage is of order O(min(M,N) ^ 2),
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
422 whereas the alternative is of order O(max(M,N)).
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
423
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
424 The routine @code{gejsv} uses a preconditioned Jacobi SVD algorithm. Unlike
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
425 @code{gesvd} and @code{gesdd}, in @code{gejsv}, there is no bidiagonalization
29418
9245ae55b6bd Use Octave coding conventions in patch for GEJSV SVD driver (bug #55727).
Rik <rik@octave.org>
parents: 29412
diff changeset
426 step that could contaminate accuracy in some extreme cases. Also, @code{gejsv}
9245ae55b6bd Use Octave coding conventions in patch for GEJSV SVD driver (bug #55727).
Rik <rik@octave.org>
parents: 29412
diff changeset
427 is known to be optimally accurate in some sense. However, the speed is slower
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
428 (single threaded at its core) and uses more memory (O(min(M,N) ^ 2 + M + N)).
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
429
26630
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
430 Beyond speed and memory issues, there have been instances where some input
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
431 matrices were not accurately decomposed by @code{gesdd}. See currently active
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
432 bug @url{https://savannah.gnu.org/bugs/?55564}. Until these accuracy issues
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
433 are resolved in a new version of the @sc{lapack} library, the default driver
1212568010a8 Use 'gesvd' as default svd_driver (bug #55564).
Rik <rik@octave.org>
parents: 26614
diff changeset
434 in Octave has been set to @qcode{"gesvd"}.
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
435
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
436 @seealso{svd}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
437 @end deftypefn */)
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
438 {
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
439 static const char *driver_names[] = { "gesvd", "gesdd", "gejsv", nullptr };
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
440
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
441 return set_internal_variable (Vsvd_driver, args, nargout,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
442 "svd_driver", driver_names);
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
443 }
15887
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
444
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
445 /*
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
446 %!test
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
447 %! A = [1+1i, 1-1i, 0; 0, 2, 0; 1i, 1i, 1+2i];
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
448 %! old_driver = svd_driver ("gesvd");
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
449 %! [U1, S1, V1] = svd (A);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
450 %! svd_driver ("gesdd");
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
451 %! [U2, S2, V2] = svd (A);
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
452 %! svd_driver ("gejsv");
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
453 %! [U3, S3, V3] = svd (A);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
454 %! assert (svd_driver (), "gejsv");
22977
750c8b4b7164 Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents: 22893
diff changeset
455 %! svd_driver (old_driver);
29412
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
456 %! assert (U1, U2, 6*eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
457 %! assert (S1, S2, 6*eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
458 %! assert (V1, V2, 6*eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
459 %! z = U1(1,:) ./ U3(1,:);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
460 %! assert (U1, U3 .* z, 100*eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
461 %! assert (S1, S3, 6*eps);
9b6bf68ea663 Add additional svd driver GEJSV for accurate SVD (bug #55727).
Eddy Xiao <bewantbe@gmail.com>
parents: 29359
diff changeset
462 %! assert (V1, V3 .* z, 100*eps);
15887
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
463 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29418
diff changeset
464
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30588
diff changeset
465 OCTAVE_END_NAMESPACE(octave)