-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataTypes.cs
More file actions
1728 lines (1525 loc) · 40.3 KB
/
DataTypes.cs
File metadata and controls
1728 lines (1525 loc) · 40.3 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
using System;
using MathParser.DataTypes.DynamicDataTypes;
using MathParser;
namespace MathParser
{
namespace DataTypes
{
public abstract class BaseDataType
{
public abstract void Print();
public string Tag = "";
}
/// <summary>
/// Dynamic Data Types are held here which configure their internal dataTypes on the run time.
/// </summary>
namespace DynamicDataTypes
{
/// <summary>
/// The DataType to hold the numeric types.
/// The data stord in it is dynamic.
/// </summary>
public class Number : BaseDataType
{
dynamic data;
string tag;
public delegate void PrinterFunction(Number number); ///Don't use it outside the class.
public PrinterFunction OnPrint = null; // delegate that gets called when Print function is called.
public static PrinterFunction staticOnPrint = null; //delegate that gets called in Print. It is statically defined and gets called only when
//the OnPrint is not defined i.e null
public Number() { }
public Number(dynamic Data)
{
data = Data;
tag = "";
}
public static Number Parse(string NumberString, string dataType = "double")
{
Number num = null;
if (dataType.ToLowerInvariant() == "double")
num = new Number(double.Parse(NumberString));
else if (dataType.ToLowerInvariant() == "decimal")
num = new Number(decimal.Parse(NumberString));
else if (dataType.ToLowerInvariant() == "int")
num = new Number(int.Parse(NumberString));
else if (dataType.ToLowerInvariant() == "float")
num = new Number(float.Parse(NumberString));
else
{
num = new Number(double.Parse(NumberString));
}
return num;
}
public Number(dynamic Data, string dataTag)
{
data = Data;
tag = dataTag;
}
public dynamic Data
{
get { return data; }
set { data = value; }
}
public new string Tag
{
get { return tag; }
set { tag = value; }
}
public Number(Number number)
{
data = number.Data;
tag = number.Tag;
OnPrint = number.OnPrint;
}
/// <summary>
/// Print this instance.
/// Prints the Number as defined by the OnPrint function delegate;
/// </summary>
public override void Print() // Prints the Number as defined by the OnPrint function delegate;
{
if (OnPrint != null)
{
OnPrint(this);
}
else {
staticOnPrint(this);
}
}
public static Number operator +(Number lhs, Number rhs)
{
return (new Number(lhs.Data + rhs.Data));
}
public static Number operator *(Number lhs, Number rhs)
{
return (new Number(lhs.Data * rhs.Data));
}
public static Number operator -(Number lhs, Number rhs)
{
return (new Number(lhs.Data - rhs.Data));
}
public static Number operator /(Number lhs, Number rhs)
{
return (new Number(lhs.Data / rhs.Data));
}
}
// End Number class.
/// <summary>
/// The DataType to hold the Matrices types.
/// The data stord in it is dynamic.
/// </summary>
public class Matrix : BaseDataType
{
public dynamic[,] data
{
private set;
get;
}
public int Rows
{
private set;
get;
} = 0;
public int Columns
{
private set;
get;
} = 0;
//public string tag;
public new string Tag
{
set;
get;
}
public delegate void PrinterFunction(Matrix matrix); // don't use this outside the class.
public PrinterFunction OnPrint = null; // this Function gets called matrix is printed by calling Print() function.
public static PrinterFunction staticOnPrint = null; //delegate that gets called in Print. It is statically defined and gets called only when
//the OnPrint is not defined i.e null
public Matrix() { }
public Matrix(dynamic[,] Data, int Rows, int Columns, string tag = " ")
{
data = Data;
Tag = tag;
this.Rows = Rows;
this.Columns = Columns;
}
public Matrix(int Rows, int Columns, string tag = " ")
{
Tag = tag;
this.Rows = Rows; this.Columns = Columns;
data = new dynamic[this.Rows, this.Columns];
for (int c = 0; c < this.Rows; c++)
{
for (int c1 = 0; c1 < this.Columns; c1++)
{
data[c, c1] = 0;
}
}
}
public dynamic this[int indexRows, int indexCols]
{
set { data[indexRows, indexCols] = value; }
get { return (data[indexRows, indexCols]); }
}
public Matrix(Matrix matrix) // copy constructor.
{
data = new dynamic[matrix.Rows, matrix.Columns];
for (int c = 0; c < matrix.Rows; c++)
{
for (int c1 = 0; c1 < matrix.Columns; c1++)
{
data[c, c1] = matrix[c, c1];
}
}
Rows = matrix.Rows;
Columns = matrix.Columns;
Tag = matrix.Tag;
OnPrint = matrix.OnPrint;
}
public static Matrix Parse(string matrixString)
{
MathParser.MatrixBuilder build = new MatrixBuilder(matrixString);
build.SyncFlags();
build.Parse();
Matrix result = build.getMatrix();
if (!build.isProcessed())
{
throw (new MathParserException("Matrix string could not be processed."));
}
return result;
}
/// <summary>
/// Print this instance.
/// Function prints the matrix data as defined by the delegate function of ' OnPrint '.
/// </summary>
public override void Print() // function prints the matrix data as defined by the delegate function of ' OnPrint '.
{
if (OnPrint != null)
{
OnPrint(this);
}
else
{
staticOnPrint(this);
}
}
public static Matrix operator +(Matrix lhs, Matrix rhs)
{
Matrix ans;
if ((lhs.Rows != rhs.Rows) || (lhs.Columns != rhs.Columns))
{
throw new MathParserException("Not similar matrix.");
}
else {
ans = new Matrix(lhs.Rows, rhs.Columns);
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < rhs.Columns; c1++)
{
ans[c, c1] = lhs[c, c1] + rhs[c, c1];
}
}
}
return ans;
}
public static Matrix operator -(Matrix lhs, Matrix rhs)
{
Matrix ans;
if ((lhs.Rows != rhs.Rows) || (lhs.Columns != rhs.Columns))
{
throw new MathParserException("Not similar matrix.");
}
else {
ans = new Matrix(lhs.Rows, rhs.Columns);
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < rhs.Columns; c1++)
{
ans[c, c1] = lhs[c, c1] - rhs[c, c1];
}
}
}
return ans;
}
public static Matrix operator *(Matrix lhs, Matrix rhs)
{
Matrix result = new Matrix(lhs.Rows, rhs.Columns);
if (lhs.Columns == rhs.Rows)
{
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < rhs.Columns; c1++)
{
for (int c2 = 0; c2 < lhs.Columns; c2++)
{
if (c2 == 0)
result[c, c1] = lhs[c, c2] * rhs[c, c2];
else
result[c, c1] = (result[c, c1] + (lhs[c, c2] * rhs[c2, c1]));
}
}
}
}
else {
throw new MathParserException("No valid matrix sequences.");
}
return result;
}
static public Matrix operator /(Matrix lhs, double num)
{
Matrix ans = new Matrix(lhs.Rows, lhs.Columns);
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < lhs.Columns; c1++)
{
ans[c, c1] = lhs[c,c1]/num;
}
}
return ans;
}
static public Matrix operator *(Matrix lhs, double num)
{
Matrix ans = new Matrix(lhs.Rows, lhs.Columns);
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < lhs.Columns; c1++)
{
ans[c, c1] = lhs[c,c1]*num;
}
}
return ans;
}
static public Matrix operator *(double num, Matrix lhs)
{
Matrix ans = new Matrix(lhs.Rows, lhs.Columns);
for (int c = 0; c < lhs.Rows; c++)
{
for (int c1 = 0; c1 < lhs.Columns; c1++)
{
ans[c, c1] = lhs[c,c1]*num;
}
}
return ans;
}
public Matrix RowEchelonForm()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
mat.GaussElimination ();
return (mathParserMatrixMaker (mat));
}
public Matrix ReducedRowEchelonForm()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
mat.GaussJordan ();
return (mathParserMatrixMaker (mat));
}
public Number Determinant()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
double d = mat.getdetreminant (mat);
DataTypes.DynamicDataTypes.Number det = new Number (d);
return det;
}
public Matrix Adjoint()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
return(mathParserMatrixMaker(mat.getAdjoint ()));
}
public Matrix Transpose()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
return (mathParserMatrixMaker (mat.getTranspose ()));
}
public Number Rank()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
double r = mat.rank (mat);
DataTypes.DynamicDataTypes.Number rank = new Number (r);
return rank;
}
public Matrix Inverse()
{
DataTypeSpace.Matrix mat = dataTypeSpaceMatrixMaker (this);
Matrix inv = mathParserMatrixMaker (mat.Inverse1 (mat));
return inv;
}
static public DataTypeSpace.Matrix dataTypeSpaceMatrixMaker(Matrix theMat)
{
DataTypeSpace.Matrix mat = new DataTypeSpace.Matrix (theMat.Rows, theMat.Columns);
for (int c = 0; c < theMat.Rows; c++) {
for (int c1 = 0; c1 < theMat.Columns; c1++) {
mat.setElement ((double)theMat[c,c1],c+1,c1+1);
}
}
mat.setTag (theMat.Tag);
return mat;
}
static public Matrix mathParserMatrixMaker(DataTypeSpace.Matrix theMat)
{
DataTypes.DynamicDataTypes.Matrix mat = new DataTypes.DynamicDataTypes.Matrix (theMat.getRows (), theMat.getColumns (), theMat.getTag ());
for (int c = 0; c < theMat.getRows (); c++) {
for (int c1 = 0; c1 < theMat.getColumns (); c1++) {
mat [c, c1] = theMat.getElement (c + 1, c1 + 1);
}
}
mat.Tag = "";
return mat;
}
}
// End Matrix Class.
/// <summary>
/// The DataType to hold the data of the complex numbers.
/// </summary>
public class ComplexNumber : BaseDataType
{
public dynamic RealPart
{
private set;
get;
}
public dynamic ImaginaryPart
{
private set;
get;
}
public new string Tag
{
set;
get;
}
public delegate void PrinterFunction(ComplexNumber complexNumber); // dont use it outside the class.
public PrinterFunction OnPrint = null; // this Function gets called matrix is printed by calling Print() function.
public static PrinterFunction staticOnPrint = null; //delegate that gets called in Print. It is statically defined and gets called only when
//the OnPrint is not defined i.e null
public ComplexNumber() { }
public ComplexNumber(dynamic realData, dynamic imaginaryData, string tag = " ")
{
RealPart = realData;
ImaginaryPart = imaginaryData;
Tag = tag;
}
public ComplexNumber(ComplexNumber complexNumber)
{
RealPart = complexNumber.RealPart;
ImaginaryPart = complexNumber.ImaginaryPart;
Tag = complexNumber.Tag;
OnPrint = complexNumber.OnPrint;
}
/// <summary>
/// Print this instance.
/// Calls OnPrint function delegate. Or staticOnPrint function delegate.
/// </summary>
public override void Print()
{
if (OnPrint != null)
{
OnPrint(this);
}
else {
staticOnPrint(this);
}
}
}
// end complex number datatype.
/// <summary>
/// Vector. To hold vector data.
/// </summary>
public class Vector : BaseDataType
{
public dynamic i
{
private set;
get;
} = 0;
public dynamic j
{
private set;
get;
} = 0;
public dynamic k
{
private set;
get;
} = 0;
public new string Tag
{
set;
get;
}
public delegate void PrinterFunction(Vector vector); // dont use it outside the class.
public PrinterFunction OnPrint = null; // this Function gets called matrix is printed by calling Print() function.
public static PrinterFunction staticOnPrint = null; //delegate that gets called in Print. It is statically defined and gets called only when
public Vector() { }
public Vector(dynamic i = null, dynamic j = null, dynamic k = null, string tag = "")
{
this.i = i;
this.j = j;
this.k = k;
this.Tag = tag;
}
public Vector(Vector vector)
{
i = vector.i;
j = vector.j;
k = vector.k;
Tag = vector.Tag;
}
public override void Print()
{
if (OnPrint != null)
{
OnPrint(this);
}
else {
staticOnPrint(this);
}
}
} //end vector datatype.
} // end dynamic datatypes.
public class MathParserExpression : BaseDataType
{
dynamic data;
public override void Print() {
data.Print ();
}
public dynamic Data
{
get { return data; }
private set { data = value; }
}
//string tag;
public new string Tag {
get;
set;
}
public void setEntireTag(string name)
{
data.Tag = name;
}
public string Statement {
get;
set;
}
public string Type
{
get;
private set;
}
public MathParserExpression() { }
public MathParserExpression(BaseDataType Data)
{
this.Data = Data;
this.Tag = this.data.Tag;
this.Type = this.data.GetType().Name;
}
public MathParserExpression(MathParserExpression expression)
{
this.Data = expression.Data;
this.Tag = expression.Tag;
this.Type = expression.Type;
this.Statement = expression.Statement;
}
}
} //end data type Space
} // end Math Parser.
// Start namespace DataTypesSpace
namespace DataTypeSpace
{
public class Matrix //matrix class starts
{
protected double[,] theMatrix; //the basic matrix;
protected int mRows, mCols;
private OperationsClass theOperations = new OperationsClass();
private static OperationsClass staticOperations = new OperationsClass();
private string tag;
// code for magic matrix
private Matrix Matrixbuilder(Matrix m1, Matrix m2, Matrix m3, Matrix m4)
{
int rows = m1.getRows();
int columns = m1.getColumns();
Matrix temp = new Matrix(2 * rows, 2 * columns);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
temp.setElement(m1.getElement(i + 1, j + 1), i + 1, j + 1);
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
temp.setElement(m2.getElement(i + 1, j + 1), i + rows + 1, j + 1);
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
temp.setElement(m3.getElement(i + 1, j + 1), i + 1, j + columns + 1);
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
temp.setElement(m4.getElement(i + 1, j + 1), i + rows + 1, j + columns + 1);
}
}
return temp;
}
public Matrix magic(int size)
{
Matrix temp = new Matrix(size, size);
if (size % 2 == 1)
{
if (size == 1)
{
temp.setElement(1, 1, 1);
return temp;
}
bool donefor = false;
int r = 0;
int c = (size - 1) / 2;
temp.setElement(1, r + 1, c + 1);
for (int i = 2; i <= size * size; i++)
{
while (donefor == false)
{
r--;
c++;
if (r < 0) { r += size; }
else if (r >= size) r -= size;
if (c >= size) c -= size;
else if (c < 0) c += size;
if (temp.getElement(r + 1, c + 1) != 0)
{
r += 2;
c--;
}
if (r < 0) { r += size; }
else if (r >= size) r -= size;
if (c >= size) c -= size;
else if (c < 0) c += size;
if (temp.getElement(r + 1, c + 1) == 0) donefor = true;
}
temp.setElement(i, r + 1, c + 1);
donefor = false;
}
}
else if (size % 4 == 0)// for matrices having number of rows divisible by 4
{
int k = 1;
Matrix x1 = new Matrix(size, size);
Matrix x2 = new Matrix(size, size);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++, k++)
{
x1.setElement(k, i + 1, j + 1);
}
}
k = size * size;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
x2.setElement(k, i + 1, j + 1);
k--;
}
}
//Console.WriteLine("");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == j)
temp.setElement(1, i + 1, j + 1);
else if (i == size - j - 1) temp.setElement(1, i + 1, j + 1);
else temp.setElement(0, i + 1, j + 1);
}
}
//Console.WriteLine("");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (temp.getElement(i + 1, j + 1) == 1) temp.setElement(x1.getElement(i + 1, j + 1), i + 1, j + 1);
else temp.setElement(x2.getElement(i + 1, j + 1), i + 1, j + 1);
}
}
}
else // for magic matrices for number of even rows not divisible by 4
{
if (size == 2)
{
//Console.WriteLine("Magic Matrix does not exist");
return temp;
}
int r = size / 2;
Matrix m1 = new Matrix(r, r);
Matrix m2 = new Matrix(r, r);
Matrix m3 = new Matrix(r, r);
Matrix m4 = new Matrix(r, r);
m1 = m1.magic(r);
Matrix i1 = new Matrix(r, r);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
i1.setElement(r * r, i + 1, j + 1);
}
}
m2 = m1 + i1;
m3 = m1 + i1 + i1;
m4 = m1 + i1 + i1 + i1;
temp = Matrixbuilder(m1, m4, m3, m2);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < (r + 1) / 2; j++)
{
if (i == (r - 1) / 2 && j == 0)
{
continue;
}
else if (i != (r - 1) / 2 && j == (r - 1) / 2)
{
continue;
}
else
{
double tempnum = temp.getElement(i + 1, j + 1);
temp.setElement(temp.getElement(i + r + 1, j + 1), i + 1, j + 1);
temp.setElement(tempnum, i + r + 1, j + 1);
}
}
for (int j = 2 * r - (r - 3) / 2; j < 2 * r; j++)
{
double tempnum = temp.getElement(i + 1, j + 1);
temp.setElement(temp.getElement(i + r + 1, j + 1), i + 1, j + 1);
temp.setElement(tempnum, i + r + 1, j + 1);
}
}
}
return temp;
}
private void numberswap(double x1, double x2)
{
double temp = x1;
x1 = x2;
x2 = temp;
}
//code for magice matrix
// code to find the rank of the matrix
public int rank(Matrix m)
{
int r = 0;
int numofrows = m.getRows();
int numofcols = m.getColumns();
Matrix temp = m;
bool rowvalue = false;
temp.GaussJordan();
for (int i=0;i<numofrows;i++)
{
for (int j=0;j< numofcols;j++)
{
if (temp.getElement(i + 1, j + 1) == 0) continue;
rowvalue = true;
r++;
break;
}
if (rowvalue == false) return r;
}
return r;
}
public Matrix(Matrix theOther)
{
mRows = theOther.mRows;
mCols = theOther.mCols;
theMatrix = new double[mRows,mCols];
for (int c = 0; c < mRows; c++) {
for (int c1 = 0; c1 < mCols; c1++) {
theMatrix [c, c1] = theOther.getElement (c+1, c1+1);
}
}
tag = theOther.tag;
}
public Matrix(string thetag, double[,] matrix,int row = 1, int column = 1 )
{
mRows = row;mCols = column;
theMatrix = matrix;
tag = thetag;
}
public Matrix(int rows = 01, int columns = 01)
{
mRows = rows; mCols = columns;
theMatrix = new double[mRows, mCols];
tag = " ";
}
public Matrix(double[,] mat, int rows , int cols)
{
mRows = rows; mCols = cols;
theMatrix = mat;
tag = " ";
}
public void Zero()
{
for (int c = 0; c < mRows; c++) {
for (int c1 = 0; c1 < mCols; c1++) {
theMatrix [c, c1] = 0;
}
}
}
public double getdetreminant(Matrix theMatrix) //getdeterminant interface
{
double[,] matrix = theMatrix.getMatrixArray();
int mRows = theMatrix.getRows();
int mCols = theMatrix.getColumns();
if (mRows != mCols) {
throw new MathParserException ("Not a square matrix.");
}
return determinant1(matrix, mRows, mCols);
}
private Matrix rowaddition(Matrix m, int row1, int row2, double factor)
{
int col = m.getColumns();
for (int i = 0; i < col; i++)
{
m.setElement(m.getElement(row2 + 1, i + 1) + factor * m.getElement(row1 + 1, i + 1), row2 + 1, i + 1);
}
return m;
}
private Matrix rowdivision(double factor, Matrix m, int rownum, int col)
{
for (int i = 0; i < col; i++)
{
double tempvalue = m.getElement(rownum + 1, i + 1);
tempvalue /= factor;
m.setElement(tempvalue, rownum + 1, i + 1);
}
return m;
}
private void rowswap(Matrix m, int row1, int row2)
{
int c = m.getColumns();
for (int i = 0; i < c; i++)
{
double num1, num2;
num1 = m.getElement(row1 + 1, i + 1);
num2 = m.getElement(row2 + 1, i + 1);
m.setElement(num1, row2 + 1, i + 1);
m.setElement(num2, row1 + 1, i + 1);
}
}
private double determinant1(double[,] matrix, int rows, int cols) //determinant implementation
{
if (rows == cols)
{
if (rows == 1)
{
return matrix[0, 0];
}
else if (rows == 2)
{
return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
}
else
{
double multiplier = 1;
bool rowvalue = false;
Matrix temp = new Matrix(rows, cols);
for (int j = 0; j < cols; j++)
{
for (int i = 0; i < rows; i++)
{
temp.setElement(matrix[i, j], i + 1, j + 1);
}
}
for (int j = 0; j < cols; j++)
{
for (int i = 0; i < rows; i++)
{
if (temp.getElement(i + 1, j + 1) == 0) continue;
rowvalue = true;
double first = temp.getElement(i + 1, j + 1);
multiplier *= first;
temp = rowdivision(first, temp, i, rows);
//Console.WriteLine("");
for (int k = i + 1; k < rows; k++)
temp = rowaddition(temp, i, k, -1 * temp.getElement(k + 1, j + 1));
if (i != 0)
{
rowswap(temp, i, 0);
multiplier *= -1;
}
if (rows == 3)
{
return multiplier * (temp.getElement(2, 2) * temp.getElement(3, 3) - temp.getElement(2, 3) * temp.getElement(3, 2));
}
else
{
double[,] newarray = new double[rows - 1, rows - 1];
for (int z = 0; z < rows - 1; z++)
{
for (int s = 0; s < rows - 1; s++)
{
newarray[z, s] = temp.getElement(z + 2, s + 2);
}
}
return multiplier * determinant1(newarray, rows - 1, rows - 1);
}
}
if (rowvalue == false) return 0;
}
}
}
else
{
//Console.WriteLine("The determinant of the matrix doesnot exist.Sorry.");
return 0;
}
return 0;
}
// vector solving code
public double dotproduct(Matrix m1, Matrix m2)
{
if (m1.getColumns() != m2.getColumns() || m1.getRows() != 1 || m2.getRows() != 1)
{