-
Notifications
You must be signed in to change notification settings - Fork 0
/
n.c
1204 lines (717 loc) · 27.6 KB
/
n.c
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
/*
gcc -std=c99 -Wall -Wextra -pedantic -O3 n.c -lm
./a.out
"The "flowers" are beautiful to behold but totally abhorrent from the numerical point of view." Ramillies
https://math.stackexchange.com/questions/2407659/why-does-the-newton-raphson-method-not-converge-for-some-functions
-----------------------------------
https://arxiv.org/abs/1703.05847
Newton's method in practice II: The iterated refinement Newton method and near-optimal complexity for finding all roots of some polynomials of very large degrees
Marvin Randig, Dierk Schleicher, Robin Stoll
(Submitted on 16 Mar 2017 (v1), last revised 31 Dec 2017 (this version, v2))
We present a practical implementation based on Newton's method to find all roots of several families of complex polynomials of degrees exceeding one billion (109) so that the observed complexity to find all roots is between O(dlnd) and O(dln3d) (measuring complexity in terms of number of Newton iterations or computing time). All computations were performed successfully on standard desktop computers built between 2007 and 2012.
------------------------------------
File 3_16_11.pgm saved. Newton iterations (rays)
File 3_16_12.pgm saved. Newton Basins
File 3_16_14.pgm saved. Newton Basins with Level Set Method
File 3_16_13.pgm saved. Newton Basins and rays
File 3_16_15.pgm saved. Newton Basins with Level Sets and rays
File 3_16_16.pgm saved. Newton Basins with Level Sets and rays, marked first quadrant
File 3_16_17.pgm saved. only roots
parameter c of the function fc(z) = z^2+c is c = 0.0000000000000000 ; 0.0000000000000000
period = 3
degree of polynomial = 8 = 2^period
prime factors of 3 = 3
number of roots = number of periodic points = degree of polynomial = 8
number of starting points sMax = 16
succes : all 8 distinct points are found !!
dt = 6.250000e-02
radius of the circle around all periodic points = 2.000000e+00
maximal allowed number of Newton iterations nMax = 180 = 10*degree + 100, see setup
maximal used number of Newton iterations maximal_n = 68
stopping criterion for the Newton iteration is epsilon_stop = 1.000000e-18
m_dist = 1.000000000000000036e-10
minimal distnce in zzd =8.677675e-01 between roots
periodic points are:
z = +1.000000000000000000; +0.000000000000000000 exact period = 1 stability = 2.000000000000000000
z = +0.000000000000000000; +0.000000000000000000 exact period = 1 stability = 0.000000000000000000
z = +0.623489801858733531; +0.781831482468029809 exact period = 3 stability = 8.000000000000000000
z = -0.222520933956314404; +0.974927912181823607 exact period = 3 stability = 8.000000000000000000
z = -0.900968867902419126; +0.433883739117558120 exact period = 3 stability = 8.000000000000000000
z = -0.900968867902419126; -0.433883739117558120 exact period = 3 stability = 8.000000000000000000
z = -0.222520933956314404; -0.974927912181823607 exact period = 3 stability = 8.000000000000000000
z = +0.623489801858733531; -0.781831482468029809 exact period = 3 stability = 8.000000000000000000
attracting limit cycle with exact period :
the sum of all roots should be zero by Viete’s formula (this sum should be the negative of the degree d − 1 coefficient)
Viete sum = 1.212174879515270018e-19 ( it should be zero )
----------------------
convert 2_8_14.pgm -resize 800x800 2_8_14.png
------------------------------------
cd existing_folder
git init
git remote add origin git@gitlab.com:adammajewski/periodic-points-of-complex-quadratic-polynomial-using-newton-method.git
git add .
git commit -m "Initial commit"
git push -u origin master
--------------------------------
*/
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // strcat
/* ----------------------- */
int period = 3;
int degree; // = 2^period
int sMax; // = 4*d = number of starting points
long double dt; // = 1.0 / sMax
// failure: we have n > Mfail, where Mfail is the largest number of allowed iterations before the algorithm gives up
int nMax; // limit for maxima number of Newton iterations = 50+10 * d = M_fail
int maximal_n = 0; // global maximal n = for all pixels
int local_max_n = 0; // local mximal n = for one pixel
complex long double c = 0.0 ; // circle
const long double pi = 3.1415926535897932384626433832795029L;
// viewport is a square : iWidth = iHeight and ( ZxMax-ZxMin) = (ZyMax-ZyMin)
// square in dyanamic ( complex plane) -> virtual ( memory) 2D array -> virtual 1D array -> pgm file
/* coordinate in world units */
static const long double ZxMin = -2.1; // slightly more than radius
static const long double ZxMax = 2.1; //
static const long double ZyMin = -2.1; //
static const long double ZyMax = 2.1; //
long double PixelWidth; // =(ZxMax-ZxMin)/ixMax;
long double PixelHeight; // =(ZyMax-ZyMin)/iyMax;
static long double ratio;
complex long double z0 ; // initial aproximation for Newton method;
complex long double zp; // periodic point
// virtual 2D array and integer ( screen) coordinate
// Indexes of array starts from 0 not 1
//unsigned int ix, iy; // var
int ixMin = 0; // Indexes of array starts from 0 not 1
int ixMax; //
int iWidth; // horizontal dimension of array
int iyMin = 0; // Indexes of array starts from 0 not 1
int iyMax; //
int iHeight = 2000; //
// The size of 1D array has to be a positive constant integer
int iSize; // = iWidth*iHeight;
// memmory 1D array
unsigned char *data;
unsigned char *data2;
// iMax = iSize-1; // Indexes of array starts from 0 not 1 so the highest elements of an array is = array_name[size-1].
//long double ER = 2.1;
//long double ER2; // = 2.0 * 2.0; // ER*ER
long double radius = 2.0;
// epsilon_succes = success: we have |zn+1−zn| ≤ εsuccess, where εsuccess is a predefined accuracy threshold for success;
long double EPS = 1e-18; // to much for double so I use long double
long double EPS2 ;// EPS*EPS
// the minimal distance between any two of them was 5.47 · 10^−11
long double m_dist = 1e-10;
long double m_dist2 ; // = m_dist*m_dist
long double MinimalDistanceBetweenRoots; // minimal distance between any two
// arrays for complex points
complex long double * zzs; // array for found points ( length = sMax)
// distinct roots = not equal
complex long double * zzd; // array for periodic points = distinct points , where length = degree -1
// counters
//int point_errors = 0;
//int point_drawn = 0;
int distinc_points = 0; // number of distinct roots = periodic points
long double VieteSum;
complex double fc(const complex double z0){
return z0*z0 + c;
}
/*
https://stackoverflow.com/questions/40273079/find-all-prime-factors-of-a-given-number
https://en.wikipedia.org/wiki/Trial_division
*/
int GiveAllPrimeFactors( int n )
{
int a;
printf("prime factors of %d = ", n);
for (a=2; a<=n; ++a)
while(n%a==0)
{
printf("%d\t", a);
n = n/a;
}
printf("\n");
return 0;
}
/* ----------- array functions -------------- */
/* gives position of 2D point (ix,iy) in 1D array ; uses also global variable iWidth */
int Give_i( int ix, int iy)
{ int i;
i = ix + iy*iWidth;
if (i>=0 && i<iSize)
return i;
else {printf("error from Give_i: i = %d ix = %d iy = %d iSize = %d \n", i, ix, iy, iSize); return -1;}
}
// plots raster point (ix,iy)
int iDrawPoint( int ix, int iy, unsigned char iColor, unsigned char A[])
{
int i;
i = Give_i(ix,iy ); // compute index of 1D array from indices of 2D array
if (i>=0 && i<iSize)
A[i] = iColor; // draw
else {printf("error from iDrawPoint\n"); return 1;} // not draw
return 0; // succes
}
// draws point to memmory array data
// uses complex type so #include <complex.h> and -lm
// The roots are marked by big spots with inverted color
int dDrawBigPoint_Inverted(complex long double point, unsigned char A[] )
{
int ix, iy; // screen coordinate = indices of virtual 2D array
int ixCenter, iyCenter;
unsigned int i; // index of 1D array
long double zx = creall(point);
long double zy = cimagl(point);
//int r;
unsigned char iColor;
int iSide =ixMax/500; /* half of width or height of big pixel */
if (zx< ZxMax && zx > ZxMin && zy > ZyMin && zy < ZyMax){
ixCenter = (int) ((zx- ZxMin)/PixelWidth);
iyCenter = (int) ((ZyMax - zy)/PixelHeight); // inverse Y axis
//
i = Give_i(ixCenter,iyCenter);
// find color
iColor = A[i];
// inverse color
if (iColor < 150 && iColor>100) // if color is near 255/2 then 255-color is similar
iColor = 254; // more different
else iColor = 255 - iColor ; // typical inverse
//printf(" color A[i] = %d and inverse = %d\n", A[i], 255-A[i]);
/* mark point by big pixel */
for(iy=iyCenter-iSide; iy<=iyCenter+iSide; iy++)
for(ix=ixCenter-iSide; ix<=ixCenter+iSide; ix++){
iDrawPoint(ix, iy, iColor, A);
}
}
return 0; //
}
int dDrawBigPoint(complex long double point, unsigned char A[], unsigned char iColor )
{
int ix, iy; // screen coordinate = indices of virtual 2D array
int ixCenter, iyCenter;
long double zx = creall(point);
long double zy = cimagl(point);
int iSide =ixMax/500; /* half of width or height of big pixel */
if (zx< ZxMax && zx > ZxMin && zy > ZyMin && zy < ZyMax){
ixCenter = (int) ((zx- ZxMin)/PixelWidth);
iyCenter = (int) ((ZyMax - zy)/PixelHeight); // inverse Y axis
/* mark point by big pixel */
for(iy=iyCenter-iSide; iy<=iyCenter+iSide; iy++)
for(ix=ixCenter-iSide; ix<=ixCenter+iSide; ix++){
iDrawPoint(ix, iy, iColor, A);
}
}
return 0; // r
}
/*
http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
*/
void iDrawLine( int x0, int y0, int x1, int y1, unsigned char iColor, unsigned char A[])
{
int x=x0;
int y=y0;
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
for(;;){
iDrawPoint(x, y, iColor, A);
if (x==x1 && y==y1) break;
e2 = err;
if (e2 >-dx) { err -= dy; x += sx; }
if (e2 < dy) { err += dx; y += sy; }
}
}
int dDrawLine(long double Zx0, long double Zy0, long double Zx1, long double Zy1, unsigned char color, unsigned char A[])
{
int ix0, iy0; // screen coordinate = indices of virtual 2D array
int ix1, iy1; // screen coordinate = indices of virtual 2D array
// first step of clipping
//if ( Zx0 < ZxMax && Zx0 > ZxMin && Zy0 > ZyMin && Zy0 <ZyMax
// && Zx1 < ZxMax && Zx1 > ZxMin && Zy1 > ZyMin && Zy1 <ZyMax )
ix0= (Zx0- ZxMin)/PixelWidth;
iy0 = (ZyMax - Zy0)/PixelHeight; // inverse Y axis
ix1= (Zx1- ZxMin)/PixelWidth;
iy1= (ZyMax - Zy1)/PixelHeight; // inverse Y axis
// second step of clipping
if (ix0 >=ixMin && ix0<=ixMax && ix0 >=ixMin && ix0<=ixMax && iy0 >=iyMin && iy0<=iyMax
&& iy1 >=iyMin && iy1<=iyMax )
iDrawLine(ix0,iy0,ix1,iy1,color, A) ;
return 0;
}
int cDrawLine(complex long double z0, complex long double z1, unsigned char color, unsigned char A[]){
dDrawLine(creall(z0), cimagl(z0), creall(z1), cimagl(z1), color, A);
return 0;
}
unsigned char GiveColor (complex long double z, unsigned char A[]){
int ix, iy; // screen coordinate = indices of virtual 2D array
int i; // indices of 1D array
unsigned char iColor;
ix = (int) ((creall(z)- ZxMin)/PixelWidth);
iy = (int) ((ZyMax - cimagl(z))/PixelHeight); // inverse Y axis
i = Give_i(ix,iy);
// find color
iColor = A[i];
// inverse color
if (iColor < 150 && iColor>100) // if color is near 255/2 then 255-color is similar
iColor = 254; // more different
else iColor = 255 - iColor ; // typical inverse
return iColor;
}
int FillArray(unsigned char A[]){
int i;
for (i=0; i<iSize; i++){
A[i] = 255; // white
}
return 0;
}
// save data array to pgm file
int SaveArray2PGMFile( unsigned char A[], int p, int s, int k, char* comment )
{
FILE * fp;
const unsigned int MaxColorComponentValue=255; /* color component is coded from 0 to 255 ; it is 8 bit color file */
char name [100]; /* name of file */
snprintf(name, sizeof name, "%d_%d_%d", p,s, k); /* period, sMax */
char *filename =strncat(name,".pgm", 4);
/* save image to the pgm file */
fp= fopen(filename,"wb"); /*create new file,give it a name and open it in binary mode */
fprintf(fp,"P5\n # %s\n %u %u\n %u\n", comment, iWidth, iHeight, MaxColorComponentValue); /*write header to the file*/
fwrite(A,iSize,1,fp); /*write image data bytes to the file in one step */
//
printf("File %s saved. ", filename);
if (comment == NULL) printf ("empty comment \n");
else printf ("%s \n", comment);
fclose(fp);
return 0;
}
/* --------------- complex plane --------------------------- */
long double complex Give_z0(long double InternalAngleInTurns, long double radius )
{
//0 <= InternalAngleInTurns <=1
long double a = InternalAngleInTurns *2.0*pi; // from turns to radians
long double Cx, Cy; /* C = Cx+Cy*i */
Cx = radius*cosl(a);
Cy = radius*sinl(a);
return Cx + Cy*I;
}
long double cabs2(complex long double z) {
return (creall(z) * creall(z) + cimagl(z) * cimagl(z));
}
/*
newton function
N(z) = z - (fp(z)-z)/fp'(z))
used for computing periodic point
of complex quadratic polynomial
f(z) = z*z +c
*/
complex long double N( complex long double c, complex long double zn , int pMax ){
complex long double z = zn;
complex long double d = 1.0; /* d = first derivative with respect to z */
int p;
for (p=0; p < pMax; p++){
d = 2*z*d; /* first derivative with respect to z */
z = z*z +c ; /* complex quadratic polynomial */
}
z = zn - (z - zn)/(d - 1) ; // Newton function
return z;
}
/*
compute periodic point
of complex quadratic polynomial1
using Newton iteration = numerical method
*/
complex long double GivePeriodicAndDrawRay(complex long double c, complex long double z0, int period, long double eps2, unsigned char A[]){
complex long double z = z0;
complex long double zPrev = z0; // prevoiuus value of z
int n ; // iteration
unsigned char iColor;
iColor = GiveColor(z0, A);
for (n=0; n<nMax; n++) {
z = N( c, z, period);
cDrawLine(zPrev, z, iColor, A);
if (cabs2(z - zPrev)< eps2) break; // succes
zPrev = z; }
if ( n>maximal_n) maximal_n = n; //
// printf(" n = %d\n", n);
dDrawBigPoint(z, A, iColor);
return z;
}
int DrawRays(unsigned char A[]){
int s; // number of starting point z0
long double t= 0.0; //
complex long double z0;
complex long double zp;
for (s=0; s<sMax; s++ ){
z0 = Give_z0(t, radius); // compute new starting point on the circle
zp = GivePeriodicAndDrawRay( c , z0, period, EPS2, A); // compute periodic point
zzs[s] = zp; // save all found points
//printf("z0 = %.16f %.16f zp = %.16f %.16f\n", creal(z0), cimag(z0), creal(zp), cimag(zp));
t += dt; // next angle using globl variable dt
}
return 0;
}
/*
check if point z is different
from all first dMax points of zzd array
uses global :
* array zzd
* limit m_dist2
*/
int IsDistinct(complex long double z, int d_length){
int d; // index of zzd array
int dMax = d_length; // d starts from 0 to dMax-1
long double distance2 = 0.0L;
for (d=0; d<dMax; d++ ){
distance2 = cabs2(z - zzd[d]);
if (distance2< m_dist2) // the mutual distance between pairs of periodic points: z and zzd[d]
return 0; // no = not distinct = not different
}
//printf("distance = %Lf\n", sqrtl(distance2));
return 1; // distinct
}
/*
find distinct point in zzs and moves it to zzd
take point from zzs
check if it is distinct
if yes then move it to zzd
if no go to next point from zzs
uses global variables :
* arrays : zzs i zzd
* counter : distinc_points
*/
int FindDistinctPoints(){
int s = 0; // index of zzs array
int d = 0; // index of zzd array
complex long double z;
//if ( iLength != sMax) {printf(" error : length(zzs) != sMax \n"); return 1;}
// first point without checking
z = zzs[s]; // take point z from zzs
zzd[d] = z; // move to zzd
d +=1;
// rest of points
for (s=1; s<sMax; s++ ){
z = zzs[s]; // take next point
//
if (IsDistinct(z,d)){
zzd[d] = z; // move from zzs to zzd
d +=1;
}
}
distinc_points = d ; // update counter = number of all distinct points
return 0;
}
// from screen to world coordinate ; linear mapping
// uses global cons
complex long double GiveZ( int ix, int iy){
long double zx = ZxMin + ix*PixelWidth;
long double zy = ZyMax - iy*PixelHeight; // reverse y axis
return (zx + zy*I);
}
/*
input :
zp = periodic point
zzd = table of periodic points
output :
index of table zzd
each periodic points has it's own basin
find it's number'
*/
complex long double GivePeriodic(complex long double c, complex long double z0, int period, long double eps2){
complex long double z = z0;
complex long double zPrev = z0; // prevoiuus value of z
int n ; // iteration
// Newton iteration
for (n=0; n<nMax; n++) {
z = N( c, z, period);
if (cabs2(z - zPrev)< eps2) break; // succes
zPrev = z; }
if ( n>maximal_n) maximal_n = n; // global value is saved for info
local_max_n = n; // local value , save for Level Set Method
return z;
}
int GiveBasinNumber(complex long double zp, complex long double zzd[]){
int d = 0; // index of zzd array
int dMax = distinc_points;
//
for (d=0; d<dMax; d++ )
if (cabs2(zp - zzd[d])<m_dist2) // the mutual distance between pairs of periodic points: z and zzd[d])
return d; //
return -1;
}
unsigned char GiveBasinColor(complex long double z0, int ColorStep){
complex long double zp;
int i;
zp = GivePeriodic( c , z0, period, EPS2); // compute periodic point using Newton method
i = GiveBasinNumber(zp, zzd); // color is proportional to Number of Newton Basin in zzd array
return 50 + i*ColorStep; // return 8 bit color = shades of gray
}
//
int DrawNewtonBasins(unsigned char A[], unsigned char B[]){
int ix, iy;
int i;
int BasinColorStep = (int)((250 - 10)/degree); // step between colors of the basins
complex long double z;
int color;
// for all points (x,y) of the image ( 2D array)
for(iy=0;iy<iyMax;iy++)
for(ix=0;ix<ixMax;ix++){
z = GiveZ(ix, iy); // compute pixel coordinate
i = Give_i(ix, iy); // compute idex of 1D array
A[i] = GiveBasinColor(z, BasinColorStep); // compute color and save it to 1D array
color = A[i] - 10*local_max_n;
if (color > 255) color = 255 - color;
if (color < 0 ) color = - color;
B[i] = color;
}
return 0;
}
//
int CheckOrientation(unsigned char A[] ){
int ix, iy;
int i;
complex long double z;
// for all points (x,y) of the image ( 2D array)
for(iy=0;iy<iyMax;iy++)
for(ix=0;ix<ixMax;ix++){
z = GiveZ(ix, iy); // compute pixel coordinate
if (creal(z)>0 && cimag(z)>0.0){
i = Give_i(ix, iy); // compute idex of 1D array
A[i] = 255-A[i]; //
}
}
return 0;
}
int DrawRoots(unsigned char A[]){
int d;
for (d=0; d<distinc_points; d++ )
dDrawBigPoint_Inverted( zzd[d], A);
return 0;
}
int DrawRootsColor(unsigned char A[], unsigned char iColor){
int d;
for (d=0; d<distinc_points; d++ )
dDrawBigPoint( zzd[d], A, iColor);
return 0;
}
// dDrawLine(long double Zx0, long double Zy0, long double Zx1, long double Zy1, unsigned char color, unsigned char A[])
int DrawCycles(unsigned char A[], unsigned char iColor){
int d;
for (d=0; d<distinc_points; d++ ){
dDrawBigPoint( zzd[d], A, iColor);
cDrawLine(zzd[d], fc(zzd[d]), 0, A);
}
return 0;
}
// https://en.wikipedia.org/wiki/Vieta%27s_formulas
long double ComputeVieteSum(){
complex long double sum = 0;
int d;
for (d=0; d<distinc_points; d++ )
sum += zzd[d]; // zzd is a array of roots (global variable)
return cabsl(sum);
}
/*
https://en.wikipedia.org/wiki/Closest_pair_of_points_problem
*/
long double ComputeMinimalDIstanceBetweenRoots(){
long double minimal_dist2 = 2.0L; // minimal_dist^2
long double minimal_dist;
long double dist2;
int i,j;
//complex long double z1 = 100;
//complex long double z2 = 200;
for (i=0; i<distinc_points-1; i++ )
for (j=i+1; j<distinc_points; j++ ){
dist2 = cabs2(zzd[i] - zzd[j]);
if (dist2 < minimal_dist2) {
minimal_dist2 = dist2;
//z1 = zzd[i];
//z2 = zzd[j];
}
}
minimal_dist = sqrtl(minimal_dist2);
//printf(" minimal distnce in zzd =%Le between\nz1 = %+.18Lf ; %+.18Lf \nz2 = %+.18Lf ; %+.18Lf\n ", minimal_dist, creall(z1), cimagl(z1), creall(z2), cimagl(z2));
//dDrawBigPoint_Inverted( z1, data);
//dDrawBigPoint_Inverted( z2, data);
return minimal_dist;
}
int setup()
{
// numerical optimisaton for cabs2
EPS2 = EPS * EPS;
m_dist2 = m_dist*m_dist;
degree = (int) pow(2.0, period);// degree = 2^period
sMax = 2*degree; // 4*d = number of starting points
nMax = 10* degree + 100; //
dt = 1.0/ sMax;
//
iWidth = iHeight; // square
iyMax = iHeight -1;
ixMax = iWidth - 1;
iSize = iWidth * iHeight;
//
PixelWidth = (ZxMax-ZxMin)/ixMax;
PixelHeight = (ZyMax-ZyMin)/iyMax;
ratio = ( ZxMax - ZxMin)/ (ZyMax-ZyMin);
// dynamic 1D array for the image
data = malloc( iSize * sizeof(unsigned char) );
if (data == NULL )
{
fprintf(stderr," Could not allocate memory for data\n");
return 1;
}
// dynamic 1D array for the image
data2 = malloc( iSize * sizeof(unsigned char) );
if (data2 == NULL )
{
fprintf(stderr," Could not allocate memory for data2\n");
return 1;
}
//
zzs = malloc((sMax) * sizeof(complex long double));
if (zzs == NULL )
{
fprintf(stderr," Could not allocate memory for zzs \n");
return 1;
}
//
zzd = malloc(degree * sizeof(complex long double));
if (zzd == NULL )
{
fprintf(stderr," Could not allocate memory for zzd \n");
return 1;
}
FillArray(data); // make all points white
return 0;
}
int GivePeriod(complex long double z0, int pMax){