Back to index

texmacs  1.0.7.15
test_math.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : test_math.cpp
00004 * DESCRIPTION: Test mathematical functions
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 //#define ENABLE_TESTS
00013 #ifdef ENABLE_TESTS
00014 #include "math_tree.hpp"
00015 #include "vector.hpp"
00016 #include "matrix.hpp"
00017 #include "polynomial.hpp"
00018 #include "ball.hpp"
00019 #include "function.hpp"
00020 #include "function_extra.hpp"
00021 
00022 void
00023 test_math () {
00024   {
00025     tree t= add ("x", mul (pow ("y", "2"), "z"));
00026     cout << "t\t= " << as_math_string (t) << "\n";
00027     cout << "t*t\t= " << as_math_string (mul (t, t)) << "\n";
00028     cout << "[t,t]\t= " << vector<tree> (t, t) << "\n";
00029     cout << "\n";
00030   }
00031 
00032   {
00033     vector<double> v (1.0, 2.0, 3.0);
00034     cout << "v\t= " << v << "\n";
00035     cout << "exp v\t= " << exp (v) << "\n";
00036     cout << "|v|\t= " << norm (v) << "\n";
00037     cout << "\n";
00038   }
00039 
00040   {
00041     vector<double> v (1.0, 2.0);
00042     matrix<double> m (1.0, 2, 2);
00043     m (0, 1)= 4;
00044     cout << "m\t= " << m << "\n";
00045     cout << "v\t= " << v << "\n";
00046     cout << "m*m\t= " << m*m << "\n";
00047     cout << "m*v\t= " << m*v << "\n";
00048     cout << "\n";
00049   }
00050 
00051   {
00052     polynomial<double> p (1.0, 2.0, 3.0);
00053     polynomial<double> q= p*p;
00054     cout << "p\t= " << p << "\n";
00055     cout << "q\t= " << q << "\n";
00056     cout << "p-p\t= " << p-p << "\n";
00057     cout << "p*p\t= " << p*p << "\n";
00058     cout << "p*p+p\t= " << p*p + p << "\n";
00059     cout << "p*p*p\t= " << p*p*p << "\n";
00060     cout << "p(3)\t= " << p (3.0) << "\n";
00061     cout << "q(3)\t= " << q (3.0, 0) << "\n";
00062     cout << "q'(3)\t= " << q (3.0, 1) << "\n";
00063     cout << "q''(3)\t= " << q (3.0, 2) << "\n";
00064     cout << "\n";
00065   }
00066 
00067   {
00068     ball<double> b (1.0, 0.1);
00069     cout << "b\t= " << b << "\n";
00070     cout << "b+b\t= " << b+b << "\n";
00071     cout << "exp b\t= " << exp (b) << "\n";
00072     cout << "\n";
00073   }
00074 
00075   {
00076     ball<double> b (1.0, 0.1);
00077     function<double,double> x= coordinate_function<double,double> (0);
00078     function<double,double> f= pow (x, x);
00079     vector<function<double,double> > v (x, exp(x));
00080     function<double,double> g= pw_function (v, false);
00081     polynomial<double> P (3.0, 2.0, 5.0);
00082     function<double,double> p= polynomial_function (P);
00083     cout << "x\t= " << x << "\n";
00084     cout << "x+x\t= " << x+x << "\n";
00085     cout << "exp x\t= " << exp (x) << "\n";
00086     cout << "f(x)\t= " << f << "\n";
00087     cout << "f'(x)\t= " << derive (f, 0) << "\n";
00088     cout << "f''(x)\t= " << derive (derive (f, 0), 0) << "\n";
00089     cout << "f(2)\t= " << f (2) << "\n";
00090     cout << "f'(2)\t= " << derive (f, 0) (2) << "\n";
00091     cout << "b\t= " << b << "\n";
00092     cout << "f(b)\t= " << f (b) << "\n";
00093     cout << "g\t= " << g << "\n";
00094     cout << "g(0.2)\t= " << g(0.2) << "\n";
00095     cout << "g(0.8)\t= " << g(0.8) << "\n";
00096     cout << "g'\t= " << derive (g, 0) << "\n";
00097     cout << "g''\t= " << derive (derive (g, 0), 0) << "\n";
00098     cout << "g'''\t= " << derive (derive (derive (g, 0), 0), 0) << "\n";
00099     cout << "fn v\t= " << vector_function (v) << "\n";
00100     cout << "fn v(0)\t= " << vector_function (v) (0.0) << "\n";
00101     cout << "p\t= " << p << "\n";
00102     cout << "p(1)\t= " << p(1) << "\n";
00103     cout << "p(b)\t= " << p(b) << "\n";
00104     cout << "p'\t= " << derive (p, 0) << "\n";
00105     cout << "\n";
00106   }
00107 
00108   {
00109     vector<double> v (1.0, 2.0);
00110     ball<vector<double> > b (v, 0.01);
00111     function<vector<double>,double> x1=
00112       coordinate_function<vector<double>,double> (0);
00113     function<vector<double>,double> x2=
00114       coordinate_function<vector<double>,double> (1);
00115     function<vector<double>,double> f= exp (sin (x1) * x2);
00116     cout << "x1\t= " << x1 << "\n";
00117     cout << "x2\t= " << x2 << "\n";
00118     cout << "x1+x2\t= " << x1+x2 << "\n";
00119     cout << "f\t= " << f << "\n";
00120     cout << "f_1\t= " << derive (f, 0) << "\n";
00121     cout << "f_12\t= " << derive (derive (f, 0), 1) << "\n";
00122     cout << "b\t= " << b << "\n";
00123     cout << "f(b)\t= " << f (b) << "\n";
00124     cout << "\n";
00125   }
00126 
00127   {
00128     ball<double> b (1.0, 0.1);
00129     vector<double> vt (1.0, 0.0);
00130     vector<double> vu (0.0, 1.0);
00131     function<double,vector<double> > t=
00132       coordinate_function<double,vector<double> > (0, vt);
00133     function<double,vector<double> > u=
00134       coordinate_function<double,vector<double> > (0, vu);
00135     function<double,vector<double> > f= exp (t * u) + t;
00136     cout << "t\t= " << t << "\n";
00137     cout << "u\t= " << u << "\n";
00138     cout << "t+u\t= " << t+u << "\n";
00139     cout << "f\t= " << f << "\n";
00140     cout << "b\t= " << b << "\n";
00141     cout << "f(b)\t= " << f (b) << "\n";
00142     cout << "\n";
00143   }
00144 }
00145 
00146 #endif // defined ENABLE_TESTS