-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICSMinor.c
More file actions
1127 lines (957 loc) · 26.4 KB
/
ICSMinor.c
File metadata and controls
1127 lines (957 loc) · 26.4 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
/*
ICS Minor Project
Matrix Algebra Utility Program
1. Matrix Operations
a. Addition/Subtraction
b. Multiplication
c. Determinant
d. Adjoint
e. Transpose
f. Inverse
g. Determine whether matrix is
Symmetric/Skew Sym
Row/Column
Diagonol
Upper/Lower Triangular
Singular/Non-Singular
Idempotent
Nilpotent
Orthogonal
Involutory
2. Single Variable Polynomial Operations
a. Addition
b. Multiplication
c. Estimate Real Roots (based on Newton's Method)
d. Derivative
e. Integration
3. Solve Linear Equations (Row-Echelon or Gaussian Elimination + Back Substitution Method)
*/
#include <stdio.h>
#include <math.h>
// Matrix Start
// Function to input matrix
void inpMat(int rows, int cols, double mat[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%lf", &mat[i][j]);
}
// Function to print matrix on terminal
void outMat(int rows, int cols, double mat[rows][cols])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%lf ", mat[i][j]);
printf("\n");
}
}
// Matrix Operation Calculations Functions Start
// Make Matrix 2 = Matrix 1
void equateMat(int rows, int cols, double mat1[rows][cols], double mat2[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
mat2[i][j] = mat1[i][j];
}
// Add two matrices
void addMat(int rows, int cols, double mat1[rows][cols], double mat2[rows][cols], double add[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
add[i][j] = mat1[i][j] + mat2[i][j];
}
// Multiply two matrices
void mulMat(int rows1, int cols1, double mat1[rows1][cols1], int rows2, int cols2, double mat2[rows2][cols2], double mul[rows1][cols2])
{
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
mul[i][j] = 0;
for (int k = 0; k < cols1; k++)
mul[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
// Calculate Determininant of a Square Matrix
double determinant(int n, double mat[n][n])
{
double det = 0;
if (n == 1)
return mat[0][0];
else if (n == 2)
return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
else
{
for (int k = 0; k < n; k++)
{
double submatrix[n - 1][n - 1];
for (int i = 1; i < n; i++)
for (int j = 0; j < n; j++)
{
if (j < k)
submatrix[i - 1][j] = mat[i][j];
else if (j > k)
submatrix[i - 1][j - 1] = mat[i][j];
}
det += (k % 2 == 0 ? 1 : -1) * mat[0][k] * determinant(n - 1, submatrix);
}
}
return det;
}
// Find transpose of a matrix
void transpose(int rows, int cols, double mat[rows][cols], double trans[cols][rows])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
trans[j][i] = mat[i][j];
}
// Find Adjoint of a matrix
void adjoint(int n, double mat[n][n], double adj[n][n])
{
double temp[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
double submatrix[n - 1][n - 1];
double O = (i + j) % 2 == 0 ? 1 : -1;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
if (row != i && col != j)
{
int newRow = row < i ? row : row - 1;
int newCol = col < j ? col : col - 1;
submatrix[newRow][newCol] = mat[row][col];
}
}
}
temp[i][j] = O * determinant(n - 1, submatrix);
}
}
transpose(n, n, temp, adj);
}
// Invert a matrix
void inverse(int n, double mat[n][n], double inv[n][n])
{ // Inverse(A) = Adjoint(A)/determinant(A)
double adj[n][n];
adjoint(n, mat, adj);
double det = determinant(n, mat);
double inv_det = 1.0 / det;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
inv[i][j] = adj[i][j] * inv_det;
}
// Matrix Calculations End
// Matrix Properties Start
// Find if a matrix is Symmetric, i.e. A = A^
int isSym(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
double trans[cols][rows];
transpose(rows, cols, mat, trans);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (trans[i][j] != mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Skew-Symmetric, i.e. A+A^ = O
int isSkewSym(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
double trans[cols][rows];
transpose(rows, cols, mat, trans);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (trans[i][j] != -mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Diagonal, i.e. Aij = 0 for j!=i
int isDiag(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (i != j && mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Upper Triangular, i.e. Aij = 0 for j<i
int isUpTr(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (j < i && mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Lower Triangular, i.e. Aij = 0 for j>i
int isLoTr(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (i < j && mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Idempotent, i.e. A^2 = A
int isIdem(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
double sq[rows][cols];
mulMat(rows, cols, mat, rows, cols, mat, sq);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (sq[i][j] != mat[i][j])
return 0;
return 1;
}
// Find if a matrix is Zero, i.e. A = O
int isZero(int rows, int cols, double mat[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (mat[i][j] != 0)
return 0;
return 1;
}
// Find if a matrix is Nilpotent, i.e. A^k = O for k<n
int isNil(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
int k = 1;
double temp[rows][cols], temp2[rows][cols];
mulMat(rows, cols, mat, rows, cols, mat, temp);
while (isZero(rows, cols, temp) == 0 && k <= rows)
{
mulMat(rows, cols, mat, rows, cols, temp, temp2);
equateMat(rows, cols, temp, temp2);
k++;
}
if (k >= rows)
return 0;
return 1;
}
// Find if a matrix is Orthogonal, i.e. A*A^ = I
int isOrt(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
double trans[cols][rows];
transpose(rows, cols, mat, trans);
double mul[rows][cols];
mulMat(rows, cols, mat, cols, rows, trans, mul);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (i == j && mul[i][j] != 1)
return 0;
if (i != j && mul[i][j])
return 0;
}
return 1;
}
// Find if a matrix is Involutory, i.e. A^2 = I
int isInv(int rows, int cols, double mat[rows][cols])
{
if (rows != cols)
return 0;
double sq[rows][cols];
mulMat(rows, cols, mat, rows, cols, mat, sq);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (i == j && sq[i][j] != 1)
return 0;
if (i != j && sq[i][j])
return 0;
}
return 1;
}
// Matrix Properties End
// Matrix Operation Utility Functions Start
// Operation to call addition
void OPaddMat()
{
system("cls");
printf("Enter number of Rows and Columns of Matrices:\n");
int rows, cols;
scanf("%d %d", &rows, &cols);
double mat1[rows][cols], mat2[rows][cols], add[rows][cols];
printf("Enter Matrix 1\n");
inpMat(rows, cols, mat1);
printf("Enter Matrix 2\n");
inpMat(rows, cols, mat2);
addMat(rows, cols, mat1, mat2, add);
printf("Added Matrix\n");
outMat(rows, cols, add);
}
// Operation to call subtraction
void OPsubMat()
{
system("cls");
printf("Enter number of Rows and Columns of Matrices:\n");
int rows, cols;
scanf("%d %d", &rows, &cols);
double mat1[rows][cols], mat2[rows][cols], add[rows][cols];
printf("Enter Matrix 1\n");
inpMat(rows, cols, mat1);
printf("Enter Matrix 2\n");
inpMat(rows, cols, mat2);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
mat2[i][j] *= -1;
addMat(rows, cols, mat1, mat2, add);
printf("Subtracted Matrix\n");
outMat(rows, cols, add);
}
// Operation to call multiplication
void OPmulMat()
{
system("cls");
printf("Enter number of Rows and Columns of Matrix 1:\n");
int rows1, cols1;
scanf("%d %d", &rows1, &cols1);
printf("Enter number of Rows and Columns of Matrix 2:\n");
int rows2, cols2;
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2)
{
printf("Cannot multiply matrices: invalid dimensions\n");
return;
}
double mat1[rows1][cols1], mat2[rows2][cols2], mul[rows1][cols2];
printf("Enter Matrix 1\n");
inpMat(rows1, cols1, mat1);
printf("Enter Matrix 2\n");
inpMat(rows2, cols2, mat2);
mulMat(rows1, cols1, mat1, rows2, cols2, mat2, mul);
printf("Multiplied Matrix\n");
outMat(rows1, cols2, mul);
}
// Operation to call determinant
void OPdetMat()
{
system("cls");
printf("Enter number of Rows and Columns of the Square Matrix:\n");
int n;
scanf("%d", &n);
double mat[n][n];
printf("Enter the Square Matrix\n");
inpMat(n, n, mat);
double det = determinant(n, mat);
printf("Determinant of the Matrix: %lf\n", det);
}
// Operation to call adjoint
void OPadjMat()
{
system("cls");
printf("Enter number of Rows and Columns of the Square Matrix:\n");
int n;
scanf("%d", &n);
double mat[n][n];
printf("Enter the Square Matrix\n");
inpMat(n, n, mat);
double adj[n][n];
adjoint(n, mat, adj);
printf("Adjoint of the Matrix\n");
outMat(n, n, adj);
}
// Operation to call transpose
void OPtransMat()
{
system("cls");
printf("Enter number of Rows and Columns of the Matrix:\n");
int rows, cols;
scanf("%d %d", &rows, &cols);
double mat[rows][cols];
printf("Enter the Matrix\n");
inpMat(rows, cols, mat);
double trans[cols][rows];
transpose(rows, cols, mat, trans);
printf("Transpose of the Matrix\n");
outMat(cols, rows, trans);
}
// Operation to call inversion
void OPinvMat()
{
system("cls");
printf("Enter the size of the Square Matrix:\n");
int n;
scanf("%d", &n);
double mat[n][n];
printf("Enter the Square Matrix\n");
inpMat(n, n, mat);
double inv[n][n];
inverse(n, mat, inv);
printf("Inverse of the Matrix\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf("%lf ", inv[i][j]);
printf("\n");
}
}
// Operation to find properties
void OPpropertiesMat()
{
system("cls");
printf("Enter number of Rows and Columns of the Matrix:\n");
int rows, cols;
scanf("%d %d", &rows, &cols);
double mat[rows][cols];
printf("Enter the Matrix\n");
inpMat(rows, cols, mat);
if (isSym(rows, cols, mat))
printf("Matrix is Symmetric\n"); // A=A^
if (isSkewSym(rows, cols, mat))
printf("Matrix is Skew-Symmetric\n"); // A=-A^
if (isDiag(rows, cols, mat))
printf("Matrix is Diagnol\n"); // Aij=0; i!=j;
if (isUpTr(rows, cols, mat))
printf("Matrix is Upper Triangular\n"); // Aij=0; j<i;
if (isLoTr(rows, cols, mat))
printf("Matrix is Lower Triangular\n"); // Aij=0; i<j
if (rows == cols && determinant(rows, mat) == 0)
printf("Matrix is Singular\n"); // detA=0
else if (rows == cols && determinant(rows, mat))
printf("Matrix is Non-Singular\n"); // detA!=0
if (isIdem(rows, cols, mat))
printf("Matrix is Idempotent\n"); // A^2=A
if (isNil(rows, cols, mat))
printf("Matrix is Nilpotent\n"); // A^k=0, k<n
if (isOrt(rows, cols, mat))
printf("Matrix is Orthogonal\n"); // A*A^=I
if (isInv(rows, cols, mat))
printf("Matrix is Involutory\n"); // A^2 = I
}
// Matrix OP end
// Matrix end
// Polynomial Start
// Function to input Polynomial
void inpPol(int d, double pol[d + 1])
{
for (int i = d; i >= 0; i--)
scanf("%lf", &pol[i]);
}
// Function to print Polynomial on terminal
void outPol(int d, double pol[d + 1])
{
for (int j = 0; j < d + 1; j++)
{
if (j < d)
printf("%lfx^%d + ", pol[j], j);
else
printf("%lfx^%d\n", pol[j], d);
}
}
// Polynomial Operation Calculations Start
// Function to make an array zero
void zeroArr(int rows, double mat[rows])
{
for (int i = 0; i < rows; i++)
mat[i] = 0;
}
// Function to add two polynomials
void addPol(int d1, double pol1[d1 + 1], int d2, double pol2[d2 + 1], int d_res, double res_pol[d_res + 1])
{
for (int i = 0; i < d_res; i++)
{
if (d_res > d2)
{
for (int i = 0; i < d2 + 1; i++)
{
res_pol[i] = pol1[i] + pol2[i];
}
for (int i = d2 + 1; i < d_res + 1; i++)
{
res_pol[i] = pol1[i];
}
}
else
{
for (int i = 0; i < d1 + 1; i++)
{
res_pol[i] = pol1[i] + pol2[i];
}
for (int i = d1 + 1; i < d_res + 1; i++)
{
res_pol[i] = pol2[i];
}
}
}
}
// Function to divide polynomial by linear function
void divPol(int d, double poly[d + 1], int r, double q[d])
{
q[0] = poly[0];
for (int i = 1; i <= d; i++)
{
q[i] = (q[i - 1] * r) + poly[i];
}
}
// Function to multiply two polynomials
void mulPol(int deg1, int deg2, double coef1[deg1 + 1], double coef2[deg2 + 2], double mul[deg1 + deg2 + 1])
{
zeroArr(deg1 + deg2 + 1, mul);
for (int i = 0; i < deg1 + 1; i++)
{
for (int j = 0; j < deg2 + 1; j++)
{
mul[i + j] += coef1[i] * coef2[j];
}
}
}
// Function to differentiate a polynomial
void derPol(int d, double pol[d + 1], double der[d])
{
for (int i = 0; i < d; i++)
{
der[i] = (i + 1) * pol[i + 1];
}
}
// Function to indefintely integrate a polynomial
void intPol(int d, double pol[d + 1], double inte[d + 2])
{
for (int i = 0; i < d + 1; i++) // Fix: start from 0
{
inte[i + 1] = pol[i] / (i + 1); // Fix: divide by i instead of i-1
}
}
// Function to reverse a matrix
void reverse(int n, double arr[n])
{
int left = 0;
int right = n - 1;
while (left < right)
{
double temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
// Second Function to Print a polynomial
void print_polynomial(int degree, double coefficients[])
{
printf("Polynomial: ");
for (int i = degree; i >= 0; i--)
{
if (coefficients[i] != 0)
{
if (i == 0)
{
printf("%lfx^%d ", coefficients[i], i);
}
else
{
printf("%lfx^%d + ", coefficients[i], i);
}
}
}
printf("\n");
}
// Function to evaluate the value of the polynomial at a given point x
double evaluate_polynomial(int degree, double coefficients[], double x)
{
double result = 0.0;
for (int i = degree; i >= 0; i--)
{
result += coefficients[i] * pow(x, i);
}
return result;
}
// Function to evaluate the derivative of the polynomial at a given point x
double evaluate_derivative(int degree, double coefficients[], double x)
{
double result = 0.0;
for (int i = degree; i > 0; i--)
{
result += i * coefficients[i] * pow(x, i - 1);
}
return result;
}
// Function to perform synthetic division of a polynomial by a linear polynomial (x - r)
void synthetic_division(int degree, double coefficients[], double r, double q[])
{
double temp[100 + 1]; // Temporary array to hold intermediate results
for (int i = 0; i <= degree; i++)
{
temp[i] = coefficients[i];
}
q[0] = temp[degree];
for (int i = degree - 1; i >= 0; i--)
{
q[i] = temp[i + 1] + q[i + 1] * r;
}
// print_polynomial(degree - 1, q);
}
// Function to estimate the root of a polynomial using Newton's method
double estimate_root(int degree, double coefficients[], double epsilon)
{
// Automatically taking initial guess as 0
int it = 0;
double x = 3.141592653598979;
double f_x, f_prime_x;
do
{
f_x = evaluate_polynomial(degree, coefficients, x);
f_prime_x = evaluate_derivative(degree, coefficients, x);
x = x - f_x / f_prime_x; // Newton's Theorem
it++;
} while (fabs(f_x) > epsilon && it < 5000 && fabs(f_prime_x) > epsilon);
if (fabs(f_x) < epsilon)
return x;
else
return 3.141592653598979;
}
// Operation to call for solving
void OPsolvePol()
{
system("cls");
int degree, D, it = 0;
printf("Enter the degree of the polynomial: ");
scanf("%d", °ree);
D = degree;
double coefficients[degree + 1];
for (int i = degree; i >= 0; i--)
{
printf("Coefficient of x^%d\n = ", i);
scanf("%lf", &coefficients[i]);
}
print_polynomial(degree, coefficients);
double roots[degree]; // Array to store roots
int num_roots = 0;
while (num_roots < D)
{
double root = estimate_root(degree, coefficients, 0.000000000001);
if (root != 3.141592653598979)
roots[num_roots++] = root;
else if (root == 3.141592653598979)
{
// printf("No More Real Root Found\n");
break;
}
// Divide the polynomial by (x - root)
double q[degree];
synthetic_division(degree, coefficients, root, q);
for (int i = 0; i < degree; i++)
{
coefficients[i] = q[i];
}
// print_polynomial(degree, coefficients);
degree--;
}
if (num_roots == 0)
{
printf("No real roots of the polynomial");
}
else
{
printf("Roots of the polynomial are: ");
for (int i = 0; i < num_roots; i++)
{
printf("%lf ", roots[i]);
}
printf("\n");
}
}
// Poly Op. Calc End
// Polynomial Operation Utility Functions Start
// Function to call integration
void OPintPol()
{
system("cls");
printf("Enter degree of Polynomial\n");
int d;
scanf("%d", &d);
printf("Enter the Polynomial\n");
double pol[d + 1];
inpPol(d, pol);
double inte[d + 2];
intPol(d, pol, inte);
printf("C");
for (int i = 1; i < d + 2; i++)
{
printf("+%lfx^%d", inte[i], i);
}
}
// Function to call Differentiation
void OPderPol()
{
system("cls");
printf("Enter degree of Polynomial\n");
int d;
scanf("%d", &d);
printf("Enter the Polynomial\n");
double pol[d + 1];
inpPol(d, pol);
double der[d];
derPol(d, pol, der);
outPol(d - 1, der);
}
// Function to call division
void OPdivPol()
{
system("cls");
printf("Enter degree of divident Polyonmial\n");
int d;
scanf("%d", &d);
printf("Enter Divident Polynomiial\n");
double poly[d + 1];
for (int i = 0; i <= d; i++)
{
printf("Coefficient of x^%d\n = ", d - i);
scanf("%lf", &poly[i]);
}
int r;
printf("Enter value of constt 'r' in divisor (x-r)\n");
scanf("%d", &r);
double q[d], rem;
divPol(d, poly, r, q);
rem = q[d - 1] * r + poly[d];
outPol(d - 1, q);
}
// Function to call Polynomial Multiplication
void OPmulPol()
{
system("cls");
printf("Enter degree of Polynomial 1\n");
int d1;
scanf("%d", &d1);
printf("Enter Polynomial 1\n");
double pol1[d1 + 1];
inpPol(d1, pol1);
printf("Enter degree of Polynomial 2\n");
int d2;
scanf("%d", &d2);
printf("Enter Polynomial 2\n");
double pol2[d2 + 1];
inpPol(d2, pol2);
double mul[d1 + d2 + 1];
mulPol(d1, d2, pol1, pol2, mul);
outPol(d1 + d2, mul);
}
// Function to call Polynomial Addition
void OPaddPol()
{
system("cls");
printf("Enter degree of Polynomial 1\n");
int d1;
scanf(" %d", &d1);
printf("Enter Polynomial 1\n");
double pol1[d1 + 1];
inpPol(d1, pol1);
outPol(d1, pol1);
printf("Enter degree of Polynomial 2\n");
int d2;
scanf("%d", &d2);
printf("Enter Polynomial 2\n");
double pol2[d2 + 1];
inpPol(d2, pol2);
outPol(d2, pol2);
int d_res;
if (d1 > d2)
d_res = d1;
else
d_res = d2;
double res_pol[d_res + 1];
addPol(d1, pol1, d2, pol2, d_res, res_pol);
outPol(d_res, res_pol);
return;
}
// Polynomial OP End
// Polynomial End
// Linear Equations Start
void back_subs(double eq[][101], double x[], int n)
{
int i, j;
for (i = n - 1; i >= 0; i--)
{
x[i] = eq[i][n];
for (j = i + 1; j < n; j++)
x[i] -= eq[i][j] * x[j];
x[i] /= eq[i][i];
}
}
void gauss_elim(double eq[][101], int n, int *solvable)
{
int i, j, k;
double factor;
*solvable = 1;
for (i = 0; i < n; i++)
{
if (eq[i][i] == 0)
{
*solvable = 0;
return;
}
for (j = i + 1; j < n; j++)
{
factor = eq[j][i] / eq[i][i];
for (k = i; k <= n; k++)
eq[j][k] -= factor * eq[i][k];
}
}
}
void OPlin()
{
system("cls");
int n, solvable;
double eq[100][101];
printf("Enter the number of equations (maximum 100): ");
scanf("%d", &n);
if (n > 100)
{
printf("Number of equations exceeds the maximum allowed limit.\n");
return;
}
printf("Enter the coefficients of the equations:\n");
for (int i = 0; i < n; i++)
{
printf("Equation %d:\n", i + 1);
for (int j = 0; j < n + 1; j++)
{
printf("Coefficient %d: ", j + 1);
scanf("%lf", &eq[i][j]);
}
}
double soln[100] = {0};
gauss_elim(eq, n, &solvable);
if (solvable == 0)
{
printf("The system of equations is singular or inconsistent.\n");
return;
}
back_subs(eq, soln, n);
printf("Solution:\n");
for (int i = 0; i < n; i++)
printf("x[%d] = %lf\n", i + 1, soln[i]);
return;
}
// Linear Equatons End
// Main Functions Start
void OPmat()
{
system("cls");
printf("Choose Matrix Operation\n");
printf("For Addition press A\n");
printf("For Subtraction press S\n");
printf("For Multiplication press M\n");