On this page we will try to show you, how to use the C-XSC class library with some short and simple examples.
Example 1 - An Introduction
We will start with the following simple program showing you how to use intervals, compute one operation and print out the result:
1 #include "interval.hpp"
11 cout <<
"a/b = " << a/b << endl;
The Scalar Type interval.
The namespace cxsc, providing all functionality of the class library C-XSC.
Let's start investigating the interesting lines. With the line
0 #include "interval.hpp"
you include the functionality of the interval class of C-XSC in your program. After that you have to inform the compiler about the namespace cxsc - where all classes and methods of C-XSC are stored in - to use C-XSC without fully qualified identifiers.
Then you declare your variables and assign adequate values in the following lines.
Finally you want to print out the result for your desired computation.
10 cout <<
"a/b = " << a/b << endl;
So it's just that easy to use C-XSC.
Example 2 - Input / Output
2 #include "interval.hpp"
10 cout <<
"Please enter real a: ";
13 cout << SetPrecision(7,4);
22 cout << SetPrecision(18,15);
32 cout << SetPrecision(10,7);
Example 3 - Compute all zeros of a function
14 DerivType f (
const DerivType& x )
28 int NumberOfZeros, i, Error;
30 cout << SetPrecision(23,15) << Scientific;
32 cout <<
"Search interval : ";
33 cin >> SearchInterval;
34 cout <<
"Tolerance (relative): ";
39 AllZeros(f,SearchInterval,Tolerance,
Zero,Unique,NumberOfZeros,Error);
41 for ( i = 1; i <= NumberOfZeros; i++) {
42 cout <<
Zero[i] << endl;
44 cout <<
"encloses a locally unique zero!" << endl;
46 cout <<
"may contain a zero (not verified unique)!" << endl;
48 cout << endl << NumberOfZeros <<
" interval enclosure(s)" << endl;
49 if (Error) cout << endl << AllZerosErrMsg(Error) << endl;
cinterval power(const cinterval &z, int n) noexcept
Calculates .
cinterval exp(const cinterval &z) noexcept
Calculates .
cinterval sin(const cinterval &z) noexcept
Calculates .
int Zero(ivector &x)
Checks if vector is zero vector.
Example 4 - Interval Newton method
5 #include "interval.hpp"
23 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
29 cout << SetPrecision(20,15);
31 cout <<
"Starting interval is [2,3]" << endl;
36 cout <<
"Actual enclosure is " << x << endl;
37 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
39 cout <<
"Final enclosure of the zero: " << x << endl;
42 cout <<
"Criterion not satisfied!" << endl;
cinterval cos(const cinterval &z) noexcept
Calculates .
cinterval sqrt(const cinterval &z) noexcept
Calculates .
cvector mid(const cimatrix_subv &mv) noexcept
Returns the middle of the matrix.
Example 5 -
1 #include "l_interval.hpp"
12 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
16 cout <<
"a/b = " << a/b << endl;
The Multiple-Precision Data Type l_interval.
Example 6 -
5 #include "l_interval.hpp"
24 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
32 cout <<
"Starting interval is [2,3]" << endl;
33 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
41 cout <<
"Diameter of actual enclosure: " <<
real(
diam(x)) << endl;
42 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
44 cout <<
"Final enclosure of the zero: " << x << endl;
47 cout <<
"Criterion not satisfied!" << endl;
The Multiple-Precision Data Type l_real.
cvector diam(const cimatrix_subv &mv) noexcept
Returns the diameter of the matrix.
Example 7 -
14 Z[3] = -0.522 * Y[1] * Y[2];
21 Y[1] = 0.0; Y[2] = 1.0; Y[3] = 1.0;
30 for (
int i=1; i<=3; i++) {
32 K2 = h * F(x + h / 2, Y + K1 / 2);
33 K3 = h * F(x + h / 2, Y + K2 / 2);
34 K4 = h * F(x + h, Y + K3);
35 Y = Y + (K1 + 2 * K2 + 2 * K3 + K4) / 6;
37 cout << SetPrecision(10,6) << Dec;
38 cout <<
"Step: " << i <<
", "
39 <<
"x = " << x << endl;
40 cout <<
"Y = " << endl << Y << endl;
Example 8 -
13 cout <<
"Please enter the matrix dimension n: "; cin >> n;
16 cout <<
"Please enter the matrix A:" << endl; cin >> A;
17 cout <<
"Please enter the matrix B:" << endl; cin >> B;
19 for (
int i=1; i<=n; i++) accumulate(accu, A[i], B[
Col(i)]);
25 cout << SetPrecision(12,6) << RndNext << Dec;
26 cout <<
"Trace of product matrix: " << result << endl;
The Data Type cdotprecision.
cimatrix_subv Col(cimatrix &m, const int &i) noexcept
Returns one column of the matrix as a vector.