xarray_differentiate
- PlasmaCalcs.tools.xarray_tools.xarray_coords.xarray_differentiate(array, coord, *, chain=False, keep_attrs=True, **kw__differentiate)
differentiate array along coord, treating array like it is an xarray.DataArray.
more lenient than xarray.DataArray.differentiate:- returns 0 if can’t differentiate along coord (due to coord having size 1 or not existing.)- if ndim(coord)>1, differentiate using chain rule!chain: ‘grid’, ‘naive’, or Falsewhether and how to apply chain rule whenever ndim(coord)>1.‘grid’ –> apply chain rule but keeping only the term for ‘{coord}_dim’:dA/dc = (dA/d(c_dim))*(d(c_dim)/dc)This is useful if you want “take partial derivative along coord_dim,but respecting the actual step size along coord”.For example, if you have an array of rays, one at each FOVx and FOVy,and ray_s = distance along each ray, generated from xarray_grid(),then ray_s might be 1D (e.g. xarray_grid(min=0, step=5, N=100, name=’ray_s’),or it might be 3D (e.g. xarray_grid(min=start, max=stop, N=100, name=’ray_s’),with start and stop both being 2D with dims FOVx and FOVy.)If it is 1D, array will have ‘ray_s’ dimension only, with appropriate coords.If it is 3D, array will have ‘ray_s’ coord, and ‘ray_s_dim’ dimension.To take derivative “along each ray, respecting step size”, can simply do:xarray_differentiate(array, ‘ray_s’, chain=’grid’),instead of needing to handle each case separately.‘naive’ –> apply full NAIVE chain rule (which gives WRONG answers):dA/dc = (dA/d(dim0))*(d(dim0)/dc) + (dA/d(dim1))*(d(dim1)/dc) + …where dim0, dim1, … are the dimensions of array[coord].That part is technically correct.But, the naive part is the following:assume d(dimN)/dc = 1/(dc/d(dimN)) if coord actually varies with dimN, else 0.This is WRONG in general; the Jacobian needs to be used instead,except for in very particular cases.[TODO] - implement jacobian in generic case, make chain=’jacob’ option?False –> never use chain rule; crash if ndim(coord)>1.keep_attrs: boolwhether to copy attrs from array into the result. Default True.requires that array.coords and array.differentiate exist, otherwise raises AttributeError.