I would like to show how to code the NAO robot to beat us at Jotto (5-letter Mastermind) with python
in Choregraphe
. I will employ a brute force technique that does not require any knowledge of the frequency of letters in the English language, or smart combinations of vowels and consonants to try to minimize the number of attempts. It doesn’t use any information about the opponent, either. It goes like this:
Coding this strategy in python
requires only four variables:
whole_dict
: the list with all the words step = [x for x in whole_dict]
: A copy of whole_dict
, which is going to be shortened on each step (hence the name). Note that stating step = whole_dict
will change the contents of whole_dict
when we change the contents of step
— not a good idea. guess = random.choice(step)
: A random choice from the list step
. score
: A string containing the two digits we obtain after scoring the guess. The first digit indicates the number of correct letters in the same position as the target word; the second digit indicates the number of correct letters in the wrong position. attempts
: optional. The number of attempts at guessing words. For quality control purposes. At this point, I urge you to stop reading the post and try to implement this strategy as a simple script. When done, come back to see how it can be coded in the NAO robot.
First, a screenshot of the root
view in choregraph. Note the simplicity of the code.
The first box, ALMemory starter
, is a script box where we define all variables needed to run the program — old school. This is the code of that box:
from urllib2 import urlopen def no_repeats(word): """ Boolean: indicates whether a word has repeated letters """ new_word = "" for letter in word: if letter not in new_word: new_word += letter return new_word == word # Stole a list of 5-letter words from the following URL file = urlopen("http://www.math.sc.edu/~blanco/words.txt") words = file.read().split("\n") file.close() # remove words with repeated letters words = filter(lambda x: len(x)>0, words) words = filter(no_repeats, words) class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) pass def onLoad(self): # We start by creating a proxy to the memory of the robot, # where we will insert all the needed variables self.memoryProxy = ALProxy("ALMemory") pass def onUnload(self): pass def onInput_onStart(self): # These are the variables we need: self.memoryProxy.insertData("whole_dict", words) self.memoryProxy.insertData("step", [x for x in words]) self.memoryProxy.insertData("attempts", 0) self.memoryProxy.insertData("score", "00") self.memoryProxy.insertData("guess", None) self.onStopped() pass def onInput_onStop(self): self.onUnload() pass
The second box, Deal with my word
, is a flow diagram containing two boxes:
Say the word
. This box performs the random choice from step
, stores it in the variable guess
, and increments the value of attempts
by one. It asks then the player if guess
is the correct word — and spells it, just in case.
This is the code of Say the word
:
from time import sleep from random import choice class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) pass def onLoad(self): self.talkProxy = ALProxy("ALTextToSpeech") self.memoryProxy = ALProxy("ALMemory") pass def onUnload(self): pass def onInput_onStart(self): step = self.memoryProxy.getData("step") guess = choice(step) self.memoryProxy.insertData("guess", guess) attempts = self.memoryProxy.getData("attempts") self.memoryProxy.insertData("attempts", attempts + 1) self.talkProxy.say("Is it %s?" % guess) sleep(1) for letter in guess: self.talkProxy.say(letter) sleep(1) self.onStopped() pass def onInput_onStop(self): self.onUnload() pass
A switch case is next: if the player says “yes”, the program concludes with the script box Success!
, that states the number of attempts. The code is simple, and it illustrates once again the way to access data from ALMemory
. The only relevant portions of the code on that box are the methods onLoad
and onInput_osStart
:
def onLoad(self): self.talkProxy = ALProxy("ALTextToSpeech") self.memoryProxy = ALProxy("ALMemory") pass def onInput_onStart(self): attempts = self.memoryProxy.getData("attempts") self.talkProxy.say("Wonderful! I got it in %s attempts!" % str(attempts)) pass
If the player says “no”, we retrieve the two scores with two flow diagram boxes: First Score
and Second Score
. I will present the structure and code of the latter, and leave the code of the former as an exercise.
The first box is a simple Say that asks for the score. The second box is a Speech Recognition box that expects any digit from 0 to 5. The third is a script box that receives the second box output (as a string
), and performs the following operation (only onLoad
and onInput_onStart
are shown):
def onLoad(self): self.memoryProxy = ALProxy("ALMemory") pass def onInput_onStart(self, p): score = self.memoryProxy.getData("score") score = score[0] + p self.memoryProxy.insertData("score", score) self.onStopped() pass
And finally, the script box Say the score
. This is where the magic happens. In this box we receive the complete score, and filter all the words from step
to eliminate those that do not share the same score as guess
. We perform that with the following code:
def compute_score(guess, target): """ This function scores guess against target, mastermind style """ correct = 0 moved = 0 for x in range(5): if guess[x] in target: if guess[x] == target[x]: correct += 1 else: moved += 1 return str(correct)+str(moved) def register_score(score, step, guess): """ a simple fliter to eliminate unwanted words from step """ return filter(lambda x: compute_score(guess, x)==score, step) class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) pass def onLoad(self): self.memoryProxy = ALProxy("ALMemory") self.talkProxy = ALProxy("ALTextToSpeech") pass def onUnload(self): pass def onInput_onStart(self): score = self.memoryProxy.getData("score") step = self.memoryProxy.getData("step") guess = self.memoryProxy.getData("guess") self.talkProxy.say("So, the score of %s is %s %s" % (guess, score[0], score[1])) step = register_score(score, step, guess) #self.talkProxy.say("We are down to %s words" % str(len(step))) self.memoryProxy.insertData("step", step) self.onStopped() pass def onInput_onStop(self): self.onUnload() pass
That is all! I hope you enjoy playing Jotto with your NAO. The code can be greatly improved by adding motions in the different steps, or implementing better winning strategies. With brute force, NAO is able to find your word in about 5 attempts (as average, of course). Do you think you can find a strategy that brings this number down to 4 attempts? If so, I would love to hear about it.
- A robot may not injure a human being or, through inaction, allow a human being to come to harm.
- A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.
- A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.
Back in the 1980s, robotics—understood as autonomous mechanical thinking—was no more than a dream. A wonderful dream that fueled many children’s imaginations and probably shaped the career choices of some. I know in my case it did.
Fast forward some thirty-odd years, when I met Astro: one of three research robots manufactured by the French company Aldebaran. This NAO robot found its way into the computer science classroom of Tom Simpson in Heathwood Hall Episcopal School, and quickly learned to navigate mazes, recognize some student’s faces and names, and even dance the Macarena! It did so with effortless coding: a basic command of the computer language python
, and some idea of object oriented programming.
I could not let this opportunity pass. I created a small undergraduate team with Danielle Talley from USC (a brilliant sophomore in computer engineering, with a minor in music), and two math majors from Morris College: my Geometry expert Fabian Maple, and a McGyver-style problem solver, Wesley Alexander. Wesley and Fabian are supported by a Department of Energy-Environmental Management grant to Morris College, which funds their summer research experience at USC. Danielle is funded by the National Science Foundation through the Louis Stokes South Carolina-Alliance for Minority Participation (LS-SCAMP).
They spent the best of their first week on this project completing a basic programming course online. At the same time, the four of us reviewed some of the mathematical tools needed to teach Astro new tricks: basic algebra and trigonometry, basic geometry, and basic calculus and statistics. The emphasis—I need to point out in case you missed it—is in the word basic.
The psychologist seated herself and watched Herbie narrowly as he took a chair at the other side of the table and went through the three books systematically.
At the end of half an hour, he put them down, “Of course, I know why you brought these.”
The corner of Dr. Calvin’s lip twitched, “I was afraid you would. It’s difficult to work with you, Herbie. You’re always a step ahead of me.”
“It’s the same with these books, you know, as with the others. They just don’t interest me. There’s nothing to your textbooks. Your science is just a mass of collected data plastered together by makeshift theory — and all so incredibly simple, that it’s scarcely worth bothering about.”
“It’s your fiction that interests me. Your studies of the interplay of human motives and emotions” – his mighty hand gestured vaguely as he sought the proper words.
Liar!
— Isaac Asimov
Astro can understand what I say, and maintain conversations with me. He has four microphones for voice recognition and sound localization, and two speakers for text-to-speech synthesis. But he sometimes gets confused: I say “acute”, and he understands “abide,” for example. This might sound frustrating by all standards. But mathematicians can’t be judged by any reasonable standard. Far from seeing this as a shortcoming, my team used as motivation the mechanics behind his speech recognition functionality, and they wrote some code to turn him into an expert Mastermind player.
Wait, what? How are these two things possibly connected?
Since Astro’s hearing is far from perfect, when he hears a word which is not clear, he gathers a short list of possible candidates. He then chooses the most likely guess based upon different considerations—using clues from context, for example. Based on similar strategies, he can be taught to be a perfect player of 5-letter-word Mastermind: Pick any five-letter word in the English language. Any word, provided it has no repeated letters. Astro chooses one random word as his first guess, and requests a score like in Mastermind.
To illustrate this process, I will show you how the game went when I chose Fabian’s favorite word: “acute.”
Astro: Is it firth? Me : NO Astro: How many letters of my guess are in your word, in the right position? How many letters of my guess are in your word, in the wrong position? Me : 1, 0 Astro: Is it foams? Me : NO Astro: How many letters of my guess are in your word, in the right position? How many letters of my guess are in your word, in the wrong position? Me : 0, 1 Astro: Is it junta? Me : NO Astro: How many letters of my guess are in your word, in the right position? How many letters of my guess are in your word, in the wrong position? Me : 1, 2 Astro: Is it acute? Me : YES Astro: Wonderful, I found it in 4 attempts!
I don’t want to get too technical here, but will mention some of the ideas. The main algorithm is based on techniques of numerical root finding and solving nonlinear equations — nothing complex: high-school level bracketing by bisection, or Newton’s method. To design better winning strategies, my team exploits the benefits of randomness. The analysis of this part is done with basic probability and statistics.
Donovan’s pencil pointed nervously. “The red cross is the selenium pool. You marked it yourself.”
“Which one is it?” interrupted Powell. “There were three that MacDougal located for us before he left.”
“I sent Speedy to the nearest, naturally; seventeen miles away. But what difference does that make?” There was tension in his voice. “There are penciled dots that mark Speedy’s position.”
And for the first time Powell’s artificial aplomb was shaken and his hands shot forward for the man.
“Are you serious? This is impossible.”
“There it is,” growled Donovan.
The little dots that marked the position formed a rough circle about the red cross of the selenium pool. And Powell’s fingers went to his brown mustache, the unfailing signal of anxiety.
Donovan added: “In the two hours I checked on him, he circled that damned pool four times. It seems likely to me that he’ll keep that up forever. Do you realize the position we’re in?”
Runaround
— Isaac Asimov
Astro moves around too. It does so thanks to a sophisticated system combining one accelerometer, one gyrometer and four ultrasonic sensors that provide him with stability and positioning within space. He also enjoys eight force-sensing resistors and two bumpers. And that is only for his legs! He can move his arms, bend his elbows, open and close his hands, or move his torso and neck (up to 25 degrees of freedom for the combination of all possible joints). Out of the box, and without much effort, he can be coded to walk around, although in a mechanical way: He moves forward a few feet, stops, rotates in place or steps to a side, etc. A very naïve way to go from A
to B
retrieving an object at C
, could be easily coded in this fashion as the diagram shows:
Fabian and Wesley devised a different way to code Astro taking full advantage of his inertial measurement unit. This will allow him to move around smoothly, almost like a human would. The key to their success? Polynomial interpolation and plane geometry. For advanced solutions, they need to learn about splines, curvature, and optimization. Nothing they can’t handle.
He said he could manage three hours and Mortenson said that would be perfect when I gave him the news. We picked a night when she was going to be singing Bach or Handel or one of those old piano-bangers, and was going to have a long and impressive solo.
Mortenson went to the church that night and, of course, I went too. I felt responsible for what was going to happen and I thought I had better oversee the situation.
Mortenson said, gloomily, “I attended the rehearsals. She was just singing the same way she always did; you know, as though she had a tail and someone was stepping on it.”
One Night of Song
— Isaac Asimov
Astro has excellent eyesight and understanding of the world around him. He is equipped with two HD cameras, and a bunch of computer vision algorithms, including facial and shape recognition. Danielle’s dream is to have him read from a music sheet and sing or play the song in a toy piano. She is very close to completing this project: Astro is able now to identify partitures, and extract from them the location of the pentagrams. Danielle is currently working on identifying the notes and the clefs. This is one of her test images, and the result of one of her early experiments:
Most of the techniques Danielle is using are accessible to any student with a decent command of vector calculus, and enough scientific maturity. The extraction of pentagrams and the different notes on them, for example, is performed with the Hough transform. This is a fancy term for an algorithm that basically searches for straight lines and circles by solving an optimization problem in two or three variables.
The only thing left is an actual performance. Danielle will be leading Fabian and Wes, and with the assistance of Mr. Simpson’s awesome students Erica and Robert, Astro will hopefully learn to physically approach the piano, choose the right keys, and play them in the correct order and speed. Talent show, anyone?
Let so that too.
Show that and
I got this problem by picking some strictly positive value and breaking the integral as follows:
Let us examine now the factors and above:
We have thus proven that with At this point, all you have to do is pick (provided the denominator is not zero) and you are done.
But I am not entirely happy with what I see: my lack of training in the area of Combinatorics results in a rather dry treatment of that part of the mindmap, for example. I am afraid that the same could be told about other parts of the diagram. Any help from the reader to clarify and polish this information will be very much appreciated.
And as a bonus, I included a script to generate the diagram with the aid of the tikz libraries.
\tikzstyle{level 2 concept}+=[sibling angle=40] \begin{tikzpicture}[scale=0.49, transform shape] \path[mindmap,concept color=black,text=white] node[concept] {Pure Mathematics} [clockwise from=45] child[concept color=DeepSkyBlue4]{ node[concept] {Analysis} [clockwise from=180] child { node[concept] {Multivariate \& Vector Calculus} [clockwise from=120] child {node[concept] {ODEs}}} child { node[concept] {Functional Analysis}} child { node[concept] {Measure Theory}} child { node[concept] {Calculus of Variations}} child { node[concept] {Harmonic Analysis}} child { node[concept] {Complex Analysis}} child { node[concept] {Stochastic Analysis}} child { node[concept] {Geometric Analysis} [clockwise from=-40] child {node[concept] {PDEs}}}} child[concept color=black!50!green, grow=-40]{ node[concept] {Combinatorics} [clockwise from=10] child {node[concept] {Enumerative}} child {node[concept] {Extremal}} child {node[concept] {Graph Theory}}} child[concept color=black!25!red, grow=-90]{ node[concept] {Geometry} [clockwise from=-30] child {node[concept] {Convex Geometry}} child {node[concept] {Differential Geometry}} child {node[concept] {Manifolds}} child {node[concept,color=black!50!green!50!red,text=white] {Discrete Geometry}} child { node[concept] {Topology} [clockwise from=-150] child {node [concept,color=black!25!red!50!brown,text=white] {Algebraic Topology}}}} child[concept color=brown,grow=140]{ node[concept] {Algebra} [counterclockwise from=70] child {node[concept] {Elementary}} child {node[concept] {Number Theory}} child {node[concept] {Abstract} [clockwise from=180] child {node[concept,color=red!25!brown,text=white] {Algebraic Geometry}}} child {node[concept] {Linear}}} node[extra concept,concept color=black] at (200:5) {Applied Mathematics} child[grow=145,concept color=black!50!yellow] { node[concept] {Probability} [clockwise from=180] child {node[concept] {Stochastic Processes}}} child[grow=175,concept color=black!50!yellow] {node[concept] {Statistics}} child[grow=205,concept color=black!50!yellow] {node[concept] {Numerical Analysis}} child[grow=235,concept color=black!50!yellow] {node[concept] {Symbolic Computation}}; \end{tikzpicture}
I would like to show a few more examples of beautiful curves generated with this technique, together with their generating axiom, rules and parameters. Feel free to click on each of the images below to download a larger version.
Note that any coding language with plotting capabilities should be able to tackle this project. I used once again tikz for , but this time with the tikzlibrary lindenmayersystems.
Would you like to experiment a little with axioms, rules and parameters, and obtain some new pleasant curves with this method? If the mathematical properties of the fractal that they approximate are interesting enough, I bet you could attach your name to them. Like the astronomer that finds through her telescope a new object in the sky, or the zoologist that discover a new species of spider in the forest.
Most of these results are easily shown with sympy without the need to resort to Gröbner bases or Ritt-Wu techniques. As usual, we realize that the properties are independent of rotation, translation or dilation, and so we may assume that the vertices of the triangle are and for some positive parameters To prove the last statement, for instance we may issue the following:
>>> import sympy >>> from sympy import * >>> A=Point(0,0) >>> B=Point(1,0) >>> r,s=var('r,s') >>> C=Point(r,s) >>> D=Segment(A,B).midpoint >>> E=Segment(B,C).midpoint >>> F=Segment(A,C).midpoint >>> simplify(Triangle(A,B,C).circumcircle.area/Triangle(D,E,F).circumcircle.area) 4
But probably the most amazing property of the nine-point circle, is the fact that it is tangent to the incircle of the triangle. With exception of the case of equilateral triangles, both circles intersect only at one point: the so-called Feuerbach point.
The two results that we want to prove, whose synthetic proofs can be read in [this article] are the following:
Theorem 1. The circle through the feet of the internal bisectors of triangle passes through the Feuerbach point of the triangle.
Theorem 2. The Feuerbach point of triangle is the anti-Steiner point of the Euler line of the intouch triangle of with respect to the same triangle.
Let us prove the first Theorem for the case of a right-triangle. We only need to modify the definition of the point to guarantee this situation. We set for some positive value of the parameter .
Let us start by collecting the hypothesis polynomials that define the coordinates of the Feuerbach point of the triangle
>>> import sympy >>> from sympy import * >>> A=Point(0,0) >>> B=Point(1,0) >>> var('s',positive=True) s >>> C=Point(0,s) >>> Triangle(A,B,C).incircle.equation() -s**2/(s + sqrt(s**2 + 1) + 1)**2 + (-s/(s + sqrt(s**2 + 1) + 1) + x)**2 + (-s/(s + sqrt(s**2 + 1) + 1) + y)**2
We shall introduce the parameter in our polynomial rings, and relate it to the parameter so that This will allow us to input the incenter, incircle and their interaction with other objects, in a simple polynomial way.
>>> var('u') u >>> expr=Triangle(A,B,C).incircle.equation().subs(sqrt(s**2+1),u) >>> numer(together(expr)) -s**2 + (-s + x*(s + u + 1))**2 + (-s + y*(s + u + 1))**2 >>> T=Triangle( Segment(A,B).midpoint, Segment(A,C).midpoint, Segment(B,C).midpoint) >>> expr=together(T.circumcircle.equation()) >>> numer(expr) -s**2 + (-s + 4*y)**2 + (4*x - 1)**2 - 1
We can then use the following three polynomials to define the Feuerbach point:
Let us now proceed to computing the three feet of the internal bisectors, starting by collecting the coordinates of the incenter:
>>> Ic=Triangle(A,B,C).incenter.subs(sqrt(s**2+1),u) >>> intersection(Line(A,C), Line(B,Ic)) [Point(0, s/(u + 1))] >>> intersection(Line(A,B), Line(C,Ic)) [Point(s/(s + u), 0)] >>> intersection(Line(B,C), Line(A,Ic)) [Point(s/(s + 1), s/(s + 1))]
This gives us the hypothesis polynomials for the coordinates of the three feet:
Note we do not need to include polynomials for or since those are always zero. We proceed to compute a polynomial equation of a circle that goes through the last three points:
>>> var('x2:5 y2:5') (x2, x3, x4, y2, y3, y4) >>> Q1=Point(0,y2) >>> Q2=Point(x3,0) >>> Q3=Point(x4,y4) >>> expr=together(Triangle(Q1,Q2,Q3).circumcircle.equation()) >>> numer(expr) -(y2*(-x3**2 + x4**2 + y4**2) + y4*(x3**2 - y2**2))**2 + (2*x*(x3*y4 - y2*(x3 - x4)) - y2*(-x3**2 + x4**2 + y4**2) - y4*(x3**2 - y2**2))**2 + (-x3*(-x3**2 + x4**2 + y4**2) + 2*y*(x3*y4 - y2*(x3 - x4)) - (x3 - x4)*(x3**2 - y2**2))**2 - (x3*(-x3**2 + x4**2 + y4**2) - 2*y2*(x3*y4 - y2*(x3 - x4)) + (x3 - x4)*(x3**2 - y2**2))**2
We thus have the thesis polynomial for the coordinates of the Feuerbach point to belong on the required circumcircle:
We could use then the following code in sage or sympy in a Python session, to prove the result:
import sympy from sympy import var, perm var('s u x1:5 y1:5) # Hypotheses for the Feuerbach point h1=s**2+1-u**2 h2=-s**2 + (-s + 4*y1)**2 + (4*x1 - 1)**2 - 1 h3=-s**2 + (-s + x1*(s + u + 1))**2 + (-s + y1*(s + u + 1))**2 # Hypotheses for the three feet h4=(u+1)*y2-s h5=(s+u)*x3-s h6=(s+1)*x4-s h7=(s+1)*y4-s # Thesis polynomial g=-(y2*(-x3**2 + x4**2 + y4**2) + y4*(x3**2 - y2**2))**2 + (2*x1*(x3*y4 - y2*(x3 - x4)) - y2*(-x3**2 + x4**2 + y4**2) - y4*(x3**2 - y2**2))**2 + (-x3*(-x3**2 + x4**2 + y4**2) + 2*y1*(x3*y4 - y2*(x3 - x4)) - (x3 - x4)*(x3**2 - y2**2))**2 - (x3*(-x3**2 + x4**2 + y4**2) - 2*y2*(x3*y4 - y2*(x3 - x4)) + (x3 - x4)*(x3**2 - y2**2))**2 # The system is almost triangular, except for polynomials h2 and h3. # Substitute those two by the proper pseudo-reminders wrt y1 h3a=prem(h3,h2,y1) h2a=prem(h2,h3a,y1) # The proof (by the Ritt-Wu method): # If the Theorem is true, the last reminder must be zero. R=prem(g,h7,y4) R=prem(R,h6,x4) R=prem(R,h5,x3) R=prem(R,h4,y2) R=prem(R,h3a,y1) R=prem(R,h2a,x1) R=prem(R,h1)
To prove the second theorem, we need to go over the notion of anti-Steiner points first, and some of their properties:
It is known that there is a one-to-one correspondence between any point in the circumcircle of a triangle with a unique line that goes through the orthocenter, via reflections with the sides of the triangle:
It is relatively easy to prove these two facts with sympy (for the geometric computations) and sagemath (for the Gröbner bases calculations). For example, for the first property, we could do as follows: Start by defining a function that computes the reflection of a point with respect to a line.
def reflection(Pt,ln): Q=intersection(ln.perpendicular_line(Pt),ln)[0] return Point(2*Q.x+Pt.x, 2*Q.y+Pt.y)
Given a generic point , the three reflections with respect to the sides of the triangle read as follows:
>>> reflection(P,Line(A,B)) Point(x, -y) >>> reflection(P,Line(B,C)) Point(-x + 2*(s**2 + (r - 1)*(r*x + s*y - x))/(s**2 + (r - 1)**2), 2*s*(r*x - r + s*y - x + 1)/(s**2 + (r - 1)**2) - y) >>> reflection(P,Line(A,C)) Point(2*r*(r*x + s*y)/(r**2 + s**2) - x, 2*s*(r*x + s*y)/(r**2 + s**2) - y)
If the point is to belong in the circumcircle of triangle then it must satisfy the corresponding polynomial equation:
>>> numer(together(Triangle(A,B,C).circumcircle.equation())) s**2*(2*x - 1)**2 - s**2 - (r**2 - r + s**2)**2 + (-r**2 + r - s**2 + 2*s*y)**2
This is the (only needed) hypothesis polynomial. The thesis polynomial, that inquires whether the three reflection points are collinear, can be reduced to asking whether the triangle has area zero:
>>> numer(together(Triangle(P1,P2,P3).area)) 2*s**2*(r**2*y - r*y + s**2*y - s*x**2 + s*x - s*y**2)
A quick sagemath session proves the result true:
sage: R.<x,y,z,r,s>=PolynomialRing(QQ,5,order='lex') sage: h=s**2*(2*x - 1)**2-s**2-(r**2-r+s**2)**2+(-r**2+r-s**2+2*s*y)**2 sage: g=2*s**2*(r**2*y-r*y+s**2*y-s*x**2+s*x-s*y**2) sage: I=R.ideal(1-z*g,h) sage: I.groebner_basis() [1]
We start, as usual, indicating that this property is independent of shifts, rotations or dilations, and therefore we may assume that the isosceles triangle has one vertex at , another vertex at and the third vertex at for some value In that case, we will need to work on the polynomial ring since we need the parameter free, the variables and are used to input the conditions for the circumcenter of the triangle, the variables and for centroid, and the variables and for the incenter (note that we do not need to use the orthocenter in this case).
We may obtain all six conditions by using sympy, as follows:
>>> import sympy >>> from sympy import * >>> A=Point(0,0) >>> B=Point(1,0) >>> s=symbols("s",real=True,positive=True) >>> C=Point(1/2.,s) >>> T=Triangle(A,B,C) >>> T.circumcenter Point(1/2, (4*s**2 - 1)/(8*s)) >>> T.centroid Point(1/2, s/3) >>> T.incenter Point(1/2, s/(sqrt(4*s**2 + 1) + 1))
This translates into the following polynomials
The hypothesis polynomial comes simply from asking whether the slope of the line through two of those centers is the same as the slope of the line through another choice of two centers; we could use then, for example, It only remains to compute the Gröbner basis of the ideal Let us use SageMath for this task:
sage: R.<s,x1,x2,x3,y1,y2,y3,z>=PolynomialRing(QQ,8,order='lex') sage: h=[2*x1-1,8*r*y1-4*r**2+1,2*x2-1,3*y2-r,2*x3-1,(4*r*y3+1)**2-4*r**2-1] sage: g=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1) sage: I=R.ideal(1-z*g,*h) sage: I.groebner_basis() [1]
This proves the result.
To celebrate, I would like to pose a few coding challenges on the use of this library, based on a fun geometric puzzle from cut-the-knot: Rhombus in Circles
Segments and are equal. Lines and intersect at Form four circumcircles: Prove that the circumcenters form a rhombus, with
Note that if this construction works, it must do so independently of translations, rotations and dilations. We may then assume that is the origin, that the segments have length one, and that for some parameters it is We let SymPy take care of the computation of circumcenters:
import sympy from sympy import * # Point definitions M=Point(0,0) A=Point(2,0) B=Point(1,0) a,theta=symbols('a,theta',real=True,positive=True) C=Point((a+1)*cos(theta),(a+1)*sin(theta)) D=Point(a*cos(theta),a*sin(theta)) #Circumcenters E=Triangle(A,C,M).circumcenter F=Triangle(A,D,M).circumcenter G=Triangle(B,D,M).circumcenter H=Triangle(B,C,M).circumcenter
Finding that the alternate angles are equal in the quadrilateral is pretty straightforward:
In [11]: P=Polygon(E,F,G,H) In [12]: P.angles[E]==P.angles[G] Out[12]: True In [13]: P.angles[F]==P.angles[H] Out[13]: True
To prove it a rhombus, the two sides that coincide on each angle must be equal. This presents us with the first challenge: Note for example that if we naively ask SymPy whether the triangle is equilateral, we get a False statement:
In [14]: Triangle(E,F,G).is_equilateral() Out[14]: False In [15]: F.distance(E) Out[15]: Abs((a/2 - cos(theta))/sin(theta) - (a - 2*cos(theta) + 1)/(2*sin(theta))) In [16]: F.distance(G) Out[16]: sqrt(((a/2 - cos(theta))/sin(theta) - (a - cos(theta))/(2*sin(theta)))**2 + 1/4)
Part of the reason is that we have not indicated anywhere that the parameter theta is to be strictly bounded above by (we did indicate that it must be strictly positive). The other reason is that SymPy does not handle identities well, unless the expressions to be evaluated are perfectly simplified. For example, if we trust the routines of simplification of trigonometric expressions alone, we will not be able to resolve this problem with this technique:
In [17]: trigsimp(F.distance(E)-F.distance(G),deep=True)==0 Out[17]: False
Finding that with SymPy is not that easy either. This is the second challenge.
How would the reader resolve this situation?
Exercise 1 Let be a measurable space and suppose is a sequence of measurable functions in that converge almost everywhere to a function and such that the sequence of norms converges to . Prove that the sequence of integrals converges to the integral for every measurable set .
Proof: Note first that
Set then (which are non-negative functions) and apply Fatou’s Lemma to that sequence. We have then
which implies
It must then be . But this proves the statement, since
Exercise 2 Let be a finite measure space and let . Suppose that is a sequence of measurable functions in whose norms are uniformly bounded in and which converge almost everywhere to a function . Prove that the sequence converges to for all where is the conjugate exponent of .
Proof: The proof is very similar to the previous problem. We start by noticing that under the hypotheses of the problem,
If we prove that , we are done.
We will achieve this by using the convexity of , since in that case it is
hence,
Set then (which are non-negative functions) and apply Fatou’s Lemma as before.