For illustration, you could pass an Empty down the length of the cube, create a vertex group in the cube, (say, 'Prox',) and give the cube a Vertex Weight Proximity modifier, modifying the weights in the group by the proximity of the Empty.
Here, we see the Vertex Weight Proximity modifier, affecting the 'Prox' group, targeted on the plain-axis Empty, and the consequent weights (made visible in the 'Overlay' settings,in Edit Mode).
Those weights can then be used to control other modifiers. For longitudinal compression, you could parent a plane to the Empty, and give the cube a Shrinkwrap modifier, projected in + and - of the length-axis (mine is X) towards the plane, weighted by the 'Prox' group:
If, for example, you wanted a leading compression front, rather than contraction on both sides of the plane, you could get the shrinkwrap to project on one side only.
This, though, is as shown:
For transverse compression, you could use a Displace (along normals) modifier instead of the shrinkwrap.. no need for the plane:
If you needed something more mathematically exact, I'm sure there will be a Geometry Nodes solution. For that, maybe you could provide your wave function.