comparison m-toolbox/classes/@matrix/det.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 % DET evaluates the determinant for matrix object.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: DET evaluates the determinant for matrix objects.
5 % The objects must belong to the same class (e.g. ao, smodel, ...)
6 % The result is an object of the class itself
7 %
8 % CALL: obj = det(mat)
9 % obj = mat.det()
10 %
11 % <a href="matlab:utils.helper.displayMethodInfo('matrix', 'det')">Parameters Description</a>
12 %
13 % VERSION: $Id: det.m,v 1.13 2011/04/08 08:56:31 hewitson Exp $
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 function out = det(varargin)
18
19 callerIsMethod = utils.helper.callerIsMethod;
20
21 if callerIsMethod
22 as = [varargin{:}];
23 else
24 % Check if this is a call for parameters
25 if utils.helper.isinfocall(varargin{:})
26 out = getInfo(varargin{3});
27 return
28 end
29
30 % Collect input variable names
31 in_names = cell(size(varargin));
32 for ii = 1:nargin,in_names{ii} = inputname(ii);end
33
34 % Collect all matrices and plists
35 [as, matrix_invars, rest] = utils.helper.collect_objects(varargin(:), 'matrix', in_names);
36 [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
37
38 % Combine input plists and default plist
39 usepl = parse(pl, getDefaultPlist());
40 end
41
42 % extract objects
43 objmat = as.objs;
44
45 % we check now the dimensionality to be sure to have a square matrix
46 [rw, cl] = size(objmat);
47 if rw ~= cl
48 error('### Matrix must be square');
49 end
50
51 % Let's go with textbook definition
52 switch cl
53 case 0
54 % To be compliant with definition.
55 % Here we choose to output an ao, so we can have history;
56 % if used with other objetcs (ao, smodel, ...) it will be promoted anyways
57 out = ao(1);
58 case 1
59 out = objmat;
60 case 2
61 out = objmat(1,1) .* objmat(2,2) - objmat(2,1) .* objmat(1,2);
62 otherwise
63 dmod_minor = eval(sprintf('%s.initObjectWithSize(1,cl);', class(objmat)));
64
65 % Cache these objects not to produce them iteratively
66 coeff_minus = ao(-1);
67 coeff_plus = ao(1);
68 for jj = 1:cl
69 Amod = objmat;
70 Amod(1,:) = [];
71 Amod(:,jj) = [];
72 Am = matrix(Amod);
73 if (-1).^(jj+1) > 0
74 coeff = coeff_plus;
75 else
76 coeff = coeff_minus;
77 end
78 dmod_minor(jj) = objmat(1,jj) .* det(Am) * coeff;
79 end
80 % sum over elements
81 out = dmod_minor(1);
82 for kk = 2:numel(dmod_minor)
83 out = out + dmod_minor(kk);
84 end
85 end
86
87 if ~callerIsMethod
88 % Set name
89 out.setName(sprintf('det(%s)', in_names{1}));
90
91 % Add history
92 out.addHistory(getInfo('None'), usepl, {inputname(1)}, [as(:).hist]);
93 end
94
95 end
96
97 %--------------------------------------------------------------------------
98 % Get Info Object
99 %--------------------------------------------------------------------------
100 function ii = getInfo(varargin)
101
102 if nargin == 1 && strcmpi(varargin{1}, 'None')
103 sets = {};
104 pl = [];
105 else
106 sets = {'Default'};
107 pl = getDefaultPlist;
108 end
109 % Build info object
110 ii = minfo(mfilename, 'matrix', 'ltpda', utils.const.categories.op, '$Id: det.m,v 1.13 2011/04/08 08:56:31 hewitson Exp $', sets, pl);
111 end
112
113 %--------------------------------------------------------------------------
114 % Get Default Plist
115 %--------------------------------------------------------------------------
116
117 function plout = getDefaultPlist()
118 persistent pl;
119 if ~exist('pl', 'var') || isempty(pl)
120 pl = buildplist();
121 end
122 plout = pl;
123 end
124
125 function pl = buildplist()
126 pl = plist.EMPTY_PLIST;
127 end
128