-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTypes.h
4187 lines (3899 loc) · 98.3 KB
/
MyTypes.h
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
#pragma once
#ifndef MYTYPE_H
#define MYTYPE_H
// ver = 29.3 18/03/1400
// for linux add this switch to QT .pro file
//QMAKE_CXXFLAGS += -std=c++11
//CONFIG += c++11
#define Use_MKL 1 // if you want use MKL Library Must Set UseMKL To 1 and Doing Below Step For Add MKL Library To Project ; if you dont need MKL Functions Set UseMKL to 0
#define Use_Ipp 0 // if you want use IPP Library Must Set UseIpp To 1 and Doing Below Step For Add IPP Library To Project ; if you dont need IPP Functions Set UseIpp to 0
#define Use_MATLAB 0 // if you want use MATLAB Must Set Use_MATLAB To 1 and Doing Below Step For Add Use_MATLAB Library To Project ; if you dont need MATLAB Functions Set Use_MATLAB to 0
// For use this Class Must Do This :
// 1. Add MATLAB Include Directory Path To "Additional Include Directory"
// "C:\Program Files\MATLAB\R2017b\extern\include"
//
// 2. Add MATLAB LIB Directory Path To "Additional Library Directory"
// "C:\Program Files\MATLAB\R2017b\extern\lib\win64\microsoft" for built 64bit assemblies
//
// 3. Add "libMatlabEngine.lib"; "libMatlabDataArray.lib"; To "Additional Dependencies" for built 64bit assemblies
// 4. Add Path="C:\Program Files\MATLAB\R2017b\extern\bin\win64" to Environment Variables for Run 64bit assemblies
// 5. Set Matlab shared Engine name : matlab.engine.shareEngine('SME')
//
#include <cstring> //for memset
// MemManager Always return align memory with DataType size alignment.
// MyComplexClass V5.1 For Use With MKL Library and Ipp Library
//------------------------------Intel Compiler 11---------------------
// For use this Class Must Do This :
// 1. Add MKL Include Directory Path To "Additional Include Directory"
// "C:\Program Files\Intel\Compiler\11.0\066\cpp\mkl\include"
//
// 2. Add MKL LIB Directory Path To "Additional Library Directory"
// "C:\Program Files\Intel\Compiler\11.0\066\cpp\mkl\ia32\lib" for built 32bit assemblies
// "C:\Program Files\Intel\Compiler\11.0\066\cpp\mkl\em64t\lib" for built 64bit assemblies
//
// 3. Add "mkl_intel_c_dll.lib" ; "mkl_core_dll.lib" To "Additional Dependencies" for built 32bit assemblies
// ADD "mkl_intel_lp64_dll.lib"; "mkl_core_dll.lib" To "Additional Dependencies" for built 64bit assemblies
// MKL_THREADING_LAYER :
// A. MKL_THREADING_INTEL
// for intel threading, mkl functions run multithread with intel thread threading. use "mkl_intel_thread_dll.lib" to utilize intel threading.
// in this mode application is not threaded and since intel use omp for threading it also need libiomp5.lib
//
// B. MKL_THREADING_SEQUENTIAL
// for sequential mode of Intel MKL. Application could be threaded and doesnt need libiomp5.lib.
//
// note : MKL_THREADING_LAYER could be selected by mkl_set_threading_layer() funtion programmitically.
//---------------------------------------------------------------------
//------------Intel Parallel Studio XE 2015 Composer Edition-----------
// For use MKL Must Do This :
// 1. Add MKL Include Directory Path To "Additional Include Directory"
// "C:\Program Files (x86)\Intel\Composer XE 2015\mkl\include"
//
// 2. Add MKL LIB Directory Path To "Additional Library Directory"
// "C:\Program Files (x86)\Intel\Composer XE 2015\mkl\lib\ia32" for built 32bit assemblies
// "C:\Program Files (x86)\Intel\Composer XE 2015\mkl\lib\intel64" for built 64bit assemblies
//
// 3. Add "mkl_intel_c_dll.lib" ; "mkl_core_dll.lib" To "Additional Dependencies" for built 32bit assemblies
// ADD "mkl_intel_lp64_dll.lib"; "mkl_core_dll.lib" To "Additional Dependencies" for built 64bit assemblies
//
// MKL_THREADING_LAYER :
// A. MKL_THREADING_INTEL
// for intel threading, mkl functions run multithread with intel thread threading. use "mkl_intel_thread_dll.lib" to utilize intel threading.
// in this mode application is not threaded and since intel use omp for threading it also need libiomp5.lib
//
// B. MKL_THREADING_SEQUENTIAL
// for sequential mode of Intel MKL. Application could be threaded and doesnt need libiomp5.lib.
//
// note : MKL_THREADING_LAYER could be selected by mkl_set_threading_layer() funtion programmitically.
//
// 4. Add Path="C:\Program Files (x86)\Intel\Composer XE 2015\redist\ia32\mkl" to Environment Variables for Run 32bit assemblies
// Add Path="C:\Program Files (x86)\Intel\Composer XE 2015\redist\intel64\mkl" to Environment Variables for Run 64bit assemblies
#include <vector>
#include <time.h>
#include <stdexcept>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <stdio.h>
#include "MemoryManagerClass.h"
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
#if Use_Ipp==1
#include<ippcore.h>
#include<ippvm.h>
#include<ipps.h>
#pragma comment(lib;"ipps")
#endif
#if Use_MKL==1
#include <mkl_vsl.h>
#include <mkl.h>
#include <mkl_dfti.h>
//#pragma comment(lib;"mkl_intel_lp64_dll")
//#pragma comment(lib;"mkl_intel_thread_dll")
//#pragma comment(lib;"mkl_core_dll")
#endif
#if Use_MATLAB
#include "MatlabEngine.hpp"
#define MATLABSharedEngineName "SME"
// run this by MATLAB : matlab.engine.shareEngine('SME')
using namespace matlab::engine;
#endif
// it is impossible to define assignment overload as non member function according to below page:
// en.cppreference.com/w/cpp/language/operator
//
#define PTcArr64 TcArr64
#define PTcArr32 TcArr32
#define PTcArrInt TcArrInt
#define PTcArrShort TcArrShort
#define PTcArrChar TcArrChar
#define PTdArr TdArr
#define PTfArr TfArr
#define PTsArr TsArr
#define PTintArr TintArr
#define PTCharArr TCharArr
#define PTbArr TbArr
#define PTcArr2d64 TcArr2d64
#define PTcArr2d32 TcArr2d32
#define PTcArr2dInt TcArr2dInt
#define PTcArr2dShort TcArr2dShort
#define PTcArr2dChar TcArr2dChar
#define PTdArr2d TdArr2d
#define PTfArr2d TfArr2d
#define PTsArr2d TsArr2d
#define PTCharArr2d TCharArr2d
#define PTintArr2d TintArr2d
#define END(x) (x.Length-1)
#ifdef _DEBUG
#define DebugBuild
#endif
#if Use_MATLAB ==1
#include <type_traits>
enum MatlabColor
{
yellow='y',
magenta='m',
cyan='c',
red = 'r',
green='g',
blue='b',
white='w',
black='k',
};
class DimensionClass
{
public :
int Row;
int Column;
};
class MATLABEngineClass
{
public:
MATLABEngineClass(string MatlabSharedEngineName)
{
if (!_CS)
{
_CS = new CRITICAL_SECTION();
InitializeCriticalSection(_CS);
}
if (_inited==false)
{
Sleep(10); // waiting for when two thread call class constructor simultounsly and create two _CS . waiting last thread fill _CS
}
// test is matlab engine shared (matlab command) : matlab.engine.isEngineShared
// for sharing matlab engine (matlab command) : matlab.engine.shareEngine(name)
_MatlabSharedEngineName = MatlabSharedEngineName;
if (!_matlabptr && !_inited)
{
EnterCriticalSection(_CS);
if (!_matlabptr )
{
char16_t* m_str = Getchar16(_MatlabSharedEngineName);
try
{
_matlabptr = connectMATLAB(m_str);
}
catch (...)
{
}
free(m_str);
}
LeaveCriticalSection(_CS);
}
_inited = true;
}
double DiffValue(double* CppArr,int CppArrLength, string MatlabVarName)
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
int MatlabVarLength=GetMatlabVariableLength(MatlabVarName);
if (MatlabVarLength!=CppArrLength)
{
return -1;
}
if (NeedTranspose(MatlabVarName,CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "sum(abs(%s-%s))", MatlabVarName.c_str(), CppVarname.c_str());
Eval(cmd);
double ans = GetMatlabScalarVariable("ans");
return ans;
}
void Plot(double* CppArr, int CppArrLength,string title="CppAns",MatlabColor lineColor=MatlabColor::red,string Legend="CppVar")
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "plot(%s,'%c'), title('%s'), legend('%s')", CppVarname.c_str(),color,title.c_str(),Legend.c_str());
Eval(cmd);
}
void ScatterPlot(double* CppArr, int CppArrLength, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "plot(%s,'.%c'), title('%s'), legend('%s')", CppVarname.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void Stem(double* CppArr, int CppArrLength, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "stem(%s,'%c'), title('%s'), legend('%s')", CppVarname.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void StemDiffIndex(double* CppArr, int CppArrLength, string MatlabVarName, double Tolerance = 0.0000000001, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "CppDiff = %s-%s ; CppDiffindex= find(abs(CppDiff)>%0.15lf); if isempty(CppDiffindex)stem(CppDiffindex) ; else stem(1:length(CppAns),zeros(length(CppAns),1)) ; hold on ; stem(CppDiffindex,CppDiff(CppDiffindex),'%c');hold off;, title('%s'), legend('%s');ylim([min(CppDiff)-2 max(CppDiff)+2]); end", CppVarname.c_str(), MatlabVarName.c_str(), Tolerance, color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void PlotDiffValue(double* CppArr, int CppArrLength, string MatlabVarName, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "plot(%s-%s,'%c'), title('%s'), legend('%s')", CppVarname.c_str(), MatlabVarName.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
bool isSame(double* CppArr, int CppArrLength, string MatlabVarName, double Tolerance = 0.0000000001)
{
DimensionClass dim = GetVariableDimension(MatlabVarName);
string CppVarname = "CppAns";
SetVariable(CppArr, CppArrLength, CppVarname);
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "CppDiff = %s-%s ; CppDiffindex= find(abs(CppDiff)>%0.15lf); ", CppVarname.c_str(), MatlabVarName.c_str(), Tolerance);
Eval(cmd);
int differenceIndexCount = GetMatlabVariableLength("CppDiffindex");
if (differenceIndexCount == 0)
{
return true;
}
return false;
}
double* GetMatlabArrayVariable(string MatlabVarName, int &Length)
{
auto m_str = Getchar16(MatlabVarName);
double* arr = nullptr;
try
{
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
// auto dim=result_m.getDimensions();
Length = result_m.getNumberOfElements();
arr = (double*)malloc(sizeof(double*)*Length);
for (int i = 0; i < Length; i++)
{
//std::complex<short> a(result_m[i]);
arr[i] = result_m[i];
}
}
catch (...)
{
arr = nullptr;
}
return arr;
}
double GetMatlabScalarVariable(string MatlabVarName)
{
auto m_str = Getchar16(MatlabVarName);
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
return result_m[0];
}
int GetMatlabVariableLength(string MatlabVarName)
{
char ss[300] = { 0 };
sprintf(ss, "length(%s)", MatlabVarName.c_str());
string str3 = string(ss);
auto m_str = Getchar16(str3);
_matlabptr->eval(m_str);
free(m_str);
m_str = Getchar16("ans");
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
int length = result_m[0];
return length;
}
void SetVariable(double* CppArr, int CppArrLength, string VarName)
{
matlab::data::ArrayFactory factory;
auto data = factory.createArray<double>({ 1, (unsigned __int64)CppArrLength }, &CppArr[0], &CppArr[CppArrLength]);
auto m_str = Getchar16(VarName);
_matlabptr->setVariable(m_str, data);
free(m_str);
}
double DiffValue_Complex(std::complex<double>* CppArr, int CppArrLength, string MatlabVarName)
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
int MatlabVarLength = GetMatlabVariableLength_Complex(MatlabVarName);
if (MatlabVarLength != CppArrLength)
{
return -1;
}
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "sum(abs(%s-%s))", MatlabVarName.c_str(), CppVarname.c_str());
Eval(cmd);
double ans = GetMatlabScalarVariable("ans");
return ans;
}
void Plot_Complex(std::complex<double>* CppArr, int CppArrLength, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "plot(%s,'%c'), title('%s'), legend('%s')", CppVarname.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void ScatterPlot_Complex(std::complex<double>* CppArr, int CppArrLength, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "plot(%s,'.%c'), title('%s'), legend('%s')", CppVarname.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void Stem_Complex(std::complex<double>* CppArr, int CppArrLength, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "stem(%s,'%c'), title('%s'), legend('%s')", CppVarname.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
void StemDiffIndex_Complex(std::complex<double>* CppArr, int CppArrLength, string MatlabVarName, double Tolerance = 0.0000000001, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char color = (char)lineColor;
char cmd[500] = { 0 };
sprintf(cmd, "CppDiff = abs(%s)-abs(%s) ; CppDiffindex= find(abs(CppDiff)>%0.15lf);if isempty(CppDiffindex)stem(CppDiffindex) ; else stem(1:length(CppAns),zeros(length(CppAns),1)) ; hold on ; stem(CppDiffindex,CppDiff(CppDiffindex),'%c');hold off;, title('%s'), legend('%s');ylim([min(CppDiff)-2 max(CppDiff)+2]); end", CppVarname.c_str(), MatlabVarName.c_str(), Tolerance, color, title.c_str(), Legend.c_str());
Eval(cmd);
}
bool isSame_Complex(std::complex<double>* CppArr, int CppArrLength, string MatlabVarName, double Tolerance = 0.0000000001)
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "CppDiff = abs(%s)-abs(%s) ; CppDiffindex= find(abs(CppDiff)>%0.15lf); ", CppVarname.c_str(), MatlabVarName.c_str(), Tolerance);
Eval(cmd);
int differenceIndexCount=GetMatlabVariableLength("CppDiffindex");
if (differenceIndexCount==0)
{
return true;
}
return false;
}
void PlotDiffValue_Complex(std::complex<double>* CppArr, int CppArrLength, string MatlabVarName, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
string CppVarname = "CppAns";
SetVariable_Complex(CppArr, CppArrLength, CppVarname);
char color = (char)lineColor;
if (NeedTranspose(MatlabVarName, CppArrLength))
{
MatlabVarName = "transpose(" + MatlabVarName + ")";
}
char cmd[500] = { 0 };
sprintf(cmd, "plot(abs(%s)-abs(%s),'%c'), title('%s'), legend('%s')", CppVarname.c_str(), MatlabVarName.c_str(), color, title.c_str(), Legend.c_str());
Eval(cmd);
}
std::complex<double>* GetMatlabArrayVariable_Complex(string MatlabVarName, int &Length)
{
auto m_str = Getchar16(MatlabVarName);
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
// auto dim=result_m.getDimensions();
Length = result_m.getNumberOfElements();
std::complex<double>* arr = (std::complex<double>*)malloc(sizeof(std::complex<double>)*Length);
for (int i = 0; i < Length; i++)
{
//std::complex<short> a(result_m[i]);
arr[i] = complex<double>(result_m[i]);
}
return arr;
}
std::complex<double> GetMatlabScalarVariable_Complex(string MatlabVarName)
{
auto m_str = Getchar16(MatlabVarName);
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
return result_m[0];
}
int GetMatlabVariableLength_Complex(string MatlabVarName)
{
char ss[300] = { 0 };
sprintf(ss, "length(%s)", MatlabVarName.c_str());
string str3 = string(ss);
auto m_str = Getchar16(str3);
_matlabptr->eval(m_str);
free(m_str);
m_str = Getchar16("ans");
auto result_m = _matlabptr->getVariable(m_str);
free(m_str);
int length = result_m[0];
return length;
}
void SetVariable_Complex(std::complex<double>* CppArr, int CppArrLength, string VarName)
{
matlab::data::ArrayFactory factory;
auto data = factory.createArray<std::complex<double>>({ 1, (unsigned __int64)CppArrLength }, &CppArr[0], &CppArr[CppArrLength]);
auto m_str = Getchar16(VarName);
_matlabptr->setVariable(m_str, data);
free(m_str);
}
void Eval(const char* cmd)
{
string str3 = string(cmd);
auto m_str = Getchar16(str3);
_matlabptr->eval(m_str);
free(m_str);
}
void HoldOn()
{
Eval("hold on");
}
void HoldOff()
{
Eval("hold off");
}
void Figure()
{
Eval("figure()");
}
DimensionClass GetVariableDimension(string MatlabVarName)
{
DimensionClass dim;
char cmd[500] = { 0 };
sprintf(cmd, "CppArrayDim=size(%s);",MatlabVarName.c_str());
Eval(cmd);
int length = 0;
double* dimlength = GetMatlabArrayVariable("CppArrayDim", length);
dim.Row = dimlength[0];
if (length>0)
{
dim.Column = dimlength[1];
}
if (dimlength)
{
free(dimlength);
}
return dim;
}
bool NeedTranspose(string MatlabVarName, int CppVarColumnCount, int CppVarRowcount=1)
{
DimensionClass dimlen= GetVariableDimension(MatlabVarName);
if (CppVarRowcount == dimlen.Row && CppVarColumnCount == dimlen.Column)
{
// it is square matrix
return false;
}
if (CppVarRowcount == dimlen.Column && CppVarColumnCount == dimlen.Row)
{
return true;
}
return false;
}
~MATLABEngineClass()
{
//matlab::engine::terminateEngineClient();
}
private:
char16_t* Getchar16(string cmd)
{
int length = cmd.length();
char16_t* cmd16 = (char16_t*)malloc(sizeof(char16_t)* (length + 1));
for (int i = 0; i < length; i++)
{
cmd16[i] = cmd[i];
}
cmd16[length] = 0;
return cmd16;
}
static std::unique_ptr<MATLABEngine> _matlabptr;
string _MatlabSharedEngineName;
static CRITICAL_SECTION *_CS;
static bool _inited;
// static variables should define outside of class body again.
// if class is Template : static variable define should placed in .h file
// if class isn't Template : static variable define should placed in .cpp file.
};
// Forward declaration of TArr for using in MATLABClass
template<class T,class Tptr>
class TArr;
template<class T,class Tptr>
class MATLABClass
{
public:
MATLABClass(TArr<T, Tptr> *A)
{
_SourceArr = A;
_MatlabEng = new MATLABEngineClass(MATLABSharedEngineName);
}
double Diff(string MatlabVarName)
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
double diff = _MatlabEng->Diff(srcdouble, _SourceArr->Length, MatlabVarName);
free(srcdouble);
return diff;
}
void Plot(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->Plot(srcdouble, _SourceArr->Length, title, lineColor, Legend);
free(srcdouble);
}
void ScatterPlot(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->ScatterPlot(srcdouble, _SourceArr->Length, , title, lineColor, Legend);
free(srcdouble);
}
void Stem(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->Stem(srcdouble, _SourceArr->Length, title, lineColor, Legend);
free(srcdouble);
}
bool isSame(string MatlabVarName, double Tolerance = 0.0000000001)
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
bool issame = _MatlabEng->isSame(srcdouble, _SourceArr->Length, MatlabVarName, Tolerance);
free(srcdouble);
return issame;
}
void StemDiffIndex(string MatlabVarName, double Tolerance = 0.0000000001, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->StemDiffIndex(srcdouble, _SourceArr->Length, MatlabVarName, Tolerance, title, lineColor, Legend);
free(srcdouble);
}
void HoldOn()
{
_MatlabEng->HoldOn();
}
void HoldOff()
{
_MatlabEng->HoldOff();
}
void Figure()
{
_MatlabEng->Figure();
}
void PlotDiffValue(string MatlabVarName, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->PlotDiffValue(srcdouble, _SourceArr->Length, MatlabVarName, title, lineColor, Legend);
free(srcdouble);
}
void LoadFromVar(string MatlabVarName)
{
int Length = 0;
double* arr=_MatlabEng->GetMatlabArrayVariable(MatlabVarName, Length);
T* srcdouble = (T*)malloc(Length*sizeof(T));
for (int i = 0; i <Length; i++)
{
srcdouble[i] = arr[i];
}
_SourceArr->CopyFrom(srcdouble, Length);
free(srcdouble);
}
void SetIntoVar(string MatlabVarName)
{
double* srcdouble = (double*)malloc(_SourceArr->Length*sizeof(double));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = _SourceArr->Array[i];
}
_MatlabEng->SetVariable(srcdouble, _SourceArr->Length, MatlabVarName);
free(srcdouble);
}
~MATLABClass()
{
delete _MatlabEng;
_MatlabEng = nullptr;
}
private:
TArr<T, Tptr> * _SourceArr =nullptr;
MATLABEngineClass* _MatlabEng;
};
// Forward declaration of TcArr for using in CMATLABClass
template<class Tc, class Ts, class TcAr, class TsAr>
class TcArr;
template<class Tc, class Ts, class TcAr, class TsAr>
class CMATLABClass
{
public:
CMATLABClass(TcArr< Tc, Ts, TcAr, TsAr> *A)
{
_SourceArr = A;
_MatlabEng = new MATLABEngineClass(MATLABSharedEngineName);
}
double Diff(string MatlabVarName)
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
double diff = _MatlabEng->Diff_Complex(srcdouble, _SourceArr->Length, MatlabVarName);
free(srcdouble);
return diff;
}
bool isSame(string MatlabVarName, double Tolerance = 0.0000000001)
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
bool issame = _MatlabEng->isSame_Complex(srcdouble, _SourceArr->Length, MatlabVarName, Tolerance);
free(srcdouble);
return issame;
}
void StemDiffIndex(string MatlabVarName, double Tolerance = 0.0000000001, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->StemDiffIndex_Complex(srcdouble, _SourceArr->Length, MatlabVarName, Tolerance, title, lineColor, Legend);
free(srcdouble);
}
void Plot(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->Plot_Complex(srcdouble, _SourceArr->Length, title, lineColor, Legend);
free(srcdouble);
}
void ScatterPlot(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->ScatterPlot_Complex(srcdouble, _SourceArr->Length, title, lineColor, Legend);
free(srcdouble);
}
void Stem(string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->Stem_Complex(srcdouble, _SourceArr->Length, title, lineColor, Legend);
free(srcdouble);
}
void HoldOn()
{
_MatlabEng->HoldOn();
}
void HoldOff()
{
_MatlabEng->HoldOff();
}
void Figure()
{
_MatlabEng->Figure();
}
void PlotDiffValues(string MatlabVarName, string title = "CppAns", MatlabColor lineColor = MatlabColor::red, string Legend = "CppVar")
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->PlotDiffValue_Complex(srcdouble, _SourceArr->Length, MatlabVarName, title, lineColor, Legend);
free(srcdouble);
}
void SetIntoVar(string MatlabVarName)
{
std::complex<double>* srcdouble = (std::complex<double>*)malloc(_SourceArr->Length*sizeof(std::complex<double>));
for (int i = 0; i < _SourceArr->Length; i++)
{
srcdouble[i] = complex<double>(_SourceArr->Array[i].real, _SourceArr->Array[i].imag);
}
_MatlabEng->SetVariable_Complex(srcdouble, _SourceArr->Length, MatlabVarName);
free(srcdouble);
}
void Release()
{
delete _MatlabEng;
}
~CMATLABClass()
{
delete _MatlabEng;
_MatlabEng = nullptr;
}
private:
TcArr<Tc, Ts, TcAr, TsAr> * _SourceArr=nullptr;
MATLABEngineClass* _MatlabEng;
};
#endif
// implementation of template class should placed in class header in .h file
template<class T,class Tptr>
class TArr
{
protected:
T* Array = nullptr;
public:
int Length = 0;
protected:
int MemSize = 0;
public:
TArr(int ElementNumber)
{
Alloc(ElementNumber);
IsPointer = false;
#if Use_MATLAB ==1
MATLAB = new MATLABClass<T, Tptr>(this);
#endif
}
TArr(const TArr &obj)
{
*this = obj;// copy constructor
// initialized one object from another of the same type
// or copy object to pass it as an argument to a function
// copy an object to return it from a function
}
TArr(void)
{
#if Use_MATLAB ==1
MATLAB = new MATLABClass<T, Tptr>(this);
#endif
}
TArr(MemoryManagerClass *MemoryManager)
{
MemManager = MemoryManager;
IsPointer = false;
#if Use_MATLAB ==1
MATLAB = new MATLABClass<T, Tptr>(this);
#endif
}
TArr(int ElementNumber, MemoryManagerClass *MemoryManager)
{
MemManager = MemoryManager;
Alloc(ElementNumber);
IsPointer = false;
#if Use_MATLAB ==1
MATLAB = new MATLABClass<T, Tptr>(this);
#endif
}
TArr(T* A, int Alength, bool CopyValues, MemoryManagerClass *MemoryManager)
{
MemManager = MemoryManager;
if (CopyValues)
{
CopyFrom(A, Alength);
}
else
{
SetArray(A, Alength);
}
IsPointer = false;
#if Use_MATLAB ==1
MATLAB = new MATLABClass<T, Tptr>(this);
#endif
}
~TArr()
{
MemSize = 0;
Array = nullptr;
Length = 0;
TagArr = nullptr;
TagArrLength = 0;
TagObject = nullptr;
MemManager = nullptr;
IsPointer = false;
#if Use_MATLAB ==1
delete MATLAB ;
MATLAB = nullptr;
#endif
}
MemoryManagerClass *MemManager = nullptr;
char* TagArr = nullptr;
int TagArrLength = 0;
void* TagObject = nullptr;
#if Use_MATLAB ==1
friend class MATLABClass<T, Tptr>;
MATLABClass<T, Tptr>* MATLAB = nullptr;
#endif
T endValue()
{
if (Length > 0)
{
return Array[Length - 1];
}
return 0;
}
int Size()
{
return MemSize;
}
T At(int Index)
{