testing
[gnucap-felix.git] / src / measure_integral.cc
blob10c3a80cdfeceaed70f4403dec70804f5864865e
1 /*$Id: measure_integral.cc,v 1.3 2009-12-16 17:22:07 felix Exp $ -*- C++ -*-
2 * vim:ts=8:sw=2:et
3 * Copyright (C) 2008 Albert Davis
4 * Author: Albert Davis <aldavis@gnu.org>
6 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *------------------------------------------------------------------
24 #include "u_parameter.h"
25 #include "m_wave.h"
26 #include "u_function.h"
27 /*--------------------------------------------------------------------------*/
28 namespace {
29 /*--------------------------------------------------------------------------*/
30 class MEASURE : public WAVE_FUNCTION {
31 PARAMETER<double> before;
32 PARAMETER<double> after;
33 public:
34 MEASURE():
35 WAVE_FUNCTION(),
36 before(BIGBIG),
37 after(-BIGBIG)
39 virtual FUNCTION_BASE* clone()const { return new MEASURE(*this);}
40 string label()const{return "integral";}
41 void expand(CS& Cmd, const CARD_LIST* Scope)
44 unsigned here = Cmd.cursor();
45 Cmd >> probe_name;
46 _w = find_wave(probe_name);
48 if (!_w) {
49 Cmd.reset(here);
50 }else{
53 here = Cmd.cursor();
54 do {
55 ONE_OF
56 || Get(Cmd, "probe", &probe_name)
57 || Get(Cmd, "before", &before)
58 || Get(Cmd, "after", &after)
59 || Get(Cmd, "end", &before)
60 || Get(Cmd, "begin", &after)
62 }while (Cmd.more() && !Cmd.stuck(&here));
64 if (!_w) {
65 _w = find_wave(probe_name);
66 }else{
69 before.e_val(BIGBIG, Scope);
70 after.e_val(-BIGBIG, Scope);
73 fun_t wave_eval()const
75 if (_w) {
76 WAVE::const_iterator begin = lower_bound(_w->begin(), _w->end(), DPAIR(after, -BIGBIG));
77 WAVE::const_iterator end = upper_bound(_w->begin(), _w->end(), DPAIR(before, BIGBIG));
78 WAVE::const_iterator lower = begin;
80 double area = 0;
81 for (WAVE::const_iterator i = begin; ++i < end; ) {
82 area += .5 * (lower->second + i->second) * (i->first - lower->first);
83 lower = i;
85 return to_fun_t(area);
86 }else{
87 throw Exception_No_Match(probe_name);
90 } p4;
91 DISPATCHER<FUNCTION_BASE>::INSTALL d4(&measure_dispatcher, "integrate|integral|area", &p4);
92 /*--------------------------------------------------------------------------*/
94 /*--------------------------------------------------------------------------*/
95 /*--------------------------------------------------------------------------*/
96 // vim:ts=8:sw=2:noet: