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"
2#include <iostream>
4using namespace std;
5
6int main()
7{
9 a = 1.0;
10 b = 3.0;
11 cout << "a/b = " << a/b << endl;
12 return 0;
13}
14
15
16
17
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
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
1#include <iostream>
2#include "interval.hpp"
4using namespace std;
5
6int main()
7{
9
10 cout << "Please enter real a: ";
11 cout << RndDown;
12 cin >> a;
13 cout << SetPrecision(7,4);
14
15 cout << a << endl;
16 cout << RndUp;
17 cout << a << endl;
18
19 "0.3" >> b;
20
21
22 cout << SetPrecision(18,15);
23
24 cout << b << endl;
25 cout << RndDown;
26 cout << b << endl;
28 "[1.5, 2]" >> x;
29 cout << x << endl;
30
31 cout << SaveOpt;
32 cout << SetPrecision(10,7);
33
34 cout << x << endl;
35 cout << RestoreOpt;
36 cout << x << endl;
37 return 0;
38}
39
40
41
42
43
44
45
46
47
48
49
Example 3 - Compute all zeros of a function
1
2
3
4
5
6
7#include "nlfzero.hpp"
8#include "stacksz.hpp"
9
11using namespace std;
12
13
14DerivType f ( const DerivType& x )
15{
17}
18
19
20
21
22int main()
23{
28 int NumberOfZeros, i, Error;
29
30 cout << SetPrecision(23,15) << Scientific;
31
32 cout << "Search interval : ";
33 cin >> SearchInterval;
34 cout << "Tolerance (relative): ";
35 cin >> Tolerance;
36 cout << endl;
37
38
39 AllZeros(f,SearchInterval,Tolerance,
Zero,Unique,NumberOfZeros,Error);
40
41 for ( i = 1; i <= NumberOfZeros; i++) {
42 cout <<
Zero[i] << endl;
43 if (Unique[i])
44 cout << "encloses a locally unique zero!" << endl;
45 else
46 cout << "may contain a zero (not verified unique)!" << endl;
47 }
48 cout << endl << NumberOfZeros << " interval enclosure(s)" << endl;
49 if (Error) cout << endl << AllZerosErrMsg(Error) << endl;
50 return 0;
51}
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
1
2
3
4#include <iostream>
5#include "interval.hpp"
6#include "imath.hpp"
7using namespace std;
9
11{
14}
15
17{
19}
20
22{
23 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
24}
25
26int main(void)
27{
29 cout << SetPrecision(20,15);
31 cout << "Starting interval is [2,3]" << endl;
32 if (criter(x))
33 {
34 do {
35 xOld = x;
36 cout << "Actual enclosure is " << x << endl;
37 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
38 } while (x != xOld);
39 cout << "Final enclosure of the zero: " << x << endl;
40 }
41 else
42 cout << "Criterion not satisfied!" << endl;
43 return 0;
44}
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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"
2#include <iostream>
4using namespace std;
5
6int main()
7{
9 a = 1.0;
10 b = 3.0;
11 stagprec = 2;
12 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
13
14
15
16 cout << "a/b = " << a/b << endl;
17 return(0);
18}
19
20
21
22
The Multiple-Precision Data Type l_interval.
Example 6 -
1
2
3
4#include <iostream>
5#include "l_interval.hpp"
6#include "l_imath.hpp"
7#include "l_rmath.hpp"
8using namespace std;
10
12{
15}
16
18{
20}
21
23{
24 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
25}
26
27int main(void)
28{
30 stagprec= 3;
32 cout << "Starting interval is [2,3]" << endl;
33 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
34
35
36
37 if (criter(x))
38 {
39 do {
40 xOld = x;
41 cout <<
"Diameter of actual enclosure: " <<
real(
diam(x)) << endl;
42 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
43 } while (x != xOld);
44 cout << "Final enclosure of the zero: " << x << endl;
45 }
46 else
47 cout << "Criterion not satisfied!" << endl;
48 return 0;
49}
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
The Multiple-Precision Data Type l_real.
cvector diam(const cimatrix_subv &mv) noexcept
Returns the diameter of the matrix.
Example 7 -
1
2
3#include <iostream>
4#include "rvector.hpp"
5using namespace std;
7
9{
11
12 Z[1] = Y[2] * Y[3];
13 Z[2] = -Y[1] * Y[3];
14 Z[3] = -0.522 * Y[1] * Y[2];
15 return Z;
16}
17
19{
20 x = 0.0; h = 0.1;
21 Y[1] = 0.0; Y[2] = 1.0; Y[3] = 1.0;
22}
23
24int main(void)
25{
28
29 Init(x, h, Y);
30 for (int i=1; i<=3; i++) {
31 K1 = h * F(x, Y);
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;
36 x += h;
37 cout << SetPrecision(10,6) << Dec;
38 cout << "Step: " << i << ", "
39 << "x = " << x << endl;
40 cout << "Y = " << endl << Y << endl;
41 }
42 return 0;
43}
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Example 8 -
1
2
3
4
5#include <iostream>
6#include "cmatrix.hpp"
7using namespace std;
9
10int main()
11{
12 int n;
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;
18 accu = 0.0;
19 for (
int i=1; i<=n; i++) accumulate(accu, A[i], B[
Col(i)]);
20
21
22
23
25 cout << SetPrecision(12,6) << RndNext << Dec;
26 cout << "Trace of product matrix: " << result << endl;
27 return 0;
28}
29
30
31
32
33
34
35
36
37
38
39
40
41
The Data Type cdotprecision.
cimatrix_subv Col(cimatrix &m, const int &i) noexcept
Returns one column of the matrix as a vector.