Implement the Radial Basis Function (RBF) kernel, also known as the Gaussian kernel. This kernel is widely used in Support Vector Machines (SVMs), Gaussian Processes, and other kernel-based machine learning methods.
Given two sets of samples X1 (with n1 samples) and X2 (with n2 samples), each having d features, compute the kernel matrix K of shape (n1, n2) where each element K[i,j] represents the kernel value between sample i from X1 and sample j from X2.
The RBF kernel measures similarity between two samples based on the Euclidean distance between them. Points that are close together in feature space will have kernel values close to 1, while points far apart will have kernel values close to 0.
Your function should take the gamma parameter which controls the width of the kernel - a larger gamma makes the kernel more localized (sensitive to small differences), while a smaller gamma makes it more smooth.
Handle the computation efficiently using matrix operations rather than nested loops.