This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fsm_fourier.pyx
2337 lines (1878 loc) · 81 KB
/
fsm_fourier.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Fourier Coefficients
Introduction
============
Let `T` be a complete deterministic transducer reading the `q`-ary
digit expansion of a random integer `n` with `0\le n<N`.
Then the expected sum of the output labels of the transducer is
`e_T \log_q N + \Phi(\log_q N) + o(1)` for some periodic fluctuation
`\Phi` and some constant `e_T`.
This module computes the Fourier Coefficients of `\Phi`, following
[HKP2015]_.
Contents
========
:class:`FSMFourier`
-------------------
The class :class:`FSMFourier` is the main class of this module, it
precomputes values needed for all Fourier coefficients and then offers
the method :meth:`~FSMFourier.FourierCoefficient`.
.. csv-table::
:class: contentstable
:widths: 30, 70
:delim: |
:meth:`~FSMFourier.FourierCoefficient` | Compute a Fourier Coefficient
:meth:`~FSMFourier.plot_fluctuation` | Generate a plot of the fluctuation
:meth:`~FSMFourier.plot_fluctuation_asymptote` | Generate an asymptote plot of the fluctuation
:meth:`~FSMFourier.b0` | Compute the final output word
:class:`FSMFourierComponent`
------------------------------
Data corresponding to a final component of the transducer is stored in
:class:`FSMFourierComponent`. This mainly comprises eigenvectors.
.. csv-table::
:class: contentstable
:widths: 30, 70
:delim: |
:meth:`~FSMFourierComponent.mu_prime` | Derivative `\mu_{j0}'(0)`
:meth:`~FSMFourierComponent.a` | Constant `a_j`
:meth:`~FSMFourierComponent.coefficient_lambda` | Constant `\lambda_j`
:meth:`~FSMFourierComponent.right_eigenvectors` | Right eigenvectors `\mathbf{v}_{jk}(0)`
:meth:`~FSMFourierComponent.v_prime` | Derivative of the right eigenvector `\mathbf{v}'_{jk}(0)`
:meth:`~FSMFourierComponent.left_eigenvectors` | Left eigenvectors
:meth:`~FSMFourierComponent.w` | Scaled left eigenvectors `\mathbf{w}_{jk}(0)`
:meth:`~FSMFourierComponent.w_prime` | Derivative of the left eigenvector `\mathbf{w}'_{jk}(0)`
:meth:`~FSMFourierComponent.w_ell` | Left eigenvector to given eigenvalue.
:class:`FSMFourierCache`
------------------------
This is a mostly internal class speeding up some of the computation.
.. csv-table::
:class: contentstable
:widths: 30, 70
:delim: |
:meth:`~FSMFourierCache.b` | Compute `\mathbf{b}(r)`.
:meth:`~FSMFourierCache.fluctuation_empirical` | Compute the fluctuation empirically.
:meth:`~FSMFourierCache.fluctuation_fourier` | Compute the fluctuation via the Fourier series.
Example
=======
::
sage: function('f')
f
sage: var('n')
n
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = transducers.Recursion([
....: f(2*n + 1) == f(n) + 1,
....: f(2*n) == f(n),
....: f(0) == 0],
....: 2, f, n)
sage: F = FSMFourier(T) # optional - arb
sage: F.FourierCoefficient(0) # optional - arb
-0.1455994557084? + 0.?e-16*I
sage: F.FourierCoefficient(10) # optional - arb
0.0016818573864? + 0.0003201978624?*I
AUTHORS:
- Clemens Heuberger (2014-08-14--2014-10-13): initial version
- Sara Kropf (2014-10-13--2014-10-21): corrections and 0th Fourier Coefficient
- Clemens Heuberger (2014-10-23--2014-10-26): Optimization and Preparation for Sage
ACKNOWLEDGEMENT:
- Clemens Heuberger and Sara Kropf are supported by the
Austrian Science Fund (FWF): P 24644-N26.
.. TODO::
The following private and special methods are included now for
proofreading, they should be excluded. Furthermore, this module cannot
be shown in the documentation as it depends on arb.
Classes and Methods
===================
.. autofunction:: _hurwitz_zeta_
.. autofunction:: _reduce_resolution_
.. automethod:: FSMFourierComponent.__init__
.. automethod:: FSMFourierComponent._eigenvectors_
.. automethod:: FSMFourierComponent._mask_
.. automethod:: FSMFourierCache.__init__
.. automethod:: FSMFourier.__init__
.. automethod:: FSMFourier._H_m_rhs_
.. automethod:: FSMFourier._w_H_Res_
.. automethod:: FSMFourier._H_m_
"""
#*****************************************************************************
# Copyright (C) 2014 Clemens Heuberger <clemens.heuberger@aau.at>
# 2014 Sara Kropf <sara.kropf@aau.at>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from libc.stdlib cimport malloc, realloc, free
import itertools
from sage.combinat.finite_state_machine import Transducer
from sage.functions.log import log
from sage.functions.other import floor
from sage.libs.arb.acb cimport *
from sage.libs.arb.acb_mat cimport *
from sage.matrix.constructor import matrix
from sage.matrix.matrix_complex_ball_dense cimport (
matrix_to_acb_mat, acb_mat_to_matrix)
from sage.matrix.matrix_space import MatrixSpace
from sage.misc.cachefunc import cached_method
from sage.misc.misc import srange, verbose
from sage.modules.free_module_element import vector
from sage.modules.free_module_element cimport FreeModuleElement_generic_dense
import sage.rings.arith
from sage.rings.complex_interval cimport ComplexIntervalFieldElement
from sage.rings.complex_ball_acb cimport ComplexIntervalFieldElement_to_acb
from sage.rings.complex_interval_field import ComplexIntervalField
from sage.functions.other import ceil
from sage.rings.infinity import infinity
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.real_arb cimport arb_to_mpfi
from sage.rings.real_mpfi import RealIntervalField
from sage.rings.real_mpfi cimport RealIntervalFieldElement
from sage.rings.real_mpfi import RealIntervalFieldElement as RIF_E
from sage.structure.sage_object cimport SageObject
def infinity_vector_norm(v):
"""
Compute the infinity norm of a vector of
:class:`~sage.rings.real_mpfi.RealIntervalFieldElement`.
INPUT:
- ``v`` -- a vector of
:class:`~sage.rings.real_mpfi.RealIntervalFieldElement`.
OUTPUT:
A :class:`~sage.rings.real_mpfi.RealIntervalFieldElement` element.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import infinity_vector_norm # optional - arb
sage: a = RIF(1, 4)
sage: b = RIF(-3, -2)
sage: infinity_vector_norm(vector([a, b])).endpoints() # optional - arb
(2.00000000000000, 4.00000000000000)
sage: infinity_vector_norm(vector([b, a])).endpoints() # optional - arb
(2.00000000000000, 4.00000000000000)
Note that the
:meth:`sage.modules.free_module_element.FreeModuleElement.norm`
method is inadequate, as it uses the Python max instead of
:meth:`~sage.rings.real_mpfi.RealIntervalFieldElement.max`::
sage: vector([a, b]).norm(infinity).endpoints()
(1.00000000000000, 4.00000000000000)
sage: vector([b, a]).norm(infinity).endpoints()
(2.00000000000000, 3.00000000000000)
"""
return RIF_E.max(*(abs(r) for r in v))
def infinity_matrix_norm(A):
"""
Compute the infinity norm of a matrix A, in the same ring as
the original matrix A.
INPUT:
- ``A`` -- a matrix.
OUTPUT:
An entry.
In contrast to :meth:`sage.matrix.matrix2.Matrix.norm`,
sparse zero matrices are allowed and the result is not
necessarily a
:class:`~sage.rings.real_double.RealDoubleElement`.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import infinity_matrix_norm # optional - arb
sage: M = matrix([[1, 2], [-5, -1]])
sage: infinity_matrix_norm(M) # optional - arb
6
sage: M.norm(infinity)
6.0
sage: M = matrix([[0]], sparse=True)
sage: infinity_matrix_norm(M) # optional - arb
0
sage: M.norm(infinity)
Traceback (most recent call last):
...
TypeError: base_ring (=Category of objects) must be a ring
"""
if A.is_zero():
return 0
return max(sum(r) for r in A.apply_map(abs).rows())
def _hurwitz_zeta_(s, alpha, m=0, max_approximation_error=0):
r"""
Compute the truncated Hurwitz zeta function `\sum_{k\ge m} (k+\alpha)^{-s}`.
INPUT:
- ``s`` -- a :class:`ComplexIntervalFieldElement`
- ``alpha`` -- a :class:`~sage.rings.real_mpfi.RealIntervalFieldElement`
- ``m`` -- a positive integer
- ``max_approximation_error`` -- a non-negative number; an
approximation error less than ``max_approximation_error`` is
accepted even if it is not small with respect to the result.
OUTPUT:
A :class:`ComplexIntervalFieldElement` in the same field as ``s``.
EXAMPLES:
- Simple example::
sage: from sage.combinat.fsm_fourier import _hurwitz_zeta_ # optional - arb
sage: _hurwitz_zeta_(CIF(2), RIF(3/4), 10) # optional - arb
0.097483848201852? + 0.?e-17*I
- Compare with well-known value `\zeta(2)=\zeta(2, 1)=\pi^2/6`::
sage: _hurwitz_zeta_(CIF(2), 1) # optional - arb
1.64493406684823? + 0.?e-17*I
sage: (_hurwitz_zeta_(CIF(2), 1) - CIF(pi)^2/6).abs()<10^(-13) # optional - arb
True
sage: _hurwitz_zeta_(CIF(2*pi*I/log(2)), 1) # optional - arb
1.598734526809? + 0.278338669639?*I
- There is a singularity at `s=1`. ::
sage: _hurwitz_zeta_(CIF(RIF(0.9, 1.1), (-0.1, 0.1)), 1) # optional - arb
Traceback (most recent call last):
...
ZeroDivisionError: zeta is singular at 1.
- Checking the function for non-positive integers::
sage: zeta(-1) in _hurwitz_zeta_(CIF(-1), 1) # optional - arb
True
sage: zeta(0) in _hurwitz_zeta_(CIF(0), 1) # optional - arb
True
- Debugging output can be enabled using
:func:`~sage.misc.misc.set_verbose`. ::
sage: set_verbose(2)
sage: _hurwitz_zeta_(CIF(1+100/log(2)*I), 1) # optional - arb
verbose 1 (...) _hurwitz_zeta_(1 + 144.2695040888963?*I, 1, 0): M = 172
verbose 2 (...) N = 2, error = 0.0352354068797?, acceptable_error = 3.41199291042927e-13, result = 2.125571548789? + 0.511221280470?*I
verbose 2 (...) N = 4, error = 0.000310532577681?, acceptable_error = 3.41254802194158e-13, result = 2.125575595864? + 0.51121897538?*I
verbose 2 (...) N = 6, error = 3.65215306101?e-6, acceptable_error = 3.41351946708813e-13, result = 2.125575660430? + 0.511218933060?*I
verbose 2 (...) N = 8, error = 4.83820940904?e-8, acceptable_error = 3.41407457860044e-13, result = 2.125575661484? + 0.511218932225?*I
verbose 2 (...) N = 10, error = 6.84787778802?e-10, acceptable_error = 3.41504602374699e-13, result = 2.125575661501? + 0.511218932208?*I
verbose 2 (...) N = 12, error = 1.011644165731?e-11, acceptable_error = 3.41560113525930e-13, result = 2.125575661501? + 0.51121893221?*I
verbose 2 (...) N = 14, error = 1.54088223831?e-13, acceptable_error = 3.41615624677161e-13, result = 2.125575661501? + 0.51121893221?*I
verbose 1 (...) N = 14, error = 1.54088223831?e-13, acceptable_error = 3.41615624677161e-13, result = 2.125575661501? + 0.51121893221?*I
2.125575661501? + 0.51121893221?*I
sage: set_verbose(0)
- The current implementation does not work well with values with negative real
part, all precision is lost::
sage: _hurwitz_zeta_(CIF(-15+I), 1) # optional - arb
0.?e12 + 0.?e12*I
sage: hurwitz_zeta(ComplexField(200)(-15 + I), 1) # optional - arb
0.66621329305522618549073659441004805750538410627754288912677
- 0.84614995218731390314834131849322502368334938802936485299779*I
A work-around is to start with higher precision; however, we have
to clear the cache first::
sage: _hurwitz_zeta_(ComplexIntervalField(200)(-15 + I), 1) # optional - arb
0.66621329305522618549073660?
- 0.8461499521873139031483414?*I
"""
from sage.rings.arith import bernoulli, falling_factorial
CIF = s.parent()
RIF = s.real().parent()
if ZZ(1) in s:
raise ZeroDivisionError("zeta is singular at 1.")
# We rely on (2pi)^-N for convergence of the error term.
# As a conservative estimate, 2pi is approximately 2^2,
# so we will finally need N ~ s.prec()/2 to achieve an error which
# is less than the resolution of s.
# In order to have the falling factorial (-s)^\underline{N}
# smaller than (M+a)^N, we choose M>|s|+N
M = max(m, (s.prec()/ZZ(2)).ceil() + ZZ(s.abs().upper().ceil()))
verbose("_hurwitz_zeta_(%s, %s, %s): M = %d" % (s, alpha, m, M),
level=1)
sigma = s.real()
result = sum((r + alpha)**(-s) for r in reversed(srange(m, M)))
result += (M + alpha)**(1-s) / (s-1)
factor = (M + alpha)**(-s)
result += factor/2
N = 0
error_factor = RIF(4)
N_factorial = ZZ(1)
while True:
N += 2
factor *= (-s - N + 2)/(M + alpha)
#assert factor.overlaps(falling_factorial(-s, N - 1)/(M + alpha)**(s + N - 1))
N_factorial *= (N - 1)*N
#assert ZZ(N).factorial() == N_factorial
result -= bernoulli(N)/N_factorial * factor
factor *= (-s - N + 1)
#assert factor.overlaps(falling_factorial(-s, N)/(M + alpha)**(s + N - 1))
error_factor /= 4*RIF.pi()**2
#assert error_factor.overlaps(4/(2*RIF.pi())**N)
error_bound = error_factor / (sigma + N - 1) * factor.abs()
if result.abs().upper().is_zero():
error_acceptable = 0
else:
rounding_error = result.real().upper() + result.imag().upper() \
- result.real().lower() - result.imag().lower()
error_acceptable = max(max_approximation_error, rounding_error/8)
verbose(" N = %d, error = %s, acceptable_error = %s, result = %s" %
(N, error_bound, error_acceptable, result), level=2)
if error_bound.abs() < error_acceptable:
error_real = RIF(-error_bound, error_bound)
error = CIF(error_real, error_real)
verbose(" N = %d, error = %s, acceptable_error = %s, result = %s" %
(N, error_bound, error_acceptable, result), level=1)
return result + error
factor /= (M + alpha)
#assert factor.overlaps(falling_factorial(-s, N)/(M + alpha)**(s + N))
def _reduce_resolution_(data, x_min, x_max, resolution):
r"""
Reduce a list of pairs `(x, y)` to a list of triples `(x,
y_\mathrm{min}, y_\mathrm{max})` corresponding to
``resolution`` equidistant `x` values.
INPUT:
- ``data`` -- a list (or iterable) of pairs of doubles.
- ``x_min`` -- a double, start of the interval.
- ``x_max`` -- a double, end of the interval.
- ``resolution`` -- a positive integer, the number of points
in `x` direction.
OUTPUT:
A list of triples of doubles.
Each `(x, y)` is mapped to some
`(x', y_\mathrm{min}, y_\mathrm{max})`
such that `y_\mathrm{min}\le y\le y_\mathrm{max}` and
`0\le x-x'< (x_\mathrm{max}-x_\mathrm{min})/\mathit{resolution}`.
A list plot of the original list thus corresponds to plot of the
vertical line segments defined by the output.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import _reduce_resolution_ # optional - arb
sage: _reduce_resolution_(((i/10, i) for i in range(10)), # optional - arb
....: 0, 1, 2)
[(0.0, 0, 4), (0.5, 5, 9)]
sage: _reduce_resolution_(((1 - i/10, i) for i in range(1, 11)), # optional - arb
....: 0, 1, 2)
[(0.0, 6, 10), (0.5, 1, 5)]
sage: _reduce_resolution_(((0, 2), (0.2, 1), (0.4, 0), # optional - arb
....: (0.6, 1.5), (0.8, 3)), 0, 1, 2)
[(0.0, 0, 2), (0.5, 1.50000000000000, 3)]
sage: _reduce_resolution_([(0, 10)], # optional - arb
....: 0, 1, 2)
[(0.0, 10, 10)]
TESTS::
sage: _reduce_resolution_([(1, 10)], # optional - arb
....: 0, 1, 2)
Traceback (most recent call last):
...
ValueError: x values must be >= x_min and < x_max.
sage: _reduce_resolution_([(-1, 10)], # optional - arb
....: 0, 1, 2)
Traceback (most recent call last):
...
ValueError: x values must be >= x_min and < x_max.
"""
result = [None for _ in range(resolution)]
f = (<double> resolution)/(x_max-x_min)
for (x, y) in data:
if x < x_min and x > x_min - 1/f:
# Allow undershooting x_min by one pixel in order to be
# more robust against numerical noise.
x = x_min
if x < x_min or x >= x_max:
raise ValueError(
"x values must be >= x_min and < x_max.")
i = floor((x - x_min)*f)
current = result[i]
if current is None:
result[i] = (y, y)
else:
result[i] = (min(y, current[0]), max(y, current[1]))
return [(x_min + (<double> i)/f, y[0], y[1])
for i, y in enumerate(result)
if y is not None]
class FSMFourierComponent(SageObject):
r"""
Final component of a
:class:`~sage.combinat.finite_state_machine.Transducer` and its
associated data for computing the Fourier coefficients of the
fluctuations of the sum of output.
INPUT:
- ``fsm`` -- the final component as a
:class:`~sage.combinat.finite_state_machine.Transducer`.
- ``parent`` -- an instance of
:class:`~sage.combinat.fsm_fourier.FSMFourier` holding all
relevant data for the full transducer.
EXAMPLES::
sage: function('f')
f
sage: var('n')
n
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: F = FSMFourier(transducers.Recursion([ # indirect doctest; optional - arb
....: f(2*n + 1) == f(n) + 1,
....: f(2*n) == f(n),
....: f(0) == 0],
....: 2, f, n))
sage: F.components[0] # optional - arb
<class 'sage.combinat.fsm_fourier.FSMFourierComponent'>
sage: F.components[0].period # optional - arb
1
"""
def __init__(self, fsm, parent):
r"""
Initialize the :class:`FSMFourierComponent`.
INPUT:
- ``fsm`` -- the final component as a
:class:`~sage.combinat.finite_state_machine.Transducer`.
- ``parent`` -- an instance of :class:`FSMFourier` holding all
relevant data for the full transducer.
EXAMPLE::
sage: function('f')
f
sage: var('n')
n
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: F = FSMFourier(transducers.Recursion([ # indirect doctest; optional - arb
....: f(2*n + 1) == f(n) + 1,
....: f(2*n) == f(n),
....: f(0) == 0],
....: 2, f, n))
sage: F.components[0] # optional - arb
<class 'sage.combinat.fsm_fourier.FSMFourierComponent'>
sage: F.components[0].period # optional - arb
1
"""
self.fsm = fsm
self.period = fsm.graph().period()
self.n_states = len(self.fsm.states())
self.parent = parent
def _mask_(self):
r"""
Return a matrix whose rows consist of the incidence
vectors of the states contained in the other final components
of ``self.parent`` than self.
INPUT:
None.
OUTPUT:
A matrix.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: FC = FSMFourier(T).components[0] # optional - arb
sage: FC._mask_() # optional - arb
[0 0 1]
"""
n = self.parent.M.nrows()
nrows = sum(c.n_states
for c in self.parent.components if c != self)
_mask_ = matrix(
nrows, n,
[self.parent.standard_basis[
self.parent.positions[state.label()]]
for other in self.parent.components
if other != self
for state in other.fsm.iter_states()])
return _mask_
def _eigenvectors_(self, M):
r"""
Determine the dominant eigenvectors of the given full
adjacency matrix where all coordinates corresponding to final
components other than self vanish.
INPUT:
- ``M`` -- a matrix.
OUTPUT:
A list of vectors, the `k`-th entry is the eigenvector to
the eigenvalue `q \exp(2\pi i k/p_j)` where `p_j` is
the period of this component.
The first eigenvector (to the positive eigenvalue) is
normalized such that its entries sum up to one.
This method is called by :meth:`.right_eigenvectors` with
``M = self.parent.M`` and by :meth:`.left_eigenvectors`
with ``M = self.parent.M.transpose()``.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC._eigenvectors_(F.M) # optional - arb
[(1/3, 2/3, 0)]
.. SEEALSO::
:meth:`.right_eigenvectors`, :meth:`.left_eigenvectors`
"""
mask = self._mask_()
def eigenvector(j):
eigenvalue = self.parent.q * self.parent.alpha**(
j * self.parent.common_period / self.period)
S = matrix.block(
[[M - eigenvalue*matrix.identity(M.nrows())],
[mask]],
subdivide=False)
kernel = S.right_kernel_matrix()
assert kernel.nrows() == 1
if j == 0:
#normalize for positive eigenvector
return kernel.row(0) / sum(kernel.row(0))
else:
return kernel.row(0)
return [eigenvector(j) for j in range(self.period)]
@cached_method()
def right_eigenvectors(self):
r"""
Determine the dominant right eigenvectors of the full
adjacency matrix where all coordinates corresponding to final
components other than self vanish.
INPUT:
None.
OUTPUT:
A list of vectors, the `k`-th entry is the eigenvector to
the eigenvalue `q \exp(2\pi i k/p_j)` where `p_j` is
the period of this component.
The first eigenvector (to the positive eigenvalue) is
normalized such that its entries sum up to one.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.right_eigenvectors() # optional - arb
[(1/3, 2/3, 0)]
.. SEEALSO::
:meth:`.left_eigenvectors`
"""
return self._eigenvectors_(self.parent.M)
@cached_method()
def left_eigenvectors(self):
r"""
Determine the dominant left eigenvectors of the full
adjacency matrix where all coordinates corresponding to final
components other than self vanish.
INPUT:
None.
OUTPUT:
A list of vectors, the `k`-th entry is the eigenvector to
the eigenvalue `q \exp(2\pi i k/p_j)` where `p_j` is
the period of this component.
The eigenvectors are normalized such that their product
with the corresponding right eigenvectors computed by
:meth:`.right_eigenvectors` is `1`.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.left_eigenvectors() # optional - arb
[(0, 3/2, 0)]
.. SEEALSO::
:meth:`.right_eigenvectors`
"""
left_eigenvectors = self._eigenvectors_(self.parent.M.transpose())
return [w/(v*w) for v, w
in itertools.izip(self.right_eigenvectors(),
left_eigenvectors)]
@cached_method()
def w(self):
r"""
Compute `w_{jk}` for `0\le k< p_j`.
INPUT:
None.
OUTPUT:
A list of vectors.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.w() # optional - arb
[(0, 1/2, 0)]
.. SEEALSO::
:meth:`.left_eigenvectors`, :meth:`.right_eigenvectors`
"""
return [(self.parent.initial_vector*v)*w for v, w
in itertools.izip(self.right_eigenvectors(),
self.left_eigenvectors())]
@cached_method()
def coefficient_lambda(self):
r"""
Compute `\lambda_j`.
INPUT:
None.
OUTPUT:
An element of a cyclotomic field representing the value
`\lambda_j`.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.coefficient_lambda() # optional - arb
1/2
"""
products = [w*self.parent.ones for w in self.w()]
assert all(e.is_zero() for e in products[1:])
return products[0]
@cached_method()
def mu_prime(self):
r"""
Compute `\mu_{j0}'(0)`, the derivative of the dominant positive
eigenvalue of `M(t)` corresponding to ``self`` at `t=0`.
INPUT:
None.
OUTPUT:
An element of a cyclotomic field representing the value
`\mu'_{j0}(0)`.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.mu_prime() # optional - arb
0
"""
Y = self.parent.Y
p = self.fsm.adjacency_matrix(
entry=lambda t:Y**sum(t.word_out)).charpoly('Z')
Z = p.parent().gen()
assert p(Y=1, Z=self.parent.q) == 0
mu_prime_Z = (- p.derivative(Y)/p.derivative(Z))(
Y=1, Z=self.parent.q)
return self.parent.I*mu_prime_Z
@cached_method()
def a(self):
r"""
Compute the constant `a_{j}`.
INPUT:
None.
OUTPUT:
An element of a cyclotomic field representing the value
`a_j`.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 2, 0, 0), (2, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.a() # optional - arb
0
"""
return QQ(-self.parent.I * self.mu_prime()/self.parent.q)
def w_ell(self, ell):
r"""
Compute the left eigenvector to the eigenvalue
`\exp(2\pi i \ell/p)` to `M(0)` if the former is an eigenvalue
of `M(0)`. Otherwise, return the zero vector.
INPUT:
- ``ell`` -- an integer.
OUTPUT:
A vector over a complex interval field or the zero vector.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 3, 0, 0), (2, 3, 1, 0),
....: (3, 2, 0, 0), (3, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2, 3]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.w_ell(0) # optional - arb
(0, 0.50000000000000000?, 0, 0)
sage: FC.w_ell(1) # optional - arb
(0, 0, 0, 0)
"""
if self.parent.common_period.divides(ell*self.period):
k = self.period*ell/self.parent.common_period % self.period
return vector(self.parent.field_to_CIF(c) for c in self.w()[k])
else:
return vector(0 for _ in self.parent.ones)
def v_prime(self, k):
r"""
Compute the derivative `v_{jk}'(0)`, the right
eigenvector to the `k`-th dominant eigenvalue.
INPUT:
- ``k`` -- a non-negative integer.
OUTPUT:
A vector.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 3, 0, 0), (2, 3, 1, 0),
....: (3, 2, 0, 0), (3, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2, 3]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.v_prime(0) # optional - arb
(0, 0, 0, 0)
"""
M = self.parent.M
mask = self._mask_()
eigenvalue = self.parent.q * self.parent.alpha**(
k * self.parent.common_period / self.period)
S = matrix.block(
[[M - eigenvalue*matrix.identity(M.nrows())],
[matrix(self.parent.ones)],
[mask]],
subdivide=False)
eigenvector_right = vector(self.right_eigenvectors()[k])
M_prime = self.parent.I*self.parent.Delta
right_side = - matrix.block(
[[M_prime - self.mu_prime()*matrix.identity(M.nrows())],
[0*matrix(self.parent.ones)],
[0*mask]],
subdivide=False) * eigenvector_right
v_prime = S.solve_right(right_side)
return v_prime
def w_prime(self, k):
r"""
Compute the derivative `w_{jk}'(0)`, the left
eigenvector to the `k`-th dominant eigenvalue.
INPUT:
- ``k`` -- a non-negative integer.
OUTPUT:
A vector.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 1, 0, 0), (0, 2, 1, 0),
....: (1, 1, 0, 0), (1, 1, 1, 0),
....: (2, 3, 0, 0), (2, 3, 1, 0),
....: (3, 2, 0, 0), (3, 2, 1, 0)],
....: initial_states=[0],
....: final_states=[0, 1, 2, 3]) # optional - arb
sage: F = FSMFourier(T) # optional - arb
sage: FC = F.components[0] # optional - arb
sage: FC.w_prime(0) # optional - arb
(0, 0, 0, 0)
"""
M = self.parent.M
mask = self._mask_()
eigenvalue = self.parent.q * self.parent.alpha**(
k * self.parent.common_period / self.period)
eigenvector_right = vector(self.right_eigenvectors()[k])
eigenvector_left = vector(self.left_eigenvectors()[k])
M_prime = self.parent.I*self.parent.Delta
S = matrix.block(
[[M.transpose() - eigenvalue*matrix.identity(M.nrows())],
[matrix(eigenvector_right)],
[mask]],
subdivide=False)
right_side = - matrix.block(
[[M_prime.transpose() - self.mu_prime()*matrix.identity(M.nrows())],
[matrix(self.v_prime(k))],
[0*mask]],
subdivide=False) * eigenvector_left
left_prime = S.solve_right(right_side)
w_prime = self.parent.initial_vector*self.v_prime(k)*eigenvector_left \
+ self.parent.initial_vector*eigenvector_right*left_prime
return w_prime
cdef class FSMFourierCache(SageObject):
r"""
Compute and cache `\mathbf{b}(r)` for increased performance
and compute partial sums of the Dirichlet series `\mathbf{H}(s)`.
INPUT:
- ``parent`` -- a :class:`FSMFourier` instance.
OUTPUT:
None.
EXAMPLES::
sage: from sage.combinat.fsm_fourier import FSMFourier # optional - arb
sage: T = Transducer([(0, 0, 0, 0), (0, 0, 1, 1)],
....: initial_states=[0],
....: final_states=[0])
sage: FSMFourier(T).cache # optional - arb
<type 'sage.combinat.fsm_fourier.FSMFourierCache'>
"""
cdef acb_mat_t *bb
cdef unsigned long bb_computed
cdef unsigned long bb_allocated
cdef acb_mat_t *Delta_epsilon_ones
cdef acb_mat_t *M_epsilon
cdef unsigned long q