0001 function img= inv_solve_abs_CG( inv_model, data1);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 if isstr(inv_model) && strcmp(inv_model,'UNIT_TEST'); img = do_unit_test; return; end
0031
0032
0033 inv_model = deprecate_imdl_opt(inv_model, 'parameters');
0034 inv_model = deprecate_imdl_opt(inv_model, 'inv_solve');
0035 inv_model = deprecate_imdl_opt(inv_model, 'inv_solve_abs_core');
0036
0037
0038 if ~isfield(inv_model, 'inv_solve_abs_CG')
0039 inv_model.inv_solve_abs_CG = struct;
0040 end
0041 if ~isfield(inv_model.inv_solve_abs_CG, 'update_func')
0042 inv_model.inv_solve_abs_CG.update_func = @CG_update;
0043 end
0044 if ~isfield(inv_model.inv_solve_abs_CG, 'beta_func')
0045 inv_model.inv_solve_abs_CG.beta_func = @beta_reset_polak_ribiere;
0046 end
0047
0048
0049 if isfield(inv_model, 'inv_solve_abs_CG')
0050 inv_model.inv_solve_abs_core = inv_model.inv_solve_abs_CG;
0051 inv_model = rmfield(inv_model, 'inv_solve_abs_CG');
0052 end
0053
0054 img = inv_solve_abs_core(inv_model, data1);
0055
0056 if isfield(img, 'inv_solve_abs_core')
0057 img.inv_solve_abs_CG = img.inv_solve_abs_core;
0058 img=rmfield(img, 'inv_solve_abs_core');
0059 end
0060
0061 function dx = CG_update(J, W, hp2RtR, dv, de)
0062
0063 dx = (J'*W*J + hp2RtR)\(J'*dv + hp2RtR*de);
0064
0065
0066
0067
0068
0069
0070
0071 function beta = beta_fletcher_reeves(dx_k, dx_km1, sx_km1)
0072 beta = (dx_k' * dx_k)/(dx_km1' * dx_km1);
0073 if isinf(beta) || isnan(beta)
0074 beta = 0;
0075 end
0076
0077
0078
0079
0080
0081
0082 function beta = beta_polak_ribiere(dx_k, dx_km1, sx_km1)
0083 ddx = dx_k - dx_km1;
0084 beta = (dx_k' * ddx)/(dx_km1' * dx_km1);
0085 if isinf(beta) || isnan(beta)
0086 beta = 0;
0087 end
0088
0089
0090
0091
0092
0093
0094 function beta = beta_hestenes_stiefel(dx_k, dx_km1, sx_km1)
0095 ddx = dx_k - dx_km1;
0096 beta = -(dx_k' * ddx)/(sx_km1' * ddx);
0097 if isinf(beta) || isnan(beta)
0098 beta = 0;
0099 end
0100
0101
0102
0103
0104 function beta = beta_reset_polak_ribiere(dx_k, dx_km1, sx_km1)
0105 beta = beta_polak_ribiere(dx_k, dx_km1, sx_km1);
0106 if beta < 0
0107 beta = 0;
0108 end
0109
0110
0111
0112
0113
0114
0115 function beta = beta_dai_yuan(dx_k, dx_km1, sx_km1)
0116 beta = -dx_k'*dx_k/(sx_km1'*(dx_k-dx_km1));
0117
0118 function imdl = deprecate_imdl_opt(imdl,opt)
0119 if ~isfield(imdl, opt)
0120 return;
0121 end
0122 if ~isstruct(imdl.(opt))
0123 error(['unexpected inv_model.' opt ' where ' opt ' is not a struct... i do not know what to do']);
0124 end
0125
0126
0127 Af = fieldnames(imdl.(opt));
0128 if ~strcmp(opt, 'inv_solve') || (length(Af(:)) ~= 1) || ~strcmp(Af(:),'calc_solution_error')
0129 disp(imdl)
0130 disp(imdl.(opt))
0131 warning('EIDORS:deprecatedParameters',['INV_SOLVE inv_model.' opt '.* are deprecated in favor of inv_model.inv_solve_abs_CG.* as of 30-Apr-2014.']);
0132 end
0133
0134 if ~isfield(imdl, 'inv_solve_abs_CG')
0135 imdl.inv_solve_abs_CG = imdl.(opt);
0136 else
0137
0138
0139 for i = fieldnames(imdl.(opt))'
0140 imdl.inv_solve_abs_CG.(i{1})=imdl.(opt).(i{1});
0141 end
0142 end
0143 imdl = rmfield(imdl, opt);
0144
0145 function pass = do_unit_test()
0146 pass=1;
0147 imdl=mk_common_model('a2c');
0148 imdl.reconst_type='absolute';
0149 imdl.solve=@inv_solve_abs_CG;
0150 fimg=mk_image(imdl,1);
0151 fimg.elem_data(5:10)=1.1;
0152 vi=fwd_solve(fimg);
0153 img=inv_solve(imdl,vi);
0154 clf; subplot(121); show_fem(fimg,1); title('forward model');
0155 subplot(122); show_fem(img,1); title('reconstruction');
0156 try unit_test_cmp('fwd vs. reconst', fimg.elem_data, img.elem_data, 0.08);
0157 catch me; disp(me.message); pass=0; end
0158