The following is not simpler but invents a "continued root" in which dyadic
root %: plays the role dyadic % plays in a continued fraction.
Here is the picture of a "continued root":
a1
b0 + %:
b1 + a2
%:
b2 + a3
%:
b3 +
.
.
.
Verb cr below produces "convergents" which stop with a "diagonal"
element. The basic idea in verb cr belongs to Raul Miller.
cr =: {.@] , {.@] + [: ([: %:`+/ ,)\ [: |: [ ,: }.@] NB. Usage a cr b
(13#2) cr 0,13#1 NB. The 2 means square roots are used
0 1 1.414213562 1.553773974 1.598053182 1.611847754 1.616121207 1.617442799
1.617851291 1.617977531 1.618016542 1.618028597 1.618032323 1.618033474
--Kip Murray
On Thursday, April 14, 2016, Raul Miller <[email protected]> wrote:
> One thing you could do is get rid of the intermediate names in gr:
>
> gr=: monad define
> 1-~ (+&%:)/ y$1
> )
>
> And you might want to make this tacit, for example:
>
> 13 :'1-~ (+&%:)/ y$1'
> 1 -~ [: +&%:/ 1 $~ ]
>
> Or, depending on your preferences, you might want to use induction
> rather than insertion:
>
> gri=: 1-~(1+%:@])^:([-1:)&1
>
> I guess it's really a matter of what your idea of "elegance" is...
>
> Personally, when I am fiddling with small expressions, I like to set
> up a line that evaluates and then tweak the expression and watch to
> make sure the result does not change. For this example, I'd have lines
> like:
>
> gr 10
> 1.61798
> 1-~(1+%:)^:9]1
> 1.61798
> 13 :'((+&%:)/ y$1) - 1' 10
> 1.61798
> (1-~(1+%:@])^:([-1:)&1) 10
> 1.61798
>
> (with lots of other lines, including some errors, mixed in)
>
> But the precision issue you are seeing is really the print precision
> global parameter. See
> http://www.jsoftware.com/help/dictionary/dx009.htm for how to change
> that.
>
> I hope this helps.
>
> --
> Raul
>
> On Thu, Apr 14, 2016 at 3:13 PM, Martin Kreuzer <[email protected]
> <javascript:;>> wrote:
> > Moving from continued fraction to continued square root, I arrived at
> this:
> >
> > NB. modelling gr=. rt(1+rt(1+rt(1+rt(1+...))))
> >
> > gr=. monad define
> > ps=. +
> > rt=. %:
> > v=. y $ 1
> > r=. 1-~ (ps&rt)/ v
> > )
> > gr 10
> > 1.61798
> > gr 13
> > 1.61803
> >
> > Q1:
> > What would be (more elegant and/or concise) ways to do this, especially
> the
> > line with the return value (r)..?
> > Q2:
> > What should I do to get higher precision (more digits) in the result (and
> > still having a floating point number); does that need a "foreign"..?
> > (I'm sure that I have seen this before, but can't remember where.)
> >
> > Thanks
> > -M
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
--
Sent from Gmail Mobile
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm