samples/fixed_point.js

Mon, 18 Mar 2019 09:12:13 +0100

author
clanger
date
Mon, 18 Mar 2019 09:12:13 +0100
changeset 2476
5ac73294cf68
parent 1524
3f4320a01539
permissions
-rw-r--r--

Merge

sundar@1524 1 /*
sundar@1524 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
sundar@1524 3 *
sundar@1524 4 * Redistribution and use in source and binary forms, with or without
sundar@1524 5 * modification, are permitted provided that the following conditions
sundar@1524 6 * are met:
sundar@1524 7 *
sundar@1524 8 * - Redistributions of source code must retain the above copyright
sundar@1524 9 * notice, this list of conditions and the following disclaimer.
sundar@1524 10 *
sundar@1524 11 * - Redistributions in binary form must reproduce the above copyright
sundar@1524 12 * notice, this list of conditions and the following disclaimer in the
sundar@1524 13 * documentation and/or other materials provided with the distribution.
sundar@1524 14 *
sundar@1524 15 * - Neither the name of Oracle nor the names of its
sundar@1524 16 * contributors may be used to endorse or promote products derived
sundar@1524 17 * from this software without specific prior written permission.
sundar@1524 18 *
sundar@1524 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
sundar@1524 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
sundar@1524 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sundar@1524 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
sundar@1524 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sundar@1524 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
sundar@1524 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
sundar@1524 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
sundar@1524 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
sundar@1524 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sundar@1524 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sundar@1524 30 */
sundar@1524 31
sundar@1524 32 // Simple sample demonstrating "fixed point" computation with Streams
sundar@1524 33
sundar@1524 34 // See also https://mitpress.mit.edu/sicp/chapter1/node21.html#secprocgeneralmethods
sundar@1524 35 var Stream = Java.type("java.util.stream.Stream");
sundar@1524 36
sundar@1524 37 // generic fixed point procedure
sundar@1524 38 function fixed_point(f, init_guess) {
sundar@1524 39 var tolerance = 0.00001;
sundar@1524 40 function close_enough(v1, v2) Math.abs(v1 - v2) < tolerance;
sundar@1524 41
sundar@1524 42 var prev;
sundar@1524 43 return Stream.iterate(init_guess, f)
sundar@1524 44 .filter(function(x) {
sundar@1524 45 try {
sundar@1524 46 return prev == undefined? false : close_enough(prev, x);
sundar@1524 47 } finally {
sundar@1524 48 prev = x;
sundar@1524 49 }
sundar@1524 50 })
sundar@1524 51 .findFirst()
sundar@1524 52 .get();
sundar@1524 53 }
sundar@1524 54
sundar@1524 55 // solution to x = cos(x)
sundar@1524 56 print(fixed_point(Math.cos, 1.0))
sundar@1524 57
sundar@1524 58 // solution to x = sin(x) + cos(x)
sundar@1524 59 print(fixed_point(function(x) Math.sin(x) + Math.cos(x), 1.0));
sundar@1524 60
sundar@1524 61 // square root by Newton's method
sundar@1524 62 // http://en.wikipedia.org/wiki/Newton's_method
sundar@1524 63 function sqrt(n)
sundar@1524 64 fixed_point(function(x) (x + n/x) / 2, 2.0);
sundar@1524 65
sundar@1524 66 print(sqrt(2))
sundar@1524 67 print(sqrt(3))
sundar@1524 68
sundar@1524 69

mercurial