Home > manopt > autodiff > functions_AD > cinnerprodgeneral.m

# cinnerprodgeneral

## PURPOSE

Computes the Euclidean inner product between x and y in the complex case

## SYNOPSIS

function innerpro = cinnerprodgeneral(x, y)

## DESCRIPTION

``` Computes the Euclidean inner product between x and y in the complex case

function innerpro = cinnerprodgeneral(x, y)

The input x and y are numeric data structures which can be defined
recursively by arrays, structs and cells. Each part of x and y should
be a struct which contains the fields real and iamg which indicate
the real and imaginary part of the stored complex numbers. 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 products
are computed only for the common fields.

Note: Operations between dlarrays containing complex numbers have been
introduced in Matlab R2021b or later. This file is only useful for Matlab
R2021a or earlier. It will be discarded when Matlab R2021b is stable.

## CROSS-REFERENCE INFORMATION

This function calls:
• cconj Computes the complex conjugate of x
• cdottimes Computes the element-wise multiplication between A and B
• creal Extracts the real part of x
This function is called by:
• ehesscompute Computes the Euclidean Hessian of the cost function at x along xdot via AD.
• cnormfro Computes the Frobenius norm of X
• cnormsqfro Computes the squared Frobenius norm of X

## SOURCE CODE

```0001 function innerpro = cinnerprodgeneral(x, y)
0002 % Computes the Euclidean inner product between x and y in the complex case
0003 %
0004 % function innerpro = cinnerprodgeneral(x, y)
0005 %
0006 % The input x and y are numeric data structures which can be defined
0007 % recursively by arrays, structs and cells. Each part of x and y should
0008 % be a struct which contains the fields real and iamg which indicate
0009 % the real and imaginary part of the stored complex numbers. The inner
0010 % product between x and y is defined as sum(real(conj(x(:)).*y(:))).
0011 % The return is the sum of the inner products over each part of x and y.
0012 % In case that x and y are structs with different fields, the inner products
0013 % are computed only for the common fields.
0014 %
0015 % Note: Operations between dlarrays containing complex numbers have been
0016 % introduced in Matlab R2021b or later. This file is only useful for Matlab
0017 % R2021a or earlier. It will be discarded when Matlab R2021b is stable.
0018 %
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)) || (isstruct(x) && isnumeric(y)))
0028
0029         up = MException('manopt:autodiff:cinnerprodgeneral' ,...
0030             'cinnerprodgeneral should only accept structs, cells or arrays.');
0031         throw(up);
0032
0033     end
0034     % recursively compute the inner product
0035     if isstruct(x) && isstruct(y) && (~isfield(x,'real')) && (~isfield(y,'real'))
0036         innerpro  = cinnerprodgeneral_struct(x,y);
0037     elseif iscell(x) && iscell(y)
0038         innerpro = cinnerprodgeneral_cell(x,y);
0039     else
0040         xconj = cconj(x);
0041         product = cdottimes(xconj,y);
0042         innerpro = sum(creal(product),'all');
0043         % slower
0044         % xcol = cmat2col(x);
0045         % innerpro = creal(cprod(ctransp(xcol),xcol));
0046     end
0047
0048     % struct case
0049     function innerpro = cinnerprodgeneral_struct(x,y)
0050         innerpro = 0;
0051         elemsx = fieldnames(x);
0052         elemsy = fieldnames(y);
0053         % find the common fields
0054         [elems,ix,iy] = intersect(elemsx,elemsy, 'stable');
0055         nelems = numel(elems);
0056         for ii = 1:nelems
0057             if isstruct(x.(elemsx{ix(ii)})) && (~isfield(x.(elemsx{ix(ii)}),'real'))...,
0058                     && (~isfield(y.(elemsy{iy(ii)}),'real'))
0059                 innerpro = innerpro + cinnerprodgeneral_struct(...,
0060                     x.(elemsx{ix(ii)}),y.(elemsy{iy(ii)}));
0061             elseif iscell(x.(elemsx{ix(ii)}))
0062                 innerpro = innerpro + cinnerprodgeneral_cell(...,
0063                     x.(elemsx{ix(ii)}),y.(elemsy{iy(ii)}));
0064             else
0065                 xconj = cconj(x.(elemsx{ix(ii)}));
0066                 product = cdottimes(xconj, y.(elemsy{iy(ii)}));
0067                 innerpro = innerpro + sum(creal(product), 'all');
0068             end
0069         end
0070     end
0071
0072     % cell case
0073     function innerpro = cinnerprodgeneral_cell(x,y)
0074         innerpro = 0;
0075         ncell = length(x);
0076         for ii = 1:ncell
0077             if isstruct(x{ii}) && (~isfield(x{ii},'real')) && (~isfield(y{ii},'real'))
0078                 innerpro = innerpro + cinnerprodgeneral_struct(...,
0079                     x{ii},y{ii});
0080             elseif iscell(x{ii})
0081                 innerpro = innerpro + cinnerprodgeneral_cell(...,
0082                     x{ii},y{ii});
0083             else
0084                 xconj = cconj(x{ii});
0085                 product = cdottimes(xconj, y{ii});
0086                 innerpro = innerpro + sum(creal(product), 'all');
0087             end
0088         end
0089     end
0090
0091 end
0092```

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