0001 function t_om_solve_leqs(quiet)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 if nargin < 1
0013 quiet = 0;
0014 end
0015
0016
0017 cfg = {
0018 {'', 'default', [] [] },
0019 {'\', '\', [] [] },
0020 {'LU', 'LU', [] [] },
0021
0022 };
0023
0024 A1 = [2 1; -3 5];
0025 b1 = [8; 1];
0026 x1 = [3;2];
0027
0028 A2 = sparse([2 -1 0; -3 1 -2; 0 5 -4]);
0029 b2 = [-5; 1; -7];
0030 x2 = [-2; 1; 3];
0031
0032 n = 7;
0033
0034 t_begin(10+n*length(cfg), quiet);
0035
0036 for k = 1:length(cfg)
0037 alg = cfg{k}{1};
0038 name = cfg{k}{2};
0039
0040 t = sprintf('%s - full A matrix : ', name);
0041 om = opt_model;
0042 om.add_var('x', 2, zeros(size(x1)));
0043 om.add_lin_constraint('A', A1, b1, b1);
0044 if isempty(alg)
0045 opt = struct();
0046 else
0047 opt = struct('leq_opt', struct('solver', alg));
0048 end
0049 x = om.solve(opt);
0050 t_is(x, x1, 14, [t 'x']);
0051 t_ok(~isfield(om.soln, 'var'), [t 'no parse_soln() outputs']);
0052
0053 t = sprintf('%s - sparse A matrix : ', name);
0054 om = opt_model;
0055 om.add_var('x', 3, zeros(size(x2)));
0056 om.add_lin_constraint('A12', A2(1:2, :), b2(1:2), b2(1:2));
0057 om.add_lin_constraint('A3', A2(3, :), b2(3), b2(3));
0058 opt = struct('leq_opt', struct('solver', alg), 'parse_soln', 1);
0059 [x, f, e, out, jac] = om.solve(opt);
0060 t_is(x, x2, 14, [t 'x']);
0061 t_is(f, A2*x-b2, 14, [t 'f']);
0062 t_is(e, 1, 14, [t 'exitflag']);
0063 t_ok(strcmp(out.alg, alg), [t 'output']);
0064 t_is(jac, A2, 14, [t 'jac']);
0065 opt.parse_soln = 0;
0066 end
0067
0068 t = 'om.soln.';
0069 t_is(om.soln.x, x, 14, [t 'x']);
0070 t_is(om.soln.f, f, 14, [t 'f']);
0071 t_is(om.soln.eflag, e, 14, [t 'eflag']);
0072 t_ok(strcmp(om.soln.output.alg, out.alg), [t 'output.alg']);
0073 t_is(om.soln.jac, jac, 14, [t 'jac']);
0074
0075 t = 'om.get_soln(''var'', ''x'') : ';
0076 t_is(om.get_soln('var', 'x'), x, 14, [t 'x']);
0077
0078 t = 'om.get_soln(''lin'', ''A12'') : ';
0079 f12 = om.get_soln('lin', 'A12');
0080 t_is(f12, f(1:2), 14, [t 'A12 * x - u12']);
0081
0082 t = 'om.get_soln(''lin'', ''f'', ''A3'') : ';
0083 g = om.get_soln('lin', 'Ax_u', 'A3');
0084 t_is(g, f(3), 14, [t 'A3 * x - u3']);
0085
0086 t = 'om.get_soln(''lin'', ''l_Ax'', ''A3'') : ';
0087 g = om.get_soln('lin', 'l_Ax', 'A3');
0088 t_is(g, f(3), 14, [t 'l3 - A3 * x']);
0089
0090 t = 'parse_soln : ';
0091 t_is(om.soln.var.val.x, om.get_soln('var', 'x'), 14, [t 'var.val.x']);
0092
0093 t_end;