{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadaptive.cpp
More file actions
1097 lines (1006 loc) · 36.6 KB
/
Copy pathadaptive.cpp
File metadata and controls
1097 lines (1006 loc) · 36.6 KB
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
//
// Non-Degree Granting Education License -- for use at non-degree
// granting, nonprofit, education, and research organizations only. Not
// for commercial or industrial use.
//
// adaptive.cpp
//
// Code generation for function 'adaptive'
//
// Include files
#include "adaptive.h"
#include "RATMain_internal_types.h"
#include "SLDFunction.h"
#include "acos.h"
#include "allOrAny.h"
#include "eml_mtimes_helper.h"
#include "linspace.h"
#include "minOrMax.h"
#include "repmat.h"
#include "rt_nonfinite.h"
#include "sortrows.h"
#include "coder_array.h"
#include <cmath>
// Function Declarations
namespace RAT
{
static void b_binary_expand_op(::coder::array<boolean_T, 1U> &in1, const
cell_25 *in2, const ::coder::array<real_T, 1U> &in3, real_T in4);
static void binary_expand_op(const ::coder::array<real_T, 2U> &in1, const ::
coder::array<real_T, 2U> &in2, const ::coder::array<real_T, 2U> &in3, ::
coder::array<real_T, 2U> &in4, ::coder::array<real_T, 2U> &in5, ::coder::
array<real_T, 2U> &in6);
static void binary_expand_op(::coder::array<real_T, 2U> &in1, const ::coder::
array<real_T, 2U> &in2, int32_T in3, int32_T in4, int32_T in5, int32_T in6);
static void binary_expand_op(::coder::array<real_T, 2U> &in1, const ::coder::
array<real_T, 2U> &in2, int32_T in3, int32_T in4, int32_T in5);
static void binary_expand_op(::coder::array<creal_T, 1U> &in1, const ::coder::
array<real_T, 1U> &in2, const ::coder::array<real_T, 1U> &in3, const ::coder::
array<real_T, 1U> &in4, const ::coder::array<real_T, 1U> &in5, const ::coder::
array<real_T, 1U> &in6);
static void calculateCentralAngles(const ::coder::array<real_T, 2U> &XYdata,
const real_T dataBoxSize[2], ::coder::array<real_T, 1U> &cornerAngle);
static void calculateTrianglesSides(const ::coder::array<real_T, 2U> &XYdata, ::
coder::array<real_T, 2U> &firstStep, ::coder::array<real_T, 2U> &secondStep,
::coder::array<real_T, 2U> &longStep);
static void increaseSampling(::coder::array<real_T, 2U> &dataPoints, const ::
coder::array<boolean_T, 1U> &segmentsToSplit, const ::coder::array<real_T,
2U> &sldProfile);
static void normalizeFunction(const ::coder::array<real_T, 1U> &x, const ::
coder::array<real_T, 2U> &sldProfile, ::coder::array<real_T, 1U> &y);
static void times(::coder::array<real_T, 1U> &in1, const ::coder::array<real_T,
1U> &in2, const ::coder::array<real_T, 1U> &in3);
}
// Function Definitions
namespace RAT
{
static void b_binary_expand_op(::coder::array<boolean_T, 1U> &in1, const
cell_25 *in2, const ::coder::array<real_T, 1U> &in3, real_T in4)
{
int32_T i;
int32_T in2_idx_0;
int32_T stride_1_0;
in2_idx_0 = in2->f1.size(0) - 2;
if (in3.size(0) == 1) {
i = in2_idx_0;
} else {
i = in3.size(0);
}
in1.set_size(i);
stride_1_0 = (in3.size(0) != 1);
if (in3.size(0) != 1) {
in2_idx_0 = in3.size(0);
}
for (i = 0; i < in2_idx_0; i++) {
in1[i] = (in3[i * stride_1_0] < in4);
}
}
static void binary_expand_op(const ::coder::array<real_T, 2U> &in1, const ::
coder::array<real_T, 2U> &in2, const ::coder::array<real_T, 2U> &in3, ::
coder::array<real_T, 2U> &in4, ::coder::array<real_T, 2U> &in5, ::coder::
array<real_T, 2U> &in6)
{
::coder::array<real_T, 2U> b_in1;
int32_T i;
int32_T loop_ub;
int32_T stride_0_0;
int32_T stride_1_0;
int32_T stride_2_0;
if (in3.size(0) == 1) {
if (in2.size(0) == 1) {
i = in1.size(0);
} else {
i = in2.size(0);
}
} else {
i = in3.size(0);
}
b_in1.set_size(i, 2);
stride_0_0 = (in1.size(0) != 1);
stride_1_0 = (in2.size(0) != 1);
stride_2_0 = (in3.size(0) != 1);
if (in3.size(0) == 1) {
if (in2.size(0) == 1) {
loop_ub = in1.size(0);
} else {
loop_ub = in2.size(0);
}
} else {
loop_ub = in3.size(0);
}
for (i = 0; i < 2; i++) {
for (int32_T i1{0}; i1 < loop_ub; i1++) {
b_in1[i1 + b_in1.size(0) * i] = in1[i1 * stride_0_0 + in1.size(0) * i] /
in2[i1 * stride_1_0 + in2.size(0) * i] - in3[i1 * stride_2_0 +
in3.size(0) * i];
}
}
calculateTrianglesSides(b_in1, in4, in5, in6);
}
static void binary_expand_op(::coder::array<real_T, 2U> &in1, const ::coder::
array<real_T, 2U> &in2, int32_T in3, int32_T in4, int32_T in5, int32_T in6)
{
int32_T i;
int32_T loop_ub;
int32_T stride_0_0;
int32_T stride_1_0;
if ((in6 - in5) + 1 == 1) {
i = (in4 - in3) + 1;
} else {
i = (in6 - in5) + 1;
}
in1.set_size(i, 2);
stride_0_0 = ((in4 - in3) + 1 != 1);
stride_1_0 = ((in6 - in5) + 1 != 1);
if ((in6 - in5) + 1 == 1) {
loop_ub = (in4 - in3) + 1;
} else {
loop_ub = (in6 - in5) + 1;
}
for (i = 0; i < 2; i++) {
for (int32_T i1{0}; i1 < loop_ub; i1++) {
in1[i1 + in1.size(0) * i] = in2[(in3 + i1 * stride_0_0) + in2.size(0) *
i] - in2[(in5 + i1 * stride_1_0) + in2.size(0) * i];
}
}
}
static void binary_expand_op(::coder::array<real_T, 2U> &in1, const ::coder::
array<real_T, 2U> &in2, int32_T in3, int32_T in4, int32_T in5)
{
int32_T i;
int32_T loop_ub;
int32_T stride_0_0;
int32_T stride_1_0;
if (in5 + 1 == 1) {
i = (in4 - in3) + 1;
} else {
i = in5 + 1;
}
in1.set_size(i, 2);
stride_0_0 = ((in4 - in3) + 1 != 1);
stride_1_0 = (in5 + 1 != 1);
if (in5 + 1 == 1) {
loop_ub = (in4 - in3) + 1;
} else {
loop_ub = in5 + 1;
}
for (i = 0; i < 2; i++) {
for (int32_T i1{0}; i1 < loop_ub; i1++) {
in1[i1 + in1.size(0) * i] = in2[(in3 + i1 * stride_0_0) + in2.size(0) *
i] - in2[i1 * stride_1_0 + in2.size(0) * i];
}
}
}
static void binary_expand_op(::coder::array<creal_T, 1U> &in1, const ::coder::
array<real_T, 1U> &in2, const ::coder::array<real_T, 1U> &in3, const ::coder::
array<real_T, 1U> &in4, const ::coder::array<real_T, 1U> &in5, const ::coder::
array<real_T, 1U> &in6)
{
int32_T i;
int32_T loop_ub;
int32_T stride_0_0;
int32_T stride_1_0;
int32_T stride_2_0;
int32_T stride_3_0;
if (in6.size(0) == 1) {
if (in4.size(0) == 1) {
if (in3.size(0) == 1) {
i = in2.size(0);
} else {
i = in3.size(0);
}
} else {
i = in4.size(0);
}
} else {
i = in6.size(0);
}
in1.set_size(i);
stride_0_0 = (in2.size(0) != 1);
stride_1_0 = (in3.size(0) != 1);
stride_2_0 = (in4.size(0) != 1);
stride_3_0 = (in6.size(0) != 1);
if (in6.size(0) == 1) {
if (in4.size(0) == 1) {
if (in3.size(0) == 1) {
loop_ub = in2.size(0);
} else {
loop_ub = in3.size(0);
}
} else {
loop_ub = in4.size(0);
}
} else {
loop_ub = in6.size(0);
}
for (i = 0; i < loop_ub; i++) {
int32_T i1;
i1 = i * stride_2_0;
in1[i].re = ((in2[i * stride_0_0] + in3[i * stride_1_0]) - (in4[i1] +
in5[i1])) / 2.0 / in6[i * stride_3_0];
in1[i].im = 0.0;
}
}
static void calculateCentralAngles(const ::coder::array<real_T, 2U> &XYdata,
const real_T dataBoxSize[2], ::coder::array<real_T, 1U> &cornerAngle)
{
::coder::array<creal_T, 1U> r4;
::coder::array<real_T, 2U> b_XYdata;
::coder::array<real_T, 2U> firstStep;
::coder::array<real_T, 2U> longStep;
::coder::array<real_T, 2U> r;
::coder::array<real_T, 2U> secondStep;
::coder::array<real_T, 1U> firstStepSquared;
::coder::array<real_T, 1U> r1;
::coder::array<real_T, 1U> r2;
::coder::array<real_T, 1U> r3;
::coder::array<real_T, 1U> secondStepSquared;
real_T b_dv[2];
real_T varargin_1;
int32_T i;
int32_T i1;
int32_T k;
// Calculate the central angle of the triangles formed by data points.
// For input size NxM, the output size is (N-2)xN, because the first and the
// last point are not the central corner of any triangle.
// Normalize data, because angles depend on scaling.
// calculate cosine of central angles
coder::repmat(dataBoxSize, static_cast<real_T>(XYdata.size(0)), b_XYdata);
coder::internal::minimum(XYdata, b_dv);
coder::repmat(b_dv, static_cast<real_T>(XYdata.size(0)), r);
if (XYdata.size(0) == 1) {
i = b_XYdata.size(0);
} else {
i = XYdata.size(0);
}
if ((XYdata.size(0) == b_XYdata.size(0)) && (i == r.size(0))) {
b_XYdata.set_size(XYdata.size(0), 2);
k = XYdata.size(0);
for (i = 0; i < 2; i++) {
for (i1 = 0; i1 < k; i1++) {
b_XYdata[i1 + b_XYdata.size(0) * i] = XYdata[i1 + XYdata.size(0) * i] /
b_XYdata[i1 + b_XYdata.size(0) * i] - r[i1 + r.size(0) * i];
}
}
calculateTrianglesSides(b_XYdata, firstStep, secondStep, longStep);
} else {
binary_expand_op(XYdata, b_XYdata, r, firstStep, secondStep, longStep);
}
// calculate area of squares of length of triangle sides
r1.set_size(firstStep.size(0));
k = firstStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = firstStep[i];
r1[i] = varargin_1 * varargin_1;
}
r2.set_size(firstStep.size(0));
k = firstStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = firstStep[i + firstStep.size(0)];
r2[i] = varargin_1 * varargin_1;
}
firstStepSquared.set_size(r1.size(0));
k = r1.size(0);
for (i = 0; i < k; i++) {
firstStepSquared[i] = r1[i] + r2[i];
}
r1.set_size(secondStep.size(0));
k = secondStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = secondStep[i];
r1[i] = varargin_1 * varargin_1;
}
r2.set_size(secondStep.size(0));
k = secondStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = secondStep[i + secondStep.size(0)];
r2[i] = varargin_1 * varargin_1;
}
secondStepSquared.set_size(r1.size(0));
k = r1.size(0);
for (i = 0; i < k; i++) {
secondStepSquared[i] = r1[i] + r2[i];
}
r1.set_size(longStep.size(0));
k = longStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = longStep[i];
r1[i] = varargin_1 * varargin_1;
}
r2.set_size(longStep.size(0));
k = longStep.size(0);
for (i = 0; i < k; i++) {
varargin_1 = longStep[i + longStep.size(0)];
r2[i] = varargin_1 * varargin_1;
}
if (firstStepSquared.size(0) == secondStepSquared.size(0)) {
r3.set_size(firstStepSquared.size(0));
k = firstStepSquared.size(0);
for (i = 0; i < k; i++) {
r3[i] = firstStepSquared[i] * secondStepSquared[i];
}
} else {
times(r3, firstStepSquared, secondStepSquared);
}
i = r3.size(0);
for (k = 0; k < i; k++) {
r3[k] = std::sqrt(r3[k]);
}
if (firstStepSquared.size(0) == 1) {
i = secondStepSquared.size(0);
} else {
i = firstStepSquared.size(0);
}
if (firstStepSquared.size(0) == 1) {
i1 = secondStepSquared.size(0);
} else {
i1 = firstStepSquared.size(0);
}
if (i1 == 1) {
i1 = r1.size(0);
} else if (firstStepSquared.size(0) == 1) {
i1 = secondStepSquared.size(0);
} else {
i1 = firstStepSquared.size(0);
}
if ((firstStepSquared.size(0) == secondStepSquared.size(0)) && (i == r1.size
(0)) && (i1 == r3.size(0))) {
r4.set_size(firstStepSquared.size(0));
k = firstStepSquared.size(0);
for (i = 0; i < k; i++) {
r4[i].re = ((firstStepSquared[i] + secondStepSquared[i]) - (r1[i] + r2[i]))
/ 2.0 / r3[i];
r4[i].im = 0.0;
}
} else {
binary_expand_op(r4, firstStepSquared, secondStepSquared, r1, r2, r3);
}
i = r4.size(0);
for (k = 0; k < i; k++) {
coder::internal::scalar::b_acos(&r4[k]);
}
cornerAngle.set_size(r4.size(0));
k = r4.size(0);
for (i = 0; i < k; i++) {
cornerAngle[i] = r4[i].re;
}
}
static void calculateTrianglesSides(const ::coder::array<real_T, 2U> &XYdata, ::
coder::array<real_T, 2U> &firstStep, ::coder::array<real_T, 2U> &secondStep,
::coder::array<real_T, 2U> &longStep)
{
int32_T i;
int32_T i1;
int32_T i2;
int32_T i3;
int32_T loop_ub;
// Return the sides (deltaX, deltaY) of the triangles formed by data points.
// For input size NxM, the output size is (N-2)xN, because the first and the
// last point are not the central corner of any triangle.
if (XYdata.size(0) - 1 < 2) {
i = 0;
i1 = 0;
} else {
i = 1;
i1 = XYdata.size(0) - 1;
}
if (XYdata.size(0) - 2 < 1) {
i2 = 0;
} else {
i2 = XYdata.size(0) - 2;
}
loop_ub = i1 - i;
if (loop_ub == i2) {
firstStep.set_size(loop_ub, 2);
for (i1 = 0; i1 < 2; i1++) {
for (i2 = 0; i2 < loop_ub; i2++) {
firstStep[i2 + firstStep.size(0) * i1] = XYdata[(i + i2) + XYdata.size
(0) * i1] - XYdata[i2 + XYdata.size(0) * i1];
}
}
} else {
binary_expand_op(firstStep, XYdata, i, i1 - 1, i2 - 1);
}
if (XYdata.size(0) < 3) {
i = 0;
i1 = 0;
i2 = 0;
i3 = 0;
} else {
i = 2;
i1 = XYdata.size(0);
i2 = 1;
i3 = XYdata.size(0) - 1;
}
loop_ub = i1 - i;
if (loop_ub == i3 - i2) {
secondStep.set_size(loop_ub, 2);
for (i1 = 0; i1 < 2; i1++) {
for (i3 = 0; i3 < loop_ub; i3++) {
secondStep[i3 + secondStep.size(0) * i1] = XYdata[(i + i3) +
XYdata.size(0) * i1] - XYdata[(i2 + i3) + XYdata.size(0) * i1];
}
}
} else {
binary_expand_op(secondStep, XYdata, i, i1 - 1, i2, i3 - 1);
}
if (XYdata.size(0) < 3) {
i = 0;
i1 = 0;
} else {
i = 2;
i1 = XYdata.size(0);
}
if (XYdata.size(0) - 2 < 1) {
i2 = 0;
} else {
i2 = XYdata.size(0) - 2;
}
loop_ub = i1 - i;
if (loop_ub == i2) {
longStep.set_size(loop_ub, 2);
for (i1 = 0; i1 < 2; i1++) {
for (i2 = 0; i2 < loop_ub; i2++) {
longStep[i2 + longStep.size(0) * i1] = XYdata[(i + i2) + XYdata.size(0)
* i1] - XYdata[i2 + XYdata.size(0) * i1];
}
}
} else {
binary_expand_op(longStep, XYdata, i, i1 - 1, i2 - 1);
}
}
static void increaseSampling(::coder::array<real_T, 2U> &dataPoints, const ::
coder::array<boolean_T, 1U> &segmentsToSplit, const ::coder::array<real_T,
2U> &sldProfile)
{
::coder::array<real_T, 2U> b_dataPoints;
::coder::array<real_T, 2U> newDataPoints;
::coder::array<real_T, 1U> b_newDataPoints;
::coder::array<real_T, 1U> r4;
::coder::array<int32_T, 1U> r2;
::coder::array<int32_T, 1U> r3;
::coder::array<boolean_T, 1U> r;
::coder::array<boolean_T, 1U> r1;
int32_T b_i;
int32_T input_sizes_idx_0;
int32_T trueCount;
// increaseSampling increase the sampling of an input function
input_sizes_idx_0 = segmentsToSplit.size(0);
trueCount = 0;
for (int32_T i{0}; i < input_sizes_idx_0; i++) {
if (segmentsToSplit[i]) {
trueCount++;
}
}
newDataPoints.set_size(trueCount, 2);
for (int32_T i{0}; i < 2; i++) {
for (b_i = 0; b_i < trueCount; b_i++) {
newDataPoints[b_i + newDataPoints.size(0) * i] = 0.0;
}
}
r.set_size(segmentsToSplit.size(0) + 1);
trueCount = segmentsToSplit.size(0);
for (int32_T i{0}; i < trueCount; i++) {
r[i] = segmentsToSplit[i];
}
r[segmentsToSplit.size(0)] = false;
r1.set_size(segmentsToSplit.size(0) + 1);
r1[0] = false;
trueCount = segmentsToSplit.size(0);
for (int32_T i{0}; i < trueCount; i++) {
r1[i + 1] = segmentsToSplit[i];
}
input_sizes_idx_0 = r.size(0) - 1;
trueCount = 0;
for (int32_T i{0}; i <= input_sizes_idx_0; i++) {
if (r[i]) {
trueCount++;
}
}
r2.set_size(trueCount);
trueCount = 0;
for (int32_T i{0}; i <= input_sizes_idx_0; i++) {
if (r[i]) {
r2[trueCount] = i + 1;
trueCount++;
}
}
input_sizes_idx_0 = r1.size(0) - 1;
trueCount = 0;
for (int32_T i{0}; i <= input_sizes_idx_0; i++) {
if (r1[i]) {
trueCount++;
}
}
r3.set_size(trueCount);
trueCount = 0;
for (int32_T i{0}; i <= input_sizes_idx_0; i++) {
if (r1[i]) {
r3[trueCount] = i + 1;
trueCount++;
}
}
if (r2.size(0) == r3.size(0)) {
trueCount = r2.size(0);
for (int32_T i{0}; i < trueCount; i++) {
newDataPoints[i] = 0.5 * (dataPoints[r2[i] - 1] + dataPoints[r3[i] - 1]);
}
} else {
binary_expand_op(newDataPoints, dataPoints, r2, r3);
}
b_newDataPoints.set_size(newDataPoints.size(0));
trueCount = newDataPoints.size(0);
for (int32_T i{0}; i < trueCount; i++) {
b_newDataPoints[i] = newDataPoints[i];
}
normalizeFunction(b_newDataPoints, sldProfile, r4);
trueCount = r4.size(0);
for (int32_T i{0}; i < trueCount; i++) {
newDataPoints[i + newDataPoints.size(0)] = r4[i];
}
// For simplicity append the new points at the end and then sort.
if (dataPoints.size(0) != 0) {
trueCount = dataPoints.size(0);
} else {
trueCount = 0;
}
if (newDataPoints.size(0) != 0) {
input_sizes_idx_0 = newDataPoints.size(0);
} else {
input_sizes_idx_0 = 0;
}
if (newDataPoints.size(0) != 0) {
b_i = newDataPoints.size(0);
} else {
b_i = 0;
}
b_dataPoints.set_size(trueCount + b_i, 2);
for (int32_T i{0}; i < 2; i++) {
for (b_i = 0; b_i < trueCount; b_i++) {
b_dataPoints[b_i + b_dataPoints.size(0) * i] = dataPoints[b_i +
trueCount * i];
}
}
for (int32_T i{0}; i < 2; i++) {
for (b_i = 0; b_i < input_sizes_idx_0; b_i++) {
b_dataPoints[(b_i + trueCount) + b_dataPoints.size(0) * i] =
newDataPoints[b_i + input_sizes_idx_0 * i];
}
}
dataPoints.set_size(b_dataPoints.size(0), 2);
trueCount = b_dataPoints.size(0);
for (int32_T i{0}; i < 2; i++) {
for (b_i = 0; b_i < trueCount; b_i++) {
dataPoints[b_i + dataPoints.size(0) * i] = b_dataPoints[b_i +
b_dataPoints.size(0) * i];
}
}
coder::sortrows(dataPoints);
}
static void normalizeFunction(const ::coder::array<real_T, 1U> &x, const ::
coder::array<real_T, 2U> &sldProfile, ::coder::array<real_T, 1U> &y)
{
::coder::array<real_T, 1U> r;
int32_T i;
// Subfunctions
// NORMALIZEFUNCTION evaluates a function and returns a NxM array, where N
// is the number of elements of x and M is the number of outputs of func.
// All the outputs of func must be scalar.
// The optional parameter 'vectorizable' (default false) allows to specify
// that the input function can be vectorized.
// Modified by AVH for use with coder
// if (~exist('vectorizable','var') || isempty(vectorizable))
// end
// abs(nargout(func)); %for anonymous functions nargout<0
y.set_size(x.size(0));
// if vectorizable
// % For uniformity reasons, transform the 'x' array into a column vector.
// % In this way it does not matter if it is given as a column or a row
// % vector.
// [newValues{:}] = func(x(:));
// y = cell2mat(newValues);
// else
i = x.size(0);
for (int32_T b_i{0}; b_i < i; b_i++) {
// Remove cell array so no need for cell2mat
// which won't compile - AVH
// [newValues{:}] = func(x(i));
// y(i,:) = cell2mat(newValues);
b_SLDFunction(x[b_i], sldProfile, r);
y[b_i] = r[0];
}
// end
}
static void times(::coder::array<real_T, 1U> &in1, const ::coder::array<real_T,
1U> &in2, const ::coder::array<real_T, 1U> &in3)
{
int32_T i;
int32_T loop_ub;
int32_T stride_0_0;
int32_T stride_1_0;
if (in3.size(0) == 1) {
i = in2.size(0);
} else {
i = in3.size(0);
}
in1.set_size(i);
stride_0_0 = (in2.size(0) != 1);
stride_1_0 = (in3.size(0) != 1);
if (in3.size(0) == 1) {
loop_ub = in2.size(0);
} else {
loop_ub = in3.size(0);
}
for (i = 0; i < loop_ub; i++) {
in1[i] = in2[i * stride_0_0] * in3[i * stride_1_0];
}
}
void adaptive(const ::coder::array<real_T, 2U> &sldProfile, const real_T
startDomain[2], real_T minAngle, real_T nPoints, cell_25 *out)
{
::coder::array<real_T, 2U> b_out;
::coder::array<real_T, 2U> r;
::coder::array<real_T, 1U> cornerAngle;
::coder::array<real_T, 1U> hiVal;
::coder::array<real_T, 1U> newDomain;
::coder::array<boolean_T, 1U> b_trianglesToRefine;
::coder::array<boolean_T, 1U> r1;
::coder::array<boolean_T, 1U> segmentsToSplit;
::coder::array<boolean_T, 1U> trianglesToRefine;
int32_T loop_ub;
int32_T nRefinements;
boolean_T exitg1;
// adaptive: evaluates a matlab function on a given range
//
// 'adaptive.m' allows to sample a function using a reduced number of
// points. It works iteratively adding new points where needed.
// It is especially useful for functions which are computationally intensive
// (e.g. involve solving a differential equation).
//
// Usage:
// XY = adaptive(func, [xstart, xend])
// evaluates 'func' in the range [xstart, xend]. Key-value arguments are
// used to control the function evaluation. If the function 'func' returns
// multiple output values, only the first one is used for the refinement
// process, but all of them are calculated and returned as additional
// columns in the output matrix. The output matrix XY contains the new
// domain points in the first column and the output values in the other
// columns.
// [x,yy] = adaptive(func, [xstart, xend])
// as before but separately returns the array with the domain points and
// the array/matrix with the function output values.
// [x,yy] = adaptive(func, xarray, ...)
// as before but explicitly provide an initial array of domain points.
//
// Methods:
// 'adaptive' provides three methods for refining the function evaluation:
// 1) add more points near the sharp corners, which are found by
// considering the triangles formed by three successive points and
// measuring the central angle.
// 2) measure the area of the same triangles and add more points when the
// area is bigger than a threshold.
// 3) measure the length of the segments formed by pairs of successive
// and split the segments which are longer than a threshold.
// If no methods is explicitly specified, the 'angle' method is used.
//
// Input parameters
// - func: input function (function handle)
// - initialDomain: initial domain points (1D array)
//
// Optional key-value input parameters
// - 'nPoints': (default 20)
// initial number of domain points, only used if an initial domain
// array is not excplitely provided.
// - 'maxRefinements': (default 10)
// Specifies the maximum number of refinement steps.
// - 'minAngle': (default 0.8*pi)
// Refine near the points which forms, together with their left and right
// neighbours, a triangle with central angle smaller than a given value.
// - 'maxArea': (default 5e-4)
// Refine near the points which forms, together with their left and right
// neighbours, a triangle with area larger than a threshold. The threshold
// in normalized to the area enclosing th graph:
// threshold==maxArea*(max(x)-min(x))*(max(f(x))-min(f(x)))
// - 'maxLength': (default Inf)
// Refine all the sements which are longer than a given threshold. The
// threshold is relative to the input and output ranges. Specifically,
// before applying the threshold, the data are normalized so that
// max(x)-min(x)==1 and max(f(x))-min(f(x))==1.
// - 'minLength': (default 0)
// Exclude from the refinement process the segments which are shorter
// than a given threshold. The threshold is relative to the input and
// output ranges. Specifically, before applying the threshold, the
// data are normalized so that max(x)-min(x)==1 and max(f(x))-min(f(x))==1.
// - 'minSignal': (default 0.2)
// Exclude from the refinement process the points where the function is
// below a threshold. The threshold is relative to the output range: In
// this example threshold == 0.01*(max(f(x))-min(f(x))).
// - 'vectorizable': (default false)
// Specifies whether the input function accepts arrays as input
// (e.g. f(x)==x.^2).
// - 'waitbar': (default false)
// Display a waitbar.
//
// Output parameters
// - a NxM array where N is the number of domain points and M is the number
// of output parameters of the input function.
//
// Examples:
//
// % Refine a function near sharp corners. The option 'minAngle' is useful
// % for having more points near the peaks of the function.
// f = @(x) exp(-x.^2/4).*sin(3*x);
// % for test-purpose also evaluate the function directly
// x2 = -10:0.01:10;
// y2 = f(x2);
// y = adaptive(f, [-5,5], 'minAngle',0.8*pi);
// figure(1); plot(x2,f(x2),'k--',y(:,1),y(:,2),'o-');
// legend('high sampling','adaptive')
// title('y = adaptive(f, [xstart, xend], ''minAngle'',0.8*pi)')
// % as before but starting with an inital array of domain points
// x = -5:5;
// y = adaptive(f, x, 'minAngle',0.8*pi);
// figure(2); plot(x,f(x),'s-',x2,f(x2),'k--',y(:,1),y(:,2),'o-');
// legend('initial sampling','high sampling','adaptive')
// title('y = adaptive(f, x, ''minAngle'',0.8*pi)')
//
// % Refine a function near sharp corners, but do not split segments which
// % are already shorter than 'minLength'.
// y = adaptive(f, x, 'minAngle',0.8*pi, 'minLength',0.05);
// figure(3); plot(x,f(x),'s-',x2,f(x2),'k--',y(:,1),y(:,2),'o-');
// legend('initial sampling','high sampling','adaptive')
// title('y = adaptive(f, x, ''minAngle'',0.8*pi, ''minLength'',0.05)');
//
// % Refine a function until the areas of the triangles formed by
// % triplets of successive points are smaller than 'maxArea'.
// y = adaptive(f, x, 'maxArea',1e-3);
// figure(4); plot(x,f(x),'s-',x2,f(x2),'k--',y(:,1),y(:,2),'o-');
// legend('initial sampling','high sampling','adaptive')
// title('y = adaptive(f, x, ''maxArea'',1e-3)')
//
// % Refine a function until the segments formed by pairs of successive
// % points are shorter than 'maxLength'.
// y = adaptive(f, x, 'maxLength',0.1);
// figure(5); plot(x,f(x),'s-',x2,f(x2),'k--',y(:,1),y(:,2),'o-');
// legend('initial sampling','high sampling','adaptive')
// title('y = adaptive(f, x, ''maxLength'',0.1)');
// Copyright
// 2017, Alberto Comin - LMU Muenchen
// Version changes:
//
// 24/01/2017: 1) new default: when no optional argument is given, use the
// 'angle' method as default 2) it is now possible to provide just the
// start and the end of the function domain, instead of having to
// explicitly provide an initial array 3) a new key-word argument
// 'nPoints' controls the number of initial domain points in the cases when
// the initial array is not explicitly provided. 4) it is now possible to
// return the domain points and the function values either as a single 2D
// array or as two separate arrays.
// 25/01/2017: fixed defaults for the case when no method is specified
// Default settings
// nPoints = 20;
// minAngle = 0.8*pi;
// units normalized to data range
// Test-mode
// The test mode is activated by calling 'adaptive.m' with no input.
// if nargin==0
// initialDomain = -10:10;
// input_func = @(x) 100*exp(-(x+5.2).^2) + 50*exp(-5*(x-0.5).^2)+ 20*exp(-10*(x-5.8).^2);
// thresholdingAngles = true;
// minAngle = 0.8*pi;
// thresholdingLength = true;
// minLength = 0.02;
// disp('Running adaptive.m in test mode');
// fprintf('input function: %s\n',func2str(input_func));
// disp('Plotting the function on a initial set of points');
// testFigureHandle = figure();
// plot(initialDomain, input_func(initialDomain),'bs-','LineWidth',1.5);
// grid on; xlabel('x'); ylabel('y'); title('adaptive.m example');
// end
// Processing input arguments
// assert(isa(input_func,'function_handle'),'adaptiveFunctionEvaluation:ArgChk',...
// 'the first argument must be a function handle');
// assert(isnumeric(initialDomain) && isvector(initialDomain),...
// 'adaptiveFunctionEvaluation:ArgChk','initial points must be specified as a numeric vector');
//
// nExtraArgIn = numel(varargin);
// if mod(nExtraArgIn,2)==1
// error('adaptiveFunctionEvaluation:ArgChk', ...
// 'At least a key or a value is missing in the key-value arguments list.');
// end
// usingDefaultMethod = true;
// n = 1;
// minAngle = 0.7 * pi;
// thresholdingAngles = true;
// nPoints = 50;
// while n < nExtraArgIn
// switch lower(varargin{n})
// case 'minangle'
// minAngle = varargin{n+1};
// n = n+2;
// case 'maxarea'
// maxArea = varargin{n+1};
// thresholdingArea = true;
// usingDefaultMethod = false;
// n = n+2;
// case 'maxlength'
// maxLength = varargin{n+1};
// thresholdingLength = true;
// usingDefaultMethod = false;
// n = n+2;
// case 'minlength'
// minLength = varargin{n+1};
// thresholdingLength = true;
// n = n+2;
// case 'minsignal'
// minSignal = varargin{n+1};
// thresholdingSignal = true;
// n = n+2;
// case 'npoints'
// nPoints = varargin{n+1};
// n = n+2;
// case 'vectorize'
// vectorizable = varargin{n+1};
// n = n+2;
// case 'maxrefinements'
// maxRefinements = varargin{n+1};
// n = n+2;
// case 'waitbar'
// displayWaitbar = varargin{n+1};
// n = n+2;
// otherwise
// error('adaptiveFunctionEvaluation:ArgChk',...
// ['unknown keyword argument: ', varargin{n}]);
// end
// end
// if no method is specified use the 'angle' method as default
// if usingDefaultMethod
// thresholdingAngles = true;
// end
// Initial function evaluation
// if initialDomain only contains the start and the end points, create a new
// array with 'nPoints' points.
coder::linspace(startDomain[0], startDomain[1], nPoints, r);
newDomain.set_size(r.size(1));
loop_ub = r.size(1);
for (int32_T i{0}; i < loop_ub; i++) {
newDomain[i] = r[i];
}
// Normalize the input function: This step allows to use the same syntax for
// functions with single or multiple output parameters.
// Remove this syntax for compile - AVH
// func = @(x) normalizeFunction(x,sldProfile,vectorizable);
// Evaluate the input function on the initial set of points.
normalizeFunction(newDomain, sldProfile, hiVal);
// dataPoints = [initialDomain(:), func(initialDomain(:))];
out->f1.set_size(newDomain.size(0), 2);
loop_ub = newDomain.size(0);
for (int32_T i{0}; i < loop_ub; i++) {
out->f1[i] = newDomain[i];
}
loop_ub = hiVal.size(0);
for (int32_T i{0}; i < loop_ub; i++) {
out->f1[i + out->f1.size(0)] = hiVal[i];
}
// Iterative function refinement
// if displayWaitbar
// refinementWaitbar = waitbar(0,['Evaluating function ',func2str(func)],...
// 'CreateCancelBtn','setappdata(gcbf,''canceling'',true)');
// setappdata(refinementWaitbar,'canceling',false)
// end
nRefinements = 0;
exitg1 = false;
while ((!exitg1) && (nRefinements < 10)) {
real_T b_dv[2];
real_T b_dv1[2];
boolean_T y;
// calculate the box which encloses the current data points:
// Each point is considered as the central corner of the triangle formed
// with its left and right hand side neighbours. The first and the last
// points are not the central corner of any triangle, so for N points
// there are only N-2 triangles.
// if thresholdingArea
// triangleArea = calculateTrianglesArea(dataPoints(:,1:2));
// bigTriangles = triangleArea > (maxArea * dataBoxArea);
// trianglesToRefine = trianglesToRefine | bigTriangles;
// end
b_out.set_size(out->f1.size(0), 2);
loop_ub = out->f1.size(0);
for (int32_T i{0}; i < 2; i++) {
for (int32_T i1{0}; i1 < loop_ub; i1++) {
b_out[i1 + b_out.size(0) * i] = out->f1[i1 + out->f1.size(0) * i];
}
}
coder::internal::maximum(b_out, b_dv);
b_out.set_size(out->f1.size(0), 2);
loop_ub = out->f1.size(0);
for (int32_T i{0}; i < 2; i++) {
for (int32_T i1{0}; i1 < loop_ub; i1++) {
b_out[i1 + b_out.size(0) * i] = out->f1[i1 + out->f1.size(0) * i];
You can’t perform that action at this time.
