Home > manopt > autodiff > innerprodgeneral.m

innerprodgeneral

PURPOSE ^

Compute the Euclidean inner product between x and y

SYNOPSIS ^

function innerpro = innerprodgeneral(x,y)

DESCRIPTION ^

 Compute the Euclidean inner product between x and y

 function innerpro = innerprodgeneral(x,y)

 The input x and y are numeric data structures which can be defined  
 recursively by arrays, structs and cells. For the real case, the 
 inner product is defined as the sum of the hadamard product. For the
 complex case, the inner product between x and y is defined as 
 sum(real(conj(x(:)).*y(:))). The return is the sum of the inner products
 over each part of x and y. In case that x and y are structs with
 different fields, the inner product are computed only for the common fields.

 Note: Operations between dlarrays containing complex numbers are only
 introduced in Matlab R2021b or later. For Matlab R2021a or earlier, try 
 cinnerprodgeneral as an alternative way to deal with complex numbers
 stored in dlarrays. 

 See also: cinnerprodgeneral

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function innerpro = innerprodgeneral(x,y)
0002 % Compute the Euclidean inner product between x and y
0003 %
0004 % function innerpro = innerprodgeneral(x,y)
0005 %
0006 % The input x and y are numeric data structures which can be defined
0007 % recursively by arrays, structs and cells. For the real case, the
0008 % inner product is defined as the sum of the hadamard product. For the
0009 % complex case, the inner product between x and y is defined as
0010 % sum(real(conj(x(:)).*y(:))). The return is the sum of the inner products
0011 % over each part of x and y. In case that x and y are structs with
0012 % different fields, the inner product are computed only for the common fields.
0013 %
0014 % Note: Operations between dlarrays containing complex numbers are only
0015 % introduced in Matlab R2021b or later. For Matlab R2021a or earlier, try
0016 % cinnerprodgeneral as an alternative way to deal with complex numbers
0017 % stored in dlarrays.
0018 %
0019 % See also: cinnerprodgeneral
0020 
0021 % This file is part of Manopt: www.manopt.org.
0022 % Original author: Xiaowen Jiang, Aug. 31, 2021.
0023 % Contributors: Nicolas Boumal
0024 % Change log:
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     % recursively compute the inner product
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         % real case
0039         if isreal(x) && isreal(y)
0040             innerpro = x(:)'*y(:);
0041         else
0042         % complex case
0043             innerpro = sum(real(conj(x(:)).*y(:)));
0044         end
0045     end
0046     
0047     % struct case
0048     function innerpro = innerprodgeneral_struct(x,y)
0049         innerpro = 0;
0050         elemsx = fieldnames(x);
0051         elemsy = fieldnames(y);
0052         % find the common fields
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     % cell case
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

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005