Mercurial > hg > ltpda
comparison m-toolbox/classes/@matrix/inv.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % INV evaluates the inverse for matrix object. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: INV evaluates the inverse for matrix objects. | |
5 % | |
6 % CALL: obj = inv(mat) | |
7 % obj = mat.inv() | |
8 % | |
9 % <a href="matlab:utils.helper.displayMethodInfo('matrix', 'inv')">Parameters Description</a> | |
10 % | |
11 % VERSION: $Id: inv.m,v 1.10 2011/04/08 08:56:31 hewitson Exp $ | |
12 % | |
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
14 | |
15 function varargout = inv(varargin) | |
16 | |
17 % Check if this is a call for parameters | |
18 if utils.helper.isinfocall(varargin{:}) | |
19 varargout{1} = getInfo(varargin{3}); | |
20 return | |
21 end | |
22 | |
23 % Check if the method was called by another method | |
24 callerIsMethod = utils.helper.callerIsMethod; | |
25 | |
26 % Collect input variable names | |
27 in_names = cell(size(varargin)); | |
28 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
29 | |
30 % Collect all matrices and plists | |
31 [as, matrix_invars, rest] = utils.helper.collect_objects(varargin(:), 'matrix', in_names); | |
32 [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
33 | |
34 % Decide on a deep copy or a modify | |
35 bs = copy(as, nargout); | |
36 | |
37 % Combine input plists and default plist | |
38 usepl = parse(pl, getDefaultPlist()); | |
39 | |
40 % do dimension check | |
41 [rw,cl] = size(bs.objs); | |
42 if rw ~= cl | |
43 error('### Matrix must be square'); | |
44 end | |
45 | |
46 % get the determinant | |
47 DA = det(bs); | |
48 | |
49 % build cofactor matrix | |
50 C = copy(bs,1); | |
51 for ii = 1:rw % raw index | |
52 for jj = 1:cl % column index | |
53 % ij Minor of A | |
54 MA = copy(bs,1); | |
55 MA.objs(ii,:) = []; | |
56 MA.objs(:,jj) = []; | |
57 % cofactor | |
58 C.objs(ii,jj) = det(MA).*((-1)^(ii+jj)); | |
59 end | |
60 end | |
61 % get the transpose of cofactors matrix | |
62 CT = transpose(C); | |
63 % do inverse | |
64 bs = CT./matrix(DA); | |
65 | |
66 if ~callerIsMethod | |
67 % set name | |
68 bs.setName(sprintf('inv(%s)', in_names{1})); | |
69 | |
70 % Add history | |
71 bs.addHistory(getInfo('None'), usepl, [matrix_invars(:)], [as(:).hist]); | |
72 end | |
73 | |
74 varargout{1} = bs; | |
75 | |
76 end | |
77 | |
78 %-------------------------------------------------------------------------- | |
79 % Get Info Object | |
80 %-------------------------------------------------------------------------- | |
81 function ii = getInfo(varargin) | |
82 | |
83 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
84 sets = {}; | |
85 pl = []; | |
86 else | |
87 sets = {'Default'}; | |
88 pl = getDefaultPlist; | |
89 end | |
90 % Build info object | |
91 ii = minfo(mfilename, 'matrix', 'ltpda', utils.const.categories.op, '$Id: inv.m,v 1.10 2011/04/08 08:56:31 hewitson Exp $', sets, pl); | |
92 end | |
93 | |
94 %-------------------------------------------------------------------------- | |
95 % Get Default Plist | |
96 %-------------------------------------------------------------------------- | |
97 | |
98 function plout = getDefaultPlist() | |
99 persistent pl; | |
100 if ~exist('pl', 'var') || isempty(pl) | |
101 pl = buildplist(); | |
102 end | |
103 plout = pl; | |
104 end | |
105 | |
106 function pl = buildplist() | |
107 pl = plist.EMPTY_PLIST; | |
108 end | |
109 |