18 from __future__
import division
19 from __future__
import print_function
22 VERSION =
'finitec --- 2012 June 25'
34 renvoie (-1)**d * 2**(2d) si 0 <= m < 2,
36 (-1)**(d + 1) * 2**(2d + 1) si 3 <= m < 4
37 avec d, m = divmod(x, 4)
39 Pour les n naturels, correspond à [OEIS A146559]
46 assert isinstance(x, numbers.Number), x
55 return ((4**d)*sign
if m < 2
57 else (4**d)*(-2)*sign))
62 """Fonction identité : id(x),
70 assert isinstance(x, numbers.Number), x
78 renvoie 0 si 0 <= m < 1,
79 (-1)**d * 2**(2d) si 1 <= m < 2,
80 (-1)**d * 2**(2d + 1) si 2 <= m < 4
81 avec d, m = divmod(x, 4)
83 Pour les n naturels, correspond à [OEIS A009545]
90 assert isinstance(x, numbers.Number), x
100 else ((4**d)*sign
if m < 2
106 """Fonction signe : signe(x),
116 assert isinstance(x, numbers.Number), x
129 """Opérateur de différence (progressive) : Delta_alpha f,
130 renvoie la fonction x -> f(x + alpha) - f(x)
132 Pre: f: fonction : Number -> (Number ou None)
135 Result: fonction : Number -> (Number ou None)
138 assert isinstance(f, types.FunctionType), type(f)
139 assert isinstance(alpha, numbers.Number), alpha
141 return lambda x :
diff_x(f, x, alpha=alpha)
146 """Différence (progressive) évaluée en x :
147 Delta_alpha f(x) == f(x + alpha) - f(x)
149 Pre: f: fonction : Number -> (Number ou None)
153 Result: Number ou None
155 O(f, x, alpha) = ..."""
156 assert isinstance(f, types.FunctionType), type(f)
157 assert isinstance(x, numbers.Number), x
158 assert isinstance(alpha, numbers.Number), alpha
162 fxalpha = f(x + alpha)
170 """Opérateur de sommation (progressive) : sum_alpha f,
171 renvoie la fonction x -> sum_alpha f(x)
173 Pre: f: fonction : Number -> (Number ou None)
176 Result: fonction : Number -> (Number ou None)
179 assert isinstance(f, types.FunctionType), type(f)
180 assert isinstance(alpha, numbers.Number), alpha
181 assert alpha != 0, alpha
183 return lambda x :
sum_x(f, x, alpha=alpha)
188 """Sommation (progressive) évaluée en x : sum_alpha f(x) ==
189 f(x%alpha) + f(x%alpha + alpha) + f(x%alpha + 2*alpha)
190 + ... + f(x - alpha) si alpha > 0 et x >= 0,
191 -f(x) - ... - f(x%alpha - 3*alpha) - f(x%alpha - 2*alpha)
192 - f(x%alpha - alpha) si alpha > 0 et x <= 0,
193 -f(x%alpha - alpha) - f(x%alpha - 2*alpha) - f(x%alpha - 3*alpha)
194 - ... - f(x) si alpha < 0 et x >= 0,
195 f(x - alpha) + ... + f(x%alpha + 2*alpha) + f(x%alpha + alpha)
196 + f(x%alpha) si alpha < 0 et x <= 0
198 Pre: f: fonction : Number -> (Number ou None)
202 Result: Number ou None
204 O(f, x, alpha) = ..."""
205 assert isinstance(f, types.FunctionType), type(f)
206 assert isinstance(x, numbers.Number), type(x)
207 assert isinstance(alpha, numbers.Integral), type(alpha)
208 assert alpha != 0, alpha
258 """Opérateur de réflexion (symétrie horizontale) : check f,
259 renvoie la fonction x -> f(-x)
261 Pre: f: fonction : Number -> (Number ou None)
263 Result: fonction : Number -> (Number ou None)
266 assert isinstance(f, types.FunctionType), type(f)
268 return lambda x :
trans_x(f, x)
273 """Réflexion (symétrie horizontale) évaluée en x : check f(x) == f(-x)
275 Pre: f: fonction : Number -> (Number ou None)
278 Result: Number ou None
281 assert isinstance(f, types.FunctionType), type(f)
282 assert isinstance(x, numbers.Number), x
289 """Opérateur de translation (horizontale) : E_alpha f,
290 renvoie la fonction x -> f(x + alpha)
292 Pre: f: fonction : Number -> (Number ou None)
295 Result: fonction : Number -> (Number ou None)
298 assert isinstance(f, types.FunctionType), type(f)
299 assert isinstance(alpha, numbers.Number), alpha
301 return lambda x :
trans_x(f, x, alpha=alpha)
306 """Translation (horizontale) évaluée en x : E_alpha f(x) == f(x + alpha)
308 Pre: f: fonction : Number -> (Number ou None)
312 Result: Number ou None
314 O(f, x, alpha) = ..."""
315 assert isinstance(f, types.FunctionType), type(f)
316 assert isinstance(x, numbers.Number), x
317 assert isinstance(alpha, numbers.Number), alpha
324 """Opérateur de translation verticale : V_alpha f,
325 renvoie la fonction x -> f(x) + alpha
327 Pre: f: fonction : Number -> (Number ou None)
330 Result: fonction : Number -> (Number ou None)
333 assert isinstance(f, types.FunctionType), type(f)
334 assert isinstance(alpha, numbers.Number), alpha
341 """Translation verticale évaluée en x : V_alpha f(x) == f(x) + alpha
343 Pre: f: fonction : Number -> (Number ou None)
347 Result: Number ou None
349 O(f, x, alpha) = ..."""
350 assert isinstance(f, types.FunctionType), type(f)
351 assert isinstance(x, numbers.Number), x
352 assert isinstance(alpha, numbers.Number), alpha
355 return (fx + alpha
if fx !=
None
363 if __name__ ==
'__main__':
371 debug.test_begin(VERSION, __debug__)
374 def _eq(x, y, prec=2**32):
375 """Renvoie True si x == y (avec une précision relative de 1/prec)
377 return ((((x >= 0)
and (y >= 0))
or ((x <= 0)
and (y <= 0)))
378 and (abs(x - y) <= 1/prec)
if (x !=
None)
and (y !=
None)
383 print(
'c()...', end=
''); sys.stdout.flush()
384 assert c(-3) == -0.25,
c(-3)
385 assert c(-2) == 0,
c(-2)
386 assert c(-1) == 0.5,
c(-1)
387 assert c(0) == 1,
c(0)
388 assert c(1) == 1,
c(1)
389 assert c(2) == 0,
c(2)
390 assert c(3) == -2,
c(3)
391 assert c(4) == -4,
c(4)
392 assert c(5) == -4,
c(5)
394 for n
in range(-100, 100):
395 assert(
c(n + 4) == -4*
c(n)), n
396 assert(
c(n + 3) == 4*
c(n + 2) - 6*
c(n + 1) + 4*
c(n)), n
397 assert(
c(n + 2) == 2*
c(n + 1) - 2*
c(n)), n
399 assert(
c(n/10 + 4) == -4*
c(n/10)), n/10
400 assert(
c(n/10 + 3) == 4*
c(n/10 + 2) - 6*
c(n/10 + 1) + 4*
c(n/10)), n/10
401 assert(
c(n/10 + 2) == 2*
c(n/10 + 1) - 2*
c(n/10)), n/10
403 assert(
c(n) == ((1 + 1j)**n).real), n
404 for n
in range(-90, 100):
405 assert(
c(n) == ((1 + 1j)**(n - 2) - (1 - 1j)**(n - 2))*1j), \
406 (n,
c(n), ((1 + 1j)**(n - 2) - (1 - 1j)**(n - 2))*1j)
407 print(
'ok'); sys.stdout.flush()
410 print(
'id()...', end=
''); sys.stdout.flush()
411 for x
in range(-100, 100):
412 assert id(x) == x, (x,
id(x))
413 assert id(x/10) == x/10, (x,
id(x/10))
414 print(
'ok'); sys.stdout.flush()
417 print(
's()...', end=
''); sys.stdout.flush()
418 assert s(-3) == -0.25,
s(-3)
419 assert s(-2) == -0.5,
s(-2)
420 assert s(-1) == -0.5,
s(-1)
421 assert s(0) == 0,
s(0)
422 assert s(1) == 1,
s(1)
423 assert s(2) == 2,
s(2)
424 assert s(3) == 2,
s(3)
425 assert s(4) == 0,
s(4)
426 assert s(5) == -4,
s(5)
428 for n
in range(-100, 100):
429 assert(
s(n + 4) == -4*
s(n)), n
430 assert(
s(n + 3) == 4*
s(n + 2) - 6*
s(n + 1) + 4*
s(n)), n
431 assert(
s(n + 2) == 2*
s(n + 1) - 2*
s(n)), n
433 assert(
s(n/10 + 4) == -4*
s(n/10)), n/10
434 assert(
s(n/10 + 3) == 4*
s(n/10 + 2) - 6*
s(n/10 + 1) + 4*
s(n/10)), n/10
435 assert(
s(n/10 + 2) == 2*
s(n/10 + 1) - 2*
s(n/10)), n/10
437 assert(
s(n) == ((1 + 1j)**n).imag), n
438 for n
in range(-90, 100):
439 assert(
s(n) == (1 + 1j)**(n - 2) + (1 - 1j)**(n - 2)), n
443 for n
in range(-30, 30):
444 assert(_eq(
s(n), 2**(n/2) * math.sin(math.pi*n/4))), \
445 (n,
s(n), 2**(n/2) * math.sin(math.pi*n/4))
446 print(
'ok'); sys.stdout.flush()
449 print(
'sign()...', end=
''); sys.stdout.flush()
450 for x
in range(-100, 0):
452 assert sign(x/10) < 0, (x,
sign(x/10))
454 for x
in range(1, 100):
456 assert sign(x/10) > 0, (x,
sign(x/10))
457 print(
'ok'); sys.stdout.flush()
461 print(
'diff()...', end=
''); sys.stdout.flush()
462 for x
in range(-500, 500):
463 assert diff(
lambda x: x)(x) == 1, (x,
diff(
lambda x: x)(x))
464 assert diff(
lambda x: x)(x/2) == 1, (x/2,
diff(
lambda x: x)(x/2))
466 assert diff(
lambda x: 3*x)(x) == 3, (x,
diff(
lambda x: 3*x)(x))
467 assert diff(
lambda x: 3*x)(x/2) == 3, (x/2,
diff(
lambda x: 3*x)(x/2))
469 assert diff(
lambda x: x*x)(x) == 2*x + 1, (x,
diff(
lambda x: x*x)(x), 2*x + 1)
470 assert diff(
lambda x: x*x)(x/2) == x + 1, (x/2,
diff(
lambda x: x*x)(x/2), x + 1)
472 for k
in range(1, 10):
473 assert diff(
lambda x: numbernone.falling_factorial_pow(x, k))(x) \
474 == k * numbernone.falling_factorial_pow(x, k - 1), \
475 (x, k,
diff(
lambda x: numbernone.falling_factorial_pow(x, k))(x),
476 k * numbernone.falling_factorial_pow(x, k - 1))
478 for alpha
in range(5):
479 assert diff(
lambda x: x, alpha)(x) == alpha, (x, alpha,
diff(
lambda x: x, alpha)(x))
480 assert diff(
lambda x: x, alpha)(x/2) == alpha, \
481 (x/2, alpha,
diff(
lambda x: x, alpha)(x/2))
483 assert diff(
lambda x: 3*x, alpha)(x) == 3*alpha, \
484 (x, alpha,
diff(
lambda x: 3*x, alpha)(x))
485 assert diff(
lambda x: 3*x, alpha)(x/2) == 3*alpha, \
486 (x/2, alpha,
diff(
lambda x: 3*x, alpha)(x/2))
488 assert diff(
lambda x: x*x, alpha)(x) == 2*x*alpha + alpha*alpha, \
489 (x,
diff(
lambda x: x*x, alpha)(x), 2*x*alpha + alpha*alpha)
490 assert diff(
lambda x: x*x, alpha)(x/2) == x*alpha + alpha*alpha, \
491 (x/2,
diff(
lambda x: x*x, alpha)(x/2), x*alpha + alpha*alpha)
495 for x
in range(-50
if debug.assertspeed >= debug.ASSERT_NORMAL
else -25,
496 50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 25):
497 for k
in range(-5, 0):
498 assert _eq(
diff(
lambda x:
499 numbernone.falling_factorial_pow(x, k))(decimal.Decimal(x)),
501 numbernone.falling_factorial_pow(decimal.Decimal(x),
505 numbernone.falling_factorial_pow(x, k))(decimal.Decimal(x)),
506 numbernone.mul(k, numbernone.falling_factorial_pow(decimal.Decimal(x),
509 if debug.assertspeed < debug.ASSERT_NORMAL:
510 print(debug.assertspeed_str(), end=
'')
511 print(
'ok'); sys.stdout.flush()
514 print(
'diff_x()...', end=
''); sys.stdout.flush()
515 for x
in range(-500
if debug.assertspeed >= debug.ASSERT_NORMAL
else -100,
516 500
if debug.assertspeed >= debug.ASSERT_NORMAL
else 100):
517 assert diff_x(
lambda x: x, x) == 1, (x,
diff_x(
lambda x: x, x))
518 assert diff_x(
lambda x: x, x/2) == 1, (x/2,
diff_x(
lambda x: x, x/2))
520 assert diff_x(
lambda x: 3*x, x) == 3, (x,
diff_x(
lambda x: 3*x, x))
521 assert diff_x(
lambda x: 3*x, x/2) == 3, (x/2,
diff_x(
lambda x: 3*x, x/2))
523 assert diff_x(
lambda x: x*x, x) == 2*x + 1, (x,
diff_x(
lambda x: x*x, x), 2*x + 1)
524 assert diff_x(
lambda x: x*x, x/2) == x + 1, \
525 (x/2,
diff_x(
lambda x: x*x, x/2), x + 1)
527 for k
in range(1, 10):
528 assert diff_x(
lambda x: numbernone.falling_factorial_pow(x, k), x) \
529 == k * numbernone.falling_factorial_pow(x, k - 1), \
530 (x, k,
diff_x(
lambda x: numbernone.falling_factorial_pow(x, k), x),
531 k * numbernone.falling_factorial_pow(x, k - 1))
533 for alpha
in range(5):
534 assert diff_x(
lambda x: x, x, alpha,) == alpha, \
535 (x, alpha,
diff_x(
lambda x: x, x, alpha))
536 assert diff_x(
lambda x: x, x/2, alpha) == alpha, \
537 (x/2, alpha,
diff_x(
lambda x: x, x/2, alpha))
539 assert diff_x(
lambda x: 3*x, x, alpha) == 3*alpha, \
540 (x, alpha,
diff_x(
lambda x: 3*x, x, alpha))
541 assert diff_x(
lambda x: 3*x, x/2, alpha) == 3*alpha, \
542 (x/2, alpha,
diff_x(
lambda x: 3*x, x/2, alpha))
544 assert diff_x(
lambda x: x*x, x, alpha) == 2*x*alpha + alpha*alpha, \
545 (x,
diff_x(
lambda x: x*x, x, alpha), 2*x*alpha + alpha*alpha)
546 assert diff_x(
lambda x: x*x, x/2, alpha) == x*alpha + alpha*alpha, \
547 (x/2,
diff_x(
lambda x: x*x, x/2, alpha), x*alpha + alpha*alpha)
549 for x
in range(-50, 50):
550 for k
in range(-5, 0):
551 assert _eq(
diff_x(
lambda x: numbernone.falling_factorial_pow(x, k),
554 numbernone.falling_factorial_pow(decimal.Decimal(x),
557 diff_x(
lambda x: numbernone.falling_factorial_pow(x, k),
559 numbernone.mul(k, numbernone.falling_factorial_pow(decimal.Decimal(x),
562 if debug.assertspeed < debug.ASSERT_NORMAL:
563 print(debug.assertspeed_str(), end=
'')
564 print(
'ok'); sys.stdout.flush()
567 print(
'sum()...', end=
''); sys.stdout.flush()
568 for x
in range(-1000
if debug.assertspeed >= debug.ASSERT_NORMAL
else -100,
569 1000
if debug.assertspeed >= debug.ASSERT_NORMAL
else 100):
570 assert sum(
lambda x: x) (x) == (x - 1)*x/2, (x,
sum(
lambda x: x)(x), (x - 1)*x/2)
571 assert sum(
lambda x: x + 1)(x) == x*(x + 1)/2, \
572 (x,
sum(
lambda x: x + 1)(x), x*(x + 1)/2)
573 assert sum(
lambda x: x, -1)(x) == -x*(x + 1)/2, \
574 (x,
sum(
lambda x: x, -1)(x), -x*(x + 1)/2)
576 if debug.assertspeed < debug.ASSERT_NORMAL:
577 print(debug.assertspeed_str(), end=
'')
578 print(
'ok'); sys.stdout.flush()
581 print(
'sum_x()...', end=
''); sys.stdout.flush()
582 for x
in range(-1000
if debug.assertspeed >= debug.ASSERT_NORMAL
else -100,
583 1000
if debug.assertspeed >= debug.ASSERT_NORMAL
else 100):
584 assert sum_x(
lambda x: x, x) == (x - 1)*x/2, (x,
sum_x(
lambda x: x, x), (x - 1)*x/2)
585 assert sum_x(
lambda x: x + 1, x) == x*(x + 1)/2, \
586 (x,
sum_x(
lambda x: x + 1, x), x*(x + 1)/2)
587 assert sum_x(
lambda x: x, x, -1) == -x*(x + 1)/2, \
588 (x,
sum_x(
lambda x: x, x, -1), -x*(x + 1)/2)
590 if debug.assertspeed < debug.ASSERT_NORMAL:
591 print(debug.assertspeed_str(), end=
'')
592 print(
'ok'); sys.stdout.flush()
595 print(
'sym()...', end=
''); sys.stdout.flush()
597 print(
'ok'); sys.stdout.flush()
600 print(
'sym_x()...', end=
''); sys.stdout.flush()
602 print(
'ok'); sys.stdout.flush()
605 print(
'trans()...', end=
''); sys.stdout.flush()
606 for x
in range(-1000, 1000):
607 assert trans(
lambda x: x)(x) == x + 1, (x,
trans(
lambda x: x)(x), x + 1)
608 assert trans(
lambda x: x, -1)(x) == x - 1, (x,
trans(
lambda x: x, -1)(x), x - 1)
609 assert trans(
lambda x: x, 2.5)(x) == x + 2.5, (x,
trans(
lambda x: x, 2.5)(x), x + 2.5)
610 assert trans(
lambda x: x*x, 2.5)(x) == (x + 2.5)**2, \
611 (x,
trans(
lambda x: x*x, 2.5)(x), (x + 2.5)**2)
612 assert trans(
lambda x: 3*x, 2.5)(x) == 3*(x + 2.5), \
613 (x,
trans(
lambda x: 3*x, 2.5)(x), 3*(x + 2.5))
614 assert trans(
lambda x: 3*x, 2.5)(x/2) == 3*(x/2 + 2.5), \
615 (x,
trans(
lambda x: 3*x, 2.5)(x/2), 3*(x/2 + 2.5))
616 print(
'ok'); sys.stdout.flush()
619 print(
'trans_x()...', end=
''); sys.stdout.flush()
620 for x
in range(-1000, 1000):
621 assert trans_x(
lambda x: x, x) == x + 1, (x,
trans_x(
lambda x: x, x), x + 1)
622 assert trans_x(
lambda x: x, x, -1) == x - 1, (x,
trans_x(
lambda x: x, x, -1), x - 1)
623 assert trans_x(
lambda x: x, x, 2.5) == x + 2.5, \
624 (x,
trans_x(
lambda x: x, x, 2.5), x + 2.5)
625 assert trans_x(
lambda x: x*x, x, 2.5) == (x + 2.5)**2, \
626 (x,
trans_x(
lambda x: x*x, x, 2.5), (x + 2.5)**2)
627 assert trans_x(
lambda x: 3*x, x, 2.5) == 3*(x + 2.5), \
628 (x,
trans_x(
lambda x: 3*x, x, 2.5), 3*(x + 2.5))
629 assert trans_x(
lambda x: 3*x, x/2, 2.5) == 3*(x/2 + 2.5), \
630 (x,
trans_x(
lambda x: 3*x, x/2, 2.5), 3*(x/2 + 2.5))
631 print(
'ok'); sys.stdout.flush()
634 print(
'trans_vert()...', end=
''); sys.stdout.flush()
635 for x
in range(-1000, 1000):
637 assert trans_vert(
lambda x: x, -1)(x) == x - 1, \
639 assert trans_vert(
lambda x: x, 2.5)(x) == x + 2.5, \
641 assert trans_vert(
lambda x: x*x, 2.5)(x) == x*x + 2.5, \
642 (x,
trans_vert(
lambda x: x*x, 2.5)(x), x*x + 2.5)
643 assert trans_vert(
lambda x: 3*x, 2.5)(x) == 3*x + 2.5, \
644 (x,
trans_vert(
lambda x: 3*x, 2.5)(x), 3*x + 2.5)
645 assert trans_vert(
lambda x: 3*x, 2.5)(x/2) == 3*x/2 + 2.5, \
646 (x,
trans_vert(
lambda x: 3*x, 2.5)(x/2), 3*x/2 + 2.5)
647 print(
'ok'); sys.stdout.flush()
650 print(
'trans_vert_x()...', end=
''); sys.stdout.flush()
651 for x
in range(-1000, 1000):
657 assert trans_vert_x(
lambda x: x*x, x, 2.5) == x*x + 2.5, \
659 assert trans_vert_x(
lambda x: 3*x, x, 2.5) == 3*x + 2.5, \
661 assert trans_vert_x(
lambda x: 3*x, x/2, 2.5) == 3*x/2 + 2.5, \
663 print(
'ok'); sys.stdout.flush()