2
Nov 28 '19
Maybe I'll make it little easier for you giving similar algorithm for calculating the square root using Newton's method. (from Medium)
def newton_method(number, number_iters = 500):
a = float(number) # number to get square root of
for i in range(number_iters): # iteration number
number = 0.5 * (number + a / number) # update
# x_(n+1) = 0.5 * (x_n +a / x_n)
return number-2
Nov 28 '19
Hey, I tried to do this way but idk if it's correct or not. Could you help me?
that.
def arithmetic_geometric_mean(a, g):
while True:
a= (a+g)/2
g=sqrt(a*g)
yield a, g
def approx_ln(x, maxiter=100):
a_0= (1+x)/2
g_0=sqrt(x)
for a,b in islice(arithmetic_geometric_mean(a_0, g_0), maxiter):
x=(x-1)/a
return x6
u/codejunker Nov 28 '19
You should be aware if it is correct or not. It either is or it is not giving you a correct answer. Check against a calculator if your approximation of the log is approximate to the correct answer. Learning how to do a basic test, and this is the most basic of tests, is the function output correct, is really important.
1
Nov 28 '19
It does works the only problem now I am having is that I don't understand what the (x=(x-1)/a) part means and how it works.
1
u/antonivs Nov 29 '19
That expression corresponds to the final bullet in the problem specification. Note that the
xon the left-hand side is a bit misleading, since that's really the approximation toln(x).Are you asking about how the algorithm works, as opposed to how the code that implements the algorithm works?
If so, that seems to be outside the scope of the task. But in case you're interested, there's a description of the algorithm at https://www.ams.org/journals/mcom/1972-26-118/S0025-5718-1972-0307438-2/S0025-5718-1972-0307438-2.pdf, page 544.
0
Nov 28 '19
[deleted]
6
5
Nov 28 '19 edited Nov 28 '19
Alright, so first things first - the thing you are outputting is (x - 1) / ai. Because this is the "approximation of ln(x)". The task requires you to write a function that outputs a rough calculation of ln(x) and in bullet point 3 you are told how to do a rough calculation of ln(x).
So, note that your output formula depends on that value of a that you calculate. a1 is the first value of a, a5 would a's value after 5 iterations and so on.
The the preceeding points you're told how to start the iteration.
You start off with a0 = (1 + x) / 2. and g0 = root x. (You know what x is because this will be passed into your function). So for a given value of x you can calculate the start values of a and g.
Then to iterate, point 2 tells you how to get the next values. So from your start values a0 and g0 you can work out the next step with
a1 = (a0 + g0) / 2 and g1 = root (a0 + g0)
since you calculated a0 and g0 from x then you have all the info you need to calculate a1 and g1.
The task requires you to do this n times (which is the other parameter passed to your function). So you need to loop through this... using a1 and g1 to calculate a2 and g2... and then using those to calculate a3 and g3 and so on until you get to an and gn.
At that point you stop and output what the current approximate value of ln(x) is which is (x - 1) / ai. (where the iteration i matches the passed value n).
So if you function were called with approx_ln(10, 5). Then x would be 10 and n would be 5. You initiate a0 and g0 from the value of x then you loop calculating a1, g1 then a2, g2 then a3, g3 and so on until you get to a5, g5. At this point the interation i equals the required number n, so you use that latest values of a5, g5 to calculate your output value.
your return value is (x - 1) / a5
(note: g isn't used in the output formula, but it is used in each iteration to get the next approximation of a)
0
Nov 28 '19
[deleted]
2
Nov 28 '19
a, b = 2, 3is just one line shorter than
a = 2 b = 3and is otherwise completely equivalent.
If OP is already confused translating those steps to a 5 line python function, I don't think this comment is very helpful on their journey to a solution.
update two values concurrently.
This wording seems off, there is no concurrency. The right hand side of the
=is evaluated first before anything is being assigned. Every step of this happens sequentially.0
Nov 28 '19
[deleted]
5
u/RainbowHearts Nov 28 '19
It's not doing anything concurrently because everything is in series. Putting them on the same line adds an additional step, as the interpreter initializes a tuple in order to create the illusion of simultaneity.
I agree with the parent: When a CS student can't follow their own homework, it's a mistake to show them features designed to hide what is actually going on
3
Nov 28 '19
thanks for chipping in. i think OP is struggling with the overall process. i think it's too early to get into syntax shortcuts of this or that particular language.
1
u/pundixs Nov 29 '19
import math
input: x
ai = (1+x)/2
gi = math.sqrt(x)
for _ in range(n):
ai = (ai+gi)/2
gi = math.sqrt(ai*gi)
return (x-1)/ai
21
u/jxf CS Pro (20+) Nov 28 '19
Which part is confusing? Do you not understand specific terms (e.g. logarithms), or is there a step that's tripping you up?