Skip to content

Vectorized calculations#17

Merged
vnmanoharan merged 48 commits intomasterfrom
vectorized
Jun 24, 2025
Merged

Vectorized calculations#17
vnmanoharan merged 48 commits intomasterfrom
vectorized

Conversation

@vnmanoharan
Copy link
Copy Markdown
Contributor

This PR vectorizes the calculations, meaning that most of the functions in the package can now take an array of size parameters representing (for example) different wavelengths. The vectorization takes advantage of NumPy broadcasting, so that no additional Python loops are needed. This functionality should be useful in calculating spectroscopic quantities in the structcol package. Spectroscopic quantities can now in principle be calculated without looping over wavelengths, which should allow for vectorized (and therefore faster) Monte Carlo calculations.

Additional changes

  • sped up calculation of scattering coefficients for multilayer spheres by a factor of 10 or more by removing redundant operations and optimizing the loops
  • sped up recursion calculations by removing redundant intermediate calculations and removing loops (where possible)
  • fixed some bugs related to assumptions about the default units of the wavevector
  • fixed a bug in _cross_sections_complex_medium_sudiarta() that led to negative absorption cross sections and discrepancies in the scattering cross sections from those reported by _cross_sections_complex_medium_fu(). The cross sections calculated from the two methods now agree
  • by default, we now use scipy.special.spherical_jn and scipy.special.spherical_yn instead of scipy.special.riccati_jn and scipy.special.riccati_yn. The spherical_ functions can accept vector arguments
  • implemented continuous integration using GitHub Actions, so that the code is linted and tested after each commit and PR
  • added a suite of tests to make sure that the vectorized versions of the functions return the same results as explicit loops do
  • updated pyproject.toml to specify dependencies and installation parameters for setuptools (deleted old setup.py). Updated README.md to give installation instructions using pip.
  • bumped version number to 0.2

Notes

  • This PR will break structcol but I have a patch that I will submit as soon as this PR is resolved.
  • Most docstrings are updated, but it would be helpful to document the package using sphinx and to make the docstrings follow a uniform format. I filed an issue (Document package using sphinx #15)
  • Several functions have not yet been vectorized. These functions are not used in structcol and may need to be marked for deprecation (or deleted from the package). I filed an issue (Remove unused functions #16)

Copy link
Copy Markdown
Collaborator

@avonr avonr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test in python-mie pass but tests in structural-color do not pass when on 'vectorized' branch of python-mie and 'master' branch of strctural-color. Current 'master' branch of structural-color only compatible with 'master' of python-mie. Same error on all structcol tests
Screenshot 2025-06-20 at 11 23 14

@vnmanoharan
Copy link
Copy Markdown
Contributor Author

Right - see note 1 in the PR comment above. I have a patch for master branch in structcol, but it will have to wait until this PR is merged.

@avonr
Copy link
Copy Markdown
Collaborator

avonr commented Jun 22, 2025

In that case, looks good to me!

@vnmanoharan vnmanoharan merged commit e0ab8a4 into master Jun 24, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants