220 11601 <lon1rr$nrk$1@ger.gmane.org> article
Path: news.gmane.org!not-for-mail
From: Jim Porter <jvp4846@g.rit.edu>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Proposal. Generators of numeric sequences: the
 steps class template
Date: Sat, 28 Jun 2014 13:34:21 -0500
Lines: 51
Approved: news@gmane.org
Message-ID: <lon1rr$nrk$1@ger.gmane.org>
References: <8aec4acb-2eea-4288-b215-81e828cd0b42@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
X-Trace: ger.gmane.org 1403982918 21424 80.91.229.3 (28 Jun 2014 19:15:18 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sat, 28 Jun 2014 19:15:18 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDYZZG6PQYFRBPFIXSOQKGQEGIOANWI@isocpp.org Sat Jun 28 21:15:12 2014
Return-path: <std-proposals+bncBDYZZG6PQYFRBPFIXSOQKGQEGIOANWI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wi0-f199.google.com ([209.85.212.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDYZZG6PQYFRBPFIXSOQKGQEGIOANWI@isocpp.org>)
	id 1X0y5g-0005Xv-H1
	for gclcip-std-proposals@m.gmane.org; Sat, 28 Jun 2014 21:15:12 +0200
Original-Received: by mail-wi0-f199.google.com with SMTP id n15sf2962142wiw.6
        for <gclcip-std-proposals@m.gmane.org>; Sat, 28 Jun 2014 12:15:12 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:to:from:subject:date:lines:message-id:references
         :mime-version:user-agent:in-reply-to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=IXo6iK3Bwor7cB4iux/zu68iP8vYZ4j8dLHvtm213DQ=;
        b=QmXGPL2B0qjROphFrVl8QGuYFvslwjT8/n+lUBDWH28tn2UL6l9FyCI2fH4x1KDS8r
         frb/RQ6P0qHZA/DaKMSfXfHnOWuRUSfh34hoXdbICoZCdKnwZm4XoAYYQcpQBR5yYvEQ
         0ZEM+cIJ0iQFWgnkq4jwvt84n0bNwNf/IUKnCd58DNqakUVuhgfecUerJv26JKprIhNV
         pXtzSM1byke+ULgd9PVPfh48m32YIZLVwEqRfRDo3feBpNsNgfKvMeQ0VuiNyIdRaiiG
         Cohl1fALRsffyaIDYh5gnsNKL+Kx9oXy7JisGul6cWoSp+bMoipNsi+LZU7ggvofljg5
         7Kyw==
X-Gm-Message-State: ALoCoQmJI2Qp9d/W9IvW54KMXgdIcktq0385sRPfO9dz1mfyJFcpnYN1vjeMcl39Td8LZFwxzvAX
X-Received: by 10.112.163.195 with SMTP id yk3mr3177lbb.20.1403982912179;
        Sat, 28 Jun 2014 12:15:12 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.152.36.133 with SMTP id q5ls193484laj.12.gmail; Sat, 28 Jun
 2014 12:15:07 -0700 (PDT)
X-Received: by 10.112.210.9 with SMTP id mq9mr21338671lbc.47.1403982907704;
        Sat, 28 Jun 2014 12:15:07 -0700 (PDT)
Original-Received: from plane.gmane.org (plane.gmane.org. [80.91.229.3])
        by mx.google.com with ESMTPS id be5si26076597lbc.32.2014.06.28.12.15.07
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=RC4-SHA bits=128/128);
        Sat, 28 Jun 2014 12:15:07 -0700 (PDT)
Received-SPF: pass (google.com: domain of gclcip-std-proposals@m.gmane.org designates 80.91.229.3 as permitted sender) client-ip=80.91.229.3;
Original-Received: from list by plane.gmane.org with local (Exim 4.69)
	(envelope-from <gclcip-std-proposals@m.gmane.org>)
	id 1X0y5X-0005T9-KM
	for std-proposals@isocpp.org; Sat, 28 Jun 2014 21:15:03 +0200
Original-Received: from 71-87-112-150.dhcp.mdsn.wi.charter.com ([71.87.112.150])
        by main.gmane.org with esmtp (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <std-proposals@isocpp.org>; Sat, 28 Jun 2014 21:15:03 +0200
Original-Received: from jvp4846 by 71-87-112-150.dhcp.mdsn.wi.charter.com with local (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <std-proposals@isocpp.org>; Sat, 28 Jun 2014 21:15:03 +0200
X-Injected-Via-Gmane: http://gmane.org/
Original-Lines: 42
Original-X-Complaints-To: usenet@ger.gmane.org
X-Gmane-NNTP-Posting-Host: 71-87-112-150.dhcp.mdsn.wi.charter.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To: <8aec4acb-2eea-4288-b215-81e828cd0b42@isocpp.org>
X-Original-Sender: jvp4846@g.rit.edu
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of gclcip-std-proposals@m.gmane.org designates 80.91.229.3 as
 permitted sender) smtp.mail=gclcip-std-proposals@m.gmane.org
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:11601
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/11601>

On 6/28/2014 10:50 AM, Mikhail Semenov wrote:
> https://dl.dropboxusercontent.com/u/35715999/steps_template.htm

In this, you describe ways to use std::steps with floating point values, 
which requires some special handling to stop the iteration if you're 
within epsilon of the endpoint. However, for the floating point case, 
I'd really recommend using something more like NumPy's linspace, which 
takes, a start, a stop, and a *number of samples*.

Supposing we had a function like template <typename T> std::linspace(T 
start, T stop, size_t num, include_endpoint = true) and a corresponding 
std::make_linspace, this is how you could write your integrate function:

   double integrate(
     std::function<double(double)> f, double a, double b, int N
   ) {
     double h = (b - a) / N;
     double h2 = h * 0.5;
     double sum = 0.0;

     for(auto x : std::make_linspace(a + h2, b - h2, N)) {
       sum += f(x);
     }

     return sum * h;
   }

This allows you to avoid worrying about how to define epsilon so that 
you're guaranteed to get the number of steps you expect. The extra 
complexity for the user is minimal, and makes it clearer that you expect 
N steps. If you cared less about efficiency, you could make the loop 
look even simpler:

     for(auto x : std::make_linspace(a, b, N, false)) {
       sum += f(x + h2);
     }

Even with the epsilon calculation for std::steps, I'd still be worried 
about using it if I expected a particular number of iteration steps, and 
if that number were quite large. std::linspace would have no such trouble.

- Jim

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.
