@@ -503,7 +503,7 @@ def _scatcoeffs(m, x, nstop, eps1 = DEFAULT_EPS1, eps2 = DEFAULT_EPS2):
503503 an = ( (Dnmx / m + n / x )* psi - psishift ) / ( (Dnmx / m + n / x )* xi - xishift )
504504 bn = ( (Dnmx * m + n / x )* psi - psishift ) / ( (Dnmx * m + n / x )* xi - xishift )
505505
506- # coefficient array has shape [ 2, num_values , nstop]
506+ # coefficient array has shape ( 2, ... , nstop)
507507 return np .array ([an [..., 1 :nstop + 1 ], bn [..., 1 :nstop + 1 ]])
508508
509509
@@ -1441,8 +1441,7 @@ def diff_abs_intensity_complex_medium(m, x, thetas, ktd):
14411441 return I_par .real , I_perp .real
14421442
14431443
1444- def amplitude_scattering_matrix (m , x , thetas ,
1445- phis = None ):
1444+ def amplitude_scattering_matrix (m , x , thetas , phis = None ):
14461445 """
14471446 Calculates the amplitude scattering matrix for an n-dim array of thetas
14481447 (and phis if in cartesian coordinate system)
@@ -1506,32 +1505,27 @@ def amplitude_scattering_matrix(m, x, thetas,
15061505 # calculate mie coefficients
15071506 coeffs = _scatcoeffs (m , x , nstop )
15081507
1509- # expand dims to allow broadcasting over phi
1510- if phis is not None :
1511- if np .ndim (phis ) < 2 :
1512- thetas = thetas [..., np .newaxis ]
1513- phis = phis [..., np .newaxis , :]
1514-
1515- # calculate amplitude scattering matrix in 'scattering plane' coordinate
1516- # system
1517- S2_sp , S1_sp = _amplitude_scattering_matrix (nstop , prefactor ,
1518- coeffs , thetas )
1519- S3_sp = np .zeros_like (S1_sp )
1520- S4_sp = np .zeros_like (S1_sp )
1508+ # calculate amplitude scattering matrix in scattering plane basis
1509+ S2 , S1 = _amplitude_scattering_matrix (nstop , prefactor , coeffs , thetas )
15211510
15221511 if phis is not None :
1523- # calculate sines and cosines
1524- cosphi = np .cos (phis )
1525- sinphi = np .sin (phis )
1512+ # expand dims to allow broadcasting over phi
1513+ S1 = S1 [..., np .newaxis ]
1514+ S2 = S2 [..., np .newaxis ]
1515+ phis = phis [..., np .newaxis , :]
15261516
15271517 # calculate elements of scattering matrix
1528- S1_xy = S2_sp * (sinphi )** 2 + S1_sp * (cosphi )** 2
1529- S2_xy = S2_sp * (cosphi )** 2 + S1_sp * (sinphi )** 2
1530- S3_xy = S2_sp * sinphi * cosphi - S1_sp * sinphi * cosphi
1531- S4_xy = S2_sp * cosphi * sinphi - S1_sp * cosphi * sinphi
1518+ cosphi = np .cos (phis )
1519+ sinphi = np .sin (phis )
1520+ S1_xy = S2 * (sinphi )** 2 + S1 * (cosphi )** 2
1521+ S2_xy = S2 * (cosphi )** 2 + S1 * (sinphi )** 2
1522+ S3_xy = S2 * sinphi * cosphi - S1 * sinphi * cosphi
1523+ S4_xy = S2 * cosphi * sinphi - S1 * cosphi * sinphi
15321524 return S1_xy , S2_xy , S3_xy , S4_xy
15331525 else :
1534- return S1_sp , S2_sp , S3_sp , S4_sp
1526+ S3 = np .zeros_like (S1 )
1527+ S4 = np .zeros_like (S1 )
1528+ return S1 , S2 , S3 , S4
15351529
15361530
15371531def vector_scattering_amplitude (m , x , thetas ,
@@ -1639,26 +1633,15 @@ def _amplitude_scattering_matrix(n_stop, prefactor, coeffs, thetas):
16391633 """
16401634 pis , taus = _pis_and_taus (n_stop , thetas )
16411635
1642- # to broadcast correctly over the dimensions of coeffs (which may be
1643- # wavelength or other variable), we need to add leading dimensions to the
1644- # pis and taus, which have shape [num_angles, order]. Result should have
1645- # pis, taus shape: [1, ..., 1, num_angles, order]
1646- # Similarly, we need to insert dimensions in coeffs corresponding to the
1647- # angles in pis and taus. Result should have
1648- # coeffs[0].shape: [num_values, ..., 1, order]
1649- num_leading_dims = len (coeffs [0 ].shape [:- 1 ])
1650- num_insert_dims = len (pis .shape [:- 1 ])
1651- new_coeffs_shape = (coeffs .shape [:- 1 ] + num_insert_dims * (1 ,)
1652- + (coeffs .shape [- 1 ],))
1653- pis = pis .reshape (num_leading_dims * (1 ,) + pis .shape )
1654- taus = taus .reshape (num_leading_dims * (1 ,) + taus .shape )
1655- coeffs = coeffs .reshape (new_coeffs_shape )
1656-
1657- # result should have shape [num_values, ..., num_angles]
1636+ # For broadcasting over theta, set shape to (..., 1, order)
1637+ coeffs = coeffs [..., np .newaxis , :]
1638+
1639+ # result should have shape (..., num_thetas)
16581640 S1 = np .sum (prefactor * (coeffs [0 ]* pis + coeffs [1 ]* taus ), axis = - 1 )
16591641 S2 = np .sum (prefactor * (coeffs [0 ]* taus + coeffs [1 ]* pis ), axis = - 1 )
16601642 return S2 , S1
16611643
1644+
16621645def _amplitude_scattering_matrix_RG (prefactor , x , thetas ):
16631646 """Amplitude scattering matrix from Rayleigh-Gans approximation
16641647
@@ -1674,7 +1657,7 @@ def _amplitude_scattering_matrix_RG(prefactor, x, thetas):
16741657 p = np .divide (np .sin (u ) - u * np .cos (u ), u ** 3 , out = np .ones_like (u ),
16751658 where = u != 0 )
16761659
1677- # result should have shape [num_values, ..., num_angles]
1660+ # result should have shape ( ..., num_angles)
16781661 S1 = prefactor * 3 * p
16791662 S2 = S1 * np .cos (thetas )
16801663 return S2 , S1
0 commit comments