Post on 16-Jan-2016
transcript
Extending C for
Scientific and Numerical Computing
SoftIntegration, Inc.
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Outline of Presentation
• Revision of C90 and C99
• New features for numerical computing in C99
• Extending C99 for numerical and script computing in Ch
• Applications and demos in Ch
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Numerical Extensions in C90 over K&R C
1) Honored parentheses
2) Floating-point operations for floats are performed in float instead of in double data type
3) Defined standard mathematical functions in header file math.h
4) Add header file float.h for characteristics of different floating-point data types
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Guiding Principle for Revision of C99
1) Existing code is important, existing implementations are not
2) C code can be portable
3) C code can be non-portable
4) Avoid "quit changes"
5) A standard is a treaty between implementer and programmer
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Guiding Principle for Revision of C99 (con’d)
6) Keep the spirit of C Trust the programmer Don't prevent the programmer from doing
what needs to be done Keep the language small and simple Provide only one way to do operation Make it fast, even if it is not guaranteed to
be portable ...
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Guiding Principle for Revision of C99 (con’d)
7) Support international programming
8) Codify existing practice to address evident deficiencies Note: this principle was violated in design of complex numbers.
9) Minimize incompatibilities with C90
10)Minimize incompatibilities with C++ Note: C++ IS NOT a superset of C.
11)Maintain conceptual simplicity
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Numerical Extensions in C99 over C90
1) IEEE floating-point arithmetic for real numbers
2) Complex numbers
3) Variable length arrays (VLA)
4) Type generic mathematical functions
5) Integral types with different number of bits including long long for 64 bits
Copyright 20002, SoftIntegration, Inc., All rights reserved.
IEEE Floating-Point Arithmetic for Real Numbers
Special Numbers
0.0, +0.0 positive zero
-0.0 negative zero
Inf infinity
-inf negative infinity
NaN Not-a-Number
Copyright 20002, SoftIntegration, Inc., All rights reserved.
IEEE Floating-Point Arithmetic for Real Numbers
#include <stdio.h>
#include <math.h>
int main() {
printf("1/0.0 = %f\n", 1/0.0);
printf("-1/0.0 = %f\n", -1/0.0);
printf("0.0/0.0 = %f\n", 0.0/0.0);
printf("sqrt(-1.0) = %f\n", sqrt(-1.0));
printf("INFINITY/INFINITY = %f\n",
INFINITY/INFINITY);
}
Output: 1/0.0 = inf -1/0.0 = -inf 0.0/0.0 = nan sqrt(-1.0) = nan INFINITY/INFINITY = nan
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Complex Numbers
#include <stdio.h>
#include <complex.h>
int main() {
double complex z, sz;
z = 1+I*2;
sz = csqrt(z);
printf("sz = %f, %f\n", creal(sz), cimag(sz));
}
Output: sz = 1.272020, 0.786151
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Variable Length Arrays (VLA)
#include <stdio.h>
void func(int n, int m) {
int a[n][m]; // a is a VLA
printf("sizeof(a) = %d\n", sizeof(a));
}
int main() {
func(2, 3);
func(4, 5);
return 0;
}
Output: sizeof(a) = 24 sizeof(a) = 80
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Type Generic Math Functions
#include <stdio.h>#include <tgmath.h>#include <complex.h>int main() { int i=3; float fi, f=3; double d=3; complex z=3; double complex dz=3; fi = sin(i); // fi = sinf(i) f = sin(f); // f = sinf(f) d = sin(d); // d = sin(d) z = sin(z); // d = csinf(z) dz = sin(dz); // dz = csin(z) /* ... */}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Major Problem in C99
Complex model in Informative Annex violated one of guiding principles for revision of C99 ---- “Codify existing practice to address evident deficiencies.”
"complex (in Annex G) is a notable failure."
--- Larry Jones, Editor for C99
Copyright 20002, SoftIntegration, Inc., All rights reserved.
The Complex Model in Informative Annex G in C99
1) With three new types of float imaginary, double imaginary, and long double imaginary
2) Sign of zeros for complex numbers is honored
3) NaN is treated as any number
4) Non-conventional numerical results are invented, dangerous for applications in engineering and science
Copyright 20002, SoftIntegration, Inc., All rights reserved.
One of Problems of Complex Model in Informative Annex G in C99
Example 1: Complex(Inf,NaN) is treated as complex infinity
Example 2: imag(complex(NaN, 42)) is 42, not NaN
Example 3: In complex analysis:
exp(Inf) == Inf
exp(-Inf) == 0
exp(ComplexInf) is undefined
In C99:
exp(-Inf, NaN)==(+-0,+-0)
exp(Inf, NaN)==(+-Inf,NaN)
exp(-Inf, Inf) == (+-0,+-0)
exp(Inf, Inf)==(Inf, NaN) Copyright 20002, SoftIntegration, Inc., All rights reserved.
Recent Trend in Computing
• CPU speed is getting faster and faster
• Developer’s time is more expensive than CPU time
• Heterogeneous computing platforms
• Network and mobile computing
• Scripting for rapid application development instead of compiling
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Ch for Numerical and Script Computing
Note: C++ IS NOT a superset of C.
Ch is a superset of embeddable C interpreter
Ch is C with high-level extensions
Ch == C+
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Standards and Libraries Supported in Ch
C90
Major features in C99
POSIX
X/Motif
OpenGL
ODBC
GTK+
CGI
XML
NI-DAQ
...
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Major Extensions of Ch over C99
1) Numerical features
2) 2D/3D graphical plotting
3) Classes in C++
4) Interactive execution of C statements
5) Cross-platform shell programming
6) Programming without pointers
7) Embeddable C interpreter
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Numerical Extensions of Ch over C99
1) Ch can handle complex numbers in both C99 and C++
2) IEEE floating-point arithmetic for complex numbers
3) Variable length array of assumed shape
4) Adjustable array bounds
5) Computational Arrays
6) Advanced numerical analysis functions
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Complex Numbers in Ch Are Compatible with Both C99 and C++
#include <stdio.h>
#include <complex.h>
int main() {
double complex z1 = 1+I*2; // C99 and Ch
double_complex z2 = double_complex(1, 2); // C++ and Ch
double complex z3 = complex(1, 2); // Ch
double complex sz1, sz2, sz3;
sz1 = csqrt(z1); // C99 and Ch
sz2 = sqrt(z2); // C++ and Ch
sz3 = sqrt(z3); // C++ and Ch
printf("sz1 = %f, %f\n", creal(sz1), cimag(sz1)); // C99 and Ch
printf("sz2 = %f, %f\n", real(sz2), imag(sz2)); // C++ and Ch
printf("sz3 = %f\n", sz3); // Ch
}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Extending IEEE Floating-Point Arithmetic to Complex Domain
Special complex numbers:
complex(0.0, 0.0) ignore sign of zeros
ComplexInf complex infinity
ComplexNaN complex Not-a-Number
Copyright 20002, SoftIntegration, Inc., All rights reserved.
The Riemann Sphere and Extended Finite Complex Plane
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Assumed-Shape Arrays#include <stdio.h>
void func(int a[:][:]) { printf(“a[1][1] = %d\n”, a[1][1]);}
int main() { int a1[2][3] = {1, 2, 3, 4, 5, 6}; int a2[3][4] = {1,2,3,4, 5,6,7,8, 9,10,11,12}; func(a1); func(a2); }
Output:
a[1][1] = 5a[1[1] = 6
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Adjustable Array Bounds
#include <stdio.h>
int main() {
int n=2, m = 5;
int a[1:3][2:4] = {1, 2, 3,
4, 5, 6};
int b[n:m];
b[n] = a[1];
b[n+1] = a[2];
b[m] = a[3];
return 0;
}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Computational Arrays
#include <stdio.h> #include <array.h>
int main() { array double A[2][3] = {1, 2, 3, 4, 5, 6}; array double B[3][2];
printf("A= \n%f \n", A+A); B = 2*transpose(A); printf("B= \n%f \n", B); }
Output:
A= 2.000000 4.000000 6.000000 8.000000 10.000000 12.000000
B= 2.000000 8.000000 4.000000 10.000000 6.000000 12.000000
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Function Returns a Computational Array
Output:
2.000000 4.000000 6.000000
#include <stdio.h>#include <array.h> array double fun(int i)[3] { array double a[3]={1,2,3}; a = a+a; return a; }int main() { array double b[3]; b = fun(3); printf("%f \n", b); return 0;}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
2D/3D Plottingarray double x[36]linspace(x, -3.14, 3.14)plotxy(x, sin(x), “Ch plot”, “xlabel”, “ylabel”)
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Numerical Analysis Functions
• Correlation
• Data interpretation
• Eigenvalues and eigenvectors
• Elementary math functions
• filtering and convolution
• Fourier transforms
• Integration
• Linear equations
Matrix analysis
Matrix functions
Ordinary diff equations
Polynomials
Singular values
Special matrices
Special math functions
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Calculating eigenvalues and eigenvectors
#include <numeric.h>#include <iostream.h>int main() { array double a[3][3] = {8, 2, 1, 2, 7, 3, 1, 3, 6}; array double evalues[3], evectors[3][3]; eigensystem(evalues, evectors, a); cout << “eigenvalues\n” << evalues << endl; cout << “eigenvectors\n” << evectors << endl;}
eigenvalues:11.0881 6.5262 3.3857
eigenvectors:0.5795 0.8035 0.13620.6471 -0.3520 -0.67630.4954 -0.4801 0.7239
Ax = λxA: matrixλ: eigenvaluex: eigenvector
Copyright 20002, SoftIntegration, Inc., All rights reserved.
#include <numeric.h>#include <chplot.h>
#define NVAR 2#define POINTS 300void derives(double t, double y, double dydt[ ]) { double mu = 2; dydt[0] = y[1]; dydt[1] = mu*(1-y[0]*y[0])*y[1]-y[0];}
int main() { double t0 = 1, tf = 30, y0[NVAR] = {1, 0}; double t[POINTS], y[NVAR][POINTS]; string_t title = “solution for the vander Pol equation”, xlabel = “t (seconds)”, ylabel = “y1 and y2”;
odesolve(t, y, derives, t0, tf, y0); plotxy(t, y, title, xlabel, ylabel);}
Solving Ordinary Differential Equation
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Output from Ch Program for ODE Solution
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Interactive Execution of C Statements
> int i, *p, **p2 // i is an integer, p pointer, p2 double pointer
> i=10 // i is assigned value 10
10
> p=&i // p points to address of i
00D847C0
> *p // the memory pointed by p has value 10
10
> p2=&p // p2 points to address of p
00D84D30
> **p2 // **p2 == *p == i
10
>
Copyright 20002, SoftIntegration, Inc., All rights reserved.
C++ Feartures in Ch
1) Classes
2) Member function
3) Private/public data and functions in class
4) The this-> pointer
5) Reference type and pass-by-reference
6) Static member of class/struct/union
7) The new and delete operators
8) The constructor and destructor
9) Polymorphical functions
10) The scope resolution operator ::
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Shell Programming
#!/bin/ch
#include <stdio.h>
int main () {
printf(“Save file names with strings to tmpfile\n");
grep –l * strings > tmpfile
}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Programming Without Pointers
Pass-by-reference for function arguments
String type string_t with internal memory management
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Embeddable C Interpreter
#include <embedch.h>
int main() {
char *argvv[]={"prog.c", NULL};
Ch_Initialize(NULL, NULL); // initialize embedded Ch
Ch_RunScript(NULL, argvv); // run embedded C program
Ch_End(NULL); // clean up
}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Application Examples in Ch
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Ch Sample: Plotting
Copyright 20002, SoftIntegration, Inc., All rights reserved.
GUI in GTK+
Copyright 20002, SoftIntegration, Inc., All rights reserved.
GUI in X/Motif
Copyright 20002, SoftIntegration, Inc., All rights reserved.
GUI in Windows
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Animation in Ch OpenGL
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Real-time Control of a Robot Workcell in Ch
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Ch Program for Control of the Robot Workcell: Declaration
#include <robot.h>
int main() {
array double P1[4][4],P2[4][4], // Puma T-matrix
I1[4][4],I2[4][4], // IBM T-matrix
jold1[6]={0,0,0,0,0,0}, // Puma old joint values
jold2[4] = {0,0,0,0}, // IBM old joint values
p1[6],p2[6],i1[4],i2[4], // Puma & IBM joint value
c1[1]; // conveyer1 joint value
// Puma is configurated as flip, below and left
int robot1conf = FLIP|ABOVE|LEFT;
//I BM is configurated as flip
int robot2conf = FLIP;
// construct the class with real-time running
string_t flag="realtime";
int i;
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Ch Program for Control of the Robot Workcell: Initialization
// setup, initialize and check PMAC DACs output
class CRobot robot1 = CRobot(ROBOT1,flag); // Puma 560
class Crobot robot2 = CRobot(ROBOT2,flag); // IBM 7575
class CRobot conveyer = CRobot(CONVEYER1,flag); // Conveyer
// calibrate Robots
robot1.Calibrate(); // calibrate Puma 560
robot2.Calibrate(); // calibrate IBM 7575
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Ch Program for Control of the Robot Workcell: Position Calculation
// calculate the position of the arms and conveyer P1 = P2 = robot1ZeroPosition; //zero position P1[0][3] += 300; P1[1][3] += 300; P1[2][3] += -600; // Puma wrist position// inverse kinematics robot1.InverseKinematics(P1,jold1,p1,robot1conf); P2[0][3] += 0; P2[1][3] += 300; P2[2][3] += -400; // Puma wrist position// inverse kinematics robot1.InverseKinematics(P2,jold1,p2,robot1conf); I1 = I2 = robot2ZeroPosition; // zero position I1[0][3] += 700; I1[1][3] += 200; I1[2][3] += -60; // IBM wrist position// inverse kinematics robot2.InverseKinematics(I1,jold2,i1,robot2conf); I2[0][3] += 500; I2[1][3] += -200; I2[2][3] += -20; // IBM wrist position// inverse kinematics robot2.InverseKinematics(I2,jold2,i2,robot2conf); c1[0] = -500; // move conveyer1 250 mm
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Ch Program for Control of the Robot Workcell: Assembly Operations
// a loop for assembly operations for(i=0; i<5; i++) { robot1.GripperOpen(); // open Puma 560 gripper robot1.Drive(p1); // move Puma to position P1 robot2.Drive(i1); // move IBM to position I1 conveyer.Drive(c1); // move conveyer1 500mm //waiting Puma,IBM and conveyer to complete motion robot1.MoveWait(); robot2.MoveWait(); conveyer.MoveWait();
robot1.GripperClose(); // PUMA picks up a part at P1 robot2.GripperOpen(); // IBM stacks a part at I1 robot1.Drive(p2); // move Puma to position P2 robot2.Drive(i2); // move IBM to position I2
//waiting Puma,IBM and conveyer to complete motion robot1.MoveWait(); robot2.MoveWait(); conveyer.MoveWait();
robot1.GripperOpen(); // PUMA releases the part at P2 robot2.GripperClose(); // IBM picks up the part at I2 }
Copyright 20002, SoftIntegration, Inc., All rights reserved.
A Ch Program for Control of the Robot Workcell: Finish the task
// shutdown the system
robot1.MoveReady(); // move Puma to ready position
robot2.MoveReady(); // move IBM to ready position
// waiting for Puma,IBM to complete their motion
robot1.MoveWait();
robot2.MoveWait();
exit(0); // exit the CH language environment
}
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Demos in Ch
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Ch vs MATLAB
• MATLAB is proprietary• More toolboxes at present
• Ch is a superset of C with an international standard• Ch has all salient features of MATLAB• Ch is as simple as MATLAB in numerical computing• Ch has a large C/C++ code and user base• Ch has better support for Web programming and
distance learning
Copyright 20002, SoftIntegration, Inc., All rights reserved.
Conclusions• C99 is a major milestone in extending C for
numerical computing• Ch is the most complete C interpreter, with support
of industry standard C libs and classes in C++, for embedded script computing.
• Ch extended C99 for numerical computing. (a) Ch is the simplest solution for numerical
computing and visualization within the framework of C/C++.
(b) Ch is the only computing environment with consistent numerical results under the IEEE floating-point arithmetic in both entire real and complex domains
• C/Ch/C++ for any programming and numerical computing tasks
Copyright 20002, SoftIntegration, Inc., All rights reserved.
http://www.softintegration.com
More Information at
Copyright 20002, SoftIntegration, Inc., All rights reserved.