For MRI and NMR magnet design, a highly homogeneous and high magnetic field has to be achieved by active and/or passive shimming. The active and passive shimming commonly homogenize the magnetic field around the magnet center by cancellation of the higher terms of a spherical harmonic series than the 0th term. So far, the spherical harmonic expression in the cylindrical coordinate system is well known for a circular coil, a solenoid coil, and magnetization of ferromagnetic material with/without its volume. In a shimming design, only the z-component of an inhomogeneous magnetic field is compensated because it is dominant to the performance of MRI/NMR. However, various kinds of MRI and NMR systems have recently been developed, and these magnets sometimes have an axially asymmetric configuration or generate a tilted magnetic field. Therefore, the entire x-, y-, and z-components have to be homogenized for such magnets. I derive the spherical harmonic expression of the entire components of magnetic field generated by a circular coil, a dipole coil, and a straight line current. In addition, since the entire magnetization components of the ferromagnetic material having the volume contribute the entire components of the magnetic field around the magnet center, I also derive the equations of the spherical harmonic coefficients of the magnetic field generated by ferromagnetic material. Since these equations need a numerical integration, such as the Gauss quadrature integration, the computation accuracy of the spherical harmonic coefficients is investigated against the number of the evaluation points. We can calculate the highly accurate spherical harmonic coefficients with the small number of the evaluation points. (C) 2014 AIP Publishing LLC