From 19c413ceb04ffc02a987917b0c94525881243f0a Mon Sep 17 00:00:00 2001 From: Yiheng Wu <36156959+jingkaimori@users.noreply.github.com> Date: Mon, 15 Jan 2024 11:02:34 +0800 Subject: [PATCH] [1_45] clean up for src/Graphics/Mathematics ## Why you open this Pull Request? clean up codebase, remove usage of `ENABLE_TESTS` ## What work have you done in the current Pull Request? remove miscellaneous inactive code inside `src/Graphics/Mathematics`. - [x] remove unused `ball` and `function` class - [x] fix an compilation error - [x] rewrite test with QTest --- src/Graphics/Mathematics/ball.hpp | 169 ----------------- src/Graphics/Mathematics/function.hpp | 194 -------------------- src/Graphics/Mathematics/function_extra.hpp | 163 ---------------- src/Graphics/Mathematics/matrix.hpp | 4 +- src/Graphics/Mathematics/polynomial.hpp | 3 +- src/Graphics/Mathematics/test_math.cpp | 146 --------------- src/Mogan/Code/code.cpp | 16 -- src/Mogan/Command/tm2html.cpp | 16 -- src/Mogan/Draw/draw.cpp | 16 -- src/Mogan/Research/research.cpp | 16 -- tests/Graphics/Mathematics/math_test.cpp | 92 ++++++++++ 11 files changed, 95 insertions(+), 740 deletions(-) delete mode 100644 src/Graphics/Mathematics/ball.hpp delete mode 100644 src/Graphics/Mathematics/function.hpp delete mode 100644 src/Graphics/Mathematics/function_extra.hpp delete mode 100644 src/Graphics/Mathematics/test_math.cpp create mode 100644 tests/Graphics/Mathematics/math_test.cpp diff --git a/src/Graphics/Mathematics/ball.hpp b/src/Graphics/Mathematics/ball.hpp deleted file mode 100644 index 7854d82729..0000000000 --- a/src/Graphics/Mathematics/ball.hpp +++ /dev/null @@ -1,169 +0,0 @@ - -/****************************************************************************** -* MODULE : ball.hpp -* DESCRIPTION: balls with center in C and radius in R (= norm_type (C)) -* COPYRIGHT : (C) 2006 Joris van der Hoeven -******************************************************************************* -* This software falls under the GNU general public license version 3 or later. -* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE -* in the root directory or . -******************************************************************************/ - -#ifndef BALL_H -#define BALL_H -#include "properties.hpp" -#include "operators.hpp" -#define TMPL template -#define BINARY_TMPL template -#define R typename properties::norm_type -#define M typename binary_properties::product_type - -/****************************************************************************** -* The ball class -******************************************************************************/ - -TMPL -class ball { -public: - C cen; - R rad; -public: - inline ball (C c=0, R r=0): cen (c), rad (r) {} -}; - -TMPL inline C center (const ball& b) { return b.cen; } -TMPL inline R radius (const ball& b) { return b.rad; } -TMPL inline R upper (const ball& b) { return norm (b.cen) + b.rad; } -TMPL inline R lower (const ball& b) { return norm (b.cen) - b.rad; } - -TMPL inline tree as_tree (const ball& b) { - return compound ("ball", as_tree (center (b)), as_tree (radius (b))); } -TMPL inline tm_ostream& operator << (tm_ostream& out, const ball& b) { - return out << as_math_string (as_tree (b)); } - -TMPL -class properties > { -public: - typedef ball::scalar_type> scalar_type; - typedef typename properties::norm_type norm_type; - typedef typename properties::index_type index_type; - static inline tree index_name (index_type i) { - return properties::index_name (i); } - static inline scalar_type access (ball b, index_type var) { - return scalar_type (properties::access (center (b), var), radius (b)); } -}; - -BINARY_TMPL -class binary_properties,ball > { -public: - typedef ball product_type; -}; - -/****************************************************************************** -* Basic ball arithmetic -******************************************************************************/ - -TMPL ball -operator - (const ball& b) { - return ball (-center (b), radius (b)); -} - -TMPL ball -operator + (const ball& b1, const ball& b2) { - return ball (center (b1) + center (b2), radius (b1) + radius (b2)); -} - -TMPL ball -operator - (const ball& b1, const ball& b2) { - return ball (center (b1) - center (b2), radius (b1) + radius (b2)); -} - -BINARY_TMPL ball -operator * (const ball& b1, const ball& b2) { - return ball (center (b1) * center (b2), - norm (center (b1)) * radius (b2) + - radius (b1) * norm (center (b2)) + - radius (b1) * radius (b2)); -} - -TMPL ball -invert (const ball& b) { - return ball (invert (center (b)), radius (b) / square (lower (b))); -} - -BINARY_TMPL ball -operator / (const ball& b1, const ball& b2) { - return b1 * invert (b2); -} - -/****************************************************************************** -* Special functions -******************************************************************************/ - -TMPL ball -sqrt (const ball& b) { - return ball (sqrt (center (b)), radius (b) / (2 * square (loer (b)))); -} - -TMPL ball -exp (const ball& b) { - return ball (exp (center (b)), exp (upper (b)) * radius (b)); -} - -TMPL ball -log (const ball& b) { - return ball (log (center (b)), radius (b) / lower (b)); -} - -TMPL inline ball -pow (const ball& b1, const ball& b2) { - return exp (b2 * log (b1)); -} - -TMPL ball -cos (const ball& z) { - if (radius (z) >= R (3.14159)) - return ball (C(0), R(1)); - else { - R u1= sin (lower (z)); - R u2= sin (upper (z)); - if (u1 * u2 <= 0) - return ball (cos (center (z)), radius (z)); - return ball (cos (center (z)), - max (norm (u1), norm (u2)) * radius (z)); - } -} - -TMPL ball -sin (const ball& z) { - if (radius (z) >= R (3.14159)) - return ball (sin (center (z)), radius (z)); - else { - R u1= cos (lower (z)); - R u2= cos (upper (z)); - if (u1 * u2 <= 0) - return ball (sin (center (z)), radius (z)); - return ball (sin (center (z)), - max (norm (u1), norm (u2)) * radius (z)); - } -} - -TMPL inline ball -tan (const ball& b) { - return sin (b) / cos (b); -} - -/****************************************************************************** -* Other routines -******************************************************************************/ - -TMPL ball -norm (const ball& b) { - return ball (norm (center (b)), radius (b)); -} - -#undef TMPL -#undef BINARY_TMPL -#undef R -#undef M -#endif // defined BALL_H diff --git a/src/Graphics/Mathematics/function.hpp b/src/Graphics/Mathematics/function.hpp deleted file mode 100644 index 21cac04eec..0000000000 --- a/src/Graphics/Mathematics/function.hpp +++ /dev/null @@ -1,194 +0,0 @@ - -/****************************************************************************** -* MODULE : function.hpp -* DESCRIPTION: Mathematical functions from F into T -* COPYRIGHT : (C) 2006 Joris van der Hoeven -******************************************************************************* -* This software falls under the GNU general public license version 3 or later. -* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE -* in the root directory or . -******************************************************************************/ - -#ifndef FUNCTION_HPP -#define FUNCTION_HPP -#include "ball.hpp" -#include "vector.hpp" -#include "polynomial.hpp" -#define TMPL template -#define V typename properties::index_type -#define C typename properties::scalar_type - -TMPL class function; -TMPL bool is_nil (function f); - -/****************************************************************************** -* Abstract function class -******************************************************************************/ - -TMPL -class function_rep: public abstract_struct { -public: - inline function_rep () {} - inline virtual ~function_rep () {} - virtual T apply (F x) = 0; - virtual ball apply (ball x) = 0; - virtual function derive (V var) = 0; - virtual tree expression () = 0; -}; - -TMPL -class function { -public: -ABSTRACT_NULL_TEMPLATE_2(function,F,T); - inline function (T x); - inline T operator () (F x); - inline ball operator () (ball x); -}; -ABSTRACT_NULL_TEMPLATE_2_CODE(function,typename,F,typename,T); - -TMPL inline T function::operator () (F x) { - return rep->apply (x); } -TMPL inline ball function::operator () (ball x) { - return rep->apply (x); } -TMPL inline function derive (function f, V var) { - return f->derive (var); } -TMPL inline tree as_tree (function f) { - return f->expression (); } -TMPL inline tm_ostream& operator << (tm_ostream& out, function f) { - return out << as_math_string (as_tree (f)); } - -TMPL -class properties > { -public: - typedef function::scalar_type> scalar_type; - typedef function::norm_type> norm_type; - typedef function::index_type> index_type; -}; - -/****************************************************************************** -* Basic functions -******************************************************************************/ - -TMPL -class constant_function_rep: public function_rep { - T c; -public: - inline constant_function_rep (T c2): c (c2) {} - inline T apply (F x) { return c; } - inline ball apply (ball x) { return ball (c, 0); } - inline function derive (V var) { return C(0) * c; } - inline tree expression () { return as_tree (c); } -}; - -TMPL inline function::function (T c): - rep (tm_new > (c)) { rep->ref_count++; } - -TMPL -class coordinate_function_rep: public function_rep { - V var; - T c; -public: - inline coordinate_function_rep (V var2, T c2): var (var2), c (c2) {} - inline T apply (F x) { return properties::access (x, var) * c; } - inline ball apply (ball x) { - return properties >::access (x, var) * ball (c, 0); } - inline function derive (V var2) { return var2==var? c: C(0) * c; } - inline tree expression () { - return mul (as_tree (c), properties::index_name (var)); } -}; - -TMPL inline function -coordinate_function (V var, T c=1) { - return tm_new > (var, c); } - -/****************************************************************************** -* Operators on functions -******************************************************************************/ - -template -class unary_function_rep: public function_rep { - function f; -public: - inline unary_function_rep (function f2): f (f2) {} - inline T apply (F x) { return Op::op (f (x)); } - inline ball apply (ball x) { return Op::op (f (x)); } - inline function derive (V var) { return Op::diff (f, var); } - inline tree expression () { return Op::op (as_tree (f)); } -}; - -template inline function -unary_function (function f) { - return tm_new > (f); } - -template -class binary_function_rep: public function_rep { - function f, g; -public: - inline binary_function_rep (function f2, function g2): - f (f2), g (g2) {} - inline T apply (F x) { return Op::op (f (x), g (x)); } - inline ball apply (ball x) { return Op::op (f (x), g (x)); } - inline function derive (V var) { return Op::diff (f, g, var); } - inline tree expression () { return Op::op (as_tree (f), as_tree (g)); } -}; - -template inline function -binary_function (function f, function g) { - return tm_new > (f, g); } - -/****************************************************************************** -* Standard functions -******************************************************************************/ - -TMPL inline function -operator - (function f) { - return unary_function (f); } - -TMPL inline function -operator + (function f, function g) { - return binary_function (f, g); } - -TMPL inline function -operator - (function f, function g) { - return binary_function (f, g); } - -TMPL inline function -operator * (function f, function g) { - return binary_function (f, g); } - -TMPL inline function -operator / (function f, function g) { - return binary_function (f, g); } - -TMPL inline function -sqrt (function f) { - return unary_function (f); } - -TMPL inline function -exp (function f) { - return unary_function (f); } - -TMPL inline function -log (function f) { - return unary_function (f); } - -TMPL inline function -pow (function f, function g) { - return binary_function (f, g); } - -TMPL inline function -cos (function f) { - return unary_function (f); } - -TMPL inline function -sin (function f) { - return unary_function (f); } - -TMPL inline function -tan (function f) { - return unary_function (f); } - -#undef TMPL -#undef V -#undef C -#endif // FUNCTION_H diff --git a/src/Graphics/Mathematics/function_extra.hpp b/src/Graphics/Mathematics/function_extra.hpp deleted file mode 100644 index 4d780b25b3..0000000000 --- a/src/Graphics/Mathematics/function_extra.hpp +++ /dev/null @@ -1,163 +0,0 @@ - -/****************************************************************************** -* MODULE : function_extra.hpp -* DESCRIPTION: Extra routines for functions from F into T -* COPYRIGHT : (C) 2006 Joris van der Hoeven -******************************************************************************* -* This software falls under the GNU general public license version 3 or later. -* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE -* in the root directory or . -******************************************************************************/ - -#ifndef FUNCTION_EXTRA_HPP -#define FUNCTION_EXTRA_HPP -#include "function.hpp" -#include "vector.hpp" -#include "polynomial.hpp" -#define TMPL template -#define V typename properties::index_type -#define C typename properties::scalar_type - -#define MATH_INFINITY HUGE_VAL - -/****************************************************************************** -* Piecewise functions -******************************************************************************/ - -TMPL function -pw_function (vector > v, bool jump_flag= false); - -TMPL -class pw_function_rep: public function_rep { - vector > v; - bool jump_flag; -public: - inline pw_function_rep (vector > v2, bool jump_flag2): - v (v2), jump_flag (jump_flag2) {} - int which (F x) { - if (x <= 0) return 0; - if (x >= 1) return N(v)-1; - return (int) floor (N(v) * x); - } - F position (F x) { return N(v) * x - which (x); } - T apply (F x) { return v[which (x)] (position (x)); } - ball apply (ball x) { - int w1= which (lower (x)); - int w2= which (upper (x)); - if (w1 == w2) return v[w1] (ball (N(v)) * x - ball (w1)); - if (jump_flag) return ball (T(0), MATH_INFINITY); - } - function derive (V var) { - function dummy; - vector > w (dummy, N(v)); - for (int i=0; i (N(v)) * v[i]->derive (var); - bool flag= jump_flag; - for (int i=0; i a (n+1); - for (i=0; i -pw_function (vector > v, bool jump_flag) { - return tm_new > (v, jump_flag); -} - -/****************************************************************************** -* Vector functions -******************************************************************************/ - -template ball > -as_ball (vector > v) { - typedef typename properties::norm_type radius_type; - int i, n= N(v); - vector c (T(0), n); - vector r (radius_type(0), n); - for (i=0; i > (c, norm (r)); -} - -TMPL function > vector_function (vector > v); - -TMPL -class vector_function_rep: public function_rep > { - vector > v; -public: - inline vector_function_rep (vector > v2): v (v2) {} - vector apply (F x) { - int i, n= N(v); - T* a= tm_new_array (n); - for (i=0; i (a, n); - } - ball > apply (ball x) { - int i, n= N(v); - ball* a= tm_new_array > (n); - for (i=0; i > (a, n)); - } - function > derive (V var) { - int i, n= N(v); - vector > w; - for (i=0; iderive (var); - return vector_function (w); - } - tree expression () { - return compound ("as_function", as_tree (v)); - } -}; - -TMPL inline function > -vector_function (vector > v) { - return tm_new > (v); -} - -/****************************************************************************** -* Polynomial functions -******************************************************************************/ - -template function polynomial_function (polynomial p); - -template -class polynomial_function_rep: public function_rep { - polynomial p; -public: - inline polynomial_function_rep (polynomial p2): p (p2) {} - inline T apply (T x) { return p (x); } - ball apply (ball x) { - int i, n= N(p); - if (n == 0) return 0; - T* a= A(p); - ball sum= a[n-1]; - for (i=n-2; i>=0; i--) - sum = x * sum + ball (a[i]); - return sum; } - function derive (typename properties::index_type var) { - return polynomial_function (::derive (p)); } - tree expression () { - return compound ("as_function", as_tree (p)); } -}; - -template inline function -polynomial_function (polynomial p) { - return tm_new > (p); -} - -#undef TMPL -#undef V -#undef C -#endif // FUNCTION_EXTRA_H diff --git a/src/Graphics/Mathematics/matrix.hpp b/src/Graphics/Mathematics/matrix.hpp index 00e2e00cfa..be79829dc9 100644 --- a/src/Graphics/Mathematics/matrix.hpp +++ b/src/Graphics/Mathematics/matrix.hpp @@ -198,9 +198,7 @@ TMPL inline vector operator * (matrix m, vector v) { int i, j, rows= NR (m), cols= NC (m); ASSERT (N (v) == cols, "dimensions don't match"); - vector prod (rows); - for (j=0; j prod (T(0), rows); for (i=0; i p) { int i, n= N(p); if (n == 0) return "0"; tree sum= as_tree (p[0]); + tree var("x"); for (i=1; i. -******************************************************************************/ - -//#define ENABLE_TESTS -#ifdef ENABLE_TESTS -#include "math_tree.hpp" -#include "vector.hpp" -#include "matrix.hpp" -#include "polynomial.hpp" -#include "ball.hpp" -#include "function.hpp" -#include "function_extra.hpp" - -void -test_math () { - { - tree t= add ("x", mul (pow ("y", "2"), "z")); - cout << "t\t= " << as_math_string (t) << "\n"; - cout << "t*t\t= " << as_math_string (mul (t, t)) << "\n"; - cout << "[t,t]\t= " << vector (t, t) << "\n"; - cout << "\n"; - } - - { - vector v (1.0, 2.0, 3.0); - cout << "v\t= " << v << "\n"; - cout << "exp v\t= " << exp (v) << "\n"; - cout << "|v|\t= " << norm (v) << "\n"; - cout << "\n"; - } - - { - vector v (1.0, 2.0); - matrix m (1.0, 2, 2); - m (0, 1)= 4; - cout << "m\t= " << m << "\n"; - cout << "v\t= " << v << "\n"; - cout << "m*m\t= " << m*m << "\n"; - cout << "m*v\t= " << m*v << "\n"; - cout << "\n"; - } - - { - polynomial p (1.0, 2.0, 3.0); - polynomial q= p*p; - cout << "p\t= " << p << "\n"; - cout << "q\t= " << q << "\n"; - cout << "p-p\t= " << p-p << "\n"; - cout << "p*p\t= " << p*p << "\n"; - cout << "p*p+p\t= " << p*p + p << "\n"; - cout << "p*p*p\t= " << p*p*p << "\n"; - cout << "p(3)\t= " << p (3.0) << "\n"; - cout << "q(3)\t= " << q (3.0, 0) << "\n"; - cout << "q'(3)\t= " << q (3.0, 1) << "\n"; - cout << "q''(3)\t= " << q (3.0, 2) << "\n"; - cout << "\n"; - } - - { - ball b (1.0, 0.1); - cout << "b\t= " << b << "\n"; - cout << "b+b\t= " << b+b << "\n"; - cout << "exp b\t= " << exp (b) << "\n"; - cout << "\n"; - } - - { - ball b (1.0, 0.1); - function x= coordinate_function (0); - function f= pow (x, x); - vector > v (x, exp(x)); - function g= pw_function (v, false); - polynomial P (3.0, 2.0, 5.0); - function p= polynomial_function (P); - cout << "x\t= " << x << "\n"; - cout << "x+x\t= " << x+x << "\n"; - cout << "exp x\t= " << exp (x) << "\n"; - cout << "f(x)\t= " << f << "\n"; - cout << "f'(x)\t= " << derive (f, 0) << "\n"; - cout << "f''(x)\t= " << derive (derive (f, 0), 0) << "\n"; - cout << "f(2)\t= " << f (2) << "\n"; - cout << "f'(2)\t= " << derive (f, 0) (2) << "\n"; - cout << "b\t= " << b << "\n"; - cout << "f(b)\t= " << f (b) << "\n"; - cout << "g\t= " << g << "\n"; - cout << "g(0.2)\t= " << g(0.2) << "\n"; - cout << "g(0.8)\t= " << g(0.8) << "\n"; - cout << "g'\t= " << derive (g, 0) << "\n"; - cout << "g''\t= " << derive (derive (g, 0), 0) << "\n"; - cout << "g'''\t= " << derive (derive (derive (g, 0), 0), 0) << "\n"; - cout << "fn v\t= " << vector_function (v) << "\n"; - cout << "fn v(0)\t= " << vector_function (v) (0.0) << "\n"; - cout << "p\t= " << p << "\n"; - cout << "p(1)\t= " << p(1) << "\n"; - cout << "p(b)\t= " << p(b) << "\n"; - cout << "p'\t= " << derive (p, 0) << "\n"; - cout << "\n"; - } - - { - vector v (1.0, 2.0); - ball > b (v, 0.01); - function,double> x1= - coordinate_function,double> (0); - function,double> x2= - coordinate_function,double> (1); - function,double> f= exp (sin (x1) * x2); - cout << "x1\t= " << x1 << "\n"; - cout << "x2\t= " << x2 << "\n"; - cout << "x1+x2\t= " << x1+x2 << "\n"; - cout << "f\t= " << f << "\n"; - cout << "f_1\t= " << derive (f, 0) << "\n"; - cout << "f_12\t= " << derive (derive (f, 0), 1) << "\n"; - cout << "b\t= " << b << "\n"; - cout << "f(b)\t= " << f (b) << "\n"; - cout << "\n"; - } - - { - ball b (1.0, 0.1); - vector vt (1.0, 0.0); - vector vu (0.0, 1.0); - function > t= - coordinate_function > (0, vt); - function > u= - coordinate_function > (0, vu); - function > f= exp (t * u) + t; - cout << "t\t= " << t << "\n"; - cout << "u\t= " << u << "\n"; - cout << "t+u\t= " << t+u << "\n"; - cout << "f\t= " << f << "\n"; - cout << "b\t= " << b << "\n"; - cout << "f(b)\t= " << f (b) << "\n"; - cout << "\n"; - } -} - -#endif // defined ENABLE_TESTS diff --git a/src/Mogan/Code/code.cpp b/src/Mogan/Code/code.cpp index 2975f40fc4..e5b1522994 100755 --- a/src/Mogan/Code/code.cpp +++ b/src/Mogan/Code/code.cpp @@ -63,19 +63,6 @@ static QTMApplication* qtmapp = NULL; static QTMCoreApplication* qtmcoreapp= NULL; #endif -/****************************************************************************** - * For testing - ******************************************************************************/ - -// #define ENABLE_TESTS -#ifdef ENABLE_TESTS -void -test_routines () { - extern void test_math (); - test_math (); -} -#endif - /****************************************************************************** * Clean exit on segmentation faults ******************************************************************************/ @@ -494,9 +481,6 @@ main (int argc, char** argv) { bench_start ("initialize texmacs"); init_texmacs (); bench_cumul ("initialize texmacs"); -#ifdef ENABLE_TESTS - test_routines (); -#endif // #ifdef EXPERIMENTAL // test_environments (); // #endif diff --git a/src/Mogan/Command/tm2html.cpp b/src/Mogan/Command/tm2html.cpp index 96f8733daa..39dbd60275 100644 --- a/src/Mogan/Command/tm2html.cpp +++ b/src/Mogan/Command/tm2html.cpp @@ -77,19 +77,6 @@ static QTMApplication* qtmapp = NULL; static QTMCoreApplication* qtmcoreapp= NULL; #endif -/****************************************************************************** - * For testing - ******************************************************************************/ - -// #define ENABLE_TESTS -#ifdef ENABLE_TESTS -void -test_routines () { - extern void test_math (); - test_math (); -} -#endif - /****************************************************************************** * Clean exit on segmentation faults ******************************************************************************/ @@ -312,9 +299,6 @@ main (int argc, char** argv) { bench_start ("initialize texmacs"); init_texmacs (); bench_cumul ("initialize texmacs"); -#ifdef ENABLE_TESTS - test_routines (); -#endif start_scheme (argc, argv, TeXmacs_main); #ifdef QTTEXMACS delete qtmcoreapp; diff --git a/src/Mogan/Draw/draw.cpp b/src/Mogan/Draw/draw.cpp index 361b7e8b33..6fdee58c73 100755 --- a/src/Mogan/Draw/draw.cpp +++ b/src/Mogan/Draw/draw.cpp @@ -63,19 +63,6 @@ static QTMApplication* qtmapp = NULL; static QTMCoreApplication* qtmcoreapp= NULL; #endif -/****************************************************************************** - * For testing - ******************************************************************************/ - -// #define ENABLE_TESTS -#ifdef ENABLE_TESTS -void -test_routines () { - extern void test_math (); - test_math (); -} -#endif - /****************************************************************************** * Clean exit on segmentation faults ******************************************************************************/ @@ -494,9 +481,6 @@ main (int argc, char** argv) { bench_start ("initialize texmacs"); init_texmacs (); bench_cumul ("initialize texmacs"); -#ifdef ENABLE_TESTS - test_routines (); -#endif // #ifdef EXPERIMENTAL // test_environments (); // #endif diff --git a/src/Mogan/Research/research.cpp b/src/Mogan/Research/research.cpp index a2aa4d9dcb..36e15e22ed 100644 --- a/src/Mogan/Research/research.cpp +++ b/src/Mogan/Research/research.cpp @@ -76,19 +76,6 @@ static QTMApplication* qtmapp = NULL; static QTMCoreApplication* qtmcoreapp= NULL; #endif -/****************************************************************************** - * For testing - ******************************************************************************/ - -// #define ENABLE_TESTS -#ifdef ENABLE_TESTS -void -test_routines () { - extern void test_math (); - test_math (); -} -#endif - /****************************************************************************** * Clean exit on segmentation faults ******************************************************************************/ @@ -583,9 +570,6 @@ main (int argc, char** argv) { bench_start ("initialize texmacs"); init_texmacs (); bench_cumul ("initialize texmacs"); -#ifdef ENABLE_TESTS - test_routines (); -#endif // #ifdef EXPERIMENTAL // test_environments (); // #endif diff --git a/tests/Graphics/Mathematics/math_test.cpp b/tests/Graphics/Mathematics/math_test.cpp new file mode 100644 index 0000000000..3268b8357e --- /dev/null +++ b/tests/Graphics/Mathematics/math_test.cpp @@ -0,0 +1,92 @@ + +/****************************************************************************** + * MODULE : test_math.cpp + * DESCRIPTION: Test mathematical functions + * COPYRIGHT : (C) 2006 Joris van der Hoeven + ******************************************************************************* + * This software falls under the GNU general public license version 3 or later. + * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE + * in the root directory or . + ******************************************************************************/ + +#include "base.hpp" +#include "math_tree.hpp" +#include "matrix.hpp" +#include "polynomial.hpp" +#include "vector.hpp" +#include +Q_DECLARE_METATYPE (tree) + +class TestMath : public QObject { + Q_OBJECT + +private slots: + void test_math_tree (); + void test_vector (); + void test_matrix (); + void test_polynomial (); +}; + +void +TestMath::test_math_tree () { + tree t= add ("x", mul (pow ("y", "2"), "z")); + qcompare (as_math_string (t), "x + y^2 * z"); + qcompare (as_math_string (mul (t, t)), "(x + y^2 * z) * (x + y^2 * z)"); + qcompare (as_math_string (as_tree (vector (t, t))), + "[ x + y^2 * z, x + y^2 * z ]"); +} + +void +TestMath::test_vector () { + vector v (1.0, 2.0, 3.0); + QCOMPARE (as_tree (v), tree (TUPLE, "1", "2", "3")); + QCOMPARE (as_tree (exp (v)), tree (TUPLE, as_tree (exp (1)), + as_tree (exp (2)), as_tree (exp (3)))); + QCOMPARE (norm (v), 3.74165738677394); +} + +void +TestMath::test_matrix () { + vector v (1.0, 2.0); + matrix m (1.0, 2, 2); + m (0, 1)= 4; + QCOMPARE (as_tree (m), + tree (TUPLE, tree (TUPLE, "1", "4"), tree (TUPLE, "0", "1"))); + qcompare (as_math_string (as_tree (m)), "[ [ 1, 4 ], [ 0, 1 ] ]"); + QCOMPARE (as_tree (v), tree (TUPLE, "1", "2")); + QCOMPARE (as_tree (m * m), + tree (TUPLE, tree (TUPLE, "1", "8"), tree (TUPLE, "0", "1"))); + QCOMPARE (as_tree (m * v), tree (TUPLE, "9", "2")); + qcompare (as_math_string (as_tree (m * m)), "[ [ 1, 8 ], [ 0, 1 ] ]"); + qcompare (as_math_string (as_tree (m * v)), "[ 9, 2 ]"); +} + +void +TestMath::test_polynomial () { + polynomial p (1.0, 2.0, 3.0); + polynomial q= p * p; + QCOMPARE (as_tree (q), + tree (PLUS, + tree (PLUS, + tree (PLUS, tree (PLUS, "1", tree (TIMES, "4", "x")), + tree (TIMES, "10", tree (POW, "x", "2"))), + tree (TIMES, "12", tree (POW, "x", "3"))), + tree (TIMES, "9", tree (POW, "x", "4")))); + qcompare (as_math_string (as_tree (p)), "1 + 2 * x + 3 * x^2"); + qcompare (as_math_string (as_tree (q)), + "1 + 4 * x + 10 * x^2 + 12 * x^3 + 9 * x^4"); + qcompare (as_math_string (as_tree (p - p)), "0"); + qcompare (as_math_string (as_tree (p * p)), + "1 + 4 * x + 10 * x^2 + 12 * x^3 + 9 * x^4"); + qcompare (as_math_string (as_tree (p * p + p)), + "2 + 6 * x + 13 * x^2 + 12 * x^3 + 9 * x^4"); + qcompare (as_math_string (as_tree (p * p * p)), + "1 + 6 * x + 21 * x^2 + 44 * x^3 + 63 * x^4 + 54 * x^5 + 27 * x^6"); + QCOMPARE (p (3.0), 34); + QCOMPARE (q (3.0, 0), 1156); + QCOMPARE (q (3.0, 1), 1360); + QCOMPARE (q (3.0, 2), 1208); +} + +QTEST_MAIN (TestMath) +#include "math_test.moc"