Skip to content

Commit

Permalink
Upgrade to arguments blocks to more nicely handle keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jmert committed Mar 19, 2021
1 parent af7db38 commit b4f976e
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 127 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A package providing Matlab bindings to the [HEALPix][healpix] C++ package.
## Build Requirements

- GCC 7+ (or a compiler which supports C++17's structured bindings)
- Matlab R2018a+
- Matlab R2019b+
- CMake 3.9.0+
- `pkg-config` (or `pkgconf`)
- Internet connection (to download HEALPix sources)
Expand Down
23 changes: 11 additions & 12 deletions matlab/+healmex/alm2cl.m.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function cl = alm2cl(alms1, alms2, varargin)
% cl = alm2cl(alms1, alms2, varargin)
function cl = alm2cl(alms1, alms2, opt)
% cl = alm2cl(alms1, alms2, ...)
%
% Computes the angular power [cross-]spectrum from the set of harmonic
% coefficients alms1 and alms2. If alms2 is empty, then alms1 is used to
Expand All @@ -20,18 +20,17 @@ function cl = alm2cl(alms1, alms2, varargin)
% cl The angular power spectrum <alms1 x alms2*>.
%

p = inputParser();
addParameter(p, 'lmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'mmax', [], @(x) isnumeric(x) & isscalar(x));
parse(p, varargin{:});
opt = p.Results;

[lmax, mmax] = @PACKAGEPREFIX@alm_getlmmax(alms, opt.lmax, opt.mmax);
if ~exist('alms2', 'var') || isempty(alms2)
alms2 = alms1;
arguments
alms1 (:,1) {mustBeNumeric}
alms2 (:,1) {mustBeNumeric} = alms1
opt.lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
end

[lmax, mmax] = @PACKAGEPREFIX@alm_getlmmax(alms1, opt.lmax, opt.mmax);
if numel(alms1) ~= numel(alms2)
error('Mismatched sizes in alms1 and alms2');
throwAsCaller(MException('healmex:alm2cl:dimensionMismatch', ...
'Mismatched sizes in alms1 and alms2'));
end

cl = libhealmex(int64(61), ...
Expand Down
19 changes: 10 additions & 9 deletions matlab/+healmex/alm2map.m.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function maps = alm2map(alms, nside, varargin)
% maps = alm2map(alms, nside, varargin)
function maps = alm2map(alms, nside, opt)
% maps = alm2map(alms, nside, ...)
%
% INPUTS
% alms The spherical harmonic coefficients of map. If map is Nx1, then
Expand All @@ -24,11 +24,12 @@ function maps = alm2map(alms, nside, varargin)
%
% EXAMPLE

p = inputParser();
addParameter(p, 'lmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'mmax', [], @(x) isnumeric(x) & isscalar(x));
parse(p, varargin{:});
opt = p.Results;
arguments
alms (:,:) {mustBeNumeric}
nside (1,1) {mustBeInteger,mustBeNonnegative}
opt.lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
end

[lmax, mmax] = @PACKAGEPREFIX@alm_getlmmax(alms, opt.lmax, opt.mmax);

Expand All @@ -43,8 +44,8 @@ function maps = alm2map(alms, nside, varargin)
almsG = complex(double(alms(:,2)));
almsC = complex(double(alms(:,3)));
else
error('alms: Expected size 1 or 3 in second dimension, got %d', ...
size(alms, 2));
throwAsCaller(MException('healmex:alm2map:dimensionMismatch', ...
'Expected alms to have size 1 or 3 in second dimension, got %d', size(alms, 2)));
end

[mapT, mapQ, mapU] = libhealmex(int64(55), ...
Expand Down
20 changes: 8 additions & 12 deletions matlab/+healmex/alm2map_der1.m.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function maps = alm2map_der1(alms, nside, varargin)
% maps = alm2map_der1(alms, nside, varargin)
function maps = alm2map_der1(alms, nside, opt)
% maps = alm2map_der1(alms, nside, ...)
%
% Synthesizes a map and its first derivatives from the provided alms at
% Nisde = nside with RING ordered pixelization.
Expand All @@ -24,19 +24,15 @@ function maps = alm2map_der1(alms, nside, varargin)
%
% EXAMPLE

p = inputParser();
addParameter(p, 'lmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'mmax', [], @(x) isnumeric(x) & isscalar(x));
parse(p, varargin{:});
opt = p.Results;
arguments
alms (:,1) {mustBeNumeric}
nside (1,1) {mustBeInteger,mustBeNonnegative}
opt.lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
end

[lmax, mmax] = @PACKAGEPREFIX@alm_getlmmax(alms, opt.lmax, opt.mmax);

if size(alms, 2) ~= 1
error('alm2map_der1: Expected alms to have size 1 in second dimension, got %d', ...
size(alms, 2));
end

[map, mapdth, mapdph] = libhealmex(int64(56), ...
int32(lmax), int32(mmax), complex(double(alms)), ...
int64(nside));
Expand Down
12 changes: 5 additions & 7 deletions matlab/+healmex/alm_getidx.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
% Returns the index of the alm coefficient (l,m) within a vector which includes
% coefficients up to lmax.

if ~isscalar(lmax)
error('lmax must be a scalar');
end
m = m(:);
if length(m) > 1
l = l(:).';
arguments
lmax (1,1) {mustBeInteger}
l (:,:) {mustBeNumeric}
m (:,:) {mustBeNumeric}
end

idx = m .* (2*lmax+1 - m) / 2 + l + 1;
idx = idx(:);
end
9 changes: 4 additions & 5 deletions matlab/+healmex/alm_getlmmax.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
% Infers the lmax and/or mmax from the alms vector (or length of the
% first dimension if alms is a matrix).

if ~exist('lmax', 'var')
lmax = [];
end
if ~exist('mmax', 'var')
mmax = [];
arguments
alms (:,:)
lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
end

if isempty(lmax)
Expand Down
11 changes: 4 additions & 7 deletions matlab/+healmex/alm_getn.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
% lmax Must be a non-negative integer.
% mmax Defaults to lmax. Must be in the range 0 to lmax.

if lmax < 0 || fix(lmax) ~= lmax
error('lmax must be a positive integer')
arguments
lmax {mustBeNonnegative}
mmax {mustBeNonnegative} = lmax
end
if ~exist('mmax', 'var') || isempty(mmax)
mmax = lmax;
end

nel = ((mmax + 1) * (mmax + 2)) / 2 + (mmax + 1) * (lmax - mmax);
nel = ((mmax + 1) .* (mmax + 2)) / 2 + (mmax + 1) .* (lmax - mmax);
end
13 changes: 7 additions & 6 deletions matlab/+healmex/almxfl.m.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function alms = almxfl(alms, fl, varargin)
function alms = almxfl(alms, fl, opt)
% alms = almxfl(alms, fl, varargin)
%
% Multiplies a set of alms by a vector fl.
Expand All @@ -18,11 +18,12 @@ function alms = almxfl(alms, fl, varargin)
% OUTPUTS
% alms Modified alms.

p = inputParser();
addParameter(p, 'lmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'mmax', [], @(x) isnumeric(x) & isscalar(x));
parse(p, varargin{:});
opt = p.Results;
arguments
alms (:,:) {mustBeNumeric}
fl (:,1) {mustBeNumeric}
opt.lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
end

[lmax, mmax] = @PACKAGEPREFIX@alm_getlmmax(alms, opt.lmax, opt.mmax);

Expand Down
41 changes: 35 additions & 6 deletions matlab/+healmex/ang2pix.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
function ipix = ang2pix(nside, order, theta, phi)
% ipix = ang2pix(nside, order, theta, phi)
function ipix = ang2pix(nside, theta, phi, opt)
% ipix = ang2pix(nside, theta, phi, ...)
%
% Calculates HEALPix pixel indices ipix (for an Nside = nside map with ordering
% scheme order) which contain the spherical coordinate points (theta,phi),
% where theta and phi are colatitude/azimuth angle in radians. order may be
% 'RING' or 'NESTED'.
% INPUTS
% nside The HEALPix Nside parameter.
% theta If lonlat==true, the latitude in degrees (-90 <= lat <= 90),
% otherwise the colatitude in radians (0 <= theta <= pi).
% phi If lonlat==true, the longitude in degrees (0 <= lon < 360),
% otherwise the azimuth in radians (0 <= phi < 2*pi).
%
% KEY-VALUE PAIRS
% 'lonlat' Defaults to false. If true, instead returns the longitude and
% latitude coordinates [lon,lat] in degrees.
% 'nest' Defaults to false. If true, `ipix` are NESTED ordering pixels,
% otherwise assumes RING ordering.
%
% OUTPUT
% ipix Pixel indices.
%

arguments
nside (1,1) {mustBeNumeric}
theta {mustBeNumeric}
phi {mustBeNumeric}
opt.lonlat (1,1) logical = false
opt.nest (1,1) logical = false
end

if opt.nest
order = 'NESTED';
else
order = 'RING';
end
if opt.lonlat
theta = deg2rad(90 - theta);
phi = deg2rad(phi);
end
ipix = libhealmex(int64(16), ...
int64(nside), char(order), double(theta), double(phi));
end
Expand Down
19 changes: 9 additions & 10 deletions matlab/+healmex/map2alm.m.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function alms = map2alm(maps, varargin)
% alms = map2alm(maps, varargin)
% alms = map2alm(maps, ...)
%
% INPUTS
% maps A vector (Nx1) or matrix (Nx3) of maps pixel values. Must be in
Expand All @@ -24,14 +24,13 @@ function alms = map2alm(maps, varargin)
% respectively, spin-2 transforms.
%
% EXAMPLE
%

p = inputParser();
addParameter(p, 'lmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'mmax', [], @(x) isnumeric(x) & isscalar(x));
addParameter(p, 'iter', 3, @(x) isnumeric(x) & isscalar(x) & x >= 0);
parse(p, varargin{:});
opt = p.Results;
arguments
maps (:,:) {mustBeNumeric}
opt.lmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.mmax {mustBeNumeric,mustBeScalarOrEmpty} = []
opt.iter {mustBeNumeric,mustBeScalarOrEmpty} = 3
end

lmax = opt.lmax;
mmax = opt.mmax;
Expand Down Expand Up @@ -59,8 +58,8 @@ function alms = map2alm(maps, varargin)
mapQ = maps(:,2);
mapU = maps(:,3);
else
error('map2alm: Expected maps to have size 1 or 3 in second dimension, got %d', ...
size(maps, 2));
throwAsCaller(MException('healmex:map2alm:dimensionMismatch', ...
'Expected maps to have size 1 or 3 in second dimension, got %d', size(maps, 2)));
end

[almsT,almsG,almsC] = libhealmex(int64(53), ...
Expand Down
16 changes: 9 additions & 7 deletions matlab/+healmex/pix2ang.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [theta, phi] = pix2ang(nside, ipix, varargin)
% [theta, phi] = pix2ang(nside, ipix, varargin)
function [theta, phi] = pix2ang(nside, ipix, opt)
% [theta, phi] = pix2ang(nside, ipix, ...)
%
% INPUTS
% nside The HEALPix Nside parameter.
Expand All @@ -10,6 +10,7 @@
% latitude coordinates [lon,lat] in degrees.
% 'nest' Defaults to false. If true, `ipix` are NESTED ordering pixels,
% otherwise assumes RING ordering.
%
% OUTPUT
% theta If lonlat==true, the latitude in degrees (-90 <= lat <= 90),
% otherwise the colatitude in radians (0 <= theta <= pi).
Expand All @@ -19,11 +20,12 @@
% EXAMPLE
% [lon,lat] = healmex.pix2ang(512, (1000:2000)', 'lonlat', true);

p = inputParser();
addParameter(p, 'nest', false, @islogical);
addParameter(p, 'lonlat', false, @islogical);
parse(p, varargin{:});
opt = p.Results;
arguments
nside (1,1) {mustBeNumeric}
ipix {mustBeNumeric}
opt.lonlat (1,1) logical = false
opt.nest (1,1) logical = false
end

if opt.nest
order = 'NESTED';
Expand Down
13 changes: 7 additions & 6 deletions matlab/+healmex/pix2vec.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [x, y, z] = pix2vec(nside, ipix, varargin)
% [x, y, z] = pix2vec(nside, ipix, varargin)
function [x, y, z] = pix2vec(nside, ipix, opt)
% [x, y, z] = pix2vec(nside, ipix, ...)
%
% INPUTS
% nside The HEALPix Nside parameter.
Expand All @@ -17,10 +17,11 @@
% EXAMPLE
% [x, y, z] = healmex.pix2vec(512, 0:4*512-1);

p = inputParser();
addParameter(p, 'nest', false, @islogical);
parse(p, varargin{:});
opt = p.Results;
arguments
nside (1,1) {mustBeNumeric}
ipix {mustBeNumeric}
opt.nest (1,1) logical = false
end

if opt.nest
order = 'NESTED';
Expand Down
13 changes: 7 additions & 6 deletions matlab/+healmex/pix2xyf.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [x, y, f] = pix2xyf(nside, ipix, varargin)
% [x, y, f] = pix2xyf(nside, ipix, varargin)
function [x, y, f] = pix2xyf(nside, ipix, opt)
% [x, y, f] = pix2xyf(nside, ipix, ...)
%
% INPUTS
% nside The HEALPix Nside parameter.
Expand All @@ -17,10 +17,11 @@
% EXAMPLE
% [x, y, f] = healmex.pix2xyf(512, (1000:2000)');

p = inputParser();
addParameter(p, 'nest', false, @islogical);
parse(p, varargin{:});
opt = p.Results;
arguments
nside (1,1) {mustBeNumeric}
ipix {mustBeNumeric}
opt.nest (1,1) logical = false
end

if opt.nest
order = 'NESTED';
Expand Down
13 changes: 7 additions & 6 deletions matlab/+healmex/pix2zphi.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [z, phi] = pix2zphi(nside, ipix, varargin)
% [z, phi] = pix2zphi(nside, ipix, varargin)
function [z, phi] = pix2zphi(nside, ipix, opt)
% [z, phi] = pix2zphi(nside, ipix, ...)
%
% INPUTS
% nside The HEALPix Nside parameter.
Expand All @@ -16,10 +16,11 @@
% EXAMPLE
% [z, phi] = healmex.pix2zphi(512, (1000:2000)');

p = inputParser();
addParameter(p, 'nest', false, @islogical);
parse(p, varargin{:});
opt = p.Results;
arguments
nside (1,1) {mustBeNumeric}
ipix {mustBeNumeric}
opt.nest (1,1) logical = false
end

if opt.nest
order = 'NESTED';
Expand Down
Loading

0 comments on commit b4f976e

Please sign in to comment.