A little help understanding math....
|
12-16-2013, 11:56 PM
(This post was last modified: 05-27-2022 06:28 PM by Thomas Klemm.)
Post: #1
|
|||
|
|||
A little help understanding math....
There was a recent thread in the old forum where Cyrille asked a question concerning the implementation of the complex inverse trigonometric functions.
I must admit that it took me a while to understand what it was about. I will only deal with the complex inverse of cosine. Let's assume \(w=u+iv\). Then \[ \begin{align} z&=&x+iy=cos(w)=cos(u+iv) \\ &=&cos(u)cos(iv)-sin(u)sin(iv) \\ &=&cos(u)cosh(v)-i sin(u)sinh(v) \\ \end{align} \] But we would like to find \(w=arccos(z)\). Better don't ask me who came up with this solution: \[ \begin{align} |z+1|^2 &=& (x+1)^2+y^2 \\ &=& x^2+2x+1+y^2 \\ &=& cos^2(u)cosh^2(v)+2cos(u)cosh(v)+1+sin^2(u)sinh^2(v) \\ &=& cos^2(u)(1+sinh^2(v))+2cos(u)cosh(v)+1+sin^2(u)sinh^2(v) \\ &=& cos^2(u)+cos^2(u)sinh^2(v)+2cos(u)cosh(v)+1+sin^2(u)sinh^2(v) \\ &=& cos^2(u)+2cos(u)cosh(v)+1+(cos^2(u)+sin^2(u))sinh^2(v) \\ &=& cos^2(u)+2cos(u)cosh(v)+1+sinh^2(v) \\ &=& cos^2(u)+2cos(u)cosh(v)+cosh^2(v) \\ &=& (cos(u)+cosh(v))^2 \\ |z+1| &=& cosh(v)+cos(u) \end{align} \] And similarly we can conclude: \[ |z-1| = cosh(v)-cos(u) \] Add the two equations to get: \[ |z+1|+|z-1| = 2cosh(v) \\ cosh(v)=\frac{|z+1|+|z-1|}{2} \] From \(x=cos(u)cosh(v)\) we conclude: \[ 2x=2cos(u)cosh(v) \\ cos(u)=\frac{2x}{|z+1|+|z-1|} \] This leads to the following function: Code: from math import sqrt, acos, acosh Let's compare this to the builtin function: Code: >>> import cmath It appears we loose some accuracy when calculating \(v=acosh(W)\) for values of W close to 1. Why is that? (substitute \(t=e^v\)) \[ \begin{align} W &=& cosh(v) \\ &=& \frac{e^v+e^{-v}}{2} \\ W &=& \frac{t+\frac{1}{t}}{2} \\ 2Wt &=& t^2+1 \\ t^2-2Wt+1 &=& 0 \\ t &=& W\pm\sqrt{W^2-1} \\ \end{align} \] Now we can see what's happening: when W is close to 1 we experience cancellation and thus loss of accuracy. We'd like to calculate W-1 without loosing digits. Can we do better? Let's see what the HP-48 suggests: Code: * Q := y^2/(sqrt((|x|+1)^2 + y^2) + (|x|+1)) I've taken the liberty to add some new variables: Code: def sgn(x): Please note that M/2 = W-1 since M = U+V-2. That's exactly the value we were looking for. Instead of ln we use lnp1 (or log1p in Python) for values close to 1: \[v=ln(t)=lnp1(t-1)=lnp1(W-1\pm\sqrt{(W-1)(W+1)}\] Substitute W-1 by M/2 and we end up with the formula for IM. Well nearly, we still have to deal with the sign of the expression. What about \(\frac{1}{\sqrt{2}}\)? That was Cyrille's question. For small values of B close to 0 I assume both acos(B) and asin(B) are rather accurate. However with values close to 1 we experience again cancellation and thus loss of accuracy. In this case we'd prefer to use \(\sqrt{1-B^2}\) instead. That's exactly what is calculated with C but without cancellation. I'm lazy and leave that as an exercise. So where exactly should we switch? Why not just in the middle between 0 and \(\frac{\pi}{2}\)? And: \(cos(\frac{\pi}{4})=sin(\frac{\pi}{4})=\frac{1}{\sqrt{2}}\). But in this area we don't experience cancellation at all thus .7 will do as well. For those interested in the SYSRPL code I've added the stack-diagrams: Code: :: ; z It's much easier to understand this program thanks to the comments. I guess I'd never have figured that out without them. Thus my appeal to Cyrille and Tim: please make sure that this information doesn't get lost. Send the source-code of HP-48 (and whatever you have) to a computer museum if that's legally possible. There's so much we can learn from that. Kind regards Thomas PS: There are these two lines somewhere close to the end: Code: %%ACOSRAD ; IM B arcsin(C) arccos(C) However the use of the absolute value is missing in the comment: Code: * RE2 := { arcsin(B) |B| <= (1/sqrt(2)) Since %%ACOSRAD should never return negative values I don't understand why this was added. IMHO the command %%ABS could be removed. Any ideas why this is needed? |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
A little help understanding math.... - Thomas Klemm - 12-16-2013 11:56 PM
RE: A little help understanding math.... - Paul Dale - 12-17-2013, 10:19 AM
RE: A little help understanding math.... - Namir - 12-17-2013, 01:17 PM
RE: A little help understanding math.... - Thomas Klemm - 12-17-2013, 03:04 PM
RE: A little help understanding math.... - Albert Chan - 08-15-2021, 03:48 AM
RE: A little help understanding math.... - Albert Chan - 08-15-2021, 12:25 PM
RE: A little help understanding math.... - Albert Chan - 08-26-2021, 02:31 PM
RE: A little help understanding math.... - Albert Chan - 08-26-2021, 06:16 PM
RE: A little help understanding math.... - Albert Chan - 08-29-2021, 12:20 AM
RE: A little help understanding math.... - Albert Chan - 06-14-2023, 04:55 PM
|
User(s) browsing this thread: 1 Guest(s)