BRL-CAD
vm_test.cpp
Go to the documentation of this file.
1 /* V M _ T E S T . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file vm_test.cpp
21  *
22  * @brief Simple Test cases for Math Virtual Machine
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "pcMathGrammar.h"
29 #include "pcMathVM.h"
30 #include <iostream>
31 #include <cmath>
32 
33 
34 typedef boost::shared_ptr<MathFunction> ct;
35 
36 /* Test functions */
37 
38 double add(double a, double b) { return a+b; }
39 double multiply(double a, double b) { return a*b; }
40 double div(double a, double b) { return a / b; }
41 double avg(double a, double b) { return (a+b)/2; }
42 
43 /* Type definitions for unary and binary function pointers */
44 
45 typedef double (* function2_ptr) (double, double);
46 typedef double (* function1_ptr) (double);
47 
48 /* Binary function maker */
49 boost::shared_ptr<MathFunction> make_function(char const *name, function2_ptr f2_p)
50 {
51  return boost::shared_ptr<MathFunction>(new MathF2<double>(name, f2_p));
52 }
53 
54 
55 /* Unary function maker */
56 boost::shared_ptr<MathFunction> make_function(char const *name, function1_ptr f1_p)
57 {
58  return boost::shared_ptr<MathFunction>(new MathF1<double>(name, f1_p));
59 }
60 
61 
62 /* Arity based function maker
63  boost::shared_ptr<MathFunction> make_function(char const *name, int arity)
64  {
65  return boost::shared_ptr<MathFunction>(new UserFunction(name, arity));
66  }
67 */
68 void findfunction(ct **ap, const char *s, MathVM & vm) {
69  *ap = boost::spirit::classic::find<ct>(vm.functions, s);
70  if (! *ap) {
71  std::cout << "Function not found" << std::endl;
72  } else {
73  std::cout << "Function found " << (**ap)->arity() << std::endl;
74  (**ap)->display();
75  }
76 }
77 
78 
79 void eval()
80 {
81  MathVM vm;
82  ct * a;
83  std::vector<double> args;
84  std::cout << "MathVM evaluation" <<std::endl;
85  vm.functions.add("sin", make_function("sin", &sin))
86  ("sqrt", make_function("sqrt", &sqrt))
87  ("add", make_function("add", &add))
88  ("multiply", make_function("multiply", &multiply))
89  ("divide", make_function("div", &div))
90  ("avg", make_function("avg", &avg));
91  findfunction(&a, "sqrt", vm);
92  if (a) {
93  args.push_back(3);
94  std::cout << (*a)->eval(args) <<std::endl;
95  args.clear();
96  }
97  findfunction(&a, "avg", vm);
98  if (a) {
99  args.push_back(24.3);
100  args.push_back(42.3);
101  std::cout << (*a)->eval(args) <<std::endl;
102  args.clear();
103  }
104 
105  vm.stack.push_back(new ConstantNode(100));
106  vm.stack.push_back(new ConstantNode(2));
107  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "sqrt")));
108  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "add")));
109  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "sqrt")));
110 
111  std::cout << " sqrt(100 + sqrt(2)) = " << evaluate(vm.stack) << std::endl;
112  vm.stack.clear();
113 
114  vm.stack.push_back(new ConstantNode(3.14));
115  vm.stack.push_back(new ConstantNode(2));
116  vm.stack.push_back(new ConstantNode(4));
117  vm.stack.push_back(new ConstantNode(4));
118  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "multiply")));
119  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "sqrt")));
120  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "add")));
121  vm.stack.push_back(new ConstantNode(12));
122  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "divide")));
123  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "multiply")));
124  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "sin")));
125 
126  std::cout << " sin(pi * (2 + sqrt(4*4)) / 12) = " << evaluate(vm.stack) << std::endl;
127  vm.stack.clear();
128 
129  vm.stack.push_back(new ConstantNode(3.14));
130  vm.stack.push_back(new ConstantNode(2));
131  vm.stack.push_back(new sysFunctionNode(*boost::spirit::classic::find(vm.functions, "divide")));
132 
133  std::cout << " pi / 2 = " << evaluate(vm.stack) << std::endl;
134 }
135 
136 
137 int main()
138 {
139  eval();
140  return 0;
141 }
142 
143 
144 /*
145  * Local Variables:
146  * mode: C++
147  * tab-width: 8
148  * indent-tabs-mode: t
149  * c-file-style: "stroustrup"
150  * End:
151  * ex: shiftwidth=4 tabstop=8
152  */
int main()
Definition: vm_test.cpp:137
union tree * evaluate(union tree *tr, const struct rt_tess_tol *ttol, const struct bn_tol *tol)
Definition: bottess.c:409
if lu s
Definition: nmg_mod.c:3860
Definition: clone.c:90
void eval()
Definition: vm_test.cpp:79
Header file for the BRL-CAD common definitions.
double add(double a, double b)
Definition: vm_test.cpp:38
boost::shared_ptr< MathFunction > make_function(char const *name, function2_ptr f2_p)
Definition: vm_test.cpp:49
void findfunction(ct **ap, const char *s, MathVM &vm)
Definition: vm_test.cpp:68
double multiply(double a, double b)
Definition: vm_test.cpp:39
double div(double a, double b)
Definition: vm_test.cpp:40
double(* function1_ptr)(double)
Definition: vm_test.cpp:46
double(* function2_ptr)(double, double)
Definition: vm_test.cpp:45
double avg(double a, double b)
Definition: vm_test.cpp:41
boost::shared_ptr< MathFunction > ct
Definition: vm_test.cpp:34