0001 function innerpro = innerprodgeneral(x,y)
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 if ~((isstruct(x) && isstruct(y)) || (iscell(x) && iscell(y))...
0027 || (isnumeric(x) && isnumeric(y)))
0028 up = MException('manopt:autodiff:innerprodgeneral' ,...
0029 'innerprodgeneral should only accept structs, cells or arrays.');
0030 throw(up);
0031 end
0032
0033 if isstruct(x) && isstruct(y)
0034 innerpro = innerprodgeneral_struct(x,y);
0035 elseif iscell(x) && iscell(y)
0036 innerpro = innerprodgeneral_cell(x,y);
0037 else
0038
0039 if isreal(x) && isreal(y)
0040 innerpro = x(:)'*y(:);
0041 else
0042
0043 innerpro = sum(real(conj(x(:)).*y(:)));
0044 end
0045 end
0046
0047
0048 function innerpro = innerprodgeneral_struct(x,y)
0049 innerpro = 0;
0050 elemsx = fieldnames(x);
0051 elemsy = fieldnames(y);
0052
0053 [elems,ix,iy] = intersect(elemsx,elemsy, 'stable');
0054 nelems = numel(elems);
0055 for ii = 1:nelems
0056 if isstruct(x.(elemsx{ix(ii)}))
0057 innerpro = innerpro + innerprodgeneral_struct(...
0058 x.(elemsx{ix(ii)}),y.(elemsy{iy(ii)}));
0059 elseif iscell(x.(elemsx{ix(ii)}))
0060 innerpro = innerpro + innerprodgeneral_cell(...
0061 x.(elemsx{ix(ii)}),y.(elemsy{iy(ii)}));
0062 else
0063 xelem = x.(elemsx{ix(ii)});
0064 yelem = y.(elemsy{iy(ii)});
0065 if isreal(xelem) && isreal(yelem)
0066 innerpro = innerpro + xelem(:)'*yelem(:);
0067 else
0068 innerpro = innerpro + sum(real(conj(xelem(:)).*yelem(:)));
0069 end
0070 end
0071 end
0072 end
0073
0074
0075 function innerpro = innerprodgeneral_cell(x,y)
0076 innerpro = 0;
0077 ncell = length(x);
0078 for ii = 1:ncell
0079 if isstruct(x{ii})
0080 innerpro = innerpro + innerprodgeneral_struct(x{ii},y{ii});
0081 elseif iscell(x{ii})
0082 innerpro = innerpro + innerprodgeneral_cell(x{ii},y{ii});
0083 else
0084 xii = x{ii};
0085 yii = y{ii};
0086 if isreal(xii) && isreal(yii)
0087 innerpro = innerpro + xii(:)'*yii(:);
0088 else
0089 innerpro = innerpro + sum(real(conj(xii(:)).*yii(:)));
0090 end
0091 end
0092 end
0093 end
0094
0095 end
0096