Home > manopt > autodiff > dl2mat_complex.m

dl2mat_complex

PURPOSE ^

Convert dlx which stores complex numbers in a structure into double

SYNOPSIS ^

function x = dl2mat_complex(dlx)

DESCRIPTION ^

 Convert dlx which stores complex numbers in a structure into double

 function dlx = dl2mat_complex(x)
 
 The iput dlx can be defined recursively by arrays, structs and cells. 
 Each part of dlx is a struct containing dlarrays with fields real and imag 
 which indicate the real and imaginary part of the stored complex numbers. 
 The function converts the struct of each part back to complex numbers. 

 See also: mat2dl_complex, manoptADhelp

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function x = dl2mat_complex(dlx)
0002 % Convert dlx which stores complex numbers in a structure into double
0003 %
0004 % function dlx = dl2mat_complex(x)
0005 %
0006 % The iput dlx can be defined recursively by arrays, structs and cells.
0007 % Each part of dlx is a struct containing dlarrays with fields real and imag
0008 % which indicate the real and imaginary part of the stored complex numbers.
0009 % The function converts the struct of each part back to complex numbers.
0010 %
0011 % See also: mat2dl_complex, manoptADhelp
0012 
0013 % This file is part of Manopt: www.manopt.org.
0014 % Original author: Xiaowen Jiang, July. 31, 2021.
0015 % Contributors: Nicolas Boumal
0016 % Change log:
0017 
0018     if ~isstruct(dlx) && ~iscell(dlx) 
0019         up = MException('manopt:autodiff:dl2mat_complex', ...
0020                     'dl2mat_complex should only accept a struct or a cell.');
0021         throw(up);
0022     end
0023 
0024     % recursively convert each part of dlx into double
0025     if isstruct(dlx) && (~isfield(dlx,'real'))
0026         x = dl2mat_struct(dlx);
0027     elseif iscell(dlx)
0028         x = dl2mat_cell(dlx);
0029     else
0030         x.real = extractdata(dlx.real);
0031         x.imag = extractdata(dlx.imag);
0032         % recover complex numbers
0033         x = x.real + 1i*x.imag;
0034     end
0035     % convert dlx into double if dlx is a struct
0036     function x = dl2mat_struct(dlx)
0037         elems = fieldnames(dlx);
0038         nelems = numel(elems);
0039         for ii = 1:nelems
0040             if isstruct(dlx.(elems{ii})) && (~isfield(dlx.(elems{ii}),'real'))
0041                 x.(elems{ii}) = dl2mat_struct(dlx.(elems{ii}));
0042             elseif iscell(dlx.(elems{ii})) 
0043                 x.(elems{ii}) = dl2mat_cell(dlx.(elems{ii}));
0044             else
0045                 % recover complex numbers
0046                 x.(elems{ii}) = extractdata(dlx.(elems{ii}).real) + 1i*extractdata(dlx.(elems{ii}).imag);
0047             end
0048         end
0049     end
0050     % convert dlx into double if dlx is a cell
0051     function x = dl2mat_cell(dlx)
0052         ncell = length(dlx);
0053         for ii = 1:ncell
0054             if isstruct(dlx{ii}) && (~isfield(dlx{ii},'real'))
0055                 x{ii} = dl2mat_struct(dlx{ii});
0056             elseif iscell(dlx{ii})
0057                 x{ii} = dl2mat_cell(dlx{ii});
0058             else
0059                 % recover complex numbers
0060                 x{ii} = extractdata(dlx{ii}.real) + 1i*extractdata(dlx{ii}.imag);
0061             end
0062         end
0063     end
0064 end
0065

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