a4d7ccead36ce3cf8b63f907dee780ecfee9905e
[scilab.git] / scilab / modules / ast / tests / unit_tests / sparse.dia.ref
1 //
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
4 // Copyright (C) 2012 - Scilab Enterprises - Cedric Delamarre
5 //
6 // This file must be used under the terms of the CeCILL.
7 // This source file is licensed as described in the file COPYING, which
8 // you should have received as part of this distribution.  The terms
9 // are also available at
10 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11 //
12 //
13 // <-- CLI SHELL MODE -->
14 // sparse function
15 //from dense form
16 s=[ 1,0,0,0,0,0,0;
17     0,1,0,0,0,0,0;
18     0,0,1,0,0,0,0;
19     0,0,1,1,0,0,0;
20     0,0,1,1,1,0,0;
21     0,0,1,1,0,1,0;
22     0,0,1,1,0,1,1];
23 sp=sparse(s);
24 assert_checkequal(size(s), size(sp));
25 assert_checkequal(full(sp), s);
26 //from coord
27 sp = sparse([1 1;2 2;3 3;4 3;4 4;5 3;5 4;5 5;6 3;6 4;6 6;7 3;7 4;7 6;7 7], [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]);
28 assert_checkequal(size(s), size(sp));
29 assert_checkequal(full(sp), s);
30 //with size
31 newsp = sparse([], [], [10,20]);
32 assert_checkequal(size(newsp), [10,20]);
33 //concatenation
34 spc = [sp [1; 0; 1; 0; 1; 0; 1]];
35 sc = [s [1; 0; 1; 0; 1; 0; 1]];
36 assert_checkequal(full(spc), sc);
37 spc = [sp ;[1 0 1 0 1 0 1]];
38 sc = [s ;[1 0 1 0 1 0 1]];
39 assert_checkequal(full(spc), sc);
40 spc = [[1; 0; 1; 0; 1; 0; 1] sp];
41 sc = [[1; 0; 1; 0; 1; 0; 1] s];
42 assert_checkequal(full(spc), sc);
43 spc = [[1 0 1 0 1 0 1]; sp];
44 sc = [[1 0 1 0 1 0 1]; s];
45 assert_checkequal(full(spc), sc);
46 sc = [s s];
47 spc = [sp sp];
48 assert_checkequal(full(spc), sc);
49 sc = [s; s];
50 spc = [sp; sp];
51 assert_checkequal(full(spc), sc);
52 sc = [s s];
53 spc = [sp sp];
54 assert_checkequal(full(spc), sc);
55 A = sparse(1);
56 A = [A 0];
57 A = [A 0];
58 A = [A 0];
59 assert_checkequal(full(A), [1 0 0 0]);
60 assert_checkequal(size(A, 'c'), 4);
61 //extraction
62 assert_checkequal(full(sp(:)), s(:));
63 assert_checkequal(size(sp(:)), size(s(:)));
64 assert_checkequal(full(sp(1:7, 1:7)), s(1:7, 1:7));
65 assert_checkequal(size(sp(1:7, 1:7)), size(s(1:7, 1:7)));
66 assert_checkequal(full(sp(1:4, :)), s(1:4, :));
67 assert_checkequal(size(sp(1:4, :)), size(s(1:4, :)));
68 assert_checkequal(full(sp(:, 2:6)), s(:, 2:6));
69 assert_checkequal(size(sp(:, 2:6)), size(s(:, 2:6)));
70 assert_checkequal(full(sp(:, $)), s(:, $));
71 assert_checkequal(size(sp(:, $)), size(s(:, $)));
72 //next tests get from old module sparse (Scilab 5.4)
73 ij=[1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6];
74 v=[1;2;3;4;5;6;7;8;9];
75 ij1=[1 6;1 2;6 5];
76 v1=[-1;-1;-1];
77 vc=v+%i*(21:29)';
78 v1c=v1+%i*[0;0.3;-1.2];
79 zer=sparse([],[],[6,6]);
80 a=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],1:9,[6 6]);
81 b=sparse([1 6;1 2;6 5],[-1;-1;-1],[6 6]);
82 ma=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],-(1:9),[6 6]);
83 mb=sparse([1 6;1 2;6 5],-[-1;-1;-1],[6 6]);
84 ac=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],(1:9)+%i*(21:29),[6 6]);
85 bc=sparse([1 6;1 2;6 5],[-1;-1;-1]+%i*[0;0.3;-1.2],[6 6]);
86 mac=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],-(1:9)-%i*(21:29),[6 6]);
87 mbc=sparse([1 6;1 2;6 5],[1;1;1]+%i*[0;-0.3;1.2],[6 6]);
88 //-------------------------------------------------------------
89 //test des primitives sparse, full
90 //--------------------------------------------------------------
91 //  -- for real matrix
92 assert_checkequal(full(sparse(0.3)), 0.3);
93 v=0*ones(1,3);v(3)=5;v(1)=1;
94 assert_checkequal(full(sparse([1 1;1 3],[1 5])), v);
95 v=0*ones(1,300);v(300)=5;v(1)=1;
96 assert_checkequal(full(sparse([1 1;1 300],[1 5])), v);
97 v=0*ones(3,1);v(3)=5;v(1)=1;
98 assert_checkequal(full(sparse([1 1;3 1],[1 5])), v);
99 v=0*ones(300,1);v(300)=5;v(1)=1;
100 assert_checkequal(full(sparse([1 1;300 1],[1 5])), v);
101 sp=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],1:9,[6 6]);
102 a=[0,0,3,0,2,1;;5,0,0,4,0,0;0,0,0,0,0,0;0,0,7,6,0,0;8,0,0,0,0,0;0,0,0,0,0,9];
103 assert_checkequal(full(sp), a);
104 sp=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],1:9,[8 6]);a(8,6)=0;
105 assert_checkequal(full(sp), a);
106 assert_checkequal(full(sparse([],[],[4 10])), 0*ones(4,10));
107 v=sparse([2 1;3 1;4 1;6 1],[10;11;12;13],[6,1]);
108 a=[0;10;11;12;0;13];
109 assert_checkequal(full(v), a);
110 v=sparse([1 2;1 3;1 4;1 6],[10;11;12;13],[1,6]);
111 a=[0,10,11,12,0,13];
112 assert_checkequal(full(v), a);
113 //  -- for complex matrix
114 sp=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],(1:9)+%i*(21:29),[6 6]);
115 a=[0,0,3+%i*23,0,2+%i*22,1+%i*21
116    5+%i*25,0,0,4+%i*24,0,0
117    0,0,0,0,0,0
118    0,0,7+%i*27,6+%i*26,0,0
119    8+%i*28,0,0,0,0,0
120    0,0,0,0,0,9+%i*29];
121 assert_checkequal(full(sp), a);
122 sp=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],(1:9)+%i*(21:29),[8 6]);
123 a(8,6)=0;
124 assert_checkequal(full(sp), a);
125 v=sparse([2 1;3 1;4 1;6 1],[10-3*%i;11;12+5*%i;13+0.5*%i],[6,1]);
126 a=[0;10-%i*3;11;12+%i*5;0;13+%i*0.5];
127 assert_checkequal(full(v), a);
128 v=sparse([1 2;1 3;1 4;1 6],[10-3*%i;11;12+5*%i;13+0.5*%i],[1,6]);
129 a=[0,10-%i*3,11,12+%i*5,0,13+%i*0.5];
130 assert_checkequal(full(v), a);
131 //-----------------------------------------------
132 // addition and substraction tests
133 //-----------------------------------------------
134 // -- real real
135 a=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],1:9,[6 6]);
136 b=sparse([1 6;1 2;6 5],[-1;-1;-1],[6 6]);
137 v=sparse([2 1;3 1;4 1;6 1],[10;11;12;13],[6,1]);
138 vt=sparse([1 2;1 3;1 4;1 6],[10;11;12;13],[1,6]);
139 ma=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],-(1:9),[6 6]);
140 mb=sparse([1 6;1 2;6 5],-[-1;-1;-1],[6 6]);
141 zer=sparse([],[],[6,6]);
142 ac=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],(1:9)+%i*(21:29),[6 6]);
143 bc=sparse([1 6;1 2;6 5],[-1;-1;-1]+%i*[0;0.3;-1.2],[6 6]);
144 vc=sparse([2 1;3 1;4 1;6 1],[10-3*%i;11;12+5*%i;13+0.5*%i],[6,1]);
145 vct=sparse([1 2;1 3;1 4;1 6],[10-3*%i;11;12+5*%i;13+0.5*%i],[1,6]);
146 mac=sparse([1 6;1 5;1 3;2 4;2 1;4 4;4 3;5 1;6 6],-(1:9)-%i*(21:29),[6 6]);
147 mbc=sparse([1 6;1 2;6 5],[1;1;1]+%i*[0;-0.3;1.2],[6 6]);
148 assert_checkequal(full(v+sparse([],[],[6,1])), full(v));
149 assert_checkequal(full(sparse([],[],[6,1])+v), full(v));
150 assert_checkequal(full(v+v), full(v)+full(v));
151 assert_checkequal(full(v-v), full(v)-full(v));
152 assert_checkequal(full(vt+sparse([],[],[1,6])), full(vt));
153 assert_checkequal(full(vt+vt), full(vt)+full(vt));
154 assert_checkequal(full(vt-vt), full(vt)-full(vt));
155 assert_checkequal(full(zer+zer), 0*ones(6,6));
156 assert_checkequal(full(a+a), full(a)+full(a));
157 assert_checkequal(full(b+b), full(b)+full(b));
158 assert_checkequal(full(a+zer), full(a));
159 assert_checkequal(full(zer+a), full(a));
160 assert_checkequal(full(b+a), full(b)+full(a));
161 assert_checkequal(full(a+b), full(b)+full(a));
162 assert_checkequal(full(a+ma), full(a)+full(ma));
163 assert_checkequal(full(a-a), full(a)-full(a));
164 assert_checkequal(full(a-ma), full(a)-full(ma));
165 assert_checkequal(full(b-mb), full(b)-full(mb));
166 assert_checkequal(full(a-zer), full(a));
167 assert_checkequal(full(zer-a), -full(a));
168 assert_checkequal(full(a-mb), full(a)-full(mb));
169 //  -- real complex
170 assert_checkequal(full(sparse([],[],[6,1])+vc), full(vc));
171 assert_checkequal(full(v+vc), full(v)+full(vc));
172 assert_checkequal(full(v-vc), full(v)-full(vc));
173 assert_checkequal(full(vt+vct), full(vt)+full(vct));
174 assert_checkequal(full(vt-vct), full(vt)-full(vct));
175 assert_checkequal(full(a+ac), full(a)+full(ac));
176 assert_checkequal(full(b+bc), full(b)+full(bc));
177 assert_checkequal(full(a+bc), full(a)+full(bc));
178 assert_checkequal(full(zer+ac), full(zer)+full(ac));
179 assert_checkequal(full(b+ac), full(b)+full(ac));
180 assert_checkequal(full(a-ac), full(a)-full(ac));
181 assert_checkequal(full(b-bc), full(b)-full(bc));
182 assert_checkequal(full(a-bc), full(a)-full(bc));
183 assert_checkequal(full(zer-ac), full(zer)-full(ac));
184 assert_checkequal(full(b-ac), full(b)-full(ac));
185 // -- complex real
186 assert_checkequal(full(vc+v), full(vc)+full(v));
187 assert_checkequal(full(vc-v), full(vc)-full(v));
188 assert_checkequal(full(vct+vt), full(vct)+full(vt));
189 assert_checkequal(full(vct-vt), full(vct)-full(vt));
190 assert_checkequal(full(ac+a), full(ac)+full(a));
191 assert_checkequal(full(bc+b), full(bc)+full(b));
192 assert_checkequal(full(ac+b), full(ac)+full(b));
193 assert_checkequal(full(ac+zer), full(zer)+full(ac));
194 assert_checkequal(full(bc+a), full(bc)+full(a));
195 assert_checkequal(full(ac-a), full(ac)-full(a));
196 assert_checkequal(full(bc-b), full(bc)-full(b));
197 assert_checkequal(full(ac-b), full(ac)-full(b));
198 assert_checkequal(full(ac-zer), -full(zer)+full(ac));
199 assert_checkequal(full(bc-a), full(bc)-full(a));
200 // -- complex complex
201 assert_checkequal(full(vc+vc), full(vc)+full(vc));
202 assert_checkequal(full(vc-vc), full(vc)-full(vc));
203 assert_checkequal(full(vct+vct), full(vct)+full(vct));
204 assert_checkequal(full(vct-vct), full(vct)-full(vct));
205 assert_checkequal(full(ac+ac), full(ac)+full(ac));
206 assert_checkequal(full(bc+bc), full(bc)+full(bc));
207 assert_checkequal(full(ac+bc), full(ac)+full(bc));
208 assert_checkequal(full(bc+ac), full(bc)+full(ac));
209 assert_checkequal(real(full(ac-ac)), full(zer));
210 assert_checkequal(imag(full(ac-ac)), full(zer));
211 assert_checkequal(real(full(bc-bc)), full(zer));
212 assert_checkequal(imag(full(bc-bc)), full(zer));
213 assert_checkequal(full(ac-bc), full(ac)-full(bc));
214 assert_checkequal(full(bc-ac), full(bc)-full(ac));
215 // sparse full and full sparse operation (soft coded apoerations)
216 assert_checkequal(full(a+1), full(a)+1);
217 assert_checkequal(full(1+a), full(a)+1);
218 assert_checkequal(full(a+ones(6,6)), full(a)+ones(6,6));
219 assert_checkequal(full(ones(6,6)+a), full(a)+ones(6,6));
220 assert_checkequal(full(a+2*eye()), full(a)+2*eye());
221 assert_checkequal(full(2*eye()+a), full(a)+2*eye());
222 assert_checkequal(full(a-1), full(a)-1);
223 assert_checkequal(full(1-a), 1-full(a));
224 assert_checkequal(full(a-ones(6,6)), full(a)-ones(6,6));
225 assert_checkequal(full(ones(6,6)-a), ones(6,6)-full(a));
226 assert_checkequal(full(a-2*eye()), full(a)-2*eye());
227 assert_checkequal(full(2*eye()-a), 2*eye()-full(a));
228 assert_checkequal(full(ac+1), full(ac)+1);
229 assert_checkequal(full(1+ac), full(ac)+1);
230 assert_checkequal(full(ac+ones(6,6)), full(ac)+ones(6,6));
231 assert_checkequal(full(ones(6,6)+ac), full(ac)+ones(6,6));
232 assert_checkequal(full(ac+2*eye()), full(ac)+2*eye());
233 assert_checkequal(full(2*eye()+ac), full(ac)+2*eye());
234 assert_checkequal(full(ac-1), full(ac)-1);
235 assert_checkequal(full(1-ac), 1-full(ac));
236 assert_checkequal(full(ac-ones(6,6)), full(ac)-ones(6,6));
237 assert_checkequal(full(ones(6,6)-ac), ones(6,6)-full(ac));
238 assert_checkequal(full(ac-2*eye()), full(ac)-2*eye());
239 assert_checkequal(full(2*eye()-ac), 2*eye()-full(ac));
240 assert_checkequal(full(ac+full(bc)), full(ac)+full(bc));
241 assert_checkequal(full(ac-full(bc)), full(ac)-full(bc));
242 assert_checkequal(full(full(ac)+full(bc)), full(ac)+full(bc));
243 //-----------------------------------------------
244 // multiplication tests
245 //-----------------------------------------------
246 // real x real
247 // sparse scalar , saclar sparse
248 assert_checkequal(full(a*2), full(a)*2);
249 assert_checkequal(full(2*a), full(a)*2);
250 assert_checkequal(a*[], []);
251 assert_checkequal([]*a, []);
252 c=rand(6,6);
253 assert_checkequal(a*c, full(a)*c);
254 assert_checkequal(c*a, c*full(a));
255 // sparse sparse
256 assert_checkequal(full(zer*zer), full(zer));
257 assert_checkequal(full(a*zer), full(zer));
258 assert_checkequal(full(zer*a), full(zer));
259 assert_checkequal(full(a*a), full(a)*full(a));
260 assert_checkequal(full(b*b), full(b)*full(b));
261 assert_checkequal(full(a*b), full(a)*full(b));
262 assert_checkequal(full(b*a), full(b)*full(a));
263 // complex x real real x complex
264 // sparse scalar , scalar sparse
265 assert_checkequal(full(ac*2), full(ac)*2);
266 assert_checkequal(full(2*ac), full(ac)*2);
267 assert_checkequal(full((2+%i)*a), (2+%i)*full(a));
268 assert_checkequal(full(a*(2+%i)), (2+%i)*full(a));
269 assert_checkequal(ac*[], []);
270 assert_checkequal([]*ac, []);
271 c=rand(6,6);
272 cc=c+rand(6,6)*%i;
273 assert_checkequal(ac*c, full(ac)*c);
274 assert_checkequal(cc*a, cc*full(a));
275 // sparse sparse
276 assert_checkequal(real(full(ac*zer)), full(zer));
277 assert_checkequal(imag(full(ac*zer)), full(zer));
278 assert_checkequal(real(full(zer*ac)), full(zer));
279 assert_checkequal(imag(full(zer*ac)), full(zer));
280 assert_checkequal(full(ac*a), full(ac)*full(a));
281 assert_checkequal(full(ac*a), full(ac)*full(a));
282 assert_checkequal(full(bc*b), full(bc)*full(b));
283 assert_checkequal(full(a*bc), full(a)*full(bc));
284 assert_checkequal(full(b*ac), full(b)*full(ac));
285 // // complex x complex
286 assert_checkequal(ac*cc, full(ac)*cc);
287 assert_checkequal(cc*ac, cc*full(ac));
288 // sparse sparse
289 assert_checkequal(full(ac*ac), full(ac)*full(ac));
290 assert_checkequal(full(bc*bc), full(bc)*full(bc));
291 assert_checkequal(full(bc*ac), full(bc)*full(ac));
292 //----------------------------------------------------------
293 // element wise multiplication tests
294 //----------------------------------------------------------
295 assert_checkequal(full(ac.*2), full(ac)*2);
296 assert_checkequal(full((2).*ac), full(ac)*2);
297 assert_checkequal(a.*[], []);
298 assert_checkequal([].*a, []);
299 c=rand(6,6);
300 //if norm(a*c-full(a)*c) >100*%eps then bugmes();quit;end
301 //if norm(c*a-c*full(a)) >100*%eps then bugmes();quit;end
302 // sparse sparse
303 assert_checkequal(full(zer.*zer), full(zer));
304 assert_checkequal(full(a.*zer), full(zer));
305 assert_checkequal(full(zer.*a), full(zer));
306 assert_checkequal(full(a.*a), full(a).*full(a));
307 assert_checkequal(full(b.*b), full(b).*full(b));
308 assert_checkequal(full(a.*b), full(a).*full(b));
309 assert_checkequal(full(b.*a), full(b).*full(a));
310 // complex x real real x complex
311 // sparse scalar , scalar sparse
312 assert_checkequal(full((2+%i).*a), (2+%i).*full(a));
313 assert_checkequal(full(a.*(2+%i)), (2+%i).*full(a));
314 assert_checkequal(ac.*[], []);
315 assert_checkequal([].*ac, []);
316 c=rand(6,6);
317 cc=c+rand(6,6)*%i;
318 assert_checkequal(full(ac.*c), full(ac).*c);
319 assert_checkequal(full(cc.*a), cc.*full(a));
320 // sparse sparse
321 assert_checkequal(full(ac.*zer), full(zer));
322 assert_checkequal : L'objet calculé est de type complexe, cependant il aurait dû être réel.
323 at line    86 of function assert_checkequal called by :
324 assert_checkequal(full((ac .* zer)), full(zer))
325 at line   355 of exec file called by :