On 15. 5. 2019 15:44, Tomaž Turk wrote:
In javascript I believe is

var f = function(x) { return Math.cos(x) + x; }
var df = function(x) { return f(x + 1e-8) - f(x) * 1e8; }

You should use modern JS for comparision, though, so:

const f = x => Math.cos(x) + x;
const df = x => (f(x + 1e-8) - f(x)) * 1e8;

(fixed the operator precedence as well)

Herby

P.S.: I would parametrize the epsilon, as well as function, so

const deriv = epsilon => f => x => (f(x + epsilon) - f(x)) * epsilon;
const df = deriv(1e8)(f);


Best wishes,
Tomaz

------ Original Message ------
From: "Atharva Khare" <khareatha...@gmail.com <mailto:khareatha...@gmail.com>> To: "Any question about pharo is welcome" <pharo-users@lists.pharo.org <mailto:pharo-users@lists.pharo.org>>
Sent: 15.5.2019 15:26:11
Subject: Re: [Pharo-users] Bloc of code in tiers programming language

Hey,

I think in python, you use Lambda Expressions. Here is how I would do it in python3:
import math
f = lambda x: math.cos(x) + x
d_f = lambda x: (f(x + 1e-8) - f(x)) * 1e8



On Wed, May 15, 2019 at 6:33 PM Hilaire <hila...@drgeo.eu <mailto:hila...@drgeo.eu>> wrote:

    Hi,

    We, Smalltalkers, use bloc of code as easily as we breathe air.

    I am writing an article on Smalltalk programming for a French
    mathematics teachers magazine.

    To illustrate the simplicity of Smalltalk, I would like to compare how
    the bloc of code 'f' and 'df' below will be implemented in Javascript
    and Python:


    f := [ :x | x cos + x ].
    df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8].

    Here f is a way to implement a function and df its derivate.

    Do some of you knows how it will be written in Javascript and Python
    with their own ad-hoc anonymous function?

    Thanks

    Hilaire

-- Dr. Geo
    http://drgeo.eu





Reply via email to