Done in SymPy. General formula is
This code was used in:
- Toftul, I., Fedorovich, G., Kislov, D., Frizyuk, K., Koshelev, K., Kivshar, Y., & Petrov, M. (2023). Nonlinearity-Induced Optical Torque. Phys. Rev. Lett., 130(24), 243802. doi: 10.1103/PhysRevLett.130.243802