Many answers have gone into great detail about temperaments and offered this up as a reason we can’t tune mathematically, but this is not correct.
Temperaments can be easily defined mathematically with high precision, and the most commonly used, equal temperament, is the simplest. The frequency ratio between any two adjacent notes (semitones) is the 12th root of 2. That is pure mathematics, we can precisely calculate the target frequency for any note.
The problem with the piano, and stringed instruments in general, is the imprecise nature of its overtones. When a string vibrates, it rarely vibrates at just its fundamental frequency, or the tone we primarily perceive. It also vibrates at integer multiples of the fundamental, and these vibrations are called overtones or harmonics.
The first overtone (second harmonic - just to confuse things!) is double the frequency, or one octave higher. It’s possible to excite the string in a manner that it will only vibrate at the frequency of the overtone, and this results in motionless nodes along the string with the peak amplitudes of the vibrations forming an “s” shape.
In practice the string will vibrate at the fundamental frequency and many of its overtones simultaneously, forming a quite complex motion.
A string vibrating at its fundamental frequency will transition to a harmonic simply by touching it (damping) at one of the nodes corresponding to that harmonic. Piano hammers are positioned very precisely along the strings to ensure that the harmonics produced by a strike give exactly the tone quality the manufacturer intends, and likewise the dampers are positioned so as to not damp at the nodes of any strong harmonics, which would leave them ringing.
So what does this all have to do with the mathematics of tuning?
As I said earlier, the overtones of strings are not perfect. When a string vibrates at its fundamental frequency, this frequency is mostly governed by the mass of the string, and the forces required to displace it against its tension. But as the string displaces its tension increases as its no longer in a straight line between the bridges/agraffes that support it. The higher the amplitude of the vibration the higher the string’s average tension becomes. This change in tension affects the overtones to a greater degree than the fundamental, by making them slightly sharp.
When tuning a piano, octaves are tuned to each other by striking them together and listening for beats. The beats are an interference pattern that manifests as audible amplitude modulation (warbling) whereby slightly different frequencies alternately complement and cancel each other. These frequencies consist of the fundamental tone of the higher note and the first overtone of the note an octave below it. Octaves are in tune when the beating is eliminated.
You can probably already see the problem here, we have just tuned a note to the slightly sharp imperfect overtone of the note an octave below it. After setting a temperament in the middle section of the piano, the remainder of the piano is tuned in octaves from this temperament octave resulting in a compounding error in both directions.
This is the reason we can’t tune a piano mathematically. There’s no practical way to calculate the degree of error in each string’s overtones. Even the force with which the piano tuner strikes the keys can influence this, as do the small forces the string’s vibrations require to bend them where they traverse the bridges. Other factors also come into play such as the length of the strings relative to their gauge, and the consistency of windings and how closely to the bridge they’re wound.
“Stretch” tuning as it’s known, is the only way to make a piano sound harmonious across its entire range, otherwise we’d hear beating with every octave played. This becomes a problem when a piano is played accompanied by other perfectly tuned instruments. Orchestral scores must be carefully crafted to avoid these pitch mismatches, and indeed highly skilled musicians will make minute adjustments to their own pitches where necessary to remain harmonious with the piano. This is one of the reasons large grand pianos are used in orchestral settings, the increased length of their bass strings significantly reduces the error in their overtones, so only the top octave or two are problematic.
Which brings me to a final point. There is a computer application for tuning pianos which claims to precisely compensate for overtone error and calculates the pitch of every note to achieve a perfect stretch tuning. It does this by first listening to every note on the piano and logging the overtone errors. An algorithm is then applied to arrive at the target pitches and it then assists the operator to precisely set these pitches. I don’t know how this compares to the tune of an experienced human tuner but one thing is for certain, the experienced tuner’s technique would most definitely ensure that the tune is more stable. Piano tuning is a lot more than just turning pins!