Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Compute the mediant of a list of non-negative rationals
| ResourceFunction["Mediant"][{r1,r2,…}] gives the sum of the numerators of the ri divided by the sum of their denominators. | 
Take the mediant of a pair of fractions:
| In[1]:= | ![ResourceFunction["Mediant"][{1/2, 3/4}]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/10c88085f6f62f38.png) | 
| Out[1]= |  | 
Create a list of pseudorandom positive rationals:
| In[2]:= | ![rats = RandomInteger[{1, 10}, 20]/RandomInteger[{1, 10}, 20]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/1e21810b77996a87.png) | 
| Out[2]= |  | 
Take the mediant:
| In[3]:= | ![med = ResourceFunction["Mediant"][rats]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/55ef1bfcb5d00cd1.png) | 
| Out[3]= |  | 
Check that the mediant lies between the smallest and largest:
| In[4]:= | ![Between[med, MinMax[rats]]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/3f1e4506af3d8f66.png) | 
| Out[4]= |  | 
In a Farey sequence, the kth element is the mediant of the (k-1)th and (k+1)th elements:
| In[5]:= | ![rats = FareySequence[9]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/29d8c34306d25b23.png) | 
| Out[5]= |  | 
| In[6]:= | ![With[{k = 7}, rats[[k]] == ResourceFunction["Mediant"][rats[[{k - 1, k + 1}]]]]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/5efb75a7a7548f99.png) | 
| Out[6]= |  | 
The value of the Minkowski question mark function at the mediant can be expressed in terms of the values at the Farey neighbors:
| In[7]:= | ![With[{k = 11},
 MinkowskiQuestionMark[
   ResourceFunction["Mediant"][rats[[{k - 1, k + 1}]]]] == 1/2 (MinkowskiQuestionMark[rats[[k - 1]]] + MinkowskiQuestionMark[rats[[k + 1]]])]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/257696333d3e716f.png) | 
| Out[7]= |  | 
Mediant will not work with numbers that are not explicitly non-negative integers or rationals:
| In[8]:= | ![ResourceFunction["Mediant"][{1/2, -3/4}]](https://www.wolframcloud.com/obj/resourcesystem/images/25d/25d3c6cc-a9fa-40d5-a869-57c881eff760/486c1e555f88538d.png) | 
| Out[8]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License