OKlibrary  0.2.1.6
Auxiliary.mac
Go to the documentation of this file.
00001 /* Oliver Kullmann, 7.6.2008 (Swansea) */
00002 /* Copyright 2008, 2009, 2010, 2011, 2012, 2013 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00022 oklib_include("OKlib/ComputerAlgebra/TestSystem/Lisp/Asserts.mac")$
00023 oklib_include("OKlib/ComputerAlgebra/NumberTheory/Lisp/Auxiliary.mac")$
00024 oklib_include("OKlib/ComputerAlgebra/DataStructures/Lisp/Lists.mac")$
00025 
00026 kill(f)$
00027 
00028 /* **************
00029    * Logarithms *
00030    **************
00031 */
00032 
00033 okltest_ld(f) := (
00034   for n : 0 thru 10 do
00035     assert(f(2^n) = n),
00036   true)$
00037 
00038 okltest_log10(f) := (
00039   for n : 0 thru 10 do
00040     assert(f(10^n) = n),
00041   true)$
00042 
00043 okltest_fld(f) := (
00044   assert(f(0) = minf),
00045   assert(f(1) = 0),
00046   assert(f(2) = 1),
00047   assert(f(3) = 1),
00048   assert(f(4) = 2),
00049   assert(f(5) = 2),
00050   assert(f(2^10) = 10),
00051   assert(f(2^10-1) = 9),
00052   assert(f(2^10+1) = 10),
00053   for n : 1 thru 100 do
00054     assert(f(n) = floor(log(n)/log(2))),
00055   true)$
00056 
00057 okltest_cld(f) := (
00058   assert(f(0) = minf),
00059   for n : 1 thru 100 do
00060     assert(f(n) = ceiling(log(n)/log(2))),
00061   true)$
00062 
00063 okltest_floorlog(f) := (
00064   assert(okltest_fld(buildq([f],
00065     lambda([x], if x=0 then minf else f(x,2))))),
00066   for b : 3 thru 7 do
00067     for e : 0 thru 5 do
00068       assert(f(b^e,b) = e),
00069   /* XXX */
00070   true)$
00071 
00072 
00073 /* **********************
00074    * Sign functionality *
00075    **********************
00076 */
00077 
00078 okltest_posp(f) := (
00079   assert(map(f,[0,-4,+7]) = [false,false,true]),
00080   true)$
00081 
00082 okltest_negp(f) := (
00083   assert(map(f,[0,-4,+7]) = [false,true,false]),
00084   true)$
00085 
00086 okltest_nullp(f) := (
00087   assert(map(f,[0,-4,+7]) = [true,false,false]),
00088   true)$
00089 
00090 okltest_possignum(f) := (
00091   assert(f(0) = 0),
00092   assert(f(-4) = 0),
00093   assert(f(7) = 1),
00094   true)$
00095 
00096 okltest_negsignum(f) := (
00097   assert(f(0) = 0),
00098   assert(f(-4) = -1),
00099   assert(f(7) = 0),
00100   true)$
00101 
00102 
00103 /* ******************************
00104    * Representations of numbers *
00105    ******************************
00106 */
00107 
00108 okltest_str2chrl(f) := block(
00109   assert(f("") = []),
00110   assert(f("1") = ["1"]),
00111   assert(f("a") = ["a"]),
00112   assert(f("10") = ["1","0"]),
00113   assert(f("abcd") = ["a","b","c","d"]),
00114   true)$
00115 
00116 okltest_digit2int(f) := block(
00117   for n : 0 thru 9 do
00118     assert(f(string(n)) = n),
00119   assert(f("A") = 10),
00120   assert(f("F") = 15),
00121   assert(f("Z") = 35),
00122   true)$
00123 
00124 okltest_int2digit(f) := block(
00125   for n : 0 thru 9 do
00126     assert(f(n) = string(n)),
00127   assert(f(10) = "A"),
00128   assert(f(15) = "F"),
00129   assert(f(35) = "Z"),
00130   true)$
00131 
00132 okltest_polyadic2int(f) := (
00133   assert(f([],0) = 0),
00134   assert(f([],1) = 0),
00135   assert(f([0],0) = 0),
00136   assert(f([1],0) = 1),
00137   assert(f([0],1) = 0),
00138   assert(f([1],1) = 1),
00139   assert(f([1,1,1],0) = 1),
00140   assert(f([-2,3,-1],2) = -3),
00141   true)$
00142 
00143 okltest_polyadicstr2int(f) := block(
00144   for n : 0 thru 3 do
00145     assert(f("0",n) = 0),
00146   for n : 0 thru 3 do
00147     assert(f("00000",n) = 0),
00148   for n : 0 thru 3 do
00149     assert(f("10",n) = n),
00150   assert(f("1110101",1) = 5),
00151   for n : -3 thru 3 do
00152     assert(f(string(n),abs(n)+1) = n),
00153   assert(f("FGDD",0) = 13),
00154   assert(f("EE8C04",16) = 15633412),
00155   assert(f("-EE8C04",16) = -15633412),
00156   assert(f("MK014LZDF",36) = 63631769456211), 
00157   assert(f("-MK014LZDF",36) = -63631769456211),
00158   true)$
00159 
00160 okltest_int2polyadic(f) := (
00161   assert(f(0,0) = [0]),
00162   assert(f(0,1) = [0]),
00163   assert(f(9,10) = [9]),
00164   assert(f(99,10) = [9,9]),
00165   assert(f(100,101) = [100]),
00166   assert(f(101,101) = [1,0]).
00167   assert(f(136792598789324718765670228683992083246,256) = 
00168     map(hex2int, ["66","E9","4B","D4","EF","8A","2C","3B","88","4C","FA","59","CA","34","2B","2E"])),
00169   true)$
00170 
00171 okltest_int2polyadic_padd(f) := (
00172   assert(f(0,0,0) = [0]),
00173   assert(f(0,0,1) = [0]),
00174   assert(f(0,0,2) = [0,0]),
00175   assert(f(9,10,0) = [9]),
00176   assert(f(9,10,1) = [9]),
00177   assert(f(9,10,2) = [0,9]),
00178   true)$
00179 
00180 okltest_int2polyadicstr(f) := block(
00181   for n : 0 thru 3 do
00182     assert(f(0,n) = "0"),
00183   for n : 2 thru 12 do
00184     assert(f(n,n) = "10"),
00185   for n : -9 thru 9 do
00186     assert(f(n,10) = string(n)),
00187   assert(f(15633412,16) = "EE8C04"),
00188   assert(f(-15633412,16) = "-EE8C04"),
00189   assert(f(63631769456211,36) = "MK014LZDF"), 
00190   assert(f(-63631769456211,36) = "-MK014LZDF"),
00191   true)$
00192 
00193 okltest_hexstr2binv(f) := block(
00194   assert(f("") = []),
00195   assert(f("0") = [0,0,0,0]),
00196   assert(f("1") = [0,0,0,1]),
00197   assert(f("2") = [0,0,1,0]),
00198   assert(f("3") = [0,0,1,1]),
00199   assert(f("7") = [0,1,1,1]),
00200   assert(f("E") = [1,1,1,0]),
00201   assert(f("a12") = [1,0,1,0,0,0,0,1,0,0,1,0]),
00202   for i : 2 thru 10 do
00203     assert(
00204       f(apply(sconcat, create_list("0",j,1,i))) =
00205       lappend(create_list([0,0,0,0],j,1,i))),
00206   for i : 2 thru 10 do
00207     assert(
00208       f(apply(sconcat, create_list("F",j,1,i))) =
00209       lappend(create_list([1,1,1,1],j,1,i))),
00210   assert(f("F3A21001A012B3ABB2") =
00211     [1,1,1,1,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,1,0,0,
00212      0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,1,
00213      0,0,1,0,1,0,1,1,0,0,1,1,1,0,1,0,1,0,1,1,1,0,
00214      1,1,0,0,1,0]),
00215   assert(f("f3A21001a012b3ABb2") =
00216     [1,1,1,1,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,1,0,0,
00217      0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,1,
00218      0,0,1,0,1,0,1,1,0,0,1,1,1,0,1,0,1,0,1,1,1,0,
00219      1,1,0,0,1,0]),
00220   true)$
00221 
00222 okltest_binv2hexstr(f) := block(
00223   assert(f([]) = ""),
00224   assert(f([0]) = "0"),
00225   assert(f([1]) = "1"),
00226   assert(f([1,0]) = "2"),
00227   assert(f([0,1,0]) = "2"),
00228   assert(f([0,0,1,0]) = "2"),
00229   assert(f([0,0,1,1]) = "3"),
00230   assert(f([1,1,1]) = "7"),
00231   assert(f([1,1,1,1]) = "F"),
00232   assert(f([1,1,1,1,1]) = "F1"),
00233   assert(f([1,1,1,1,0,1]) = "F1"),
00234   assert(f([1,1,1,1,0,0,1]) = "F1"),
00235   for i : 2 thru 10 do
00236     assert(
00237       f(lappend(create_list([1,1,1,1],k,1,i))) =
00238       apply(sconcat, create_list("F",k,1,i))),
00239   for i : 2 thru 10 do
00240       assert(
00241         f(lappend(create_list([0,0,0,0],k,1,i))) =
00242         apply(sconcat, create_list("0",k,1,i))),
00243   /* Testing non-multiple-of-4 values */
00244   for i : 2 thru 10 do
00245     assert(
00246       f(append(lappend(create_list([1,1,1,1],k,1,i)),[1])) =
00247       sconcat(apply(sconcat, create_list("F",k,1,i)),"1")),
00248   for i : 2 thru 10 do
00249     assert(
00250       f(append(lappend(create_list([1,1,1,1],k,1,i)),[0,1])) =
00251       sconcat(apply(sconcat, create_list("F",k,1,i)),"1")),
00252   true)$
00253 
00254 okltest_factoradic2int(f) := (
00255   assert(f([0]) = 0),
00256   assert(f([1,0]) = 1),
00257   assert(f([1,0,0]) = 2),
00258   assert(f([1,1,0]) = 3),
00259   assert(f([2,0,0]) = 4),
00260   assert(f([2,1,0]) = 5),
00261   assert(f([3,1,0,0]) = 20),
00262   assert(f([5,4,3,2,1,0]) = 719),
00263   true)$
00264 
00265 okltest_int2factoradic(f) := (
00266   assert(f(0) = [0]),
00267   assert(f(1) = [1,0]),
00268   assert(f(17) = [2,2,1,0]),
00269   assert(f(0) = [0]),
00270   for n : 0 thru cokltl(120,720) do
00271     assert(factoradic2int(f(n)) = n),
00272   true)$
00273 
00274 
00275 /* ***************
00276    * Conversions *
00277    ***************
00278 */
00279 
00280 okltest_round_fdd(f) := (
00281   for d : 0 thru 4 do block([r : f(1,d)],
00282     assert(equal(r,1)),
00283     assert(floatnump(f(1,d)) = true)
00284   ),
00285   assert(equal(f(10.0,0), 10)),
00286   assert(equal(f(10.1,0), 10)),
00287   assert(equal(f(10.2,0), 10)),
00288   assert(equal(f(10.3,0), 10)),
00289   assert(equal(f(10.4,0), 10)),
00290   assert(equal(f(10.5,0), 10)),
00291   assert(equal(f(10.6,0), 11)),
00292   assert(equal(f(10.7,0), 11)),
00293   assert(equal(f(10.8,0), 11)),
00294   assert(equal(f(10.9,0), 11)),
00295   assert(equal(f(11.5,0), 12)),
00296   assert(equal(f(10.50,1), 10.5)),
00297   assert(equal(f(10.51,1), 10.5)),
00298   assert(equal(f(10.52,1), 10.5)),
00299   assert(equal(f(10.53,1), 10.5)),
00300   assert(equal(f(10.54,1), 10.5)),
00301   assert(equal(f(10.55,1), 10.6)),
00302   assert(equal(f(10.56,1), 10.6)),
00303   assert(equal(f(10.57,1), 10.6)),
00304   assert(equal(f(10.58,1), 10.6)),
00305   assert(equal(f(10.59,1), 10.6)),
00306   assert(equal(f(10.65,1), 10.6)),
00307   assert(equal(f(10.66,1), 10.7)),
00308   true)$
00309 
00310 okltest_round_bfdd(f) := block(
00311  [fpprec : 16],
00312    for d : 0 thru 4 do block([r : f(1,d)],
00313      assert(equal(r,1)),
00314      assert(bfloatp(r) = true)
00315   ),
00316   assert(equal(f(10.0,0), 10)),
00317   assert(equal(f(10.1,0), 10)),
00318   assert(equal(f(10.2,0), 10)),
00319   assert(equal(f(10.3,0), 10)),
00320   assert(equal(f(10.4,0), 10)),
00321   assert(equal(f(10.5,0), 10)),
00322   assert(equal(f(10.6,0), 11)),
00323   assert(equal(f(10.7,0), 11)),
00324   assert(equal(f(10.8,0), 11)),
00325   assert(equal(f(10.9,0), 11)),
00326   assert(equal(f(11.5,0), 12)),
00327   assert(equal(f(10.50,1), 10.5)),
00328   assert(equal(f(10.51,1), 10.5)),
00329   assert(equal(f(10.52,1), 10.5)),
00330   assert(equal(f(10.53,1), 10.5)),
00331   assert(equal(f(10.54,1), 10.5)),
00332   assert_bfloat_equal(f(10.55,1), 10.6, 16),
00333   assert_bfloat_equal(f(10.56,1), 10.6, 16),
00334   assert_bfloat_equal(f(10.57,1), 10.6, 16),
00335   assert_bfloat_equal(f(10.58,1), 10.6, 16),
00336   assert_bfloat_equal(f(10.59,1), 10.6, 16),
00337   assert_bfloat_equal(f(10.65,1), 10.6, 16),
00338   assert_bfloat_equal(f(10.66,1), 10.7, 16),
00339   true)$
00340 
00341 /* The part after the decimal point: */
00342 okltest_fractional_part(f) := (
00343   assert(f(2/4) = 1/2),
00344   assert_float_equal(f(3.4), 0.4),
00345   assert(f(-3-1/3) = 2/3),
00346   assert(f(-4+1/3) = 1/3),
00347   assert(f(3+1/3) = 1/3),
00348   true)$
00349 
00350 /* Remark: Given a fraction q, the numerator is nom(q), and
00351    the denominator is denom(q).
00352 */
00353 
00354 
00355 /* **********************
00356    * Integer partitions *
00357    **********************
00358 */
00359 
00360 okltest_ext_integer_partitions(f) := (
00361   assert(f(-1) = {}),
00362   assert(f(0) = {[]}),
00363   assert(f(1) = {[1]}),
00364   assert(f(2) = {[1,1],[2]}),
00365   assert(f(3) = {[1,1,1],[2,1],[3]}),
00366   assert(f(4) = {[1,1,1,1],[2,1,1],[3,1],[2,2],[4]}),
00367   true)$
00368 
00369 okltest_num_ext_integer_partitions(f) := (
00370   for n : 0 thru 6 do
00371     assert(f(n) = length(ext_integer_partitions(n))),
00372   true)$
00373 
00374 okltest_uinteger_partitions(f) := (
00375   assert(f(-1) = {}),
00376   assert(f(0) = {[]}),
00377   assert(f(1) = {[1]}),
00378   assert(f(2) = {[1,1],[2]}),
00379   assert(f(3) = {[1,1,1],[2,1],[1,2],[3]}),
00380   assert(f(4) = {[1,1,1,1],[2,1,1],[1,2,1],[1,1,2],[3,1],[1,3],[2,2],[4]}),
00381   true)$
00382 
00383 okltest_num_uinteger_partitions(f) := (
00384   for n : 0 thru 7 do
00385     assert(f(n) = length(uinteger_partitions(n))),
00386   true)$
00387 
00388 
00389 /* ***********************
00390    * Corrected functions *
00391    ***********************
00392 */
00393 
00394 okltest_pow(f) := block([x,y,a],
00395   assert(pow(0,0) = 1),
00396   assert(pow(0,1) = 0),
00397   assert(pow(1,0) = 1),
00398   assert(pow(1,1) = 1),
00399   assert(pow(2,2) = 4),
00400   assert(pow(2,-2) = 1/4),
00401   assert(pow(x,0) = 1),
00402   assert(pow(0,x) = if equal(x,0) then 1 else 0),
00403   assert(pow(x,1) = x),
00404   assert(pow(1,x) = 1),
00405   assert(pow(x,y) = ev(pow(x,y),uneval)),
00406   a : pow(x,y),
00407   assert(a = ev(pow(x,y),uneval)),
00408   y : 1,
00409   assert(ev(a) = x),
00410   y : 0,
00411   assert(ev(a) = 1),
00412   kill(y), 
00413   assert(a = ev(pow(x,y),uneval)),
00414   x : 0,
00415   assert(ev(a) = if equal(y,0) then 1 else 0),
00416   y : 0,
00417   assert(ev(a) = 1),
00418   true)$
00419