[100] 042 FAQ - msharpmath, The Simple is the Best

advertisement
[100] 042
[100] 042
FAQ,
Tutorial by www.msharpmath.com
revised on 2012.11.13
cemmath
The Simple is the Best
FAQ
//
//
//
//
//
//
(Q) What is unique in Cemmath ?
(Q) Between C-language and Cemmath ?
(Q) Between C++ and Cemmath ?
(Q) Between MATLAB and Cemmath ?
(Q) Between MATHEMATICA and Cemmath ?
(Q) Pitfalls in Cemmath ?
//-------------------------------------------------------------------------------------// (Q) What is unique in msharpmath ?
//-------------------------------------------------------------------------------------(A) Several unique innovations are implemented in msharpmath.
■ various data types (matrix, poly, vertex, csys, complex, double, signal)
■ Umbrella grammar
■ dot function
■ tuple function
■ all-in-one file handling
Among the above, the most impressive one would be the 'Umbrella' which
realizes 'expression-look-alike' treatment of equations. For example,
#> solve .x ( exp(x) = 5 );
#> solve .a.b ( a+b = 3, a*b = 2 );
#>
#>
#>
#>
// single nonlinear equation
// coupled nonlinear equations
plot .x( 0,pi ) ( |x|*sin(x) );
// plot 2d curve
int .x( 1,2 ) .y(x,2*x+1) ( exp(-x-y)*sin(x+y) ); // double integration
ode .t( 0,1 ) ( y' = t*y+1, y = 1 ).plot; // Ordinary Differential Equation
plot .r[21](0,1).t[61](0,6*pi) ( (r^(1/3)+t!/3).cyl .y ).cyl;
are almost similar to their relevant mathematical problems.
1
[100] 042
FAQ,
Tutorial by www.msharpmath.com
Another uniqueness could be the use of dot function. Most of built-in
functions start with a dot (.) and thus can be distinguished from user-defined
variables.
// 'I' for users' name space,
// while '.I' for name space of built-in functions
#> I = .I(2,3) ;
ans =
[
1
0
0 ]
[
0
1
0 ]
Also, it is possible to denote "expression-look-alike" functions such as the
Bessel functions by dot functions
.J_nu(x)
.I_nu(x)
.K_n+1/2 (x) .Y_n(x)
The concept of 'tuple' is also useful to handle different types of data. For
example,
#>
a
b
c
(a,b,c) = ( 3 +2!, <1,2,3>,
= 3 + 2!
= <
1
2
= [
1
2
3 ]
[1,2,3] );
3 >
As can be seen above, Cemmath employs a variety of data types including
matrix, polynomial, double, complex, vertex, coordinate system, signal.
Richness in data type however sacrifices the convenience in reference to data.
For example, sqrt(-1) is NaN (not a number) in Cemmath, and thus sqrt(-1+0!)
should be used instead.
From the standpoint of user-file management, no path is necessary to find a file.
All functions can be included in one file and compiled for run.
//-------------------------------------------------------------------------------------// (Q) Between C-language and Cemmath ?
//-------------------------------------------------------------------------------------(A) In conclusion, Cemmath can be regarded as a mini-C. Some keywords of C
are implemented in Cemmath.
2
[100] 042
FAQ,
■ if
else
do
for
■ switch
case
default
Tutorial by www.msharpmath.com
while
break
continue
return
void
double
goto
Cemmath does not use a pointer except for array pointer. Although array pointer
is used in Cemmath, no operation is allowed except subscript. As a mini-C,
Cemmath does not have so many functions in C. Since the primary objective is
'mathematics', math-related functions are of importance in Cemmath.
Nevertheless, some points are helpful in use. For example,
#>
#>
#>
#>
double f(x)= |x|*sin(x);
f++( [1,2,3] );
f ' (3) ; f '' (3);
double area(a, b=a) = a*b;
#> s=0; for[10] s += 10;
//
//
//
//
in Cemmath
function upgrade for matrix
extention to differentiation
dynamic default assignment
// simple iteration, no index
are implemented for easy use.
//-------------------------------------------------------------------------------------// (Q) Between C++ and Cemmath ?
//-------------------------------------------------------------------------------------(A) Absolutely, Cemmath cannot be compared with C++ in power and
efficiency. Remember that Cemmath is just an easy-to-use interpreter not a
powerful compiler. The size of Cemmath is only a few MB !
However, the concept of objective-oriented is somehow realized in Cemmath by
member function. For example,
#> A.inv ; A.row(i) ;
#> z.x ; z.r ;
// inverse and i-th row vector of a matrix A
// elements of complex number
are of major use in Cemmath. All the data types are of built-in nature, and no
new class can be defined.
//-------------------------------------------------------------------------------------// (Q) Between MATLAB and Cemmath ?
//-------------------------------------------------------------------------------------3
[100] 042
FAQ,
Tutorial by www.msharpmath.com
(A) The structure of matrix and data type are of major difference. At present,
please do not mention the level of capabilities yet. However, Cemmath utilizes a
variety of data such as polynomial, vertex including matrix. Richness in data
type helps users work in 'expression-look-alike' manner. For example, a
polynomial can be evaluated, differentiated, sumed or differenced just by
#>
#>
#>
#>
p = [1,0,0,0].poly ;
p(2+3!);
p' ;
p~ ;
#> p` ;
#> p.~;
//
//
//
//
p(x) = x^3
p(z), z = 2+3!
p'(x) = 3x^2, differentiation
int p(x) dx = x^4/4, integration
// p(x)-p(x-1) = 3x^2 -3x +1,
// sum k^3 = (1/4)n^2(n+1)^2,
finite difference
series sum
all of which are treated by functions in MATLAB such as polyval(p,z).
In Cemmath, data type must be explicitly or implicitly defined and cannot be
changed during run until cleared. In this regard, 'double' and 'complex' is
different. This means that sqrt(-1) = NaN in Cemmath but sqrt(-1) = i in
MATLAB. Since -1 is 'double', sqrt(-1) is treated as a 'double' data with no
value. The correct usage in Cemmath is sqrt( -1 + 0! ) where ! is employed to
denote a pure imaginary number rather than a factorial in mathematics.
Matrix dimension can be modified by some operations, but it cannot be changed
by 'subscript' operation in Cemmath. Thus, when a variable A is not a priori
defined,
#> A(10) = 3;
line 1 : error #10970: undefined 'A'
; ??? A(10)=3
// no pre-defined 'A' in Cemmath, while MATLAB creates a matrix A
Also, for a matrix A=[1,2], A = 0 becomes [0,0] in M# but '0' in MATLAB.
The main cause of different treatment comes from the fact that Cemmath strives
to be consistent with C-language. A unique advantage of using Cemmath is the
approach of 'expression-look-alike' as can be seen in 'Umbrella' grammar.
4
[100] 042
FAQ,
Tutorial by www.msharpmath.com
From the aspect of function definition, *.msm file of Cemmath includes
multiple functions in one file. But *.m file of MATLAB defines one function
for each file.
//-------------------------------------------------------------------------------------// (Q) Between MATHEMATICA and Cemmath ?
//-------------------------------------------------------------------------------------(A) Cemmath does not provide 'symbolic operation'. Of course, grammars are
very different between the two.
//-------------------------------------------------------------------------------------// (Q) Pitfalls in Cemmath ?
//-------------------------------------------------------------------------------------(A) Most frequent errors in using Cemmath arise from confusion between
'double' and 'complex' data type. Since (-1) is 'double' data, sqrt(-1) tries to
return 'double' data. In general, the function 'sqrt' returns the same date type
with the input. This means that
■ sqrt(double)=double,
sqrt(-1) = NaN
// not a number
■ sqrt(complex)=complex,
sqrt(-1+0!) = 1!
// complex number
Distinction between 'double' and 'complex' may seem redundant, but the main
reason inherits from our desire to keep other valuable data type such as
polynomial, vertex, csys (coordinate system). Otherwise, we have stick to
matrix only, and treat all other data by matrices.
Similar to the above, subscript for a matrix can be misused.
■ A(i,j) for real part
■ A{i,j} for imaginary part
■ A[i,j] for complex element (real is extended)
This could be the most annoying feature of Cemmath. By sacrificing this
instead, we are successful in handling various data types including polynomials
and vertices.
As for the Umbrella innovated in Cemmath, care should be taken for nesting of
Umbrella. Nested Umbrella causes a serous error at present. For example,
5
[100] 042
FAQ,
Tutorial by www.msharpmath.com
#> double f(x) = int.t(0,x) ( t );
#> double g(x) = int.t(0,x) ( f(t) );
// f(x) includes Umbrella
leads to the most dangerous pitfall in Cemmath at this stage.
6
Download