DSPython  00.03.03 — 25 juin 2012
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
finitec.py
Aller à la documentation de ce fichier.
1 #!/usr/bin/env python
2 # -*- coding: latin-1 -*-
3 ##\package DSPython.finitec Calcul finitésimal : opérateurs aux différences finies
4 #
5 # Cf. \htmlonly <a href="http://www.opimedia.be/Bruno_Marchal/index.htm#Nombres1" target="_blank">
6 # <tt>http://www.opimedia.be/Bruno_Marchal/index.htm#Nombres1</tt></a>\endhtmlonly
7 
8 ##\file
9 # Calcul finitésimal : opérateurs aux différences finies
10 #
11 # Cf. \htmlonly <a href="http://www.opimedia.be/Bruno_Marchal/index.htm#Nombres1" target="_blank">
12 # <tt>http://www.opimedia.be/Bruno_Marchal/index.htm#Nombres1</tt></a>\endhtmlonly
13 
14 # (c) Olivier Pirson --- DragonSoft
15 # http://www.opimedia.be/DS/
16 # Débuté le 4 février 2006
17 ####################################
18 from __future__ import division
19 from __future__ import print_function
20 
21 ## Date du dernier changement pour ce module
22 VERSION = 'finitec --- 2012 June 25'
23 
24 import numbers, types
25 
26 
27 
28 # ##############################
29 # Fonctions pour les fonctions #
30 ################################
31 ## Fonction c : c(x)
32 def c(x):
33  """Fonction c : c(x),
34  renvoie (-1)**d * 2**(2d) si 0 <= m < 2,
35  0 si 2 <= m < 3,
36  (-1)**(d + 1) * 2**(2d + 1) si 3 <= m < 4
37  avec d, m = divmod(x, 4)
38 
39  Pour les n naturels, correspond à [OEIS A146559]
40 
41  Pre: x: Number
42 
43  Result: Number
44 
45  O(f) = ..."""
46  assert isinstance(x, numbers.Number), x
47 
48  d, m = divmod(x, 4)
49 
50  assert 0 <= m < 4
51 
52  sign = (1 if d%2 == 0
53  else -1)
54 
55  return ((4**d)*sign if m < 2
56  else (0 if m < 3
57  else (4**d)*(-2)*sign))
58 
59 
60 ## Fonction identité : id(x)
61 def id(x):
62  """Fonction identité : id(x),
63  renvoie x
64 
65  Pre: x: Number
66 
67  Result: Number
68 
69  O(f) = 1"""
70  assert isinstance(x, numbers.Number), x
71 
72  return x
73 
74 
75 ## Fonction s : s(x)
76 def s(x):
77  """Fonction s : s(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)
82 
83  Pour les n naturels, correspond à [OEIS A009545]
84 
85  Pre: x: Number
86 
87  Result: Number
88 
89  O(f) = ..."""
90  assert isinstance(x, numbers.Number), x
91 
92  d, m = divmod(x, 4)
93 
94  assert 0 <= m < 4
95 
96  sign = (1 if d%2 == 0
97  else -1)
98 
99  return (0 if m < 1
100  else ((4**d)*sign if m < 2
101  else (4**d)*2*sign))
102 
103 
104 ## Fonction signe : sign(x)
105 def sign(x):
106  """Fonction signe : signe(x),
107  renvoie -1 si x < 0,
108  0 si x == 0,
109  1 si x > 0
110 
111  Pre: x: Number
112 
113  Result: -1, 0 ou 1
114 
115  O(f) = 1"""
116  assert isinstance(x, numbers.Number), x
117 
118  return (1 if x > 0
119  else (-1 if x < 0
120  else 0))
121 
122 
123 
124 # ###############################
125 # Fonctions pour les opérateurs #
126 #################################
127 ## Opérateur de différence (progressive) : Delta<sub>alpha</sub> f
128 def diff(f, alpha=1):
129  """Opérateur de différence (progressive) : Delta_alpha f,
130  renvoie la fonction x -> f(x + alpha) - f(x)
131 
132  Pre: f: fonction : Number -> (Number ou None)
133  alpha: Number
134 
135  Result: fonction : Number -> (Number ou None)
136 
137  O(f, alpha) = ..."""
138  assert isinstance(f, types.FunctionType), type(f)
139  assert isinstance(alpha, numbers.Number), alpha
140 
141  return lambda x : diff_x(f, x, alpha=alpha)
142 
143 
144 ## Delta<sub>alpha</sub> f(x) == f(x + alpha) - f(x)
145 def diff_x(f, x, alpha=1):
146  """Différence (progressive) évaluée en x :
147  Delta_alpha f(x) == f(x + alpha) - f(x)
148 
149  Pre: f: fonction : Number -> (Number ou None)
150  x: Number
151  alpha: Number
152 
153  Result: Number ou None
154 
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
159 
160  fx = f(x)
161  if fx != None:
162  fxalpha = f(x + alpha)
163  if fxalpha != None:
164  return fxalpha - fx
165  return None
166 
167 
168 ## Opérateur de sommation (progressive) : sum<sub>alpha</sub> f
169 def sum(f, alpha=1):
170  """Opérateur de sommation (progressive) : sum_alpha f,
171  renvoie la fonction x -> sum_alpha f(x)
172 
173  Pre: f: fonction : Number -> (Number ou None)
174  alpha: Number != 0
175 
176  Result: fonction : Number -> (Number ou None)
177 
178  O(f, alpha) = ..."""
179  assert isinstance(f, types.FunctionType), type(f)
180  assert isinstance(alpha, numbers.Number), alpha
181  assert alpha != 0, alpha
182 
183  return lambda x : sum_x(f, x, alpha=alpha)
184 
185 
186 ## sum<sub>alpha</sub> f(x)
187 def sum_x(f, x, alpha=1):
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
197 
198  Pre: f: fonction : Number -> (Number ou None)
199  x: Number
200  alpha: Integral != 0
201 
202  Result: Number ou None
203 
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
209 
210  if alpha > 0:
211  if x > 0:
212  s = 0
213  x -= alpha
214  while x >= 0:
215  fx = f(x)
216  if fx == None:
217  return None
218  s += fx
219  x -= alpha
220  return s
221  elif x < 0:
222  s = 0
223  while x < 0:
224  fx = f(x)
225  if fx == None:
226  return None
227  s -= fx
228  x += alpha
229  return s
230  else:
231  return 0
232  else: # alpha < 0
233  if x > 0:
234  s = 0
235  while x > 0:
236  fx = f(x)
237  if fx == None:
238  return None
239  s -= fx
240  x += alpha
241  return s
242  elif x < 0:
243  s = 0
244  x -= alpha
245  while x <= 0:
246  fx = f(x)
247  if fx == None:
248  return None
249  s += fx
250  x -= alpha
251  return s
252  else:
253  return 0
254 
255 
256 ## Opérateur de réflexion (symétrie horizontale) : check f
257 def sym(f):
258  """Opérateur de réflexion (symétrie horizontale) : check f,
259  renvoie la fonction x -> f(-x)
260 
261  Pre: f: fonction : Number -> (Number ou None)
262 
263  Result: fonction : Number -> (Number ou None)
264 
265  O(f) = ..."""
266  assert isinstance(f, types.FunctionType), type(f)
267 
268  return lambda x : trans_x(f, x)
269 
270 
271 ## check f(x) == f(-x)
272 def sym_x(f, x):
273  """Réflexion (symétrie horizontale) évaluée en x : check f(x) == f(-x)
274 
275  Pre: f: fonction : Number -> (Number ou None)
276  x: Number
277 
278  Result: Number ou None
279 
280  O(f, x) = ..."""
281  assert isinstance(f, types.FunctionType), type(f)
282  assert isinstance(x, numbers.Number), x
283 
284  return f(-x)
285 
286 
287 ## Opérateur de translation (horizontale) : E<sub>alpha</sub> f
288 def trans(f, alpha=1):
289  """Opérateur de translation (horizontale) : E_alpha f,
290  renvoie la fonction x -> f(x + alpha)
291 
292  Pre: f: fonction : Number -> (Number ou None)
293  alpha: Number
294 
295  Result: fonction : Number -> (Number ou None)
296 
297  O(f, alpha) = ..."""
298  assert isinstance(f, types.FunctionType), type(f)
299  assert isinstance(alpha, numbers.Number), alpha
300 
301  return lambda x : trans_x(f, x, alpha=alpha)
302 
303 
304 ## E<sub>alpha</sub> f(x) == f(x + alpha)
305 def trans_x(f, x, alpha=1):
306  """Translation (horizontale) évaluée en x : E_alpha f(x) == f(x + alpha)
307 
308  Pre: f: fonction : Number -> (Number ou None)
309  x: Number
310  alpha: Number
311 
312  Result: Number ou None
313 
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
318 
319  return f(x + alpha)
320 
321 
322 ## Opérateur de translation verticale : V<sub>alpha</sub> f
323 def trans_vert(f, alpha=1):
324  """Opérateur de translation verticale : V_alpha f,
325  renvoie la fonction x -> f(x) + alpha
326 
327  Pre: f: fonction : Number -> (Number ou None)
328  alpha: Number
329 
330  Result: fonction : Number -> (Number ou None)
331 
332  O(f, alpha) = ..."""
333  assert isinstance(f, types.FunctionType), type(f)
334  assert isinstance(alpha, numbers.Number), alpha
335 
336  return lambda x : trans_vert_x(f, x, alpha=alpha)
337 
338 
339 ## V<sub>alpha</sub> f(x) == f(x) + alpha
340 def trans_vert_x(f, x, alpha=1):
341  """Translation verticale évaluée en x : V_alpha f(x) == f(x) + alpha
342 
343  Pre: f: fonction : Number -> (Number ou None)
344  x: Number
345  alpha: Number
346 
347  Result: Number ou None
348 
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
353 
354  fx = f(x)
355  return (fx + alpha if fx != None
356  else None)
357 
358 
359 
360 # ######\cond MAINTEST
361 # Main #
362 ########
363 if __name__ == '__main__':
364  def main_test():
365  """Test du module"""
366  import sys
367 
368  import DSPython.debug as debug
369  import DSPython.numbernone as numbernone
370 
371  debug.test_begin(VERSION, __debug__)
372 
373 
374  def _eq(x, y, prec=2**32):
375  """Renvoie True si x == y (avec une précision relative de 1/prec)
376  False sinon"""
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)
379  else x == y)
380 
381 
382 
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)
393 
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
398 
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
402 
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()
408 
409 
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()
415 
416 
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)
427 
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
432 
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
436 
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
440 
441  import math
442 
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()
447 
448 
449  print('sign()...', end=''); sys.stdout.flush()
450  for x in range(-100, 0):
451  assert sign(x) < 0, (x, sign(x))
452  assert sign(x/10) < 0, (x, sign(x/10))
453  assert sign(0) == 0, sign(0)
454  for x in range(1, 100):
455  assert sign(x) > 0, (x, sign(x))
456  assert sign(x/10) > 0, (x, sign(x/10))
457  print('ok'); sys.stdout.flush()
458 
459 
460 
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))
465 
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))
468 
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)
471 
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))
477 
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))
482 
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))
487 
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)
492 
493  import decimal
494 
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)),
500  numbernone.mul(k,
501  numbernone.falling_factorial_pow(decimal.Decimal(x),
502  k - 1))), \
503  (x, k,
504  diff(lambda x:
505  numbernone.falling_factorial_pow(x, k))(decimal.Decimal(x)),
506  numbernone.mul(k, numbernone.falling_factorial_pow(decimal.Decimal(x),
507  k - 1)))
508  print('???', end='')
509  if debug.assertspeed < debug.ASSERT_NORMAL:
510  print(debug.assertspeed_str(), end='')
511  print('ok'); sys.stdout.flush()
512 
513 
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))
519 
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))
522 
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)
526 
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))
532 
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))
538 
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))
543 
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)
548 
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),
552  decimal.Decimal(x)),
553  numbernone.mul(k,
554  numbernone.falling_factorial_pow(decimal.Decimal(x),
555  k - 1))),\
556  (x, k,
557  diff_x(lambda x: numbernone.falling_factorial_pow(x, k),
558  decimal.Decimal(x)),
559  numbernone.mul(k, numbernone.falling_factorial_pow(decimal.Decimal(x),
560  k - 1)))
561  print('???', end='')
562  if debug.assertspeed < debug.ASSERT_NORMAL:
563  print(debug.assertspeed_str(), end='')
564  print('ok'); sys.stdout.flush()
565 
566 
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)
575  print('???', end='')
576  if debug.assertspeed < debug.ASSERT_NORMAL:
577  print(debug.assertspeed_str(), end='')
578  print('ok'); sys.stdout.flush()
579 
580 
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)
589  print('???', end='')
590  if debug.assertspeed < debug.ASSERT_NORMAL:
591  print(debug.assertspeed_str(), end='')
592  print('ok'); sys.stdout.flush()
593 
594 
595  print('sym()...', end=''); sys.stdout.flush()
596  print('???', end='')
597  print('ok'); sys.stdout.flush()
598 
599 
600  print('sym_x()...', end=''); sys.stdout.flush()
601  print('???', end='')
602  print('ok'); sys.stdout.flush()
603 
604 
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()
617 
618 
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()
632 
633 
634  print('trans_vert()...', end=''); sys.stdout.flush()
635  for x in range(-1000, 1000):
636  assert trans_vert(lambda x: x)(x) == x + 1, (x, trans_vert(lambda x: x)(x), x + 1)
637  assert trans_vert(lambda x: x, -1)(x) == x - 1, \
638  (x, trans_vert(lambda x: x, -1)(x), x - 1)
639  assert trans_vert(lambda x: x, 2.5)(x) == x + 2.5, \
640  (x, 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()
648 
649 
650  print('trans_vert_x()...', end=''); sys.stdout.flush()
651  for x in range(-1000, 1000):
652  assert trans_vert_x(lambda x: x, x) == x + 1, (x, trans_vert_x(lambda x: x, x), x + 1)
653  assert trans_vert_x(lambda x: x, x, -1) == x - 1, \
654  (x, trans_vert_x(lambda x: x, x, -1), x - 1)
655  assert trans_vert_x(lambda x: x, x, 2.5) == x + 2.5, \
656  (x, trans_vert_x(lambda x: x, x, 2.5), x + 2.5)
657  assert trans_vert_x(lambda x: x*x, x, 2.5) == x*x + 2.5, \
658  (x, 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, \
660  (x, 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, \
662  (x, trans_vert_x(lambda x: 3*x, x/2, 2.5), 3*x/2 + 2.5)
663  print('ok'); sys.stdout.flush()
664  debug.test_end()
665 
666  main_test()
667 ##\endcond MAINTEST