Section 1-1 Introduction of cemmath


Section 1-3 Compatibilty with C language



Download 316.38 Kb.
Page2/2
Date28.05.2018
Size316.38 Kb.
#50916
1   2

Section 1-3 Compatibilty with C language
Basic Grammar. Most keywords in CEMMATH are shared with the C-language. Therefore, the following keywords in C language
void, char, long, double

if, else


switch, case, default

goto, break, continue, return

for, while, do while
work also in CEMMATH. Nevertheless, there is still a limitation. For example, the keyword ‘case’ in CEMMATH can admit only a constant integer. Of course, such a limitation will be cleared as CEMMATH is updated.
Abbreviation. Although CEMMATH can handle the programs in C language to a considerable degree, it is possible to abbreviate prototypes of a function. For example,
double ftn(double a,double b,double &c,double *d);
can be abbreviated as
double ftn(a,b,&c,*d);
CEMMATH presumes that a preceding declarer applied to the following variables. If no explicit declarer appears as with ‘void’, ‘double’ is by default used as type declarer. This means that
void ftn(a,b,long i,j, char x,y);
in CEMMATH is the same as
void ftn(double a,double b, long i,long j, char x,char y);
Compatibility with C-Language. Consider the following two subroutines developed in C language.
double x_fun(double);

double x_fun(double x) { return exp(x)-sin(x)-2; }


double x_bisect(double (*fun)(double),double,double);

double x_bisect(double (*fun)(double),double xa,double xb ) {

double iter,eps,fa,fb,x,f;
iter = 0; eps = 1.e-7;

fa = fun(xa); if( fabs(fa) < eps ) return xa;

fb = fun(xb); if( fabs(fb) < eps ) return xb;

if( fa*fb > 0 ) { return 0; }

while( ++iter < 200 ) {

x = 0.5*(xa+xb);

f = fun(x);

if( fabs(f) < eps) return x;

if( fa*f > 0 ) { xa = x; fa = f; }

else { xb = x; fb = f; }

}

return 0;



}
It is easy to find that the role of these two subroutines is to find a root by the bisection method in numerical analysis. The grammars with the function pointer, prototypes are all implemented in CEMMATH too. Therefore, the above two subroutines can be compiled in CEMMATH and thus the following command
#> bisect_x3 = x_bisect(x_fun,0,4);
results in
bisect_x3 = 1.0541271
Recurrence Function. The recurrence function in C-language is also available in CEMMATH. The following is an example to compute a factorial of an integer.
double factorial(double n);

double factorial(double n) {

if( n == 0 ) return 1;

return n*factorial(n-1); // recurrence function

}
#> factorial(10);

ans = 3628800


Embedding CEMMATH commands in C language. Since major keywords in C language are provided, CEMMATH can utilize various advantages of C-language. For example,
#> .hold; for.nu(0,19) plot .x(0,20) ( .J_nu(x) ); plot;
result in the following figure

Figure 3 Bessel functions



Note also that the philosophy of “expression-look-alike” is implemented in
.J_nu (x)
which represents the Bessel function

Dot functions in CEMMATH allows the special functions to be expressed as they look as possible.
Upgrading ‘double’ function. Any ‘double’-returning functions with all ‘double’ arguments can be upgraded in CEMMATH to accept matrix by postfixing ‘++’ at the end of function names. An example is
#> double f(x) = x*sin(x); // ';' ends the function definition

#> f++( [1,2] ); // extension to matrix

#> f ' (1); f '' (1); // extension to differentiation

ans =


[ 0.84147 1.8186 ]

ans = 1.3817733

ans = 0.23913538
Note that the ‘double’-returning function is also upgraded to allow differentiation operator.
Dynamic Default Assignment. In C and C++ languages, the default arguments allow only constant initialization, for example
double area(double a, double b = 10);
However, CEMMATH allows dynamic default arguments as long as the variable has appeared in the preceding arguments.
Consider an area of a rectagule which becomes for the case of a retangule with a side . This kind of special degradation can be fulfilled by applying dynamic default assignment
#> double area(a, b=a) = a*b; // semicolon is must-element

#> area(3);

#> area(3,4);

ans = 9


ans = 12

Section 1-4 Auto Tracing of CEMMATH
Auto Tracing. When one writes a number of functions, a hierarchy can be present between functions. In such cases, it would be very useful to track the paths of execution. In CEMMATH, the auto tracing is by default carried out by printing the names of functions in which the commands are executed.
Consider the following four functions
#> void f1(&x1) { x1 = sin(x1);; z1 = 1;; }

#> void f2(&x2) { x2 = cos(x2);; f1(x2);; z2 = 2;; }

#> void f3(&x3) { x3 = exp(x3);; f2(x3);; z3 = 3;; }

#> void f4(&x4) { x4 = log(x4);; f3(x4);; z4 = 4;; }


which have a cascade-like hierarchy, i.e. ‘f4’ calls a subfunction ‘f3’, ‘f3’ calls a subfunction ‘f2’, and so on. Then,
#> y = 3;

#> f4(y);


results in the following auto tracing
y = 3

f4.y = 1.0986123

f4.f3.y = 3

f4.f3.f2.y = -0.9899925

f4.f3.f2.f1.y = -0.83602186

f4.f3.f2.f1.z1 = 1

f4.f3.f2.z2 = 2

f4.f3.z3 = 3

f4.z4 = 4
It is noteworthy that the variable ‘y’ has never been appeared in the contents of functions. Nevertheless, the execution shows the name of ‘y’ in all four functions, since the variable ‘y’ is a refered variable by the prefix ‘&’.
The above discussion can be compared with the following case
#> void g1(x1) { x1 = sin(x1);; z1 = 1;; }

#> void g2(x2) { x2 = cos(x2);; g1(x2);; z2 = 2;; }

#> void g3(x3) { x3 = exp(x3);; g2(x3);; z3 = 3;; }

#> void g4(x4) { x4 = log(x4);; g3(x4);; z4 = 4;; }

#> y=3;

#> g4(y);


y = 3

g4.x4 = 1.0986123

g4.g3.x3 = 3

g4.g3.g2.x2 = -0.9899925

g4.g3.g2.g1.x1 = -0.83602186

g4.g3.g2.g1.z1 = 1

g4.g3.g2.z2 = 2

g4.g3.z3 = 3

g4.z4 = 4
Note that the variable ‘y’ has been copied before it enters a function as an argument.
The capability of auto tracing would help users to follow what is exactly happening in the course of execution. The auto tracing may replace the heavy burden of debugging mode, even if not fully covered.
//----------------------------------------------------------------------------

// end of file



//----------------------------------------------------------------------------



Download 316.38 Kb.

Share with your friends:
1   2




The database is protected by copyright ©ininet.org 2024
send message

    Main page