Mercurial > octave-nkf
annotate libinterp/corefcn/sqrtm.cc @ 17787:175b392e91fe
Use GNU style coding conventions for code in libinterp/
* libinterp/corefcn/Cell.h, libinterp/corefcn/__contourc__.cc,
libinterp/corefcn/__dispatch__.cc, libinterp/corefcn/__lin_interpn__.cc,
libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/__qp__.cc,
libinterp/corefcn/balance.cc, libinterp/corefcn/besselj.cc,
libinterp/corefcn/betainc.cc, libinterp/corefcn/bitfcns.cc,
libinterp/corefcn/bsxfun.cc, libinterp/corefcn/c-file-ptr-stream.cc,
libinterp/corefcn/c-file-ptr-stream.h, libinterp/corefcn/cellfun.cc,
libinterp/corefcn/colloc.cc, libinterp/corefcn/comment-list.h,
libinterp/corefcn/conv2.cc, libinterp/corefcn/daspk.cc,
libinterp/corefcn/dasrt.cc, libinterp/corefcn/dassl.cc,
libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc,
libinterp/corefcn/defaults.cc, libinterp/corefcn/defaults.in.h,
libinterp/corefcn/defun-int.h, libinterp/corefcn/defun.cc,
libinterp/corefcn/det.cc, libinterp/corefcn/dirfns.cc,
libinterp/corefcn/display.cc, libinterp/corefcn/dlmread.cc,
libinterp/corefcn/dot.cc, libinterp/corefcn/dynamic-ld.cc,
libinterp/corefcn/dynamic-ld.h, libinterp/corefcn/eig.cc,
libinterp/corefcn/ellipj.cc, libinterp/corefcn/error.cc,
libinterp/corefcn/error.h, libinterp/corefcn/event-queue.h,
libinterp/corefcn/fft.cc, libinterp/corefcn/fft2.cc, libinterp/corefcn/fftn.cc,
libinterp/corefcn/file-io.cc, libinterp/corefcn/filter.cc,
libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc,
libinterp/corefcn/gcd.cc, libinterp/corefcn/getgrent.cc,
libinterp/corefcn/getpwent.cc, libinterp/corefcn/getrusage.cc,
libinterp/corefcn/givens.cc, libinterp/corefcn/gl-render.cc,
libinterp/corefcn/gl2ps-renderer.cc, libinterp/corefcn/gl2ps-renderer.h,
libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h,
libinterp/corefcn/gripes.cc, libinterp/corefcn/gripes.h,
libinterp/corefcn/help.cc, libinterp/corefcn/hess.cc,
libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc,
libinterp/corefcn/input.h, libinterp/corefcn/inv.cc,
libinterp/corefcn/jit-ir.h, libinterp/corefcn/jit-typeinfo.cc,
libinterp/corefcn/jit-typeinfo.h, libinterp/corefcn/jit-util.h,
libinterp/corefcn/kron.cc, libinterp/corefcn/load-path.cc,
libinterp/corefcn/load-path.h, libinterp/corefcn/load-save.cc,
libinterp/corefcn/load-save.h, libinterp/corefcn/lookup.cc,
libinterp/corefcn/ls-ascii-helper.cc, libinterp/corefcn/ls-hdf5.cc,
libinterp/corefcn/ls-hdf5.h, libinterp/corefcn/ls-mat-ascii.cc,
libinterp/corefcn/ls-mat-ascii.h, libinterp/corefcn/ls-mat4.cc,
libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/ls-mat5.h,
libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lsode.cc,
libinterp/corefcn/lu.cc, libinterp/corefcn/luinc.cc,
libinterp/corefcn/mappers.cc, libinterp/corefcn/matrix_type.cc,
libinterp/corefcn/max.cc, libinterp/corefcn/md5sum.cc,
libinterp/corefcn/mex.cc, libinterp/corefcn/mexproto.h,
libinterp/corefcn/mgorth.cc, libinterp/corefcn/mxarray.in.h,
libinterp/corefcn/nproc.cc, libinterp/corefcn/oct-hist.cc,
libinterp/corefcn/oct-lvalue.h, libinterp/corefcn/oct-map.cc,
libinterp/corefcn/oct-map.h, libinterp/corefcn/oct-obj.h,
libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-stdstrm.h,
libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h,
libinterp/corefcn/octave-link.cc, libinterp/corefcn/octave-link.h,
libinterp/corefcn/pager.cc, libinterp/corefcn/pinv.cc,
libinterp/corefcn/pr-output.cc, libinterp/corefcn/procstream.h,
libinterp/corefcn/profiler.cc, libinterp/corefcn/pt-jit.cc,
libinterp/corefcn/pt-jit.h, libinterp/corefcn/quad.cc,
libinterp/corefcn/quadcc.cc, libinterp/corefcn/qz.cc,
libinterp/corefcn/rand.cc, libinterp/corefcn/rcond.cc,
libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc,
libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sighandlers.h,
libinterp/corefcn/sparse-xdiv.cc, libinterp/corefcn/sparse-xdiv.h,
libinterp/corefcn/sparse-xpow.cc, libinterp/corefcn/sparse.cc,
libinterp/corefcn/spparms.cc, libinterp/corefcn/sqrtm.cc,
libinterp/corefcn/str2double.cc, libinterp/corefcn/strfind.cc,
libinterp/corefcn/strfns.cc, libinterp/corefcn/sub2ind.cc,
libinterp/corefcn/svd.cc, libinterp/corefcn/syl.cc,
libinterp/corefcn/symtab.cc, libinterp/corefcn/symtab.h,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/sysdep.cc,
libinterp/corefcn/sysdep.h, libinterp/corefcn/time.cc,
libinterp/corefcn/toplev.cc, libinterp/corefcn/toplev.h,
libinterp/corefcn/tril.cc, libinterp/corefcn/txt-eng-ft.cc,
libinterp/corefcn/txt-eng-ft.h, libinterp/corefcn/txt-eng.h,
libinterp/corefcn/typecast.cc, libinterp/corefcn/urlwrite.cc,
libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc,
libinterp/corefcn/variables.h, libinterp/corefcn/xdiv.cc,
libinterp/corefcn/xdiv.h, libinterp/corefcn/xnorm.h, libinterp/corefcn/xpow.cc,
libinterp/corefcn/xpow.h, libinterp/corefcn/zfstream.cc,
libinterp/corefcn/zfstream.h, libinterp/dldfcn/__delaunayn__.cc,
libinterp/dldfcn/__dsearchn__.cc, libinterp/dldfcn/__eigs__.cc,
libinterp/dldfcn/__fltk_uigetfile__.cc, libinterp/dldfcn/__glpk__.cc,
libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/__init_gnuplot__.cc,
libinterp/dldfcn/__magick_read__.cc, libinterp/dldfcn/__voronoi__.cc,
libinterp/dldfcn/amd.cc, libinterp/dldfcn/ccolamd.cc, libinterp/dldfcn/chol.cc,
libinterp/dldfcn/colamd.cc, libinterp/dldfcn/convhulln.cc,
libinterp/dldfcn/dmperm.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc,
libinterp/dldfcn/symbfact.cc, libinterp/dldfcn/symrcm.cc,
libinterp/dldfcn/tsearch.cc, libinterp/octave-value/ov-base-diag.cc,
libinterp/octave-value/ov-base-diag.h, libinterp/octave-value/ov-base-int.cc,
libinterp/octave-value/ov-base-int.h, libinterp/octave-value/ov-base-mat.h,
libinterp/octave-value/ov-base-scalar.cc,
libinterp/octave-value/ov-base-scalar.h,
libinterp/octave-value/ov-base-sparse.cc,
libinterp/octave-value/ov-base-sparse.h, libinterp/octave-value/ov-base.cc,
libinterp/octave-value/ov-base.h, libinterp/octave-value/ov-bool-mat.cc,
libinterp/octave-value/ov-bool-mat.h, libinterp/octave-value/ov-bool-sparse.cc,
libinterp/octave-value/ov-bool-sparse.h, libinterp/octave-value/ov-bool.cc,
libinterp/octave-value/ov-bool.h, libinterp/octave-value/ov-builtin.cc,
libinterp/octave-value/ov-builtin.h, libinterp/octave-value/ov-cell.cc,
libinterp/octave-value/ov-cell.h, libinterp/octave-value/ov-ch-mat.cc,
libinterp/octave-value/ov-ch-mat.h, libinterp/octave-value/ov-class.cc,
libinterp/octave-value/ov-class.h, libinterp/octave-value/ov-colon.h,
libinterp/octave-value/ov-complex.cc, libinterp/octave-value/ov-complex.h,
libinterp/octave-value/ov-cx-diag.cc, libinterp/octave-value/ov-cx-diag.h,
libinterp/octave-value/ov-cx-mat.cc, libinterp/octave-value/ov-cx-mat.h,
libinterp/octave-value/ov-cx-sparse.cc, libinterp/octave-value/ov-cx-sparse.h,
libinterp/octave-value/ov-dld-fcn.h, libinterp/octave-value/ov-fcn-handle.cc,
libinterp/octave-value/ov-fcn-handle.h,
libinterp/octave-value/ov-fcn-inline.cc,
libinterp/octave-value/ov-fcn-inline.h, libinterp/octave-value/ov-fcn.h,
libinterp/octave-value/ov-float.cc, libinterp/octave-value/ov-float.h,
libinterp/octave-value/ov-flt-complex.cc,
libinterp/octave-value/ov-flt-complex.h,
libinterp/octave-value/ov-flt-cx-diag.cc,
libinterp/octave-value/ov-flt-cx-diag.h,
libinterp/octave-value/ov-flt-cx-mat.cc,
libinterp/octave-value/ov-flt-cx-mat.h,
libinterp/octave-value/ov-flt-re-diag.cc,
libinterp/octave-value/ov-flt-re-diag.h,
libinterp/octave-value/ov-flt-re-mat.cc,
libinterp/octave-value/ov-flt-re-mat.h, libinterp/octave-value/ov-int16.cc,
libinterp/octave-value/ov-int32.cc, libinterp/octave-value/ov-int64.cc,
libinterp/octave-value/ov-int8.cc, libinterp/octave-value/ov-intx.h,
libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-lazy-idx.h,
libinterp/octave-value/ov-mex-fcn.cc, libinterp/octave-value/ov-mex-fcn.h,
libinterp/octave-value/ov-null-mat.cc, libinterp/octave-value/ov-null-mat.h,
libinterp/octave-value/ov-oncleanup.cc, libinterp/octave-value/ov-perm.cc,
libinterp/octave-value/ov-perm.h, libinterp/octave-value/ov-range.cc,
libinterp/octave-value/ov-range.h, libinterp/octave-value/ov-re-diag.cc,
libinterp/octave-value/ov-re-diag.h, libinterp/octave-value/ov-re-mat.cc,
libinterp/octave-value/ov-re-mat.h, libinterp/octave-value/ov-re-sparse.cc,
libinterp/octave-value/ov-re-sparse.h, libinterp/octave-value/ov-scalar.cc,
libinterp/octave-value/ov-scalar.h, libinterp/octave-value/ov-str-mat.cc,
libinterp/octave-value/ov-str-mat.h, libinterp/octave-value/ov-struct.cc,
libinterp/octave-value/ov-struct.h, libinterp/octave-value/ov-type-conv.h,
libinterp/octave-value/ov-typeinfo.cc, libinterp/octave-value/ov-typeinfo.h,
libinterp/octave-value/ov-uint16.cc, libinterp/octave-value/ov-uint32.cc,
libinterp/octave-value/ov-uint64.cc, libinterp/octave-value/ov-uint8.cc,
libinterp/octave-value/ov-usr-fcn.cc, libinterp/octave-value/ov-usr-fcn.h,
libinterp/octave-value/ov.cc, libinterp/octave-value/ov.h, libinterp/octave.cc,
libinterp/operators/op-b-bm.cc, libinterp/operators/op-b-sbm.cc,
libinterp/operators/op-bm-b.cc, libinterp/operators/op-bm-bm.cc,
libinterp/operators/op-cdm-cdm.cc, libinterp/operators/op-chm.cc,
libinterp/operators/op-class.cc, libinterp/operators/op-cm-cm.cc,
libinterp/operators/op-cm-cs.cc, libinterp/operators/op-cm-s.cc,
libinterp/operators/op-cm-scm.cc, libinterp/operators/op-cm-sm.cc,
libinterp/operators/op-cs-cm.cc, libinterp/operators/op-cs-cs.cc,
libinterp/operators/op-cs-scm.cc, libinterp/operators/op-cs-sm.cc,
libinterp/operators/op-dm-dm.cc, libinterp/operators/op-dm-scm.cc,
libinterp/operators/op-double-conv.cc, libinterp/operators/op-fcdm-fcdm.cc,
libinterp/operators/op-fcm-fcm.cc, libinterp/operators/op-fcm-fcs.cc,
libinterp/operators/op-fcm-fm.cc, libinterp/operators/op-fcm-fs.cc,
libinterp/operators/op-fcs-fcm.cc, libinterp/operators/op-fcs-fcs.cc,
libinterp/operators/op-fcs-fm.cc, libinterp/operators/op-fcs-fs.cc,
libinterp/operators/op-fdm-fdm.cc, libinterp/operators/op-float-conv.cc,
libinterp/operators/op-fm-fcm.cc, libinterp/operators/op-fm-fcs.cc,
libinterp/operators/op-fm-fm.cc, libinterp/operators/op-fm-fs.cc,
libinterp/operators/op-fs-fcm.cc, libinterp/operators/op-fs-fcs.cc,
libinterp/operators/op-fs-fm.cc, libinterp/operators/op-fs-fs.cc,
libinterp/operators/op-m-cm.cc, libinterp/operators/op-m-cs.cc,
libinterp/operators/op-m-m.cc, libinterp/operators/op-m-s.cc,
libinterp/operators/op-m-scm.cc, libinterp/operators/op-m-sm.cc,
libinterp/operators/op-pm-scm.cc, libinterp/operators/op-range.cc,
libinterp/operators/op-s-cm.cc, libinterp/operators/op-s-cs.cc,
libinterp/operators/op-s-scm.cc, libinterp/operators/op-sbm-b.cc,
libinterp/operators/op-sbm-bm.cc, libinterp/operators/op-sbm-sbm.cc,
libinterp/operators/op-scm-cm.cc, libinterp/operators/op-scm-cs.cc,
libinterp/operators/op-scm-m.cc, libinterp/operators/op-scm-s.cc,
libinterp/operators/op-scm-scm.cc, libinterp/operators/op-scm-sm.cc,
libinterp/operators/op-sm-cm.cc, libinterp/operators/op-sm-m.cc,
libinterp/operators/op-sm-s.cc, libinterp/operators/op-sm-scm.cc,
libinterp/operators/op-sm-sm.cc, libinterp/operators/op-str-m.cc,
libinterp/operators/op-str-s.cc, libinterp/operators/op-str-str.cc,
libinterp/operators/ops.h, libinterp/parse-tree/lex.h,
libinterp/parse-tree/parse.h, libinterp/parse-tree/pt-arg-list.cc,
libinterp/parse-tree/pt-arg-list.h, libinterp/parse-tree/pt-assign.cc,
libinterp/parse-tree/pt-assign.h, libinterp/parse-tree/pt-binop.cc,
libinterp/parse-tree/pt-binop.h, libinterp/parse-tree/pt-bp.h,
libinterp/parse-tree/pt-cbinop.cc, libinterp/parse-tree/pt-check.cc,
libinterp/parse-tree/pt-colon.cc, libinterp/parse-tree/pt-colon.h,
libinterp/parse-tree/pt-const.cc, libinterp/parse-tree/pt-decl.cc,
libinterp/parse-tree/pt-decl.h, libinterp/parse-tree/pt-eval.cc,
libinterp/parse-tree/pt-except.h, libinterp/parse-tree/pt-exp.h,
libinterp/parse-tree/pt-fcn-handle.cc, libinterp/parse-tree/pt-id.cc,
libinterp/parse-tree/pt-id.h, libinterp/parse-tree/pt-idx.cc,
libinterp/parse-tree/pt-idx.h, libinterp/parse-tree/pt-loop.h,
libinterp/parse-tree/pt-mat.cc, libinterp/parse-tree/pt-misc.cc,
libinterp/parse-tree/pt-misc.h, libinterp/parse-tree/pt-pr-code.cc,
libinterp/parse-tree/pt-select.h, libinterp/parse-tree/pt-stmt.h,
libinterp/parse-tree/token.h, libinterp/version.cc:
Use GNU style coding conventions for code in libinterp/
author | Rik <rik@octave.org> |
---|---|
date | Mon, 28 Oct 2013 19:51:46 -0700 |
parents | d63878346099 |
children | 6a71e5030df5 |
rev | line source |
---|---|
4331 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17336
diff
changeset
|
3 Copyright (C) 2001-2013 Ross Lippert and Paul Kienzle |
10608 | 4 Copyright (C) 2010 VZLU Prague |
4331 | 5 |
7016 | 6 This file is part of Octave. |
4331 | 7 |
7016 | 8 Octave is free software; you can redistribute it and/or modify it |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 3 of the License, or (at your | |
11 option) any later version. | |
12 | |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
4331 | 17 |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4331 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <float.h> | |
29 | |
30 #include "CmplxSCHUR.h" | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
31 #include "fCmplxSCHUR.h" |
4331 | 32 #include "lo-ieee.h" |
33 #include "lo-mappers.h" | |
10608 | 34 #include "oct-norm.h" |
4331 | 35 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
36 #include "defun.h" |
4331 | 37 #include "error.h" |
38 #include "gripes.h" | |
39 #include "utils.h" | |
10608 | 40 #include "xnorm.h" |
4331 | 41 |
10608 | 42 template <class Matrix> |
43 static void | |
44 sqrtm_utri_inplace (Matrix& T) | |
4331 | 45 { |
10608 | 46 typedef typename Matrix::element_type element_type; |
4331 | 47 |
10608 | 48 const element_type zero = element_type (); |
4331 | 49 |
10608 | 50 bool singular = false; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
51 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
52 // The following code is equivalent to this triple loop: |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
53 // |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
54 // n = rows (T); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
55 // for j = 1:n |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
56 // T(j,j) = sqrt (T(j,j)); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
57 // for i = j-1:-1:1 |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
58 // T(i,j) /= (T(i,i) + T(j,j)); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
59 // k = 1:i-1; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
60 // T(k,j) -= T(k,i) * T(i,j); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
61 // endfor |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
62 // endfor |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
63 // |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
64 // this is an in-place, cache-aligned variant of the code |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
65 // given in Higham's paper. |
4331 | 66 |
10608 | 67 const octave_idx_type n = T.rows (); |
68 element_type *Tp = T.fortran_vec (); | |
5275 | 69 for (octave_idx_type j = 0; j < n; j++) |
4331 | 70 { |
10608 | 71 element_type *colj = Tp + n*j; |
72 if (colj[j] != zero) | |
73 colj[j] = sqrt (colj[j]); | |
74 else | |
75 singular = true; | |
4331 | 76 |
10608 | 77 for (octave_idx_type i = j-1; i >= 0; i--) |
78 { | |
79 const element_type *coli = Tp + n*i; | |
80 const element_type colji = colj[i] /= (coli[i] + colj[j]); | |
81 for (octave_idx_type k = 0; k < i; k++) | |
82 colj[k] -= coli[k] * colji; | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
83 } |
4331 | 84 } |
85 | |
10608 | 86 if (singular) |
10629
b7c4954e1c00
add specific ID for sqrtm singularity warning
Jaroslav Hajek <highegg@gmail.com>
parents:
10619
diff
changeset
|
87 warning_with_id ("Octave:sqrtm:SingularMatrix", |
b7c4954e1c00
add specific ID for sqrtm singularity warning
Jaroslav Hajek <highegg@gmail.com>
parents:
10619
diff
changeset
|
88 "sqrtm: matrix is singular, may not have a square root"); |
4331 | 89 } |
90 | |
10608 | 91 template <class Matrix, class ComplexMatrix, class ComplexSCHUR> |
92 static octave_value | |
93 do_sqrtm (const octave_value& arg) | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
94 { |
10608 | 95 |
96 octave_value retval; | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
97 |
10608 | 98 MatrixType mt = arg.matrix_type (); |
99 | |
100 bool iscomplex = arg.is_complex_type (); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
101 |
10608 | 102 typedef typename Matrix::element_type real_type; |
103 | |
104 real_type cutoff = 0, one = 1; | |
105 real_type eps = std::numeric_limits<real_type>::epsilon (); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
106 |
10608 | 107 if (! iscomplex) |
108 { | |
109 Matrix x = octave_value_extract<Matrix> (arg); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
110 |
10608 | 111 if (mt.is_unknown ()) // if type is not known, compute it now. |
112 arg.matrix_type (mt = MatrixType (x)); | |
113 | |
114 switch (mt.type ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
115 { |
10608 | 116 case MatrixType::Upper: |
117 case MatrixType::Diagonal: | |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
118 if (! x.diag ().any_element_is_negative ()) |
10608 | 119 { |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
120 // Do it in real arithmetic. |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
121 sqrtm_utri_inplace (x); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
122 retval = x; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
123 retval.matrix_type (mt); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
124 } |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
125 else |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
126 iscomplex = true; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
127 break; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
128 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
129 case MatrixType::Lower: |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
130 if (! x.diag ().any_element_is_negative ()) |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
131 { |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
132 x = x.transpose (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
133 sqrtm_utri_inplace (x); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
134 retval = x.transpose (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
135 retval.matrix_type (mt); |
10608 | 136 } |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
137 else |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
138 iscomplex = true; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
139 break; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
140 |
10608 | 141 default: |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
142 iscomplex = true; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
143 break; |
10608 | 144 } |
145 | |
146 if (iscomplex) | |
147 cutoff = 10 * x.rows () * eps * xnorm (x, one); | |
148 } | |
149 | |
150 if (iscomplex) | |
151 { | |
152 ComplexMatrix x = octave_value_extract<ComplexMatrix> (arg); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
153 |
10608 | 154 if (mt.is_unknown ()) // if type is not known, compute it now. |
155 arg.matrix_type (mt = MatrixType (x)); | |
156 | |
157 switch (mt.type ()) | |
158 { | |
159 case MatrixType::Upper: | |
160 case MatrixType::Diagonal: | |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
161 sqrtm_utri_inplace (x); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
162 retval = x; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
163 retval.matrix_type (mt); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
164 break; |
10608 | 165 |
166 case MatrixType::Lower: | |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
167 x = x.transpose (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
168 sqrtm_utri_inplace (x); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
169 retval = x.transpose (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
170 retval.matrix_type (mt); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
171 break; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
172 |
10608 | 173 default: |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
174 { |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
175 ComplexMatrix u; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
176 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
177 do |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
178 { |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
179 ComplexSCHUR schur (x, std::string (), true); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
180 x = schur.schur_matrix (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
181 u = schur.unitary_matrix (); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
182 } |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
183 while (0); // schur no longer needed. |
10608 | 184 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
185 sqrtm_utri_inplace (x); |
10608 | 186 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
187 x = u * x; // original x no longer needed. |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
188 ComplexMatrix res = xgemm (x, u, blas_no_trans, blas_conj_trans); |
10608 | 189 |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
190 if (cutoff > 0 && xnorm (imag (res), one) <= cutoff) |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
191 retval = real (res); |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
192 else |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
193 retval = res; |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
194 } |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
195 break; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
196 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
197 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
198 |
10608 | 199 return retval; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
200 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
201 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
202 DEFUN (sqrtm, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
203 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
204 @deftypefn {Built-in Function} {@var{s} =} sqrtm (@var{A})\n\ |
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
205 @deftypefnx {Built-in Function} {[@var{s}, @var{error_estimate}] =} sqrtm (@var{A})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
206 Compute the matrix square root of the square matrix @var{A}.\n\ |
4331 | 207 \n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10629
diff
changeset
|
208 Ref: N.J. Higham. @cite{A New sqrtm for @sc{matlab}}. Numerical\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10629
diff
changeset
|
209 Analysis Report No. 336, Manchester @nospell{Centre} for Computational\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10629
diff
changeset
|
210 Mathematics, Manchester, England, January 1999.\n\ |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7795
diff
changeset
|
211 @seealso{expm, logm}\n\ |
5642 | 212 @end deftypefn") |
4331 | 213 { |
214 octave_value_list retval; | |
215 | |
216 int nargin = args.length (); | |
217 | |
218 if (nargin != 1) | |
219 { | |
5823 | 220 print_usage (); |
4331 | 221 return retval; |
222 } | |
223 | |
224 octave_value arg = args(0); | |
225 | |
5275 | 226 octave_idx_type n = arg.rows (); |
227 octave_idx_type nc = arg.columns (); | |
4331 | 228 |
10608 | 229 if (n != nc || arg.ndims () > 2) |
4331 | 230 { |
231 gripe_square_matrix_required ("sqrtm"); | |
232 return retval; | |
233 } | |
234 | |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
235 if (nargout > 1) |
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
236 { |
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
237 retval.resize (1, 2); |
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
238 retval(2) = -1.0; |
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
239 } |
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
12926
diff
changeset
|
240 |
10608 | 241 if (arg.is_diag_matrix ()) |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
242 // sqrtm of a diagonal matrix is just sqrt. |
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
243 retval(0) = arg.sqrt (); |
10608 | 244 else if (arg.is_single_type ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
245 retval(0) = do_sqrtm<FloatMatrix, FloatComplexMatrix, FloatComplexSCHUR> |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
246 (arg); |
10608 | 247 else if (arg.is_numeric_type ()) |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
248 retval(0) = do_sqrtm<Matrix, ComplexMatrix, ComplexSCHUR> (arg); |
4331 | 249 |
10608 | 250 if (nargout > 1 && ! error_state) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
251 { |
10608 | 252 // This corresponds to generic code |
12902
b9c66f891281
maint: style fixes for sqrtm
John W. Eaton <jwe@octave.org>
parents:
12829
diff
changeset
|
253 // |
10608 | 254 // norm (s*s - x, "fro") / norm (x, "fro"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
255 |
10608 | 256 octave_value s = retval(0); |
257 retval(1) = xfrobnorm (s*s - arg) / xfrobnorm (arg); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
258 } |
4331 | 259 |
260 return retval; | |
261 } | |
12829
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
262 |
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
263 /* |
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
264 %!assert (sqrtm (2*ones (2)), ones (2), 3*eps) |
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
265 |
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
266 ## The following two tests are from the reference in the docstring above. |
12926
838ada8779a5
Fix brain-o in sqrtm test
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12917
diff
changeset
|
267 %!test |
838ada8779a5
Fix brain-o in sqrtm test
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12917
diff
changeset
|
268 %! x = [0 1; 0 0]; |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
269 %! assert (any (isnan (sqrtm (x))(:))); |
12829
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
270 |
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
271 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
272 %! x = eye (4); x(2,2) = x(3,3) = 2^-26; x(1,4) = 1; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
273 %! z = eye (4); z(2,2) = z(3,3) = 2^-13; z(1,4) = 0.5; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
274 %! [y, err] = sqrtm (x); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
275 %! assert (y, z); |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
276 %! assert (err, 0); # Yes, this one has to hold exactly |
12829
ab529124f516
codesprint: Wrote 3 tests for sqrtm.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
277 */ |