Complex solutions to 1**x=4

This is a short code to demonstrate some features of fortran using complex arithmetic.

There is a familiar online puzzle to find solutions to the equation 1^x=4 (or some other right-hand-side value). There are of course no real solutions to this problem. In the online comments, you will invariably see declarations like “1 raised to any power is always 1, so there are no solutions.” However, there are complex solutions because the inverse of complex exponentiation is multivalued, and if you work on different branches you can get different solutions. It turns out that you can find numerical approximations to these solutions in fortran, but you must be careful exactly how the expressions are evaluated. Here is a code that demonstrates this.

program xxx

   ! print some complex solutions to the equation: 1**x = rhs.
   
   use, intrinsic :: iso_fortran_env, only: wp=>real64
   implicit none
   complex(wp) :: z1, arg, x
   real(wp), parameter :: rhs = 4.0_wp, twopi = 8 * atan(1.0_wp), lnrhs = log(rhs)
   integer :: k
   character(*), parameter :: cfmth='("Complex solutions to 1**x=",g0.4/a2,*(4x,a11,5x))', &
        &  cfmt='(i2,*(" (",es0.2,",",es0.2,")":))'
   write(*,cfmth) rhs, 'k', 'arg=i2Pik', 'exp(arg)', 'x', 'exp(arg)**x', 'exp(arg*x)'
   do k = 1, 10
      arg = cmplx( 0.0_wp, twopi * k, kind=wp )
      z1  = exp( arg )
      x   = cmplx( 0.0_wp, -lnrhs/(twopi * k), kind=wp )
      write(*,cfmt) k, arg, z1, x, z1**x, exp(arg*x)
   enddo
end program xxx

$ flang xxx.f90 && a.out
Complex solutions to 1**x=4.000
 k      arg=i2Pik            exp(arg)                   x         exp(arg)**x          exp(arg*x)
 1 (0.00E+00,6.28E+00) (1.00E+00,-2.45E-16) (0.00E+00,-2.21E-01) (1.00E+00,-6.62E-33) (4.00E+00,0.00E+00)
 2 (0.00E+00,1.26E+01) (1.00E+00,-4.90E-16) (0.00E+00,-1.10E-01) (1.00E+00,-1.32E-32) (4.00E+00,0.00E+00)
 3 (0.00E+00,1.88E+01) (1.00E+00,-7.35E-16) (0.00E+00,-7.35E-02) (1.00E+00,-1.99E-32) (4.00E+00,0.00E+00)
 4 (0.00E+00,2.51E+01) (1.00E+00,-9.80E-16) (0.00E+00,-5.52E-02) (1.00E+00,-2.65E-32) (4.00E+00,0.00E+00)
 5 (0.00E+00,3.14E+01) (1.00E+00,-1.22E-15) (0.00E+00,-4.41E-02) (1.00E+00,-3.31E-32) (4.00E+00,0.00E+00)
 6 (0.00E+00,3.77E+01) (1.00E+00,-1.47E-15) (0.00E+00,-3.68E-02) (1.00E+00,-3.97E-32) (4.00E+00,0.00E+00)
 7 (0.00E+00,4.40E+01) (1.00E+00,-1.71E-15) (0.00E+00,-3.15E-02) (1.00E+00,-4.63E-32) (4.00E+00,0.00E+00)
 8 (0.00E+00,5.03E+01) (1.00E+00,-1.96E-15) (0.00E+00,-2.76E-02) (1.00E+00,-5.29E-32) (4.00E+00,0.00E+00)
 9 (0.00E+00,5.65E+01) (1.00E+00,-2.20E-15) (0.00E+00,-2.45E-02) (1.00E+00,-5.96E-32) (4.00E+00,0.00E+00)
10 (0.00E+00,6.28E+01) (1.00E+00,-2.45E-15) (0.00E+00,-2.21E-02) (1.00E+00,-6.62E-32) (4.00E+00,0.00E+00)
end program xxx

The code is written so that a student can easily change the precision (the wp parameter) and the right hand side value.

First note that the cmplx() intrinsic needs to use the kind= optional argument to ensure consistent precision. Without that argument the result will always be returned with the default real kind. This quirk is due to fortran’s history when it originally supported multiple real floating point kinds but only a single complex kind (using modern fortran terminology here).

The mathematical trick here is that the number 1 can be represented as e^{i 2 \pi k} for any integer k. There are an infinite number of integers, so there are an infinite number of ways to represent the number 1. For the solution to this problem, only nonzero k values are appropriate. If you let k be a real number, then you can vary k in the neighborhood of an integer value and the result is well defined and smooth, so the inverse function ln() is also well defined and smooth in that neighborhood. The different values of k define the different branches of the ln() function.

if you substitute that expression into the equation to be solved, take the natural log of both sides, and solve for x you get x=-i ln(4)/(2 \pi k) for k≠0. These are the infinite number of solutions to the problem. The above code loops over some values of the integer k and evaluates the lhs of the expression in two different ways. One way as (e^{i 2 \pi k})^x and the other way as e^{i 2 \pi k x}. Although these two expressions appear to be the same mathematically, they differ when evaluated in fortran. This is because the first expression loses it branch identity too soon, and there is no way to evaluate the exponential and to stay on the correct branch. This is the z1 value that is printed in the code above, it is always close to 1, or to the complex number (1,0). Only the latter way works to give the desired rhs value of 4. This works because the k in the numerator correctly cancels the k in the denominator within x, keeping both the numerator and the denominator consistent and on the same branch.

So yes, there are solutions to the equation 1^x=4 and you can find them in fortran and verify that they are correct.

10 Likes

Very interesting. The issue with selecting the correct solution branch also shows up in classical airfoil theory based on the work of one of the great engineers of the 20th century, Theodore Theodorsen. His now classic NACA (the forerunner of NASA) reports were the first to present an “exact” (in the sense of how you did hand calculations in the 1930’s) solution of the problem of the potential flow about an arbitrary airfoil. Theodorsens method is based on successive conformal maps in the complex plane of an airfoil into a near circle and then mapping the near circle into an exact circle. The solution of Laplace’s equation for the flow around a circle can be computed analytically. Inverse maps are then used to return the solution to the physical space. Recently, I’ve been playing around with some of the newer methods for solving the circle map problem so I’ve had to relearn how Fortran handles complex variables along with the math underlying conformal mapping. It’s actually kind of neat and “almost” magical how well complex math work in Fortran.

For those who have an appreciation for elegant mathematics, try to work through Theodorsens two classic reports.

A short bio of Theodorsen is here:

5 Likes

Last year, I wrote a small Julia reactive notebook using Pluto where I played around these conformal mapping and Jukowski’s theory to illustrate basic aerodynamic principles to 10-12th graders. I hadn’t played around with that since Uni (15-ish years ago) and have always been marveled at how, back in the days, people could hand-draw such nice and precise pictures without the help of a computer.

1 Like

I started my engineering education in the fall of 1970. We were required to take two semesters of engineering drawing (ie drafting) where we had to do both plots as well as exploded part blueprint type drawings. I was terrible at it since even as a child I couldn’t “color between the lines”. Computer graphics makes life a lot simpler but I also marvel at the quality of the drawings and artwork in old reports. Trying to find just the right section of a French curve to plot a continuous curve through a set of data points is an art form.

Plus, don’t get me started about pocket calculators vs slide rules :grinning_face_with_smiling_eyes:
I used this one for three years before the HP35 arrived. A major investment in 1973.

Amazingly, 1.4x2 is still 2.8, even on a slide rule.

3 Likes

I also used slide rules through high school and part of college. In my case, my roommate was an electronic hobbyist, and he bought and assembled a 4-function calculator kit from Heathkit to experiment with. I used that for homework, along with my slide rule, for most of a year. Then in 1974 I bought a Sinclair scientific calculator for (I think) $99, which was about half the price of an HP 35 at that time, and about 1/4 the cost of the HP 35 when it first came out (so yes, an HP 35 was a major investment for a student). The Sinclair was smaller than the HP, it used only scientific notation with a 5-digit display, and it had fewer builtin functions, but it was a good replacement for my slide rule.

A few months later I noticed that the price for my calculator had dropped to $69. Then, a few months after that, I noticed that you could get one for free if you subscribed to Popular Science magazine. :slight_smile: By that time, the price of programmable calculators had dropped enough, and I replaced my Sinclair with an HP 25, which I used all through grad school and even beyond. I still have two of my slide rules and both of these calculators, and I still use them from time to time just for the nice memories of those days “when I wore a younger man’s clothes.”

I was a part-time student worker in the aerospace eng. depts avionics lab when the HP-35 first came out. The lab was run by a EE. An HP salesman brought a HP-35 by one day and demonstrated it to Dr. Owens and me. Back then HP had a reputation for building test equipment that would almost never break. The salesman wanted to demonstrate that the 35 carried on HP’s reputation for rugged reliability. He plugged in the power supply and then stomped on the power cord sending the calculator in a parabolic trajectory towards the labs concrete floor. He picked it up and said “see still works”. He then explained that the keys were made so that if you cut one in half, the key symbol was still visible. They really were built like tanks which is why the majority of them still work today. Sadly, I think changes in management and corporate culture made HP a shadow of its former self. The big thing for me as a student when calculators came out was I was forced to buy one just to compete with all the other students who rushed out and bought them. When all the professors had calculators, the notion that a couple decimal places of accuracy on tests etc was “good enough for Government work” no longer was true. Homework assignments requiring elaborate calculations like isentropic flow solutions got a lot harder and more involved. Still, I wouldn’t trade that experience for what todays students face. I think with slide rules and the first generation of calculators you had better feel for just what the numbers were telling you. But like you, that was when “I wore a younger mans clothes”

1 Like

For solving some problems, such as finding the angles of a triangle given the sides of the triangle, the slide rule is faster than your other computer, provided the sliding is still possible (I remember “lubricating” my slide rule (a 6 inch K&E) with boric powder.

After 55 years it could use a little slickem but still works as designed. The sticker was given to me by a Cray support engineer around 2002 or 2003 (maybe later). I was working as a contractor at a DoD HPC center. The center director was hosting some big shots from D.C. and wanted to show how the computing power had progressed over the years. I lent them my slide rule and for some reason I thought the sticker belonged on my slide rule case instead of my Gov’t supplied workstation.

Also, haven’t tried it yet but I think there is a good chance if I tried to multiply 1.4 x 2 in Fortran (single precision) and printed it out using a list directed write I would get something like 2.799997 instead of 2.8.

1 Like