Home > manopt > autodiff > mat2dl_complex.m

mat2dl_complex

PURPOSE ^

Convert x into a particular data structure to store complex numbers

SYNOPSIS ^

function dlx = mat2dl_complex(x)

DESCRIPTION ^

 Convert x into a particular data structure to store complex numbers 

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

 See also: dl2mat_complex, manoptADhelp

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function dlx = mat2dl_complex(x)
0002 % Convert x into a particular data structure to store complex numbers
0003 %
0004 % function dlx = mat2dl_complex(x)
0005 %
0006 % The iput x can be defined recursively by arrays, structs and cells. Each
0007 % part of x should contain complex numbers. The function converts each
0008 % part of x into a struct containing dlarrays with fields real and imag
0009 % which indicate the real and imaginary part of the stored complex numbers.
0010 %
0011 % See also: dl2mat_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(x) && ~iscell(x) && ~isnumeric(x)
0019         up = MException('manopt:autodiff:mat2dl_complex', ...
0020                     'mat2dl_complex should only accept a struct, a cell or a numeric array');
0021         throw(up);
0022     end
0023 
0024     % recursively convert each part of x into a particular struct
0025     if isstruct(x)
0026         dlx = mat2dl_struct(x);
0027     elseif iscell(x)
0028         dlx = mat2dl_cell(x);
0029     else
0030         xreal = real(x);
0031         ximag = imag(x);
0032         dlx.real = dlarray(xreal);
0033         dlx.imag = dlarray(ximag);
0034     end
0035 
0036     % convert x into a particular dlarray struct if x is a struct
0037     function dlx = mat2dl_struct(x)
0038         elems = fieldnames(x);
0039         nelems = numel(elems);
0040         for ii = 1:nelems
0041             if isstruct(x.(elems{ii}))
0042                 dlx.(elems{ii}) = mat2dl_struct(x.(elems{ii}));
0043             elseif iscell(x.(elems{ii}))
0044                 dlx.(elems{ii}) = mat2dl_cell(x.(elems{ii}));
0045             else
0046                 dlx.(elems{ii}) = struct();
0047                 xreal = real(x.(elems{ii}));
0048                 ximag = imag(x.(elems{ii}));
0049                 dlx.(elems{ii}).real = dlarray(xreal);
0050                 dlx.(elems{ii}).imag = dlarray(ximag);
0051             end
0052         end
0053     end
0054 
0055     % convert x into a particular dlarray struct if x is a cell
0056     function dlx = mat2dl_cell(x)
0057         ncell = length(x);
0058         for ii = 1:ncell
0059             if isstruct(x{ii})
0060                 dlx{ii} = mat2dl_struct(x{ii});
0061             elseif iscell(x{ii})
0062                 dlx{ii} = mat2dl_cell(x{ii});
0063             else
0064                 xreal = real(x{ii});
0065                 ximag = imag(x{ii});
0066                 dlx{ii} = struct();
0067                 dlx{ii}.real = dlarray(xreal);
0068                 dlx{ii}.imag = dlarray(ximag);
0069             end
0070         end
0071     end
0072 end
0073

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