comparison libinterp/dldfcn/chol.cc @ 20939:b17fda023ca6

maint: Use new C++ archetype in more files. Place input validation first in files. Move declaration of retval down in function to be closer to point of usage. Eliminate else clause after if () error. Use "return ovl()" where it makes sense. * find.cc, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, graphics.cc, help.cc, hess.cc, hex2num.cc, input.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc, mappers.cc, matrix_type.cc, mgorth.cc, nproc.cc, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, profiler.cc, psi.cc, quad.cc, rcond.cc, regexp.cc, schur.cc, sighandlers.cc, sparse.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, urlwrite.cc, utils.cc, variables.cc, __delaunayn__.cc, __eigs__.cc, __glpk__.cc, __magick_read__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, chol.cc, colamd.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc, ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-java.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-re-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc, octave.cc: Use new C++ archetype in more files.
author Rik <rik@octave.org>
date Fri, 18 Dec 2015 15:37:22 -0800
parents 8da80da1ac37
children 48b2ad5ee801
comparison
equal deleted inserted replaced
20938:aac911d8847b 20939:b17fda023ca6
335 Compute the inverse of the symmetric positive definite matrix @var{A} using\n\ 335 Compute the inverse of the symmetric positive definite matrix @var{A} using\n\
336 the Cholesky@tie{}factorization.\n\ 336 the Cholesky@tie{}factorization.\n\
337 @seealso{chol, chol2inv, inv}\n\ 337 @seealso{chol, chol2inv, inv}\n\
338 @end deftypefn") 338 @end deftypefn")
339 { 339 {
340 octave_value retval;
341
342 if (args.length () != 1) 340 if (args.length () != 1)
343 print_usage (); 341 print_usage ();
344 342
343 octave_value retval;
345 octave_value arg = args(0); 344 octave_value arg = args(0);
346 345
347 octave_idx_type nr = arg.rows (); 346 octave_idx_type nr = arg.rows ();
348 octave_idx_type nc = arg.columns (); 347 octave_idx_type nc = arg.columns ();
349 348
464 diagonal elements. @code{chol2inv (@var{U})} provides\n\ 463 diagonal elements. @code{chol2inv (@var{U})} provides\n\
465 @code{inv (@var{U}'*@var{U})} but it is much faster than using @code{inv}.\n\ 464 @code{inv (@var{U}'*@var{U})} but it is much faster than using @code{inv}.\n\
466 @seealso{chol, cholinv, inv}\n\ 465 @seealso{chol, cholinv, inv}\n\
467 @end deftypefn") 466 @end deftypefn")
468 { 467 {
469 octave_value retval;
470
471 if (args.length () != 1) 468 if (args.length () != 1)
472 print_usage (); 469 print_usage ();
470
471 octave_value retval;
473 472
474 octave_value arg = args(0); 473 octave_value arg = args(0);
475 474
476 octave_idx_type nr = arg.rows (); 475 octave_idx_type nr = arg.rows ();
477 octave_idx_type nc = arg.columns (); 476 octave_idx_type nc = arg.columns ();
610 if (down) 609 if (down)
611 err = fact.downdate (u); 610 err = fact.downdate (u);
612 else 611 else
613 fact.update (u); 612 fact.update (u);
614 613
615 retval(0) = get_chol_r (fact); 614 retval = ovl (get_chol_r (fact));
616 } 615 }
617 else 616 else
618 { 617 {
619 // complex case 618 // complex case
620 FloatComplexMatrix R = argr.float_complex_matrix_value (); 619 FloatComplexMatrix R = argr.float_complex_matrix_value ();
627 if (down) 626 if (down)
628 err = fact.downdate (u); 627 err = fact.downdate (u);
629 else 628 else
630 fact.update (u); 629 fact.update (u);
631 630
632 retval(0) = get_chol_r (fact); 631 retval = ovl (get_chol_r (fact));
633 } 632 }
634 } 633 }
635 else 634 else
636 { 635 {
637 if (argr.is_real_type () && argu.is_real_type ()) 636 if (argr.is_real_type () && argu.is_real_type ())
646 if (down) 645 if (down)
647 err = fact.downdate (u); 646 err = fact.downdate (u);
648 else 647 else
649 fact.update (u); 648 fact.update (u);
650 649
651 retval(0) = get_chol_r (fact); 650 retval = ovl (get_chol_r (fact));
652 } 651 }
653 else 652 else
654 { 653 {
655 // complex case 654 // complex case
656 ComplexMatrix R = argr.complex_matrix_value (); 655 ComplexMatrix R = argr.complex_matrix_value ();
662 if (down) 661 if (down)
663 err = fact.downdate (u); 662 err = fact.downdate (u);
664 else 663 else
665 fact.update (u); 664 fact.update (u);
666 665
667 retval(0) = get_chol_r (fact); 666 retval = ovl (get_chol_r (fact));
668 } 667 }
669 } 668 }
670 669
671 if (nargout > 1) 670 if (nargout > 1)
672 retval(1) = err; 671 retval(1) = err;
773 772
774 if (! argr.is_numeric_type () || ! argu.is_numeric_type () 773 if (! argr.is_numeric_type () || ! argu.is_numeric_type ()
775 || ! argj.is_real_scalar ()) 774 || ! argj.is_real_scalar ())
776 print_usage (); 775 print_usage ();
777 776
778 octave_value_list retval (nargout == 2 ? 2 : 1);
779
780 octave_idx_type n = argr.rows (); 777 octave_idx_type n = argr.rows ();
781 octave_idx_type j = argj.scalar_value (); 778 octave_idx_type j = argj.scalar_value ();
782 779
783 if (argr.columns () != n || argu.rows () != n+1 || argu.columns () != 1) 780 if (argr.columns () != n || argu.rows () != n+1 || argu.columns () != 1)
784 error ("cholinsert: dimension mismatch between R and U"); 781 error ("cholinsert: dimension mismatch between R and U");
785 782
786 if (j < 1 || j > n+1) 783 if (j < 1 || j > n+1)
787 error ("cholinsert: index J out of range"); 784 error ("cholinsert: index J out of range");
785
786 octave_value_list retval (nargout == 2 ? 2 : 1);
788 787
789 int err = 0; 788 int err = 0;
790 if (argr.is_single_type () || argu.is_single_type ()) 789 if (argr.is_single_type () || argu.is_single_type ())
791 { 790 {
792 if (argr.is_real_type () && argu.is_real_type ()) 791 if (argr.is_real_type () && argu.is_real_type ())
797 796
798 FloatCHOL fact; 797 FloatCHOL fact;
799 fact.set (R); 798 fact.set (R);
800 err = fact.insert_sym (u, j-1); 799 err = fact.insert_sym (u, j-1);
801 800
802 retval(0) = get_chol_r (fact); 801 retval = ovl (get_chol_r (fact));
803 } 802 }
804 else 803 else
805 { 804 {
806 // complex case 805 // complex case
807 FloatComplexMatrix R = argr.float_complex_matrix_value (); 806 FloatComplexMatrix R = argr.float_complex_matrix_value ();
810 809
811 FloatComplexCHOL fact; 810 FloatComplexCHOL fact;
812 fact.set (R); 811 fact.set (R);
813 err = fact.insert_sym (u, j-1); 812 err = fact.insert_sym (u, j-1);
814 813
815 retval(0) = get_chol_r (fact); 814 retval = ovl (get_chol_r (fact));
816 } 815 }
817 } 816 }
818 else 817 else
819 { 818 {
820 if (argr.is_real_type () && argu.is_real_type ()) 819 if (argr.is_real_type () && argu.is_real_type ())
825 824
826 CHOL fact; 825 CHOL fact;
827 fact.set (R); 826 fact.set (R);
828 err = fact.insert_sym (u, j-1); 827 err = fact.insert_sym (u, j-1);
829 828
830 retval(0) = get_chol_r (fact); 829 retval = ovl (get_chol_r (fact));
831 } 830 }
832 else 831 else
833 { 832 {
834 // complex case 833 // complex case
835 ComplexMatrix R = argr.complex_matrix_value (); 834 ComplexMatrix R = argr.complex_matrix_value ();
838 837
839 ComplexCHOL fact; 838 ComplexCHOL fact;
840 fact.set (R); 839 fact.set (R);
841 err = fact.insert_sym (u, j-1); 840 err = fact.insert_sym (u, j-1);
842 841
843 retval(0) = get_chol_r (fact); 842 retval = ovl (get_chol_r (fact));
844 } 843 }
845 } 844 }
846 845
847 if (nargout > 1) 846 if (nargout > 1)
848 retval(1) = err; 847 retval(1) = err;
1001 triangular, return the Cholesky@tie{}factorization of @w{A(p,p)}, where\n\ 1000 triangular, return the Cholesky@tie{}factorization of @w{A(p,p)}, where\n\
1002 @w{p = [1:j-1,j+1:n+1]}.\n\ 1001 @w{p = [1:j-1,j+1:n+1]}.\n\
1003 @seealso{chol, cholupdate, cholinsert, cholshift}\n\ 1002 @seealso{chol, cholupdate, cholinsert, cholshift}\n\
1004 @end deftypefn") 1003 @end deftypefn")
1005 { 1004 {
1006 octave_value_list retval;
1007
1008 if (args.length () != 2) 1005 if (args.length () != 2)
1009 print_usage (); 1006 print_usage ();
1010 1007
1011 octave_value argr = args(0); 1008 octave_value argr = args(0);
1012 octave_value argj = args(1); 1009 octave_value argj = args(1);
1015 print_usage (); 1012 print_usage ();
1016 1013
1017 octave_idx_type n = argr.rows (); 1014 octave_idx_type n = argr.rows ();
1018 octave_idx_type j = argj.scalar_value (); 1015 octave_idx_type j = argj.scalar_value ();
1019 1016
1020 if (argr.columns () == n) 1017 if (argr.columns () != n)
1021 { 1018 error ("choldelete: matrix R must be square");
1022 if (j > 0 && j <= n) 1019
1023 { 1020 if (j < 0 && j > n)
1024 if (argr.is_single_type ()) 1021 error ("choldelete: index J out of range");
1025 { 1022
1026 if (argr.is_real_type ()) 1023 octave_value_list retval;
1027 { 1024
1028 // real case 1025 if (argr.is_single_type ())
1029 FloatMatrix R = argr.float_matrix_value (); 1026 {
1030 1027 if (argr.is_real_type ())
1031 FloatCHOL fact; 1028 {
1032 fact.set (R); 1029 // real case
1033 fact.delete_sym (j-1); 1030 FloatMatrix R = argr.float_matrix_value ();
1034 1031
1035 retval(0) = get_chol_r (fact); 1032 FloatCHOL fact;
1036 } 1033 fact.set (R);
1037 else 1034 fact.delete_sym (j-1);
1038 { 1035
1039 // complex case 1036 retval = ovl (get_chol_r (fact));
1040 FloatComplexMatrix R = argr.float_complex_matrix_value (); 1037 }
1041 1038 else
1042 FloatComplexCHOL fact; 1039 {
1043 fact.set (R); 1040 // complex case
1044 fact.delete_sym (j-1); 1041 FloatComplexMatrix R = argr.float_complex_matrix_value ();
1045 1042
1046 retval(0) = get_chol_r (fact); 1043 FloatComplexCHOL fact;
1047 } 1044 fact.set (R);
1048 } 1045 fact.delete_sym (j-1);
1049 else 1046
1050 { 1047 retval = ovl (get_chol_r (fact));
1051 if (argr.is_real_type ()) 1048 }
1052 {
1053 // real case
1054 Matrix R = argr.matrix_value ();
1055
1056 CHOL fact;
1057 fact.set (R);
1058 fact.delete_sym (j-1);
1059
1060 retval(0) = get_chol_r (fact);
1061 }
1062 else
1063 {
1064 // complex case
1065 ComplexMatrix R = argr.complex_matrix_value ();
1066
1067 ComplexCHOL fact;
1068 fact.set (R);
1069 fact.delete_sym (j-1);
1070
1071 retval(0) = get_chol_r (fact);
1072 }
1073 }
1074 }
1075 else
1076 error ("choldelete: index J out of range");
1077 } 1049 }
1078 else 1050 else
1079 error ("choldelete: matrix R must be square"); 1051 {
1052 if (argr.is_real_type ())
1053 {
1054 // real case
1055 Matrix R = argr.matrix_value ();
1056
1057 CHOL fact;
1058 fact.set (R);
1059 fact.delete_sym (j-1);
1060
1061 retval = ovl (get_chol_r (fact));
1062 }
1063 else
1064 {
1065 // complex case
1066 ComplexMatrix R = argr.complex_matrix_value ();
1067
1068 ComplexCHOL fact;
1069 fact.set (R);
1070 fact.delete_sym (j-1);
1071
1072 retval = ovl (get_chol_r (fact));
1073 }
1074 }
1080 1075
1081 return retval; 1076 return retval;
1082 } 1077 }
1083 1078
1084 /* 1079 /*
1131 @code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}. @*\n\ 1126 @code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}. @*\n\
1132 \n\ 1127 \n\
1133 @seealso{chol, cholupdate, cholinsert, choldelete}\n\ 1128 @seealso{chol, cholupdate, cholinsert, choldelete}\n\
1134 @end deftypefn") 1129 @end deftypefn")
1135 { 1130 {
1136 octave_value_list retval;
1137
1138 if (args.length () != 3) 1131 if (args.length () != 3)
1139 print_usage (); 1132 print_usage ();
1140 1133
1141 octave_value argr = args(0); 1134 octave_value argr = args(0);
1142 octave_value argi = args(1); 1135 octave_value argi = args(1);
1148 1141
1149 octave_idx_type n = argr.rows (); 1142 octave_idx_type n = argr.rows ();
1150 octave_idx_type i = argi.scalar_value (); 1143 octave_idx_type i = argi.scalar_value ();
1151 octave_idx_type j = argj.scalar_value (); 1144 octave_idx_type j = argj.scalar_value ();
1152 1145
1153 if (argr.columns () == n) 1146 if (argr.columns () != n)
1154 { 1147 error ("cholshift: R must be a square matrix");
1155 if (j > 0 && j <= n+1 && i > 0 && i <= n+1) 1148
1156 { 1149 if (j < 0 || j > n+1 || i < 0 || i > n+1)
1157 1150 error ("cholshift: index I or J is out of range");
1158 if (argr.is_single_type () && argi.is_single_type () 1151
1159 && argj.is_single_type ()) 1152 octave_value_list retval;
1160 { 1153
1161 if (argr.is_real_type ()) 1154 if (argr.is_single_type () && argi.is_single_type ()
1162 { 1155 && argj.is_single_type ())
1163 // real case 1156 {
1164 FloatMatrix R = argr.float_matrix_value (); 1157 if (argr.is_real_type ())
1165 1158 {
1166 FloatCHOL fact; 1159 // real case
1167 fact.set (R); 1160 FloatMatrix R = argr.float_matrix_value ();
1168 fact.shift_sym (i-1, j-1); 1161
1169 1162 FloatCHOL fact;
1170 retval(0) = get_chol_r (fact); 1163 fact.set (R);
1171 } 1164 fact.shift_sym (i-1, j-1);
1172 else 1165
1173 { 1166 retval = ovl (get_chol_r (fact));
1174 // complex case 1167 }
1175 FloatComplexMatrix R = argr.float_complex_matrix_value (); 1168 else
1176 1169 {
1177 FloatComplexCHOL fact; 1170 // complex case
1178 fact.set (R); 1171 FloatComplexMatrix R = argr.float_complex_matrix_value ();
1179 fact.shift_sym (i-1, j-1); 1172
1180 1173 FloatComplexCHOL fact;
1181 retval(0) = get_chol_r (fact); 1174 fact.set (R);
1182 } 1175 fact.shift_sym (i-1, j-1);
1183 } 1176
1184 else 1177 retval = ovl (get_chol_r (fact));
1185 { 1178 }
1186 if (argr.is_real_type ())
1187 {
1188 // real case
1189 Matrix R = argr.matrix_value ();
1190
1191 CHOL fact;
1192 fact.set (R);
1193 fact.shift_sym (i-1, j-1);
1194
1195 retval(0) = get_chol_r (fact);
1196 }
1197 else
1198 {
1199 // complex case
1200 ComplexMatrix R = argr.complex_matrix_value ();
1201
1202 ComplexCHOL fact;
1203 fact.set (R);
1204 fact.shift_sym (i-1, j-1);
1205
1206 retval(0) = get_chol_r (fact);
1207 }
1208 }
1209 }
1210 else
1211 error ("cholshift: index I or J is out of range");
1212 } 1179 }
1213 else 1180 else
1214 error ("cholshift: R must be a square matrix"); 1181 {
1182 if (argr.is_real_type ())
1183 {
1184 // real case
1185 Matrix R = argr.matrix_value ();
1186
1187 CHOL fact;
1188 fact.set (R);
1189 fact.shift_sym (i-1, j-1);
1190
1191 retval = ovl (get_chol_r (fact));
1192 }
1193 else
1194 {
1195 // complex case
1196 ComplexMatrix R = argr.complex_matrix_value ();
1197
1198 ComplexCHOL fact;
1199 fact.set (R);
1200 fact.shift_sym (i-1, j-1);
1201
1202 retval = ovl (get_chol_r (fact));
1203 }
1204 }
1215 1205
1216 return retval; 1206 return retval;
1217 } 1207 }
1218 1208
1219 /* 1209 /*