Assert

Assert Commit Details

Date:2011-04-13 14:09:14 (7 years 8 months ago)
Author:Michael Baudin
Commit:119
Parents: 118
Message:Removed automatically generated demos.
Changes:
D/demos/assert_checktrue.sce
D/demos/assert_comparecomplex.sce
D/demos/assert_sortcomplex.sce
D/demos/assert_computedigits.sce
D/demos/assert_cond2reqdigits.sce
D/demos/assert_generror.sce
D/demos/assert.dem.gateway.sce
D/demos/assert_checkalmostequal.sce
D/demos/assert_compare.sce
D/demos/assert_csvread.sce
D/demos/assert_cond2reltol.sce
D/demos/assert_checkerror.sce
D/demos/assert_sort.sce
D/demos/assert_checkfalse.sce
D/demos/assert_checkfilesequal.sce
D/demos/assert_accuratedigits.sce
D/demos/assert_checkequal.sce
D/demos/assert_csvwrite.sce

File differences

demos/assert_generror.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// This help file was automatically generated from assert_generror.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_generror.sci
//
// Both these calls generate an error
assert_generror ( "oups" );
assert_generror ( "oups" , 123456789 );
halt() // Press return to continue
// The following call generates an error.
assert_checktrue ( [%t %f] );
//
// Define our own error handler
function myerror ( varargin )
[lhs,rhs]=argn()
errormsg = varargin(1)
if ( rhs == 1 ) then
mprintf( "myerror: %s\n", errormsg )
else
errornb = varargin(2)
mprintf( "myerror: %s (%d)\n", errormsg , errornb )
end
endfunction
//
// Configure the new error handler
back=funcprot();
funcprot(0);
olderrorfunction = assert_generror;
assert_generror = myerror;
funcprot(back);
//
// Check that the new error handler is in place
assert_checktrue ( [%t %f] );
//
// Put back the regular error handler in place
back=funcprot();
funcprot(0);
assert_generror = olderrorfunction;
funcprot(back);
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_generror.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert.dem.gateway.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// This help file was automatically generated using helpupdate
// PLEASE DO NOT EDIT
demopath = get_absolute_file_path("assert.dem.gateway.sce");
subdemolist = [
"assert_sortcomplex", "assert_sortcomplex.sce"; ..
"assert_sort", "assert_sort.sce"; ..
"assert_generror", "assert_generror.sce"; ..
"assert_csvwrite", "assert_csvwrite.sce"; ..
"assert_csvread", "assert_csvread.sce"; ..
"assert_cond2reqdigits", "assert_cond2reqdigits.sce"; ..
"assert_cond2reltol", "assert_cond2reltol.sce"; ..
"assert_computedigits", "assert_computedigits.sce"; ..
"assert_comparecomplex", "assert_comparecomplex.sce"; ..
"assert_compare", "assert_compare.sce"; ..
"assert_checktrue", "assert_checktrue.sce"; ..
"assert_checkfilesequal", "assert_checkfilesequal.sce"; ..
"assert_checkfalse", "assert_checkfalse.sce"; ..
"assert_checkerror", "assert_checkerror.sce"; ..
"assert_checkequal", "assert_checkequal.sce"; ..
"assert_checkalmostequal", "assert_checkalmostequal.sce"; ..
"assert_accuratedigits", "assert_accuratedigits.sce"; ..
];
subdemolist(:,2) = demopath + subdemolist(:,2)
demos/assert_checkalmostequal.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// This help file was automatically generated from assert_checkalmostequal.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checkalmostequal.sci
//
// Comparisons which are successful.
// Relative error :
assert_checkalmostequal ( 1 , 1 );
assert_checkalmostequal ( 1 , 1 , %eps );
assert_checkalmostequal ( ones(10,1) , ones(10,1) , %eps );
// Absolute error :
assert_checkalmostequal ( 1.23456789123456789e-30 , 0 , 0 , 1.e-10 );
assert_checkalmostequal ( [1 %nan], [1 %nan] , 0 , %eps );
halt() // Press return to continue
// Comparisons which are failures.
// Error message produced :
assert_checkalmostequal ( 1 , 2 , %eps );
// Error message produced :
flag = assert_checkalmostequal ( 1 , 2 , %eps )
// No error message produced :
[flag,errmsg] = assert_checkalmostequal ( 1 , 2 , %eps )
assert_checkalmostequal ( 1 , [2 3] , %eps );
assert_checkalmostequal ( [%nan 1], [1 %nan] , %eps );
assert_checkalmostequal ( 1 + 5 * %eps , 1 , %eps );
assert_checkalmostequal ( 1.23456789123456789e-30 , 1.3e-30 , %eps );
halt() // Press return to continue
// In the case where expected is nonzero, the
// tolerance for relative error should be a
// multiple of %eps.
// The following test is a success and shows
// that less than 11 digits are lost with respect
// to the maximum possible accuracy.
assert_checkalmostequal ( 1.23456 , 1.23457 , 1.e11*%eps );
halt() // Press return to continue
// We cannot exchange the relative and absolute tolerances.
// The following test pass: we use an absolute tolerance
// because the expected value is zero.
assert_checkalmostequal ( 1.23456789e-30 , 0 , 0 , 1.e-10 );
// The following test fail: we use a relative tolerance.
assert_checkalmostequal ( 0 , 1.23456789e-30 , 1.e-10 );
halt() // Press return to continue
// We must configure the tolerance as tightly as possible.
// The following test fails, because the tolerance is too small
// with respect to the given numbers.
assert_checkalmostequal ( 1.23456 , 1.23457 , %eps );
// In order to get the number of common digits:
assert_computedigits(1.23456 , 1.23457)
// which returns 5.09...
// We now make a judgment on the accuracy and conclude it is acceptable:
assert_checkalmostequal ( 1.23456 , 1.23457 , 1.e-5 );
halt() // Press return to continue
// We give here a practical example of the use of assert_checkalmostequal.
// We solve an averagely ill-conditionned system of linear
// equations, based on the Hilbert matrix.
n = 6;
// The expected value is set by ourselves.
expected = ones(n,1);
A = testmatrix("hilb",n);
// We compute the condition number of the matrix : ~10^8
ceil(log10(cond(A)))
// This means that the number of digits lost,
// predicted by theory, is 8.
// The right-hand side is computed given A and expected.
b = A * expected;
// In this case, a Gauss algorithm with partial
// pivoting is used.
computed = A\b;
// The following test fails: we have lost some digits.
assert_checkalmostequal(computed,expected,%eps)
// We compute the actual number of common digits: from 10 to 12
assert_computedigits(computed, expected)
// We accept this computation.
// The following test pass.
assert_checkalmostequal(computed,expected,1.e5*%eps);
halt() // Press return to continue
// The following test pass.
assert_checkalmostequal ( [1 1.e5] , [2 1.e5] , 1.e-3 )
// We see that 1 is not almost equal to 2: we must use the elementwise comparison.
// The following test does not pass.
assert_checkalmostequal ( [1 1.e5] , [2 1.e5] , 1.e-3 , [] , "element" )
halt() // Press return to continue
// The following test pass.
// It is non-trivial to take into account for IEEE values.
[flag,errmsg] = assert_checkalmostequal ( [1.2345 %inf -%inf %nan] , [1.2346 %inf -%inf %nan] , 1.e-4 )
halt() // Press return to continue
// This function takes into account for complex numbers.
// The following test pass.
assert_checkalmostequal ( 1+%i , 1+(1+1.e-4)*%i , 1.e-3 , [], "element" );
// The following test fails.
assert_checkalmostequal ( 1+%i , 1+(1+1.e-4)*%i , 1.e-5 , [], "element" );
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checkalmostequal.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_compare.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// This help file was automatically generated from assert_compare.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_compare.sci
//
order = assert_compare ( 1 , -1 ) // 1
order = assert_compare ( -1 , 1 ) // -1
order = assert_compare ( 1 , 1 ) // 0
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_compare.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_sort.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// This help file was automatically generated from assert_sort.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_sort.sci
//
// Sort x in increasing order
x = [4 5 1 2 3]
y = assert_sort ( x )
halt() // Press return to continue
// Get the indices
x = [4 5 1 2 3]
[y,indices] = assert_sort ( x )
x(indices)==y
halt() // Press return to continue
// Sort x in decreasing order
x = [4 5 1 2 3];
y = assert_sort ( x , %f )
halt() // Press return to continue
// Use a customized comparison function:
// sort into decreasing order.
function order = myorder ( x , y )
if ( x < y ) then
order = 1
elseif ( x==y ) then
order = 0
else
order = -1
end
endfunction
x = [4 5 1 2 3]
// Notice that we use the default direction (i.e. increasing)
// by setting direction to the empty matrix [].
y = assert_sort ( x , [] , myorder )
halt() // Press return to continue
// Use a customized comparison function:
// sort real values into increasing order,
// with an absolute tolerance.
function order = myrealorder ( x , y , atol )
if ( abs(x-y)<atol ) then
order = 0
elseif ( x < y ) then
order = -1
else
order = 1
end
endfunction
x = [1,2,1.2347,1.2346,1.2345,3,4]
atol = 1.e-3
compfun = list(myrealorder,atol)
y = assert_sort ( x , [] , compfun )
// Notice that the basic comparison function would
// produce a difference sorted matrix.
assert_sort ( x )
// See that this is a stable sort.
// Notice that the equal values are not moved.
y = assert_sort ( x , %f , compfun )
halt() // Press return to continue
// Check that we can sort lists.
// We consider list of elements, where each element
// is a couple. The order depends on the first element (denoted "x"),
// then the second element (denoted "y"), in case of tie.
z = list();
z(1) = list(6,5);
z(2) = list(5,4);
z(3) = list(4,3);
z(4) = list(3,2);
z(5) = list(2,1);
z(6) = list(1,0);
sci2exp(zsorted)
// Use a customized comparison function:
// sort into increasing order.
function order = myorder2 ( a , b )
ax = a(1)
ay = a(2)
bx = b(1)
by = b(2)
if ( ax == bx ) then
if ( ay == by ) then
order = 0
elseif ( ay < by ) then
order = -1
else
order = 1
end
elseif ( ax < bx ) then
order = -1
else
order = 1
end
endfunction
zsorted = assert_sort(z,[],myorder2);
sci2exp(zsorted)
halt() // Press return to continue
// Use a callback to print the status of sorting
function mycb ( status , x , imin , imax )
mprintf("%s: (%d,%d) : %s\n",status,imin,imax,sci2exp(x))
endfunction
// Sort this into [1 2 3 4 5]
x = [4 5 1 2 3];
// Notice that we set compfun = [], so that the default
// comparison function is used.
y = assert_sort ( x , [] , [] , mycb );
halt() // Press return to continue
// Display a graphical demo of the sorting process
function mycb2 ( status , x , imin , imax )
n = length(x)
if ( status == "start") then
h = scf()
xp = 1:n
yp = x
plot(xp,yp,"b*")
titlestr = "Merge sort n="+string(n)+" indices = ("+string(imin)+","+string(imax)+")"
xtitle(titlestr,"Rank","Value")
elseif ( status == "out") then
h = gcf()
h.children.children.children.data(imin:imax,2) = x
titlestr = "Merge sort n="+string(n)+" indices = ("+string(imin)+","+string(imax)+")"
h.children.title.text = titlestr
end
endfunction
N = 50;
// Take the values 1,2,...,50 and permute them randomly
x = (1:50)';
x = grand(1,"prm",x);
y = assert_sort ( x , [] , [] , mycb2 );
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_sort.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_checkerror.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// This help file was automatically generated from assert_checkerror.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checkerror.sci
//
// This function generates an error when
// * the number of input arguments in wrong
// * the type of x is wrong
function y = f(x)
[lhs,rhs]=argn()
if ( rhs <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected number of input arguments : %d provided while %d to %d are expected.") , "f" , rhs , 1 , 1 )
error(errmsg)
end
if ( typeof(x) <> "constant" ) then
localstr = gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.")
errmsg = sprintf ( localstr , "f" , 1 , "x" , typeof(x) , "constant" )
error(errmsg,123456789)
end
y = x
endfunction
// Our task is to check the error messages produced by this function
// Test which pass : the error message is the expected one
msg1="f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.";
msg2="f: Unexpected type of input argument #1 : variable x has type string while constant is expected.";
assert_checkerror ( "y=f()" , msg1 );
assert_checkerror ( "y=f(""a"")" , msg2 );
// Also check the error number
assert_checkerror ( "y=f()" , msg1 , 10000 );
assert_checkerror ( "y=f(""a"")" , msg2 , 123456789 );
// Test which fail
assert_checkerror ( "y=f()" , "oups" );
assert_checkerror ( "y=f()" , msg1 , 12 );
halt() // Press return to continue
// When errmsg is given as output argument, no error is generated
// A test which pass: flag is %t, errmsg is empty
[flag,errmsg] = assert_checkerror ( "y=f()" , msg1 )
// A test which fail: flag is %f, errmsg is not empty
[flag,errmsg] = assert_checkerror ( "y=f()" , "oups" )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checkerror.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_cond2reltol.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// This help file was automatically generated from assert_cond2reltol.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_cond2reltol.sci
//
assert_cond2reltol ( 0 ) // 1.110D-16
assert_cond2reltol ( 1 ) // 1.110D-16
assert_cond2reltol ( 1.e1 ) // 1.110D-15
assert_cond2reltol ( 1.e2 ) // 1.110D-14
assert_cond2reltol ( 1.e3 ) // 1.110D-13
assert_cond2reltol ( 1.e16 ) // 1
assert_cond2reltol ( 1.e17 ) // 1
assert_cond2reltol ( 1.e18 ) // 1
halt() // Press return to continue
// Matrix input.
condition = [0,1,10,100,1000,1.D13,1.D14,1.D15,1.D16,1.D17,1.D18];
expected = [1.110D-16 1.110D-16 1.110D-15 1.110D-14 1.110D-13 0.0011102 0.0111022 0.1110223 1. 1. 1.];
assert_cond2reltol ( condition )
halt() // Press return to continue
// Using offset
// Negative offset : require less accuracy
assert_cond2reltol ( 1.e2 , [0 -1] ) // [1.1D-14 1.1D-13]
// Positive offset : requires more accuracy
// See that the impact of offset is constrained.
assert_cond2reltol ( 1.e2 , [0 1 2 3] ) // [1.1D-14 1.1D-15 1.1D-16 1.1D-16]
// Negative offset
// See that the impact of offset is constrained.
assert_cond2reltol ( 1.e14 , [0 -1 -2 -3] ) // [1.1D-02 1.1D-01 1 1]
halt() // Press return to continue
// Plot the relative tolerance depending on the condition
condition = logspace(0,18,1000);
r = assert_cond2reltol ( condition );
plot(condition,r)
h=gcf();
h.children.log_flags="lln";
h.children.children.children.thickness=4;
xt = h.children.x_ticks;
xt.locations = 10^(0:2:18)';
xt.labels = ["10^0";"10^2";"10^4";"10^6";"10^8";"10^10";"10^12";"10^14";"10^16";"10^18"];
h.children.x_ticks=xt;
yt = h.children.y_ticks;
yt.locations = 10^-(0:2:18)';
yt.labels = ["10^0";"10^-2";"10^-4";"10^-6";"10^-8";"10^-10";"10^-12";"10^-14";"10^-16";"10^-18"];
h.children.y_ticks=yt;
xtitle("Relative tolerance","Condition","Relative tolerance");
r = assert_cond2reltol ( condition , +3 );
plot(condition,r,"r")
r = assert_cond2reltol ( condition , -3 );
plot(condition,r,"g")
legend(["Offset=0","Offset=+3","Offset=-3"]);
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_cond2reltol.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_csvread.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// This help file was automatically generated from assert_csvread.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_csvread.sci
//
// Define a matrix of strings
Astr = [
"1" "8" "15" "22" "29" "36" "43" "50"
"2" "9" "16" "23" "30" "37" "44" "51"
"3" "10" "17" "6+3*I" "31" "38" "45" "52"
"4" "11" "18" "25" "32" "39" "46" "53"
"5" "12" "19" "26" "33" "40" "47" "54"
"6" "13" "20" "27" "34" "41" "48" "55"
"+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323"
];
halt() // Press return to continue
// Create a file with some data separated with commas
srcstring = fullfile(TMPDIR , 'foo.csv');
sep = ",";
fd = mopen(srcstring,'wt');
for i = 1 : size(Astr,"r")
mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
// To see the file : edit(srcstring)
halt() // Press return to continue
// Read this file
Bstr = assert_csvread ( srcstring )
// To convert this data into a double :
Bdbl = evstr(Bstr)
halt() // Press return to continue
// Create a file with a particular separator: here ";"
srcstring = fullfile(TMPDIR , 'foo.csv');
sep = ";";
fd = mopen(srcstring,'wt');
for i = 1 : size(Astr,"r")
mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
//
// Read the file and customize the separator
assert_csvread ( srcstring , sep )
halt() // Press return to continue
// Create a file with comments
srcstring = fullfile(TMPDIR , 'foo.csv');
sep = ",";
fd = mopen(srcstring,'wt');
mfprintf(fd,"#This is a comment\n");
for i = 1 : size(Astr,"r")
mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
mfprintf(fd,"# This is another comment\n");
end
mfprintf(fd,"# This is a final comment\n");
mclose(fd);
//
// Read the file and ignore the comments
assert_csvread ( srcstring )
// Customize the comment regular expression
assert_csvread ( srcstring , [] , [] , "/#(.*)/" )
halt() // Press return to continue
halt() // Press return to continue
// Create a data file with particular Infinities and complex numbers
Astr = [
"6+3*I" "13-7*I" "20+4*I" "27-1.5*I" "34+3.14*I" "41-3*I" "48+3*I" "55-7*I"
"+0" "-0" "Infinity" "-Infinity" "Nan" "1.D+308" "1.e-308" "1.e-323"
];
expected = [
"6+3*%i" "13-7*%i" "20+4*%i" "27-1.5*%i" "34+3.14*%i" "41-3*%i" "48+3*%i" "55-7*%i"
"+0" "-0" "%inf" "-%inf" "%nan" "1.D+308" "1.e-308" "1.e-323"
];
filename = fullfile(TMPDIR , "foo.csv");
sep = ",";
fd = mopen(filename,'wt');
for i = 1 : size(Astr,"r")
mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
//
// Customize the replace map.
replacemap=["Nan","%nan";"Infinity","%inf";"I","%i"];
assert_csvread ( filename , [] , [] , [] , [] , replacemap )
halt() // Press return to continue
// Configure the decimal mark.
Atext = [
" 1,000000000D+00; 0,000000000D+00; 2,000000000D+02; Inf; 0,000000000D+00";
" 1,000000000D+00; 1,00000000D-300; 2,000000000D+02; Inf; 0,000000000D+00";
" 1,000000000D+00; 1,00000000D-200; 2,000000000D+02; 3,15000000D+300; 1,020000000D+02";
" 9,999999999D-01; 1,00000000D-100; 2,000000000D+02; 2,960000000D+02; 1,170000000D+02";
" 1,000000000D+00; Inf; -Inf; Nan; 0,000000000D+00"
];
assert_csvread ( Atext , ";" , "," , [] , %t )
halt() // Press return to continue
// Check that datasetread and datasetwrite are inverse.
A = rand(3,4)+rand(3,4)*%i
Astr = assert_csvwrite(A);
Bstr = assert_csvread(Astr,[],[],[],%t);
B = evstr(Bstr);
and(A==B)
halt() // Press return to continue
// Read a data with an inconsistent number of columns
Atxt=[
"40 5 30 1.6 0.2 1.2"
"15 25 35 0.6 1 1.4"
"20 45 10 0.8 1.8 0.4"
""
"2.6667 0.33333 2"
"1 1.6667 2.3333"
"1.3333 3 0.66667"
];
A = assert_csvread ( Atxt," ",[],[],%t )
// Read a file with an unconsistent number of columns
Atxt=[
"2.6667 0.33333 2"
"1 1.6667 2.3333"
"1.3333 3 0.66667"
""
"40 5 30 1.6 0.2 1.2"
"15 25 35 0.6 1 1.4"
"20 45 10 0.8 1.8 0.4"
];
A = assert_csvread ( Atxt," ",[],[],%t )
halt() // Press return to continue
// Read data and convert into doubles
Astr = [
"1,8,15,22,29,36,43,50"
"2,9,16,23,30,37,44,51"
"3,10,17,6+3*I,31,38,45,52"
"4,11,18,25,32,39,46,53"
"5,12,19,26,33,40,47,54"
"6,13,20,27,34,41,48,55"
"+0,-0,Inf,-Inf,Nan,1.D+308,1.e-308,1.e-323"
];
A = assert_csvread ( Astr,[],[],[],%t,[],2 )
halt() // Press return to continue
// Read only rows/columns in range
Astr = [
"1,8,15,22,29,36,43,50"
"2,9,16,23,30,37,44,51"
"3,10,17,6+3*I,31,38,45,52"
"4,11,18,25,32,39,46,53"
"5,12,19,26,33,40,47,54"
"6,13,20,27,34,41,48,55"
"+0,-0,Inf,-Inf,Nan,1.D+308,1.e-308,1.e-323"
];
A = assert_csvread ( Astr,[],[],[],%t,[],[],[2 3 5 6] );
halt() // Press return to continue
// The irange argument allows to read large datasets.
// In the following example, we read only the last line of a large dataset.
n=50;
M = ones(n, n);
filename=fullfile(TMPDIR,"foo.csv");
assert_csvwrite(M,filename);
assert_csvread(filename);
Mlast = assert_csvread ( filename , [] , [] , [] , [] , [] , [] , [n 1 n n] );
halt() // Press return to continue
//
// We create a "large" csv file with 10 000 rows and 3 columns.
filename=fullfile(TMPDIR,"prices.csv");
fd = mopen(filename,"w");
for i= 1:10000
mputl("02/05/10 00:00:02,1.32453,1.32491",fd);
end
mclose(fd);
//
// We read it by blocks of 1234 rows and end the read when the
// end of the file is reached.
blocksize = 1234;
C1 = 1;
C2 = 3;
iblock = 1
while (%t)
R1 = (iblock-1) * blocksize + 1;
R2 = blocksize + R1-1;
irange = [R1 C1 R2 C2];
M=assert_csvread(filename , [] , [] , [] , [] , [] , [] , irange );
nrows = size(M,"r");
disp([iblock R1 R2 nrows])
if ( nrows < blocksize ) then
break
end
iblock = iblock + 1;
end
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_csvread.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_checkfalse.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// This help file was automatically generated from assert_checkfalse.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checkfalse.sci
//
// Tests which pass
assert_checkfalse ( %f );
flag = assert_checkfalse ( %f )
[flag,errmsg] = assert_checkfalse ( %f )
[flag,errmsg] = assert_checkfalse ( [%f %f] );
halt() // Press return to continue
// Tests which fail
assert_checkfalse ( [%t %f] );
flag = assert_checkfalse ( [%t %f] )
// No error generated
[flag,errmsg] = assert_checkfalse ( [%t %f] )
halt() // Press return to continue
// Wrong calls
assert_checkfalse ( "a" )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checkfalse.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_checkfilesequal.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// This help file was automatically generated from assert_checkfilesequal.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checkfilesequal.sci
//
//
// Prepare data for the tests
// fileref1 : three lines of text.
// filecomp1 : == fileref1
// filecomp2 : <> fileref1
fileref1 = fullfile(TMPDIR,"fileref.txt");
txt1 = [
"Line #1"
"Line #2"
"Line #3"
];
fd = mopen(fileref1,"w");
mputl(txt1,fd);
mclose(fd);
filecomp1 = fullfile(TMPDIR,"filecomp1.txt");
fd = mopen(filecomp1,"w");
mputl(txt1,fd);
mclose(fd);
filecomp2 = fullfile(TMPDIR,"filecomp2.txt");
txt2 = [
"Line #1"
"Line #4"
"Line #3"
];
fd = mopen(filecomp2,"w");
mputl(txt2,fd);
mclose(fd);
halt() // Press return to continue
// A test which pass
flag = assert_checkfilesequal ( filecomp1 , fileref1 )
[flag,errmsg] = assert_checkfilesequal ( filecomp1 , fileref1 )
halt() // Press return to continue
// Failure: filecomp2 <> fileref1
// Error generated
flag = assert_checkfilesequal ( filecomp2 , fileref1 )
// No error generated
[flag,errmsg] = assert_checkfilesequal ( filecomp2 , fileref1 )
halt() // Press return to continue
// Prepare data for the tests
// fileref2 == filecomp3, given that comment lines are ignored.
// fileref2 <> filecomp4, given that comment lines are ignored.
// Notice that the comments are inserted at different positions in the files:
// sometimes at the begining, sometimes in the middle.
fileref2 = fullfile(TMPDIR,"fileref2.txt");
txt = [
"// bla 2"
"Line #1"
"// bla 2"
"Line #2"
"Line #3"
];
fd = mopen(fileref2,"w");
mputl(txt,fd);
mclose(fd);
filecomp3 = fullfile(TMPDIR,"filecomp3.txt");
txt = [
"Line #1"
"// bla 5168"
"Line #2"
"Line #3"
"// bla oups"
];
fd = mopen(filecomp3,"w");
mputl(txt,fd);
mclose(fd);
filecomp4 = fullfile(TMPDIR,"filecomp4.txt");
txt = [
"// bla 3"
"Line #1"
"Line #4"
"// bla 5168"
"Line #3"
"// bla oups"
];
fd = mopen(filecomp4,"w");
mputl(txt,fd);
mclose(fd);
halt() // Press return to continue
// A test with a comparison function which ignores comment lines.
function otxt = myfilter ( itxt )
nr = size(itxt,"r")
// This is the pattern for a comment line of the form "// blabla"
pattern = "/\/\/.*/"
k = 1
for i = 1 : nr
start = regexp(itxt(i),pattern)
if ( start == [] ) then
otxt(k) = itxt(i)
k = k + 1
end
end
endfunction
function areequal = mycompfun ( ctxt , etxt )
ctxt = myfilter ( ctxt )
etxt = myfilter ( etxt )
areequal = ( ctxt == etxt )
endfunction
//
// A test which pass
[flag,errmsg] = assert_checkfilesequal ( filecomp3 , fileref2 , mycompfun )
// A test which fails
[flag,errmsg] = assert_checkfilesequal ( filecomp4 , fileref2 , mycompfun )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checkfilesequal.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_accuratedigits.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// This help file was automatically generated from assert_accuratedigits.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_accuratedigits.sci
//
d = assert_accuratedigits ( 1 , 1 ) // d = dmax
d = assert_accuratedigits ( 0 , 0 ) // d = dmax
d = assert_accuratedigits ( 1 , 0 ) // d = dmin
d = assert_accuratedigits ( 0 , 1 ) // d = dmin
d = assert_accuratedigits ( 3.1415926 , %pi ) // d = 7.7680678
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_accuratedigits.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_checkequal.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// This help file was automatically generated from assert_checkequal.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checkequal.sci
//
// Tests with success
assert_checkequal ( %T , %T );
flag = assert_checkequal ( list() , list() );
[flag , errmsg] = assert_checkequal ( [%T %F], [%T %F] );
assert_checkequal ( %nan , %nan );
halt() // Press return to continue
// Tests with failure
assert_checkequal ( %F , %T );
flag = assert_checkequal ( %F , %T );
// No error produced :
[flag , errmsg] = assert_checkequal ( %F , %T )
assert_checkequal ( [1 2], [3 4] )
assert_checkequal ( 1 , [2 3] )
assert_checkequal ( 1 , "b" )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checkequal.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_csvwrite.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// This help file was automatically generated from assert_csvwrite.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_csvwrite.sci
//
A = [
1 0 200 %inf 0
1 1.e-300 200 %inf 0
9.99999999999990010e-001 9.99999999999999980e-201 200 3.15e300 102
9.99999999899999990e-001 1.e-100 200 296 117
1 %inf -%inf %nan 0
];
strmat = assert_csvwrite ( A )
halt() // Press return to continue
// Write into a file
filename=fullfile(TMPDIR,"foo.csv");
assert_csvwrite ( A , filename );
edit(filename)
halt() // Press return to continue
halt() // Press return to continue
// Customize the separator
strmat = assert_csvwrite ( A , [] , ";" )
// Customize the decimal mark
strmat = assert_csvwrite ( A , [] , ";" , "," )
// Customize the number of digits
strmat = assert_csvwrite ( A , [] , [] , [] , 8 )
halt() // Press return to continue
// Create a particular matrix
A = [
1+6*%i , 3-4*%i , 5-2*%i , 1.e-300
-2-5*%i , -4+3*%i , 6+1*%i , 1.e300
%inf , -%inf , %nan , 1.e-323
];
//
// Use the replace map to configure the way %inf, %nan and %i are printed
replacemap = ["%inf","infinity";"%nan","notanumber";"%i","i"];
strmat = assert_csvwrite ( A , [] , [] , [] , 8 , replacemap)
halt() // Press return to continue
// Configure the format
assert_csvwrite ( A , [] , [] , [] , "%.17e" )
assert_csvwrite ( A , [] , [] , [] , "%.5e" )
halt() // Press return to continue
// Configure the header
header = [
"# Copyright (C) 2012 - Foo"
"# Do what you want with this file."
];
strmat = assert_csvwrite ( A , [] , [] , [] , [] , [] , header )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_csvwrite.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_checktrue.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// This help file was automatically generated from assert_checktrue.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_checktrue.sci
//
// Tests which pass
assert_checktrue ( %t );
flag = assert_checktrue ( %t )
[flag,errmsg] = assert_checktrue ( %t )
[flag,errmsg] = assert_checktrue ( [%t %t] );
halt() // Press return to continue
// Tests which fail
assert_checktrue ( [%t %f] );
flag = assert_checktrue ( [%t %f] )
// No error generated
[flag,errmsg] = assert_checktrue ( [%t %f] )
halt() // Press return to continue
// Wrong calls
assert_checktrue ( "a" )
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_checktrue.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_comparecomplex.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// This help file was automatically generated from assert_comparecomplex.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_comparecomplex.sci
//
// Compare real values
assert_comparecomplex ( 1 , -1 ) // 1
assert_comparecomplex ( -1 , 1 ) // -1
assert_comparecomplex ( 1 , 1 ) // 0
halt() // Press return to continue
// Compare complex values #1
assert_comparecomplex ( 1+2*%i , 1+3*%i ) // -1
assert_comparecomplex ( 1+3*%i , 1+2*%i ) // 1
assert_comparecomplex ( 1+2*%i , 1+2*%i ) // 0
halt() // Press return to continue
// Compare complex values #2
assert_comparecomplex ( 1+%i , -1+%i ) // 1
assert_comparecomplex ( -1+%i , 1+%i ) // -1
assert_comparecomplex ( 1+%i , 1+%i ) // 0
[order,msg] = assert_comparecomplex ( 1+%i , 1+%i )
halt() // Press return to continue
// Compare with tolerances : equality cases
assert_comparecomplex ( 1.2345+%i , 1.2346+%i , %eps , 1.e-3 ) // 0
assert_comparecomplex ( 1.2345+%i , 1.2346+%i , 1.e12*%eps , 0 ) // 0
assert_comparecomplex ( 1+1.2345*%i , 1+1.2347*%i , %eps , 1.e-3 ) // 0
assert_comparecomplex ( 1+1.2345*%i , 1+1.2347*%i , 1.e12*%eps , 0 ) // 0
halt() // Press return to continue
// Compare more realistic data
x = [
-0.123452 - 0.123454 * %i
-0.123451 + 0.123453 * %i
0.123458 - 0.123459 * %i
0.123456 + 0.123457 * %i
];
// Consider less than 4 significant digits
for i = 1 : size(x,"*")-1
order = assert_comparecomplex ( x(i) , x(i+1) , 1.e-4 );
mprintf("compare(x(%d),x(%d))=%d\n",i,i+1,order)
end
halt() // Press return to continue
// Compare data from bug #415
x = [
-1.9914145
-1.895889
-1.6923826
-1.4815461
-1.1302576
-0.5652256 - 0.0655080 * %i
-0.5652256 + 0.0655080 * %i
0.3354023 - 0.1602902 * %i
0.3354023 + 0.1602902 * %i
1.3468911
1.5040136
1.846668
1.9736772
1.9798866
];
// Consider less than 4 significant digits
for i = 1 : size(x,"*")-1
order = assert_comparecomplex ( x(i) , x(i+1) , 1.e-5 );
mprintf("compare(x(%d),x(%d))=%d\n",i,i+1,order)
end
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_comparecomplex.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_sortcomplex.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// This help file was automatically generated from assert_sortcomplex.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_sortcomplex.sci
//
// Sort realistic data
x = [
-0.123452 - 0.123454 * %i
-0.123451 + 0.123453 * %i
0.123458 - 0.123459 * %i
0.123456 + 0.123457 * %i
];
// Permute randomly the array
n = size(x,"*");
grand("setsd",0);
ip = grand(1,"prm",(1:n)');
x = x(ip)
// Consider less than 4 significant digits
y = assert_sortcomplex(x,1.e-4)
halt() // Press return to continue
// Sort data from bug #415
x = [
-1.9914145
-1.895889
-1.6923826
-1.4815461
-1.1302576
-0.5652256 - 0.0655080 * %i
-0.5652256 + 0.0655080 * %i
0.3354023 - 0.1602902 * %i
0.3354023 + 0.1602902 * %i
1.3468911
1.5040136
1.846668
1.9736772
1.9798866
];
// Permute randomly the array
n = size(x,"*");
grand("setsd",0);
ip = grand(1,"prm",(1:n)');
x = x(ip)
// Consider less than 4 significant digits
y = assert_sortcomplex(x,1.e-5)
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_sortcomplex.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_computedigits.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// This help file was automatically generated from assert_computedigits.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_computedigits.sci
//
d = assert_computedigits ( 1 , 1 ) // d ~ 16
d = assert_computedigits ( 1 , 1 , 2 ) // d = 53
d = assert_computedigits ( 0 , 0 ) // d ~ 16
d = assert_computedigits ( 1 , 0 ) // d = 0
d = assert_computedigits ( 0 , 1 ) // d = 0
d = assert_computedigits ( 3.1415926 , %pi ) // d ~ 8
d = assert_computedigits ( 3.1415926 , %pi , 2 ) // d ~ 26
d = assert_computedigits ( [0 0 1 1] , [0 1 0 1] ) // d ~ [16 0 0 16]
d = assert_computedigits(ones(3,2),ones(3,2)) // d ~ 16 * ones(3,2)
d = assert_computedigits(1.224646799D-16,8.462643383D-18) // d = 0
halt() // Press return to continue
// Check IEEE values
// d ~ [16 0 0 0]
d = assert_computedigits([%nan %nan %nan %nan],[%nan %inf -%inf 0])
// d ~ [0 16 0 0]
d = assert_computedigits([%inf %inf %inf %inf],[%nan %inf -%inf 0])
// d = [0 0 16 0]
d = assert_computedigits([-%inf -%inf -%inf -%inf],[%nan %inf -%inf 0])
// d = [0 0 0 16]
d = assert_computedigits([0 0 0 0],[%nan %inf -%inf 0])
halt() // Press return to continue
// Check complex values
d = assert_computedigits ( 1.2345 + %i*6.7891 , 1.23456789 + %i*6.789123456 ) // d ~ 4
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_computedigits.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );
demos/assert_cond2reqdigits.sce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// This help file was automatically generated from assert_cond2reqdigits.sci using help_from_sci().
// PLEASE DO NOT EDIT
//
mode(1)
//
// Demo of assert_cond2reqdigits.sci
//
assert_cond2reqdigits ( 0 ) // 15.95459
assert_cond2reqdigits ( 1 ) // 15.95459
assert_cond2reqdigits ( 1.e1 ) // 14.95459
assert_cond2reqdigits ( 1.e2 ) // 13.95459
assert_cond2reqdigits ( 1.e3 ) // 12.95459
assert_cond2reqdigits ( 1.e16 ) // 0
assert_cond2reqdigits ( 1.e17 ) // 0
assert_cond2reqdigits ( 1.e18 ) // 0
halt() // Press return to continue
// Matrix input.
condition = [0,1,10,100,1000,1.D13,1.D14,1.D15,1.D16,1.D17,1.D18];
assert_cond2reqdigits ( condition )
halt() // Press return to continue
// Using offset
// Netative offset : decrease number of required digits (requires less accuracy)
assert_cond2reqdigits ( 1.e2 , [0 -1] ) // [13.95459 12.95459]
// Positive offset : increase number of required digits (requires more accuracy)
// See that the impact of offset is constrained.
assert_cond2reqdigits ( 1.e2 , [0 1 2 3] ) // [13.95459 14.95459 15.95459 15.95459]
// Netative offset (requires less accuracy)
// See that the impact of offset is constrained.
assert_cond2reqdigits ( 1.e14 , [0 -1 -2 -3] ) // [1.9545898 0.9545898 0. 0.]
halt() // Press return to continue
// Using base-2
assert_cond2reqdigits ( 0 , [] , 2 ) // 53
assert_cond2reqdigits ( 1 , [] , 2 ) // 53
assert_cond2reqdigits ( 1.e1 , [] , 2 ) // 49.678072
assert_cond2reqdigits ( 1.e2 , [] , 2 ) // 46.356144
assert_cond2reqdigits ( 1.e3 , [] , 2 ) // 43.034216
assert_cond2reqdigits ( 1.e16 , [] , 2 ) // 0
assert_cond2reqdigits ( 1.e17 , [] , 2 ) // 0
assert_cond2reqdigits ( 1.e18 , [] , 2 ) // 0
halt() // Press return to continue
// Plot the number of required decimal digits depending on the condition
condition = logspace(0,18,1000);
d = assert_cond2reqdigits ( condition );
plot(condition,d)
h=gcf();
h.children.log_flags="lnn";
h.children.children.children.thickness=4;
xt = h.children.x_ticks;
xt.locations = 10^(0:2:18)';
xt.labels = ["10^0";"10^2";"10^4";"10^6";"10^8";"10^10";"10^12";"10^14";"10^16";"10^18"];
h.children.x_ticks=xt;
xtitle("Number of required digits","Condition","Required decimal digits");
halt() // Press return to continue
// Plot the number of required binary digits depending on the condition
condition = logspace(0,18,1000);
d = assert_cond2reqdigits ( condition , [] , 2 );
plot(condition,d)
h=gcf();
h.children.log_flags="lnn";
h.children.children.children.thickness=4;
xt = h.children.x_ticks;
xt.locations = 10^(0:2:18)';
xt.labels = ["10^0";"10^2";"10^4";"10^6";"10^8";"10^10";"10^12";"10^14";"10^16";"10^18"];
h.children.x_ticks=xt;
xtitle("Number of required digits","Condition","Required binary digits");
d = assert_cond2reqdigits ( condition , +10 , 2 );
plot(condition,d,"r")
d = assert_cond2reqdigits ( condition , -10 , 2 );
plot(condition,d,"g")
legend(["Offset=0","Offset=+10","Offset=-10"]);
halt() // Press return to continue
//========= E N D === O F === D E M O =========//
//
// Load this script into the editor
//
filename = "assert_cond2reqdigits.sce";
dname = get_absolute_file_path(filename);
editor ( fullfile(dname,filename) );

Archive Download the corresponding diff file

Revision: 119