1include <../std.scad>
  2
  3// Simple Calculations
  4
  5module test_quant() {
  6    assert_equal(quant(-4,3), -3);
  7    assert_equal(quant(-3,3), -3);
  8    assert_equal(quant(-2,3), -3);
  9    assert_equal(quant(-1,3), 0);
 10    assert_equal(quant(0,3), 0);
 11    assert_equal(quant(1,3), 0);
 12    assert_equal(quant(2,3), 3);
 13    assert_equal(quant(3,3), 3);
 14    assert_equal(quant(4,3), 3);
 15    assert_equal(quant(7,3), 6);
 16    assert_equal(quant(12,2.5), 12.5);
 17    assert_equal(quant(11,2.5), 10.0);
 18    assert_equal(quant([12,13,13.1,14,14.1,15,16],4), [12,12,12,16,16,16,16]);
 19    assert_equal(quant([9,10,10.4,10.5,11,12],3), [9,9,9,12,12,12]);
 20    assert_equal(quant([[9,10,10.4],[10.5,11,12]],3), [[9,9,9],[12,12,12]]);
 21}
 22test_quant();
 23
 24
 25module test_quantdn() {
 26    assert_equal(quantdn(-4,3), -6);
 27    assert_equal(quantdn(-3,3), -3);
 28    assert_equal(quantdn(-2,3), -3);
 29    assert_equal(quantdn(-1,3), -3);
 30    assert_equal(quantdn(0,3), 0);
 31    assert_equal(quantdn(1,3), 0);
 32    assert_equal(quantdn(2,3), 0);
 33    assert_equal(quantdn(3,3), 3);
 34    assert_equal(quantdn(4,3), 3);
 35    assert_equal(quantdn(7,3), 6);
 36    assert_equal(quantdn(12,2.5), 10.0);
 37    assert_equal(quantdn(11,2.5), 10.0);
 38    assert_equal(quantdn([12,13,13.1,14,14.1,15,16],4), [12,12,12,12,12,12,16]);
 39    assert_equal(quantdn([9,10,10.4,10.5,11,12],3), [9,9,9,9,9,12]);
 40    assert_equal(quantdn([[9,10,10.4],[10.5,11,12]],3), [[9,9,9],[9,9,12]]);
 41}
 42test_quantdn();
 43
 44
 45module test_quantup() {
 46    assert_equal(quantup(-4,3), -3);
 47    assert_equal(quantup(-3,3), -3);
 48    assert_equal(quantup(-2,3), 0);
 49    assert_equal(quantup(-1,3), 0);
 50    assert_equal(quantup(0,3), 0);
 51    assert_equal(quantup(1,3), 3);
 52    assert_equal(quantup(2,3), 3);
 53    assert_equal(quantup(3,3), 3);
 54    assert_equal(quantup(4,3), 6);
 55    assert_equal(quantup(7,3), 9);
 56    assert_equal(quantup(12,2.5), 12.5);
 57    assert_equal(quantup(11,2.5), 12.5);
 58    assert_equal(quantup([12,13,13.1,14,14.1,15,16],4), [12,16,16,16,16,16,16]);
 59    assert_equal(quantup([9,10,10.4,10.5,11,12],3), [9,12,12,12,12,12]);
 60    assert_equal(quantup([[9,10,10.4],[10.5,11,12]],3), [[9,12,12],[12,12,12]]);
 61}
 62test_quantup();
 63
 64
 65module test_constrain() {
 66    assert_equal(constrain(-2,-1,1), -1);
 67    assert_equal(constrain(-1.75,-1,1), -1);
 68    assert_equal(constrain(-1,-1,1), -1);
 69    assert_equal(constrain(-0.75,-1,1), -0.75);
 70    assert_equal(constrain(0,-1,1), 0);
 71    assert_equal(constrain(0.75,-1,1), 0.75);
 72    assert_equal(constrain(1,-1,1), 1);
 73    assert_equal(constrain(1.75,-1,1), 1);
 74    assert_equal(constrain(2,-1,1), 1);
 75}
 76test_constrain();
 77
 78
 79
 80module test_all_integer() {
 81    assert(!all_integer(undef));
 82    assert(!all_integer(true));
 83    assert(!all_integer(false));
 84    assert(!all_integer(4.3));
 85    assert(!all_integer("foo"));
 86    assert(!all_integer([]));
 87    assert(!all_integer([3,4.1,5,7]));
 88    assert(!all_integer([[1,2,3],[4,5,6],[7,8]]));
 89    assert(all_integer(-4));
 90    assert(all_integer(0));
 91    assert(all_integer(5));
 92    assert(all_integer([-3]));
 93    assert(all_integer([0]));
 94    assert(all_integer([3]));
 95    assert(all_integer([2,-4,0,5,7,9876543210]));
 96}
 97test_all_integer();
 98
 99
100
101
102module test_posmod() {
103    assert_equal(posmod(-5,3), 1);
104    assert_equal(posmod(-4,3), 2);
105    assert_equal(posmod(-3,3), 0);
106    assert_equal(posmod(-2,3), 1);
107    assert_equal(posmod(-1,3), 2);
108    assert_equal(posmod(0,3), 0);
109    assert_equal(posmod(1,3), 1);
110    assert_equal(posmod(2,3), 2);
111    assert_equal(posmod(3,3), 0);
112}
113test_posmod();
114
115
116module test_modang() {
117    assert_equal(modang(-700), 20);
118    assert_equal(modang(-270), 90);
119    assert_equal(modang(-120), -120);
120    assert_equal(modang(120), 120);
121    assert_equal(modang(270), -90);
122    assert_equal(modang(700), -20);
123}
124test_modang();
125
126
127module test_sqr() {
128    assert_equal(sqr(-3), 9);
129    assert_equal(sqr(0), 0);
130    assert_equal(sqr(1), 1);
131    assert_equal(sqr(2), 4);
132    assert_equal(sqr(2.5), 6.25);
133    assert_equal(sqr(3), 9);
134    assert_equal(sqr(16), 256);
135    assert_equal(sqr([2,3,4]), 29);
136    assert_equal(sqr([[2,3,4],[3,5,7],[3,5,1]]), [[25,41,33],[42,69,54],[24,39,48]]);
137}
138test_sqr();
139
140
141module test_log2() {
142    assert_equal(log2(0.125), -3);
143    assert_equal(log2(16), 4);
144    assert_equal(log2(256), 8);
145}
146test_log2();
147
148
149module test_rand_int() {
150    nums = rand_int(-100,100,1000,seed=2134);
151    assert_equal(len(nums), 1000);
152    for (num = nums) {
153        assert(num>=-100);
154        assert(num<=100);
155        assert_equal(num, floor(num));
156    }
157}
158test_rand_int();
159
160
161module test_gaussian_rands() {
162    nums1 = gaussian_rands(1000,0,10,seed=2132);
163    nums2 = gaussian_rands(1000,0,10,seed=2130);
164    nums3 = gaussian_rands(1000,0,10,seed=2132);
165    assert_equal(len(nums1), 1000);
166    assert_equal(len(nums2), 1000);
167    assert_equal(len(nums3), 1000);
168    assert_equal(nums1, nums3);
169    assert(nums1!=nums2);
170
171    R = [[4,2],[2,17]];
172    data = gaussian_rands(100000,[0,0],R,seed=49);
173    assert(approx(mean(data), [0,0], eps=1e-2));
174    assert(approx(transpose(data)*data/len(data), R, eps=2e-2));
175    
176    R2 = [[4,2,-1],[2,17,4],[-1,4,11]];
177    data3 = gaussian_rands(100000,[1,2,3],R2,seed=97);
178    assert(approx(mean(data3),[1,2,3], eps=1e-2));
179    cdata = move(-mean(data3),data3);    
180    assert(approx(transpose(cdata)*cdata/len(cdata),R2,eps=.1));
181}
182test_gaussian_rands();
183
184
185
186module test_lerp() {
187    assert_equal(lerp(-20,20,0), -20);
188    assert_equal(lerp(-20,20,0.25), -10);
189    assert_equal(lerp(-20,20,0.5), 0);
190    assert_equal(lerp(-20,20,0.75), 10);
191    assert_equal(lerp(-20,20,1), 20);
192    assert_equal(lerp(-20,20,[0,0.25,0.5,0.75,1]), [-20,-10,0,10,20]);
193    assert_equal(lerp(-20,20,[0:0.25:1]), [-20,-10,0,10,20]);
194    assert_equal(lerp([10,10],[30,-10],0.5), [20,0]);
195}
196test_lerp();
197
198
199module test_u_add() {
200    assert_equal(u_add(1,2),3);
201    assert_equal(u_add(1,-2),-1);
202    assert_equal(u_add(-1,2),1);
203    assert_equal(u_add(-1,-2),-3);
204    assert_equal(u_add(243,-27),216);
205    assert_equal(u_add([2,3,4],[8,7,9]),[10,10,13]);
206    assert_equal(u_add(undef,27),undef);
207    assert_equal(u_add(undef,-27),undef);
208    assert_equal(u_add(243,undef),undef);
209    assert_equal(u_add(-43,undef),undef);
210    assert_equal(u_add(undef,[8,7,9]),undef);
211    assert_equal(u_add([2,3,4],undef),undef);
212}
213test_u_add();
214
215
216module test_u_sub() {
217    assert_equal(u_sub(1,2),-1);
218    assert_equal(u_sub(1,-2),3);
219    assert_equal(u_sub(-1,2),-3);
220    assert_equal(u_sub(-1,-2),1);
221    assert_equal(u_sub(243,-27),270);
222    assert_equal(u_sub([2,3,4],[8,7,9]),[-6,-4,-5]);
223    assert_equal(u_sub(undef,27),undef);
224    assert_equal(u_sub(undef,-27),undef);
225    assert_equal(u_sub(243,undef),undef);
226    assert_equal(u_sub(-43,undef),undef);
227    assert_equal(u_sub(undef,[8,7,9]),undef);
228    assert_equal(u_sub([2,3,4],undef),undef);
229}
230test_u_sub();
231
232
233module test_u_mul() {
234    assert_equal(u_mul(3,2),6);
235    assert_equal(u_mul(3,-2),-6);
236    assert_equal(u_mul(-3,2),-6);
237    assert_equal(u_mul(-3,-2),6);
238    assert_equal(u_mul(243,-27),-6561);
239    assert_equal(u_mul([2,3,4],[8,7,9]),[16,21,36]);
240    assert_equal(u_mul(undef,27),undef);
241    assert_equal(u_mul(undef,-27),undef);
242    assert_equal(u_mul(243,undef),undef);
243    assert_equal(u_mul(-43,undef),undef);
244    assert_equal(u_mul(undef,[8,7,9]),undef);
245    assert_equal(u_mul([2,3,4],undef),undef);
246}
247test_u_mul();
248
249
250module test_u_div() {
251    assert_equal(u_div(1,2),1/2);
252    assert_equal(u_div(1,-2),-1/2);
253    assert_equal(u_div(-1,2),-1/2);
254    assert_equal(u_div(-1,-2),1/2);
255    assert_equal(u_div(243,-27),-9);
256    assert_equal(u_div([8,7,9],[2,3,4]),[4,7/3,9/4]);
257    assert_equal(u_div(undef,27),undef);
258    assert_equal(u_div(undef,-27),undef);
259    assert_equal(u_div(243,undef),undef);
260    assert_equal(u_div(-43,undef),undef);
261    assert_equal(u_div(undef,[8,7,9]),undef);
262    assert_equal(u_div([2,3,4],undef),undef);
263}
264test_u_div();
265
266
267module test_hypot() {
268    assert_approx(hypot(20,30), norm([20,30]));
269}
270test_hypot();
271
272
273module test_sinh() {
274    assert_approx(sinh(-2), -3.6268604078);
275    assert_approx(sinh(-1), -1.1752011936);
276    assert_approx(sinh(0), 0);
277    assert_approx(sinh(1), 1.1752011936);
278    assert_approx(sinh(2), 3.6268604078);
279}
280test_sinh();
281
282
283module test_cosh() {
284    assert_approx(cosh(-2), 3.7621956911);
285    assert_approx(cosh(-1), 1.5430806348);
286    assert_approx(cosh(0), 1);
287    assert_approx(cosh(1), 1.5430806348);
288    assert_approx(cosh(2), 3.7621956911);
289}
290test_cosh();
291
292
293module test_tanh() {
294    assert_approx(tanh(-2), -0.9640275801);
295    assert_approx(tanh(-1), -0.761594156);
296    assert_approx(tanh(0), 0);
297    assert_approx(tanh(1), 0.761594156);
298    assert_approx(tanh(2), 0.9640275801);
299}
300test_tanh();
301
302
303module test_asinh() {
304    assert_approx(asinh(sinh(-2)), -2);
305    assert_approx(asinh(sinh(-1)), -1);
306    assert_approx(asinh(sinh(0)), 0);
307    assert_approx(asinh(sinh(1)), 1);
308    assert_approx(asinh(sinh(2)), 2);
309}
310test_asinh();
311
312
313module test_acosh() {
314    assert_approx(acosh(cosh(-2)), 2);
315    assert_approx(acosh(cosh(-1)), 1);
316    assert_approx(acosh(cosh(0)), 0);
317    assert_approx(acosh(cosh(1)), 1);
318    assert_approx(acosh(cosh(2)), 2);
319}
320test_acosh();
321
322
323module test_atanh() {
324    assert_approx(atanh(tanh(-2)), -2);
325    assert_approx(atanh(tanh(-1)), -1);
326    assert_approx(atanh(tanh(0)), 0);
327    assert_approx(atanh(tanh(1)), 1);
328    assert_approx(atanh(tanh(2)), 2);
329}
330test_atanh();
331
332
333module test_sum() {
334    assert_equal(sum([]), 0);
335    assert_equal(sum([],dflt=undef), undef);
336    assert_equal(sum([1,2,3]), 6);
337    assert_equal(sum([-2,-1,0,1,2]), 0);
338    assert_equal(sum([[1,2,3], [3,4,5], [5,6,7]]), [9,12,15]);
339}
340test_sum();
341
342
343module test_cumsum() {
344    assert_equal(cumsum([]), []);
345    assert_equal(cumsum([1,1,1]), [1,2,3]);
346    assert_equal(cumsum([2,2,2]), [2,4,6]);
347    assert_equal(cumsum([1,2,3]), [1,3,6]);
348    assert_equal(cumsum([-2,-1,0,1,2]), [-2,-3,-3,-2,0]);
349    assert_equal(cumsum([[1,2,3], [3,4,5], [5,6,7]]), [[1,2,3],[4,6,8],[9,12,15]]);
350}
351test_cumsum();
352
353
354module test_sum_of_sines() {
355    assert_equal(sum_of_sines(0, [[3,4,0],[2,2,0]]), 0);
356    assert_equal(sum_of_sines(45, [[3,4,0],[2,2,0]]), 2);
357    assert_equal(sum_of_sines(90, [[3,4,0],[2,2,0]]), 0);
358    assert_equal(sum_of_sines(135, [[3,4,0],[2,2,0]]), -2);
359    assert_equal(sum_of_sines(180, [[3,4,0],[2,2,0]]), 0);
360}
361test_sum_of_sines();
362
363
364module test_deltas() {
365    assert_equal(deltas([2,5,9,17]), [3,4,8]);
366    assert_equal(deltas([2,5,9,17],wrap=true), [3,4,8,-15]);
367    assert_equal(deltas([[1,2,3], [3,6,8], [4,8,11]]), [[2,4,5], [1,2,3]]);
368    assert_equal(deltas([[1,2,3], [3,6,8], [4,8,11]],wrap=true), [[2,4,5], [1,2,3], [-3,-6,-8]]);
369}
370test_deltas();
371
372
373module test_product() {
374    assert_equal(product([2,3,4]), 24);
375    assert_equal(product([[1,2,3], [3,4,5], [5,6,7]]), [15, 48, 105]);
376    m1 = [[2,3,4],[4,5,6],[6,7,8]];
377    m2 = [[4,1,2],[3,7,2],[8,7,4]];
378    m3 = [[3,7,8],[9,2,4],[5,8,3]];
379    assert_equal(product([m1,m2,m3]), m1*m2*m3);
380}
381test_product();
382
383
384module test_mean() {
385    assert_equal(mean([2,3,4]), 3);
386    assert_equal(mean([[1,2,3], [3,4,5], [5,6,7]]), [3,4,5]);
387}
388test_mean();
389
390
391module test_median() {
392    assert_equal(median([2,3,7]), 3);
393    assert_equal(median([2,4,5,8]), 4.5);
394}
395test_median();
396
397
398
399module test_convolve() {
400    assert_equal(convolve([],[1,2,1]), []);
401    assert_equal(convolve([1,1],[]), []);
402    assert_equal(convolve([1,1],[1,2,1]), [1,3,3,1]);
403    assert_equal(convolve([1,2,3],[1,2,1]), [1,4,8,8,3]);
404    assert_equal(convolve([1,2,3],[[1],[2],[1]]),  [[1], [4], [8], [8], [3]]);
405    assert_equal(convolve([[1],[2],[3]],[[1],[2],[1]]), [1,4,8,8,3]);
406    assert_equal(convolve([[1,0],[2,1],[3,2]],[[1,0],[2,1],[1,2]]), [1,4,9,12,7]);
407    assert_equal(convolve([1,2,3],[[1,0],[2,1],[1,2]]), [[1,0],[4,1],[8,4],[8,7],[3,6]]);
408}
409test_convolve();
410
411
412
413
414// Logic
415
416
417
418module test_any() {
419    assert_equal(any([0,false,undef]), false);
420    assert_equal(any([1,false,undef]), true);
421    assert_equal(any([1,5,true]), true);
422    assert_equal(any([[0,0], [0,0]]), true);
423    assert_equal(any([[0,0], [1,0]]), true);
424    assert_equal(any([[false,false],[[false,[false],[[[true]]]],false],[false,false]]), true);
425    assert_equal(any([[false,false],[[false,[false],[[[false]]]],false],[false,false]]), true);
426    assert_equal(any([]), false);
427    assert_equal(any([1,3,5,7,9], function (a) a%2==0),false);
428    assert_equal(any([1,3,6,7,9], function (a) a%2==0),true);
429    assert_equal(any([1,3,5,7,9], function (a) a%2!=0),true);
430}
431test_any();
432
433
434module test_all() {
435    assert_equal(all([0,false,undef]), false);
436    assert_equal(all([1,false,undef]), false);
437    assert_equal(all([1,5,true]), true);
438    assert_equal(all([[0,0], [0,0]]), true);
439    assert_equal(all([[0,0], [1,0]]), true);
440    assert_equal(all([[1,1], [1,1]]), true);
441    assert_equal(all([[true,true],[[true,[true],[[[true]]]],true],[true,true]]), true);
442    assert_equal(all([[true,true],[[true,[true],[[[false]]]],true],[true,true]]), true);
443    assert_equal(all([]), true);
444    assert_equal(all([1,3,5,7,9], function (a) a%2==0),false);
445    assert_equal(all([1,3,6,8,9], function (a) a%2==0),false);
446    assert_equal(all([1,3,5,7,9], function (a) a%2!=0),true);
447}
448test_all();
449
450
451module test_factorial() {
452    assert_equal(factorial(0), 1);
453    assert_equal(factorial(1), 1);
454    assert_equal(factorial(2), 2);
455    assert_equal(factorial(3), 6);
456    assert_equal(factorial(4), 24);
457    assert_equal(factorial(5), 120);
458    assert_equal(factorial(6), 720);
459    assert_equal(factorial(7), 5040);
460    assert_equal(factorial(8), 40320);
461    assert_equal(factorial(25,21), 303600);
462    assert_equal(factorial(25,25), 1);
463}
464test_factorial();
465
466
467module test_binomial() {
468    assert_equal(binomial(1), [1,1]);
469    assert_equal(binomial(2), [1,2,1]);
470    assert_equal(binomial(3), [1,3,3,1]);
471    assert_equal(binomial(5), [1,5,10,10,5,1]);
472}
473test_binomial();
474
475
476module test_binomial_coefficient() {
477    assert_equal(binomial_coefficient(2,1), 2);
478    assert_equal(binomial_coefficient(3,2), 3);
479    assert_equal(binomial_coefficient(4,2), 6);
480    assert_equal(binomial_coefficient(10,7), 120);
481    assert_equal(binomial_coefficient(10,7), binomial(10)[7]);
482    assert_equal(binomial_coefficient(15,4), binomial(15)[4]);
483}
484test_binomial_coefficient();
485
486
487module test_gcd() {
488    assert_equal(gcd(15,25), 5);
489    assert_equal(gcd(15,27), 3);
490    assert_equal(gcd(270,405), 135);
491    assert_equal(gcd(39, 101),1);
492    assert_equal(gcd(15,-25), 5);
493    assert_equal(gcd(-15,25), 5);
494    assert_equal(gcd(5,0), 5);
495    assert_equal(gcd(0,5), 5);
496}
497test_gcd();
498
499
500module test_lcm() {
501    assert_equal(lcm(15,25), 75);
502    assert_equal(lcm(15,27), 135);
503    assert_equal(lcm(270,405), 810);
504    assert_equal(lcm([3,5,15,25,35]),525);
505}
506test_lcm();
507
508module test_rational_approx()
509{
510   pq1 = rational_approx(PI,10);       // Returns: [22,7]
511   pq2 = rational_approx(PI,10000);    // Returns: [355, 113]
512   pq3 = rational_approx(221/323,500); // Returns: [13,19]
513   pq4 = rational_approx(0,50);        // Returns: [0,1]
514   assert_equal(pq1,[22,7]);
515   assert_equal(pq2,[355,113]);
516   assert_equal(pq3,[13,19]);
517   assert_equal(pq4,[0,1]);
518   assert_equal(rational_approx(-PI,10),[-22,7]);
519   assert_equal(rational_approx(7,10), [7,1]);
520}
521test_rational_approx();
522
523
524
525
526
527module test_complex(){
528    assert_equal( complex(ident(4)), c_ident(4));
529    assert_equal( complex(3), [3,0]);
530    assert_equal( complex([1,2]), [[1,0],[2,0]]);
531    assert_equal( complex([[1,2],[3,4]]), [[ [1,0],[2,0] ], [ [3,0],[4,0]]]);
532}
533test_complex();
534
535module test_c_mul() {
536    assert_equal(c_mul([4,5],[9,-4]), [56,29]);
537    assert_equal(c_mul([-7,2],[24,3]), [-174, 27]);
538    assert_equal(c_mul([3,4], [[3,-7], [4,9], [4,8]]), [[37,-9],[-24,43], [-20,40]]);
539    assert_equal(c_mul([[3,-7], [4,9], [4,8]], [[1,1],[3,4],[-3,4]]), [-58,31]);
540    M = [
541           [ [3,4], [9,-1], [4,3] ],
542           [ [2,9], [4,9], [3,-1] ]
543        ];
544    assert_equal(c_mul(M, [ [3,4], [4,4],[5,5]]), [[38,91], [-30, 97]]);
545    assert_equal(c_mul([[4,4],[9,1]], M), [[5,111],[67,117], [32,22]]);
546    assert_equal(c_mul(M,transpose(M)), [  [[80,30], [30, 117]], [[30,117], [-134, 102]]]);
547    assert_equal(c_mul(transpose(M),M), [  [[-84,60],[-42,87],[15,50]], [[-42,87],[15,54],[60,46]], [[15,50],[60,46],[15,18]]]);
548}
549test_c_mul();
550
551
552module test_c_div() {
553    assert_equal(c_div([56,29],[9,-4]), [4,5]);
554    assert_equal(c_div([-174,27],[-7,2]), [24,3]);
555}    
556test_c_div();
557
558module test_c_conj(){
559    assert_equal(c_conj([3,4]), [3,-4]);
560    assert_equal(c_conj(           [ [2,9], [4,9], [3,-1] ]),            [ [2,-9], [4,-9], [3,1] ]);
561    M = [
562           [ [3,4], [9,-1], [4,3] ],
563           [ [2,9], [4,9], [3,-1] ]
564        ];
565    Mc = [
566           [ [3,-4], [9,1], [4,-3] ],
567           [ [2,-9], [4,-9], [3,1] ]
568        ];
569    assert_equal(c_conj(M), Mc);
570}
571test_c_conj();
572
573module test_c_real(){
574    M = [
575           [ [3,4], [9,-1], [4,3] ],
576           [ [2,9], [4,9], [3,-1] ]
577        ];
578    assert_equal(c_real(M), [[3,9,4],[2,4,3]]);
579    assert_equal(c_real(           [ [3,4], [9,-1], [4,3] ]), [3,9,4]);
580    assert_equal(c_real([3,4]),3);
581}
582test_c_real();
583
584
585module test_c_imag(){
586    M = [
587           [ [3,4], [9,-1], [4,3] ],
588           [ [2,9], [4,9], [3,-1] ]
589        ];
590    assert_equal(c_imag(M), [[4,-1,3],[9,9,-1]]);
591    assert_equal(c_imag(           [ [3,4], [9,-1], [4,3] ]), [4,-1,3]);
592    assert_equal(c_imag([3,4]),4);
593}
594test_c_imag();
595
596
597module test_c_ident(){
598  assert_equal(c_ident(3), [[[1, 0], [0, 0], [0, 0]], [[0, 0], [1, 0], [0, 0]], [[0, 0], [0, 0], [1, 0]]]);
599}
600test_c_ident();
601
602module test_c_norm(){
603  assert_equal(c_norm([3,4]), 5);
604  assert_approx(c_norm([[3,4],[5,6]]), 9.273618495495704);
605}
606test_c_norm();
607
608
609
610module test_cumprod(){
611  assert_equal(cumprod([1,2,3,4]), [1,2,6,24]);
612  assert_equal(cumprod([4]), [4]);
613  assert_equal(cumprod([]),[]);
614  assert_equal(cumprod([[2,3],[4,5],[6,7]]), [[2,3],[8,15],[48,105]]);
615  assert_equal(cumprod([[5,6,7]]),[[5,6,7]]);
616  assert_equal(cumprod([
617                        [[1,2],[3,4]],
618                        [[-4,5],[6,4]],
619                        [[9,-3],[4,3]]
620                       ]),
621                       [
622                        [[1,2],[3,4]],
623                        [[11,12],[18,28]],
624                        [[45,24],[98,132]]
625                       ]);
626  assert_equal(cumprod([[[1,2],[3,4]]]), [[[1,2],[3,4]]]);
627}
628test_cumprod();
629                         
630
631
632module test_deriv(){
633  pent = [for(x=[0:70:359]) [cos(x), sin(x)]];
634  assert_approx(deriv(pent,closed=true), 
635        [[-0.321393804843,0.556670399226],
636         [-0.883022221559,0.321393804843],
637         [-0.604022773555,-0.719846310393],
638         [0.469846310393,-0.813797681349],
639         [0.925416578398,0.163175911167],
640         [0.413175911167,0.492403876506]]);
641  assert_approx(deriv(pent,closed=true,h=2), 
642     0.5*[[-0.321393804843,0.556670399226],
643         [-0.883022221559,0.321393804843],
644         [-0.604022773555,-0.719846310393],
645         [0.469846310393,-0.813797681349],
646         [0.925416578398,0.163175911167],
647         [0.413175911167,0.492403876506]]);
648  assert_approx(deriv(pent,closed=false),
649        [[-0.432937491789,1.55799143673],
650         [-0.883022221559,0.321393804843],
651         [-0.604022773555,-0.719846310393],
652         [0.469846310393,-0.813797681349],
653         [0.925416578398,0.163175911167],
654         [0.696902572292,1.45914323952]]);
655  spent = yscale(8,p=pent);
656  lens = path_segment_lengths(spent,closed=true);
657  assert_approx(deriv(spent, closed=true, h=lens),
658         [[-0.0381285841663,0.998065839726],
659          [-0.254979378104,0.0449763331253],
660          [-0.216850793938,-0.953089506601],
661          [0.123993253223,-0.982919228715],
662          [0.191478335034,0.0131898128456],
663          [0.0674850818111,0.996109041561]]);
664  assert_approx(deriv(spent, closed=false, h=select(lens,0,-2)),
665         [[-0.0871925973657,0.996191473044],
666          [-0.254979378104,0.0449763331253],
667          [-0.216850793938,-0.953089506601],
668          [0.123993253223,-0.982919228715],
669          [0.191478335034,0.0131898128456],
670          [0.124034734589,0.992277876714]]);
671}
672test_deriv();
673
674
675module test_deriv2(){
676    oct = [for(x=[0:45:359]) [cos(x), sin(x)]];
677    assert_approx(deriv2(oct),
678           [[-0.828427124746,0.0719095841794],[-0.414213562373,-0.414213562373],[0,-0.585786437627],
679            [0.414213562373,-0.414213562373],[0.585786437627,0],[0.414213562373,0.414213562373],
680            [0,0.585786437627],[-0.636634192232,0.534938683021]]);
681    assert_approx(deriv2(oct,closed=false),
682           [[-0.828427124746,0.0719095841794],[-0.414213562373,-0.414213562373],[0,-0.585786437627],
683            [0.414213562373,-0.414213562373],[0.585786437627,0],[0.414213562373,0.414213562373],
684            [0,0.585786437627],[-0.636634192232,0.534938683021]]);
685    assert_approx(deriv2(oct,closed=true),
686           [[-0.585786437627,0],[-0.414213562373,-0.414213562373],[0,-0.585786437627],
687            [0.414213562373,-0.414213562373],[0.585786437627,0],[0.414213562373,0.414213562373],
688            [0,0.585786437627],[-0.414213562373,0.414213562373]]);
689    assert_approx(deriv2(oct,closed=false,h=2),
690         0.25*[[-0.828427124746,0.0719095841794],[-0.414213562373,-0.414213562373],[0,-0.585786437627],
691            [0.414213562373,-0.414213562373],[0.585786437627,0],[0.414213562373,0.414213562373],
692            [0,0.585786437627],[-0.636634192232,0.534938683021]]);
693    assert_approx(deriv2(oct,closed=true,h=2),
694         0.25* [[-0.585786437627,0],[-0.414213562373,-0.414213562373],[0,-0.585786437627],
695            [0.414213562373,-0.414213562373],[0.585786437627,0],[0.414213562373,0.414213562373],
696            [0,0.585786437627],[-0.414213562373,0.414213562373]]);
697}
698test_deriv2();
699
700
701module test_deriv3(){
702    oct = [for(x=[0:45:359]) [cos(x), sin(x)]];
703    assert_approx(deriv3(oct),
704           [[0.414213562373,-0.686291501015],[0.414213562373,-0.343145750508],[0.414213562373,0],
705            [0.292893218813,0.292893218813],[0,0.414213562373],[-0.292893218813,0.292893218813],
706            [-0.535533905933,0.0502525316942],[-0.778174593052,-0.192388155425]]);
707    assert_approx(deriv3(oct,closed=false),
708           [[0.414213562373,-0.686291501015],[0.414213562373,-0.343145750508],[0.414213562373,0],
709            [0.292893218813,0.292893218813],[0,0.414213562373],[-0.292893218813,0.292893218813],
710            [-0.535533905933,0.0502525316942],[-0.778174593052,-0.192388155425]]);
711    assert_approx(deriv3(oct,closed=false,h=2),
712           [[0.414213562373,-0.686291501015],[0.414213562373,-0.343145750508],[0.414213562373,0],
713            [0.292893218813,0.292893218813],[0,0.414213562373],[-0.292893218813,0.292893218813],
714            [-0.535533905933,0.0502525316942],[-0.778174593052,-0.192388155425]]/8);
715    assert_approx(deriv3(oct,closed=true),
716           [[0,-0.414213562373],[0.292893218813,-0.292893218813],[0.414213562373,0],[0.292893218813,0.292893218813],
717            [0,0.414213562373],[-0.292893218813,0.292893218813],[-0.414213562373,0],[-0.292893218813,-0.292893218813]]);
718    assert_approx(deriv3(oct,closed=true,h=2),
719           [[0,-0.414213562373],[0.292893218813,-0.292893218813],[0.414213562373,0],[0.292893218813,0.292893218813],
720            [0,0.414213562373],[-0.292893218813,0.292893218813],[-0.414213562373,0],[-0.292893218813,-0.292893218813]]/8);
721}
722test_deriv3();
723  
724
725
726module test_polynomial(){
727  assert_equal(polynomial([0],12),0);
728  assert_equal(polynomial([0],[12,4]),[0,0]);
729//  assert_equal(polynomial([],12),0);
730//  assert_equal(polynomial([],[12,4]),[0,0]);
731  assert_equal(polynomial([1,2,3,4],3),58);
732  assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]);
733  assert_equal(polynomial([0,0,2],4),2);
734}
735test_polynomial();
736
737
738module test_poly_roots(){
739   // Fifth roots of unity
740   assert_approx(
741        poly_roots([1,0,0,0,0,-1]),
742        [[1,0],[0.309016994375,0.951056516295],[-0.809016994375,0.587785252292],
743         [-0.809016994375,-0.587785252292],[0.309016994375,-0.951056516295]]);
744   assert_approx(poly_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50]])),
745               [[1, 1], [5, 5], [1, 2], [-3, 1], [-3, -1], [1, -1], [1, -2], [5, -5]]);
746   assert_approx(poly_roots([.124,.231,.942, -.334]),
747                 [[0.3242874219074053,0],[-1.093595323856930,2.666477428660098], [-1.093595323856930,-2.666477428660098]]);
748}
749test_poly_roots();
750
751module test_real_roots(){
752   // Wilkinson polynomial is a nasty test:
753   assert_approx(
754       sort(real_roots(poly_mult([[1,-1],[1,-2],[1,-3],[1,-4],[1,-5],[1,-6],[1,-7],[1,-8],[1,-9],[1,-10]]))),
755       count(10,1));
756   assert_equal(real_roots([3]), []);
757   assert_equal(real_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50]])),[]);
758   assert_equal(real_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50],[1,0,0]])),[0,0]);
759   assert_approx(real_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50],[1,4]])),[-4]);
760   assert(approx(real_roots([1,-10,25]),[5,5],eps=5e-6));
761   assert_approx(real_roots([4,-3]), [0.75]);
762   assert_approx(real_roots([0,0,0,4,-3]), [0.75]);
763}
764test_real_roots();
765
766
767
768module test_quadratic_roots(){
769    assert_approx(quadratic_roots([1,4,4]),[[-2,0],[-2,0]]);
770    assert_approx(quadratic_roots([1,4,4],real=true),[-2,-2]);
771    assert_approx(quadratic_roots([1,-5,6],real=true), [2,3]);
772    assert_approx(quadratic_roots([1,-5,6]), [[2,0],[3,0]]);
773}
774test_quadratic_roots();
775
776
777
778module test_poly_mult(){
779  assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]);
780  assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]);
781  assert_equal(poly_mult([3,2,1],[0]),[0]);
782  assert_equal(poly_mult([[1,2],[0],[5,6]]), [0]);
783  assert_equal(poly_mult([[3,4,5],[0,0,0]]), [0]);
784  assert_equal(poly_mult([[0],[0,0,0]]),[0]);
785}
786test_poly_mult();
787
788
789module test_poly_div(){
790  assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[0]]);
791  assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[], [1,2,3,4]]);
792  assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]);
793  assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]);
794  assert_equal(poly_div([0], [1,-3]), [[0],[0]]);
795}
796test_poly_div();
797
798
799module test_poly_add(){
800  assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]);
801  assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]);
802  assert_equal(poly_add([1,2,3],-[1,2,3]),[0]);
803//  assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
804}
805test_poly_add();
806
807
808module test_root_find(){
809  flist = [
810      function(x) x*x*x-2*x-5,
811      function(x) 1-1/x/x,
812      function(x) pow(x-3,3),
813      function(x) pow(x-2,5),
814      function(x) (let(xi=0.61489) -3062*(1-xi)*exp(-x)/(xi+(1-xi)*exp(-x)) -1013 + 1628/x),
815      function(x) exp(x)-2-.01/x/x + .000002/x/x/x,
816  ];
817  fint=[
818        [0,4],
819        [1e-4, 4],
820        [0,6],
821        [0,4],
822        [1e-4,5],
823        [-1,4]
824  ];
825  answers = [2.094551481542328,
826             1,
827             3,
828             2,
829             1.037536033287040,
830             0.7032048403631350
831  ];
832  
833  roots = [for(i=idx(flist)) root_find(flist[i], fint[i][0], fint[i][1])];
834  assert_approx(roots, answers, 1e-10);
835}
836test_root_find();
837
838
839// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap